Skip to content

Commit 06d5f37

Browse files
committed
Avoid patching symbols in the extension module
The fact that patching ourselves had not raised problems so far its really an outstanding fact in this universe. Unfortunately seems that with the latest toolchain + GCC there is something that causes memray to point the d_original entry of the hooks pointing to itself, which should never happen. To fix this resiliently, avoid patching ourselves by getting our own name in the extension module and then avoiding that shared object.
1 parent 8635569 commit 06d5f37

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/memray/_memray/elf_shenanigans.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <cstring>
2+
#include <mutex>
23
#include <set>
34
#include <string>
45
#include <sys/mman.h>
@@ -171,11 +172,23 @@ phdrs_callback(dl_phdr_info* info, [[maybe_unused]] size_t size, void* data) noe
171172
patched.insert(info->dlpi_name);
172173
}
173174

175+
// Get this so name once
176+
std::once_flag flag;
177+
static std::string self_so_name;
178+
std::call_once(flag, [&]() {
179+
Dl_info info;
180+
if (dladdr((void*)&phdrs_callback, &info)) {
181+
self_so_name = info.dli_fname;
182+
} else {
183+
self_so_name = "_memray.cpython-";
184+
}
185+
printf("self_so_name: %s\n", self_so_name.c_str());
186+
});
187+
174188
if (strstr(info->dlpi_name, "/ld-linux") || strstr(info->dlpi_name, "/ld-musl")
175-
|| strstr(info->dlpi_name, "linux-vdso.so.1"))
189+
|| strstr(info->dlpi_name, "linux-vdso.so.1") || strstr(info->dlpi_name, self_so_name.c_str()))
176190
{
177191
// Avoid chaos by not overwriting the symbols in the linker.
178-
// TODO: Don't override the symbols in our shared library!
179192
return 0;
180193
}
181194

0 commit comments

Comments
 (0)