https://github.com/nuclearcat created https://github.com/llvm/llvm-project/pull/196499
Based on and partially rewritten from PR#161737 by @ColinKinloch. This adds Clang fortify diagnostics for several `unistd.h` functions and extends the static analyzer coverage for related POSIX I/O calls. The Sema changes: * define builtins for `read`, `write`, `getcwd`, `readlink`, and `readlinkat` * add fortify checks for destination overflow and source over-read cases * introduce min/max operation-size handling so existing fortify checks can model both lower and upper bounds * recognize `pread`/`pread64`/`pwrite`/`pwrite64` in the fortify checker without declaring them as builtins, since their `off_t` argument width depends on platform/header configuration The analyzer changes: * add `SSIZE_MAX` size constraints for `read`, `write`, `readlink`, and `readlinkat` * add summaries for `pread`, `pread64`, `pwrite`, and `pwrite64` * tighten tests for over-large sizes, including cases such as passing `-1` to `size_t` parameters This may produce new `-Wfortify-source` diagnostics for code that passes buffer sizes larger than the known object size or larger than `SSIZE_MAX`. Related to/Part of PR#142230. Tested: * `ninja check-clang-sema` * `ninja check-clang-analysis` >From b421d3fcdb964fb1d8baf562578383ca8893ab36 Mon Sep 17 00:00:00 2001 From: Colin Kinloch <[email protected]> Date: Thu, 2 Oct 2025 22:01:40 +0100 Subject: [PATCH 1/8] [clang][Sema] Add fortify warnings for `unistd.h` Define as builtin and check for overflows and over-reads in: * `read` * `write` * `getcwd` * `readlink` * `readlinkat` Also recognize `pread`/`pread64`/`pwrite`/`pwrite64` by name in the fortify checker. They are deliberately not declared as builtins because their prototypes use `off_t`, whose width is platform- and macro-dependent (notably `_FILE_OFFSET_BITS`); a fixed builtin signature would clash with the system header on some targets and silently disable fortify there. It also enables `ssize_t` for use in builtin signatures. Signed-off-by: Colin Kinloch <[email protected]> --- clang/include/clang/Basic/Builtins.td | 43 +++++++++ .../clang/Basic/DiagnosticSemaKinds.td | 4 + clang/lib/AST/ASTContext.cpp | 9 +- clang/lib/Sema/SemaChecking.cpp | 89 ++++++++++++++++++- clang/test/Sema/warn-fortify-source.c | 63 +++++++++++++ clang/utils/TableGen/ClangBuiltinsEmitter.cpp | 1 + 6 files changed, 202 insertions(+), 7 deletions(-) diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td index 4a7eaeb3d353e..c1677cf2f5acc 100644 --- a/clang/include/clang/Basic/Builtins.td +++ b/clang/include/clang/Basic/Builtins.td @@ -3832,6 +3832,8 @@ def StrnCaseCmp : GNULibBuiltin<"strings.h"> { let RequiresUndef = 1; } +// POSIX unistd.h + def GNU_Exit : GNULibBuiltin<"unistd.h"> { let Spellings = ["_exit"]; let Attributes = [NoReturn]; @@ -3844,6 +3846,47 @@ def VFork : LibBuiltin<"unistd.h"> { let Prototype = "pid_t()"; } +def Read : LibBuiltin<"unistd.h"> { + let Spellings = ["read"]; + let Attributes = [NoThrow]; + let Prototype = "ssize_t(int, void*, size_t)"; + let AddBuiltinPrefixedAlias = 1; +} + +def Write : LibBuiltin<"unistd.h"> { + let Spellings = ["write"]; + let Attributes = [NoThrow]; + let Prototype = "ssize_t(int, void const*, size_t)"; + let AddBuiltinPrefixedAlias = 1; +} + +// pread/pread64/pwrite/pwrite64 are intentionally not declared as builtins. +// Their prototypes use off_t, whose width is platform- and macro-dependent +// (notably _FILE_OFFSET_BITS), so a fixed builtin signature would clash with +// the system header on some targets. Fortify checks for these functions are +// dispatched by name in Sema::checkFortifiedBuiltinMemoryFunction instead. + +def GetCWD : LibBuiltin<"unistd.h"> { + let Spellings = ["getcwd"]; + let Attributes = [NoThrow]; + let Prototype = "char*(char*, size_t)"; + let AddBuiltinPrefixedAlias = 1; +} + +def ReadLink : LibBuiltin<"unistd.h"> { + let Spellings = ["readlink"]; + let Attributes = [NoThrow]; + let Prototype = "ssize_t(char const* restrict, char* restrict, size_t)"; + let AddBuiltinPrefixedAlias = 1; +} + +def ReadLinkAt : LibBuiltin<"unistd.h"> { + let Spellings = ["readlinkat"]; + let Attributes = [NoThrow]; + let Prototype = "ssize_t(int, char const* restrict, char* restrict, size_t)"; + let AddBuiltinPrefixedAlias = 1; +} + // POSIX pthread.h def PthreadCreate : GNULibBuiltin<"pthread.h"> { diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index c69b2ce3648f8..745cb41f065c4 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -965,6 +965,10 @@ def warn_fortify_source_overflow def warn_fortify_source_size_mismatch : Warning< "'%0' size argument is too large; destination buffer has size %1," " but size argument is %2">, InGroup<FortifySource>; +def warn_fortify_destination_size_mismatch + : Warning<"'%0' size argument is too large; source buffer has size %2," + " but size argument is %1">, + InGroup<FortifySource>; def warn_fortify_strlen_overflow: Warning< "'%0' will always overflow; destination buffer has size %1," diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index a0894318dbd53..ec7eb82b8e76e 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -12696,9 +12696,12 @@ static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); Type = Context.BoolTy; break; - case 'z': // size_t. - assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); - Type = Context.getSizeType(); + case 'z': // size_t and ssize_t. + assert(HowLong == 0 && "Bad modifiers for 'z'!"); + if (Signed) + Type = Context.getSignedSizeType(); + else + Type = Context.getSizeType(); break; case 'w': // wchar_t. assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'w'!"); diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 4706fa5d3cde0..e51e7390ee5f3 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -1165,7 +1165,26 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, unsigned BuiltinID = UseDecl->getBuiltinID(/*ConsiderWrappers=*/true); - if (!BuiltinID) + // Some libc I/O functions are intentionally not Clang builtins because their + // prototypes use off_t, whose width is platform- and macro-dependent + // (notably _FILE_OFFSET_BITS). Recognize them by name so fortify checks + // work regardless of the platform's off_t encoding. + enum class LibCDispatch { None, PRead, PWrite }; + LibCDispatch LibC = LibCDispatch::None; + StringRef LibCName; + if (!BuiltinID && FD->isExternC() && FD->getIdentifier() && + TheCall->getNumArgs() == 4) { + StringRef Name = FD->getIdentifier()->getName(); + if (Name == "pread" || Name == "pread64") { + LibC = LibCDispatch::PRead; + LibCName = Name; + } else if (Name == "pwrite" || Name == "pwrite64") { + LibC = LibCDispatch::PWrite; + LibCName = Name; + } + } + + if (!BuiltinID && LibC == LibCDispatch::None) return; const TargetInfo &TI = getASTContext().getTargetInfo(); @@ -1253,8 +1272,19 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, std::optional<llvm::APSInt> DestinationSize; unsigned DiagID = 0; bool IsChkVariant = false; + bool IsTriggered = false; + + auto CompareSizeSourceToDest = [&]() { + return SourceSize && DestinationSize + ? std::optional<int>{llvm::APSInt::compareValues( + *SourceSize, *DestinationSize)} + : std::nullopt; + }; + + auto GetFunctionName = [&]() -> std::string { + if (LibC != LibCDispatch::None) + return LibCName.str(); - auto GetFunctionName = [&]() { std::string FunctionNameStr = getASTContext().BuiltinInfo.getName(BuiltinID); llvm::StringRef FunctionName = FunctionNameStr; @@ -1270,6 +1300,21 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, return FunctionName.str(); }; + if (LibC == LibCDispatch::PRead) { + // pread/pread64: ssize_t(int fd, void buf[.count], size_t count, off_t); + // Up to count(2) bytes are written into buf(1). + DiagID = diag::warn_fortify_source_size_mismatch; + SourceSize = ComputeExplicitObjectSizeArgument(2); + DestinationSize = ComputeSizeArgument(1); + IsTriggered = CompareSizeSourceToDest() > 0; + } else if (LibC == LibCDispatch::PWrite) { + // pwrite/pwrite64: ssize_t(int, const void buf[.count], size_t count, off_t); + // Up to count(2) bytes are read from buf(1). + DiagID = diag::warn_fortify_destination_size_mismatch; + SourceSize = ComputeSizeArgument(1); + DestinationSize = ComputeExplicitObjectSizeArgument(2); + IsTriggered = CompareSizeSourceToDest() < 0; + } else switch (BuiltinID) { default: return; @@ -1282,6 +1327,7 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, DiagID = diag::warn_fortify_strlen_overflow; SourceSize = ComputeStrLenArgument(1); DestinationSize = ComputeSizeArgument(0); + IsTriggered = CompareSizeSourceToDest() > 0; break; } @@ -1292,6 +1338,7 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, SourceSize = ComputeStrLenArgument(1); DestinationSize = ComputeExplicitObjectSizeArgument(2); IsChkVariant = true; + IsTriggered = CompareSizeSourceToDest() > 0; break; } @@ -1362,11 +1409,13 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, } else { DestinationSize = ComputeSizeArgument(0); } + IsTriggered = CompareSizeSourceToDest() > 0; break; } } return; } + case Builtin::BI__builtin___memcpy_chk: case Builtin::BI__builtin___memmove_chk: case Builtin::BI__builtin___memset_chk: @@ -1382,6 +1431,7 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, DestinationSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); IsChkVariant = true; + IsTriggered = CompareSizeSourceToDest() > 0; break; } @@ -1391,6 +1441,7 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, SourceSize = ComputeExplicitObjectSizeArgument(1); DestinationSize = ComputeExplicitObjectSizeArgument(3); IsChkVariant = true; + IsTriggered = CompareSizeSourceToDest() > 0; break; } @@ -1408,6 +1459,7 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, DiagID = diag::warn_fortify_source_size_mismatch; SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); DestinationSize = ComputeSizeArgument(0); + IsTriggered = CompareSizeSourceToDest() > 0; break; } @@ -1424,6 +1476,7 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, DiagID = diag::warn_fortify_source_overflow; SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); DestinationSize = ComputeSizeArgument(0); + IsTriggered = CompareSizeSourceToDest() > 0; break; } case Builtin::BIbcopy: @@ -1431,8 +1484,35 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, DiagID = diag::warn_fortify_source_overflow; SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); DestinationSize = ComputeSizeArgument(1); + IsTriggered = CompareSizeSourceToDest() > 0; + break; + } + + case Builtin::BIread: + case Builtin::BI__builtin_read: + case Builtin::BIreadlink: + case Builtin::BI__builtin_readlink: + case Builtin::BIreadlinkat: + case Builtin::BI__builtin_readlinkat: + case Builtin::BIgetcwd: + case Builtin::BI__builtin_getcwd: { + DiagID = diag::warn_fortify_source_size_mismatch; + SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); + DestinationSize = ComputeSizeArgument(TheCall->getNumArgs() - 2); + IsTriggered = CompareSizeSourceToDest() > 0; + break; + } + + case Builtin::BIwrite: + case Builtin::BI__builtin_write: { + DiagID = diag::warn_fortify_destination_size_mismatch; + SourceSize = ComputeSizeArgument(TheCall->getNumArgs() - 2); + DestinationSize = + ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); + IsTriggered = CompareSizeSourceToDest() < 0; break; } + case Builtin::BIsnprintf: case Builtin::BI__builtin_snprintf: case Builtin::BIvsnprintf: @@ -1472,11 +1552,12 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, const Expr *Dest = TheCall->getArg(0)->IgnoreCasts(); IdentifierInfo *FnInfo = FD->getIdentifier(); CheckSizeofMemaccessArgument(LenArg, Dest, FnInfo); + IsTriggered = CompareSizeSourceToDest() > 0; + break; } } - if (!SourceSize || !DestinationSize || - llvm::APSInt::compareValues(*SourceSize, *DestinationSize) <= 0) + if (!IsTriggered) return; std::string FunctionName = GetFunctionName(); diff --git a/clang/test/Sema/warn-fortify-source.c b/clang/test/Sema/warn-fortify-source.c index d0b519a516545..e53747ccd31e3 100644 --- a/clang/test/Sema/warn-fortify-source.c +++ b/clang/test/Sema/warn-fortify-source.c @@ -24,6 +24,14 @@ void *memcpy(void *dst, const void *src, size_t c); void bcopy(const void *src, void *dst, size_t n); void bzero(void *dst, size_t n); +typedef long ssize_t; +typedef long off_t; +typedef long long off64_t; +ssize_t pread(int fd, void *buf, size_t count, off_t offset); +ssize_t pread64(int fd, void *buf, size_t count, off64_t offset); +ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset); +ssize_t pwrite64(int fd, const void *buf, size_t count, off64_t offset); + #ifdef __cplusplus } #endif @@ -116,6 +124,61 @@ 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_read(void) { + char buf[10]; + __builtin_read(0, buf, 10); + __builtin_read(0, buf, 20); // expected-warning {{'read' size argument is too large; destination buffer has size 10, but size argument is 20}} +} + +void call_pread(void) { + char buf[10]; + pread(0, buf, 10, 0); + pread(0, buf, 20, 0); // expected-warning {{'pread' size argument is too large; destination buffer has size 10, but size argument is 20}} +} + +void call_pread64(void) { + char buf[10]; + pread64(0, buf, 10, 0); + pread64(0, buf, 20, 0); // expected-warning {{'pread64' size argument is too large; destination buffer has size 10, but size argument is 20}} +} + +void call_write(void) { + char buf[10]; + __builtin_write(0, buf, 10); + __builtin_write(0, buf, 20); // expected-warning {{'write' size argument is too large; source buffer has size 10, but size argument is 20}} +} + +void call_pwrite(void) { + char buf[10]; + pwrite(0, buf, 10, 0); + pwrite(0, buf, 20, 0); // expected-warning {{'pwrite' size argument is too large; source buffer has size 10, but size argument is 20}} +} + +void call_pwrite64(void) { + char buf[10]; + pwrite64(0, buf, 10, 0); + pwrite64(0, buf, 20, 0); // expected-warning {{'pwrite64' size argument is too large; source buffer has size 10, but size argument is 20}} +} + +void call_getcwd(void) { + char buf[10]; + __builtin_getcwd(buf, 10); + __builtin_getcwd(buf, 20); // expected-warning {{'getcwd' size argument is too large; destination buffer has size 10, but size argument is 20}} +} + +void call_readlink(void) { + char buf[10]; + __builtin_readlink("path", buf, 10); + __builtin_readlink("path", buf, 20); // expected-warning {{'readlink' size argument is too large; destination buffer has size 10, but size argument is 20}} +} + +void call_readlinkat(void) { + char buf[10]; + __builtin_readlinkat(0, "path", buf, 10); + __builtin_readlinkat(0, "path", buf, 20); // expected-warning {{'readlinkat' size argument is too large; destination buffer has size 10, but size argument is 20}} +} + + void call_snprintf(double d, int n) { char buf[10]; __builtin_snprintf(buf, 10, "merp"); diff --git a/clang/utils/TableGen/ClangBuiltinsEmitter.cpp b/clang/utils/TableGen/ClangBuiltinsEmitter.cpp index c2e38c0d6aeb8..ae7f99107c03b 100644 --- a/clang/utils/TableGen/ClangBuiltinsEmitter.cpp +++ b/clang/utils/TableGen/ClangBuiltinsEmitter.cpp @@ -371,6 +371,7 @@ class PrototypeParser { .Case("short", "s") .Case("sigjmp_buf", "SJ") .Case("size_t", "z") + .Case("ssize_t", "Sz") .Case("ucontext_t", "K") .Case("uint32_t", "UZi") .Case("uint64_t", "UWi") >From c5c82ab94452e641202089985600267aded017b2 Mon Sep 17 00:00:00 2001 From: Colin Kinloch <[email protected]> Date: Mon, 3 Nov 2025 02:53:09 +0000 Subject: [PATCH 2/8] [clang][Sema] Add min/max operation size for fortify checks Preserve existing bcopy/bzero fortify handling under the new min/max operation-size model. bzero is handled like memset, while bcopy is handled like memcpy with reversed source/destination arguments, allowing both destination overflow and source over-read diagnostics. --- .../clang/Basic/DiagnosticSemaKinds.td | 8 +- clang/lib/Sema/SemaChecking.cpp | 169 ++++++++++-------- clang/test/Sema/builtin-memcpy.c | 3 +- clang/test/Sema/warn-fortify-source.c | 39 +++- 4 files changed, 143 insertions(+), 76 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 745cb41f065c4..38d328b120f5d 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -962,12 +962,16 @@ def warn_builtin_chk_overflow : Warning< def warn_fortify_source_overflow : Warning<warn_builtin_chk_overflow.Summary>, InGroup<FortifySource>; +def warn_fortify_destination_over_read + : Warning<"'%0' will always over-read; source buffer has size %1," + " but size argument is %2">, + InGroup<FortifySource>; def warn_fortify_source_size_mismatch : Warning< "'%0' size argument is too large; destination buffer has size %1," " but size argument is %2">, InGroup<FortifySource>; def warn_fortify_destination_size_mismatch - : Warning<"'%0' size argument is too large; source buffer has size %2," - " but size argument is %1">, + : Warning<"'%0' size argument is too large; source buffer has size %1," + " but size argument is %2">, InGroup<FortifySource>; def warn_fortify_strlen_overflow: Warning< diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index e51e7390ee5f3..dfb898d94b1fb 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -1268,18 +1268,20 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, return std::nullopt; }; + // Size of the memory read from std::optional<llvm::APSInt> SourceSize; + // Size of the memory written to std::optional<llvm::APSInt> DestinationSize; - unsigned DiagID = 0; + // Maximum operation size for detecting possible out of bounds access + std::optional<llvm::APSInt> MaxOperationSize; + // Minimum operation size for detecting definite out of bounds access + std::optional<llvm::APSInt> MinOperationSize; + + unsigned DiagOverflowID = diag::warn_fortify_source_overflow; + unsigned DiagMayOverflowID = diag::warn_fortify_source_size_mismatch; + unsigned DiagOverReadID = diag::warn_fortify_destination_over_read; + unsigned DiagMayOverReadID = diag::warn_fortify_destination_size_mismatch; bool IsChkVariant = false; - bool IsTriggered = false; - - auto CompareSizeSourceToDest = [&]() { - return SourceSize && DestinationSize - ? std::optional<int>{llvm::APSInt::compareValues( - *SourceSize, *DestinationSize)} - : std::nullopt; - }; auto GetFunctionName = [&]() -> std::string { if (LibC != LibCDispatch::None) @@ -1303,17 +1305,13 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, if (LibC == LibCDispatch::PRead) { // pread/pread64: ssize_t(int fd, void buf[.count], size_t count, off_t); // Up to count(2) bytes are written into buf(1). - DiagID = diag::warn_fortify_source_size_mismatch; - SourceSize = ComputeExplicitObjectSizeArgument(2); DestinationSize = ComputeSizeArgument(1); - IsTriggered = CompareSizeSourceToDest() > 0; + MaxOperationSize = ComputeExplicitObjectSizeArgument(2); } else if (LibC == LibCDispatch::PWrite) { // pwrite/pwrite64: ssize_t(int, const void buf[.count], size_t count, off_t); // Up to count(2) bytes are read from buf(1). - DiagID = diag::warn_fortify_destination_size_mismatch; SourceSize = ComputeSizeArgument(1); - DestinationSize = ComputeExplicitObjectSizeArgument(2); - IsTriggered = CompareSizeSourceToDest() < 0; + MaxOperationSize = ComputeExplicitObjectSizeArgument(2); } else switch (BuiltinID) { default: @@ -1324,21 +1322,19 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, case Builtin::BIstpcpy: case Builtin::BI__builtin_strcpy: case Builtin::BIstrcpy: { - DiagID = diag::warn_fortify_strlen_overflow; - SourceSize = ComputeStrLenArgument(1); + DiagOverflowID = diag::warn_fortify_strlen_overflow; + MinOperationSize = ComputeStrLenArgument(1); DestinationSize = ComputeSizeArgument(0); - IsTriggered = CompareSizeSourceToDest() > 0; break; } case Builtin::BI__builtin___strcat_chk: case Builtin::BI__builtin___stpcpy_chk: case Builtin::BI__builtin___strcpy_chk: { - DiagID = diag::warn_fortify_strlen_overflow; - SourceSize = ComputeStrLenArgument(1); + DiagOverflowID = diag::warn_fortify_strlen_overflow; + MinOperationSize = ComputeStrLenArgument(1); DestinationSize = ComputeExplicitObjectSizeArgument(2); IsChkVariant = true; - IsTriggered = CompareSizeSourceToDest() > 0; break; } @@ -1362,12 +1358,12 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, auto Diagnose = [&](unsigned ArgIndex, unsigned DestSize, unsigned SourceSize) { - DiagID = diag::warn_fortify_scanf_overflow; unsigned Index = ArgIndex + DataIndex; std::string FunctionName = GetFunctionName(); DiagRuntimeBehavior(TheCall->getArg(Index)->getBeginLoc(), TheCall, - PDiag(DiagID) << FunctionName << (Index + 1) - << DestSize << SourceSize); + PDiag(diag::warn_fortify_scanf_overflow) + << FunctionName << (Index + 1) << DestSize + << SourceSize); }; auto ShiftedComputeSizeArgument = [&](unsigned Index) { @@ -1398,18 +1394,17 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, if (!analyze_format_string::ParsePrintfString( H, FormatBytes, FormatBytes + StrLen, getLangOpts(), Context.getTargetInfo(), false)) { - DiagID = H.isKernelCompatible() - ? diag::warn_format_overflow - : diag::warn_format_overflow_non_kprintf; - SourceSize = llvm::APSInt::getUnsigned(H.getSizeLowerBound()) - .extOrTrunc(SizeTypeWidth); + DiagOverflowID = H.isKernelCompatible() + ? diag::warn_format_overflow + : diag::warn_format_overflow_non_kprintf; + MinOperationSize = llvm::APSInt::getUnsigned(H.getSizeLowerBound()) + .extOrTrunc(SizeTypeWidth); if (BuiltinID == Builtin::BI__builtin___sprintf_chk) { DestinationSize = ComputeExplicitObjectSizeArgument(2); IsChkVariant = true; } else { DestinationSize = ComputeSizeArgument(0); } - IsTriggered = CompareSizeSourceToDest() > 0; break; } } @@ -1426,22 +1421,21 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, case Builtin::BI__builtin___stpncpy_chk: case Builtin::BI__builtin___memccpy_chk: case Builtin::BI__builtin___mempcpy_chk: { - DiagID = diag::warn_builtin_chk_overflow; - SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 2); + DiagOverflowID = diag::warn_builtin_chk_overflow; + MinOperationSize = + ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 2); DestinationSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); IsChkVariant = true; - IsTriggered = CompareSizeSourceToDest() > 0; break; } case Builtin::BI__builtin___snprintf_chk: case Builtin::BI__builtin___vsnprintf_chk: { - DiagID = diag::warn_builtin_chk_overflow; - SourceSize = ComputeExplicitObjectSizeArgument(1); + DiagOverflowID = diag::warn_builtin_chk_overflow; + MinOperationSize = ComputeExplicitObjectSizeArgument(1); DestinationSize = ComputeExplicitObjectSizeArgument(3); IsChkVariant = true; - IsTriggered = CompareSizeSourceToDest() > 0; break; } @@ -1456,35 +1450,40 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, // 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; - SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); + MaxOperationSize = + ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); DestinationSize = ComputeSizeArgument(0); - IsTriggered = CompareSizeSourceToDest() > 0; break; } + case Builtin::BImemset: + case Builtin::BI__builtin_memset: case Builtin::BIbzero: - case Builtin::BI__builtin_bzero: + case Builtin::BI__builtin_bzero: { + MinOperationSize = MaxOperationSize = + ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); + DestinationSize = ComputeSizeArgument(0); + break; + } + 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::BImempcpy: case Builtin::BI__builtin_mempcpy: { - DiagID = diag::warn_fortify_source_overflow; - SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); + MinOperationSize = MaxOperationSize = + ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); DestinationSize = ComputeSizeArgument(0); - IsTriggered = CompareSizeSourceToDest() > 0; + SourceSize = ComputeSizeArgument(1); break; } case Builtin::BIbcopy: case Builtin::BI__builtin_bcopy: { - DiagID = diag::warn_fortify_source_overflow; - SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); + MinOperationSize = MaxOperationSize = + ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); + SourceSize = ComputeSizeArgument(0); DestinationSize = ComputeSizeArgument(1); - IsTriggered = CompareSizeSourceToDest() > 0; break; } @@ -1496,20 +1495,17 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, case Builtin::BI__builtin_readlinkat: case Builtin::BIgetcwd: case Builtin::BI__builtin_getcwd: { - DiagID = diag::warn_fortify_source_size_mismatch; - SourceSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); DestinationSize = ComputeSizeArgument(TheCall->getNumArgs() - 2); - IsTriggered = CompareSizeSourceToDest() > 0; + MaxOperationSize = + ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); break; } case Builtin::BIwrite: case Builtin::BI__builtin_write: { - DiagID = diag::warn_fortify_destination_size_mismatch; SourceSize = ComputeSizeArgument(TheCall->getNumArgs() - 2); - DestinationSize = + MaxOperationSize = ComputeExplicitObjectSizeArgument(TheCall->getNumArgs() - 1); - IsTriggered = CompareSizeSourceToDest() < 0; break; } @@ -1517,12 +1513,11 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, case Builtin::BI__builtin_snprintf: case Builtin::BIvsnprintf: case Builtin::BI__builtin_vsnprintf: { - DiagID = diag::warn_fortify_source_size_mismatch; - SourceSize = ComputeExplicitObjectSizeArgument(1); + MaxOperationSize = ComputeExplicitObjectSizeArgument(1); const auto *FormatExpr = TheCall->getArg(2)->IgnoreParenImpCasts(); StringRef FormatStrRef; size_t StrLen; - if (SourceSize && + if (MaxOperationSize && ProcessFormatStringLiteral(FormatExpr, FormatStrRef, StrLen, Context)) { EstimateSizeFormatHandler H(FormatStrRef); const char *FormatBytes = FormatStrRef.data(); @@ -1532,13 +1527,13 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, llvm::APSInt FormatSize = llvm::APSInt::getUnsigned(H.getSizeLowerBound()) .extOrTrunc(SizeTypeWidth); - if (FormatSize > *SourceSize && *SourceSize != 0) { + if (FormatSize > *MaxOperationSize && *MaxOperationSize != 0) { unsigned TruncationDiagID = H.isKernelCompatible() ? diag::warn_format_truncation : diag::warn_format_truncation_non_kprintf; SmallString<16> SpecifiedSizeStr; SmallString<16> FormatSizeStr; - SourceSize->toString(SpecifiedSizeStr, /*Radix=*/10); + MaxOperationSize->toString(SpecifiedSizeStr, /*Radix=*/10); FormatSize.toString(FormatSizeStr, /*Radix=*/10); DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, PDiag(TruncationDiagID) @@ -1552,23 +1547,57 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD, const Expr *Dest = TheCall->getArg(0)->IgnoreCasts(); IdentifierInfo *FnInfo = FD->getIdentifier(); CheckSizeofMemaccessArgument(LenArg, Dest, FnInfo); - IsTriggered = CompareSizeSourceToDest() > 0; break; } } - if (!IsTriggered) - return; - std::string FunctionName = GetFunctionName(); + SmallString<16> MaxOpStr; + SmallString<16> MinOpStr; + + if (MinOperationSize) + MinOperationSize->toString(MinOpStr, /*Radix=*/10); + if (MaxOperationSize) + MaxOperationSize->toString(MaxOpStr, /*Radix=*/10); + + if (DestinationSize) { + SmallString<16> DestinationStr; + DestinationSize->toString(DestinationStr, /*Radix=*/10); + // Check for definite overflow + if (MinOperationSize && + llvm::APSInt::compareValues(*MinOperationSize, *DestinationSize) > 0) { + DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, + PDiag(DiagOverflowID) + << FunctionName << DestinationStr << MinOpStr); + } + // Check for possible overflow + else if (MaxOperationSize && llvm::APSInt::compareValues( + *MaxOperationSize, *DestinationSize) > 0) { + DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, + PDiag(DiagMayOverflowID) + << FunctionName << DestinationStr << MaxOpStr); + } + } + + if (SourceSize) { + SmallString<16> SourceStr; + SourceSize->toString(SourceStr, /*Radix=*/10); + // Check for definite over-read + if (MinOperationSize && + llvm::APSInt::compareValues(*MinOperationSize, *SourceSize) > 0) { + DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, + PDiag(DiagOverReadID) + << FunctionName << SourceStr << MinOpStr); - SmallString<16> DestinationStr; - SmallString<16> SourceStr; - DestinationSize->toString(DestinationStr, /*Radix=*/10); - SourceSize->toString(SourceStr, /*Radix=*/10); - DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, - PDiag(DiagID) - << FunctionName << DestinationStr << SourceStr); + } + // Check for possible over-read + else if (MaxOperationSize && + llvm::APSInt::compareValues(*MaxOperationSize, *SourceSize) > 0) { + DiagRuntimeBehavior(TheCall->getBeginLoc(), TheCall, + PDiag(DiagMayOverReadID) + << FunctionName << SourceStr << MaxOpStr); + } + } } static bool BuiltinSEHScopeCheck(Sema &SemaRef, CallExpr *TheCall, diff --git a/clang/test/Sema/builtin-memcpy.c b/clang/test/Sema/builtin-memcpy.c index 2a55e78034a02..af7f2034f3c30 100644 --- a/clang/test/Sema/builtin-memcpy.c +++ b/clang/test/Sema/builtin-memcpy.c @@ -7,7 +7,8 @@ /// Zero-sized structs should not crash. int b() { struct { } a[10]; - __builtin_memcpy(&a[2], a, 2); // c-warning {{buffer has size 0, but size argument is 2}} + __builtin_memcpy(&a[2], a, 2); // c-warning {{buffer has size 0, but size argument is 2}} \ + // c-warning {{buffer has size 0, but size argument is 2}} return 0; } diff --git a/clang/test/Sema/warn-fortify-source.c b/clang/test/Sema/warn-fortify-source.c index e53747ccd31e3..33623cd8d8420 100644 --- a/clang/test/Sema/warn-fortify-source.c +++ b/clang/test/Sema/warn-fortify-source.c @@ -40,6 +40,8 @@ void call_memcpy(void) { char dst[10]; char src[20]; memcpy(dst, src, 20); // expected-warning {{memcpy' will always overflow; destination buffer has size 10, but size argument is 20}} + memcpy(dst, src, 21); // expected-warning {{memcpy' will always overflow; destination buffer has size 10, but size argument is 21}} \ + // expected-warning {{memcpy' will always over-read; source buffer has size 20, but size argument is 21}} if (sizeof(dst) == sizeof(src)) memcpy(dst, src, 20); // no warning, unreachable @@ -53,18 +55,29 @@ void call_memcpy_type(void) { struct pair p; char buf[20]; memcpy(&p.first, buf, 20); // expected-warning {{memcpy' will always overflow; destination buffer has size 8, but size argument is 20}} + memcpy(&p.first, buf, 21); // expected-warning {{memcpy' will always overflow; destination buffer has size 8, but size argument is 21}} \ + // expected-warning {{memcpy' will always over-read; source buffer has size 20, but size argument is 21}} +} + +void call_memcpy_chk(void) { + char dst[10]; + char src[10]; + __builtin___memcpy_chk(dst, src, 10, 10); + __builtin___memcpy_chk(dst, src, 10, 9); // expected-warning {{memcpy' will always overflow; destination buffer has size 9, but size argument is 10}} } void call_strncat(void) { char s1[10], s2[20]; __builtin_strncat(s2, s1, 20); __builtin_strncat(s1, s2, 20); // expected-warning {{'strncat' size argument is too large; destination buffer has size 10, but size argument is 20}} + __builtin_strncat(s1, "abcd", 20); // expected-warning {{'strncat' size argument is too large; destination buffer has size 10, but size argument is 20}} } void call_strncpy(void) { char s1[10], s2[20]; __builtin_strncpy(s2, s1, 20); __builtin_strncpy(s1, s2, 20); // expected-warning {{'strncpy' size argument is too large; destination buffer has size 10, but size argument is 20}} + __builtin_strncpy(s1, "abcd", 20); // expected-warning {{'strncpy' size argument is too large; destination buffer has size 10, but size argument is 20}} } void call_stpncpy(void) { @@ -102,9 +115,17 @@ void call_stpcpy(void) { __builtin_stpcpy(dst2, src); // expected-warning {{'stpcpy' will always overflow; destination buffer has size 4, but the source string has length 5 (including NUL byte)}} } +void call_stpcpy_chk(void) { + const char *const src = "abcd"; + char dst1[5]; + __builtin___stpcpy_chk(dst1, src, 5); + __builtin___stpcpy_chk(dst1, src, 4); // expected-warning {{'stpcpy' will always overflow; destination buffer has size 4, but the source string has length 5 (including NUL byte)}} +} + void call_memmove(void) { char s1[10], s2[20]; - __builtin_memmove(s2, s1, 20); + __builtin_memmove(s2, s1, 10); + __builtin_memmove(s2, s1, 20); // expected-warning {{'memmove' will always over-read; source buffer has size 10, but size argument is 20}} __builtin_memmove(s1, s2, 20); // expected-warning {{'memmove' will always overflow; destination buffer has size 10, but size argument is 20}} } @@ -318,11 +339,23 @@ template <int A, int B> void call_memcpy_dep() { char bufferA[A]; char bufferB[B]; - memcpy(bufferA, bufferB, 10); // expected-warning{{'memcpy' will always overflow; destination buffer has size 9, but size argument is 10}} + if (sizeof(bufferA) < 10 && sizeof(bufferB) < 10) { + memcpy(bufferA, bufferB, 10); // expected-warning{{'memcpy' will always overflow; destination buffer has size 9, but size argument is 10}} \ + // expected-warning{{'memcpy' will always over-read; source buffer has size 9, but size argument is 10}} + } else if (sizeof(bufferA) < 10) { + memcpy(bufferA, bufferB, 10); // expected-warning{{'memcpy' will always overflow; destination buffer has size 9, but size argument is 10}} + } else if (sizeof(bufferB) < 10) { + memcpy(bufferA, bufferB, 10); // expected-warning{{'memcpy' will always over-read; source buffer has size 9, but size argument is 10}} + } else { + memcpy(bufferA, bufferB, 10); + } + } void call_call_memcpy() { - call_memcpy_dep<10, 9>(); + call_memcpy_dep<10, 10>(); + call_memcpy_dep<10, 9>(); // expected-note {{in instantiation of function template specialization 'call_memcpy_dep<10, 9>' requested here}} call_memcpy_dep<9, 10>(); // expected-note {{in instantiation of function template specialization 'call_memcpy_dep<9, 10>' requested here}} + call_memcpy_dep<9, 9>(); // expected-note {{in instantiation of function template specialization 'call_memcpy_dep<9, 9>' requested here}} } #endif >From 94f2a67f883b3626dfe9d4d9e91e52ddf29da0de Mon Sep 17 00:00:00 2001 From: Colin Kinloch <[email protected]> Date: Mon, 3 Nov 2025 20:46:15 +0000 Subject: [PATCH 3/8] [clang][Sema] Avoid builtin redeclaration for asm-label test As `readlink` is now defined as bultin it throws an error when redeclared with a different type: "incompatible redeclaration of library function function 'readlink'" As `sync` has the type `void(void)`, this issue should not happen in the future. --- clang/test/Sema/asm-label.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/test/Sema/asm-label.c b/clang/test/Sema/asm-label.c index eb0259863b2b2..a8b6afc252c2d 100644 --- a/clang/test/Sema/asm-label.c +++ b/clang/test/Sema/asm-label.c @@ -25,6 +25,6 @@ int z __asm__("zooms"); // expected-error{{conflicting asm label}} // No diagnostics on the following. -void __real_readlink(void) __asm("readlink"); -void readlink(void) __asm("__protected_readlink"); -void readlink(void) { __real_readlink(); } +void __real_sync(void) __asm("sync"); +void sync(void) __asm("__protected_sync"); +void sync(void) { __real_sync(); } >From 9e9d099df8bcba8e906fa76730cc4b75cf2f7b74 Mon Sep 17 00:00:00 2001 From: Colin Kinloch <[email protected]> Date: Thu, 6 Nov 2025 00:00:53 +0000 Subject: [PATCH 4/8] [clang][Sema] Use relative line number for template warnings --- clang/test/Sema/warn-fortify-source.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/clang/test/Sema/warn-fortify-source.c b/clang/test/Sema/warn-fortify-source.c index 33623cd8d8420..4176697319611 100644 --- a/clang/test/Sema/warn-fortify-source.c +++ b/clang/test/Sema/warn-fortify-source.c @@ -339,17 +339,15 @@ template <int A, int B> void call_memcpy_dep() { char bufferA[A]; char bufferB[B]; + memcpy(bufferA, bufferB, 10); if (sizeof(bufferA) < 10 && sizeof(bufferB) < 10) { - memcpy(bufferA, bufferB, 10); // expected-warning{{'memcpy' will always overflow; destination buffer has size 9, but size argument is 10}} \ - // expected-warning{{'memcpy' will always over-read; source buffer has size 9, but size argument is 10}} + // expected-warning@-2{{'memcpy' will always overflow; destination buffer has size 9, but size argument is 10}} + // expected-warning@-3{{'memcpy' will always over-read; source buffer has size 9, but size argument is 10}} } else if (sizeof(bufferA) < 10) { - memcpy(bufferA, bufferB, 10); // expected-warning{{'memcpy' will always overflow; destination buffer has size 9, but size argument is 10}} + // expected-warning@-5{{'memcpy' will always overflow; destination buffer has size 9, but size argument is 10}} } else if (sizeof(bufferB) < 10) { - memcpy(bufferA, bufferB, 10); // expected-warning{{'memcpy' will always over-read; source buffer has size 9, but size argument is 10}} - } else { - memcpy(bufferA, bufferB, 10); + // expected-warning@-7{{'memcpy' will always over-read; source buffer has size 9, but size argument is 10}} } - } void call_call_memcpy() { >From bb62d566b86a3c17d921f7b39f55b93fe9a365df Mon Sep 17 00:00:00 2001 From: Colin Kinloch <[email protected]> Date: Thu, 6 Nov 2025 01:58:37 +0000 Subject: [PATCH 5/8] [clang][test] Add over-read warnings to analysis tests --- clang/test/Analysis/array-struct.c | 4 ++-- clang/test/Analysis/bstring.c | 21 +++++++++++++++++-- clang/test/Analysis/malloc.c | 2 +- clang/test/Analysis/pr22954.c | 3 ++- .../Analysis/std-c-library-functions-POSIX.c | 4 ++-- clang/test/Analysis/std-c-library-functions.c | 4 ++-- 6 files changed, 28 insertions(+), 10 deletions(-) diff --git a/clang/test/Analysis/array-struct.c b/clang/test/Analysis/array-struct.c index f0eba86fe71bf..691bb1348a44f 100644 --- a/clang/test/Analysis/array-struct.c +++ b/clang/test/Analysis/array-struct.c @@ -175,12 +175,12 @@ void f17(void) { x = 1; } -void read(char*); +void readp(char*); void f18(void) { char *q; char *p = (char *) __builtin_alloca(10); - read(p); + readp(p); q = p; q++; if (*q) { // no-warning diff --git a/clang/test/Analysis/bstring.c b/clang/test/Analysis/bstring.c index 810241accffa2..ce991bc56ceeb 100644 --- a/clang/test/Analysis/bstring.c +++ b/clang/test/Analysis/bstring.c @@ -93,6 +93,9 @@ void memcpy1 (void) { char dst[10]; memcpy(dst, src, 5); // expected-warning{{Memory copy function accesses out-of-bound array element}} +#ifndef VARIANT + // expected-warning@-2{{memcpy' will always over-read; source buffer has size 4, but size argument is 5}} +#endif } void memcpy2 (void) { @@ -117,6 +120,9 @@ void memcpy4 (void) { char dst[10]; memcpy(dst+2, src+2, 3); // expected-warning{{Memory copy function accesses out-of-bound array element}} +#ifndef VARIANT + // expected-warning@-2{{memcpy' will always over-read; source buffer has size 2, but size argument is 3}} +#endif } void memcpy5(void) { @@ -219,6 +225,9 @@ void mempcpy1 (void) { char dst[10]; mempcpy(dst, src, 5); // expected-warning{{Memory copy function accesses out-of-bound array element}} +#ifndef VARIANT + // expected-warning@-2{{mempcpy' will always over-read; source buffer has size 4, but size argument is 5}} +#endif } void mempcpy2 (void) { @@ -243,6 +252,9 @@ void mempcpy4 (void) { char dst[10]; mempcpy(dst+2, src+2, 3); // expected-warning{{Memory copy function accesses out-of-bound array element}} +#ifndef VARIANT + // expected-warning@-2{{mempcpy' will always over-read; source buffer has size 2, but size argument is 3}} +#endif } void mempcpy5(void) { @@ -384,6 +396,9 @@ void memmove1 (void) { char dst[10]; memmove(dst, src, 5); // expected-warning{{out-of-bound}} +#ifndef VARIANT + // expected-warning@-2{{memmove' will always over-read; source buffer has size 4, but size argument is 5}} +#endif } void memmove2 (void) { @@ -501,7 +516,8 @@ void bcopy1 (void) { char src[] = {1, 2, 3, 4}; char dst[10]; - bcopy(src, dst, 5); // expected-warning{{out-of-bound}} + bcopy(src, dst, 5); // expected-warning{{out-of-bound}} \ + // expected-warning{{bcopy' will always over-read; source buffer has size 4, but size argument is 5}} } void bcopy2 (void) { @@ -541,6 +557,7 @@ void nocrash_on_empty_struct_memcpy(void) { __builtin_memcpy(&a[2], a, 2); // no-crash #if !defined(_WIN32) || defined(__MINGW32__) // expected-warning@-2 {{'memcpy' will always overflow; destination buffer has size 0, but size argument is 2}} - // expected-warning@-3 {{Memory copy function overflows the destination buffer}} + // expected-warning@-3 {{'memcpy' will always over-read; source buffer has size 0, but size argument is 2}} + // expected-warning@-4 {{Memory copy function overflows the destination buffer}} #endif } diff --git a/clang/test/Analysis/malloc.c b/clang/test/Analysis/malloc.c index 849ab3a3a0f37..199deda3db877 100644 --- a/clang/test/Analysis/malloc.c +++ b/clang/test/Analysis/malloc.c @@ -890,7 +890,7 @@ void overlappingMemcpyDoesNotSinkPath(char *s) { // Treat source buffer contents as escaped. void escapeSourceContents(char *s) { char *p = malloc(12); - memcpy(s, &p, 12); // no warning + memcpy(s, &p, 12); // expected-warning{{memcpy' will always over-read; source buffer has size}} void *p1 = malloc(7); char *a; diff --git a/clang/test/Analysis/pr22954.c b/clang/test/Analysis/pr22954.c index 3d1cac1972066..24da6bb05d493 100644 --- a/clang/test/Analysis/pr22954.c +++ b/clang/test/Analysis/pr22954.c @@ -536,7 +536,8 @@ int f262(void) { struct aa a262 = {{1, 2, 3, 4}, 0}; a262.s2 = strdup("hello"); char input[] = {'a', 'b', 'c', 'd'}; - memcpy(a262.s1, input, -1); // expected-warning{{'memcpy' will always overflow; destination buffer has size 16, but size argument is 18446744073709551615}} + memcpy(a262.s1, input, -1); // expected-warning{{'memcpy' will always overflow; destination buffer has size 16, but size argument is 18446744073709551615}} \ + // expected-warning{{'memcpy' will always over-read; source buffer has size 4, but size argument is 18446744073709551615}} clang_analyzer_eval(a262.s1[0] == 1); // expected-warning{{UNKNOWN}}\ expected-warning{{Potential leak of memory pointed to by 'a262.s2'}} clang_analyzer_eval(a262.s1[1] == 1); // expected-warning{{UNKNOWN}} diff --git a/clang/test/Analysis/std-c-library-functions-POSIX.c b/clang/test/Analysis/std-c-library-functions-POSIX.c index f6d88e6c1502d..462bbf5d8e5de 100644 --- a/clang/test/Analysis/std-c-library-functions-POSIX.c +++ b/clang/test/Analysis/std-c-library-functions-POSIX.c @@ -98,8 +98,8 @@ // CHECK: Loaded summary for: void *mmap64(void *addr, size_t length, int prot, int flags, int fd, off64_t offset) // CHECK: Loaded summary for: int pipe(int fildes[2]) // CHECK: Loaded summary for: off_t lseek(int fildes, off_t offset, int whence) -// CHECK: Loaded summary for: ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize) -// CHECK: Loaded summary for: ssize_t readlinkat(int fd, const char *restrict path, char *restrict buf, size_t bufsize) +// CHECK: Loaded summary for: __signed_size_t readlink(const char *restrict path, char *restrict buf, size_t bufsize) +// CHECK: Loaded summary for: __signed_size_t readlinkat(int fd, const char *restrict path, char *restrict buf, size_t bufsize) // CHECK: Loaded summary for: int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) // CHECK: Loaded summary for: char *realpath(const char *restrict file_name, char *restrict resolved_name) // CHECK: Loaded summary for: int execv(const char *path, char *const argv[]) diff --git a/clang/test/Analysis/std-c-library-functions.c b/clang/test/Analysis/std-c-library-functions.c index b5f663493a676..45ca1c69f89e3 100644 --- a/clang/test/Analysis/std-c-library-functions.c +++ b/clang/test/Analysis/std-c-library-functions.c @@ -61,8 +61,8 @@ // CHECK-NEXT: Loaded summary for: int getchar(void) // CHECK-NEXT: Loaded summary for: __size_t fread(void *restrict, size_t, size_t, FILE *restrict) // CHECK-NEXT: Loaded summary for: __size_t fwrite(const void *restrict, size_t, size_t, FILE *restrict) -// CHECK-NEXT: Loaded summary for: ssize_t read(int, void *, size_t) -// CHECK-NEXT: Loaded summary for: ssize_t write(int, const void *, size_t) +// CHECK-NEXT: Loaded summary for: __signed_size_t read(int, void *, size_t) +// CHECK-NEXT: Loaded summary for: __signed_size_t write(int, const void *, size_t) // CHECK-NEXT: Loaded summary for: ssize_t getline(char **restrict, size_t *restrict, FILE *restrict) // CHECK-NEXT: Loaded summary for: ssize_t getdelim(char **restrict, size_t *restrict, int, FILE *restrict) // CHECK-NEXT: Loaded summary for: char *getenv(const char *) >From 79d1240983abe0e379bff52efb6643f8d820d2e2 Mon Sep 17 00:00:00 2001 From: Colin Kinloch <[email protected]> Date: Thu, 6 Nov 2025 01:58:37 +0000 Subject: [PATCH 6/8] [test] Adjust libcxx and asan tests for new fortify warnings Two tests outside clang/test/Analysis need adjustment after introducing fortify warnings for unistd.h I/O: * The Windows asan EH-codegen test now triggers new fortify warnings on non-MSVC builds; pass -Wno-fortify-source there (MSVC mode is unaffected). * The libcxx __constexpr_wmemchr test passed an int character constant to a wchar_t parameter; use L'n' so the literal type matches the parameter. --- compiler-rt/test/asan/TestCases/Windows/issue64990.cpp | 2 +- .../libcxx/strings/c.strings/constexpr.cwchar.compile.pass.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler-rt/test/asan/TestCases/Windows/issue64990.cpp b/compiler-rt/test/asan/TestCases/Windows/issue64990.cpp index 5222ec6e08191..785e027a31be8 100644 --- a/compiler-rt/test/asan/TestCases/Windows/issue64990.cpp +++ b/compiler-rt/test/asan/TestCases/Windows/issue64990.cpp @@ -1,5 +1,5 @@ // Repro for the issue #64990: Asan with Windows EH generates __asan_xxx runtime calls without required funclet tokens -// RUN: %clang_cl_asan %Od %if MSVC %{ /Oi %} %s -EHsc %Fe%t +// RUN: %clang_cl_asan %Od %if MSVC %{ /Oi %} %else %{ -Wno-fortify-source %} %s -EHsc %Fe%t // RUN: not %run %t 2>&1 | FileCheck %s // UNSUPPORTED: target={{.*-windows-gnu}} diff --git a/libcxx/test/libcxx/strings/c.strings/constexpr.cwchar.compile.pass.cpp b/libcxx/test/libcxx/strings/c.strings/constexpr.cwchar.compile.pass.cpp index 02feed064eacc..7d9548e500da8 100644 --- a/libcxx/test/libcxx/strings/c.strings/constexpr.cwchar.compile.pass.cpp +++ b/libcxx/test/libcxx/strings/c.strings/constexpr.cwchar.compile.pass.cpp @@ -21,6 +21,6 @@ static_assert(std::__constexpr_wmemcmp(L"Banane", L"Bananf", 6) == -1, ""); constexpr bool test_constexpr_wmemchr() { const wchar_t str[] = L"Banane"; - return std::__constexpr_wmemchr(str, 'n', 6) == str + 2; + return std::__constexpr_wmemchr(str, L'n', 6) == str + 2; } static_assert(test_constexpr_wmemchr(), ""); >From 336fc696cd6302e1e90e64f7cbf9886dc157b440 Mon Sep 17 00:00:00 2001 From: Denys Fedoryshchenko <[email protected]> Date: Tue, 5 May 2026 15:11:53 +0300 Subject: [PATCH 7/8] [clang][analyzer] Check SSIZE_MAX bounds for unistd I/O sizes Add StdLibraryFunctionsChecker argument constraints that reject size arguments greater than SSIZE_MAX for read, write, readlink, and readlinkat. This catches common problematic cases such as passing -1 to a size_t parameter, using very large constants, or relying on a size that is valid on one platform but exceeds SSIZE_MAX on another. This may produce new warnings for existing code that passes size arguments larger than SSIZE_MAX to these functions. Use the visible ssize_t type to derive the platform-specific maximum. For readlink and readlinkat, tighten the existing bufsize constraint from the full size_t range to SSIZE_MAX. --- .../Checkers/StdLibraryFunctionsChecker.cpp | 17 ++++++----- .../std-c-library-functions-arg-constraints.c | 30 +++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp index 8a3ee4443eb16..666caf31e4610 100644 --- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp @@ -2073,12 +2073,17 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( std::optional<QualType> Ssize_tTy = lookupTy("ssize_t"); std::optional<RangeInt> Ssize_tMax = getMaxValue(Ssize_tTy); + auto ValidSsize_tSize = [&](ArgNo ArgN) { + return ArgumentCondition(ArgN, WithinRange, Range(0, Ssize_tMax), + "a value not greater than SSIZE_MAX"); + }; auto ReadSummary = Summary(NoEvalCall) .Case({ReturnValueCondition(LessThanOrEq, ArgNo(2)), ReturnValueCondition(WithinRange, Range(-1, Ssize_tMax))}, - ErrnoIrrelevant); + ErrnoIrrelevant) + .ArgConstraint(ValidSsize_tSize(ArgNo(2))); // FIXME these are actually defined by POSIX and not by the C standard, we // should handle them together with the rest of the POSIX functions. @@ -3012,7 +3017,7 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( ArgTypes{ConstCharPtrRestrictTy, CharPtrRestrictTy, SizeTyCanonTy}, RetType{Ssize_tTy}), Summary(NoEvalCall) - .Case({ArgumentCondition(2, WithinRange, Range(1, IntMax)), + .Case({ArgumentCondition(2, WithinRange, Range(1, Ssize_tMax)), ReturnValueCondition(LessThanOrEq, ArgNo(2)), ReturnValueCondition(WithinRange, Range(1, Ssize_tMax))}, ErrnoMustNotBeChecked, GenericSuccessMsg) @@ -3025,8 +3030,7 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( .ArgConstraint(NotNull(ArgNo(1))) .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(1), /*BufSize=*/ArgNo(2))) - .ArgConstraint( - ArgumentCondition(2, WithinRange, Range(0, SizeMax)))); + .ArgConstraint(ValidSsize_tSize(ArgNo(2)))); // ssize_t readlinkat(int fd, const char *restrict path, // char *restrict buf, size_t bufsize); @@ -3036,7 +3040,7 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( SizeTyCanonTy}, RetType{Ssize_tTy}), Summary(NoEvalCall) - .Case({ArgumentCondition(3, WithinRange, Range(1, IntMax)), + .Case({ArgumentCondition(3, WithinRange, Range(1, Ssize_tMax)), ReturnValueCondition(LessThanOrEq, ArgNo(3)), ReturnValueCondition(WithinRange, Range(1, Ssize_tMax))}, ErrnoMustNotBeChecked, GenericSuccessMsg) @@ -3050,8 +3054,7 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( .ArgConstraint(NotNull(ArgNo(2))) .ArgConstraint(BufferSize(/*Buffer=*/ArgNo(2), /*BufSize=*/ArgNo(3))) - .ArgConstraint( - ArgumentCondition(3, WithinRange, Range(0, SizeMax)))); + .ArgConstraint(ValidSsize_tSize(ArgNo(3)))); // int renameat(int olddirfd, const char *oldpath, int newdirfd, const char // *newpath); diff --git a/clang/test/Analysis/std-c-library-functions-arg-constraints.c b/clang/test/Analysis/std-c-library-functions-arg-constraints.c index 0b817dda98c72..64e3fd6842ddd 100644 --- a/clang/test/Analysis/std-c-library-functions-arg-constraints.c +++ b/clang/test/Analysis/std-c-library-functions-arg-constraints.c @@ -370,3 +370,33 @@ void test_file_fd_at_functions() { (void)readlinkat(AT_FDCWD, "newpath", Buf, 10); (void)renameat(AT_FDCWD, "oldpath", AT_FDCWD, "newpath"); } + +#define SSIZE_MAX_PLUS_ONE ((size_t)1 << (sizeof(size_t) * __CHAR_BIT__ - 1)) + +void test_read_ssize_max_io_size(int fd, char *Buf) { + read(fd, Buf, SSIZE_MAX_PLUS_ONE); // \ + // report-warning{{The 3rd argument to 'read' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} \ + // bugpath-warning{{The 3rd argument to 'read' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} \ + // bugpath-note{{The 3rd argument to 'read' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} +} + +void test_write_ssize_max_io_size(int fd, char *Buf) { + write(fd, Buf, SSIZE_MAX_PLUS_ONE); // \ + // report-warning{{The 3rd argument to 'write' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} \ + // bugpath-warning{{The 3rd argument to 'write' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} \ + // bugpath-note{{The 3rd argument to 'write' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} +} + +void test_readlink_ssize_max_io_size(char *Buf) { + readlink("path", Buf, SSIZE_MAX_PLUS_ONE); // \ + // report-warning{{The 3rd argument to 'readlink' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} \ + // bugpath-warning{{The 3rd argument to 'readlink' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} \ + // bugpath-note{{The 3rd argument to 'readlink' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} +} + +void test_readlinkat_ssize_max_io_size(char *Buf) { + readlinkat(AT_FDCWD, "path", Buf, SSIZE_MAX_PLUS_ONE); // \ + // report-warning{{The 4th argument to 'readlinkat' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} \ + // bugpath-warning{{The 4th argument to 'readlinkat' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} \ + // bugpath-note{{The 4th argument to 'readlinkat' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} +} >From d82b64fa629d70d0adc8717c47501c662a92df97 Mon Sep 17 00:00:00 2001 From: Denys Fedoryshchenko <[email protected]> Date: Tue, 5 May 2026 15:14:48 +0300 Subject: [PATCH 8/8] [clang][analyzer] Add summaries for pread and pwrite Add StdLibraryFunctionsChecker summaries for pread, pread64, pwrite, and pwrite64. Reuse the existing read/write summary so the new modeled functions inherit the SSIZE_MAX size constraint and return-value bounds. Add POSIX test declarations, loaded-summary checks, and diagnostic coverage for the new summaries. Signed-off-by: Denys Fedoryshchenko <[email protected]> --- .../Checkers/StdLibraryFunctionsChecker.cpp | 29 +++++++++++++++++++ .../Inputs/std-c-library-functions-POSIX.h | 4 +++ .../Analysis/std-c-library-functions-POSIX.c | 4 +++ .../std-c-library-functions-arg-constraints.c | 28 ++++++++++++++++++ 4 files changed, 65 insertions(+) diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp index 666caf31e4610..b579255892084 100644 --- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp @@ -3009,6 +3009,35 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( .ArgConstraint( ArgumentCondition(0, WithinRange, Range(0, IntMax)))); + // ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset); + addToFunctionSummaryMap( + "pread", + Signature(ArgTypes{IntTy, VoidPtrTy, SizeTyCanonTy, Off_tTy}, + RetType{Ssize_tTy}), + ReadSummary); + + // ssize_t pread64(int fildes, void *buf, size_t nbyte, off64_t offset); + addToFunctionSummaryMap( + "pread64", + Signature(ArgTypes{IntTy, VoidPtrTy, SizeTyCanonTy, Off64_tTy}, + RetType{Ssize_tTy}), + ReadSummary); + + // ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset); + addToFunctionSummaryMap( + "pwrite", + Signature(ArgTypes{IntTy, ConstVoidPtrTy, SizeTyCanonTy, Off_tTy}, + RetType{Ssize_tTy}), + ReadSummary); + + // ssize_t pwrite64(int fildes, const void *buf, size_t nbyte, + // off64_t offset); + addToFunctionSummaryMap( + "pwrite64", + Signature(ArgTypes{IntTy, ConstVoidPtrTy, SizeTyCanonTy, Off64_tTy}, + RetType{Ssize_tTy}), + ReadSummary); + // ssize_t readlink(const char *restrict path, char *restrict buf, // size_t bufsize); addToFunctionSummaryMap( diff --git a/clang/test/Analysis/Inputs/std-c-library-functions-POSIX.h b/clang/test/Analysis/Inputs/std-c-library-functions-POSIX.h index b146068eedb08..83d753a22b347 100644 --- a/clang/test/Analysis/Inputs/std-c-library-functions-POSIX.h +++ b/clang/test/Analysis/Inputs/std-c-library-functions-POSIX.h @@ -124,6 +124,10 @@ void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) void *mmap64(void *addr, size_t length, int prot, int flags, int fd, off64_t offset); int pipe(int fildes[2]); off_t lseek(int fildes, off_t offset, int whence); +ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset); +ssize_t pread64(int fildes, void *buf, size_t nbyte, off64_t offset); +ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset); +ssize_t pwrite64(int fildes, const void *buf, size_t nbyte, off64_t offset); ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize); ssize_t readlinkat(int fd, const char *restrict path, char *restrict buf, size_t bufsize); int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath); diff --git a/clang/test/Analysis/std-c-library-functions-POSIX.c b/clang/test/Analysis/std-c-library-functions-POSIX.c index 462bbf5d8e5de..7e9f703b1a7ea 100644 --- a/clang/test/Analysis/std-c-library-functions-POSIX.c +++ b/clang/test/Analysis/std-c-library-functions-POSIX.c @@ -98,6 +98,10 @@ // CHECK: Loaded summary for: void *mmap64(void *addr, size_t length, int prot, int flags, int fd, off64_t offset) // CHECK: Loaded summary for: int pipe(int fildes[2]) // CHECK: Loaded summary for: off_t lseek(int fildes, off_t offset, int whence) +// CHECK: Loaded summary for: ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset) +// CHECK: Loaded summary for: ssize_t pread64(int fildes, void *buf, size_t nbyte, off64_t offset) +// CHECK: Loaded summary for: ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset) +// CHECK: Loaded summary for: ssize_t pwrite64(int fildes, const void *buf, size_t nbyte, off64_t offset) // CHECK: Loaded summary for: __signed_size_t readlink(const char *restrict path, char *restrict buf, size_t bufsize) // CHECK: Loaded summary for: __signed_size_t readlinkat(int fd, const char *restrict path, char *restrict buf, size_t bufsize) // CHECK: Loaded summary for: int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) diff --git a/clang/test/Analysis/std-c-library-functions-arg-constraints.c b/clang/test/Analysis/std-c-library-functions-arg-constraints.c index 64e3fd6842ddd..c68e131e17167 100644 --- a/clang/test/Analysis/std-c-library-functions-arg-constraints.c +++ b/clang/test/Analysis/std-c-library-functions-arg-constraints.c @@ -387,6 +387,34 @@ void test_write_ssize_max_io_size(int fd, char *Buf) { // bugpath-note{{The 3rd argument to 'write' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} } +void test_pread_ssize_max_io_size(int fd, char *Buf) { + pread(fd, Buf, SSIZE_MAX_PLUS_ONE, 0); // \ + // report-warning{{The 3rd argument to 'pread' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} \ + // bugpath-warning{{The 3rd argument to 'pread' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} \ + // bugpath-note{{The 3rd argument to 'pread' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} +} + +void test_pread64_ssize_max_io_size(int fd, char *Buf) { + pread64(fd, Buf, SSIZE_MAX_PLUS_ONE, 0); // \ + // report-warning{{The 3rd argument to 'pread64' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} \ + // bugpath-warning{{The 3rd argument to 'pread64' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} \ + // bugpath-note{{The 3rd argument to 'pread64' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} +} + +void test_pwrite_ssize_max_io_size(int fd, char *Buf) { + pwrite(fd, Buf, SSIZE_MAX_PLUS_ONE, 0); // \ + // report-warning{{The 3rd argument to 'pwrite' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} \ + // bugpath-warning{{The 3rd argument to 'pwrite' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} \ + // bugpath-note{{The 3rd argument to 'pwrite' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} +} + +void test_pwrite64_ssize_max_io_size(int fd, char *Buf) { + pwrite64(fd, Buf, SSIZE_MAX_PLUS_ONE, 0); // \ + // report-warning{{The 3rd argument to 'pwrite64' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} \ + // bugpath-warning{{The 3rd argument to 'pwrite64' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} \ + // bugpath-note{{The 3rd argument to 'pwrite64' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} +} + void test_readlink_ssize_max_io_size(char *Buf) { readlink("path", Buf, SSIZE_MAX_PLUS_ONE); // \ // report-warning{{The 3rd argument to 'readlink' is 9223372036854775808 but should be a value not greater than SSIZE_MAX}} \ _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
