Skip to content

Commit c8c3dcf

Browse files
committed
fixup! address reviewer comments round 2
Created using spr 1.3.8-beta.1
1 parent 0d4640a commit c8c3dcf

File tree

2 files changed

+24
-34
lines changed

2 files changed

+24
-34
lines changed

llvm/lib/Transforms/Instrumentation/AllocToken.cpp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,6 @@ cl::opt<bool> ClCoverReplaceableNew("alloc-token-cover-replaceable-new",
113113
cl::desc("Cover replaceable operator new"),
114114
cl::Hidden, cl::init(true));
115115

116-
// strdup-family functions only operate on strings, covering them does not make
117-
// sense in most cases.
118-
cl::opt<bool>
119-
ClCoverStrdup("alloc-token-cover-strdup",
120-
cl::desc("Cover strdup-family allocation functions"),
121-
cl::Hidden, cl::init(false));
122-
123116
cl::opt<uint64_t> ClFallbackToken(
124117
"alloc-token-fallback",
125118
cl::desc("The default fallback token where none could be determined"),
@@ -128,7 +121,7 @@ cl::opt<uint64_t> ClFallbackToken(
128121
//===--- Statistics -------------------------------------------------------===//
129122

130123
STATISTIC(NumFunctionsInstrumented, "Functions instrumented");
131-
STATISTIC(NumAllocations, "Allocations found");
124+
STATISTIC(NumAllocationsInstrumented, "Allocations instrumented");
132125

133126
//===----------------------------------------------------------------------===//
134127

@@ -194,12 +187,13 @@ class TypeHashMode : public ModeBase {
194187
MDString *S = cast<MDString>(N->getOperand(0));
195188
return boundedToken(xxHash64(S->getString()));
196189
}
197-
remarkNoHint(CB, ORE);
190+
remarkNoMetadata(CB, ORE);
198191
return ClFallbackToken;
199192
}
200193

201194
/// Remark that there was no precise type information.
202-
void remarkNoHint(const CallBase &CB, OptimizationRemarkEmitter &ORE) {
195+
static void remarkNoMetadata(const CallBase &CB,
196+
OptimizationRemarkEmitter &ORE) {
203197
ORE.emit([&] {
204198
ore::NV FuncNV("Function", CB.getParent()->getParent());
205199
const Function *Callee = CB.getCalledFunction();
@@ -212,12 +206,12 @@ class TypeHashMode : public ModeBase {
212206
};
213207

214208
// Apply opt overrides.
215-
AllocTokenOptions &&transformOptionsFromCl(AllocTokenOptions &&Opts) {
209+
AllocTokenOptions transformOptionsFromCl(AllocTokenOptions Opts) {
216210
if (!Opts.MaxTokens.has_value())
217211
Opts.MaxTokens = ClMaxTokens;
218212
Opts.FastABI |= ClFastABI;
219213
Opts.Extended |= ClExtended;
220-
return std::move(Opts);
214+
return Opts;
221215
}
222216

223217
class AllocToken {
@@ -252,7 +246,7 @@ class AllocToken {
252246

253247
/// Replace a call/invoke with a call/invoke to the allocation function
254248
/// with token ID.
255-
void replaceAllocationCall(CallBase *CB, LibFunc Func,
249+
bool replaceAllocationCall(CallBase *CB, LibFunc Func,
256250
OptimizationRemarkEmitter &ORE,
257251
const TargetLibraryInfo &TLI);
258252

@@ -314,15 +308,13 @@ bool AllocToken::instrumentFunction(Function &F) {
314308
}
315309
}
316310

317-
if (AllocCalls.empty())
318-
return false;
319-
311+
bool Modified = false;
320312
for (auto &[CB, Func] : AllocCalls)
321-
replaceAllocationCall(CB, Func, ORE, TLI);
322-
NumAllocations += AllocCalls.size();
323-
NumFunctionsInstrumented++;
313+
Modified |= replaceAllocationCall(CB, Func, ORE, TLI);
324314

325-
return true;
315+
if (Modified)
316+
NumFunctionsInstrumented++;
317+
return Modified;
326318
}
327319

328320
bool AllocToken::isInstrumentableLibFunc(LibFunc Func, const Value *V,
@@ -373,24 +365,26 @@ bool AllocToken::ignoreInstrumentableLibFunc(LibFunc Func) {
373365
case LibFunc_dunder_strdup:
374366
case LibFunc_strndup:
375367
case LibFunc_dunder_strndup:
376-
return !ClCoverStrdup;
368+
return true;
377369
default:
378370
return false;
379371
}
380372
}
381373

382-
void AllocToken::replaceAllocationCall(CallBase *CB, LibFunc Func,
374+
bool AllocToken::replaceAllocationCall(CallBase *CB, LibFunc Func,
383375
OptimizationRemarkEmitter &ORE,
384376
const TargetLibraryInfo &TLI) {
385377
uint64_t TokenID = getToken(*CB, ORE);
386378

387379
FunctionCallee TokenAlloc = getTokenAllocFunction(*CB, TokenID, Func);
388380
if (!TokenAlloc)
389-
return;
381+
return false;
382+
NumAllocationsInstrumented++;
383+
390384
if (Options.FastABI) {
391385
assert(TokenAlloc.getFunctionType()->getNumParams() == CB->arg_size());
392386
CB->setCalledFunction(TokenAlloc);
393-
return;
387+
return true;
394388
}
395389

396390
IRBuilder<> IRB(CB);
@@ -416,6 +410,7 @@ void AllocToken::replaceAllocationCall(CallBase *CB, LibFunc Func,
416410
// Replace all uses and delete the old call.
417411
CB->replaceAllUsesWith(NewCall);
418412
CB->eraseFromParent();
413+
return true;
419414
}
420415

421416
FunctionCallee AllocToken::getTokenAllocFunction(const CallBase &CB,
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
; Test for all allocation functions that should be ignored by default.
22
;
3-
; RUN: opt < %s -passes=inferattrs,alloc-token -S | FileCheck %s --check-prefixes=CHECK,DEFAULT
4-
; RUN: opt < %s -passes=inferattrs,alloc-token -alloc-token-cover-strdup -S | FileCheck %s --check-prefixes=CHECK,COVER
3+
; RUN: opt < %s -passes=inferattrs,alloc-token -S | FileCheck %s
54

65
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
76
target triple = "x86_64-unknown-linux-gnu"
@@ -14,17 +13,13 @@ declare ptr @__strndup(ptr, i64)
1413
; CHECK-LABEL: @test_ignorable_allocation_functions
1514
define ptr @test_ignorable_allocation_functions(ptr %ptr) sanitize_alloc_token {
1615
entry:
17-
; COVER: call ptr @__alloc_token_strdup(
18-
; DEFAULT: call ptr @strdup(
16+
; CHECK: call ptr @strdup(
1917
%ptr1 = call ptr @strdup(ptr %ptr)
20-
; COVER: call ptr @__alloc_token_strdup(
21-
; DEFAULT: call ptr @__strdup(
18+
; CHECK: call ptr @__strdup(
2219
%ptr2 = call ptr @__strdup(ptr %ptr)
23-
; COVER: call ptr @__alloc_token_strndup(
24-
; DEFAULT: call ptr @strndup(
20+
; CHECK: call ptr @strndup(
2521
%ptr3 = call ptr @strndup(ptr %ptr, i64 42)
26-
; COVER: call ptr @__alloc_token_strndup(
27-
; DEFAULT: call ptr @__strndup(
22+
; CHECK: call ptr @__strndup(
2823
%ptr4 = call ptr @__strndup(ptr %ptr, i64 42)
2924
ret ptr %ptr1
3025
}

0 commit comments

Comments
 (0)