Skip to content

Commit 725b967

Browse files
committed
Use C-style namespacing for our hook functions
LLDB wants to resolve `&::malloc` to `memray::hooks::malloc` for reasons that I haven't fully managed to make sense of, but we can work around this by calling our hook `memray::hooks::memray_malloc` instead. Wrap that up in a macro to make it harder to forget the way to compute the hook object name from the hooked function's name. Signed-off-by: Matt Wozniski <mwozniski@bloomberg.net>
1 parent 15626a3 commit 725b967

File tree

4 files changed

+48
-39
lines changed

4 files changed

+48
-39
lines changed

src/memray/_memray/elf_shenanigans.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,13 @@ overwrite_elf_table(
8383
const char* symname = symbols.getSymbolNameByIndex(index);
8484
auto symbol_addr = relocation.r_offset + base_addr;
8585
#define FOR_EACH_HOOKED_FUNCTION(hookname) \
86-
if (strcmp(hooks::hookname.d_symbol, symname) == 0) { \
87-
patch_symbol(hooks::hookname, &intercept::hookname, symname, symbol_addr, restore_original); \
86+
if (strcmp(MEMRAY_ORIG(hookname).d_symbol, symname) == 0) { \
87+
patch_symbol( \
88+
MEMRAY_ORIG(hookname), \
89+
&intercept::hookname, \
90+
symname, \
91+
symbol_addr, \
92+
restore_original); \
8893
continue; \
8994
}
9095
MEMRAY_HOOKED_FUNCTIONS

src/memray/_memray/hooks.cpp

+32-32
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ isDeallocator(const Allocator& allocator)
9090
__builtin_unreachable();
9191
}
9292

93-
#define FOR_EACH_HOOKED_FUNCTION(f) SymbolHook<decltype(&::f)> f(#f, &::f);
93+
#define FOR_EACH_HOOKED_FUNCTION(f) SymbolHook<decltype(&::f)> MEMRAY_ORIG_NO_NS(f)(#f, &::f);
9494
MEMRAY_HOOKED_FUNCTIONS
9595
#undef FOR_EACH_HOOKED_FUNCTION
9696

9797
void
9898
ensureAllHooksAreValid()
9999
{
100-
#define FOR_EACH_HOOKED_FUNCTION(f) f.ensureValidOriginalSymbol();
100+
#define FOR_EACH_HOOKED_FUNCTION(f) MEMRAY_ORIG(f).ensureValidOriginalSymbol();
101101
MEMRAY_HOOKED_FUNCTIONS
102102
#undef FOR_EACH_HOOKED_FUNCTION
103103
}
@@ -166,12 +166,12 @@ pymalloc_free(void* ctx, void* ptr) noexcept
166166
void*
167167
malloc(size_t size) noexcept
168168
{
169-
assert(hooks::malloc);
169+
assert(MEMRAY_ORIG(malloc));
170170

171171
void* ptr;
172172
{
173173
tracking_api::RecursionGuard guard;
174-
ptr = hooks::malloc(size);
174+
ptr = MEMRAY_ORIG(malloc)(size);
175175
}
176176
if (ptr) {
177177
tracking_api::Tracker::trackAllocation(ptr, size, hooks::Allocator::MALLOC);
@@ -182,7 +182,7 @@ malloc(size_t size) noexcept
182182
void
183183
free(void* ptr) noexcept
184184
{
185-
assert(hooks::free);
185+
assert(MEMRAY_ORIG(free));
186186

187187
// We need to call our API before we call the real free implementation
188188
// to make sure that the pointer is not reused in-between.
@@ -192,19 +192,19 @@ free(void* ptr) noexcept
192192

193193
{
194194
tracking_api::RecursionGuard guard;
195-
hooks::free(ptr);
195+
MEMRAY_ORIG(free)(ptr);
196196
}
197197
}
198198

199199
void*
200200
realloc(void* ptr, size_t size) noexcept
201201
{
202-
assert(hooks::realloc);
202+
assert(MEMRAY_ORIG(realloc));
203203

204204
void* ret;
205205
{
206206
tracking_api::RecursionGuard guard;
207-
ret = hooks::realloc(ptr, size);
207+
ret = MEMRAY_ORIG(realloc)(ptr, size);
208208
}
209209
if (ret) {
210210
if (ptr != nullptr) {
@@ -218,12 +218,12 @@ realloc(void* ptr, size_t size) noexcept
218218
void*
219219
calloc(size_t num, size_t size) noexcept
220220
{
221-
assert(hooks::calloc);
221+
assert(MEMRAY_ORIG(calloc));
222222

223223
void* ret;
224224
{
225225
tracking_api::RecursionGuard guard;
226-
ret = hooks::calloc(num, size);
226+
ret = MEMRAY_ORIG(calloc)(num, size);
227227
}
228228
if (ret) {
229229
tracking_api::Tracker::trackAllocation(ret, num * size, hooks::Allocator::CALLOC);
@@ -234,11 +234,11 @@ calloc(size_t num, size_t size) noexcept
234234
void*
235235
mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) noexcept
236236
{
237-
assert(hooks::mmap);
237+
assert(MEMRAY_ORIG(mmap));
238238
void* ptr;
239239
{
240240
tracking_api::RecursionGuard guard;
241-
ptr = hooks::mmap(addr, length, prot, flags, fd, offset);
241+
ptr = MEMRAY_ORIG(mmap)(addr, length, prot, flags, fd, offset);
242242
}
243243
if (ptr != MAP_FAILED) {
244244
tracking_api::Tracker::trackAllocation(ptr, length, hooks::Allocator::MMAP);
@@ -250,11 +250,11 @@ mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) noexc
250250
void*
251251
mmap64(void* addr, size_t length, int prot, int flags, int fd, off64_t offset) noexcept
252252
{
253-
assert(hooks::mmap64);
253+
assert(MEMRAY_ORIG(mmap64));
254254
void* ptr;
255255
{
256256
tracking_api::RecursionGuard guard;
257-
ptr = hooks::mmap64(addr, length, prot, flags, fd, offset);
257+
ptr = MEMRAY_ORIG(mmap64)(addr, length, prot, flags, fd, offset);
258258
}
259259
if (ptr != MAP_FAILED) {
260260
tracking_api::Tracker::trackAllocation(ptr, length, hooks::Allocator::MMAP);
@@ -266,23 +266,23 @@ mmap64(void* addr, size_t length, int prot, int flags, int fd, off64_t offset) n
266266
int
267267
munmap(void* addr, size_t length) noexcept
268268
{
269-
assert(hooks::munmap);
269+
assert(MEMRAY_ORIG(munmap));
270270
tracking_api::Tracker::trackDeallocation(addr, length, hooks::Allocator::MUNMAP);
271271
{
272272
tracking_api::RecursionGuard guard;
273-
return hooks::munmap(addr, length);
273+
return MEMRAY_ORIG(munmap)(addr, length);
274274
}
275275
}
276276

277277
void*
278278
valloc(size_t size) noexcept
279279
{
280-
assert(hooks::valloc);
280+
assert(MEMRAY_ORIG(valloc));
281281

282282
void* ret;
283283
{
284284
tracking_api::RecursionGuard guard;
285-
ret = hooks::valloc(size);
285+
ret = MEMRAY_ORIG(valloc)(size);
286286
}
287287
if (ret) {
288288
tracking_api::Tracker::trackAllocation(ret, size, hooks::Allocator::VALLOC);
@@ -293,12 +293,12 @@ valloc(size_t size) noexcept
293293
int
294294
posix_memalign(void** memptr, size_t alignment, size_t size) noexcept
295295
{
296-
assert(hooks::posix_memalign);
296+
assert(MEMRAY_ORIG(posix_memalign));
297297

298298
int ret;
299299
{
300300
tracking_api::RecursionGuard guard;
301-
ret = hooks::posix_memalign(memptr, alignment, size);
301+
ret = MEMRAY_ORIG(posix_memalign)(memptr, alignment, size);
302302
}
303303
if (!ret) {
304304
tracking_api::Tracker::trackAllocation(*memptr, size, hooks::Allocator::POSIX_MEMALIGN);
@@ -360,11 +360,11 @@ static DlsymCache dlsym_cache;
360360
void*
361361
dlsym(void* handle, const char* symbol) noexcept
362362
{
363-
assert(hooks::dlsym);
363+
assert(MEMRAY_ORIG(dlsym));
364364
void* ret;
365365
{
366366
tracking_api::RecursionGuard guard;
367-
ret = hooks::dlsym(handle, symbol);
367+
ret = MEMRAY_ORIG(dlsym)(handle, symbol);
368368
}
369369
if (ret) {
370370
auto [_, inserted] = dlsym_cache.insert(handle);
@@ -383,12 +383,12 @@ dlsym(void* handle, const char* symbol) noexcept
383383
int
384384
dlclose(void* handle) noexcept
385385
{
386-
assert(hooks::dlclose);
386+
assert(MEMRAY_ORIG(dlclose));
387387

388388
int ret;
389389
{
390390
tracking_api::RecursionGuard guard;
391-
ret = hooks::dlclose(handle);
391+
ret = MEMRAY_ORIG(dlclose)(handle);
392392
}
393393
dlsym_cache.erase(handle);
394394
tracking_api::NativeTrace::flushCache();
@@ -399,12 +399,12 @@ dlclose(void* handle) noexcept
399399
void*
400400
aligned_alloc(size_t alignment, size_t size) noexcept
401401
{
402-
assert(hooks::aligned_alloc);
402+
assert(MEMRAY_ORIG(aligned_alloc));
403403

404404
void* ret;
405405
{
406406
tracking_api::RecursionGuard guard;
407-
ret = hooks::aligned_alloc(alignment, size);
407+
ret = MEMRAY_ORIG(aligned_alloc)(alignment, size);
408408
}
409409
if (ret) {
410410
tracking_api::Tracker::trackAllocation(ret, size, hooks::Allocator::ALIGNED_ALLOC);
@@ -417,12 +417,12 @@ aligned_alloc(size_t alignment, size_t size) noexcept
417417
void*
418418
memalign(size_t alignment, size_t size) noexcept
419419
{
420-
assert(hooks::memalign);
420+
assert(MEMRAY_ORIG(memalign));
421421

422422
void* ret;
423423
{
424424
tracking_api::RecursionGuard guard;
425-
ret = hooks::memalign(alignment, size);
425+
ret = MEMRAY_ORIG(memalign)(alignment, size);
426426
}
427427
if (ret) {
428428
tracking_api::Tracker::trackAllocation(ret, size, hooks::Allocator::MEMALIGN);
@@ -434,12 +434,12 @@ memalign(size_t alignment, size_t size) noexcept
434434
void*
435435
pvalloc(size_t size) noexcept
436436
{
437-
assert(hooks::pvalloc);
437+
assert(MEMRAY_ORIG(pvalloc));
438438

439439
void* ret;
440440
{
441441
tracking_api::RecursionGuard guard;
442-
ret = hooks::pvalloc(size);
442+
ret = MEMRAY_ORIG(pvalloc)(size);
443443
}
444444
if (ret) {
445445
tracking_api::Tracker::trackAllocation(ret, size, hooks::Allocator::PVALLOC);
@@ -464,7 +464,7 @@ prctl(int option, ...) noexcept
464464
tracking_api::Tracker::registerThreadName(name);
465465
}
466466

467-
unsigned long ret = hooks::prctl(option, args[0], args[1], args[2], args[3]);
467+
unsigned long ret = MEMRAY_ORIG(prctl)(option, args[0], args[1], args[2], args[3]);
468468

469469
return ret;
470470
}
@@ -473,7 +473,7 @@ prctl(int option, ...) noexcept
473473
PyGILState_STATE
474474
PyGILState_Ensure() noexcept
475475
{
476-
PyGILState_STATE ret = hooks::PyGILState_Ensure();
476+
PyGILState_STATE ret = MEMRAY_ORIG(PyGILState_Ensure)();
477477
tracking_api::install_trace_function();
478478
return ret;
479479
}

src/memray/_memray/hooks.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ allocatorKind(const Allocator& allocator);
140140
bool
141141
isDeallocator(const Allocator& allocator);
142142

143-
#define FOR_EACH_HOOKED_FUNCTION(f) extern SymbolHook<decltype(&::f)> f;
143+
#define MEMRAY_ORIG_concat_helper(x, y) x##y
144+
#define MEMRAY_ORIG_NO_NS(f) MEMRAY_ORIG_concat_helper(memray_, f)
145+
#define MEMRAY_ORIG(f) memray::hooks::MEMRAY_ORIG_NO_NS(f)
146+
147+
#define FOR_EACH_HOOKED_FUNCTION(f) extern SymbolHook<decltype(&::f)> MEMRAY_ORIG_NO_NS(f);
144148
MEMRAY_HOOKED_FUNCTIONS
145149
#undef FOR_EACH_HOOKED_FUNCTION
146150

src/memray/_memray/macho_shenanigans.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ patch_symbols_in_section(
5050
continue;
5151
}
5252
#define FOR_EACH_HOOKED_FUNCTION(hookname) \
53-
if (strcmp(hooks::hookname.d_symbol, symbol_name + 1) == 0) { \
53+
if (strcmp(MEMRAY_ORIG(hookname).d_symbol, symbol_name + 1) == 0) { \
5454
LOG(DEBUG) << "Patching " << symbol_name << " symbol pointer at " << std::hex << std::showbase \
5555
<< *(symbol_addr_table + i) << " for relocation entry " << (symbol_addr_table + i); \
5656
patch_symbol( \
57-
hooks::hookname, \
57+
MEMRAY_ORIG(hookname), \
5858
&intercept::hookname, \
5959
symbol_name, \
6060
symbol_addr_table + i, \
@@ -227,7 +227,7 @@ patch_stubs(
227227
}
228228
auto stub_addr = reinterpret_cast<uint64_t>(symbol_addr_table + i * element_size);
229229
#define FOR_EACH_HOOKED_FUNCTION(hookname) \
230-
if (strcmp(hooks::hookname.d_symbol, symbol_name + 1) == 0) { \
230+
if (strcmp(MEMRAY_ORIG(hookname).d_symbol, symbol_name + 1) == 0) { \
231231
LOG(DEBUG) << "Extracting symbol address for " << symbol_name << " from stub function at " \
232232
<< std::hex << std::showbase << stub_addr; \
233233
void* symbol_addr = reinterpret_cast<void*>(lazy_pointer_from_stub(stub_addr)); \
@@ -238,7 +238,7 @@ patch_stubs(
238238
LOG(DEBUG) << "Patching " << symbol_name << " pointer at address " << std::hex << std::showbase \
239239
<< symbol_addr; \
240240
patch_symbol( \
241-
hooks::hookname, \
241+
MEMRAY_ORIG(hookname), \
242242
&intercept::hookname, \
243243
symbol_name, \
244244
symbol_addr, \

0 commit comments

Comments
 (0)