[clang] [Clang][AIX] Lower __builtin___*_chk string/memory builtins to the plain libc function (PR #248966)
zhijian lin via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 4 08:51:59 PST 2026
https://github.com/diggerlin updated https://github.com/llvm/llvm-project/pull/248966
>From f3da0fdf8bed7db174dff4dc7d84f7457c6e8c69 Mon Sep 17 00:00:00 2001
From: zhijian <zhijian at ca.ibm.com>
Date: Fri, 4 Dec 2026 10:18:56 -0500
Subject: [PATCH 1/2] fortify *_chk to lib call
---
clang/lib/CodeGen/CGBuiltin.cpp | 39 +++++
.../PowerPC/aix-fortify-chk-builtins.c | 145 ++++++++++++++++++
2 files changed, 184 insertions(+)
create mode 100644 clang/test/CodeGen/PowerPC/aix-fortify-chk-builtins.c
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 7b78abdb71c390..65bf37b7c45151 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -6907,6 +6907,45 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
}
}
+ // AIX libc does not provide the fortified *_chk functions. Any call to one of
+ // these builtins that was not folded above is lowered to the plain library
+ // function (e.g. strcpy) with the trailing object-size argument dropped; no
+ // runtime check is performed.
+ if (getTarget().getTriple().isOSAIX()) {
+ switch (BuiltinID) {
+ case Builtin::BI__builtin___memccpy_chk:
+ case Builtin::BI__builtin___memcpy_chk:
+ case Builtin::BI__builtin___memmove_chk:
+ case Builtin::BI__builtin___memset_chk:
+ case Builtin::BI__builtin___stpcpy_chk:
+ case Builtin::BI__builtin___stpncpy_chk:
+ case Builtin::BI__builtin___strcat_chk:
+ case Builtin::BI__builtin___strcpy_chk:
+ case Builtin::BI__builtin___strlcat_chk:
+ case Builtin::BI__builtin___strlcpy_chk:
+ case Builtin::BI__builtin___strncat_chk:
+ case Builtin::BI__builtin___strncpy_chk: {
+ // The builtin is named "__builtin___<name>_chk"; recover "<name>".
+ // getName() returns a std::string by value, so keep it alive.
+ std::string BuiltinName = getContext().BuiltinInfo.getName(BuiltinID);
+ StringRef Name = StringRef(BuiltinName)
+ .drop_front(StringRef("__builtin___").size())
+ .drop_back(StringRef("_chk").size());
+ CallArgList Args;
+ for (const Expr *Arg : llvm::drop_end(E->arguments()))
+ Args.add(RValue::get(EmitScalarExpr(Arg)), Arg->getType());
+ const CGFunctionInfo &FuncInfo =
+ CGM.getTypes().arrangeBuiltinFunctionCall(E->getType(), Args);
+ llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FuncInfo);
+ llvm::FunctionCallee Func = CGM.CreateRuntimeFunction(FTy, Name);
+ return EmitCall(FuncInfo, CGCallee::forDirect(Func), ReturnValueSlot(),
+ Args);
+ }
+ default:
+ break;
+ }
+ }
+
// If this is an alias for a lib function (e.g. __builtin_sin), emit
// the call using the normal call path, but using the unmangled
// version of the function name.
diff --git a/clang/test/CodeGen/PowerPC/aix-fortify-chk-builtins.c b/clang/test/CodeGen/PowerPC/aix-fortify-chk-builtins.c
new file mode 100644
index 00000000000000..0930b447020755
--- /dev/null
+++ b/clang/test/CodeGen/PowerPC/aix-fortify-chk-builtins.c
@@ -0,0 +1,145 @@
+// AIX libc does not provide the fortified *_chk functions. Check that the
+// __builtin___*_chk builtins are lowered to the plain library function with
+// the trailing object-size argument dropped, and that other targets still call
+// the *_chk function.
+
+// RUN: %clang_cc1 -triple=powerpc-ibm-aix-xcoff -emit-llvm -o - %s | \
+// RUN: FileCheck --check-prefixes=AIX --implicit-check-not=_chk %s
+// RUN: %clang_cc1 -triple=powerpc64-ibm-aix-xcoff -emit-llvm -o - %s | \
+// RUN: FileCheck --check-prefixes=AIX --implicit-check-not=_chk %s
+// RUN: %clang_cc1 -triple=powerpc64le-unknown-linux-gnu -emit-llvm -o - %s | \
+// RUN: FileCheck --check-prefixes=OTHER %s
+
+typedef __SIZE_TYPE__ size_t;
+
+// String builtins.
+
+// AIX-LABEL: @test_strcpy(
+// AIX: call ptr @strcpy(ptr {{.*}}, ptr {{.*}})
+// OTHER-LABEL: @test_strcpy(
+// OTHER: call ptr @__strcpy_chk(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}})
+char *test_strcpy(char *d, const char *s, size_t sz) {
+ return __builtin___strcpy_chk(d, s, sz);
+}
+
+// AIX-LABEL: @test_stpcpy(
+// AIX: call ptr @stpcpy(ptr {{.*}}, ptr {{.*}})
+// OTHER-LABEL: @test_stpcpy(
+// OTHER: call ptr @__stpcpy_chk(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}})
+char *test_stpcpy(char *d, const char *s, size_t sz) {
+ return __builtin___stpcpy_chk(d, s, sz);
+}
+
+// AIX-LABEL: @test_strcat(
+// AIX: call ptr @strcat(ptr {{.*}}, ptr {{.*}})
+// OTHER-LABEL: @test_strcat(
+// OTHER: call ptr @__strcat_chk(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}})
+char *test_strcat(char *d, const char *s, size_t sz) {
+ return __builtin___strcat_chk(d, s, sz);
+}
+
+// AIX-LABEL: @test_strncpy(
+// AIX: call ptr @strncpy(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}})
+// OTHER-LABEL: @test_strncpy(
+// OTHER: call ptr @__strncpy_chk(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}}, {{i32|i64}} {{.*}})
+char *test_strncpy(char *d, const char *s, size_t n, size_t sz) {
+ return __builtin___strncpy_chk(d, s, n, sz);
+}
+
+// AIX-LABEL: @test_stpncpy(
+// AIX: call ptr @stpncpy(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}})
+// OTHER-LABEL: @test_stpncpy(
+// OTHER: call ptr @__stpncpy_chk(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}}, {{i32|i64}} {{.*}})
+char *test_stpncpy(char *d, const char *s, size_t n, size_t sz) {
+ return __builtin___stpncpy_chk(d, s, n, sz);
+}
+
+// AIX-LABEL: @test_strncat(
+// AIX: call ptr @strncat(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}})
+// OTHER-LABEL: @test_strncat(
+// OTHER: call ptr @__strncat_chk(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}}, {{i32|i64}} {{.*}})
+char *test_strncat(char *d, const char *s, size_t n, size_t sz) {
+ return __builtin___strncat_chk(d, s, n, sz);
+}
+
+// AIX-LABEL: @test_strlcpy(
+// AIX: call {{i32|i64}} @strlcpy(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}})
+// OTHER-LABEL: @test_strlcpy(
+// OTHER: call {{i32|i64}} @__strlcpy_chk(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}}, {{i32|i64}} {{.*}})
+size_t test_strlcpy(char *d, const char *s, size_t n, size_t sz) {
+ return __builtin___strlcpy_chk(d, s, n, sz);
+}
+
+// AIX-LABEL: @test_strlcat(
+// AIX: call {{i32|i64}} @strlcat(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}})
+// OTHER-LABEL: @test_strlcat(
+// OTHER: call {{i32|i64}} @__strlcat_chk(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}}, {{i32|i64}} {{.*}})
+size_t test_strlcat(char *d, const char *s, size_t n, size_t sz) {
+ return __builtin___strlcat_chk(d, s, n, sz);
+}
+
+// Memory builtins with non-constant sizes (cannot be folded to an intrinsic).
+
+// AIX-LABEL: @test_memcpy(
+// AIX: call ptr @memcpy(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}})
+// OTHER-LABEL: @test_memcpy(
+// OTHER: call ptr @__memcpy_chk(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}}, {{i32|i64}} {{.*}})
+void *test_memcpy(void *d, const void *s, size_t n, size_t sz) {
+ return __builtin___memcpy_chk(d, s, n, sz);
+}
+
+// AIX-LABEL: @test_memmove(
+// AIX: call ptr @memmove(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}})
+// OTHER-LABEL: @test_memmove(
+// OTHER: call ptr @__memmove_chk(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}}, {{i32|i64}} {{.*}})
+void *test_memmove(void *d, const void *s, size_t n, size_t sz) {
+ return __builtin___memmove_chk(d, s, n, sz);
+}
+
+// AIX-LABEL: @test_memset(
+// AIX: call ptr @memset(ptr {{.*}}, i32 {{.*}}, {{i32|i64}} {{.*}})
+// OTHER-LABEL: @test_memset(
+// OTHER: call ptr @__memset_chk(ptr {{.*}}, i32 {{.*}}, {{i32|i64}} {{.*}}, {{i32|i64}} {{.*}})
+void *test_memset(void *d, int c, size_t n, size_t sz) {
+ return __builtin___memset_chk(d, c, n, sz);
+}
+
+// AIX-LABEL: @test_memccpy(
+// AIX: call ptr @memccpy(ptr {{.*}}, ptr {{.*}}, i32 {{.*}}, {{i32|i64}} {{.*}})
+// OTHER-LABEL: @test_memccpy(
+// OTHER: call ptr @__memccpy_chk(ptr {{.*}}, ptr {{.*}}, i32 {{.*}}, {{i32|i64}} {{.*}}, {{i32|i64}} {{.*}})
+void *test_memccpy(void *d, const void *s, int c, size_t n, size_t sz) {
+ return __builtin___memccpy_chk(d, s, c, n, sz);
+}
+
+// Constant sizes that are known to fit are still folded to the intrinsics on
+// every target, so the AIX redirect must not change them.
+
+// AIX-LABEL: @test_memcpy_fold(
+// AIX: call void @llvm.memcpy.p0.p0.{{i32|i64}}(
+// AIX-NOT: @memcpy
+// OTHER-LABEL: @test_memcpy_fold(
+// OTHER: call void @llvm.memcpy.p0.p0.i64(
+void test_memcpy_fold(char *d, char *s) {
+ __builtin___memcpy_chk(d, s, 128, 128);
+}
+
+// AIX-LABEL: @test_memset_fold(
+// AIX: call void @llvm.memset.p0.{{i32|i64}}(
+// AIX-NOT: @memset
+// OTHER-LABEL: @test_memset_fold(
+// OTHER: call void @llvm.memset.p0.i64(
+void test_memset_fold(char *d) {
+ __builtin___memset_chk(d, 42, 128, 128);
+}
+
+// A constant size that does not fit is not folded; AIX falls back to the
+// plain function, other targets keep the checking call.
+
+// AIX-LABEL: @test_memcpy_overflow(
+// AIX: call ptr @memcpy(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}}128)
+// OTHER-LABEL: @test_memcpy_overflow(
+// OTHER: call ptr @__memcpy_chk(ptr {{.*}}, ptr {{.*}}, i64 {{.*}}128, i64 {{.*}}16)
+void test_memcpy_overflow(char *d, char *s) {
+ __builtin___memcpy_chk(d, s, 128, 16);
+}
>From b9a3011db72c169be40f9eb0df87f8fe20cad2b2 Mon Sep 17 00:00:00 2001
From: zhijian <zhijian at ca.ibm.com>
Date: Fri, 4 Dec 2026 11:51:43 -0500
Subject: [PATCH 2/2] remove strlcpy/strlcat
---
clang/lib/CodeGen/CGBuiltin.cpp | 2 --
.../CodeGen/PowerPC/aix-fortify-chk-builtins.c | 16 ----------------
2 files changed, 18 deletions(-)
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 65bf37b7c45151..25e9f3a5233d74 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -6921,8 +6921,6 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
case Builtin::BI__builtin___stpncpy_chk:
case Builtin::BI__builtin___strcat_chk:
case Builtin::BI__builtin___strcpy_chk:
- case Builtin::BI__builtin___strlcat_chk:
- case Builtin::BI__builtin___strlcpy_chk:
case Builtin::BI__builtin___strncat_chk:
case Builtin::BI__builtin___strncpy_chk: {
// The builtin is named "__builtin___<name>_chk"; recover "<name>".
diff --git a/clang/test/CodeGen/PowerPC/aix-fortify-chk-builtins.c b/clang/test/CodeGen/PowerPC/aix-fortify-chk-builtins.c
index 0930b447020755..aa3c152174638f 100644
--- a/clang/test/CodeGen/PowerPC/aix-fortify-chk-builtins.c
+++ b/clang/test/CodeGen/PowerPC/aix-fortify-chk-builtins.c
@@ -62,22 +62,6 @@ char *test_strncat(char *d, const char *s, size_t n, size_t sz) {
return __builtin___strncat_chk(d, s, n, sz);
}
-// AIX-LABEL: @test_strlcpy(
-// AIX: call {{i32|i64}} @strlcpy(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}})
-// OTHER-LABEL: @test_strlcpy(
-// OTHER: call {{i32|i64}} @__strlcpy_chk(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}}, {{i32|i64}} {{.*}})
-size_t test_strlcpy(char *d, const char *s, size_t n, size_t sz) {
- return __builtin___strlcpy_chk(d, s, n, sz);
-}
-
-// AIX-LABEL: @test_strlcat(
-// AIX: call {{i32|i64}} @strlcat(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}})
-// OTHER-LABEL: @test_strlcat(
-// OTHER: call {{i32|i64}} @__strlcat_chk(ptr {{.*}}, ptr {{.*}}, {{i32|i64}} {{.*}}, {{i32|i64}} {{.*}})
-size_t test_strlcat(char *d, const char *s, size_t n, size_t sz) {
- return __builtin___strlcat_chk(d, s, n, sz);
-}
-
// Memory builtins with non-constant sizes (cannot be folded to an intrinsic).
// AIX-LABEL: @test_memcpy(
More information about the cfe-commits
mailing list