Issue 234433
Summary [InstCombine] Miscompilation while forwarding a memset to a load causing overflow
Labels new issue
Assignees
Reporter sairam2661
    **Fuzzer Generated Test**
**Reproducer**
https://alive2.llvm.org/ce/z/oh2G4G

**Test Commit**
[e633c7da8ff144a11b3d875956fbaea06b969070](https://github.com/llvm/llvm-project/commit/e633c7da8ff144a11b3d875956fbaea06b969070)

**Description**
Memset-to-load forwarding (`FindAvailableLoadedValue` offsets from #151924) checks the load offset with a 64-bit `Offset * 8` that can wrap: a load `2^61 + 1` bytes past a 16-byte `memset` is folded to 0.

**Steps to reproduce**
- Minimized test case (src), `input.ll`

```llvm
declare void @llvm.memset.p0.i64(ptr, i8, i64, i1)
define i1 @f(ptr %a) {
  call void @llvm.memset.p0.i64(ptr %a, i8 0, i64 16, i1 false)
  %g = getelementptr i8, ptr %a, i64 2305843009213693953
  %v = load i1, ptr %g, align 1
  ret i1 %v
}
```

- InstCombine output (tgt), `output.ll`

```llvm
declare void @llvm.memset.p0.i64(ptr writeonly captures(none), i8, i64, i1 immarg)

define i1 @f(ptr %a) {
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(16) %a, i8 0, i64 16, i1 false)
 ret i1 false
}
```

**Output**

```
define i1 @f(ptr %a) {
#0:
 memset ptr %a align 1, i8 0, i64 16
  %g = gep ptr %a, 1 x i64 2305843009213693953
  %v = load i1, ptr %g, align 1
  ret i1 %v
}
=>
define i1 @f(ptr %a) {
#0:
  memset ptr %a align 1, i8 0, i64 16
  ret i1 0
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:
ptr %a = pointer(non-local, block_id=1, offset=0) / Address=#x0000000000000001

Source:
ptr %g = pointer(non-local, block_id=1, offset=2305843009213693953) / Address=#x2000000000000002
i1 %v = #x1 (1)

SOURCE MEMORY STATE
===================
NON-LOCAL BLOCKS:
Block 0 >	size: 0	align: 1	alloc type: 0	alive: false	address: #x0000000000000000
Block 1 >	size: 2305843009213693954	align: 1	alloc type: 4	alive: true	address: #x0000000000000001
Contents:
*: #x01 / written with 1 bits / byte #0


Target:

TARGET MEMORY STATE
===================
NON-LOCAL BLOCKS:
Block 0 >	size: 0	align: 1	alloc type: 0	alive: false	address: #x0000000000000000
Block 1 >	size: 2305843009213693954	align: 1	alloc type: 4	alive: true	address: #x0000000000000001
Contents:
*: #x01 / written with 1 bits / byte #0

Source value: #x1 (1)
Target value: #x0 (0)

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
```

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

Reply via email to