Skip to content

Commit 92d2176

Browse files
author
Daniel D. Daugherty
committed
8273967: gtest os.dll_address_to_function_and_library_name_vm fails on macOS12
Reviewed-by: stuefe, gziemski
1 parent a74a839 commit 92d2176

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
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
@@ -838,6 +838,17 @@ int os::current_process_id() {
838838

839839
const char* os::dll_file_extension() { return JNI_LIB_SUFFIX; }
840840

841+
static int local_dladdr(const void* addr, Dl_info* info) {
842+
#ifdef __APPLE__
843+
if (addr == (void*)-1) {
844+
// dladdr() in macOS12/Monterey returns success for -1, but that addr
845+
// value should not be allowed to work to avoid confusion.
846+
return 0;
847+
}
848+
#endif
849+
return dladdr(addr, info);
850+
}
851+
841852
// This must be hard coded because it's the system's temporary
842853
// directory not the java application's temp directory, ala java.io.tmpdir.
843854
#ifdef __APPLE__
@@ -877,19 +888,15 @@ bool os::address_is_in_vm(address addr) {
877888
return false;
878889
}
879890

880-
881-
#define MACH_MAXSYMLEN 256
882-
883891
bool os::dll_address_to_function_name(address addr, char *buf,
884892
int buflen, int *offset,
885893
bool demangle) {
886894
// buf is not optional, but offset is optional
887895
assert(buf != NULL, "sanity check");
888896

889897
Dl_info dlinfo;
890-
char localbuf[MACH_MAXSYMLEN];
891898

892-
if (dladdr((void*)addr, &dlinfo) != 0) {
899+
if (local_dladdr((void*)addr, &dlinfo) != 0) {
893900
// see if we have a matching symbol
894901
if (dlinfo.dli_saddr != NULL && dlinfo.dli_sname != NULL) {
895902
if (!(demangle && Decoder::demangle(dlinfo.dli_sname, buf, buflen))) {
@@ -898,6 +905,14 @@ bool os::dll_address_to_function_name(address addr, char *buf,
898905
if (offset != NULL) *offset = addr - (address)dlinfo.dli_saddr;
899906
return true;
900907
}
908+
909+
#ifndef __APPLE__
910+
// The 6-parameter Decoder::decode() function is not implemented on macOS.
911+
// The Mach-O binary format does not contain a "list of files" with address
912+
// ranges like ELF. That makes sense since Mach-O can contain binaries for
913+
// than one instruction set so there can be more than one address range for
914+
// each "file".
915+
901916
// no matching symbol so try for just file info
902917
if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != NULL) {
903918
if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase),
@@ -906,6 +921,10 @@ bool os::dll_address_to_function_name(address addr, char *buf,
906921
}
907922
}
908923

924+
#else // __APPLE__
925+
#define MACH_MAXSYMLEN 256
926+
927+
char localbuf[MACH_MAXSYMLEN];
909928
// Handle non-dynamic manually:
910929
if (dlinfo.dli_fbase != NULL &&
911930
Decoder::decode(addr, localbuf, MACH_MAXSYMLEN, offset,
@@ -915,6 +934,9 @@ bool os::dll_address_to_function_name(address addr, char *buf,
915934
}
916935
return true;
917936
}
937+
938+
#undef MACH_MAXSYMLEN
939+
#endif // __APPLE__
918940
}
919941
buf[0] = '\0';
920942
if (offset != NULL) *offset = -1;
@@ -929,7 +951,7 @@ bool os::dll_address_to_library_name(address addr, char* buf,
929951

930952
Dl_info dlinfo;
931953

932-
if (dladdr((void*)addr, &dlinfo) != 0) {
954+
if (local_dladdr((void*)addr, &dlinfo) != 0) {
933955
if (dlinfo.dli_fname != NULL) {
934956
jio_snprintf(buf, buflen, "%s", dlinfo.dli_fname);
935957
}

src/hotspot/share/runtime/os.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ bool os::print_function_and_library_name(outputStream* st,
907907
addr = addr2;
908908
}
909909
}
910-
#endif // HANDLE_FUNCTION_DESCRIPTORS
910+
#endif // HAVE_FUNCTION_DESCRIPTORS
911911

912912
if (have_function_name) {
913913
// Print function name, optionally demangled

test/hotspot/gtest/runtime/test_os.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -709,11 +709,7 @@ TEST_VM(os, pagesizes_test_print) {
709709
ASSERT_EQ(strcmp(expected, buffer), 0);
710710
}
711711

712-
#if defined(__APPLE__) // See JDK-8273967.
713-
TEST_VM(os, DISABLED_dll_address_to_function_and_library_name) {
714-
#else
715-
TEST_VM(os, dll_address_to_function_and_library_name) {
716-
#endif
712+
TEST_VM(os, dll_address_to_function_and_library_name) {
717713
char tmp[1024];
718714
char output[1024];
719715
stringStream st(output, sizeof(output));
@@ -726,8 +722,10 @@ TEST_VM(os, pagesizes_test_print) {
726722
#define LOG(...)
727723

728724
// Invalid addresses
725+
LOG("os::print_function_and_library_name(st, -1) expects FALSE.");
729726
address addr = (address)(intptr_t)-1;
730727
EXPECT_FALSE(os::print_function_and_library_name(&st, addr));
728+
LOG("os::print_function_and_library_name(st, NULL) expects FALSE.");
731729
addr = NULL;
732730
EXPECT_FALSE(os::print_function_and_library_name(&st, addr));
733731

0 commit comments

Comments
 (0)