Skip to content

Commit 0130fdc

Browse files
parttimenerdRealCLanger
authored andcommitted
8273967: gtest os.dll_address_to_function_and_library_name_vm fails on macOS12
Reviewed-by: goetz Backport-of: 92d2176362954a7093894057748056610eeafe4b
1 parent 4b5b58d commit 0130fdc

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

src/hotspot/os/bsd/decoder_machO.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ bool MachODecoder::demangle(const char* symbol, char *buf, int buflen) {
5252

5353
bool MachODecoder::decode(address addr, char *buf,
5454
int buflen, int *offset, const void *mach_base) {
55+
if (addr == (address)(intptr_t)-1) {
56+
// dladdr() in macOS12/Monterey returns success for -1, but that addr value
57+
// won't work in this function. Should have been handled by the caller.
58+
ShouldNotReachHere();
59+
return false;
60+
}
5561
struct symtab_command * symt = (struct symtab_command *)
5662
mach_find_command((struct mach_header_64 *)mach_base, LC_SYMTAB);
5763
if (symt == NULL) {

src/hotspot/os/bsd/os_bsd.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,17 @@ int os::current_process_id() {
903903

904904
const char* os::dll_file_extension() { return JNI_LIB_SUFFIX; }
905905

906+
static int local_dladdr(const void* addr, Dl_info* info) {
907+
#ifdef __APPLE__
908+
if (addr == (void*)-1) {
909+
// dladdr() in macOS12/Monterey returns success for -1, but that addr
910+
// value should not be allowed to work to avoid confusion.
911+
return 0;
912+
}
913+
#endif
914+
return dladdr(addr, info);
915+
}
916+
906917
// This must be hard coded because it's the system's temporary
907918
// directory not the java application's temp directory, ala java.io.tmpdir.
908919
#ifdef __APPLE__
@@ -942,19 +953,15 @@ bool os::address_is_in_vm(address addr) {
942953
return false;
943954
}
944955

945-
946-
#define MACH_MAXSYMLEN 256
947-
948956
bool os::dll_address_to_function_name(address addr, char *buf,
949957
int buflen, int *offset,
950958
bool demangle) {
951959
// buf is not optional, but offset is optional
952960
assert(buf != NULL, "sanity check");
953961

954962
Dl_info dlinfo;
955-
char localbuf[MACH_MAXSYMLEN];
956963

957-
if (dladdr((void*)addr, &dlinfo) != 0) {
964+
if (local_dladdr((void*)addr, &dlinfo) != 0) {
958965
// see if we have a matching symbol
959966
if (dlinfo.dli_saddr != NULL && dlinfo.dli_sname != NULL) {
960967
if (!(demangle && Decoder::demangle(dlinfo.dli_sname, buf, buflen))) {
@@ -963,6 +970,14 @@ bool os::dll_address_to_function_name(address addr, char *buf,
963970
if (offset != NULL) *offset = addr - (address)dlinfo.dli_saddr;
964971
return true;
965972
}
973+
974+
#ifndef __APPLE__
975+
// The 6-parameter Decoder::decode() function is not implemented on macOS.
976+
// The Mach-O binary format does not contain a "list of files" with address
977+
// ranges like ELF. That makes sense since Mach-O can contain binaries for
978+
// than one instruction set so there can be more than one address range for
979+
// each "file".
980+
966981
// no matching symbol so try for just file info
967982
if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != NULL) {
968983
if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase),
@@ -971,6 +986,10 @@ bool os::dll_address_to_function_name(address addr, char *buf,
971986
}
972987
}
973988

989+
#else // __APPLE__
990+
#define MACH_MAXSYMLEN 256
991+
992+
char localbuf[MACH_MAXSYMLEN];
974993
// Handle non-dynamic manually:
975994
if (dlinfo.dli_fbase != NULL &&
976995
Decoder::decode(addr, localbuf, MACH_MAXSYMLEN, offset,
@@ -980,6 +999,9 @@ bool os::dll_address_to_function_name(address addr, char *buf,
980999
}
9811000
return true;
9821001
}
1002+
1003+
#undef MACH_MAXSYMLEN
1004+
#endif // __APPLE__
9831005
}
9841006
buf[0] = '\0';
9851007
if (offset != NULL) *offset = -1;
@@ -994,7 +1016,7 @@ bool os::dll_address_to_library_name(address addr, char* buf,
9941016

9951017
Dl_info dlinfo;
9961018

997-
if (dladdr((void*)addr, &dlinfo) != 0) {
1019+
if (local_dladdr((void*)addr, &dlinfo) != 0) {
9981020
if (dlinfo.dli_fname != NULL) {
9991021
jio_snprintf(buf, buflen, "%s", dlinfo.dli_fname);
10001022
}

src/hotspot/share/runtime/os.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ bool os::print_function_and_library_name(outputStream* st,
916916
addr = addr2;
917917
}
918918
}
919-
#endif // HANDLE_FUNCTION_DESCRIPTORS
919+
#endif // HAVE_FUNCTION_DESCRIPTORS
920920

921921
if (have_function_name) {
922922
// Print function name, optionally demangled

test/hotspot/gtest/runtime/test_os.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,8 +715,10 @@ TEST_VM(os, dll_address_to_function_and_library_name) {
715715
#define LOG(...)
716716

717717
// Invalid addresses
718+
LOG("os::print_function_and_library_name(st, -1) expects FALSE.");
718719
address addr = (address)(intptr_t)-1;
719720
EXPECT_FALSE(os::print_function_and_library_name(&st, addr));
721+
LOG("os::print_function_and_library_name(st, NULL) expects FALSE.");
720722
addr = NULL;
721723
EXPECT_FALSE(os::print_function_and_library_name(&st, addr));
722724

0 commit comments

Comments
 (0)