-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAllocFreeChecker.cpp
699 lines (617 loc) · 26 KB
/
AllocFreeChecker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
// Copyright (c) 2018 Peter Wu <peter@lekensteyn.nl>
// SPDX-License-Identifier: MIT
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
using namespace clang;
using namespace ento;
namespace {
// record symbols and the original allocation source.
typedef std::pair<SymbolRef, const ExplodedNode *> LeakInfo;
typedef SmallVector<LeakInfo, 2> LeakInfoVector;
// Groups allocation and deallocation functions.
enum AllocationFamily : unsigned {
AF_None,
AF_Glib,
AF_GlibStringVector,
AF_GlibArray,
AF_GlibPtrArray,
AF_GlibByteArray,
AF_Wmem,
AF_WmemStringVector
};
enum WmemAllocator : unsigned {
WA_Invalid,
WA_Null,
WA_EpanScope,
WA_FileScope,
WA_PacketScope,
WA_Other
};
bool isWmemAllocationFamily(AllocationFamily family) {
return family == AF_Wmem || family == AF_WmemStringVector;
}
class AllocState {
enum Kind : unsigned { Allocated, Freed };
unsigned K : 1;
unsigned Family : 28;
unsigned WA : 3;
AllocState(Kind InK, AllocationFamily family, WmemAllocator wa)
: K(InK), Family(family), WA(wa) {
assert(family != AF_None);
assert(isWmemAllocationFamily(family) ^ (WA == WA_Invalid));
}
public:
bool isAllocated() const { return K == Allocated; }
bool isFreed() const { return K == Freed; }
bool isFamily(AllocationFamily family) const { return Family == family; }
AllocationFamily getAllocationFamily() const {
return (AllocationFamily)Family;
}
bool isWmemAllocator(WmemAllocator wa) const { return WA == wa; }
WmemAllocator getWmemAllocator() const { return (WmemAllocator)WA; }
/// Returns true if this is scoped wmem-allocated memory that is automatically
/// freed when the scope is left.
bool isManagedDeallocation() const {
return isWmemAllocationFamily((AllocationFamily)Family) && WA != WA_Null;
}
static AllocState getAllocated(AllocationFamily family, WmemAllocator wa) {
return AllocState(Allocated, family, wa);
}
static AllocState getFreed(AllocationFamily family, WmemAllocator wa) {
return AllocState(Freed, family, wa);
}
bool operator==(const AllocState &X) const {
return X.K == K && X.Family == Family && X.WA == WA;
}
void Profile(llvm::FoldingSetNodeID &ID) const {
ID.AddInteger(K);
ID.AddInteger(Family);
ID.AddInteger(WA);
}
};
class AllocFreeChecker
: public Checker<check::PostCall, check::PreCall, check::DeadSymbols,
check::PointerEscape> {
CallDescription FuncGArrayFree, FuncGArrayNew, FuncGArraySizedNew,
FuncGByteArrayNew, FuncGByteArrayNewTake, FuncGByteArraySizedNew,
FuncGByteArrayFree, FuncGFree, FuncGMalloc, FuncGMalloc0, FuncGMemdup,
FuncGPtrArrayNew, FuncGPtrArraySizedNew, FuncGPtrArrayNewWithFreeFunc,
FuncGPtrArrayNewFull, FuncGPtrArrayFree, FuncGRealloc, FuncGStrconcat,
FuncGStrdup, FuncGStrdupPrintf, FuncGStrdupVprintf, FuncGStrdupv,
FuncGStrfreev, FuncGStrjoin, FuncGStrjoinv, FuncGStrndup, FuncGStrsplit,
FuncGStrsplitSet, FuncWmemAlloc, FuncWmemAlloc0, FuncWmemAsciiStrdown,
FuncWmemFree, FuncWmemRealloc, FuncWmemStrconcat, FuncWmemStrdup,
FuncWmemStrdupPrintf, FuncWmemStrdupVprintf, FuncWmemStrjoin,
FuncWmemStrjoinv, FuncWmemStrndup, FuncWmemStrsplit;
CallDescription FuncGStrcanon, FuncGStrchomp, FuncGStrchug, FuncGStrdelimit,
FuncGStringAsciiDown, FuncGStringAsciiUp, FuncGStrreverse;
CallDescription FuncGByteArrayAppend, FuncGByteArrayPrepend,
FuncGByteArrayRemoveIndex, FuncGByteArrayRemoveIndexFast,
FuncGByteArrayRemoveRange, FuncGByteArraySetSize, FuncGByteArraySort,
FuncGByteArraySortWithData;
CallDescription FuncTvbNewRealData;
std::unique_ptr<BugType> AllocDeallocMismatchBugType;
std::unique_ptr<BugType> DoubleFreeBugType;
std::unique_ptr<BugType> LeakBugType;
AllocationFamily getAllocFamily(const CallEvent &Call) const;
AllocationFamily getDeallocFamily(const CallEvent &Call) const;
bool guaranteedNotToFreeMemory(const CallEvent &Call) const;
bool isIdentityFunction(const CallEvent &Call) const;
void reportAllocDeallocMismatch(SymbolRef AddressSym, const CallEvent &Call,
CheckerContext &C, AllocationFamily family,
WmemAllocator wmemAllocator) const;
void reportDoubleFree(SymbolRef AddressSym, const CallEvent &Call,
CheckerContext &C, const char *msg) const;
void reportLeak(SymbolRef AddressSym, CheckerContext &C, bool potential,
ExplodedNode *ErrNode, const ExplodedNode *AllocNode) const;
public:
AllocFreeChecker();
/// Process alloc.
void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
/// Process free.
void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
ProgramStateRef checkPointerEscape(ProgramStateRef State,
const InvalidatedSymbols &Escaped,
const CallEvent *Call,
PointerEscapeKind Kind) const;
/// The bug visitor which allows us to print extra diagnostics along the
/// BugReport path. For example, showing the allocation site of the leaked
/// region.
class MallocBugVisitor final
: public BugReporterVisitorImpl<MallocBugVisitor> {
// The symbol representing the memory allocated by malloc.
SymbolRef Sym;
public:
MallocBugVisitor(SymbolRef S) : Sym(S) {}
void Profile(llvm::FoldingSetNodeID &ID) const override {
// This presumably exists to ensure that this node is not folded into
// another due to being considered equivalent.
static int X = 0;
ID.AddPointer(&X);
ID.AddPointer(Sym);
}
std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
const ExplodedNode *PrevN,
BugReporterContext &BRC,
BugReport &BR) override;
};
};
} // end anonymous namespace
// Register a map from pointer addresses to their state.
REGISTER_MAP_WITH_PROGRAMSTATE(AddressMap, SymbolRef, AllocState)
AllocFreeChecker::AllocFreeChecker()
: FuncGArrayFree("g_array_free"), FuncGArrayNew("g_array_new"),
FuncGArraySizedNew("g_array_sized_new"),
FuncGByteArrayNew("g_byte_array_new"),
FuncGByteArrayNewTake("g_byte_array_new_take"),
FuncGByteArraySizedNew("g_byte_array_sized_new"),
FuncGByteArrayFree("g_byte_array_free"), FuncGFree("g_free"),
FuncGMalloc("g_malloc"), FuncGMalloc0("g_malloc0"),
FuncGMemdup("g_memdup"), FuncGPtrArrayNew("g_ptr_array_new"),
FuncGPtrArraySizedNew("g_ptr_array_sized_new"),
FuncGPtrArrayNewWithFreeFunc("g_ptr_array_new_with_free_func"),
FuncGPtrArrayNewFull("g_ptr_array_new_full"),
FuncGPtrArrayFree("g_ptr_array_free"), FuncGRealloc("g_realloc"),
FuncGStrconcat("g_strconcat"), FuncGStrdup("g_strdup"),
FuncGStrdupPrintf("g_strdup_printf"),
FuncGStrdupVprintf("g_strdup_vprintf"), FuncGStrdupv("g_strdupv"),
FuncGStrfreev("g_strfreev"), FuncGStrjoin("g_strjoin"),
FuncGStrjoinv("g_strjoinv"), FuncGStrndup("g_strndup"),
FuncGStrsplit("g_strsplit"), FuncGStrsplitSet("g_strsplit_set"),
FuncWmemAlloc("wmem_alloc"), FuncWmemAlloc0("wmem_alloc0"),
FuncWmemAsciiStrdown("wmem_ascii_strdown"), FuncWmemFree("wmem_free"),
FuncWmemRealloc("wmem_realloc"), FuncWmemStrconcat("wmem_strconcat"),
FuncWmemStrdup("wmem_strdup"), FuncWmemStrdupPrintf("wmem_strdup_printf"),
FuncWmemStrdupVprintf("wmem_strdup_vprintf"),
FuncWmemStrjoin("wmem_strjoin"), FuncWmemStrjoinv("wmem_strjoinv"),
FuncWmemStrndup("wmem_strndup"), FuncWmemStrsplit("wmem_strsplit"),
FuncGStrcanon("g_strcanon"), FuncGStrchomp("g_strchomp"),
FuncGStrchug("g_strchug"), FuncGStrdelimit("g_strdelimit"),
FuncGStringAsciiUp("g_string_ascii_up"),
FuncGStringAsciiDown("g_string_ascii_down"),
FuncGStrreverse("g_strreverse"),
FuncGByteArrayAppend("g_byte_array_append"),
FuncGByteArrayPrepend("g_byte_array_prepend"),
FuncGByteArrayRemoveIndex("g_byte_array_remove_index"),
FuncGByteArrayRemoveIndexFast("g_byte_array_remove_index_fast"),
FuncGByteArrayRemoveRange("g_byte_array_remove_range"),
FuncGByteArraySetSize("g_byte_array_set_size"),
FuncGByteArraySort("g_byte_array_sort"),
FuncGByteArraySortWithData("g_byte_array_sort_with_data"),
FuncTvbNewRealData("tvb_new_real_data") {
AllocDeallocMismatchBugType.reset(
new BugType(this, "Alloc-dealloc mismatch", categories::MemoryError));
DoubleFreeBugType.reset(
new BugType(this, "Double free", categories::MemoryError));
LeakBugType.reset(new BugType(this, "Memory leak", categories::MemoryError));
// Sinks are higher importance bugs as well as calls to assert() or exit(0).
LeakBugType->setSuppressOnSink(true);
}
WmemAllocator getWmemAllocator(const CallEvent &Call) {
const Expr *ArgE = Call.getArgExpr(0);
if (!ArgE)
return WA_Invalid;
// If this is a NULL macro or otherwise an expression that evaluates as such,
// then assume a NULL scope.
SVal ArgSVal = Call.getArgSVal(0);
if (ArgSVal.isZeroConstant())
return WA_Null;
ArgE = ArgE->IgnoreParenCasts();
if (const CallExpr *CE = dyn_cast<CallExpr>(ArgE)) {
if (const FunctionDecl *FD = CE->getDirectCallee()) {
StringRef DeallocatorName = FD->getName();
if (DeallocatorName == "wmem_epan_scope") {
return WA_EpanScope;
}
if (DeallocatorName == "wmem_file_scope") {
return WA_FileScope;
}
if (DeallocatorName == "wmem_packet_scope") {
return WA_PacketScope;
}
}
// Unknown scope
return WA_Other;
}
// Unknown type (perhaps pinfo->pool?)
return WA_Other;
}
AllocationFamily AllocFreeChecker::getAllocFamily(const CallEvent &Call) const {
if (Call.isCalled(FuncGMalloc) || Call.isCalled(FuncGMalloc0) ||
Call.isCalled(FuncGMemdup) || Call.isCalled(FuncGStrdup) ||
Call.isCalled(FuncGStrndup) || Call.isCalled(FuncGRealloc) ||
Call.isCalled(FuncGStrdupPrintf) || Call.isCalled(FuncGStrdupVprintf) ||
Call.isCalled(FuncGStrconcat) || Call.isCalled(FuncGStrjoin) ||
Call.isCalled(FuncGStrjoinv)) {
return AF_Glib;
} else if (Call.isCalled(FuncGStrdupv) || Call.isCalled(FuncGStrsplit) ||
Call.isCalled(FuncGStrsplitSet)) {
return AF_GlibStringVector;
} else if (Call.isCalled(FuncGArrayNew) ||
Call.isCalled(FuncGArraySizedNew)) {
return AF_GlibArray;
} else if (Call.isCalled(FuncGPtrArrayNew) ||
Call.isCalled(FuncGPtrArrayNewFull) ||
Call.isCalled(FuncGPtrArrayNewWithFreeFunc) ||
Call.isCalled(FuncGPtrArraySizedNew)) {
return AF_GlibPtrArray;
} else if (Call.isCalled(FuncGByteArrayNew) ||
Call.isCalled(FuncGByteArrayNewTake) ||
Call.isCalled(FuncGByteArraySizedNew)) {
return AF_GlibByteArray;
} else if (Call.isCalled(FuncGArrayFree) ||
Call.isCalled(FuncGByteArrayFree) ||
Call.isCalled(FuncGPtrArrayFree)) {
// g_array_free(..., FALSE) returns new memory to be freed with g_free
if (Call.getNumArgs() == 2 && Call.getArgSVal(1).isZeroConstant()) {
return AF_Glib;
}
} else if (Call.isCalled(FuncWmemAlloc) || Call.isCalled(FuncWmemAlloc0) ||
Call.isCalled(FuncWmemRealloc) || Call.isCalled(FuncWmemStrdup) ||
Call.isCalled(FuncWmemStrndup) ||
Call.isCalled(FuncWmemStrdupPrintf) ||
Call.isCalled(FuncWmemStrdupVprintf) ||
Call.isCalled(FuncWmemStrconcat) ||
Call.isCalled(FuncWmemStrjoin) ||
Call.isCalled(FuncWmemStrjoinv) ||
Call.isCalled(FuncWmemAsciiStrdown)) {
return AF_Wmem;
} else if (Call.isCalled(FuncWmemStrsplit)) {
return AF_WmemStringVector;
}
return AF_None;
}
AllocationFamily
AllocFreeChecker::getDeallocFamily(const CallEvent &Call) const {
if (Call.isCalled(FuncGFree) || Call.isCalled(FuncGRealloc)) {
return AF_Glib;
} else if (Call.isCalled(FuncGStrfreev)) {
return AF_GlibStringVector;
} else if (Call.isCalled(FuncGArrayFree)) {
return AF_GlibArray;
} else if (Call.isCalled(FuncGPtrArrayFree)) {
return AF_GlibPtrArray;
} else if (Call.isCalled(FuncGByteArrayFree)) {
return AF_GlibByteArray;
} else if (Call.isCalled(FuncWmemFree) || Call.isCalled(FuncWmemRealloc)) {
return AF_Wmem;
}
return AF_None;
}
bool AllocFreeChecker::guaranteedNotToFreeMemory(const CallEvent &Call) const {
// Assume that GLib functions (g_*) and wmem functions (wmem_*) do not release
// or change the address (that will be handled in PostCall).
if (Call.isCalled(FuncGFree) || Call.isCalled(FuncGRealloc) ||
Call.isCalled(FuncWmemFree) || Call.isCalled(FuncWmemRealloc))
return true;
if (Call.isCalled(FuncGByteArraySort) ||
Call.isCalled(FuncGByteArraySortWithData))
return true;
return false;
}
// Returns true iff its first argument is equal to the return value.
bool AllocFreeChecker::isIdentityFunction(const CallEvent &Call) const {
if (Call.isCalled(FuncGStrcanon) || Call.isCalled(FuncGStrchomp) ||
Call.isCalled(FuncGStrchug) || Call.isCalled(FuncGStrdelimit) ||
Call.isCalled(FuncGStringAsciiUp) ||
Call.isCalled(FuncGStringAsciiDown) || Call.isCalled(FuncGStrreverse))
return true;
if (Call.isCalled(FuncGByteArrayAppend) ||
Call.isCalled(FuncGByteArrayPrepend) ||
Call.isCalled(FuncGByteArrayRemoveIndex) ||
Call.isCalled(FuncGByteArrayRemoveIndexFast) ||
Call.isCalled(FuncGByteArrayRemoveRange) ||
Call.isCalled(FuncGByteArraySetSize))
return true;
return false;
}
void printExpectedDeallocName(raw_ostream &os, AllocationFamily family,
WmemAllocator wmemAllocator) {
switch (family) {
case AF_Glib:
os << "g_free";
break;
case AF_GlibStringVector:
os << "g_strfreev";
break;
case AF_GlibArray:
os << "g_array_free";
break;
case AF_GlibPtrArray:
os << "g_ptr_array_free";
break;
case AF_GlibByteArray:
os << "g_byte_array_free";
break;
case AF_Wmem:
case AF_WmemStringVector: // TODO find better API for wmem_strsplit
switch (wmemAllocator) {
case WA_Null:
os << "wmem_free(NULL, ...)";
break;
case WA_EpanScope:
os << "wmem_free(wmem_epan_scope(), ...)";
break;
case WA_FileScope:
os << "wmem_free(wmem_file_scope(), ...)";
break;
case WA_PacketScope:
os << "wmem_free(wmem_packet_scope(), ...)";
break;
case WA_Other:
os << "wmem_free";
break;
case WA_Invalid:
llvm_unreachable("suspicious wmem allocator argument");
}
break;
case AF_None:
llvm_unreachable("suspicious argument");
}
}
const ExplodedNode *getAllocationSite(const ExplodedNode *N, SymbolRef Sym) {
const LocationContext *LeakContext = N->getLocationContext();
// Walk the ExplodedGraph backwards and find the first node that referred to
// the tracked symbol.
const ExplodedNode *AllocNode = N;
while (N) {
ProgramStateRef State = N->getState();
if (!State->get<AddressMap>(Sym))
break;
// Only consider allocations in the same function, or higher in the call
// chain.
const LocationContext *NContext = N->getLocationContext();
if (NContext == LeakContext || NContext->isParentOf(LeakContext))
AllocNode = N;
N = N->pred_empty() ? nullptr : *(N->pred_begin());
}
return AllocNode;
}
/// Process alloc
void AllocFreeChecker::checkPostCall(const CallEvent &Call,
CheckerContext &C) const {
if (!Call.isGlobalCFunction())
return;
AllocationFamily family = getAllocFamily(Call);
if (family != AF_None) {
SymbolRef Address = Call.getReturnValue().getAsSymbol();
if (!Address)
return;
WmemAllocator WA = WA_Invalid;
if (isWmemAllocationFamily(family) && Call.getNumArgs() > 0) {
WA = getWmemAllocator(Call);
}
// Generate the next transition (an edge in the exploded graph).
ProgramStateRef State = C.getState();
State =
State->set<AddressMap>(Address, AllocState::getAllocated(family, WA));
C.addTransition(State);
} else if (isIdentityFunction(Call)) {
ProgramStateRef State = C.getState();
SVal Arg0 = Call.getArgSVal(0);
State = State->BindExpr(Call.getOriginExpr(), C.getLocationContext(), Arg0);
C.addTransition(State);
}
// HACK: avoid false positive with tvb functions (tvb_new_real_data) that have
// a non-const argument, but consume it anyway.
// TODO improve this by checking for a matching tvb_set_free_cb.
if (Call.isCalled(FuncTvbNewRealData) && Call.getNumArgs() > 0) {
ProgramStateRef State = C.getState();
SymbolRef Address = Call.getArgSVal(0).getAsSymbol();
if (!Address)
return;
State = State->remove<AddressMap>(Address);
C.addTransition(State);
}
}
void AllocFreeChecker::checkPreCall(const CallEvent &Call,
CheckerContext &C) const {
if (!Call.isGlobalCFunction() || Call.getNumArgs() == 0)
return;
AllocationFamily family = getDeallocFamily(Call);
if (family != AF_None) {
unsigned pointerParam = isWmemAllocationFamily(family) ? 1 : 0;
if (Call.getNumArgs() < pointerParam + 1)
return;
SymbolRef Address = Call.getArgSVal(pointerParam).getAsSymbol();
if (!Address)
return;
WmemAllocator WA =
isWmemAllocationFamily(family) ? getWmemAllocator(Call) : WA_Invalid;
// Check if the pointer was indeed allocated.
ProgramStateRef State = C.getState();
const AllocState *AS = State->get<AddressMap>(Address);
if (AS) {
// Special case: wmem_strsplit currently does not have a dedicated free
// function. Treat wmem_free with the correct scope as its free function.
if (AS->isFamily(AF_WmemStringVector) && family == AF_Wmem) {
family = AF_WmemStringVector;
}
if (AS->isFreed()) {
reportDoubleFree(Address, Call, C, "memory was freed before");
return;
} else if (!AS->isFamily(family)) {
reportAllocDeallocMismatch(Address, Call, C, AS->getAllocationFamily(),
AS->getWmemAllocator());
return;
} else if (isWmemAllocationFamily(family) && !AS->isWmemAllocator(WA)) {
reportAllocDeallocMismatch(Address, Call, C, AS->getAllocationFamily(),
AS->getWmemAllocator());
return;
} else if (family == AF_WmemStringVector) {
// wmem_packet_scope is quite transient, assume that other scopes are
// not safe and indicate a memleak.
if (!AS->isWmemAllocator(WA_PacketScope)) {
ExplodedNode *N = C.generateNonFatalErrorNode(State);
const ExplodedNode *AllocNode = getAllocationSite(N, Address);
reportLeak(Address, C, true, N, AllocNode);
}
}
}
// Generate the next transition (an edge in the exploded graph).
State = State->set<AddressMap>(Address, AllocState::getFreed(family, WA));
C.addTransition(State);
}
}
static bool isLeaked(SymbolRef Sym, const AllocState &AS, bool IsSymDead,
ProgramStateRef State) {
if (IsSymDead && (AS.isAllocated() && !AS.isManagedDeallocation())) {
// If a symbol is NULL, no memory was allocated (e.g. g_strdup(NULL)).
// A symbol should only be considered leaked if it is non-null.
ConstraintManager &CMgr = State->getConstraintManager();
ConditionTruthVal AllocFailed = CMgr.isNull(State, Sym);
return !AllocFailed.isConstrainedTrue();
}
return false;
}
void AllocFreeChecker::checkDeadSymbols(SymbolReaper &SymReaper,
CheckerContext &C) const {
ProgramStateRef State = C.getState();
LeakInfoVector LeakInfos;
AddressMapTy TrackedAddresses = State->get<AddressMap>();
for (AddressMapTy::iterator I = TrackedAddresses.begin(),
E = TrackedAddresses.end();
I != E; ++I) {
SymbolRef Sym = I->first;
bool IsSymDead = SymReaper.isDead(Sym);
if (isLeaked(Sym, I->second, IsSymDead, State)) {
// Check here for the original node that allocated the memory, this check
// will not always be possible when it the symbol is removed from the
// state, see below.
const ExplodedNode *AllocNode =
getAllocationSite(C.getPredecessor(), Sym);
LeakInfos.emplace_back(Sym, AllocNode);
}
if (IsSymDead)
State = State->remove<AddressMap>(Sym);
}
if (!LeakInfos.empty()) {
ExplodedNode *N = C.generateNonFatalErrorNode(State);
if (!N)
return;
for (LeakInfo Leaked : LeakInfos) {
reportLeak(Leaked.first, C, false, N, Leaked.second);
}
}
}
void AllocFreeChecker::reportAllocDeallocMismatch(
SymbolRef AddressSym, const CallEvent &Call, CheckerContext &C,
AllocationFamily family, WmemAllocator wmemAllocator) const {
// We reached a bug, stop exploring the path here by generating a sink.
ExplodedNode *ErrNode = C.generateErrorNode();
// If we have already reached this node on another path, return.
if (!ErrNode)
return;
SmallString<100> buf;
llvm::raw_svector_ostream os(buf);
os << "Memory is expected to be deallocated by ";
printExpectedDeallocName(os, family, wmemAllocator);
// Generate a bug report.
auto R = llvm::make_unique<BugReport>(*AllocDeallocMismatchBugType, os.str(),
ErrNode);
R->addRange(Call.getSourceRange());
R->markInteresting(AddressSym);
R->addVisitor(llvm::make_unique<MallocBugVisitor>(AddressSym));
C.emitReport(std::move(R));
}
void AllocFreeChecker::reportDoubleFree(SymbolRef AddressSym,
const CallEvent &Call,
CheckerContext &C,
const char *msg) const {
// We reached a bug, stop exploring the path here by generaring a sink.
ExplodedNode *ErrNode = C.generateErrorNode();
// If we have already reached this node on another path, return.
if (!ErrNode)
return;
// Generate a bug report.
auto R = llvm::make_unique<BugReport>(*DoubleFreeBugType, msg, ErrNode);
R->addRange(Call.getSourceRange());
R->markInteresting(AddressSym);
R->addVisitor(llvm::make_unique<MallocBugVisitor>(AddressSym));
C.emitReport(std::move(R));
}
void AllocFreeChecker::reportLeak(SymbolRef AddressSym, CheckerContext &C,
bool potential, ExplodedNode *ErrNode,
const ExplodedNode *AllocNode) const {
// Most bug reports are cached at the location where they occurred.
// With leaks, we want to unique them by the location where they were
// allocated, and only report a single path.
PathDiagnosticLocation LocUsedForUniqueing;
const Stmt *AllocationStmt = PathDiagnosticLocation::getStmt(AllocNode);
if (AllocationStmt)
LocUsedForUniqueing = PathDiagnosticLocation::createBegin(
AllocationStmt, C.getSourceManager(), AllocNode->getLocationContext());
auto R = llvm::make_unique<BugReport>(
*LeakBugType, potential ? "Potential memory leak" : "Memory leak",
ErrNode, LocUsedForUniqueing, AllocNode->getLocationContext()->getDecl());
R->markInteresting(AddressSym);
R->addVisitor(llvm::make_unique<MallocBugVisitor>(AddressSym));
C.emitReport(std::move(R));
}
// If the pointer we are tracking escaped, do not track the symbol as
// we cannot reason about it anymore.
ProgramStateRef AllocFreeChecker::checkPointerEscape(
ProgramStateRef State, const InvalidatedSymbols &Escaped,
const CallEvent *Call, PointerEscapeKind Kind) const {
// If this memory will not be freed, keep the memory in the state.
if (Kind == PSK_DirectEscapeOnCall &&
(guaranteedNotToFreeMemory(*Call) || isIdentityFunction(*Call))) {
return State;
}
for (InvalidatedSymbols::const_iterator I = Escaped.begin(),
E = Escaped.end();
I != E; ++I) {
SymbolRef Sym = *I;
// The symbol escaped. Optimistically, assume that the corresponding memory
// will be deallocated somewhere else.
State = State->remove<AddressMap>(Sym);
}
return State;
}
std::shared_ptr<PathDiagnosticPiece>
AllocFreeChecker::MallocBugVisitor::VisitNode(const ExplodedNode *N,
const ExplodedNode *PrevN,
BugReporterContext &BRC,
BugReport & /* BR */) {
ProgramStateRef state = N->getState();
ProgramStateRef statePrev = PrevN->getState();
const AllocState *AS = state->get<AddressMap>(Sym);
const AllocState *ASPrev = statePrev->get<AddressMap>(Sym);
if (!AS)
return nullptr;
const Stmt *S = PathDiagnosticLocation::getStmt(N);
if (!S)
return nullptr;
const char *Msg = nullptr;
// Mark new memory allocations (transition unknown/unallocated -> allocated)
if ((!ASPrev || !ASPrev->isAllocated()) && AS->isAllocated()) {
Msg = "Memory is allocated";
}
// Mark freeing of memory (transition unknown/allocated -> freed)
if ((!ASPrev || ASPrev->isAllocated()) && AS->isFreed()) {
Msg = "Memory is released";
}
if (!Msg)
return nullptr;
// Generate the extra diagnostic.
PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
N->getLocationContext());
return std::make_shared<PathDiagnosticEventPiece>(Pos, Msg, true);
}
#if 0
void ento::registerAllocFreeChecker(CheckerManager &mgr) {
mgr.registerChecker<AllocFreeChecker>();
}
#endif
// Register plugin!
extern "C" void clang_registerCheckers(CheckerRegistry ®istry) {
registry.addChecker<AllocFreeChecker>(
"alpha.AllocFree",
"Detects mismatches between memory allocations and deallocations");
}
extern "C" const char clang_analyzerAPIVersionString[] =
CLANG_ANALYZER_API_VERSION_STRING;