8273967: gtest os.dll_address_to_function_and_library_name_vm fails on macOS12

Reviewed-by: stuefe, gziemski
This commit is contained in:
Daniel D. Daugherty 2021-11-05 17:04:39 +00:00
parent a74a839af0
commit 92d2176362
4 changed files with 38 additions and 12 deletions

View file

@ -52,6 +52,12 @@ bool MachODecoder::demangle(const char* symbol, char *buf, int buflen) {
bool MachODecoder::decode(address addr, char *buf, bool MachODecoder::decode(address addr, char *buf,
int buflen, int *offset, const void *mach_base) { int buflen, int *offset, const void *mach_base) {
if (addr == (address)(intptr_t)-1) {
// dladdr() in macOS12/Monterey returns success for -1, but that addr value
// won't work in this function. Should have been handled by the caller.
ShouldNotReachHere();
return false;
}
struct symtab_command * symt = (struct symtab_command *) struct symtab_command * symt = (struct symtab_command *)
mach_find_command((struct mach_header_64 *)mach_base, LC_SYMTAB); mach_find_command((struct mach_header_64 *)mach_base, LC_SYMTAB);
if (symt == NULL) { if (symt == NULL) {

View file

@ -838,6 +838,17 @@ int os::current_process_id() {
const char* os::dll_file_extension() { return JNI_LIB_SUFFIX; } const char* os::dll_file_extension() { return JNI_LIB_SUFFIX; }
static int local_dladdr(const void* addr, Dl_info* info) {
#ifdef __APPLE__
if (addr == (void*)-1) {
// dladdr() in macOS12/Monterey returns success for -1, but that addr
// value should not be allowed to work to avoid confusion.
return 0;
}
#endif
return dladdr(addr, info);
}
// This must be hard coded because it's the system's temporary // This must be hard coded because it's the system's temporary
// directory not the java application's temp directory, ala java.io.tmpdir. // directory not the java application's temp directory, ala java.io.tmpdir.
#ifdef __APPLE__ #ifdef __APPLE__
@ -877,9 +888,6 @@ bool os::address_is_in_vm(address addr) {
return false; return false;
} }
#define MACH_MAXSYMLEN 256
bool os::dll_address_to_function_name(address addr, char *buf, bool os::dll_address_to_function_name(address addr, char *buf,
int buflen, int *offset, int buflen, int *offset,
bool demangle) { bool demangle) {
@ -887,9 +895,8 @@ bool os::dll_address_to_function_name(address addr, char *buf,
assert(buf != NULL, "sanity check"); assert(buf != NULL, "sanity check");
Dl_info dlinfo; Dl_info dlinfo;
char localbuf[MACH_MAXSYMLEN];
if (dladdr((void*)addr, &dlinfo) != 0) { if (local_dladdr((void*)addr, &dlinfo) != 0) {
// see if we have a matching symbol // see if we have a matching symbol
if (dlinfo.dli_saddr != NULL && dlinfo.dli_sname != NULL) { if (dlinfo.dli_saddr != NULL && dlinfo.dli_sname != NULL) {
if (!(demangle && Decoder::demangle(dlinfo.dli_sname, buf, buflen))) { if (!(demangle && Decoder::demangle(dlinfo.dli_sname, buf, buflen))) {
@ -898,6 +905,14 @@ bool os::dll_address_to_function_name(address addr, char *buf,
if (offset != NULL) *offset = addr - (address)dlinfo.dli_saddr; if (offset != NULL) *offset = addr - (address)dlinfo.dli_saddr;
return true; return true;
} }
#ifndef __APPLE__
// The 6-parameter Decoder::decode() function is not implemented on macOS.
// The Mach-O binary format does not contain a "list of files" with address
// ranges like ELF. That makes sense since Mach-O can contain binaries for
// than one instruction set so there can be more than one address range for
// each "file".
// no matching symbol so try for just file info // no matching symbol so try for just file info
if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != NULL) { if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != NULL) {
if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase), if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase),
@ -906,6 +921,10 @@ bool os::dll_address_to_function_name(address addr, char *buf,
} }
} }
#else // __APPLE__
#define MACH_MAXSYMLEN 256
char localbuf[MACH_MAXSYMLEN];
// Handle non-dynamic manually: // Handle non-dynamic manually:
if (dlinfo.dli_fbase != NULL && if (dlinfo.dli_fbase != NULL &&
Decoder::decode(addr, localbuf, MACH_MAXSYMLEN, offset, Decoder::decode(addr, localbuf, MACH_MAXSYMLEN, offset,
@ -915,6 +934,9 @@ bool os::dll_address_to_function_name(address addr, char *buf,
} }
return true; return true;
} }
#undef MACH_MAXSYMLEN
#endif // __APPLE__
} }
buf[0] = '\0'; buf[0] = '\0';
if (offset != NULL) *offset = -1; if (offset != NULL) *offset = -1;
@ -929,7 +951,7 @@ bool os::dll_address_to_library_name(address addr, char* buf,
Dl_info dlinfo; Dl_info dlinfo;
if (dladdr((void*)addr, &dlinfo) != 0) { if (local_dladdr((void*)addr, &dlinfo) != 0) {
if (dlinfo.dli_fname != NULL) { if (dlinfo.dli_fname != NULL) {
jio_snprintf(buf, buflen, "%s", dlinfo.dli_fname); jio_snprintf(buf, buflen, "%s", dlinfo.dli_fname);
} }

View file

@ -907,7 +907,7 @@ bool os::print_function_and_library_name(outputStream* st,
addr = addr2; addr = addr2;
} }
} }
#endif // HANDLE_FUNCTION_DESCRIPTORS #endif // HAVE_FUNCTION_DESCRIPTORS
if (have_function_name) { if (have_function_name) {
// Print function name, optionally demangled // Print function name, optionally demangled

View file

@ -709,11 +709,7 @@ TEST_VM(os, pagesizes_test_print) {
ASSERT_EQ(strcmp(expected, buffer), 0); ASSERT_EQ(strcmp(expected, buffer), 0);
} }
#if defined(__APPLE__) // See JDK-8273967. TEST_VM(os, dll_address_to_function_and_library_name) {
TEST_VM(os, DISABLED_dll_address_to_function_and_library_name) {
#else
TEST_VM(os, dll_address_to_function_and_library_name) {
#endif
char tmp[1024]; char tmp[1024];
char output[1024]; char output[1024];
stringStream st(output, sizeof(output)); stringStream st(output, sizeof(output));
@ -726,8 +722,10 @@ TEST_VM(os, pagesizes_test_print) {
#define LOG(...) #define LOG(...)
// Invalid addresses // Invalid addresses
LOG("os::print_function_and_library_name(st, -1) expects FALSE.");
address addr = (address)(intptr_t)-1; address addr = (address)(intptr_t)-1;
EXPECT_FALSE(os::print_function_and_library_name(&st, addr)); EXPECT_FALSE(os::print_function_and_library_name(&st, addr));
LOG("os::print_function_and_library_name(st, NULL) expects FALSE.");
addr = NULL; addr = NULL;
EXPECT_FALSE(os::print_function_and_library_name(&st, addr)); EXPECT_FALSE(os::print_function_and_library_name(&st, addr));