From bde454905aac7f961350e5c36af5ad0a4713963b Mon Sep 17 00:00:00 2001 From: Andreas Hausladen Date: Sun, 17 Jun 2018 11:06:42 +0200 Subject: [PATCH] Mantis #5754: JclCreateThreadStackTrace fails unpredictably on Win7 64 bit --- jcl/experts/debug/simdview/JclSIMDUtils.pas | 6 ++++-- jcl/source/windows/JclDebug.pas | 8 ++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/jcl/experts/debug/simdview/JclSIMDUtils.pas b/jcl/experts/debug/simdview/JclSIMDUtils.pas index 01bdfe6a1a..c157045c80 100644 --- a/jcl/experts/debug/simdview/JclSIMDUtils.pas +++ b/jcl/experts/debug/simdview/JclSIMDUtils.pas @@ -40,6 +40,7 @@ interface {$IFDEF UNITVERSIONING} JclUnitVersioning, {$ENDIF UNITVERSIONING} + JclBase, JclSysInfo, JclOtaResources; @@ -938,8 +939,9 @@ function GetThreadJclContext(AThread: IOTAThread; out JclContext: TJclContext): begin GetMem(ContextMemory, SizeOf(TJclContext) + 15); try - if (Cardinal(ContextMemory) and 15) <> 0 then - AlignedContext := PJclContext((Cardinal(ContextMemory) + 16) and $FFFFFFF0) + if (TJclAddr(ContextMemory) and 15) <> 0 then + // PAnsiChar: TJclAddr is signed and would cause an int overflow for half the address space + AlignedContext := PContext(TJclAddr(PAnsiChar(ContextMemory) + 16) and -16) else AlignedContext := ContextMemory; AlignedContext^.ScalarContext.ContextFlags := CONTEXT_EXTENDED_REGISTERS; diff --git a/jcl/source/windows/JclDebug.pas b/jcl/source/windows/JclDebug.pas index 0f8822adb4..96cdc65f85 100644 --- a/jcl/source/windows/JclDebug.pas +++ b/jcl/source/windows/JclDebug.pas @@ -5292,13 +5292,13 @@ function JclCreateThreadStackTrace(Raw: Boolean; const ThreadHandle: THandle): T AlignedContext: PContext; begin Result := nil; - GetMem(ContextMemory, SizeOf(TContext) + 15); + ContextMemory := AllocMem(SizeOf(TContext) + 15); try - if (Cardinal(ContextMemory) and 15) <> 0 then - AlignedContext := PContext((Cardinal(ContextMemory) + 16) and $FFFFFFF0) + if (TJclAddr(ContextMemory) and 15) <> 0 then + // PAnsiChar: TJclAddr is signed and would cause an int overflow for half the address space + AlignedContext := PContext(TJclAddr(PAnsiChar(ContextMemory) + 16) and -16) else AlignedContext := ContextMemory; - ResetMemory(AlignedContext^, SizeOf(AlignedContext^)); AlignedContext^.ContextFlags := CONTEXT_FULL; {$IFDEF CPU32} if GetThreadContext(ThreadHandle, AlignedContext^) then