Issue 234045
Summary [GlobalOpt] Constructor evaluation folds a load that straddles two struct members using only the first member
Labels miscompilation, llvm:optimizations, generated by fuzzer
Assignees
Reporter ChoKyuWon
    ### reproducer
godbolt: https://godbolt.org/z/sWqnqhfM7

```llvm
%top = type <{ { i32, i32 }, i64 }>

@g = global %top zeroinitializer
@r = global i64 0
@r2 = global i64 0
@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @ctor, ptr null }]

define internal void @ctor() {
 %p8 = getelementptr inbounds i8, ptr @g, i64 8
  store i64 1, ptr %p8, align 1
  %p4 = getelementptr inbounds i8, ptr @g, i64 4
  %v = load i64, ptr %p4, align 1     ; bytes 4..11 = 0x0000000100000000
  store i64 %v, ptr @r, align 1
  %w = load i64, ptr %p8, align 1     ; bytes 8..15 = 1
  store i64 %w, ptr @r2, align 1
  ret void
}
```

```console
$ opt -S -passes=globalopt repro.ll
@g = local_unnamed_addr global %top <{ { i32, i32 } zeroinitializer, i64 1 }>
@r = local_unnamed_addr global i64 0
@r2 = local_unnamed_addr global i64 1
@llvm.global_ctors = appending global [0 x { i32, ptr, ptr }] zeroinitializer
```

### suggested fix
`Evaluator::MutableValue::read` descends to the member that contains the load's start offset, but its size check compares the load with the enclosing aggregate (`AggTy`), not with that member.
`ConstantFoldLoadFromConst` then folds the 8-byte load at offset 4 from the 4 remaining bytes of the `{ i32, i32 }` member and drops the bytes of the next member, so `@r` becomes 0 instead of 4294967296.
Bail out (return nullptr) when the load does not fit inside the selected member.

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

Reply via email to