-Co generates checks without a preceeding compare
Original Reporter info from Mantis: bozox
-
Reporter name: Seva Alekseyev
Original Reporter info from Mantis: bozox
- Reporter name: Seva Alekseyev
Description:
When -Co (check for integer overflow) option is specified for ARM compilation on Android, sometimes the compiler emits checks for signed integer overflow condition (the V flag) without a preceeding comparison operation. This way, the check can generate false positives if the V flag was set by a previous comparison operation somewhere else. It crashes the program.
Reference for iOS compilation: http://wiki.freepascal.org/iPhone/iPod_development
Steps to reproduce:
Generate a new library project for iOS. Paste the following code into the main library file:
function F(N: integer) : integer;
var NN : integer;
begin
NN := N;
if NN < 0 then NN := 0 - NN;
result := NN;
end;
procedure Crash; cdecl;
var
N, M : integer;
begin
N := -10;
M := F(N) div 4;
end;
exports
Crash;
Build the Debug configuration for iOS device (it has -Co by default). Link to the library from an Objective C iOS project, call Crash() from main(). Run on the device, observe the run-time error.
Does not reproduce on the simulator, probably Intel's model for handling CPU flags is very different.
Additional information:
Inside the function F, the following code is being generated for integer overflow check:
# [59] if NN < 0 then NN := 0 - NN;
ldr r0,[r11, #-48]
cmp r0,#0
blt Lj82
b Lj83
Lj82:
ldr r0,[r11, #-48]
rsb r0,r0,#0
cmp r0,#-2147483648 ; <======== Sets the V flag
bne Lj86 ; <======== But that's not the overflow condition *here*
blx LFPC_OVERFLOW$stub
Lj86:
str r0,[r11, #-48]
Inside the Crash procedure, on the other hand, a check for signed integer overflow is being performed regardless.
# [68] M := F(N) div 4;
ldr r0,[r11, #-40]
blx L_P$JIPADLIB_F$LONGINT$$LONGINT$stub
mov r1,r0,asr #31
add r0,r0,r1,lsr #30
mov r0,r0,asr #2
bvc Lj99 ;<========== V is set inside the function, never cleared
blx LFPC_OVERFLOW$stub ;<========== And here we crash.
Mantis conversion info:
- Mantis ID: 23849
- OS: MacOSX
- OS Build: 10.6.8
- Platform: iOS
- Version: 2.6.0
- Fixed in version: 3.0.0
- Fixed in revision: 26089 (#b6d279d4)