-O2 -gt generates code, that overwrites random parts of the stack
Original Reporter info from Mantis: Martin @martin_frb
-
Reporter name: Martin Friebe
Original Reporter info from Mantis: Martin @martin_frb
- Reporter name: Martin Friebe
Description:
Compile the program below with "-O2 -gt -al"
The resulting code contains
# Var StockID located in register edx # Var Image located in register ecx # Var Mask located in register eax # Var $self located in register eax # Var $result located in register al movl 8(%ebp),%eax .Ll2: movb $85,%al movl $1431655765,(%ecx) movl $1431655765,(%eax)
in the initialization for TBar.foo.
At the time -gt trashes (%eax) the pointer in eax is no longer valid, since %al has been set to 0x55 before.
The result is that some other randomly chosen data on the stack is trashed. Leading to unpredicted behaviour and crashes.
Steps to reproduce:
program project1;
{$mode objfpc}{$H+}
uses Classes;
type
TBar = class
function foo(StockID: LongInt; out Image, Mask: Longint): Boolean;
end;
function TBar.foo(StockID: LongInt; out Image, Mask: Longint): Boolean;
begin
Result := False;
end;
var a,b: Integer;
x:TBar;
begin
x.foo(0,a,b);
end.
Mantis conversion info:
- Mantis ID: 16757
- Build: 15398
- Platform: Win32
- Version: 2.5.1
- Fixed in version: 2.6.0
- Fixed in revision: 15479 (#7321f9a8)