Skip to content

Commit 4084d12

Browse files
author
Christian Schafmeisterr
committed
Put a mutex on snapshot load linking
Currently there is a race condition so for now I put a mutex around the linking
1 parent cda8a33 commit 4084d12

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

src/gctools/snapshotSaveLoad.cc

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,6 +2031,9 @@ struct CodeFixup_t {
20312031
CodeFixup_t(llvmo::ObjectFile_O* o, llvmo::ObjectFile_O* n) : _oldCode(o), _newCode(n){};
20322032
};
20332033

2034+
static std::mutex g_materialize_lock;
2035+
2036+
20342037
void snapshot_load(void* maybeStartOfSnapshot, void* maybeEndOfSnapshot, const std::string& filename) {
20352038
global_InSnapshotLoad = true;
20362039
// Keep track of objects that we have already allocated
@@ -2359,7 +2362,13 @@ void snapshot_load(void* maybeStartOfSnapshot, void* maybeEndOfSnapshot, const s
23592362
// Link all the code objects
23602363
MaybeTimeStartup time5("Object file linking");
23612364
using TP = thread_pool<ThreadManager>;
2365+
#if 0
2366+
// Create a pool with one thread for debugging threading issues
2367+
TP pool(1);
2368+
#else
2369+
// Create a pool of multiple threads
23622370
TP pool(TP::sane_number_of_threads());
2371+
#endif
23632372
for (cur_header = start_header; cur_header->_Kind != ISLKind::End;) {
23642373
if (cur_header->_Kind == ISLKind::General) {
23652374
ISLGeneralHeader_s* generalHeader = (ISLGeneralHeader_s*)cur_header;
@@ -2436,8 +2445,24 @@ void snapshot_load(void* maybeStartOfSnapshot, void* maybeEndOfSnapshot, const s
24362445
core::ThreadLocalState tls;
24372446
my_thread_low_level = &tlsll;
24382447
my_thread = &tls;
2439-
if (!obj_claspJIT->force_materialize(jitdylib, objectId))
2440-
ISL_ERROR("Failed to materialize JITDylib");
2448+
// Ensure the thread-local pointers don't dangle after the
2449+
// local `tls`/`tlsll` objects are destroyed at lambda exit.
2450+
// Worker threads are reused by the pool; between tasks any
2451+
// stray access to my_thread would read freed stack memory
2452+
// and GC scans would traverse garbage.
2453+
struct ClearTLS {
2454+
~ClearTLS() {
2455+
my_thread = nullptr;
2456+
my_thread_low_level = nullptr;
2457+
}
2458+
} clear_tls_guard;
2459+
{
2460+
// Lock this code for now because multithreaded linking
2461+
// has a race condition. TODO: Fix race condition
2462+
std::lock_guard<std::mutex> lk(g_materialize_lock);
2463+
if (!obj_claspJIT->force_materialize(jitdylib, objectId))
2464+
ISL_ERROR("Failed to materialize JITDylib");
2465+
}
24412466
});
24422467
}
24432468
}

0 commit comments

Comments
 (0)