Skip to content

Commit e88a1ce

Browse files
authored
[ASan] Allow for passing AddressSanitizer command line options through the AddressSanitizerOptions struct. (#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 447da95 commit e88a1ce

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(
@@ -645,8 +644,9 @@ namespace {
645644
/// AddressSanitizer: instrument the code in module to find memory bugs.
646645
struct AddressSanitizer {
647646
AddressSanitizer(Module &M, const StackSafetyGlobalInfo *SSGI,
648-
bool CompileKernel = false, bool Recover = false,
649-
bool UseAfterScope = false,
647+
int InstrumentationWithCallsThreshold,
648+
uint32_t MaxInlinePoisoningSize, bool CompileKernel = false,
649+
bool Recover = false, bool UseAfterScope = false,
650650
AsanDetectStackUseAfterReturnMode UseAfterReturn =
651651
AsanDetectStackUseAfterReturnMode::Runtime)
652652
: CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
@@ -655,7 +655,14 @@ struct AddressSanitizer {
655655
UseAfterScope(UseAfterScope || ClUseAfterScope),
656656
UseAfterReturn(ClUseAfterReturn.getNumOccurrences() ? ClUseAfterReturn
657657
: UseAfterReturn),
658-
SSGI(SSGI) {
658+
SSGI(SSGI),
659+
InstrumentationWithCallsThreshold(
660+
ClInstrumentationWithCallsThreshold.getNumOccurrences() > 0
661+
? ClInstrumentationWithCallsThreshold
662+
: InstrumentationWithCallsThreshold),
663+
MaxInlinePoisoningSize(ClMaxInlinePoisoningSize.getNumOccurrences() > 0
664+
? ClMaxInlinePoisoningSize
665+
: MaxInlinePoisoningSize) {
659666
C = &(M.getContext());
660667
DL = &M.getDataLayout();
661668
LongSize = M.getDataLayout().getPointerSizeInBits();
@@ -774,17 +781,22 @@ struct AddressSanitizer {
774781

775782
FunctionCallee AMDGPUAddressShared;
776783
FunctionCallee AMDGPUAddressPrivate;
784+
int InstrumentationWithCallsThreshold;
785+
uint32_t MaxInlinePoisoningSize;
777786
};
778787

779788
class ModuleAddressSanitizer {
780789
public:
781-
ModuleAddressSanitizer(Module &M, bool CompileKernel = false,
782-
bool Recover = false, bool UseGlobalsGC = true,
783-
bool UseOdrIndicator = true,
790+
ModuleAddressSanitizer(Module &M, bool InsertVersionCheck,
791+
bool CompileKernel = false, bool Recover = false,
792+
bool UseGlobalsGC = true, bool UseOdrIndicator = true,
784793
AsanDtorKind DestructorKind = AsanDtorKind::Global,
785794
AsanCtorKind ConstructorKind = AsanCtorKind::Global)
786795
: CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
787796
: CompileKernel),
797+
InsertVersionCheck(ClInsertVersionCheck.getNumOccurrences() > 0
798+
? ClInsertVersionCheck
799+
: InsertVersionCheck),
788800
Recover(ClRecover.getNumOccurrences() > 0 ? ClRecover : Recover),
789801
UseGlobalsGC(UseGlobalsGC && ClUseGlobalsGC && !this->CompileKernel),
790802
// Enable aliases as they should have no downside with ODR indicators.
@@ -858,6 +870,7 @@ class ModuleAddressSanitizer {
858870
int GetAsanVersion(const Module &M) const;
859871

860872
bool CompileKernel;
873+
bool InsertVersionCheck;
861874
bool Recover;
862875
bool UseGlobalsGC;
863876
bool UsePrivateAlias;
@@ -1157,18 +1170,18 @@ AddressSanitizerPass::AddressSanitizerPass(
11571170

11581171
PreservedAnalyses AddressSanitizerPass::run(Module &M,
11591172
ModuleAnalysisManager &MAM) {
1160-
ModuleAddressSanitizer ModuleSanitizer(M, Options.CompileKernel,
1161-
Options.Recover, UseGlobalGC,
1162-
UseOdrIndicator, DestructorKind,
1163-
ConstructorKind);
1173+
ModuleAddressSanitizer ModuleSanitizer(
1174+
M, Options.InsertVersionCheck, Options.CompileKernel, Options.Recover,
1175+
UseGlobalGC, UseOdrIndicator, DestructorKind, ConstructorKind);
11641176
bool Modified = false;
11651177
auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
11661178
const StackSafetyGlobalInfo *const SSGI =
11671179
ClUseStackSafety ? &MAM.getResult<StackSafetyGlobalAnalysis>(M) : nullptr;
11681180
for (Function &F : M) {
1169-
AddressSanitizer FunctionSanitizer(M, SSGI, Options.CompileKernel,
1170-
Options.Recover, Options.UseAfterScope,
1171-
Options.UseAfterReturn);
1181+
AddressSanitizer FunctionSanitizer(
1182+
M, SSGI, Options.InstrumentationWithCallsThreshold,
1183+
Options.MaxInlinePoisoningSize, Options.CompileKernel, Options.Recover,
1184+
Options.UseAfterScope, Options.UseAfterReturn);
11721185
const TargetLibraryInfo &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
11731186
Modified |= FunctionSanitizer.instrumentFunction(F, &TLI);
11741187
}
@@ -2593,7 +2606,7 @@ bool ModuleAddressSanitizer::instrumentModule(Module &M) {
25932606
} else {
25942607
std::string AsanVersion = std::to_string(GetAsanVersion(M));
25952608
std::string VersionCheckName =
2596-
ClInsertVersionCheck ? (kAsanVersionCheckNamePrefix + AsanVersion) : "";
2609+
InsertVersionCheck ? (kAsanVersionCheckNamePrefix + AsanVersion) : "";
25972610
std::tie(AsanCtorFunction, std::ignore) =
25982611
createSanitizerCtorAndInitFunctions(M, kAsanModuleCtorName,
25992612
kAsanInitName, /*InitArgTypes=*/{},
@@ -2892,9 +2905,9 @@ bool AddressSanitizer::instrumentFunction(Function &F,
28922905
}
28932906
}
28942907

2895-
bool UseCalls = (ClInstrumentationWithCallsThreshold >= 0 &&
2908+
bool UseCalls = (InstrumentationWithCallsThreshold >= 0 &&
28962909
OperandsToInstrument.size() + IntrinToInstrument.size() >
2897-
(unsigned)ClInstrumentationWithCallsThreshold);
2910+
(unsigned)InstrumentationWithCallsThreshold);
28982911
const DataLayout &DL = F.getParent()->getDataLayout();
28992912
ObjectSizeOpts ObjSizeOpts;
29002913
ObjSizeOpts.RoundToAlign = true;
@@ -3068,7 +3081,7 @@ void FunctionStackPoisoner::copyToShadow(ArrayRef<uint8_t> ShadowMask,
30683081
for (; j < End && ShadowMask[j] && Val == ShadowBytes[j]; ++j) {
30693082
}
30703083

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

0 commit comments

Comments
 (0)