@@ -40,8 +40,8 @@ class AllocationChecker
4040 : public Checker<check::PostStmt<CastExpr>, check::PreCall, check::PostCall,
4141 check::Bind, check::EndFunction> {
4242 BugType BT_Default{this , " Allocation partitioning" , " CHERI portability" };
43- BugType BT_KnownReg {this , " Heap or static allocation partitioning" ,
44- " CHERI portability" };
43+ BugType BT_UnknownReg {this , " Unknown allocation partitioning" ,
44+ " CHERI portability" };
4545
4646 const CallDescriptionSet IgnoreFnSet = {
4747 {CDM::SimpleFunc, {" free" }, 1 },
@@ -81,6 +81,8 @@ class AllocationChecker
8181 void checkBind (SVal L, SVal V, const Stmt *S, CheckerContext &C) const ;
8282 void checkEndFunction (const ReturnStmt *RS, CheckerContext &Ctx) const ;
8383
84+ bool ReportForUnknownAllocations;
85+
8486private:
8587 ExplodedNode *emitAllocationPartitionWarning (CheckerContext &C,
8688 const MemRegion *MR,
@@ -111,7 +113,13 @@ std::pair<const MemRegion *, bool> getAllocationStart(const ASTContext &ASTCtx,
111113 return std::make_pair (R, ZeroShift);
112114}
113115
114- bool isAllocation (const MemRegion *R) {
116+ bool isAllocation (const MemRegion *R, const AllocationChecker *Chk, ProgramStateRef State) {
117+ if (!Chk->ReportForUnknownAllocations ) {
118+ const MemSpaceRegion *MemSpace = R->getMemorySpace (State);
119+ if (!isa<HeapSpaceRegion, GlobalsSpaceRegion, StackSpaceRegion>(MemSpace))
120+ return false ;
121+ }
122+
115123 if (R->getAs <SymbolicRegion>())
116124 return true ;
117125 if (const TypedValueRegion *TR = R->getAs <TypedValueRegion>()) {
@@ -176,12 +184,20 @@ ExplodedNode *AllocationChecker::emitAllocationPartitionWarning(
176184 CheckerContext &C, const MemRegion *MR, SourceRange SR,
177185 StringRef Msg = " " ) const {
178186 if (ExplodedNode *ErrNode = C.generateNonFatalErrorNode ()) {
179- auto R = std::make_unique<PathSensitiveBugReport>(BT_Default, Msg, ErrNode);
180- R->addRange (SR);
181- R->markInteresting (MR);
182187
183188 const MemRegion *PrevAlloc =
184189 getAllocationStart (C.getASTContext (), MR, C.getState ()).first ;
190+ const MemSpaceRegion *MS = PrevAlloc
191+ ? PrevAlloc->getMemorySpace (C.getState ())
192+ : MR->getMemorySpace (C.getState ());
193+ const BugType &BT =
194+ isa<HeapSpaceRegion, GlobalsSpaceRegion, StackSpaceRegion>(MS)
195+ ? BT_Default
196+ : BT_UnknownReg;
197+ auto R = std::make_unique<PathSensitiveBugReport>(BT, Msg, ErrNode);
198+ R->addRange (SR);
199+ R->markInteresting (MR);
200+
185201 R->addVisitor (std::make_unique<AllocPartitionBugVisitor>(
186202 PrevAlloc == MR ? nullptr : PrevAlloc, MR));
187203
@@ -216,14 +232,10 @@ void AllocationChecker::checkPostStmt(const CastExpr *CE,
216232 getAllocationStart (ASTCtx, MR, State);
217233
218234 const MemRegion *SR = StartPair.first ;
219- if (!isAllocation (SR))
235+ if (!isAllocation (SR, this , State ))
220236 return ;
221237 bool ZeroShift = StartPair.second ;
222238
223- const MemSpaceRegion *MemSpace = SR->getMemorySpace (State);
224- if (!isa<HeapSpaceRegion, GlobalsSpaceRegion, StackSpaceRegion>(MemSpace))
225- return ;
226-
227239 SVal DstVal = C.getSVal (CE);
228240 const MemRegion *DMR = DstVal.getAsRegion ();
229241 if (MR->getAs <ElementRegion>() && (!DMR || !DMR->getAs <ElementRegion>())) {
@@ -252,10 +264,14 @@ void AllocationChecker::checkPostStmt(const CastExpr *CE,
252264 ->getUnqualifiedDesugaredType ();
253265 const Type *Ty2 = DstTy->getPointeeType ()->getUnqualifiedDesugaredType ();
254266 if (!relatedTypes (ASTCtx, Ty1, Ty2)) {
255- State = State->add <SuballocationSet>(SR);
256- if (DMR)
267+ if (!State->contains <SuballocationSet>(SR)) {
268+ State = State->add <SuballocationSet>(SR);
269+ Updated = true ;
270+ }
271+ if (DMR && !State->contains <SuballocationSet>(DMR)) {
257272 State = State->add <SuballocationSet>(DMR);
258- Updated = true ;
273+ Updated = true ;
274+ }
259275 } // else OK
260276 } // else ??? (ignore for now)
261277 } else {
@@ -419,7 +435,10 @@ PathDiagnosticPieceRef AllocationChecker::AllocPartitionBugVisitor::VisitNode(
419435// ===----------------------------------------------------------------------===//
420436
421437void ento::registerAllocationChecker (CheckerManager &Mgr) {
422- Mgr.registerChecker <AllocationChecker>();
438+ auto *Checker = Mgr.registerChecker <AllocationChecker>();
439+ Checker->ReportForUnknownAllocations =
440+ Mgr.getAnalyzerOptions ().getCheckerBooleanOption (
441+ Checker, " ReportForUnknownAllocations" );
423442}
424443
425444bool ento::shouldRegisterAllocationChecker (const CheckerManager &Mgr) {
0 commit comments