Skip to content

Commit

Permalink
8273967: gtest os.dll_address_to_function_and_library_name_vm fails o…
Browse files Browse the repository at this point in the history
…n macOS12

Reviewed-by: goetz
Backport-of: 92d2176362954a7093894057748056610eeafe4b
  • Loading branch information
parttimenerd authored and RealCLanger committed Jan 14, 2022
1 parent 4b5b58d commit 0130fdc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/hotspot/os/bsd/decoder_machO.cpp
Expand Up @@ -52,6 +52,12 @@ bool MachODecoder::demangle(const char* symbol, char *buf, int buflen) {

bool MachODecoder::decode(address addr, char *buf,
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 *)
mach_find_command((struct mach_header_64 *)mach_base, LC_SYMTAB);
if (symt == NULL) {
Expand Down
34 changes: 28 additions & 6 deletions src/hotspot/os/bsd/os_bsd.cpp
Expand Up @@ -903,6 +903,17 @@ int os::current_process_id() {

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
// directory not the java application's temp directory, ala java.io.tmpdir.
#ifdef __APPLE__
Expand Down Expand Up @@ -942,19 +953,15 @@ bool os::address_is_in_vm(address addr) {
return false;
}


#define MACH_MAXSYMLEN 256

bool os::dll_address_to_function_name(address addr, char *buf,
int buflen, int *offset,
bool demangle) {
// buf is not optional, but offset is optional
assert(buf != NULL, "sanity check");

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
if (dlinfo.dli_saddr != NULL && dlinfo.dli_sname != NULL) {
if (!(demangle && Decoder::demangle(dlinfo.dli_sname, buf, buflen))) {
Expand All @@ -963,6 +970,14 @@ bool os::dll_address_to_function_name(address addr, char *buf,
if (offset != NULL) *offset = addr - (address)dlinfo.dli_saddr;
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
if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != NULL) {
if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase),
Expand All @@ -971,6 +986,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:
if (dlinfo.dli_fbase != NULL &&
Decoder::decode(addr, localbuf, MACH_MAXSYMLEN, offset,
Expand All @@ -980,6 +999,9 @@ bool os::dll_address_to_function_name(address addr, char *buf,
}
return true;
}

#undef MACH_MAXSYMLEN
#endif // __APPLE__
}
buf[0] = '\0';
if (offset != NULL) *offset = -1;
Expand All @@ -994,7 +1016,7 @@ bool os::dll_address_to_library_name(address addr, char* buf,

Dl_info dlinfo;

if (dladdr((void*)addr, &dlinfo) != 0) {
if (local_dladdr((void*)addr, &dlinfo) != 0) {
if (dlinfo.dli_fname != NULL) {
jio_snprintf(buf, buflen, "%s", dlinfo.dli_fname);
}
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/runtime/os.cpp
Expand Up @@ -916,7 +916,7 @@ bool os::print_function_and_library_name(outputStream* st,
addr = addr2;
}
}
#endif // HANDLE_FUNCTION_DESCRIPTORS
#endif // HAVE_FUNCTION_DESCRIPTORS

if (have_function_name) {
// Print function name, optionally demangled
Expand Down
2 changes: 2 additions & 0 deletions test/hotspot/gtest/runtime/test_os.cpp
Expand Up @@ -715,8 +715,10 @@ TEST_VM(os, dll_address_to_function_and_library_name) {
#define LOG(...)

// Invalid addresses
LOG("os::print_function_and_library_name(st, -1) expects FALSE.");
address addr = (address)(intptr_t)-1;
EXPECT_FALSE(os::print_function_and_library_name(&st, addr));
LOG("os::print_function_and_library_name(st, NULL) expects FALSE.");
addr = NULL;
EXPECT_FALSE(os::print_function_and_library_name(&st, addr));

Expand Down

1 comment on commit 0130fdc

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.