Files
Yuxuan Chen 8db94f8248 [clang][Modules] Keep existing global definition on type mismatch in GetOrCreateLLVMGlobal (#223877)
When a global is requested for definition but a definition with the same
mangled name already exists, return it as is instead of creating a fresh
global and erasing the defined one, even if its type differs from the
requested type.

The type can legitimately differ when the initializer folded to a
different (e.g. anonymous) type, as happens with unions. Erasing the
defined global dangles pointers held by callers, e.g. the
`AssociatedData` of entries in `llvm.global_ctors`, leading to a
use-after-free crash during `EmitCtorList`. This also keeps the
deferred-decls "already emitted" check sound, since it queries with
`IsForDefinition`, which must be non-destructive.

Add a header-unit regression test: an inline variable with an
anonymous-union member, constant-initialized but requiring dynamic
destructor registration, must be emitted exactly once when imported from
header units. The folded initializer has an anonymous type that differs
from the declared type; a second emission used to replace (and erase)
the already-defined global, leaving a dangling pointer in
`llvm.global_ctors` (use-after-free, observable as a crash under ASan).

Assisted-By: Meta Muse 1.3

Co-authored-by: Michael Park <mcypark@gmail.com>
(cherry picked from commit 2a6fdac960de3a6bae854474f12e8f1edfcd9a04)
2026-11-02 10:44:26 +00:00
..
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…