Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compile with GCC -O2 option generate different program

I heard that C compiler with/without optimization option may generate different program(compiling the program with optimizations causes it to behave differently), but I never encountered such case. Anyone can give simple example to show this?

like image 964
user607722 Avatar asked Dec 12 '22 14:12

user607722


2 Answers

For gcc 4.4.4, this differs with -O0 and -O2

void foo(int i) {
  foo(i+1);
}

main() {
  foo(0);
}

With optimizations this loops forever. Without optimizations, it crashes (stack overflow!)

Other and more realistic variants would typically be dependent on timing, vulnerable to float exactness variations, or depending on undefined behavior (uninitialized variables, heap/stack layout)

like image 63
Erik Avatar answered Feb 11 '23 10:02

Erik


If you look at the assembly generated by this code :

int main ()
{
    int i = 1;
    while (i) ;
    return 0;
}

Whitout the -O2 flag :

 .file   "test.c"
    .text
.globl main
    .type   main, @function
main:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $16, %esp
    movl    $1, -4(%ebp)
.L2:
    cmpl    $0, -4(%ebp)
    jne .L2
    movl    $0, %eax
    leave
    ret
    .size   main, .-main
    .ident  "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
    .section    .note.GNU-stack,"",@progbits

With the -O2 flag :

 .file   "test.c"
    .text
    .p2align 4,,15
.globl main
    .type   main, @function
main:
    pushl   %ebp
    movl    %esp, %ebp
.L2:
    jmp .L2
    .size   main, .-main
    .ident  "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
    .section    .note.GNU-stack,"",@progbits

With the -O2 flag, the declaration of i and the return value are ommited and you only have a label with a jump on this same label to constitute the infinite loop.

Without the -O2 flag, you can clearly see the allocation of the i space on the stack (subl $16, %esp) and initialization (movl $1, -4(%ebp)) as well as the evaluation of the while condition (cmpl $0, -4(%ebp)) and the return value of the main function (movl $0, %eax).

like image 32
Opera Avatar answered Feb 11 '23 09:02

Opera