@@ -8,7 +8,12 @@ var LibraryPThread = {
8
8
$PThread__postset : 'if (!ENVIRONMENT_IS_PTHREAD) PThread.initMainThreadBlock(); else PThread.initWorker();' ,
9
9
$PThread__deps : [ '$registerPthreadPtr' ,
10
10
'$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
+ ] ,
12
17
$PThread : {
13
18
MAIN_THREAD_ID : 1 , // A special constant that identifies the main JS thread ID.
14
19
mainThreadInfo : {
@@ -37,7 +42,15 @@ var LibraryPThread = {
37
42
#endif
38
43
} ,
39
44
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__ } } } ) ;
41
54
42
55
for ( var i = 0 ; i < { { { C_STRUCTS . pthread . __size__ } } } / 4 ; ++ i ) HEAPU32 [ PThread . mainThreadBlock / 4 + i ] = 0 ;
43
56
@@ -50,18 +63,24 @@ var LibraryPThread = {
50
63
{ { { makeSetValue ( 'headPtr' , 0 , 'headPtr' , 'i32' ) } } } ;
51
64
52
65
// Allocate memory for thread-local storage.
53
- var tlsMemory = { { { makeStaticAlloc ( cDefine ( 'PTHREAD_KEYS_MAX' ) * 4 ) } } } ;
66
+ var tlsMemory = _malloc ( { { { cDefine ( 'PTHREAD_KEYS_MAX' ) * 4 } } } ) ;
54
67
for ( var i = 0 ; i < { { { cDefine ( 'PTHREAD_KEYS_MAX' ) } } } ; ++ i ) HEAPU32 [ tlsMemory / 4 + i ] = 0 ;
55
68
Atomics . store ( HEAPU32 , ( PThread . mainThreadBlock + { { { C_STRUCTS . pthread . tsd } } } ) >> 2 , tlsMemory ) ; // Init thread-local-storage memory array.
56
69
Atomics . store ( HEAPU32 , ( PThread . mainThreadBlock + { { { C_STRUCTS . pthread . tid } } } ) >> 2 , PThread . mainThreadBlock ) ; // Main thread ID.
57
70
Atomics . store ( HEAPU32 , ( PThread . mainThreadBlock + { { { C_STRUCTS . pthread . pid } } } ) >> 2 , { { { PROCINFO . pid } } } ) ; // Process ID.
58
71
72
+ __main_thread_futex_wait_address = _malloc ( 4 ) ;
73
+
59
74
#if PTHREADS_PROFILING
60
75
PThread . createProfilerBlock ( PThread . mainThreadBlock ) ;
61
76
PThread . setThreadName ( PThread . mainThreadBlock , "Browser main thread" ) ;
62
77
PThread . setThreadStatus ( PThread . mainThreadBlock , { { { cDefine ( 'EM_THREAD_STATUS_RUNNING' ) } } } ) ;
63
78
#endif
64
79
80
+ #if USE_ASAN || USE_LSAN
81
+ } ) ;
82
+ #endif
83
+
65
84
// Pass the thread address inside the asm.js scope to store it for fast access that avoids the need for a FFI out.
66
85
// Global constructors trying to access this value will read the wrong value, but that is UB anyway.
67
86
registerPthreadPtr ( PThread . mainThreadBlock , /*isMainBrowserThread=*/ ! ENVIRONMENT_IS_WORKER , /*isMainRuntimeThread=*/ 1 ) ;
@@ -83,7 +102,7 @@ var LibraryPThread = {
83
102
84
103
#if PTHREADS_PROFILING
85
104
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__ } } } ) ;
87
106
Atomics . store ( HEAPU32 , ( pthreadPtr + { { { C_STRUCTS . pthread . profilerBlock } } } ) >> 2 , profilerBlock ) ;
88
107
89
108
// Zero fill contents at startup.
@@ -1094,7 +1113,7 @@ var LibraryPThread = {
1094
1113
} ,
1095
1114
1096
1115
// 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 ' ,
1098
1117
1099
1118
// Returns 0 on success, or one of the values -ETIMEDOUT, -EWOULDBLOCK or -EINVAL on error.
1100
1119
emscripten_futex_wait__deps : [ '_main_thread_futex_wait_address' , 'emscripten_main_thread_process_queued_calls' ] ,
0 commit comments