Browse Source

* WebAssembly threads: free the stack block and TLS block in case wasi_thread_spawn fails

Nikolay Nikolov 1 year ago
parent
commit
f23cc9ec6d
1 changed files with 6 additions and 0 deletions
  1. 6 0
      rtl/wasi/systhrd.inc

+ 6 - 0
rtl/wasi/systhrd.inc

@@ -372,6 +372,8 @@ Var
 begin
 begin
   {$IFDEF DEBUGWASMTHREADS}DebugWriteln('WasiBeginThread(sa: '+IntToStr(PtrUint(Sa))+',ss: '+IntToStr(PtrUint(StackSize))+',TF: '+IntToStr(PtrUint(ThreadFunction))+',Arg: '+IntToStr(PtrUint(P))+',fl: '+IntToStr(PtrUint(CreationFlags))+',ID: '+IntToStr(PtrUint(ThreadID))+')');{$ENDIF}
   {$IFDEF DEBUGWASMTHREADS}DebugWriteln('WasiBeginThread(sa: '+IntToStr(PtrUint(Sa))+',ss: '+IntToStr(PtrUint(StackSize))+',TF: '+IntToStr(PtrUint(ThreadFunction))+',Arg: '+IntToStr(PtrUint(P))+',fl: '+IntToStr(PtrUint(CreationFlags))+',ID: '+IntToStr(PtrUint(ThreadID))+')');{$ENDIF}
   T:=GetMem(SizeOf(TWasmThread));
   T:=GetMem(SizeOf(TWasmThread));
+  T^.StackBlock:=nil;
+  T^.TLSBlock:=nil;
   ThreadID:=T;
   ThreadID:=T;
   {$IFDEF DEBUGWASMTHREADS}DebugWriteln('WasiBeginThread thread ID : '+IntToStr(PtrUint(ThreadID)));{$ENDIF}
   {$IFDEF DEBUGWASMTHREADS}DebugWriteln('WasiBeginThread thread ID : '+IntToStr(PtrUint(ThreadID)));{$ENDIF}
   T^.ThreadFunction:=ThreadFunction;
   T^.ThreadFunction:=ThreadFunction;
@@ -397,6 +399,10 @@ begin
   else
   else
     begin
     begin
     {$IFDEF DEBUGWASMTHREADS}DebugWriteln('WasiBeginThread: spawn thread failed');{$ENDIF}
     {$IFDEF DEBUGWASMTHREADS}DebugWriteln('WasiBeginThread: spawn thread failed');{$ENDIF}
+    if Assigned(T^.StackBlock) then
+      FreeMem(T^.StackBlock);
+    if Assigned(T^.TLSBlock) then
+      FreeMem(T^.TLSBlock);
     FreeMem(T);
     FreeMem(T);
     {$IFDEF DEBUGWASMTHREADS}DebugWriteln('WasiBeginThread: spawn thread failed, freeing thread struct');{$ENDIF}
     {$IFDEF DEBUGWASMTHREADS}DebugWriteln('WasiBeginThread: spawn thread failed, freeing thread struct');{$ENDIF}
     WasiBeginThread:=TThreadID(0);
     WasiBeginThread:=TThreadID(0);