[clang] [Clang][Sema] Fix crash when a declaration shares the name of the size-specific `__sync` builtin (PR #246711)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 28 09:38:52 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Akash Manna (akash-manna-sky)
<details>
<summary>Changes</summary>
Fixes #<!-- -->120083
`BuiltinAtomicOverloaded` found the size-specific builtin it rewrites a `__sync_*` call to by name lookup. A program may declare `static int __sync_lock_test_and_set_4(volatile int *, int);` without redeclaring the builtin, so the lookup returned that function, the call was rewritten to it and CodeGen asserted on the argument types.
The declaration now comes from `Sema::getBuiltinFunctionDecl`, which keeps the lookup result only if it is the builtin and otherwise declares the builtin outside any scope. `BuildBuiltinCallExpr` had the same lookup and uses it too.
Assisted-by: LLMs.
---
Full diff: https://github.com/llvm/llvm-project/pull/246711.diff
8 Files Affected:
- (modified) clang/docs/ReleaseNotes.md (+3)
- (modified) clang/include/clang/Sema/Sema.h (+5)
- (modified) clang/lib/Sema/SemaChecking.cpp (+2-9)
- (modified) clang/lib/Sema/SemaDecl.cpp (+20)
- (modified) clang/lib/Sema/SemaExpr.cpp (+1-6)
- (added) clang/test/CodeGen/sync-builtins-shadowed.c (+94)
- (modified) clang/test/Sema/builtins.c (+17)
- (modified) clang/test/SemaCXX/builtins.cpp (+6)
``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index fe68c654f74a68..f3a1358a249554 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1009,6 +1009,9 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
format warnings to errors. (#GH211943)
- Fixed a crash when `__atomic_always_lock_free` or `__atomic_is_lock_free` is
called with a size of zero. (#GH170139, #GH120082)
+- Fixed a crash when a type-generic `__sync_*` builtin is called in a program
+ that declares a `static` function or a variable with the name of the
+ size-specific builtin, such as `__sync_lock_test_and_set_4`. (#GH120083)
- Fixed wrong code generation in `__builtin_clear_padding` wherein the wrong
bits of the following types were cleared: `_BitInt`, struct bitfields, and
packed boolean vectors. (#GH215809), (#GH216063), (#GH224033)
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index d9144899a6d51d..ebd427089488d5 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -4839,6 +4839,11 @@ class Sema final : public SemaBase {
NamedDecl *LazilyCreateBuiltin(IdentifierInfo *II, unsigned ID, Scope *S,
bool ForRedeclaration, SourceLocation Loc);
+ /// Get the declaration of builtin \p ID, creating it if needed. The result is
+ /// the builtin itself even if another declaration shares its name; null if
+ /// the type of the builtin is not available.
+ FunctionDecl *getBuiltinFunctionDecl(unsigned ID, SourceLocation Loc);
+
/// Get the outermost AttributedType node that sets a calling convention.
/// Valid types should not have multiple attributes with different CCs.
const AttributedType *getCallingConvAttributedType(QualType T) const;
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index ccb303624182f2..48d2ac15ddadeb 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -6212,19 +6212,12 @@ ExprResult Sema::BuiltinAtomicOverloaded(ExprResult TheCallResult) {
// Get the decl for the concrete builtin from this, we can tell what the
// concrete integer type we should convert to is.
unsigned NewBuiltinID = BuiltinIndices[BuiltinIndex][SizeIndex];
- std::string NewBuiltinName = Context.BuiltinInfo.getName(NewBuiltinID);
FunctionDecl *NewBuiltinDecl;
if (NewBuiltinID == BuiltinID)
NewBuiltinDecl = FDecl;
else {
- // Perform builtin lookup to avoid redeclaring it.
- DeclarationName DN(&Context.Idents.get(NewBuiltinName));
- LookupResult Res(*this, DN, DRE->getBeginLoc(), LookupOrdinaryName);
- LookupName(Res, TUScope, /*AllowBuiltinCreation=*/true);
- assert(Res.getFoundDecl());
- NewBuiltinDecl = dyn_cast<FunctionDecl>(Res.getFoundDecl());
- if (!NewBuiltinDecl)
- return ExprError();
+ NewBuiltinDecl = getBuiltinFunctionDecl(NewBuiltinID, DRE->getBeginLoc());
+ assert(NewBuiltinDecl && "type of a __sync builtin is always available");
}
// The first argument --- the pointer --- has a fixed type; we
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 63c581d3fb4f34..a780fae66b3a99 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -2535,6 +2535,26 @@ NamedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned ID,
return New;
}
+FunctionDecl *Sema::getBuiltinFunctionDecl(unsigned ID, SourceLocation Loc) {
+ LookupNecessaryTypesForBuiltin(TUScope, ID);
+ ASTContext::GetBuiltinTypeError Error;
+ QualType Type = Context.GetBuiltinType(ID, Error);
+ if (Error || Type.isNull())
+ return nullptr;
+
+ IdentifierInfo *II = &Context.Idents.get(Context.BuiltinInfo.getName(ID));
+ LookupResult R(*this, II, Loc, LookupOrdinaryName);
+ R.suppressDiagnostics();
+ LookupName(R, TUScope, /*AllowBuiltinCreation=*/true);
+ if (auto *FD = R.getAsSingle<FunctionDecl>())
+ if (FD->getBuiltinID() == ID && Context.hasSameType(FD->getType(), Type))
+ return FD;
+
+ // The name is taken by a declaration that is not the builtin (e.g. a static
+ // function in C); declare the builtin without adding it to any scope.
+ return CreateBuiltin(II, Type, ID, Loc);
+}
+
/// Typedef declarations don't have linkage, but they still denote the same
/// entity if their types are the same.
/// FIXME: This is notionally doing the same thing as ASTReaderDecl's
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 02fa1d8be333ef..77033e66bab6ba 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -7320,12 +7320,7 @@ ExprResult Sema::BuildCallExpr(Scope *Scope, Expr *Fn, SourceLocation LParenLoc,
Expr *Sema::BuildBuiltinCallExpr(SourceLocation Loc, unsigned Id,
MultiExprArg CallArgs) {
- std::string Name = Context.BuiltinInfo.getName(Id);
- LookupResult R(*this, &Context.Idents.get(Name), Loc,
- Sema::LookupOrdinaryName);
- LookupName(R, TUScope, /*AllowBuiltinCreation=*/true);
-
- auto *BuiltInDecl = R.getAsSingle<FunctionDecl>();
+ FunctionDecl *BuiltInDecl = getBuiltinFunctionDecl(Id, Loc);
assert(BuiltInDecl && "failed to find builtin declaration");
ExprResult DeclRef =
diff --git a/clang/test/CodeGen/sync-builtins-shadowed.c b/clang/test/CodeGen/sync-builtins-shadowed.c
new file mode 100644
index 00000000000000..97462d1cda21af
--- /dev/null
+++ b/clang/test/CodeGen/sync-builtins-shadowed.c
@@ -0,0 +1,94 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %s | FileCheck %s
+
+// GH120083: a type-generic __sync_* call is rewritten into a call to the
+// size-specific builtin; a declaration that merely shares that builtin's name
+// must not become the callee.
+
+static int __sync_lock_test_and_set_4(volatile int *, int);
+static short __sync_fetch_and_sub_2(volatile short *, short, ...);
+static void __sync_lock_release_1(volatile char *);
+static void __sync_fetch_and_or_16(volatile __int128 *, __int128);
+static int __sync_swap_4;
+typedef long long __sync_bool_compare_and_swap_8;
+enum { __sync_val_compare_and_swap_8 };
+
+char c;
+short s;
+int i;
+long long ll;
+__int128 i128;
+
+// CHECK-LABEL: define {{.*}}i32 @lock_test_and_set(
+// CHECK-NOT: call
+// CHECK: atomicrmw xchg ptr @i, i32 %{{.*}} seq_cst, align 4
+// CHECK-NOT: call
+// CHECK: ret i32
+int lock_test_and_set(int a) { return __sync_lock_test_and_set(&i, a); }
+
+// CHECK-LABEL: define {{.*}}i16 @fetch_and_sub(
+// CHECK-NOT: call
+// CHECK: atomicrmw sub ptr @s, i16 %{{.*}} seq_cst, align 2
+// CHECK-NOT: call
+// CHECK: ret i16
+short fetch_and_sub(short a) {
+ return __sync_fetch_and_sub((volatile short *)&s, a);
+}
+
+// CHECK-LABEL: define {{.*}}void @lock_release(
+// CHECK-NOT: call
+// CHECK: store atomic i8 0, ptr @c release, align 1
+// CHECK-NOT: call
+// CHECK: ret void
+void lock_release(void) { __sync_lock_release(&c); }
+
+// CHECK-LABEL: define {{.*}}void @fetch_and_or(
+// CHECK-NOT: call
+// CHECK: atomicrmw or ptr @i128, i128 %{{.*}} seq_cst, align 16
+// CHECK-NOT: call
+// CHECK: ret void
+void fetch_and_or(__int128 a) { (void)__sync_fetch_and_or(&i128, a); }
+
+// CHECK-LABEL: define {{.*}}i32 @swap(
+// CHECK-NOT: call
+// CHECK: atomicrmw xchg ptr @i, i32 %{{.*}} seq_cst, align 4
+// CHECK-NOT: call
+// CHECK: ret i32
+int swap(int a) { return __sync_swap(&i, a); }
+
+// CHECK-LABEL: define {{.*}}i32 @bool_compare_and_swap(
+// CHECK-NOT: call
+// CHECK: cmpxchg ptr @ll, i64 %{{.*}}, i64 %{{.*}} seq_cst seq_cst, align 8
+// CHECK-NOT: call
+// CHECK: ret i32
+int bool_compare_and_swap(long long a, long long b) {
+ return __sync_bool_compare_and_swap(&ll, a, b);
+}
+
+// CHECK-LABEL: define {{.*}}i64 @val_compare_and_swap(
+// CHECK-NOT: call
+// CHECK: cmpxchg ptr @ll, i64 %{{.*}}, i64 %{{.*}} seq_cst seq_cst, align 8
+// CHECK-NOT: call
+// CHECK: ret i64
+long long val_compare_and_swap(long long a, long long b) {
+ return __sync_val_compare_and_swap(&ll, a, b);
+}
+
+// In C the name is also found in enclosing block scopes.
+// CHECK-LABEL: define {{.*}}i64 @local_name(
+// CHECK-NOT: call
+// CHECK: atomicrmw add ptr @ll, i64 %{{.*}} seq_cst, align 8
+// CHECK-NOT: call
+// CHECK: ret i64
+long long local_name(long long a) {
+ long long __sync_fetch_and_add_8 = a;
+ return __sync_fetch_and_add(&ll, __sync_fetch_and_add_8);
+}
+
+// CHECK-LABEL: define {{.*}}i32 @and_and_fetch(
+// CHECK-NOT: call
+// CHECK: atomicrmw and ptr @i, i32 %{{.*}} seq_cst, align 4
+// CHECK-NOT: call
+// CHECK: ret i32
+int and_and_fetch(int a) { return __sync_and_and_fetch(&i, a); }
+
+// CHECK-NOT: @__sync_
diff --git a/clang/test/Sema/builtins.c b/clang/test/Sema/builtins.c
index 82bb43f041e126..df1ec8fb07195e 100644
--- a/clang/test/Sema/builtins.c
+++ b/clang/test/Sema/builtins.c
@@ -389,3 +389,20 @@ int test_is_fpclass(float x, int mask) {
float _Complex c = x;
int x7 = __builtin_isfpclass(c, 3); // expected-error{{floating point classification requires argument of floating point type (passed in '_Complex float')}}
}
+
+// GH120083: declarations sharing the name of the size-specific builtin that a
+// type-generic __sync builtin call is rewritten to.
+static int __sync_lock_test_and_set_4(volatile int *, int);
+static long long __sync_fetch_and_sub_8;
+typedef short __sync_swap_2;
+short __sync_add_and_fetch_2(volatile short *, short); // expected-error {{cannot redeclare builtin function '__sync_add_and_fetch_2'}} \
+ // expected-note {{'__sync_add_and_fetch_2' is a builtin with type 'short (volatile short *, short, ...)'}}
+void test_gh120083(int *ip, long long *llp, short *sp, int a, long long b, short c) {
+ int __sync_lock_release_4 = 0;
+ (void)__sync_lock_test_and_set(ip, a);
+ (void)__sync_fetch_and_sub(llp, b);
+ (void)__sync_swap(sp, c);
+ (void)__sync_add_and_fetch(sp, c);
+ __sync_lock_release(ip);
+ (void)__sync_lock_release_4;
+}
diff --git a/clang/test/SemaCXX/builtins.cpp b/clang/test/SemaCXX/builtins.cpp
index c955d5077532fe..18719ad9af5acb 100644
--- a/clang/test/SemaCXX/builtins.cpp
+++ b/clang/test/SemaCXX/builtins.cpp
@@ -195,6 +195,12 @@ static void __builtin_cpu_init(); // expected-error {{static declaration of '__b
expected-note {{'__builtin_cpu_init' is a builtin with type 'void () noexcept'}}
#endif
+// GH120083: in C++ this redeclares the builtin; the type-generic call below
+// must still refer to the builtin.
+static int __sync_lock_test_and_set_4(volatile int *, int); // expected-error {{cannot redeclare builtin function '__sync_lock_test_and_set_4'}} \
+ expected-note {{'__sync_lock_test_and_set_4' is a builtin with type 'int (volatile int *, int, ...) noexcept'}}
+int gh120083(int *p, int a) { return __sync_lock_test_and_set(p, a); }
+
#ifdef _MSC_VER
constexpr int x = [] {
__noop;
``````````
</details>
https://github.com/llvm/llvm-project/pull/246711
More information about the cfe-commits
mailing list