Issue 234325
Summary [GlobalOpt] Call that only passes `@__cxa_atexit` as an argument is deleted as an empty atexit registration
Labels miscompilation, llvm:optimizations, generated by fuzzer
Assignees
Reporter ChoKyuWon
    ### reproducer
godbolt: https://godbolt.org/z/Ynecq9nqa
alive: https://alive2.llvm.org/ce/z/NVQTf3

```llvm
declare i32 @__cxa_atexit(ptr, ptr, ptr)

define void @empty() {
  ret void
}

define i32 @f(ptr %fn, ptr %p) {
  ret i32 7
}

define i32 @main() {
  %r = call i32 @f(ptr @empty, ptr @__cxa_atexit)
  ret i32 %r
}
```

```console
$ opt -S -passes=globalopt repro.ll | grep -A1 'define i32 @main'
define i32 @main() local_unnamed_addr {
  ret i32 0

$ llubi repro.ll; echo $?
7
$ opt -S -passes=globalopt repro.ll | llubi -; echo $?
0

# same input with @__cxa_atexit renamed to @other: the call is kept
$ opt -S -passes=globalopt control.ll | grep -A2 'define i32 @main'
define i32 @main() local_unnamed_addr {
  %r = call i32 @f(ptr @empty, ptr @other)
  ret i32 %r
```

### suggested fix
`OptimizeEmptyGlobalAtExitDtors` (GlobalOpt.cpp) walks every user of the `__cxa_atexit`/`atexit` declaration and erases any `CallInst` whose first argument is an empty function, without checking that the declaration is the callee.
Skip calls where `CI->getCalledFunction() != CXAAtExitFn` (or iterate over uses and require `CI->isCallee(&U)`).

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

Reply via email to