Skip to content

Commit 7ce310a

Browse files
committed
[BOLT] Fix runOnEachFunctionWithUniqueAllocId
When runOnEachFunctionWithUniqueAllocId is invoked with ForceSequential=true, then the current implementation runs the function with AllocId==0, which is the Id for the shared, non-unique, default AnnotationAllocator. However, the documentation for runOnEachFunctionWithUniqueAllocId states: /// Perform the work on each BinaryFunction except those that are rejected /// by SkipPredicate, and create a unique annotation allocator for each /// task. This should be used whenever the work function creates annotations to /// allow thread-safe annotation creation. Therefore, even when ForceSequential==true, a unique AllocId should be used, i.e. different from 0. In the current upstream BOLT this is presumably not depended on, but it is needed to reduce memory usage for analyses that use a lot of memory/annotations. Examples are the pac-ret and stack-clash analyses that currently have prototype implementations as described in https://discourse.llvm.org/t/rfc-bolt-based-binary-analysis-tool-to-verify-correctness-of-security-hardening/78148 These analyses use the DataFlowAnalysis framework to sometimes store quite a lot of information on each MCInst. They run in parallel on each function. When the dataflow analysis is finished, the annotations on each MCInst can be removed, hugely saving on memory consumption. The only annotations that need to remain are those that indicate some unexpected properties somewhere in the binary. Fixing this bug enables implementing the deletion of the memory used by those huge number of DataFlowAnalysis annotations (by invoking BC.MIB->freeValuesAllocator(AllocatorId)), even when run with --no-threads. Without this bug fixed, the invocation of BC.MIB->freeValuesAllocator(AllocatorId) results in also the memory for all other annotations to be deleted, as AllocatorId is 0.
1 parent f78949a commit 7ce310a

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

bolt/lib/Core/ParallelUtilities.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ void runOnEachFunction(BinaryContext &BC, SchedulingPolicy SchedPolicy,
164164
Pool.wait();
165165
}
166166

167+
static void EnsureAllocatorExists(BinaryContext& BC, unsigned AllocId) {
168+
if (!BC.MIB->checkAllocatorExists(AllocId)) {
169+
MCPlusBuilder::AllocatorIdTy Id =
170+
BC.MIB->initializeNewAnnotationAllocator();
171+
(void)Id;
172+
assert(AllocId == Id && "unexpected allocator id created");
173+
}
174+
}
175+
167176
void runOnEachFunctionWithUniqueAllocId(
168177
BinaryContext &BC, SchedulingPolicy SchedPolicy,
169178
WorkFuncWithAllocTy WorkFunction, PredicateTy SkipPredicate,
@@ -188,8 +197,12 @@ void runOnEachFunctionWithUniqueAllocId(
188197
LLVM_DEBUG(T.stopTimer());
189198
};
190199

200+
unsigned AllocId = 1;
201+
191202
if (opts::NoThreads || ForceSequential) {
192-
runBlock(BC.getBinaryFunctions().begin(), BC.getBinaryFunctions().end(), 0);
203+
EnsureAllocatorExists(BC, AllocId);
204+
runBlock(BC.getBinaryFunctions().begin(), BC.getBinaryFunctions().end(),
205+
AllocId);
193206
return;
194207
}
195208
// This lock is used to postpone task execution
@@ -205,19 +218,13 @@ void runOnEachFunctionWithUniqueAllocId(
205218
ThreadPoolInterface &Pool = getThreadPool();
206219
auto BlockBegin = BC.getBinaryFunctions().begin();
207220
unsigned CurrentCost = 0;
208-
unsigned AllocId = 1;
209221
for (auto It = BC.getBinaryFunctions().begin();
210222
It != BC.getBinaryFunctions().end(); ++It) {
211223
BinaryFunction &BF = It->second;
212224
CurrentCost += computeCostFor(BF, SkipPredicate, SchedPolicy);
213225

214226
if (CurrentCost >= BlockCost) {
215-
if (!BC.MIB->checkAllocatorExists(AllocId)) {
216-
MCPlusBuilder::AllocatorIdTy Id =
217-
BC.MIB->initializeNewAnnotationAllocator();
218-
(void)Id;
219-
assert(AllocId == Id && "unexpected allocator id created");
220-
}
227+
EnsureAllocatorExists(BC, AllocId);
221228
Pool.async(runBlock, BlockBegin, std::next(It), AllocId);
222229
AllocId++;
223230
BlockBegin = std::next(It);

0 commit comments

Comments
 (0)