Skip to content

Commit b69749a

Browse files
committed
[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.
1 parent 8b4b7d2 commit b69749a

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

clang/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ static std::optional<std::string> printReferrer(const MemRegion *Referrer) {
337337
// warn_bind_ref_member_to_parameter or
338338
// warn_init_ptr_member_to_parameter_addr
339339
return std::nullopt;
340+
} else if (isa<AllocaRegion>(Referrer)) {
341+
// Skip alloca() regions, they indicate advanced memory management
342+
// and higher likelihood of CSA false positives.
343+
return std::nullopt;
340344
} else {
341345
assert(false && "Unexpected referrer region type.");
342346
return std::nullopt;

clang/test/Analysis/stack-addr-ps.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify %s -Wno-undefined-bool-conversion
1+
// RUN: %clang_analyze_cc1 \
2+
// RUN: -analyzer-checker=core,debug.ExprInspection,unix.Malloc \
3+
// RUN: -verify %s \
4+
// RUN: -Wno-undefined-bool-conversion
5+
// unix.Malloc is necessary to model __builtin_alloca,
6+
// which could trigger an "unexpected region" bug in StackAddrEscapeChecker.
27

38
typedef __INTPTR_TYPE__ intptr_t;
49

@@ -846,3 +851,25 @@ void top(char **p) {
846851
foo(); // no-warning FIXME: p binding is reclaimed before the function end
847852
}
848853
} // namespace early_reclaim_dead_limitation
854+
855+
using size_t = decltype(sizeof(int));
856+
void * malloc(size_t size);
857+
void free(void*);
858+
859+
namespace alloca_region_pointer {
860+
void callee(char **pptr) {
861+
char local;
862+
*pptr = &local;
863+
}
864+
865+
void top_alloca_no_crash() {
866+
char **pptr = (char**)__builtin_alloca(sizeof(char*));
867+
callee(pptr);
868+
}
869+
870+
void top_malloc_no_crash_fn() {
871+
char **pptr = (char**)malloc(sizeof(char*));
872+
callee(pptr);
873+
free(pptr);
874+
}
875+
} // namespace alloca_region_pointer

0 commit comments

Comments
 (0)