-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllocaUsage.cpp
More file actions
211 lines (181 loc) · 7.34 KB
/
AllocaUsage.cpp
File metadata and controls
211 lines (181 loc) · 7.34 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
// SPDX-License-Identifier: Apache-2.0
#include "analysis/AllocaUsage.hpp"
#include <optional>
#include <llvm/ADT/SmallPtrSet.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/DataLayout.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Type.h>
#include <llvm/IR/Value.h>
#include "analysis/IRValueUtils.hpp"
#include "analysis/IntRanges.hpp"
namespace ctrace::stack::analysis
{
namespace
{
static bool isValueUserControlledImpl(const llvm::Value* V, const llvm::Function& F,
llvm::SmallPtrSet<const llvm::Value*, 16>& visited,
int depth = 0)
{
using namespace llvm;
if (!V || depth > 20)
return false;
if (visited.contains(V))
return false;
visited.insert(V);
if (isa<Argument>(V))
return true; // function argument -> considered user-provided
if (isa<Constant>(V))
return false;
if (auto* LI = dyn_cast<LoadInst>(V))
{
const Value* ptr = LI->getPointerOperand()->stripPointerCasts();
if (isa<Argument>(ptr))
return true; // load through pointer passed as argument
if (!isa<AllocaInst>(ptr))
{
return true; // load from non-local memory (global / heap / unknown)
}
// If it's a local alloca, inspect what gets stored there.
const AllocaInst* AI = cast<AllocaInst>(ptr);
for (const Use& U : AI->uses())
{
if (auto* SI = dyn_cast<StoreInst>(U.getUser()))
{
if (SI->getPointerOperand()->stripPointerCasts() != ptr)
continue;
if (isValueUserControlledImpl(SI->getValueOperand(), F, visited, depth + 1))
{
return true;
}
}
}
}
if (auto* CB = dyn_cast<CallBase>(V))
{
// Value produced by a call: conservatively treat as external/user input.
(void)F;
(void)CB;
return true;
}
if (auto* I = dyn_cast<Instruction>(V))
{
for (const Value* Op : I->operands())
{
if (isValueUserControlledImpl(Op, F, visited, depth + 1))
return true;
}
}
else if (auto* CE = dyn_cast<ConstantExpr>(V))
{
for (const Value* Op : CE->operands())
{
if (isValueUserControlledImpl(Op, F, visited, depth + 1))
return true;
}
}
return false;
}
static bool isValueUserControlled(const llvm::Value* V, const llvm::Function& F)
{
llvm::SmallPtrSet<const llvm::Value*, 16> visited;
return isValueUserControlledImpl(V, F, visited, 0);
}
static std::optional<StackSize>
getAllocaUpperBoundBytes(const llvm::AllocaInst* AI, const llvm::DataLayout& DL,
const std::map<const llvm::Value*, IntRange>& ranges)
{
using namespace llvm;
const Value* sizeVal = AI->getArraySize();
auto findRange = [&ranges](const Value* V) -> const IntRange*
{
auto it = ranges.find(V);
if (it != ranges.end())
return &it->second;
return nullptr;
};
const IntRange* r = findRange(sizeVal);
if (!r)
{
if (auto* LI = dyn_cast<LoadInst>(sizeVal))
{
const Value* ptr = LI->getPointerOperand();
r = findRange(ptr);
}
}
if (r && r->hasUpper && r->upper > 0)
{
StackSize elemSize = DL.getTypeAllocSize(AI->getAllocatedType());
return static_cast<StackSize>(r->upper) * elemSize;
}
return std::nullopt;
}
static void analyzeAllocaUsageInFunction(llvm::Function& F, const llvm::DataLayout& DL,
bool isRecursive, bool isInfiniteRecursive,
std::vector<AllocaUsageIssue>& out)
{
using namespace llvm;
if (F.isDeclaration())
return;
auto ranges = computeIntRangesFromICmps(F);
for (BasicBlock& BB : F)
{
for (Instruction& I : BB)
{
auto* AI = dyn_cast<AllocaInst>(&I);
if (!AI)
continue;
// Only consider dynamic allocas: alloca(T, size) or VLA.
if (!AI->isArrayAllocation())
continue;
AllocaUsageIssue issue;
issue.funcName = F.getName().str();
issue.varName = deriveAllocaName(AI);
issue.allocaInst = AI;
issue.userControlled = isValueUserControlled(AI->getArraySize(), F);
issue.isRecursive = isRecursive;
issue.isInfiniteRecursive = isInfiniteRecursive;
StackSize elemSize = DL.getTypeAllocSize(AI->getAllocatedType());
const Value* arraySizeVal = AI->getArraySize();
if (auto* C = dyn_cast<ConstantInt>(arraySizeVal))
{
issue.sizeIsConst = true;
issue.sizeBytes = C->getZExtValue() * elemSize;
}
else if (auto* C = tryGetConstFromValue(arraySizeVal, F))
{
issue.sizeIsConst = true;
issue.sizeBytes = C->getZExtValue() * elemSize;
}
else if (auto upper = getAllocaUpperBoundBytes(AI, DL, ranges))
{
issue.hasUpperBound = true;
issue.upperBoundBytes = *upper;
}
out.push_back(std::move(issue));
}
}
}
} // namespace
std::vector<AllocaUsageIssue>
analyzeAllocaUsage(llvm::Module& mod, const llvm::DataLayout& DL,
const std::set<const llvm::Function*>& recursiveFuncs,
const std::set<const llvm::Function*>& infiniteRecursionFuncs,
const std::function<bool(const llvm::Function&)>& shouldAnalyze)
{
std::vector<AllocaUsageIssue> out;
for (llvm::Function& F : mod)
{
if (F.isDeclaration())
continue;
if (!shouldAnalyze(F))
continue;
bool isRec = recursiveFuncs.count(&F) != 0;
bool isInf = infiniteRecursionFuncs.count(&F) != 0;
analyzeAllocaUsageInFunction(F, DL, isRec, isInf, out);
}
return out;
}
} // namespace ctrace::stack::analysis