8074895: os::getenv is inadequate

Reviewed-by: dholmes, coleenp
This commit is contained in:
Jeremy Manson 2015-03-30 17:32:18 -04:00
parent 67e0ba13c0
commit 0353353bde
11 changed files with 106 additions and 141 deletions

View File

@ -257,19 +257,6 @@ julong os::physical_memory() {
return Aix::physical_memory();
}
////////////////////////////////////////////////////////////////////////////////
// environment support
bool os::getenv(const char* name, char* buf, int len) {
const char* val = ::getenv(name);
if (val != NULL && strlen(val) < (size_t)len) {
strcpy(buf, val);
return true;
}
if (len > 0) buf[0] = 0; // return a null string
return false;
}
// Return true if user is running as root.
bool os::have_special_privileges() {

View File

@ -190,20 +190,6 @@ julong os::physical_memory() {
return Bsd::physical_memory();
}
////////////////////////////////////////////////////////////////////////////////
// environment support
bool os::getenv(const char* name, char* buf, int len) {
const char* val = ::getenv(name);
if (val != NULL && strlen(val) < (size_t)len) {
strcpy(buf, val);
return true;
}
if (len > 0) buf[0] = 0; // return a null string
return false;
}
// Return true if user is running as root.
bool os::have_special_privileges() {

View File

@ -184,20 +184,6 @@ julong os::physical_memory() {
return Linux::physical_memory();
}
////////////////////////////////////////////////////////////////////////////////
// environment support
bool os::getenv(const char* name, char* buf, int len) {
const char* val = ::getenv(name);
if (val != NULL && strlen(val) < (size_t)len) {
strcpy(buf, val);
return true;
}
if (len > 0) buf[0] = 0; // return a null string
return false;
}
// Return true if user is running as root.
bool os::have_special_privileges() {

View File

@ -555,17 +555,6 @@ bool os::bind_to_processor(uint processor_id) {
return (bind_result == 0);
}
bool os::getenv(const char* name, char* buffer, int len) {
char* val = ::getenv(name);
if (val == NULL || strlen(val) + 1 > len) {
if (len > 0) buffer[0] = 0; // return a null string
return false;
}
strcpy(buffer, val);
return true;
}
// Return true if user is running as root.
bool os::have_special_privileges() {

View File

@ -153,11 +153,6 @@ static inline double fileTimeAsDouble(FILETIME* time) {
// Implementation of os
bool os::getenv(const char* name, char* buffer, int len) {
int result = GetEnvironmentVariable(name, buffer, len);
return result > 0 && result < len;
}
bool os::unsetenv(const char* name) {
assert(name != NULL, "Null pointer");
return (SetEnvironmentVariable(name, NULL) == TRUE);
@ -188,9 +183,13 @@ void os::init_system_properties_values() {
char *dll_path;
char *pslash;
char *bin = "\\bin";
char home_dir[MAX_PATH];
char home_dir[MAX_PATH + 1];
char *alt_home_dir = ::getenv("_ALT_JAVA_HOME_DIR");
if (!getenv("_ALT_JAVA_HOME_DIR", home_dir, MAX_PATH)) {
if (alt_home_dir != NULL) {
strncpy(home_dir, alt_home_dir, MAX_PATH + 1);
home_dir[MAX_PATH] = '\0';
} else {
os::jvm_path(home_dir, sizeof(home_dir));
// Found the full path to jvm.dll.
// Now cut the path to <java_home>/jre if we can.
@ -5930,4 +5929,3 @@ void TestReserveMemorySpecial_test() {
UseNUMAInterleaving = old_use_numa_interleaving;
}
#endif // PRODUCT

View File

@ -3455,15 +3455,16 @@ jint Arguments::finalize_vm_init_args(SysClassPath* scp_p, bool scp_assembly_req
if (os::is_headless_jre()) {
const char* headless = Arguments::get_property("java.awt.headless");
if (headless == NULL) {
char envbuffer[128];
if (!os::getenv("JAVA_AWT_HEADLESS", envbuffer, sizeof(envbuffer))) {
const char *headless_env = ::getenv("JAVA_AWT_HEADLESS");
if (headless_env == NULL) {
if (!add_property("java.awt.headless=true")) {
return JNI_ENOMEM;
}
} else {
char buffer[256];
strcpy(buffer, "java.awt.headless=");
strcat(buffer, envbuffer);
const char *key = "java.awt.headless=";
strcpy(buffer, key);
strncat(buffer, headless_env, 256 - strlen(key) - 1);
if (!add_property(buffer)) {
return JNI_ENOMEM;
}
@ -3494,75 +3495,91 @@ jint Arguments::parse_java_tool_options_environment_variable(SysClassPath* scp_p
}
jint Arguments::parse_options_environment_variable(const char* name, SysClassPath* scp_p, bool* scp_assembly_required_p) {
const int N_MAX_OPTIONS = 64;
const int OPTION_BUFFER_SIZE = 1024;
char buffer[OPTION_BUFFER_SIZE];
char *buffer = ::getenv(name);
// The variable will be ignored if it exceeds the length of the buffer.
// Don't check this variable if user has special privileges
// (e.g. unix su command).
if (os::getenv(name, buffer, sizeof(buffer)) &&
!os::have_special_privileges()) {
JavaVMOption options[N_MAX_OPTIONS]; // Construct option array
jio_fprintf(defaultStream::error_stream(),
"Picked up %s: %s\n", name, buffer);
char* rd = buffer; // pointer to the input string (rd)
int i;
for (i = 0; i < N_MAX_OPTIONS;) { // repeat for all options in the input string
while (isspace(*rd)) rd++; // skip whitespace
if (*rd == 0) break; // we re done when the input string is read completely
// The output, option string, overwrites the input string.
// Because of quoting, the pointer to the option string (wrt) may lag the pointer to
// input string (rd).
char* wrt = rd;
options[i++].optionString = wrt; // Fill in option
while (*rd != 0 && !isspace(*rd)) { // unquoted strings terminate with a space or NULL
if (*rd == '\'' || *rd == '"') { // handle a quoted string
int quote = *rd; // matching quote to look for
rd++; // don't copy open quote
while (*rd != quote) { // include everything (even spaces) up until quote
if (*rd == 0) { // string termination means unmatched string
jio_fprintf(defaultStream::error_stream(),
"Unmatched quote in %s\n", name);
return JNI_ERR;
}
*wrt++ = *rd++; // copy to option string
}
rd++; // don't copy close quote
} else {
*wrt++ = *rd++; // copy to option string
}
}
// Need to check if we're done before writing a NULL,
// because the write could be to the byte that rd is pointing to.
if (*rd++ == 0) {
*wrt = 0;
break;
}
*wrt = 0; // Zero terminate option
}
// Construct JavaVMInitArgs structure and parse as if it was part of the command line
JavaVMInitArgs vm_args;
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.nOptions = i;
vm_args.ignoreUnrecognized = IgnoreUnrecognizedVMOptions;
if (PrintVMOptions) {
const char* tail;
for (int i = 0; i < vm_args.nOptions; i++) {
const JavaVMOption *option = vm_args.options + i;
if (match_option(option, "-XX:", &tail)) {
logOption(tail);
}
}
}
return(parse_each_vm_init_arg(&vm_args, scp_p, scp_assembly_required_p, Flag::ENVIRON_VAR));
if (buffer == NULL || os::have_special_privileges()) {
return JNI_OK;
}
return JNI_OK;
if ((buffer = os::strdup(buffer)) == NULL) {
return JNI_ENOMEM;
}
GrowableArray<JavaVMOption> options(2, true); // Construct option array
jio_fprintf(defaultStream::error_stream(),
"Picked up %s: %s\n", name, buffer);
char* rd = buffer; // pointer to the input string (rd)
while (true) { // repeat for all options in the input string
while (isspace(*rd)) rd++; // skip whitespace
if (*rd == 0) break; // we re done when the input string is read completely
// The output, option string, overwrites the input string.
// Because of quoting, the pointer to the option string (wrt) may lag the pointer to
// input string (rd).
char* wrt = rd;
JavaVMOption option;
option.optionString = wrt;
options.append(option); // Fill in option
while (*rd != 0 && !isspace(*rd)) { // unquoted strings terminate with a space or NULL
if (*rd == '\'' || *rd == '"') { // handle a quoted string
int quote = *rd; // matching quote to look for
rd++; // don't copy open quote
while (*rd != quote) { // include everything (even spaces) up until quote
if (*rd == 0) { // string termination means unmatched string
jio_fprintf(defaultStream::error_stream(),
"Unmatched quote in %s\n", name);
os::free(buffer);
return JNI_ERR;
}
*wrt++ = *rd++; // copy to option string
}
rd++; // don't copy close quote
} else {
*wrt++ = *rd++; // copy to option string
}
}
// Need to check if we're done before writing a NULL,
// because the write could be to the byte that rd is pointing to.
if (*rd++ == 0) {
*wrt = 0;
break;
}
*wrt = 0; // Zero terminate option
}
JavaVMOption* options_arr =
NEW_C_HEAP_ARRAY_RETURN_NULL(JavaVMOption, options.length(), mtInternal);
if (options_arr == NULL) {
return JNI_ENOMEM;
}
for (int i = 0; i < options.length(); i++) {
options_arr[i] = options.at(i);
}
// Construct JavaVMInitArgs structure and parse as if it was part of the command line
JavaVMInitArgs vm_args;
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options_arr;
vm_args.nOptions = options.length();
vm_args.ignoreUnrecognized = IgnoreUnrecognizedVMOptions;
if (PrintVMOptions) {
const char* tail;
for (int i = 0; i < vm_args.nOptions; i++) {
const JavaVMOption *option = vm_args.options + i;
if (match_option(option, "-XX:", &tail)) {
logOption(tail);
}
}
}
jint result = parse_each_vm_init_arg(&vm_args, scp_p, scp_assembly_required_p,
Flag::ENVIRON_VAR);
FREE_C_HEAP_ARRAY(JavaVMOption, options_arr);
os::free(buffer);
return result;
}
void Arguments::set_shared_spaces_flags() {

View File

@ -813,16 +813,16 @@ void os::print_hex_dump(outputStream* st, address start, address end, int unitsi
st->cr();
}
void os::print_environment_variables(outputStream* st, const char** env_list,
char* buffer, int len) {
void os::print_environment_variables(outputStream* st, const char** env_list) {
if (env_list) {
st->print_cr("Environment Variables:");
for (int i = 0; env_list[i] != NULL; i++) {
if (getenv(env_list[i], buffer, len)) {
char *envvar = ::getenv(env_list[i]);
if (envvar != NULL) {
st->print("%s", env_list[i]);
st->print("=");
st->print_cr("%s", buffer);
st->print_cr("%s", envvar);
}
}
}

View File

@ -164,8 +164,7 @@ class os: AllStatic {
// Override me as needed
static int file_name_strcmp(const char* s1, const char* s2);
// get/unset environment variable
static bool getenv(const char* name, char* buffer, int len);
// unset environment variable
static bool unsetenv(const char* name);
static bool have_special_privileges();
@ -591,7 +590,7 @@ class os: AllStatic {
static void pd_print_cpu_info(outputStream* st);
static void print_memory_info(outputStream* st);
static void print_dll_info(outputStream* st);
static void print_environment_variables(outputStream* st, const char** env_list, char* buffer, int len);
static void print_environment_variables(outputStream* st, const char** env_list);
static void print_context(outputStream* st, void* context);
static void print_register_info(outputStream* st, void* context);
static void print_siginfo(outputStream* st, void* siginfo);

View File

@ -47,9 +47,9 @@ bool MemTracker::_is_nmt_env_valid = true;
NMT_TrackingLevel MemTracker::init_tracking_level() {
NMT_TrackingLevel level = NMT_off;
char buf[64];
char nmt_option[64];
jio_snprintf(buf, sizeof(buf), "NMT_LEVEL_%d", os::current_process_id());
if (os::getenv(buf, nmt_option, sizeof(nmt_option))) {
const char *nmt_option = ::getenv(buf);
if (nmt_option != NULL) {
if (strcmp(nmt_option, "summary") == 0) {
level = NMT_summary;
} else if (strcmp(nmt_option, "detail") == 0) {
@ -311,4 +311,3 @@ void MemTracker::tuning_statistics(outputStream* out) {
out->print_cr(" ");
walker.report_statistics(out);
}

View File

@ -168,6 +168,8 @@ template<class E> class GrowableArray : public GenericGrowableArray {
GrowableArray(int initial_size, bool C_heap = false, MEMFLAGS F = mtInternal)
: GenericGrowableArray(initial_size, 0, C_heap, F) {
_data = (E*)raw_allocate(sizeof(E));
// Needed for Visual Studio 2012 and older
#pragma warning(suppress: 4345)
for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
}
@ -385,6 +387,8 @@ template<class E> void GrowableArray<E>::grow(int j) {
E* newData = (E*)raw_allocate(sizeof(E));
int i = 0;
for ( ; i < _len; i++) ::new ((void*)&newData[i]) E(_data[i]);
// Needed for Visual Studio 2012 and older
#pragma warning(suppress: 4345)
for ( ; i < _max; i++) ::new ((void*)&newData[i]) E();
for (i = 0; i < old_max; i++) _data[i].~E();
if (on_C_heap() && _data != NULL) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -768,7 +768,7 @@ void VMError::report(outputStream* st) {
STEP(220, "(printing environment variables)" )
if (_verbose) {
os::print_environment_variables(st, env_list, buf, sizeof(buf));
os::print_environment_variables(st, env_list);
st->cr();
}