https://github.com/akash-manna-sky updated https://github.com/llvm/llvm-project/pull/238857
>From aed9ef8392a4f56c188bbb4da08f1ca26a135232 Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Wed, 4 Nov 2026 10:43:30 +0530 Subject: [PATCH 1/3] [clang][CodeGen] Fix crash on `GNU89` redefinition of an inline `builtin` Fixes #110774 Since clang 14 an inline `builtin` (an `always_inline` `gnu_inline` definition of a library function) is emitted into a `.inline` clone and the function stays a declaration. The "already emitted" check in `EmitGlobalFunctionDefinition` only looks at the function, so a second GNU89 definition was emitted into the clone too and tripped `Function already has body?`. The check now also looks at the clone and skips the redefinition once it has a body. The first definition keeps winning, as for other extern inline functions, and a strong definition still replaces the clone. Assisted-by: LLM, for tests. --- clang/docs/ReleaseNotes.md | 1 + clang/lib/CodeGen/CodeGenModule.cpp | 10 ++ .../CodeGen/inline-builtin-redefinition.c | 128 ++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 clang/test/CodeGen/inline-builtin-redefinition.c diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index 6d1dbbbb6eb77b..415cd07ce77c8b 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -814,6 +814,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and Such declarations are now rejected, matching GCC. (#GH225033) - Fixed a crash on `break` out of a `for` loop whose init-statement declares a variable in C89 mode (a Clang extension), which happened at `-O1` and above or when the variable had a `cleanup` attribute. (#GH170243) +- Fixed a crash when an `always_inline` `gnu_inline` definition of a library builtin such as `memcmp` was redefined. (#GH110774) - Fixed an ICE hat occurred when using `__imag int/float` as lvalue in assignment. (#GH119498) - Fixed a C23 rejects-valid where `auto T x;` (with `T` a typedef-name) was rejected as a missing initializer. (#GH164930) - Fixed an assertion failure in `-Wsign-compare` when a negated or complemented vector of unsigned integers was compared against a signed constant. (#GH203575) diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index ce084487a69820..45d210b14fe7d0 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -7065,6 +7065,16 @@ void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD, if (!GV->isDeclaration()) return; + // An inline builtin is emitted into a ".inline" clone and GV stays a + // declaration (see CodeGenFunction::GenerateCode), so check the clone too: + // a GNU89 extern inline function may be defined more than once. + if (D->isInlineBuiltinDeclaration()) { + llvm::Function *Clone = + getModule().getFunction((GV->getName() + ".inline").str()); + if (Clone && !Clone->isDeclaration()) + return; + } + // We need to set linkage and visibility on the function before // generating code for it because various parts of IR generation // want to propagate this information down (e.g. to local static diff --git a/clang/test/CodeGen/inline-builtin-redefinition.c b/clang/test/CodeGen/inline-builtin-redefinition.c new file mode 100644 index 00000000000000..4f52f9dbfa272a --- /dev/null +++ b/clang/test/CodeGen/inline-builtin-redefinition.c @@ -0,0 +1,128 @@ +// RUN: %clang_cc1 -triple x86_64-linux-gnu -O3 -disable-llvm-passes -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,OPT --implicit-check-not=strncmp.inline +// RUN: %clang_cc1 -triple x86_64-linux-gnu -O0 -disable-llvm-passes -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,O0 --implicit-check-not=strncmp.inline +// RUN: %clang_cc1 -triple x86_64-linux-gnu -O3 -disable-llvm-passes -debug-info-kind=limited -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,OPT --implicit-check-not=strncmp.inline +// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=gnu89 -O3 -disable-llvm-passes -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,OPT --implicit-check-not=strncmp.inline +// +// GH110774: a GNU89 extern inline redefinition of an inline builtin (an +// always_inline gnu_inline definition of a library builtin) must not emit a +// second body into the ".inline" clone. + +typedef unsigned long size_t; +#define AVAILABLE_EXTERNALLY extern inline __attribute__((always_inline)) __attribute__((gnu_inline)) + +// CHECK: @pstrchr ={{.*}} global ptr @strchr + +// The reproducer: the first definition is emitted, the second is skipped. +AVAILABLE_EXTERNALLY int memcmp(const void *a, const void *b, size_t c) { + return __builtin_memcmp(a, b, c); +} + +int bar(const void *a, const void *b, size_t c) { + return memcmp(a, b, c); +} + +AVAILABLE_EXTERNALLY int memcmp(const void *a, const void *b, size_t c) { + return __builtin_memcmp(a, b, c); +} + +// CHECK-LABEL: define{{.*}} i32 @bar( +// CHECK: call i32 @memcmp.inline( +// CHECK: declare{{.*}} i32 @memcmp( +// CHECK-LABEL: define internal i32 @memcmp.inline( +// CHECK: call i32 @memcmp( + +// Three definitions. +AVAILABLE_EXTERNALLY void *memcpy(void *d, const void *s, size_t n) { + return __builtin_memcpy(d, s, n); +} + +void *copy(void *d, const void *s, size_t n) { + return memcpy(d, s, n); +} + +AVAILABLE_EXTERNALLY void *memcpy(void *d, const void *s, size_t n) { + return __builtin_memcpy(d, s, n); +} + +AVAILABLE_EXTERNALLY void *memcpy(void *d, const void *s, size_t n) { + return __builtin_memcpy(d, s, n); +} + +// CHECK-LABEL: define{{.*}} ptr @copy( +// CHECK: call ptr @memcpy.inline( +// CHECK: declare{{.*}} ptr @memcpy( +// CHECK-LABEL: define internal ptr @memcpy.inline( +// CHECK: call void @llvm.memcpy.p0.p0.i64( + +// Address taken between the definitions: no clone exists before emission. +AVAILABLE_EXTERNALLY char *strchr(const char *s, int c) { + return __builtin_strchr(s, c); +} + +char *(*pstrchr)(const char *, int) = strchr; + +AVAILABLE_EXTERNALLY char *strchr(const char *s, int c) { + return __builtin_strchr(s, c); +} + +// CHECK: declare{{.*}} ptr @strchr( + +// Extern inline followed by a strong definition: the strong one wins. +AVAILABLE_EXTERNALLY int strncmp(const char *a, const char *b, size_t n) { + return __builtin_strncmp(a, b, n); +} + +int cmp1(const char *a, const char *b, size_t n) { + return strncmp(a, b, n); +} + +int strncmp(const char *a, const char *b, size_t n) { + return 0; +} + +int cmp2(const char *a, const char *b, size_t n) { + return strncmp(a, b, n); +} + +// CHECK-LABEL: define{{.*}} i32 @cmp1( +// CHECK: call i32 @strncmp( +// CHECK-LABEL: define{{.*}} i32 @strncmp( +// CHECK-LABEL: define{{.*}} i32 @cmp2( +// CHECK: call i32 @strncmp( + +// Not an inline builtin without always_inline: ordinary available_externally. +extern inline __attribute__((gnu_inline)) size_t strlen(const char *s) { + return *s ? 1 : 0; +} + +size_t len(const char *s) { + return strlen(s); +} + +extern inline __attribute__((gnu_inline)) size_t strlen(const char *s) { + return *s ? 1 : 0; +} + +// CHECK-LABEL: define{{.*}} i64 @len( +// CHECK: call i64 @strlen( +// OPT: define available_externally i64 @strlen( +// O0: declare{{.*}} i64 @strlen( + +// Control: not a library builtin. +AVAILABLE_EXTERNALLY int foo(int x) { + return x + 1; +} + +int use_foo(int x) { + return foo(x); +} + +AVAILABLE_EXTERNALLY int foo(int x) { + return x + 1; +} + +// CHECK-LABEL: define{{.*}} i32 @use_foo( +// CHECK: call i32 @foo( +// CHECK-LABEL: define available_externally i32 @foo( + +// CHECK-LABEL: define internal ptr @strchr.inline( >From 74233938aa17e964a504b1ce3066a7296923c49a Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Thu, 5 Nov 2026 08:43:29 +0530 Subject: [PATCH 2/3] [clang][Sema] Reject a second `GNU89` extern inline definition like GCC. `canRedefineFunction` only checked the old definition, so a second extern inline definition of a `gnu_inline` function was accepted; GCC rejects it as a redefinition and only allows an extern inline definition to be followed by one that isn't extern inline. Diagnose it the same way and drop the `CodeGen` workaround from my previous commit. --- clang/docs/ReleaseNotes.md | 2 +- clang/lib/CodeGen/CodeGenModule.cpp | 10 -- clang/lib/Sema/SemaDecl.cpp | 8 +- .../CodeGen/inline-builtin-redefinition.c | 128 ------------------ clang/test/Sema/inline-redef.c | 4 + 5 files changed, 11 insertions(+), 141 deletions(-) delete mode 100644 clang/test/CodeGen/inline-builtin-redefinition.c diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index 415cd07ce77c8b..85d34adc6b3ae6 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -814,7 +814,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and Such declarations are now rejected, matching GCC. (#GH225033) - Fixed a crash on `break` out of a `for` loop whose init-statement declares a variable in C89 mode (a Clang extension), which happened at `-O1` and above or when the variable had a `cleanup` attribute. (#GH170243) -- Fixed a crash when an `always_inline` `gnu_inline` definition of a library builtin such as `memcmp` was redefined. (#GH110774) +- A second `extern inline` definition of a `gnu_inline` function is now rejected as a redefinition, as in GCC, which also fixes a crash in CodeGen. (#GH110774) - Fixed an ICE hat occurred when using `__imag int/float` as lvalue in assignment. (#GH119498) - Fixed a C23 rejects-valid where `auto T x;` (with `T` a typedef-name) was rejected as a missing initializer. (#GH164930) - Fixed an assertion failure in `-Wsign-compare` when a negated or complemented vector of unsigned integers was compared against a signed constant. (#GH203575) diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 45d210b14fe7d0..ce084487a69820 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -7065,16 +7065,6 @@ void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD, if (!GV->isDeclaration()) return; - // An inline builtin is emitted into a ".inline" clone and GV stays a - // declaration (see CodeGenFunction::GenerateCode), so check the clone too: - // a GNU89 extern inline function may be defined more than once. - if (D->isInlineBuiltinDeclaration()) { - llvm::Function *Clone = - getModule().getFunction((GV->getName() + ".inline").str()); - if (Clone && !Clone->isDeclaration()) - return; - } - // We need to set linkage and visibility on the function before // generating code for it because various parts of IR generation // want to propagate this information down (e.g. to local static diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index f3d38a555a524a..247ff426fa0e24 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -16411,7 +16411,10 @@ Sema::CheckForFunctionRedefinition(FunctionDecl *FD, } } - if (canRedefineFunction(Definition, getLangOpts())) + // A GNU89 extern inline definition may be followed by a definition that is + // not itself one; GCC rejects a second extern inline definition. + if (canRedefineFunction(Definition, getLangOpts()) && + !canRedefineFunction(FD, getLangOpts())) return; // Don't emit an error when this is redefinition of a typo-corrected @@ -16437,7 +16440,8 @@ Sema::CheckForFunctionRedefinition(FunctionDecl *FD, } if (getLangOpts().GNUMode && Definition->isInlineSpecified() && - Definition->getStorageClass() == SC_Extern) + Definition->getStorageClass() == SC_Extern && + !canRedefineFunction(Definition, getLangOpts())) Diag(FD->getLocation(), diag::err_redefinition_extern_inline) << FD << getLangOpts().CPlusPlus; else diff --git a/clang/test/CodeGen/inline-builtin-redefinition.c b/clang/test/CodeGen/inline-builtin-redefinition.c deleted file mode 100644 index 4f52f9dbfa272a..00000000000000 --- a/clang/test/CodeGen/inline-builtin-redefinition.c +++ /dev/null @@ -1,128 +0,0 @@ -// RUN: %clang_cc1 -triple x86_64-linux-gnu -O3 -disable-llvm-passes -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,OPT --implicit-check-not=strncmp.inline -// RUN: %clang_cc1 -triple x86_64-linux-gnu -O0 -disable-llvm-passes -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,O0 --implicit-check-not=strncmp.inline -// RUN: %clang_cc1 -triple x86_64-linux-gnu -O3 -disable-llvm-passes -debug-info-kind=limited -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,OPT --implicit-check-not=strncmp.inline -// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=gnu89 -O3 -disable-llvm-passes -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,OPT --implicit-check-not=strncmp.inline -// -// GH110774: a GNU89 extern inline redefinition of an inline builtin (an -// always_inline gnu_inline definition of a library builtin) must not emit a -// second body into the ".inline" clone. - -typedef unsigned long size_t; -#define AVAILABLE_EXTERNALLY extern inline __attribute__((always_inline)) __attribute__((gnu_inline)) - -// CHECK: @pstrchr ={{.*}} global ptr @strchr - -// The reproducer: the first definition is emitted, the second is skipped. -AVAILABLE_EXTERNALLY int memcmp(const void *a, const void *b, size_t c) { - return __builtin_memcmp(a, b, c); -} - -int bar(const void *a, const void *b, size_t c) { - return memcmp(a, b, c); -} - -AVAILABLE_EXTERNALLY int memcmp(const void *a, const void *b, size_t c) { - return __builtin_memcmp(a, b, c); -} - -// CHECK-LABEL: define{{.*}} i32 @bar( -// CHECK: call i32 @memcmp.inline( -// CHECK: declare{{.*}} i32 @memcmp( -// CHECK-LABEL: define internal i32 @memcmp.inline( -// CHECK: call i32 @memcmp( - -// Three definitions. -AVAILABLE_EXTERNALLY void *memcpy(void *d, const void *s, size_t n) { - return __builtin_memcpy(d, s, n); -} - -void *copy(void *d, const void *s, size_t n) { - return memcpy(d, s, n); -} - -AVAILABLE_EXTERNALLY void *memcpy(void *d, const void *s, size_t n) { - return __builtin_memcpy(d, s, n); -} - -AVAILABLE_EXTERNALLY void *memcpy(void *d, const void *s, size_t n) { - return __builtin_memcpy(d, s, n); -} - -// CHECK-LABEL: define{{.*}} ptr @copy( -// CHECK: call ptr @memcpy.inline( -// CHECK: declare{{.*}} ptr @memcpy( -// CHECK-LABEL: define internal ptr @memcpy.inline( -// CHECK: call void @llvm.memcpy.p0.p0.i64( - -// Address taken between the definitions: no clone exists before emission. -AVAILABLE_EXTERNALLY char *strchr(const char *s, int c) { - return __builtin_strchr(s, c); -} - -char *(*pstrchr)(const char *, int) = strchr; - -AVAILABLE_EXTERNALLY char *strchr(const char *s, int c) { - return __builtin_strchr(s, c); -} - -// CHECK: declare{{.*}} ptr @strchr( - -// Extern inline followed by a strong definition: the strong one wins. -AVAILABLE_EXTERNALLY int strncmp(const char *a, const char *b, size_t n) { - return __builtin_strncmp(a, b, n); -} - -int cmp1(const char *a, const char *b, size_t n) { - return strncmp(a, b, n); -} - -int strncmp(const char *a, const char *b, size_t n) { - return 0; -} - -int cmp2(const char *a, const char *b, size_t n) { - return strncmp(a, b, n); -} - -// CHECK-LABEL: define{{.*}} i32 @cmp1( -// CHECK: call i32 @strncmp( -// CHECK-LABEL: define{{.*}} i32 @strncmp( -// CHECK-LABEL: define{{.*}} i32 @cmp2( -// CHECK: call i32 @strncmp( - -// Not an inline builtin without always_inline: ordinary available_externally. -extern inline __attribute__((gnu_inline)) size_t strlen(const char *s) { - return *s ? 1 : 0; -} - -size_t len(const char *s) { - return strlen(s); -} - -extern inline __attribute__((gnu_inline)) size_t strlen(const char *s) { - return *s ? 1 : 0; -} - -// CHECK-LABEL: define{{.*}} i64 @len( -// CHECK: call i64 @strlen( -// OPT: define available_externally i64 @strlen( -// O0: declare{{.*}} i64 @strlen( - -// Control: not a library builtin. -AVAILABLE_EXTERNALLY int foo(int x) { - return x + 1; -} - -int use_foo(int x) { - return foo(x); -} - -AVAILABLE_EXTERNALLY int foo(int x) { - return x + 1; -} - -// CHECK-LABEL: define{{.*}} i32 @use_foo( -// CHECK: call i32 @foo( -// CHECK-LABEL: define available_externally i32 @foo( - -// CHECK-LABEL: define internal ptr @strchr.inline( diff --git a/clang/test/Sema/inline-redef.c b/clang/test/Sema/inline-redef.c index 40368b4ac042b8..18e7b5cda4d2ae 100644 --- a/clang/test/Sema/inline-redef.c +++ b/clang/test/Sema/inline-redef.c @@ -22,3 +22,7 @@ static int g(int b) {return b;} // Check that we ensure the types of the two definitions are the same GNU_INLINE extern inline int h(int a) {return a;} // expected-note{{previous definition is here}} int h(short b) {return b;} // expected-error{{conflicting types for 'h'}} + +// GH110774: a second extern inline definition is a redefinition, as in GCC. +GNU_INLINE extern inline int k(int a) {return a;} // expected-note{{previous definition is here}} +GNU_INLINE extern inline int k(int b) {return b;} // expected-error{{redefinition of 'k'}} >From f6e6e5939038caebb89df2c535afdb0d3ec4f709 Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Thu, 5 Nov 2026 09:01:42 +0530 Subject: [PATCH 3/3] Add few more tests from reproducers. --- clang/test/Sema/inline-redef.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/clang/test/Sema/inline-redef.c b/clang/test/Sema/inline-redef.c index 18e7b5cda4d2ae..b196e97a0ad46c 100644 --- a/clang/test/Sema/inline-redef.c +++ b/clang/test/Sema/inline-redef.c @@ -26,3 +26,13 @@ int h(short b) {return b;} // expected-error{{conflicting types for 'h'}} // GH110774: a second extern inline definition is a redefinition, as in GCC. GNU_INLINE extern inline int k(int a) {return a;} // expected-note{{previous definition is here}} GNU_INLINE extern inline int k(int b) {return b;} // expected-error{{redefinition of 'k'}} + +GNU_INLINE extern inline __attribute__((always_inline)) +int memcmp(const void *a, const void *b, __SIZE_TYPE__ c) { // expected-note{{previous definition is here}} + return __builtin_memcmp(a, b, c); +} +int bar(const void *a, const void *b, __SIZE_TYPE__ c) { return memcmp(a, b, c); } +GNU_INLINE extern inline __attribute__((always_inline)) +int memcmp(const void *a, const void *b, __SIZE_TYPE__ c) { // expected-error{{redefinition of 'memcmp'}} + return __builtin_memcmp(a, b, c); +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
