Skip to content

[Cherrpick][ASan] Allow for passing AddressSanitizer command line options throug… #7844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ struct AddressSanitizerOptions {
bool UseAfterScope = false;
AsanDetectStackUseAfterReturnMode UseAfterReturn =
AsanDetectStackUseAfterReturnMode::Runtime;
int InstrumentationWithCallsThreshold = 7000;
uint32_t MaxInlinePoisoningSize = 64;
bool InsertVersionCheck = true;
};

/// Public interface to the address sanitizer module pass for instrumenting code
Expand Down
59 changes: 36 additions & 23 deletions llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ static cl::opt<bool> ClRecover(

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

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

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

static cl::opt<std::string> ClMemoryAccessCallbackPrefix(
Expand Down Expand Up @@ -644,8 +643,9 @@ namespace {
/// AddressSanitizer: instrument the code in module to find memory bugs.
struct AddressSanitizer {
AddressSanitizer(Module &M, const StackSafetyGlobalInfo *SSGI,
bool CompileKernel = false, bool Recover = false,
bool UseAfterScope = false,
int InstrumentationWithCallsThreshold,
uint32_t MaxInlinePoisoningSize, bool CompileKernel = false,
bool Recover = false, bool UseAfterScope = false,
AsanDetectStackUseAfterReturnMode UseAfterReturn =
AsanDetectStackUseAfterReturnMode::Runtime)
: CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
Expand All @@ -654,7 +654,14 @@ struct AddressSanitizer {
UseAfterScope(UseAfterScope || ClUseAfterScope),
UseAfterReturn(ClUseAfterReturn.getNumOccurrences() ? ClUseAfterReturn
: UseAfterReturn),
SSGI(SSGI) {
SSGI(SSGI),
InstrumentationWithCallsThreshold(
ClInstrumentationWithCallsThreshold.getNumOccurrences() > 0
? ClInstrumentationWithCallsThreshold
: InstrumentationWithCallsThreshold),
MaxInlinePoisoningSize(ClMaxInlinePoisoningSize.getNumOccurrences() > 0
? ClMaxInlinePoisoningSize
: MaxInlinePoisoningSize) {
C = &(M.getContext());
DL = &M.getDataLayout();
LongSize = M.getDataLayout().getPointerSizeInBits();
Expand Down Expand Up @@ -773,17 +780,22 @@ struct AddressSanitizer {

FunctionCallee AMDGPUAddressShared;
FunctionCallee AMDGPUAddressPrivate;
int InstrumentationWithCallsThreshold;
uint32_t MaxInlinePoisoningSize;
};

class ModuleAddressSanitizer {
public:
ModuleAddressSanitizer(Module &M, bool CompileKernel = false,
bool Recover = false, bool UseGlobalsGC = true,
bool UseOdrIndicator = false,
ModuleAddressSanitizer(Module &M, bool InsertVersionCheck,
bool CompileKernel = false, bool Recover = false,
bool UseGlobalsGC = true, bool UseOdrIndicator = false,
AsanDtorKind DestructorKind = AsanDtorKind::Global,
AsanCtorKind ConstructorKind = AsanCtorKind::Global)
: CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
: CompileKernel),
InsertVersionCheck(ClInsertVersionCheck.getNumOccurrences() > 0
? ClInsertVersionCheck
: InsertVersionCheck),
Recover(ClRecover.getNumOccurrences() > 0 ? ClRecover : Recover),
UseGlobalsGC(UseGlobalsGC && ClUseGlobalsGC && !this->CompileKernel),
// Enable aliases as they should have no downside with ODR indicators.
Expand Down Expand Up @@ -852,6 +864,7 @@ class ModuleAddressSanitizer {
int GetAsanVersion(const Module &M) const;

bool CompileKernel;
bool InsertVersionCheck;
bool Recover;
bool UseGlobalsGC;
bool UsePrivateAlias;
Expand Down Expand Up @@ -1150,18 +1163,18 @@ AddressSanitizerPass::AddressSanitizerPass(

PreservedAnalyses AddressSanitizerPass::run(Module &M,
ModuleAnalysisManager &MAM) {
ModuleAddressSanitizer ModuleSanitizer(M, Options.CompileKernel,
Options.Recover, UseGlobalGC,
UseOdrIndicator, DestructorKind,
ConstructorKind);
ModuleAddressSanitizer ModuleSanitizer(
M, Options.InsertVersionCheck, Options.CompileKernel, Options.Recover,
UseGlobalGC, UseOdrIndicator, DestructorKind, ConstructorKind);
bool Modified = false;
auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
const StackSafetyGlobalInfo *const SSGI =
ClUseStackSafety ? &MAM.getResult<StackSafetyGlobalAnalysis>(M) : nullptr;
for (Function &F : M) {
AddressSanitizer FunctionSanitizer(M, SSGI, Options.CompileKernel,
Options.Recover, Options.UseAfterScope,
Options.UseAfterReturn);
AddressSanitizer FunctionSanitizer(
M, SSGI, Options.InstrumentationWithCallsThreshold,
Options.MaxInlinePoisoningSize, Options.CompileKernel, Options.Recover,
Options.UseAfterScope, Options.UseAfterReturn);
const TargetLibraryInfo &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
Modified |= FunctionSanitizer.instrumentFunction(F, &TLI);
}
Expand Down Expand Up @@ -2591,7 +2604,7 @@ bool ModuleAddressSanitizer::instrumentModule(Module &M) {
} else {
std::string AsanVersion = std::to_string(GetAsanVersion(M));
std::string VersionCheckName =
ClInsertVersionCheck ? (kAsanVersionCheckNamePrefix + AsanVersion) : "";
InsertVersionCheck ? (kAsanVersionCheckNamePrefix + AsanVersion) : "";
std::tie(AsanCtorFunction, std::ignore) =
createSanitizerCtorAndInitFunctions(M, kAsanModuleCtorName,
kAsanInitName, /*InitArgTypes=*/{},
Expand Down Expand Up @@ -2893,9 +2906,9 @@ bool AddressSanitizer::instrumentFunction(Function &F,
}
}

bool UseCalls = (ClInstrumentationWithCallsThreshold >= 0 &&
bool UseCalls = (InstrumentationWithCallsThreshold >= 0 &&
OperandsToInstrument.size() + IntrinToInstrument.size() >
(unsigned)ClInstrumentationWithCallsThreshold);
(unsigned)InstrumentationWithCallsThreshold);
const DataLayout &DL = F.getParent()->getDataLayout();
ObjectSizeOpts ObjSizeOpts;
ObjSizeOpts.RoundToAlign = true;
Expand Down Expand Up @@ -3069,7 +3082,7 @@ void FunctionStackPoisoner::copyToShadow(ArrayRef<uint8_t> ShadowMask,
for (; j < End && ShadowMask[j] && Val == ShadowBytes[j]; ++j) {
}

if (j - i >= ClMaxInlinePoisoningSize) {
if (j - i >= ASan.MaxInlinePoisoningSize) {
copyToShadowInline(ShadowMask, ShadowBytes, Done, i, IRB, ShadowBase);
IRB.CreateCall(AsanSetShadowFunc[Val],
{IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i)),
Expand Down