https://github.com/LoboQ1ng updated https://github.com/llvm/llvm-project/pull/152462
>From 909f0bce1aec9939eeecdaa8c3f0a028f89d96f4 Mon Sep 17 00:00:00 2001 From: LoboQ1ng <xp...@qq.com> Date: Thu, 7 Aug 2025 16:52:39 +0800 Subject: [PATCH 1/2] [StaticAnalyzer] [MallocChecker] Detect use-after-free for field address (e.g., &ptr->field) --- clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp index 369d6194dbb65..ad1d20779f384 100644 --- a/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp @@ -3156,8 +3156,14 @@ void MallocChecker::checkPreCall(const CallEvent &Call, for (unsigned I = 0, E = Call.getNumArgs(); I != E; ++I) { SVal ArgSVal = Call.getArgSVal(I); if (isa<Loc>(ArgSVal)) { - SymbolRef Sym = ArgSVal.getAsSymbol(); - if (!Sym) + const MemRegion *MR = ArgSVal.getAsRegion(); + if (!MR) + continue; + const MemRegion *BaseRegion = MR->getBaseRegion(); + SymbolRef Sym = nullptr; + if (const auto *SR = dyn_cast<SymbolicRegion>(BaseRegion)) + Sym = SR->getSymbol(); + if (!Sym) continue; if (checkUseAfterFree(Sym, C, Call.getArgExpr(I))) return; >From a19a454b4940b0bc12c765a358eb09088f9f9e46 Mon Sep 17 00:00:00 2001 From: LoboQ1ng <xp...@qq.com> Date: Thu, 7 Aug 2025 19:19:15 +0800 Subject: [PATCH 2/2] add test case --- clang/test/Analysis/malloc-checker-arg-uaf.c | 44 ++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 clang/test/Analysis/malloc-checker-arg-uaf.c diff --git a/clang/test/Analysis/malloc-checker-arg-uaf.c b/clang/test/Analysis/malloc-checker-arg-uaf.c new file mode 100644 index 0000000000000..54cfe6633910c --- /dev/null +++ b/clang/test/Analysis/malloc-checker-arg-uaf.c @@ -0,0 +1,44 @@ +// RUN: %clang_analyze_cc1 -analyzer-checker=unix.Malloc -verify %s + +#include "Inputs/system-header-simulator-for-malloc.h" + +struct Obj { + int field; +}; + +void use(void *ptr); + +void test_direct_param_uaf() { + int *p = (int *)malloc(sizeof(int)); + free(p); + use(p); // expected-warning{{Use of memory after it is freed}} +} + +void test_struct_field_uaf() { + struct Obj *o = (struct Obj *)malloc(sizeof(struct Obj)); + free(o); + use(&o->field); // expected-warning{{Use of memory after it is freed}} +} + +void test_no_warning_const_int() { + use((void *)0x1234); // no-warning +} + +void test_no_warning_stack() { + int x = 42; + use(&x); // no-warning +} + +void test_nested_alloc() { + struct Obj *o = (struct Obj *)malloc(sizeof(struct Obj)); + use(o); // no-warning + free(o); + use(o); // expected-warning{{Use of memory after it is freed}} +} + +void test_nested_field() { + struct Obj *o = malloc(sizeof(struct Obj)); + int *f = &o->field; + free(o); + use(f); // expected-warning{{Use of memory after it is freed}} +} \ No newline at end of file _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits