Skip to content

Commit 80b0a2a

Browse files
committed
[ASan] Allow for passing AddressSanitizer command line options through the AddressSanitizerOptions struct. (llvm#72439)
This patch adds the ability to pass values for the command line options of -max-inline-poisoning-size, -instrumentation-with-calls-threshold and -asan-guard-against-version-mismatch through the AddressSanitizerOptions struct. The motivation is to use these new options when using the pass in Swift. rdar://118470958
1 parent 1f241db commit 80b0a2a

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

llvm/include/llvm/Transforms/Instrumentation/AddressSanitizer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ struct AddressSanitizerOptions {
2626
bool UseAfterScope = false;
2727
AsanDetectStackUseAfterReturnMode UseAfterReturn =
2828
AsanDetectStackUseAfterReturnMode::Runtime;
29+
int InstrumentationWithCallsThreshold = 7000;
30+
uint32_t MaxInlinePoisoningSize = 64;
31+
bool InsertVersionCheck = true;
2932
};
3033

3134
/// Public interface to the address sanitizer module pass for instrumenting code

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ static cl::opt<bool> ClRecover(
201201

202202
static cl::opt<bool> ClInsertVersionCheck(
203203
"asan-guard-against-version-mismatch",
204-
cl::desc("Guard against compiler/runtime version mismatch."),
205-
cl::Hidden, cl::init(true));
204+
cl::desc("Guard against compiler/runtime version mismatch."), cl::Hidden,
205+
cl::init(true));
206206

207207
// This flag may need to be replaced with -f[no-]asan-reads.
208208
static cl::opt<bool> ClInstrumentReads("asan-instrument-reads",
@@ -323,10 +323,9 @@ static cl::opt<unsigned> ClRealignStack(
323323

324324
static cl::opt<int> ClInstrumentationWithCallsThreshold(
325325
"asan-instrumentation-with-call-threshold",
326-
cl::desc(
327-
"If the function being instrumented contains more than "
328-
"this number of memory accesses, use callbacks instead of "
329-
"inline checks (-1 means never use callbacks)."),
326+
cl::desc("If the function being instrumented contains more than "
327+
"this number of memory accesses, use callbacks instead of "
328+
"inline checks (-1 means never use callbacks)."),
330329
cl::Hidden, cl::init(7000));
331330

332331
static cl::opt<std::string> ClMemoryAccessCallbackPrefix(
@@ -644,8 +643,9 @@ namespace {
644643
/// AddressSanitizer: instrument the code in module to find memory bugs.
645644
struct AddressSanitizer {
646645
AddressSanitizer(Module &M, const StackSafetyGlobalInfo *SSGI,
647-
bool CompileKernel = false, bool Recover = false,
648-
bool UseAfterScope = false,
646+
int InstrumentationWithCallsThreshold,
647+
uint32_t MaxInlinePoisoningSize, bool CompileKernel = false,
648+
bool Recover = false, bool UseAfterScope = false,
649649
AsanDetectStackUseAfterReturnMode UseAfterReturn =
650650
AsanDetectStackUseAfterReturnMode::Runtime)
651651
: CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
@@ -654,7 +654,14 @@ struct AddressSanitizer {
654654
UseAfterScope(UseAfterScope || ClUseAfterScope),
655655
UseAfterReturn(ClUseAfterReturn.getNumOccurrences() ? ClUseAfterReturn
656656
: UseAfterReturn),
657-
SSGI(SSGI) {
657+
SSGI(SSGI),
658+
InstrumentationWithCallsThreshold(
659+
ClInstrumentationWithCallsThreshold.getNumOccurrences() > 0
660+
? ClInstrumentationWithCallsThreshold
661+
: InstrumentationWithCallsThreshold),
662+
MaxInlinePoisoningSize(ClMaxInlinePoisoningSize.getNumOccurrences() > 0
663+
? ClMaxInlinePoisoningSize
664+
: MaxInlinePoisoningSize) {
658665
C = &(M.getContext());
659666
DL = &M.getDataLayout();
660667
LongSize = M.getDataLayout().getPointerSizeInBits();
@@ -773,17 +780,22 @@ struct AddressSanitizer {
773780

774781
FunctionCallee AMDGPUAddressShared;
775782
FunctionCallee AMDGPUAddressPrivate;
783+
int InstrumentationWithCallsThreshold;
784+
uint32_t MaxInlinePoisoningSize;
776785
};
777786

778787
class ModuleAddressSanitizer {
779788
public:
780-
ModuleAddressSanitizer(Module &M, bool CompileKernel = false,
781-
bool Recover = false, bool UseGlobalsGC = true,
782-
bool UseOdrIndicator = false,
789+
ModuleAddressSanitizer(Module &M, bool InsertVersionCheck,
790+
bool CompileKernel = false, bool Recover = false,
791+
bool UseGlobalsGC = true, bool UseOdrIndicator = false,
783792
AsanDtorKind DestructorKind = AsanDtorKind::Global,
784793
AsanCtorKind ConstructorKind = AsanCtorKind::Global)
785794
: CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
786795
: CompileKernel),
796+
InsertVersionCheck(ClInsertVersionCheck.getNumOccurrences() > 0
797+
? ClInsertVersionCheck
798+
: InsertVersionCheck),
787799
Recover(ClRecover.getNumOccurrences() > 0 ? ClRecover : Recover),
788800
UseGlobalsGC(UseGlobalsGC && ClUseGlobalsGC && !this->CompileKernel),
789801
// Enable aliases as they should have no downside with ODR indicators.
@@ -852,6 +864,7 @@ class ModuleAddressSanitizer {
852864
int GetAsanVersion(const Module &M) const;
853865

854866
bool CompileKernel;
867+
bool InsertVersionCheck;
855868
bool Recover;
856869
bool UseGlobalsGC;
857870
bool UsePrivateAlias;
@@ -1150,18 +1163,18 @@ AddressSanitizerPass::AddressSanitizerPass(
11501163

11511164
PreservedAnalyses AddressSanitizerPass::run(Module &M,
11521165
ModuleAnalysisManager &MAM) {
1153-
ModuleAddressSanitizer ModuleSanitizer(M, Options.CompileKernel,
1154-
Options.Recover, UseGlobalGC,
1155-
UseOdrIndicator, DestructorKind,
1156-
ConstructorKind);
1166+
ModuleAddressSanitizer ModuleSanitizer(
1167+
M, Options.InsertVersionCheck, Options.CompileKernel, Options.Recover,
1168+
UseGlobalGC, UseOdrIndicator, DestructorKind, ConstructorKind);
11571169
bool Modified = false;
11581170
auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
11591171
const StackSafetyGlobalInfo *const SSGI =
11601172
ClUseStackSafety ? &MAM.getResult<StackSafetyGlobalAnalysis>(M) : nullptr;
11611173
for (Function &F : M) {
1162-
AddressSanitizer FunctionSanitizer(M, SSGI, Options.CompileKernel,
1163-
Options.Recover, Options.UseAfterScope,
1164-
Options.UseAfterReturn);
1174+
AddressSanitizer FunctionSanitizer(
1175+
M, SSGI, Options.InstrumentationWithCallsThreshold,
1176+
Options.MaxInlinePoisoningSize, Options.CompileKernel, Options.Recover,
1177+
Options.UseAfterScope, Options.UseAfterReturn);
11651178
const TargetLibraryInfo &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
11661179
Modified |= FunctionSanitizer.instrumentFunction(F, &TLI);
11671180
}
@@ -2591,7 +2604,7 @@ bool ModuleAddressSanitizer::instrumentModule(Module &M) {
25912604
} else {
25922605
std::string AsanVersion = std::to_string(GetAsanVersion(M));
25932606
std::string VersionCheckName =
2594-
ClInsertVersionCheck ? (kAsanVersionCheckNamePrefix + AsanVersion) : "";
2607+
InsertVersionCheck ? (kAsanVersionCheckNamePrefix + AsanVersion) : "";
25952608
std::tie(AsanCtorFunction, std::ignore) =
25962609
createSanitizerCtorAndInitFunctions(M, kAsanModuleCtorName,
25972610
kAsanInitName, /*InitArgTypes=*/{},
@@ -2893,9 +2906,9 @@ bool AddressSanitizer::instrumentFunction(Function &F,
28932906
}
28942907
}
28952908

2896-
bool UseCalls = (ClInstrumentationWithCallsThreshold >= 0 &&
2909+
bool UseCalls = (InstrumentationWithCallsThreshold >= 0 &&
28972910
OperandsToInstrument.size() + IntrinToInstrument.size() >
2898-
(unsigned)ClInstrumentationWithCallsThreshold);
2911+
(unsigned)InstrumentationWithCallsThreshold);
28992912
const DataLayout &DL = F.getParent()->getDataLayout();
29002913
ObjectSizeOpts ObjSizeOpts;
29012914
ObjSizeOpts.RoundToAlign = true;
@@ -3069,7 +3082,7 @@ void FunctionStackPoisoner::copyToShadow(ArrayRef<uint8_t> ShadowMask,
30693082
for (; j < End && ShadowMask[j] && Val == ShadowBytes[j]; ++j) {
30703083
}
30713084

3072-
if (j - i >= ClMaxInlinePoisoningSize) {
3085+
if (j - i >= ASan.MaxInlinePoisoningSize) {
30733086
copyToShadowInline(ShadowMask, ShadowBytes, Done, i, IRB, ShadowBase);
30743087
IRB.CreateCall(AsanSetShadowFunc[Val],
30753088
{IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i)),

0 commit comments

Comments
 (0)