-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNullDerefAnalysis.cpp
More file actions
380 lines (324 loc) · 13.6 KB
/
NullDerefAnalysis.cpp
File metadata and controls
380 lines (324 loc) · 13.6 KB
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
// SPDX-License-Identifier: Apache-2.0
#include "analysis/NullDerefAnalysis.hpp"
#include "analysis/AnalyzerUtils.hpp"
#include <string>
#include <unordered_set>
#include <llvm/ADT/StringRef.h>
#include <llvm/Analysis/ValueTracking.h>
#include <llvm/IR/Argument.h>
#include <llvm/IR/CFG.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Operator.h>
namespace ctrace::stack::analysis
{
namespace
{
static bool isLocalPointerSlot(const llvm::Value* pointer)
{
const auto* allocaInst = llvm::dyn_cast_or_null<llvm::AllocaInst>(
pointer ? pointer->stripPointerCasts() : nullptr);
return allocaInst && allocaInst->isStaticAlloca();
}
static const llvm::Value* dereferencedPointer(const llvm::Instruction& inst)
{
if (const auto* load = llvm::dyn_cast<llvm::LoadInst>(&inst))
{
if (load->getType()->isPointerTy() && isLocalPointerSlot(load->getPointerOperand()))
{
return nullptr;
}
return load->getPointerOperand();
}
if (const auto* store = llvm::dyn_cast<llvm::StoreInst>(&inst))
{
if (store->getValueOperand()->getType()->isPointerTy() &&
isLocalPointerSlot(store->getPointerOperand()))
{
return nullptr;
}
return store->getPointerOperand();
}
if (const auto* atomic = llvm::dyn_cast<llvm::AtomicRMWInst>(&inst))
return atomic->getPointerOperand();
if (const auto* cmp = llvm::dyn_cast<llvm::AtomicCmpXchgInst>(&inst))
return cmp->getPointerOperand();
return nullptr;
}
static std::string pointerDisplayName(const llvm::Value* pointer)
{
if (!pointer)
return "<unknown>";
const llvm::Value* root = llvm::getUnderlyingObject(pointer, 32);
if (const auto* allocaInst = llvm::dyn_cast<llvm::AllocaInst>(root))
{
if (allocaInst->hasName())
return allocaInst->getName().str();
}
if (const auto* arg = llvm::dyn_cast<llvm::Argument>(root))
{
if (arg->hasName())
return arg->getName().str();
return "<arg>";
}
if (root && root->hasName())
return root->getName().str();
return "<pointer>";
}
static llvm::StringRef canonicalExternalCalleeName(llvm::StringRef name)
{
if (!name.empty() && name.front() == '\1')
name = name.drop_front();
if (name.starts_with("_"))
name = name.drop_front();
const std::size_t dollarPos = name.find('$');
if (dollarPos != llvm::StringRef::npos)
name = name.take_front(dollarPos);
return name;
}
static bool isAllocatorLikeName(llvm::StringRef calleeName)
{
return calleeName == "malloc" || calleeName == "calloc" || calleeName == "realloc" ||
calleeName == "aligned_alloc";
}
static bool hasKnownNonNullReturn(const llvm::CallBase& call, const llvm::Function* callee)
{
if (call.hasRetAttr(llvm::Attribute::NonNull) ||
call.hasRetAttr(llvm::Attribute::Dereferenceable))
{
return true;
}
if (!callee)
return false;
const llvm::AttributeList& attrs = callee->getAttributes();
return attrs.hasRetAttr(llvm::Attribute::NonNull) ||
attrs.hasRetAttr(llvm::Attribute::Dereferenceable);
}
static bool isUncheckedAllocatorResult(const llvm::Value* value)
{
if (!value)
return false;
const auto* call = llvm::dyn_cast<llvm::CallBase>(value->stripPointerCasts());
if (!call)
return false;
const llvm::Function* callee = call->getCalledFunction();
if (!callee || !callee->isDeclaration())
return false;
const llvm::StringRef calleeName = canonicalExternalCalleeName(callee->getName());
if (!isAllocatorLikeName(calleeName))
return false;
return !hasKnownNonNullReturn(*call, callee);
}
static const llvm::Value* stripPointerAddressOps(const llvm::Value* pointer)
{
if (!pointer)
return nullptr;
const llvm::Value* current = pointer;
for (unsigned depth = 0; depth < 8; ++depth)
{
current = current->stripPointerCasts();
const auto* gep = llvm::dyn_cast<llvm::GEPOperator>(current);
if (!gep)
break;
current = gep->getPointerOperand();
}
return current->stripPointerCasts();
}
static const llvm::Value* canonicalPointerIdentity(const llvm::Value* pointer)
{
const llvm::Value* current = stripPointerAddressOps(pointer);
if (!current)
return nullptr;
if (const auto* load = llvm::dyn_cast<llvm::LoadInst>(current))
{
const llvm::Value* slot = load->getPointerOperand()->stripPointerCasts();
if (llvm::isa<llvm::AllocaInst>(slot) || llvm::isa<llvm::Argument>(slot))
return slot;
}
if (const llvm::Value* underlying = llvm::getUnderlyingObject(current, 32))
return underlying;
return current;
}
static bool samePointerRoot(const llvm::Value* lhs, const llvm::Value* rhs)
{
if (!lhs || !rhs)
return false;
return canonicalPointerIdentity(lhs) == canonicalPointerIdentity(rhs);
}
static bool isNullValue(const llvm::Value* value)
{
return value && llvm::isa<llvm::ConstantPointerNull>(value->stripPointerCasts());
}
static bool conditionImpliesNullForSuccessor(const llvm::BranchInst& branch,
const llvm::BasicBlock& successor,
const llvm::Value*& outPointer)
{
if (!branch.isConditional())
return false;
const llvm::ICmpInst* cmp =
llvm::dyn_cast<llvm::ICmpInst>(branch.getCondition()->stripPointerCasts());
if (!cmp)
return false;
const llvm::Value* lhs = cmp->getOperand(0);
const llvm::Value* rhs = cmp->getOperand(1);
const llvm::Value* pointer = nullptr;
bool nullOnTrue = false;
if (isNullValue(lhs) && rhs->getType()->isPointerTy())
{
pointer = rhs;
}
else if (isNullValue(rhs) && lhs->getType()->isPointerTy())
{
pointer = lhs;
}
else
{
return false;
}
switch (cmp->getPredicate())
{
case llvm::ICmpInst::ICMP_EQ:
nullOnTrue = true;
break;
case llvm::ICmpInst::ICMP_NE:
nullOnTrue = false;
break;
default:
return false;
}
const bool isTrueSucc = branch.getSuccessor(0) == &successor;
const bool impliesNull = isTrueSucc ? nullOnTrue : !nullOnTrue;
if (!impliesNull)
return false;
outPointer = pointer;
return true;
}
static bool precedingStoreSetsNull(const llvm::Instruction& derefInst,
const llvm::Value* pointerOperand)
{
const auto* pointerLoad =
llvm::dyn_cast<llvm::LoadInst>(stripPointerAddressOps(pointerOperand));
if (!pointerLoad)
return false;
const llvm::Value* slot = pointerLoad->getPointerOperand()->stripPointerCasts();
if (!llvm::isa<llvm::AllocaInst>(slot))
return false;
const llvm::BasicBlock* block = derefInst.getParent();
if (!block)
return false;
for (auto it = derefInst.getIterator(); it != block->begin();)
{
--it;
const llvm::Instruction& candidate = *it;
const auto* store = llvm::dyn_cast<llvm::StoreInst>(&candidate);
if (!store)
continue;
if (store->getPointerOperand()->stripPointerCasts() != slot)
continue;
return isNullValue(store->getValueOperand());
}
return false;
}
static const llvm::StoreInst*
findNearestPrecedingStoreToSlot(const llvm::Instruction& derefInst, const llvm::Value* slot)
{
const llvm::BasicBlock* block = derefInst.getParent();
if (!block || !slot)
return nullptr;
for (auto it = derefInst.getIterator(); it != block->begin();)
{
--it;
const auto* store = llvm::dyn_cast<llvm::StoreInst>(&*it);
if (!store)
continue;
if (store->getPointerOperand()->stripPointerCasts() == slot)
return store;
}
return nullptr;
}
static bool derefComesFromUncheckedAllocator(const llvm::Instruction& derefInst,
const llvm::Value* pointerOperand)
{
const llvm::Value* producer = stripPointerAddressOps(pointerOperand);
if (!producer || !producer->getType()->isPointerTy())
return false;
if (isUncheckedAllocatorResult(producer))
return true;
const auto* pointerLoad = llvm::dyn_cast<llvm::LoadInst>(producer);
if (!pointerLoad)
return false;
const llvm::Value* slot = pointerLoad->getPointerOperand()->stripPointerCasts();
if (!llvm::isa<llvm::AllocaInst>(slot))
return false;
const llvm::StoreInst* originStore = findNearestPrecedingStoreToSlot(derefInst, slot);
if (!originStore)
return false;
return isUncheckedAllocatorResult(originStore->getValueOperand());
}
} // namespace
std::vector<NullDerefIssue>
analyzeNullDereferences(llvm::Module& mod,
const std::function<bool(const llvm::Function&)>& shouldAnalyze)
{
std::vector<NullDerefIssue> issues;
std::unordered_set<const llvm::Instruction*> emitted;
for (llvm::Function& function : mod)
{
if (function.isDeclaration() || !shouldAnalyze(function))
continue;
for (llvm::BasicBlock& block : function)
{
for (llvm::Instruction& inst : block)
{
const llvm::Value* pointer = dereferencedPointer(inst);
if (!pointer || !pointer->getType()->isPointerTy())
continue;
NullDerefIssueKind kind = NullDerefIssueKind::DirectNullPointer;
bool shouldEmit = false;
if (isNullValue(pointer))
{
kind = NullDerefIssueKind::DirectNullPointer;
shouldEmit = true;
}
else if (precedingStoreSetsNull(inst, pointer))
{
kind = NullDerefIssueKind::NullStoredInLocalSlot;
shouldEmit = true;
}
else if (llvm::pred_size(&block) == 1)
{
const llvm::BasicBlock* pred = *llvm::pred_begin(&block);
const auto* branch =
pred ? llvm::dyn_cast<llvm::BranchInst>(pred->getTerminator())
: nullptr;
const llvm::Value* nullComparedPointer = nullptr;
if (branch &&
conditionImpliesNullForSuccessor(*branch, block, nullComparedPointer) &&
samePointerRoot(pointer, nullComparedPointer))
{
kind = NullDerefIssueKind::NullBranchDereference;
shouldEmit = true;
}
}
if (!shouldEmit && derefComesFromUncheckedAllocator(inst, pointer))
{
kind = NullDerefIssueKind::UncheckedAllocatorResult;
shouldEmit = true;
}
if (!shouldEmit || !emitted.insert(&inst).second)
continue;
NullDerefIssue issue;
issue.funcName = function.getName().str();
issue.filePath = getFunctionSourcePath(function);
issue.pointerName = pointerDisplayName(pointer);
issue.kind = kind;
issue.inst = &inst;
issues.push_back(std::move(issue));
}
}
}
return issues;
}
} // namespace ctrace::stack::analysis