https://github.com/venk-ks created https://github.com/llvm/llvm-project/pull/243629
Teach `-Wfortify-source` in `Sema::checkFortifiedBuiltinMemoryFunction` to diagnose destination buffer overflows/size mismatches for: - `memccpy` (`warn_fortify_source_size_mismatch`, since copying stops if the stop byte is found in the source buffer before reaching `n`, analogous to `strncpy`/`stpncpy`/`strlcpy`) - `memset_explicit` (`LibBuiltin<"string.h">` from C23, `warn_fortify_source_overflow`) - `explicit_bzero` (`GNULibBuiltin<"string.h">`, `warn_fortify_source_overflow`) Also pass explicit builtin parameter indices rather than `TheCall->getNumArgs() - 1` to `Checker.ComputeExplicitObjectSizeArgument` in both switch blocks so `__attribute__((diagnose_as_builtin(...)))` wrappers with extra parameters translate the size argument index properly. Assisted-by: Gemini >From 0ef6177e8e6d903644bf098bd7e95f6ad19e71fe Mon Sep 17 00:00:00 2001 From: Venkatesh Srinivasan <[email protected]> Date: Tue, 17 Nov 2026 21:50:51 +0000 Subject: [PATCH] [Clang][Sema] Add fortify warnings for memccpy, memset_explicit, and explicit_bzero Teach `-Wfortify-source` in `Sema::checkFortifiedBuiltinMemoryFunction` to diagnose destination buffer overflows/size mismatches for: - `memccpy` (`warn_fortify_source_size_mismatch`, since copying stops if the stop byte is found in the source buffer before reaching `n`, analogous to `strncpy`/`stpncpy`/`strlcpy`) - `memset_explicit` (`LibBuiltin<"string.h">` from C23, `warn_fortify_source_overflow`) - `explicit_bzero` (`GNULibBuiltin<"string.h">`, `warn_fortify_source_overflow`) Also pass explicit builtin parameter indices rather than `TheCall->getNumArgs() - 1` to `Checker.ComputeExplicitObjectSizeArgument` in both switch blocks so `__attribute__((diagnose_as_builtin(...)))` wrappers with extra parameters translate the size argument index properly. Assisted-by: Gemini --- clang/docs/ReleaseNotes.md | 4 +++ clang/include/clang/Basic/Builtins.td | 13 +++++++ clang/lib/Sema/SemaChecking.cpp | 27 +++++++++------ clang/test/Sema/attr-diagnose-as-builtin.c | 20 +++++++++++ clang/test/Sema/builtins-gnu-mode.c | 1 + clang/test/Sema/warn-fortify-source.c | 40 ++++++++++++++++++++++ 6 files changed, 94 insertions(+), 11 deletions(-) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index 0601ffcec82fdb..d2e2e7cbed2ddb 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -565,6 +565,10 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and `__builtin___vsprintf_chk`, or `__builtin_sprintf` is called with a format string whose output exceeds the destination buffer size. +- `-Wfortify-source` now diagnoses when `memccpy`, `memset_explicit`, or + `explicit_bzero` is called with a size argument larger than the destination + buffer. + - The `cannot overload a member function` diagnostic now describes the previous declaration first, matching the order in which the declarations appear in the source. (#GH219803) diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td index 15693d2fe128e5..c9318d965317be 100644 --- a/clang/include/clang/Basic/Builtins.td +++ b/clang/include/clang/Basic/Builtins.td @@ -3493,6 +3493,13 @@ def StrLen : LibBuiltin<"string.h"> { let AddBuiltinPrefixedAlias = 1; } +// C23 string.h +def MemsetExplicit : LibBuiltin<"string.h"> { + let Spellings = ["memset_explicit"]; + let Attributes = [NoThrow]; + let Prototype = "void*(void*, int, size_t)"; +} + // C99 stdio.h // FIXME: This list is incomplete. def Printf : LibBuiltin<"stdio.h"> { @@ -4007,6 +4014,12 @@ def StrlCat : GNULibBuiltin<"string.h"> { let AddBuiltinPrefixedAlias = 1; } +def ExplicitBZero : GNULibBuiltin<"string.h"> { + let Spellings = ["explicit_bzero"]; + let Attributes = [NoThrow]; + let Prototype = "void(void*, size_t)"; +} + def ObjcMsgSend : ObjCLibBuiltin<"objc_message.h"> { let Spellings = ["objc_msgSend"]; let Prototype = "id(id, SEL, ...)"; diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index dfbfbf91b7cab4..615d529459f949 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -1483,6 +1483,7 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, break; } + case Builtin::BImemccpy: case Builtin::BIstrncat: case Builtin::BI__builtin_strncat: case Builtin::BIstrncpy: @@ -1494,13 +1495,14 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, case Builtin::BIstrlcpy: case Builtin::BI__builtin_strlcpy: { // Whether these functions overflow depends on the runtime strlen of the - // string, not just the buffer size, so emitting the "always overflow" - // diagnostic isn't quite right. We should still diagnose passing a buffer - // size larger than the destination buffer though; this is a runtime abort - // in _FORTIFY_SOURCE mode, and is quite suspicious otherwise. + // string (or stop character for memccpy), not just the buffer size, so + // emitting the "always overflow" diagnostic isn't quite right. We should + // still diagnose passing a buffer size larger than the destination buffer + // though; this is a runtime abort in _FORTIFY_SOURCE mode, and is quite + // suspicious otherwise. DiagID = diag::warn_fortify_source_size_mismatch; - AccessSize = - Checker.ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); + unsigned SizeIdx = BuiltinID == Builtin::BImemccpy ? 3 : 2; + AccessSize = Checker.ComputeExplicitObjectSizeArgument(SizeIdx); BufferSize = Checker.ComputeSizeArgument(0); break; } @@ -1562,24 +1564,27 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, case Builtin::BIbzero: case Builtin::BI__builtin_bzero: + case Builtin::BIexplicit_bzero: case Builtin::BImemcpy: case Builtin::BI__builtin_memcpy: case Builtin::BImemmove: case Builtin::BI__builtin_memmove: case Builtin::BImemset: case Builtin::BI__builtin_memset: + case Builtin::BImemset_explicit: case Builtin::BImempcpy: case Builtin::BI__builtin_mempcpy: { DiagID = diag::warn_fortify_source_overflow; - AccessSize = - Checker.ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); + bool IsBZero = BuiltinID == Builtin::BIbzero || + BuiltinID == Builtin::BI__builtin_bzero || + BuiltinID == Builtin::BIexplicit_bzero; + AccessSize = Checker.ComputeExplicitObjectSizeArgument(IsBZero ? 1 : 2); BufferSize = Checker.ComputeSizeArgument(0); // Buffer overread doesn't make sense for memset/bzero. - if (BuiltinID != Builtin::BImemset && + if (!IsBZero && BuiltinID != Builtin::BImemset && BuiltinID != Builtin::BI__builtin_memset && - BuiltinID != Builtin::BIbzero && - BuiltinID != Builtin::BI__builtin_bzero) { + BuiltinID != Builtin::BImemset_explicit) { Checker.checkSourceOverread(/*SrcArgIdx=*/1, /*SizeArgIdx=*/2); } break; diff --git a/clang/test/Sema/attr-diagnose-as-builtin.c b/clang/test/Sema/attr-diagnose-as-builtin.c index 3634b1b2518938..b80ba71d476826 100644 --- a/clang/test/Sema/attr-diagnose-as-builtin.c +++ b/clang/test/Sema/attr-diagnose-as-builtin.c @@ -70,6 +70,9 @@ extern "C" { #endif extern int sscanf(const char *input, const char *format, ...); +extern void *memccpy(void *dst, const void *src, int c, size_t n); +extern void *memset_explicit(void *dst, int c, size_t n); +extern void explicit_bzero(void *dst, size_t n); #ifdef __cplusplus } @@ -92,6 +95,23 @@ void warn_my_vsprintf(void) { my_vsprintf("hello!", buf, list); // expected-warning{{'vsprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}} } +void *my_memccpy(int extra, void *dst, const void *src, int c, size_t n) + __attribute__((diagnose_as_builtin(memccpy, 2, 3, 4, 5))); +void *my_memset_explicit(int extra, void *dst, int c, size_t n) + __attribute__((diagnose_as_builtin(memset_explicit, 2, 3, 4))); +void my_explicit_bzero(int extra, void *dst, size_t n) + __attribute__((diagnose_as_builtin(explicit_bzero, 2, 3))); + +void warn_my_mem_zero_and_memccpy(void) { + char src[20], dst[10]; + my_memccpy(0, dst, src, '\0', 10); + my_memccpy(0, dst, src, '\0', 11); // expected-warning{{'memccpy' size argument is too large; destination buffer has size 10, but size argument is 11}} + my_memset_explicit(0, dst, 0, 10); + my_memset_explicit(0, dst, 0, 11); // expected-warning{{'memset_explicit' will always overflow; destination buffer has size 10, but size argument is 11}} + my_explicit_bzero(0, dst, 10); + my_explicit_bzero(0, dst, 11); // expected-warning{{'explicit_bzero' will always overflow; destination buffer has size 10, but size argument is 11}} +} + #ifdef __cplusplus template <class T> diff --git a/clang/test/Sema/builtins-gnu-mode.c b/clang/test/Sema/builtins-gnu-mode.c index d93b6fdef027b6..6c7cc20679ae6f 100644 --- a/clang/test/Sema/builtins-gnu-mode.c +++ b/clang/test/Sema/builtins-gnu-mode.c @@ -17,3 +17,4 @@ int _longjmp; int siglongjmp; int strlcpy; int strlcat; +int explicit_bzero; diff --git a/clang/test/Sema/warn-fortify-source.c b/clang/test/Sema/warn-fortify-source.c index 3e61e732682064..b3200ab20ab0b2 100644 --- a/clang/test/Sema/warn-fortify-source.c +++ b/clang/test/Sema/warn-fortify-source.c @@ -52,6 +52,9 @@ void bzero(void *dst, size_t n); size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); char *fgets(char *s, int size, FILE *stream); +void *memccpy(void *dst, const void *src, int c, size_t n); +void *memset_explicit(void *dst, int c, size_t n); +void explicit_bzero(void *dst, size_t n); #ifdef __cplusplus } @@ -161,6 +164,30 @@ void call_bcopy_bzero(void) { __builtin_bzero(dst, 11); // expected-warning {{'bzero' will always overflow; destination buffer has size 10, but size argument is 11}} } +void call_memccpy_explicit_zero(size_t runtime_n) { + char src[20], dst[10]; + memccpy(dst, src, '\0', 0); + memccpy(dst, src, '\0', 10); + memccpy(dst, src, '\0', 11); // expected-warning {{'memccpy' size argument is too large; destination buffer has size 10, but size argument is 11}} + memccpy(dst, src, '\0', -1); // expected-warning {{'memccpy' size argument is too large; destination buffer has size 10, but size argument is 18446744073709551615}} + // memccpy stops at the stop byte, so passing sizeof(dst) with a shorter + // source string should not trigger an overread warning. + memccpy(dst, "hi", '\0', sizeof(dst)); + memccpy(dst, src, '\0', runtime_n); + + memset_explicit(dst, 0, 0); + memset_explicit(dst, 0, 10); + memset_explicit(dst, 0, 11); // expected-warning {{'memset_explicit' will always overflow; destination buffer has size 10, but size argument is 11}} + memset_explicit(dst, 0, -1); // expected-warning {{'memset_explicit' will always overflow; destination buffer has size 10, but size argument is 18446744073709551615}} + memset_explicit(dst, 0, runtime_n); + + explicit_bzero(dst, 0); + explicit_bzero(dst, 10); + explicit_bzero(dst, 11); // expected-warning {{'explicit_bzero' will always overflow; destination buffer has size 10, but size argument is 11}} + explicit_bzero(dst, -1); // expected-warning {{'explicit_bzero' will always overflow; destination buffer has size 10, but size argument is 18446744073709551615}} + explicit_bzero(dst, runtime_n); +} + void call_fread_fwrite_fgets(FILE *fp) { char src[4]; fread(src, 2, 3, fp); // expected-warning {{'fread' will always overflow; destination buffer has size 4, but size argument is 6}} @@ -421,4 +448,17 @@ void call_call_memcpy() { call_memcpy_dep<9, 10>(); // expected-note {{in instantiation of function template specialization 'call_memcpy_dep<9, 10>' requested here}} // expected-warning@-7 {{'memcpy' will always overflow; destination buffer has size 9, but size argument is 10}} } + +namespace non_c_linkage { +void *memccpy(void *dst, const void *src, int c, size_t n); +void *memset_explicit(void *dst, int c, size_t n); +void explicit_bzero(void *dst, size_t n); + +void call_non_builtins() { + char src[20], dst[10]; + memccpy(dst, src, '\0', 20); + memset_explicit(dst, 0, 20); + explicit_bzero(dst, 20); +} +} // namespace non_c_linkage #endif _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
