Skip to content

Commit bd08eba

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. Signed-off-by: Pablo Galindo <[email protected]>
1 parent 8635569 commit bd08eba

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

news/685.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix some crashes caused by interposing symbols in memray itself

src/memray/_memray/elf_shenanigans.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ namespace {
1717
struct elf_patcher_context_t
1818
{
1919
bool restore_original;
20-
std::set<std::string> patched;
20+
std::set<std::string>& patched;
21+
const std::string& self_so_name;
2122
};
2223

2324
} // namespace
@@ -172,10 +173,10 @@ phdrs_callback(dl_phdr_info* info, [[maybe_unused]] size_t size, void* data) noe
172173
}
173174

174175
if (strstr(info->dlpi_name, "/ld-linux") || strstr(info->dlpi_name, "/ld-musl")
175-
|| strstr(info->dlpi_name, "linux-vdso.so.1"))
176+
|| strstr(info->dlpi_name, "linux-vdso.so.1")
177+
|| strstr(info->dlpi_name, context.self_so_name.c_str()))
176178
{
177179
// Avoid chaos by not overwriting the symbols in the linker.
178-
// TODO: Don't override the symbols in our shared library!
179180
return 0;
180181
}
181182

@@ -198,14 +199,14 @@ phdrs_callback(dl_phdr_info* info, [[maybe_unused]] size_t size, void* data) noe
198199
void
199200
SymbolPatcher::overwrite_symbols() noexcept
200201
{
201-
elf_patcher_context_t context{false, symbols};
202+
elf_patcher_context_t context{false, symbols, self_so_name};
202203
dl_iterate_phdr(&phdrs_callback, (void*)&context);
203204
}
204205

205206
void
206207
SymbolPatcher::restore_symbols() noexcept
207208
{
208-
elf_patcher_context_t context{true, symbols};
209+
elf_patcher_context_t context{true, symbols, self_so_name};
209210
dl_iterate_phdr(&phdrs_callback, (void*)&context);
210211
}
211212

src/memray/_memray/linker_shenanigans.h

+14
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,28 @@
33
#include <set>
44
#include <string>
55

6+
#include <dlfcn.h>
7+
68
namespace memray::linker {
79

10+
static void
11+
_dummy(void){};
12+
813
class SymbolPatcher
914
{
1015
private:
1116
std::set<std::string> symbols;
17+
std::string self_so_name = "_memray.cpython-";
1218

1319
public:
20+
SymbolPatcher()
21+
{
22+
Dl_info info;
23+
if (dladdr((void*)&_dummy, &info)) {
24+
self_so_name = info.dli_fname;
25+
}
26+
printf("%s\n", self_so_name.c_str());
27+
}
1428
void overwrite_symbols() noexcept;
1529
void restore_symbols() noexcept;
1630
};

0 commit comments

Comments
 (0)