A very strange behavior (bug) in the latest FPC compilers 3.2.x and earlier lazarus / 21-General ---------------------------------------------------------------------- Coldzer0 · 2022-06-08 00:04 Hello guys, I was working on converting FastMM5 to FPC. and I found a bizarre bug in the compiler (the latest stable version with fixes and earlier) https://gitlab.com/freepascal.org/fpc/source/-/tree/fixes_3_2 Free Pascal Compiler version 3.2.3-616-gbe560dadf8 The code can run without optimization, but when the used optimization is -O1 or -O2 or -O3, the result is not valid for the code in BlockIsFree. BTW The code will work fine with the latest trunk version I tested the trunk version "Free Pascal Compiler version 3.3.1-11322-gf2c8018f9b [2022/06/06] for x86_64" and it works fine with all opt Here's a PoC Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program TEST_CASE_FPC;{$mode Delphi}uses  windows; const  CBlockIsFreeFlag = 1;  CBlockIsUsed = 2;   CMediumBlockAlignmentBits = 6;  CMediumBlockAlignment = 1 shl CMediumBlockAlignmentBits;  CMaximumMediumBlockSpanSize = 64 * 1024 * CMediumBlockAlignment; // = 4MB  function BlockIsFree(APSmallMediumOrLargeBlock: Pointer): Boolean;begin  {     An Access violation Error will be raised here.      "PWord(APSmallMediumOrLargeBlock)[-1]"      because the compiler will convert the statement to     mov eax, dword ptr [rcx - 2]     which is not right, and it should be     mov ax, word ptr [rcx - 2]  }  {      eax = APSmallMediumOrLargeBlock       TEST Cases                                    Status          Generated Code       PBYTE(APSmallMediumOrLargeBlock)[-2]            Works           mov ax,[eax-$02]. This should read only 1 byte, not 2      PWord(PByte(APSmallMediumOrLargeBlock) - 2)^    Bugged          mov eax,[eax-$02]      PWord(APSmallMediumOrLargeBlock)[-1]            Bugged          mov eax,[eax-$02]  }  Result := PWord(APSmallMediumOrLargeBlock)[-1] and CBlockIsFreeFlag <> 0;end; var  Buffer, BufferEnd : Pointer;  Size : Integer;  ReadTest : Word;begin  Size := 2 * CMaximumMediumBlockSpanSize;   Buffer := VirtualAlloc(nil, Size, MEM_COMMIT, PAGE_READWRITE);  if Buffer <> nil then  begin    BufferEnd := Buffer + Size;    WriteLn('Buffer Allocated : ', HexStr(Buffer));    WriteLn('Buffer End       : ', HexStr(BufferEnd));      PWord(BufferEnd - 2)^ := CBlockIsUsed; // Write the flag to the last 2 bytes of the buffer     // the compiler will convert this to "mov ax,[eax-$02]" But not the one in BlockIsFree    ReadTest := PWord(BufferEnd - 2)^;    WriteLn('ReadTest : ', ReadTest );     if not BlockIsFree(BufferEnd) then      WriteLn('Test Is Ok');  end;  VirtualFree(Buffer, 0, MEM_RELEASE);   ReadLn;end.  If you guys can test the code and Check if you have the same behavior! ---------------------------------------------------------------------- PascalDragon · 2022-06-08 07:15 I can reproduce it. Please report as a bug. Maybe someone can bisect which commit fixed that in the main...