Skip to content

Commit 2a0c9c7

Browse files
authored
Avoid static allocations in pthreads code (#12055)
Just replaces some static allocations with mallocs that are never freed. Optimally these could be actually static allocations in C in a future refactoring perhaps.
1 parent aa2731d commit 2a0c9c7

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/library_pthread.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ var LibraryPThread = {
88
$PThread__postset: 'if (!ENVIRONMENT_IS_PTHREAD) PThread.initMainThreadBlock(); else PThread.initWorker();',
99
$PThread__deps: ['$registerPthreadPtr',
1010
'$ERRNO_CODES', 'emscripten_futex_wake', '$killThread',
11-
'$cancelThread', '$cleanupThread'],
11+
'$cancelThread', '$cleanupThread',
12+
'_main_thread_futex_wait_address'
13+
#if USE_ASAN || USE_LSAN
14+
, '$withBuiltinMalloc'
15+
#endif
16+
],
1217
$PThread: {
1318
MAIN_THREAD_ID: 1, // A special constant that identifies the main JS thread ID.
1419
mainThreadInfo: {
@@ -37,7 +42,15 @@ var LibraryPThread = {
3742
#endif
3843
},
3944
initRuntime: function() {
40-
PThread.mainThreadBlock = {{{ makeStaticAlloc(C_STRUCTS.pthread.__size__) }}};
45+
#if USE_ASAN || USE_LSAN
46+
// When sanitizers are enabled, malloc is normally instrumented to call
47+
// sanitizer code that checks some things about pthreads. As we are just
48+
// setting up the main thread here, and are not ready for such calls,
49+
// call malloc directly.
50+
withBuiltinMalloc(function () {
51+
#endif
52+
53+
PThread.mainThreadBlock = _malloc({{{ C_STRUCTS.pthread.__size__ }}});
4154

4255
for (var i = 0; i < {{{ C_STRUCTS.pthread.__size__ }}}/4; ++i) HEAPU32[PThread.mainThreadBlock/4+i] = 0;
4356

@@ -50,18 +63,24 @@ var LibraryPThread = {
5063
{{{ makeSetValue('headPtr', 0, 'headPtr', 'i32') }}};
5164

5265
// Allocate memory for thread-local storage.
53-
var tlsMemory = {{{ makeStaticAlloc(cDefine('PTHREAD_KEYS_MAX') * 4) }}};
66+
var tlsMemory = _malloc({{{ cDefine('PTHREAD_KEYS_MAX') * 4 }}});
5467
for (var i = 0; i < {{{ cDefine('PTHREAD_KEYS_MAX') }}}; ++i) HEAPU32[tlsMemory/4+i] = 0;
5568
Atomics.store(HEAPU32, (PThread.mainThreadBlock + {{{ C_STRUCTS.pthread.tsd }}} ) >> 2, tlsMemory); // Init thread-local-storage memory array.
5669
Atomics.store(HEAPU32, (PThread.mainThreadBlock + {{{ C_STRUCTS.pthread.tid }}} ) >> 2, PThread.mainThreadBlock); // Main thread ID.
5770
Atomics.store(HEAPU32, (PThread.mainThreadBlock + {{{ C_STRUCTS.pthread.pid }}} ) >> 2, {{{ PROCINFO.pid }}}); // Process ID.
5871

72+
__main_thread_futex_wait_address = _malloc(4);
73+
5974
#if PTHREADS_PROFILING
6075
PThread.createProfilerBlock(PThread.mainThreadBlock);
6176
PThread.setThreadName(PThread.mainThreadBlock, "Browser main thread");
6277
PThread.setThreadStatus(PThread.mainThreadBlock, {{{ cDefine('EM_THREAD_STATUS_RUNNING') }}});
6378
#endif
6479

80+
#if USE_ASAN || USE_LSAN
81+
});
82+
#endif
83+
6584
// Pass the thread address inside the asm.js scope to store it for fast access that avoids the need for a FFI out.
6685
// Global constructors trying to access this value will read the wrong value, but that is UB anyway.
6786
registerPthreadPtr(PThread.mainThreadBlock, /*isMainBrowserThread=*/!ENVIRONMENT_IS_WORKER, /*isMainRuntimeThread=*/1);
@@ -83,7 +102,7 @@ var LibraryPThread = {
83102

84103
#if PTHREADS_PROFILING
85104
createProfilerBlock: function(pthreadPtr) {
86-
var profilerBlock = (pthreadPtr == PThread.mainThreadBlock) ? {{{ makeStaticAlloc(C_STRUCTS.thread_profiler_block.__size__) }}} : _malloc({{{ C_STRUCTS.thread_profiler_block.__size__ }}});
105+
var profilerBlock = _malloc({{{ C_STRUCTS.thread_profiler_block.__size__ }}});
87106
Atomics.store(HEAPU32, (pthreadPtr + {{{ C_STRUCTS.pthread.profilerBlock }}} ) >> 2, profilerBlock);
88107

89108
// Zero fill contents at startup.
@@ -1094,7 +1113,7 @@ var LibraryPThread = {
10941113
},
10951114

10961115
// Stores the memory address that the main thread is waiting on, if any.
1097-
_main_thread_futex_wait_address: '{{{ makeStaticAlloc(4) }}}',
1116+
_main_thread_futex_wait_address: '0',
10981117

10991118
// Returns 0 on success, or one of the values -ETIMEDOUT, -EWOULDBLOCK or -EINVAL on error.
11001119
emscripten_futex_wait__deps: ['_main_thread_futex_wait_address', 'emscripten_main_thread_process_queued_calls'],

0 commit comments

Comments
 (0)