Skip to content

Commit 055f3c7

Browse files
committed
Fix symbol resolution for malloc_type functions on macOS Sequoia
This commit addresses an issue with symbol resolution for malloc_type functions on the latest macOS Sequoia. The problem arises due to changes in how these functions are named in the dynamic symbol table of the system libraries that are part of the linker cache like the ones that contain the C++ runtime. This fix ensures that Memray correctly intercepts and tracks allocations made by malloc_type functions, fixing tracking allocations in the C++ runtime and other system libraries on macOS Sequoia.
1 parent f5eb8d7 commit 055f3c7

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/memray/_memray/macho_shenanigans.cpp

+16-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ patch_symbol(
3636
}
3737
}
3838

39+
static inline const char*
40+
get_canonical_name(const char* name) {
41+
// In macOS 15 (Sequoia)+, the symbols in the shared cache have a prefix
42+
// "_malloc_type" that we need to remove to match the symbols that we
43+
// are looking for.
44+
const char* prefix = "_malloc_type";
45+
if (strncmp(name, prefix, strlen(prefix)) != 0) {
46+
return name;
47+
}
48+
return name + strlen(prefix);
49+
}
50+
3951
static void
4052
patch_symbols_in_section(
4153
const section_t* section,
@@ -49,8 +61,9 @@ patch_symbols_in_section(
4961
if (!symbol_name || !(symbol_name[0] == '_' || symbol_name[0] == '.') || !symbol_name[1]) {
5062
continue;
5163
}
64+
const char* canonical_name = get_canonical_name(symbol_name);
5265
#define FOR_EACH_HOOKED_FUNCTION(hookname) \
53-
if (strcmp(MEMRAY_ORIG(hookname).d_symbol, symbol_name + 1) == 0) { \
66+
if (strcmp(MEMRAY_ORIG(hookname).d_symbol, canonical_name + 1) == 0) { \
5467
LOG(DEBUG) << "Patching " << symbol_name << " symbol pointer at " << std::hex << std::showbase \
5568
<< *(symbol_addr_table + i) << " for relocation entry " << (symbol_addr_table + i); \
5669
patch_symbol( \
@@ -225,9 +238,10 @@ patch_stubs(
225238
if (!symbol_name || !(symbol_name[0] == '_' || symbol_name[0] == '.') || !symbol_name[1]) {
226239
continue;
227240
}
241+
const char* canonical_name = get_canonical_name(symbol_name);
228242
auto stub_addr = reinterpret_cast<uint64_t>(symbol_addr_table + i * element_size);
229243
#define FOR_EACH_HOOKED_FUNCTION(hookname) \
230-
if (strcmp(MEMRAY_ORIG(hookname).d_symbol, symbol_name + 1) == 0) { \
244+
if (strcmp(MEMRAY_ORIG(hookname).d_symbol, canonical_name + 1) == 0) { \
231245
LOG(DEBUG) << "Extracting symbol address for " << symbol_name << " from stub function at " \
232246
<< std::hex << std::showbase << stub_addr; \
233247
void* symbol_addr = reinterpret_cast<void*>(lazy_pointer_from_stub(stub_addr)); \

0 commit comments

Comments
 (0)