Browse Source

* WebAssembly threads: call fpc_wasm32_init_tls from inline asm code to avoid
race condition in branchful exceptions mode

Nikolay Nikolov 1 year ago
parent
commit
a0e1dce1fd
1 changed files with 10 additions and 1 deletions
  1. 10 1
      rtl/wasi/systhrd.inc

+ 10 - 1
rtl/wasi/systhrd.inc

@@ -342,7 +342,6 @@ procedure WasiAllocateThreadVars; forward;
 procedure FPCWasmThreadStartPascal(tid: longint; start_arg: PWasmThread);
 begin
   {$IFDEF DEBUGWASMTHREADS}DebugWriteln('FPCWasmThreadStartPascal(...)');{$ENDIF}
-  fpc_wasm32_init_tls(start_arg^.InitTLSBase);
 
   start_arg^.ID:=tid;
   GlobalCurrentThread:=@start_arg;
@@ -383,6 +382,16 @@ asm
   i32.load
   global.set $__stack_pointer
 
+  ;; call fpc_wasm32_init_tls from within assembly code, because in branchful
+  ;; exceptions mode, Free Pascal generates threadvar access after every
+  ;; function call. Therefore, we want threadvars to be initialized, before we
+  ;; call any sort of Pascal code.
+  local.get 1  ;; start_arg
+  i32.const 4  ;; offset to InitTLSBase
+  i32.add
+  i32.load
+  call $fpc_wasm32_init_tls
+
   local.get 0
   local.get 1
   call $FPCWasmThreadStartPascal