diff --git a/news/685.bugfix.rst b/news/685.bugfix.rst new file mode 100644 index 0000000000..057c58427d --- /dev/null +++ b/news/685.bugfix.rst @@ -0,0 +1 @@ +Fix some crashes caused by interposing symbols in memray itself diff --git a/src/memray/_memray/elf_shenanigans.cpp b/src/memray/_memray/elf_shenanigans.cpp index 997599ea74..b4f5e3a012 100644 --- a/src/memray/_memray/elf_shenanigans.cpp +++ b/src/memray/_memray/elf_shenanigans.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -171,11 +172,20 @@ phdrs_callback(dl_phdr_info* info, [[maybe_unused]] size_t size, void* data) noe patched.insert(info->dlpi_name); } + // Get this so name once + std::once_flag flag; + static std::string self_so_name = "_memray.cpython-"; + std::call_once(flag, [&]() { + Dl_info info; + if (dladdr((void*)&phdrs_callback, &info)) { + self_so_name = info.dli_fname; + } + }); + if (strstr(info->dlpi_name, "/ld-linux") || strstr(info->dlpi_name, "/ld-musl") - || strstr(info->dlpi_name, "linux-vdso.so.1")) + || strstr(info->dlpi_name, "linux-vdso.so.1") || strstr(info->dlpi_name, self_so_name.c_str())) { // Avoid chaos by not overwriting the symbols in the linker. - // TODO: Don't override the symbols in our shared library! return 0; }