| Issue |
233691
|
| Summary |
[JumpTableToSwitch] Switches on the raw GEP index although a non-inbounds offset can wrap onto a table slot
|
| Labels |
miscompilation,
llvm:optimizations,
generated by fuzzer
|
| Assignees |
|
| Reporter |
ChoKyuWon
|
### reproducer
godbolt: https://godbolt.org/z/bz95WGsjv
alive: https://alive2.llvm.org/ce/z/ZE2aCC
```llvm
@table = constant [2 x ptr] [ptr @f0, ptr @f1]
define i32 @f0() {
ret i32 10
}
define i32 @f1() {
ret i32 11
}
define i32 @test(i64 %i) {
%p = getelementptr [2 x ptr], ptr @table, i64 0, i64 %i
%f = load ptr, ptr %p, align 8
%r = call i32 %f()
ret i32 %r
}
define i32 @main() {
%r = call i32 @test(i64 2305843009213693952) ; 2^61
ret i32 %r
}
```
```console
$ opt -S -passes=jump-table-to-switch repro.ll
define i32 @test(i64 %i) {
%p = getelementptr [2 x ptr], ptr @table, i64 0, i64 %i
%f = load ptr, ptr %p, align 8
switch i64 %i, label %default.switch.case.unreachable [
i64 0, label %call.0
i64 1, label %call.1
], !prof !0
default.switch.case.unreachable: ; preds = %0
unreachable
...
$ llubi repro.ll; echo $?
10
$ opt -S -passes=jump-table-to-switch repro.ll | llubi -
Immediate UB detected: Unreachable code.
$ clang -O2 repro.ll && ./a.out; echo $?
11
$ clang -O2 -mllvm -enable-jump-table-to-switch=false repro.ll && ./a.out; echo $?
10
```
### suggested fix
`parseJumpTable` (JumpTableToSwitch.cpp) takes the single variable offset from `collectOffset` and the expansion switches on that raw index with cases `0..N-1`. That is only equivalent when `index * stride` cannot wrap: here the GEP has no `inbounds`/`nusw`, so `2^61 * 8` wraps to offset 0 and the source loads `@f0`.
Bail out unless the GEP is `inbounds`/`nusw`, or switch on the offset computed in the index width instead of the raw index.
> Disclaimer: This test case was generated by a fuzzer, and the report was written with LLM assistance.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs