Skip to content

Commit a3fee33

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 a3fee33

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-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-4
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>
@@ -18,6 +19,7 @@ struct elf_patcher_context_t
1819
{
1920
bool restore_original;
2021
std::set<std::string> patched;
22+
std::string self_so_name;
2123
};
2224

2325
} // namespace
@@ -172,10 +174,10 @@ phdrs_callback(dl_phdr_info* info, [[maybe_unused]] size_t size, void* data) noe
172174
}
173175

174176
if (strstr(info->dlpi_name, "/ld-linux") || strstr(info->dlpi_name, "/ld-musl")
175-
|| strstr(info->dlpi_name, "linux-vdso.so.1"))
177+
|| strstr(info->dlpi_name, "linux-vdso.so.1")
178+
|| strstr(info->dlpi_name, context.self_so_name.c_str()))
176179
{
177180
// Avoid chaos by not overwriting the symbols in the linker.
178-
// TODO: Don't override the symbols in our shared library!
179181
return 0;
180182
}
181183

@@ -198,14 +200,14 @@ phdrs_callback(dl_phdr_info* info, [[maybe_unused]] size_t size, void* data) noe
198200
void
199201
SymbolPatcher::overwrite_symbols() noexcept
200202
{
201-
elf_patcher_context_t context{false, symbols};
203+
elf_patcher_context_t context{false, symbols, self_so_name};
202204
dl_iterate_phdr(&phdrs_callback, (void*)&context);
203205
}
204206

205207
void
206208
SymbolPatcher::restore_symbols() noexcept
207209
{
208-
elf_patcher_context_t context{true, symbols};
210+
elf_patcher_context_t context{true, symbols, self_so_name};
209211
dl_iterate_phdr(&phdrs_callback, (void*)&context);
210212
}
211213

src/memray/_memray/linker_shenanigans.h

+2
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ class SymbolPatcher
1313
public:
1414
void overwrite_symbols() noexcept;
1515
void restore_symbols() noexcept;
16+
17+
std::string self_so_name;
1618
};
1719
} // namespace memray::linker

src/memray/_memray/tracking_api.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ std::unique_ptr<std::mutex> Tracker::s_mutex(new std::mutex);
433433
pthread_key_t Tracker::s_native_unwind_vector_key;
434434
std::unique_ptr<Tracker> Tracker::s_instance_owner;
435435
std::atomic<Tracker*> Tracker::s_instance = nullptr;
436+
std::string Tracker::s_self_so_name = "_memray.cpython-";
436437

437438
std::vector<PythonStackTracker::LazilyEmittedFrame>
438439
PythonStackTracker::pythonFrameToStack(PyFrameObject* current_frame)
@@ -554,7 +555,7 @@ Tracker::Tracker(
554555
, d_trace_python_allocators(trace_python_allocators)
555556
{
556557
static std::once_flag once;
557-
call_once(once, [] {
558+
call_once(once, []() {
558559
// We use the pthread TLS API for this vector because we must be able
559560
// to re-create it while TLS destructors are running (a destructor can
560561
// call malloc, hitting our malloc hook). POSIX guarantees multiple
@@ -569,6 +570,11 @@ Tracker::Tracker(
569570

570571
hooks::ensureAllHooksAreValid();
571572
NativeTrace::setup();
573+
574+
Dl_info info;
575+
if (dladdr((void*)&get_executable, &info)) {
576+
Tracker::s_self_so_name = info.dli_fname;
577+
}
572578
});
573579

574580
d_writer->setMainTidAndSkippedFrames(thread_id(), computeMainTidSkip());
@@ -587,6 +593,7 @@ Tracker::Tracker(
587593
d_background_thread = std::make_unique<BackgroundThread>(d_writer, memory_interval);
588594
d_background_thread->start();
589595

596+
d_patcher.self_so_name = Tracker::s_self_so_name;
590597
d_patcher.overwrite_symbols();
591598
}
592599

src/memray/_memray/tracking_api.h

+1
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ class Tracker
374374
static pthread_key_t s_native_unwind_vector_key;
375375
static std::unique_ptr<Tracker> s_instance_owner;
376376
static std::atomic<Tracker*> s_instance;
377+
static std::string s_self_so_name;
377378

378379
FrameCollection<RawFrame> d_frames;
379380
std::shared_ptr<RecordWriter> d_writer;

0 commit comments

Comments
 (0)