Mercurial > hg > graal-compiler
comparison src/os/bsd/vm/os_bsd.cpp @ 17848:6048424d3865
8038201: Clean up misleading usage of malloc() in init_system_properties_values()
Summary: Remove the misleading malloc macro and cleanup the code
Reviewed-by: dsamersoff, kvn
Contributed-by: goetz.lindenmaier@sap.com
author | goetz |
---|---|
date | Thu, 10 Apr 2014 04:07:45 -0700 |
parents | 21dd1c827123 |
children | 917873d2983d 364b73402247 |
comparison
equal
deleted
inserted
replaced
17847:a57ba009d4dc | 17848:6048424d3865 |
---|---|
304 return home_dir; | 304 return home_dir; |
305 } | 305 } |
306 #endif | 306 #endif |
307 | 307 |
308 void os::init_system_properties_values() { | 308 void os::init_system_properties_values() { |
309 // char arch[12]; | |
310 // sysinfo(SI_ARCHITECTURE, arch, sizeof(arch)); | |
311 | |
312 // The next steps are taken in the product version: | 309 // The next steps are taken in the product version: |
313 // | 310 // |
314 // Obtain the JAVA_HOME value from the location of libjvm.so. | 311 // Obtain the JAVA_HOME value from the location of libjvm.so. |
315 // This library should be located at: | 312 // This library should be located at: |
316 // <JAVA_HOME>/jre/lib/<arch>/{client|server}/libjvm.so. | 313 // <JAVA_HOME>/jre/lib/<arch>/{client|server}/libjvm.so. |
333 // Otherwise exit. | 330 // Otherwise exit. |
334 // | 331 // |
335 // Important note: if the location of libjvm.so changes this | 332 // Important note: if the location of libjvm.so changes this |
336 // code needs to be changed accordingly. | 333 // code needs to be changed accordingly. |
337 | 334 |
338 // The next few definitions allow the code to be verbatim: | 335 // See ld(1): |
339 #define malloc(n) (char*)NEW_C_HEAP_ARRAY(char, (n), mtInternal) | 336 // The linker uses the following search paths to locate required |
340 #define getenv(n) ::getenv(n) | 337 // shared libraries: |
341 | 338 // 1: ... |
342 /* | 339 // ... |
343 * See ld(1): | 340 // 7: The default directories, normally /lib and /usr/lib. |
344 * The linker uses the following search paths to locate required | |
345 * shared libraries: | |
346 * 1: ... | |
347 * ... | |
348 * 7: The default directories, normally /lib and /usr/lib. | |
349 */ | |
350 #ifndef DEFAULT_LIBPATH | 341 #ifndef DEFAULT_LIBPATH |
351 #define DEFAULT_LIBPATH "/lib:/usr/lib" | 342 #define DEFAULT_LIBPATH "/lib:/usr/lib" |
352 #endif | 343 #endif |
353 | 344 |
345 // Base path of extensions installed on the system. | |
346 #define SYS_EXT_DIR "/usr/java/packages" | |
354 #define EXTENSIONS_DIR "/lib/ext" | 347 #define EXTENSIONS_DIR "/lib/ext" |
355 #define ENDORSED_DIR "/lib/endorsed" | 348 #define ENDORSED_DIR "/lib/endorsed" |
356 #define REG_DIR "/usr/java/packages" | 349 |
357 | 350 #ifndef __APPLE__ |
358 #ifdef __APPLE__ | 351 |
352 // Buffer that fits several sprintfs. | |
353 // Note that the space for the colon and the trailing null are provided | |
354 // by the nulls included by the sizeof operator. | |
355 const size_t bufsize = | |
356 MAX3((size_t)MAXPATHLEN, // For dll_dir & friends. | |
357 (size_t)MAXPATHLEN + sizeof(EXTENSIONS_DIR) + sizeof(SYS_EXT_DIR) + sizeof(EXTENSIONS_DIR), // extensions dir | |
358 (size_t)MAXPATHLEN + sizeof(ENDORSED_DIR)); // endorsed dir | |
359 char *buf = (char *)NEW_C_HEAP_ARRAY(char, bufsize, mtInternal); | |
360 | |
361 // sysclasspath, java_home, dll_dir | |
362 { | |
363 char *pslash; | |
364 os::jvm_path(buf, bufsize); | |
365 | |
366 // Found the full path to libjvm.so. | |
367 // Now cut the path to <java_home>/jre if we can. | |
368 *(strrchr(buf, '/')) = '\0'; // Get rid of /libjvm.so. | |
369 pslash = strrchr(buf, '/'); | |
370 if (pslash != NULL) { | |
371 *pslash = '\0'; // Get rid of /{client|server|hotspot}. | |
372 } | |
373 Arguments::set_dll_dir(buf); | |
374 | |
375 if (pslash != NULL) { | |
376 pslash = strrchr(buf, '/'); | |
377 if (pslash != NULL) { | |
378 *pslash = '\0'; // Get rid of /<arch>. | |
379 pslash = strrchr(buf, '/'); | |
380 if (pslash != NULL) { | |
381 *pslash = '\0'; // Get rid of /lib. | |
382 } | |
383 } | |
384 } | |
385 Arguments::set_java_home(buf); | |
386 set_boot_path('/', ':'); | |
387 } | |
388 | |
389 // Where to look for native libraries. | |
390 // | |
391 // Note: Due to a legacy implementation, most of the library path | |
392 // is set in the launcher. This was to accomodate linking restrictions | |
393 // on legacy Bsd implementations (which are no longer supported). | |
394 // Eventually, all the library path setting will be done here. | |
395 // | |
396 // However, to prevent the proliferation of improperly built native | |
397 // libraries, the new path component /usr/java/packages is added here. | |
398 // Eventually, all the library path setting will be done here. | |
399 { | |
400 // Get the user setting of LD_LIBRARY_PATH, and prepended it. It | |
401 // should always exist (until the legacy problem cited above is | |
402 // addressed). | |
403 const char *v = ::getenv("LD_LIBRARY_PATH"); | |
404 const char *v_colon = ":"; | |
405 if (v == NULL) { v = ""; v_colon = ""; } | |
406 // That's +1 for the colon and +1 for the trailing '\0'. | |
407 char *ld_library_path = (char *)NEW_C_HEAP_ARRAY(char, | |
408 strlen(v) + 1 + | |
409 sizeof(SYS_EXT_DIR) + sizeof("/lib/") + strlen(cpu_arch) + sizeof(DEFAULT_LIBPATH) + 1, | |
410 mtInternal); | |
411 sprintf(ld_library_path, "%s%s" SYS_EXT_DIR "/lib/%s:" DEFAULT_LIBPATH, v, v_colon, cpu_arch); | |
412 Arguments::set_library_path(ld_library_path); | |
413 FREE_C_HEAP_ARRAY(char, ld_library_path, mtInternal); | |
414 } | |
415 | |
416 // Extensions directories. | |
417 sprintf(buf, "%s" EXTENSIONS_DIR ":" SYS_EXT_DIR EXTENSIONS_DIR, Arguments::get_java_home()); | |
418 Arguments::set_ext_dirs(buf); | |
419 | |
420 // Endorsed standards default directory. | |
421 sprintf(buf, "%s" ENDORSED_DIR, Arguments::get_java_home()); | |
422 Arguments::set_endorsed_dirs(buf); | |
423 | |
424 FREE_C_HEAP_ARRAY(char, buf, mtInternal); | |
425 | |
426 #else // __APPLE__ | |
427 | |
359 #define SYS_EXTENSIONS_DIR "/Library/Java/Extensions" | 428 #define SYS_EXTENSIONS_DIR "/Library/Java/Extensions" |
360 #define SYS_EXTENSIONS_DIRS SYS_EXTENSIONS_DIR ":/Network" SYS_EXTENSIONS_DIR ":/System" SYS_EXTENSIONS_DIR ":/usr/lib/java" | 429 #define SYS_EXTENSIONS_DIRS SYS_EXTENSIONS_DIR ":/Network" SYS_EXTENSIONS_DIR ":/System" SYS_EXTENSIONS_DIR ":/usr/lib/java" |
361 const char *user_home_dir = get_home(); | 430 |
362 // the null in SYS_EXTENSIONS_DIRS counts for the size of the colon after user_home_dir | 431 const char *user_home_dir = get_home(); |
363 int system_ext_size = strlen(user_home_dir) + sizeof(SYS_EXTENSIONS_DIR) + | 432 // The null in SYS_EXTENSIONS_DIRS counts for the size of the colon after user_home_dir. |
364 sizeof(SYS_EXTENSIONS_DIRS); | 433 size_t system_ext_size = strlen(user_home_dir) + sizeof(SYS_EXTENSIONS_DIR) + |
365 #endif | 434 sizeof(SYS_EXTENSIONS_DIRS); |
366 | 435 |
436 // Buffer that fits several sprintfs. | |
437 // Note that the space for the colon and the trailing null are provided | |
438 // by the nulls included by the sizeof operator. | |
439 const size_t bufsize = | |
440 MAX3((size_t)MAXPATHLEN, // for dll_dir & friends. | |
441 (size_t)MAXPATHLEN + sizeof(EXTENSIONS_DIR) + system_ext_size, // extensions dir | |
442 (size_t)MAXPATHLEN + sizeof(ENDORSED_DIR)); // endorsed dir | |
443 char *buf = (char *)NEW_C_HEAP_ARRAY(char, bufsize, mtInternal); | |
444 | |
445 // sysclasspath, java_home, dll_dir | |
367 { | 446 { |
368 /* sysclasspath, java_home, dll_dir */ | 447 char *pslash; |
369 { | 448 os::jvm_path(buf, bufsize); |
370 char *home_path; | 449 |
371 char *dll_path; | 450 // Found the full path to libjvm.so. |
372 char *pslash; | 451 // Now cut the path to <java_home>/jre if we can. |
373 char buf[MAXPATHLEN]; | 452 *(strrchr(buf, '/')) = '\0'; // Get rid of /libjvm.so. |
374 os::jvm_path(buf, sizeof(buf)); | 453 pslash = strrchr(buf, '/'); |
375 | 454 if (pslash != NULL) { |
376 // Found the full path to libjvm.so. | 455 *pslash = '\0'; // Get rid of /{client|server|hotspot}. |
377 // Now cut the path to <java_home>/jre if we can. | 456 } |
378 *(strrchr(buf, '/')) = '\0'; /* get rid of /libjvm.so */ | 457 Arguments::set_dll_dir(buf); |
379 pslash = strrchr(buf, '/'); | 458 |
380 if (pslash != NULL) | 459 if (pslash != NULL) { |
381 *pslash = '\0'; /* get rid of /{client|server|hotspot} */ | 460 pslash = strrchr(buf, '/'); |
382 dll_path = malloc(strlen(buf) + 1); | 461 if (pslash != NULL) { |
383 if (dll_path == NULL) | 462 *pslash = '\0'; // Get rid of /lib. |
384 return; | 463 } |
385 strcpy(dll_path, buf); | 464 } |
386 Arguments::set_dll_dir(dll_path); | 465 Arguments::set_java_home(buf); |
387 | 466 set_boot_path('/', ':'); |
388 if (pslash != NULL) { | 467 } |
389 pslash = strrchr(buf, '/'); | 468 |
390 if (pslash != NULL) { | 469 // Where to look for native libraries. |
391 *pslash = '\0'; /* get rid of /<arch> (/lib on macosx) */ | 470 // |
392 #ifndef __APPLE__ | 471 // Note: Due to a legacy implementation, most of the library path |
393 pslash = strrchr(buf, '/'); | 472 // is set in the launcher. This was to accomodate linking restrictions |
394 if (pslash != NULL) | 473 // on legacy Bsd implementations (which are no longer supported). |
395 *pslash = '\0'; /* get rid of /lib */ | 474 // Eventually, all the library path setting will be done here. |
396 #endif | 475 // |
397 } | 476 // However, to prevent the proliferation of improperly built native |
398 } | 477 // libraries, the new path component /usr/java/packages is added here. |
399 | 478 // Eventually, all the library path setting will be done here. |
400 home_path = malloc(strlen(buf) + 1); | 479 { |
401 if (home_path == NULL) | 480 // Get the user setting of LD_LIBRARY_PATH, and prepended it. It |
402 return; | 481 // should always exist (until the legacy problem cited above is |
403 strcpy(home_path, buf); | 482 // addressed). |
404 Arguments::set_java_home(home_path); | 483 // Prepend the default path with the JAVA_LIBRARY_PATH so that the app launcher code |
405 | 484 // can specify a directory inside an app wrapper |
406 if (!set_boot_path('/', ':')) | 485 const char *l = ::getenv("JAVA_LIBRARY_PATH"); |
407 return; | 486 const char *l_colon = ":"; |
408 } | 487 if (l == NULL) { l = ""; l_colon = ""; } |
409 | 488 |
410 /* | 489 const char *v = ::getenv("DYLD_LIBRARY_PATH"); |
411 * Where to look for native libraries | 490 const char *v_colon = ":"; |
412 * | 491 if (v == NULL) { v = ""; v_colon = ""; } |
413 * Note: Due to a legacy implementation, most of the library path | 492 |
414 * is set in the launcher. This was to accomodate linking restrictions | 493 // Apple's Java6 has "." at the beginning of java.library.path. |
415 * on legacy Bsd implementations (which are no longer supported). | 494 // OpenJDK on Windows has "." at the end of java.library.path. |
416 * Eventually, all the library path setting will be done here. | 495 // OpenJDK on Linux and Solaris don't have "." in java.library.path |
417 * | 496 // at all. To ease the transition from Apple's Java6 to OpenJDK7, |
418 * However, to prevent the proliferation of improperly built native | 497 // "." is appended to the end of java.library.path. Yes, this |
419 * libraries, the new path component /usr/java/packages is added here. | 498 // could cause a change in behavior, but Apple's Java6 behavior |
420 * Eventually, all the library path setting will be done here. | 499 // can be achieved by putting "." at the beginning of the |
421 */ | 500 // JAVA_LIBRARY_PATH environment variable. |
422 { | 501 char *ld_library_path = (char *)NEW_C_HEAP_ARRAY(char, |
423 char *ld_library_path; | 502 strlen(v) + 1 + strlen(l) + 1 + |
424 | 503 system_ext_size + 3, |
425 /* | 504 mtInternal); |
426 * Construct the invariant part of ld_library_path. Note that the | 505 sprintf(ld_library_path, "%s%s%s%s%s" SYS_EXTENSIONS_DIR ":" SYS_EXTENSIONS_DIRS ":.", |
427 * space for the colon and the trailing null are provided by the | 506 v, v_colon, l, l_colon, user_home_dir); |
428 * nulls included by the sizeof operator (so actually we allocate | 507 Arguments::set_library_path(ld_library_path); |
429 * a byte more than necessary). | 508 FREE_C_HEAP_ARRAY(char, ld_library_path, mtInternal); |
430 */ | 509 } |
431 #ifdef __APPLE__ | 510 |
432 ld_library_path = (char *) malloc(system_ext_size); | 511 // Extensions directories. |
433 sprintf(ld_library_path, "%s" SYS_EXTENSIONS_DIR ":" SYS_EXTENSIONS_DIRS, user_home_dir); | 512 // |
434 #else | 513 // Note that the space for the colon and the trailing null are provided |
435 ld_library_path = (char *) malloc(sizeof(REG_DIR) + sizeof("/lib/") + | 514 // by the nulls included by the sizeof operator (so actually one byte more |
436 strlen(cpu_arch) + sizeof(DEFAULT_LIBPATH)); | 515 // than necessary is allocated). |
437 sprintf(ld_library_path, REG_DIR "/lib/%s:" DEFAULT_LIBPATH, cpu_arch); | 516 sprintf(buf, "%s" SYS_EXTENSIONS_DIR ":%s" EXTENSIONS_DIR ":" SYS_EXTENSIONS_DIRS, |
438 #endif | 517 user_home_dir, Arguments::get_java_home()); |
439 | 518 Arguments::set_ext_dirs(buf); |
440 /* | 519 |
441 * Get the user setting of LD_LIBRARY_PATH, and prepended it. It | 520 // Endorsed standards default directory. |
442 * should always exist (until the legacy problem cited above is | 521 sprintf(buf, "%s" ENDORSED_DIR, Arguments::get_java_home()); |
443 * addressed). | 522 Arguments::set_endorsed_dirs(buf); |
444 */ | 523 |
445 #ifdef __APPLE__ | 524 FREE_C_HEAP_ARRAY(char, buf, mtInternal); |
446 // Prepend the default path with the JAVA_LIBRARY_PATH so that the app launcher code can specify a directory inside an app wrapper | 525 |
447 char *l = getenv("JAVA_LIBRARY_PATH"); | |
448 if (l != NULL) { | |
449 char *t = ld_library_path; | |
450 /* That's +1 for the colon and +1 for the trailing '\0' */ | |
451 ld_library_path = (char *) malloc(strlen(l) + 1 + strlen(t) + 1); | |
452 sprintf(ld_library_path, "%s:%s", l, t); | |
453 free(t); | |
454 } | |
455 | |
456 char *v = getenv("DYLD_LIBRARY_PATH"); | |
457 #else | |
458 char *v = getenv("LD_LIBRARY_PATH"); | |
459 #endif | |
460 if (v != NULL) { | |
461 char *t = ld_library_path; | |
462 /* That's +1 for the colon and +1 for the trailing '\0' */ | |
463 ld_library_path = (char *) malloc(strlen(v) + 1 + strlen(t) + 1); | |
464 sprintf(ld_library_path, "%s:%s", v, t); | |
465 free(t); | |
466 } | |
467 | |
468 #ifdef __APPLE__ | |
469 // Apple's Java6 has "." at the beginning of java.library.path. | |
470 // OpenJDK on Windows has "." at the end of java.library.path. | |
471 // OpenJDK on Linux and Solaris don't have "." in java.library.path | |
472 // at all. To ease the transition from Apple's Java6 to OpenJDK7, | |
473 // "." is appended to the end of java.library.path. Yes, this | |
474 // could cause a change in behavior, but Apple's Java6 behavior | |
475 // can be achieved by putting "." at the beginning of the | |
476 // JAVA_LIBRARY_PATH environment variable. | |
477 { | |
478 char *t = ld_library_path; | |
479 // that's +3 for appending ":." and the trailing '\0' | |
480 ld_library_path = (char *) malloc(strlen(t) + 3); | |
481 sprintf(ld_library_path, "%s:%s", t, "."); | |
482 free(t); | |
483 } | |
484 #endif | |
485 | |
486 Arguments::set_library_path(ld_library_path); | |
487 } | |
488 | |
489 /* | |
490 * Extensions directories. | |
491 * | |
492 * Note that the space for the colon and the trailing null are provided | |
493 * by the nulls included by the sizeof operator (so actually one byte more | |
494 * than necessary is allocated). | |
495 */ | |
496 { | |
497 #ifdef __APPLE__ | |
498 char *buf = malloc(strlen(Arguments::get_java_home()) + | |
499 sizeof(EXTENSIONS_DIR) + system_ext_size); | |
500 sprintf(buf, "%s" SYS_EXTENSIONS_DIR ":%s" EXTENSIONS_DIR ":" | |
501 SYS_EXTENSIONS_DIRS, user_home_dir, Arguments::get_java_home()); | |
502 #else | |
503 char *buf = malloc(strlen(Arguments::get_java_home()) + | |
504 sizeof(EXTENSIONS_DIR) + sizeof(REG_DIR) + sizeof(EXTENSIONS_DIR)); | |
505 sprintf(buf, "%s" EXTENSIONS_DIR ":" REG_DIR EXTENSIONS_DIR, | |
506 Arguments::get_java_home()); | |
507 #endif | |
508 | |
509 Arguments::set_ext_dirs(buf); | |
510 } | |
511 | |
512 /* Endorsed standards default directory. */ | |
513 { | |
514 char * buf; | |
515 buf = malloc(strlen(Arguments::get_java_home()) + sizeof(ENDORSED_DIR)); | |
516 sprintf(buf, "%s" ENDORSED_DIR, Arguments::get_java_home()); | |
517 Arguments::set_endorsed_dirs(buf); | |
518 } | |
519 } | |
520 | |
521 #ifdef __APPLE__ | |
522 #undef SYS_EXTENSIONS_DIR | 526 #undef SYS_EXTENSIONS_DIR |
523 #endif | 527 #undef SYS_EXTENSIONS_DIRS |
524 #undef malloc | 528 |
525 #undef getenv | 529 #endif // __APPLE__ |
530 | |
531 #undef SYS_EXT_DIR | |
526 #undef EXTENSIONS_DIR | 532 #undef EXTENSIONS_DIR |
527 #undef ENDORSED_DIR | 533 #undef ENDORSED_DIR |
528 | |
529 // Done | |
530 return; | |
531 } | 534 } |
532 | 535 |
533 //////////////////////////////////////////////////////////////////////////////// | 536 //////////////////////////////////////////////////////////////////////////////// |
534 // breakpoint support | 537 // breakpoint support |
535 | 538 |
3221 sigAct.sa_flags = SA_SIGINFO|SA_RESTART; | 3224 sigAct.sa_flags = SA_SIGINFO|SA_RESTART; |
3222 } else { | 3225 } else { |
3223 sigAct.sa_sigaction = signalHandler; | 3226 sigAct.sa_sigaction = signalHandler; |
3224 sigAct.sa_flags = SA_SIGINFO|SA_RESTART; | 3227 sigAct.sa_flags = SA_SIGINFO|SA_RESTART; |
3225 } | 3228 } |
3226 #if __APPLE__ | 3229 #ifdef __APPLE__ |
3227 // Needed for main thread as XNU (Mac OS X kernel) will only deliver SIGSEGV | 3230 // Needed for main thread as XNU (Mac OS X kernel) will only deliver SIGSEGV |
3228 // (which starts as SIGBUS) on main thread with faulting address inside "stack+guard pages" | 3231 // (which starts as SIGBUS) on main thread with faulting address inside "stack+guard pages" |
3229 // if the signal handler declares it will handle it on alternate stack. | 3232 // if the signal handler declares it will handle it on alternate stack. |
3230 // Notice we only declare we will handle it on alt stack, but we are not | 3233 // Notice we only declare we will handle it on alt stack, but we are not |
3231 // actually going to use real alt stack - this is just a workaround. | 3234 // actually going to use real alt stack - this is just a workaround. |