Skip to content

Commit

Permalink
Avoid patching symbols in the extension module
Browse files Browse the repository at this point in the history
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.

Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
  • Loading branch information
pablogsal committed Sep 19, 2024
1 parent 8635569 commit 1e2532d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions news/685.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix some crashes caused by interposing symbols in memray itself
14 changes: 12 additions & 2 deletions src/memray/_memray/elf_shenanigans.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <cstring>
#include <mutex>
#include <set>
#include <string>
#include <sys/mman.h>
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 1e2532d

Please sign in to comment.