<html>
<head>
<base href="https://llvm.org/bugs/" />
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW " title="NEW --- - LSR miscompiles code after r243348" href="https://urldefense.proofpoint.com/v2/url?u=https-3A__llvm.org_bugs_show-5Fbug.cgi-3Fid-3D24347&d=AwMBaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=pF93YEPyB-J_PERP4DUZOJDzFVX5ZQ57vQk33wu0vio&m=OoeND0ckoT60EMl6oCo77PZOixvaKbgKKP00OPQXmLs&s=_GBbvjaUBBMc75Tc-IWJE3TL5MbAKY0rKi36H12L1Wc&e=">24347</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>LSR miscompiles code after r243348
</td>
</tr>
<tr>
<th>Product</th>
<td>libraries
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>Transformation Utilities
</td>
</tr>
<tr>
<th>Assignee</th>
<td>sanjoy@playingwithpointers.com
</td>
</tr>
<tr>
<th>Reporter</th>
<td>vonosmas@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvmbugs@cs.uiuc.edu, peter@pcc.me.uk, vonosmas@gmail.com
</td>
</tr>
<tr>
<th>Classification</th>
<td>Unclassified
</td>
</tr></table>
<p>
<div>
<pre>To reproduce:
$ cat example.c
#include <stdint.h>
#include <stdio.h>
struct S {
int a;
int b;
};
void foo(struct S* s, int last_index, uint8_t *arr)
{
int x, i, j, prev;
if (s->a && !s->b) {
i = 1;
} else {
i = 0;
}
prev = i - 1;
for (; i <= last_index; i++) {
j = arr[i];
fprintf(stderr, "### j = %d\n", j);
x = i - prev - 1;
prev = i;
fprintf(stderr, "### %d %d\n", x, prev);
}
}
int main() {
struct S s = { 0, 0 };
uint8_t arr[] = {0, 1, 2, 3};
foo(&s, 3, arr);
return 0;
}
$ ./bin/clang -O0 example.c && ./a.out
### j = 0
### 0 0
### j = 1
### 0 1
### j = 2
### 0 2
### j = 3
### 0 3
$ ./bin/clang -O1 example.c && ./a.out
Segmentation fault (core dumped)
Looks like the problem is that loop counter (which can be negative after the
transformation) is stored in 32-bit register, and is incorrectly zero-extended
instead of being sign-extended:
$ objdump -d a.out
0000000000400580 <foo>:
<...>
400589: 83 3f 00 cmpl $0x0,(%rdi)
40058c: 74 0b je 400599 <foo+0x19>
40058e: bb 01 00 00 00 mov $0x1,%ebx
400593: 83 7f 04 00 cmpl $0x0,0x4(%rdi)
400597: 74 02 je 40059b <foo+0x1b>
400599: 31 db xor %ebx,%ebx <--- %ebx is a
counter, can become zero here.
<...>
4005a4: ff cb dec %ebx <---
%ebx can be 0xffffffff
4005a6: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
4005ad: 00 00 00
4005b0: 89 d8 mov %ebx,%eax
4005b2: 41 0f b6 54 06 01 movzbl 0x1(%r14,%rax,1),%edx <----
here we add 0x100000000 instead of 0x0</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>