Issue 143379
Summary [X86_64] Incorrect value of local variable after longjmp with optimizatio
Labels new issue
Assignees
Reporter hutuhutong
    Summary

When compiling the following C code with optimization level -O1, the local variable x exhibits unexpected behavior. In principle, under optimization levels -O1, -O2, and -O3, Clang should treat the allocation and modification of the variable x in a consistent manner. However, when the statement printf("Address of x: %p\n", (void*)&x); is included, a different result is observed at -O1, indicating inconsistent optimization behavior. This suggests that Clang handles the optimization of the printf call involving the address of x in a non-uniform way.



Details

========test.c========
#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>


void test_gamma() {
    int x=0;
    jmp_buf env;
    if (setjmp(env)) {
 printf("Jumped here\n");
    } else {
        x = 42;
 longjmp(env, 1);
    }
    printf("Value of x: %d   Address of x: %p\n", x, (void*)&x);
}

void test_gamma1() {
    int x=0;
    jmp_buf env;
 if (setjmp(env)) {
        printf("Jumped here\n");
    } else {
 x = 42;
        longjmp(env, 1);
    }
    int a = x*1;
 printf("Value of x: %d %d\n", x, a);
}

int main() {
 test_gamma();
    test_gamma1();
    return 0;
}

========output========
$ clang -O0 test.c -o test
$ ./test
Jumped here
Value of x: 42   Address of x: 0x7ffc7979655c
Jumped here
Value of x: 42 42

$ clang -O1 test.c -o test
$ ./test
Jumped here
Value of x: 42 Address of x: 0x7ffd6dc09afc
Jumped here
Value of x: 0 0

$ clang -O2 test.c -o test
$ ./test
Jumped here
Value of x: 0   Address of x: 0x7ffceb8ec25c
Jumped here
Value of x: 0 0

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to