[clang] [analyzer][StackAddrEscapeChecker] Fix assert failure for alloca regions (PR #109655)
Arseniy Zaostrovnykh via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 23 05:28:01 PDT 2024
https://github.com/necto created https://github.com/llvm/llvm-project/pull/109655
Fixes #107852
Make it explicit that the checker skips alloca regions to avoid the risc of producing false positives for code that has advnaced memory management.
StackAddrEscapeChecker already used this strategy when it comes to malloc'ed regions, so this change relaxes the assertion and explicitly silents the issues related to memory regions generated with alloca.
>From b69749ab1854b0c12a69821c35bff1b866d6f307 Mon Sep 17 00:00:00 2001
From: Arseniy Zaostrovnykh <necto.ne at gmail.com>
Date: Mon, 23 Sep 2024 14:21:31 +0200
Subject: [PATCH] [analyzer][StackAddrEscapeChecker] Fix assert failure for
alloca regions
Fixes #107852
Make it explicit that the checker skips alloca regions to avoid the risc
of producing false positives for code that has advnaced memory
management.
StackAddrEscapeChecker already used this strategy when it comes to
malloc'ed regions, so this change relaxes the assertion and explicitly
silents the issues related to memory regions generated with alloca.
---
.../Checkers/StackAddrEscapeChecker.cpp | 4 +++
clang/test/Analysis/stack-addr-ps.cpp | 29 ++++++++++++++++++-
2 files changed, 32 insertions(+), 1 deletion(-)
diff --git a/clang/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp
index d8c52941b19366..a76639bb86b208 100644
--- a/clang/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp
@@ -337,6 +337,10 @@ static std::optional<std::string> printReferrer(const MemRegion *Referrer) {
// warn_bind_ref_member_to_parameter or
// warn_init_ptr_member_to_parameter_addr
return std::nullopt;
+ } else if (isa<AllocaRegion>(Referrer)) {
+ // Skip alloca() regions, they indicate advanced memory management
+ // and higher likelihood of CSA false positives.
+ return std::nullopt;
} else {
assert(false && "Unexpected referrer region type.");
return std::nullopt;
diff --git a/clang/test/Analysis/stack-addr-ps.cpp b/clang/test/Analysis/stack-addr-ps.cpp
index 35f38fbbfbefdc..1f8b62824772e1 100644
--- a/clang/test/Analysis/stack-addr-ps.cpp
+++ b/clang/test/Analysis/stack-addr-ps.cpp
@@ -1,4 +1,9 @@
-// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify %s -Wno-undefined-bool-conversion
+// RUN: %clang_analyze_cc1 \
+// RUN: -analyzer-checker=core,debug.ExprInspection,unix.Malloc \
+// RUN: -verify %s \
+// RUN: -Wno-undefined-bool-conversion
+// unix.Malloc is necessary to model __builtin_alloca,
+// which could trigger an "unexpected region" bug in StackAddrEscapeChecker.
typedef __INTPTR_TYPE__ intptr_t;
@@ -846,3 +851,25 @@ void top(char **p) {
foo(); // no-warning FIXME: p binding is reclaimed before the function end
}
} // namespace early_reclaim_dead_limitation
+
+using size_t = decltype(sizeof(int));
+void * malloc(size_t size);
+void free(void*);
+
+namespace alloca_region_pointer {
+void callee(char **pptr) {
+ char local;
+ *pptr = &local;
+}
+
+void top_alloca_no_crash() {
+ char **pptr = (char**)__builtin_alloca(sizeof(char*));
+ callee(pptr);
+}
+
+void top_malloc_no_crash_fn() {
+ char **pptr = (char**)malloc(sizeof(char*));
+ callee(pptr);
+ free(pptr);
+}
+} // namespace alloca_region_pointer
More information about the cfe-commits
mailing list