<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/95512>95512</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Incorrect debug information of pointer variables compiled with '-g -O3'
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
edumoot
</td>
</tr>
</table>
<pre>
We get a wrong value -21845- while dereferencing the pointer "ptr_global" in lldb environment. It should be pointing to "local_var" on line 20, instead of other address, so the correct one should be 3 .
```lldb
(lldb) b case.c:20
Breakpoint 1: where = case.out`main + 6 [inlined] func_1 at case.c:20:28, address = 0x0000000000001146
(lldb) r
Process 6657 launched: '/home/ad/Downloads/lldb/reproduce_bug/case.out' (x86_64)
Process 6657 stopped
* thread #1, name = 'case.out', stop reason = breakpoint 1.1
frame #0: 0x0000555555555146 case.out`main [inlined] func_1 at case.c:20:28
17 }
18
19 ptr_global = ptr_local_var;
-> 20 *ptr_to_global_pointer = &local_var;
21
22 return *global_pointer;
23 }
(lldb) fr v
(int) local_var = 3
(int *) ptr_local_var = 0x00007fffffffd894
(int *) ptr_global = 0x00007fffffffd894
(lldb) p *ptr_local_var
(int) 21845
(lldb) p *ptr_global
(int) 21845
(lldb) fr v -L
0x00007837c0001a40: (int) local_var = 3
scalar: (int *) ptr_local_var = 0x00007fffffffd894
scalar: (int *) ptr_global = 0x00007fffffffd894
```
`cat case.c`
```
#include "stdio.h"
static int global_var = 7;
static int *global_pointer = (void*)0;
static int func_1(void)
{
int *ptr_global = &global_var;
int local_var = 3;
int *ptr_local_var = &local_var;
int **ptr_to_global_pointer = &global_pointer;
for (global_var = 9; (global_var != 27); global_var += 3)
{
if (*ptr_global)
break;
}
ptr_global = ptr_local_var;
*ptr_to_global_pointer = &local_var;
return *global_pointer;
}
int main(void)
{
printf("%d", func_1());
return 0;
}
```
We can reproduce it in LLVM 18.1.2, LLVM17.0.6, and LLVM16.0.3, with:
```
clang case.c -g -O3 -o case.out
lldb case.out
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVt2O6jYQfhpzMyJyJv8XXMChSEfaqr3quVw5sSFujY0chz19-8pOQgK7S7eNEJHH8_PN55mJWdfJkxZiQ7IdyfYr1rvW2I3g_dkYt6oN_3tD6J7Q7Q8BJ-GAwZs1-gRXpnoBa4zLNFvDWyuVAC6sOAordCP1CVwr4GKkdsICQbw4-3pSpmaKIILUoBSvQeirtEafhXYRfHfQtaZXHOrR1PsBZ7y9Mg1Tr1dmvTkYDUpqAUgJfgOQunOCcTBHMK4VFhjnVnSd3-xMgNIYa0XjwGixiAIJREN-H_zndPh5pKMIy7DACmpoWCeihiRbpMPuzgr2V8ANMUm28NYKK4Ak-0HV9I7k9MykBoI7yIFkO6l9Gpxkezj2unmNgbk7x8kWS5_FmFDwRn_SxRPHaf4OnR0kv1vTeKs8zwpQrNdNK7iHRrAgeGjNWRA8ME7wsDdvWhnGO4KHwcvBios1vG_Ea92fCB5uWWABBMufZf6apwSrD2J1zlwugk-4tuBa6w-IYBL7dDQ7D8wQLBZuw3k5cwErWGd00KgXrEbx4BEA4GiDD0w8SSMn2fTEaf6e9K_SPYWICwBCKx-MFPtZXAbxvK4GtbnCA26_nIs22Q36a5L8Akhnx7j1is6Mpq-3lgnk5O89AADG9wh8R4z-rHC99QW2vfd3Z54E6ymlRdkcLVxvQqmdl90QBEjJctuH8Sp3qS5KtDgODy-r9DO7BWNPjCaAl4mwmZcHuGEifW43jqAvGXk2YP0yyEdwZVI0vulYSoc-ekpT1zDlj25S_O-EPfPwFeqmGbYcbEBy2syFf9t80MVE6kb13DcZdo5LE7UEcempc8zJBjyusdymfIpbwS103lXlWOXl1Ug-JEZvdu-sh469aY_lT4q5sKcgD-QQzGd0y0YI-o8H97j_WHBPOxNuR_S8rT9pztHP0fhPZvlAaUWS3aMYY7_jB2fYvdvaDfngPCdgJksevav7plhqTk-YvncZzn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