Skip to content

Commit ce20207

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 ce20207

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/memray/_memray.pyx

-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ def set_log_level(int level):
101101
"""
102102
setLogThreshold(level)
103103

104-
105104
cpdef enum AllocatorType:
106105
MALLOC = 1
107106
FREE = 2

src/memray/_memray/macho_shenanigans.cpp

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

39+
static inline const char*
40+
get_canonical_name(const char* name) {
41+
const char* prefix = "_malloc_type";
42+
if (strncmp(name, prefix, strlen(prefix)) != 0) {
43+
return name;
44+
}
45+
return name + strlen(prefix);
46+
}
47+
3948
static void
4049
patch_symbols_in_section(
4150
const section_t* section,
@@ -49,8 +58,9 @@ patch_symbols_in_section(
4958
if (!symbol_name || !(symbol_name[0] == '_' || symbol_name[0] == '.') || !symbol_name[1]) {
5059
continue;
5160
}
61+
const char* canonical_name = get_canonical_name(symbol_name);
5262
#define FOR_EACH_HOOKED_FUNCTION(hookname) \
53-
if (strcmp(MEMRAY_ORIG(hookname).d_symbol, symbol_name + 1) == 0) { \
63+
if (strcmp(MEMRAY_ORIG(hookname).d_symbol, canonical_name + 1) == 0) { \
5464
LOG(DEBUG) << "Patching " << symbol_name << " symbol pointer at " << std::hex << std::showbase \
5565
<< *(symbol_addr_table + i) << " for relocation entry " << (symbol_addr_table + i); \
5666
patch_symbol( \
@@ -225,9 +235,10 @@ patch_stubs(
225235
if (!symbol_name || !(symbol_name[0] == '_' || symbol_name[0] == '.') || !symbol_name[1]) {
226236
continue;
227237
}
238+
const char* canonical_name = get_canonical_name(symbol_name);
228239
auto stub_addr = reinterpret_cast<uint64_t>(symbol_addr_table + i * element_size);
229240
#define FOR_EACH_HOOKED_FUNCTION(hookname) \
230-
if (strcmp(MEMRAY_ORIG(hookname).d_symbol, symbol_name + 1) == 0) { \
241+
if (strcmp(MEMRAY_ORIG(hookname).d_symbol, canonical_name + 1) == 0) { \
231242
LOG(DEBUG) << "Extracting symbol address for " << symbol_name << " from stub function at " \
232243
<< std::hex << std::showbase << stub_addr; \
233244
void* symbol_addr = reinterpret_cast<void*>(lazy_pointer_from_stub(stub_addr)); \

0 commit comments

Comments
 (0)