Issue 107449
Summary Incorrect pointer assignment operation in binaries compiled with -O0 and -O1
Labels
Assignees
Reporter edumoot
    The test case matches that in #107443, with Godbolt links provided [here](https://godbolt.org/z/4M7hqPncb). At line 27, the long pointer should be set to NULL after the assignment, rather than setting g_values[7] to 0.

For the -O3 binary, the situation is debatable because the pointer local_ptr might be optimized away. Therefore, we are focusing on cases where local_ptr is clearly present in the binary.


We can reproduce this issue in LLVM versions 18.1.8, 17.0.6, and 16.0.3. 
```
clang -g -O0 -o 434_O0.out 434.c
clang -g -O1 -o 434_O1.out 434.c


(lldb) file 434_O0.out
(lldb) b 29
(lldb) r
[...]
* thread #1, name = '434_O0.out', stop reason = breakpoint 2.1
    frame #0: 0x00005555555551c2 434_O0.out`get_union_value at 434.c:29:12
   26  	 *local_ptr = 0;                  // Reset the value at index 7
   27  	 *g_ptr_wrapper = &local_ptr;     // Update reference
   28  	
-> 29 	    return g_union_vals[6];          // Return the last union value
 30  	}
   31  	
   32  	int main(void) {
(lldb) p *local_ptr
(long) 0
(lldb) p g_ptr_wrapper
(long ***volatile) 0x0000555555558098
(lldb) p *g_ptr_wrapper
(long **) 0x00007fffffffd890
(lldb) p **g_ptr_wrapper
(long *) 0x0000555555558068
(lldb) p ***g_ptr_wrapper
(long) 0
(lldb) p g_values[7]
(long) 0


(lldb) file 434_O1.out
(lldb) b 34
(lldb) r
[...]
(lldb) p *local_ptr
(long) 0
(lldb) s
* thread #1, name = '434_O1.out', stop reason = step in
    frame #0: 0x000055555555517e 434_O1.out`main at 434.c:34:5
   31  	
   32  	int main(void) {
   33  	    get_union_value();
-> 34  	 printf("Hello, world!\n");
   35  	    return 0;
   36  	}
 37  	
(lldb) p g_ptr_wrapper
(long ***volatile) 0x0000555555558098
(lldb) p *g_ptr_wrapper
(long **) 0x00007fffffffd8c0
(lldb) p **g_ptr_wrapper
(long *) 0x0000555555558068
(lldb) p ***g_ptr_wrapper
(long) 0
(lldb) p g_values[7]
(long) 0

```


_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to