[analyzer] Fix wrong builtin_*_overflow return type#111253
Merged
[analyzer] Fix wrong builtin_*_overflow return type#111253
builtin_*_overflow return type#111253Conversation
Member
|
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clang-static-analyzer-1 Author: Pavel Skripkin (pskrgag) Changes
Fix it by directly passing Closes: #111147 [1] https://clang.llvm.org/docs/LanguageExtensions.html#checked-arithmetic-builtins Full diff: https://github.com/llvm/llvm-project/pull/111253.diff 2 Files Affected:
diff --git a/clang/lib/StaticAnalyzer/Checkers/BuiltinFunctionChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/BuiltinFunctionChecker.cpp
index 69d8e968283b37..d49f01898e2241 100644
--- a/clang/lib/StaticAnalyzer/Checkers/BuiltinFunctionChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/BuiltinFunctionChecker.cpp
@@ -183,6 +183,7 @@ void BuiltinFunctionChecker::handleOverflowBuiltin(const CallEvent &Call,
ProgramStateRef State = C.getState();
SValBuilder &SVB = C.getSValBuilder();
const Expr *CE = Call.getOriginExpr();
+ auto BoolTy = C.getASTContext().BoolTy;
SVal Arg1 = Call.getArgSVal(0);
SVal Arg2 = Call.getArgSVal(1);
@@ -194,7 +195,7 @@ void BuiltinFunctionChecker::handleOverflowBuiltin(const CallEvent &Call,
auto [Overflow, NotOverflow] = checkOverflow(C, RetValMax, ResultType);
if (NotOverflow) {
ProgramStateRef StateNoOverflow =
- State->BindExpr(CE, C.getLocationContext(), SVB.makeTruthVal(false));
+ State->BindExpr(CE, C.getLocationContext(), SVB.makeTruthVal(false, BoolTy));
if (auto L = Call.getArgSVal(2).getAs<Loc>()) {
StateNoOverflow =
@@ -213,7 +214,7 @@ void BuiltinFunctionChecker::handleOverflowBuiltin(const CallEvent &Call,
if (Overflow) {
C.addTransition(
- State->BindExpr(CE, C.getLocationContext(), SVB.makeTruthVal(true)),
+ State->BindExpr(CE, C.getLocationContext(), SVB.makeTruthVal(true, BoolTy)),
createBuiltinOverflowNoteTag(C));
}
}
diff --git a/clang/test/Analysis/builtin_overflow.c b/clang/test/Analysis/builtin_overflow.c
index 5c61795661d095..9d98ce7a1af45c 100644
--- a/clang/test/Analysis/builtin_overflow.c
+++ b/clang/test/Analysis/builtin_overflow.c
@@ -1,5 +1,5 @@
// RUN: %clang_analyze_cc1 -triple x86_64-unknown-unknown -verify %s \
-// RUN: -analyzer-checker=core,debug.ExprInspection
+// RUN: -analyzer-checker=core,debug.ExprInspection,alpha.core.BoolAssignment
#define __UINT_MAX__ (__INT_MAX__ * 2U + 1U)
#define __INT_MIN__ (-__INT_MAX__ - 1)
@@ -155,3 +155,12 @@ void test_uadd_overflow_contraints(unsigned a, unsigned b)
return;
}
}
+
+void test_bool_assign(void)
+{
+ int res;
+
+ // Reproduce issue from GH#111147. __builtin_*_overflow funcions
+ // should return _Bool, but not int.
+ _Bool ret = __builtin_mul_overflow(10, 20, &res); // no crash
+}
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
Contributor
Author
|
CI looks unrelated Should I re-trigger it just in case? |
Kyvangka1610
pushed a commit
to Kyvangka1610/llvm-project
that referenced
this pull request
Oct 5, 2024
`builtin_*_overflow` functions return `_Bool` according to [1]. `BuiltinFunctionChecker` was using `makeTruthVal` w/o specifying explicit type, which creates an `int` value, since it's the type of any compassion according to C standard. Fix it by directly passing `BoolTy` to `makeTruthVal` Closes: llvm#111147 [1] https://clang.llvm.org/docs/LanguageExtensions.html#checked-arithmetic-builtins
Kyvangka1610
added a commit
to Kyvangka1610/llvm-project
that referenced
this pull request
Oct 5, 2024
* commit 'FETCH_HEAD': [clang][bytecode] Handle UETT_OpenMPRequiredSimdAlign (llvm#111259) [mlir][polynomial] Add and verify constraints of coefficientModulus for ringAttr (llvm#111016) [clang][bytecode] Save a per-Block IsWeak bit (llvm#111248) [analyzer] Fix wrong `builtin_*_overflow` return type (llvm#111253) [SelectOpt] Don't convert constant selects to branches. (llvm#110858) [InstCombine] Update and-or-icmps.ll after 574266c. NFC [Instcombine] Test for more gep canonicalization [NFC][TableGen] Change `CodeGenIntrinsics` to use const references (llvm#111219) Add warning message to `session save` when transcript isn't saved. (llvm#109020) [RISCV][TTI] Recognize CONCAT_VECTORS if a shufflevector mask is multiple insert subvector. (llvm#110457) Revert "[InstCombine] Folding `(icmp eq/ne (and X, -P2), INT_MIN)`" (llvm#111236) [NFC][lsan] Add SuspendAllThreads traces [lsan] Add `thread_suspend_fail` flag Signed-off-by: kyvangka1610 <kyvangka2002@gmail.com>
Contributor
|
@pskrgag Thanks for fixing this issue quickly! 😄 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
builtin_*_overflowfunctions return_Boolaccording to [1].BuiltinFunctionCheckerwas usingmakeTruthValw/o specifying explicit type, which creates anintvalue, since it's the type of any compassion according to C standard.Fix it by directly passing
BoolTytomakeTruthValCloses: #111147
[1] https://clang.llvm.org/docs/LanguageExtensions.html#checked-arithmetic-builtins