Skip to content

Commit 256c2e1

Browse files
committed
[esan] EfficiencySanitizer driver flags
Summary: Adds a framework to enable the instrumentation pass for the new EfficiencySanitizer ("esan") family of tools. Adds a flag for esan's cache fragmentation tool via -fsanitize=efficiency-cache-frag. Adds appropriate tests for the new flag. Reviewers: eugenis, vitalybuka, aizatsky, filcab Subscribers: filcab, kubabrecka, llvm-commits, zhaoqin, kcc Differential Revision: http://reviews.llvm.org/D19169 llvm-svn: 267059
1 parent d862c17 commit 256c2e1

File tree

12 files changed

+109
-1
lines changed

12 files changed

+109
-1
lines changed

clang/include/clang/Basic/Sanitizers.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ SANITIZER_GROUP("integer", Integer,
114114
SANITIZER("local-bounds", LocalBounds)
115115
SANITIZER_GROUP("bounds", Bounds, ArrayBounds | LocalBounds)
116116

117+
// EfficiencySanitizer
118+
SANITIZER("efficiency-cache-frag", EfficiencyCacheFrag)
119+
// Meta-group only used internally.
120+
SANITIZER_GROUP("efficiency-all", Efficiency, EfficiencyCacheFrag)
121+
117122
// Magic group, containing all sanitizers. For example, "-fno-sanitize=all"
118123
// can be used to disable all the sanitizers.
119124
SANITIZER_GROUP("all", All, ~0ULL)

clang/include/clang/Driver/SanitizerArgs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class SanitizerArgs {
5858
bool needsCfiRt() const;
5959
bool needsCfiDiagRt() const;
6060
bool needsStatsRt() const { return Stats; }
61+
bool needsEsanRt() const {
62+
return Sanitizers.hasOneOf(SanitizerKind::Efficiency);
63+
}
6164

6265
bool requiresPIE() const;
6366
bool needsUnwindTables() const;

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,17 @@ static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder,
248248
PM.add(createDataFlowSanitizerPass(LangOpts.SanitizerBlacklistFiles));
249249
}
250250

251+
static void addEfficiencySanitizerPass(const PassManagerBuilder &Builder,
252+
legacy::PassManagerBase &PM) {
253+
const PassManagerBuilderWrapper &BuilderWrapper =
254+
static_cast<const PassManagerBuilderWrapper&>(Builder);
255+
const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
256+
EfficiencySanitizerOptions Opts;
257+
if (LangOpts.Sanitize.has(SanitizerKind::EfficiencyCacheFrag))
258+
Opts.ToolType = EfficiencySanitizerOptions::ESAN_CacheFrag;
259+
PM.add(createEfficiencySanitizerPass(Opts));
260+
}
261+
251262
static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
252263
const CodeGenOptions &CodeGenOpts) {
253264
TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple);
@@ -407,6 +418,13 @@ void EmitAssemblyHelper::CreatePasses(ModuleSummaryIndex *ModuleSummary) {
407418
addDataFlowSanitizerPass);
408419
}
409420

421+
if (LangOpts.Sanitize.hasOneOf(SanitizerKind::Efficiency)) {
422+
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
423+
addEfficiencySanitizerPass);
424+
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
425+
addEfficiencySanitizerPass);
426+
}
427+
410428
// Set up the per-function pass manager.
411429
legacy::FunctionPassManager *FPM = getPerFunctionPasses();
412430
if (CodeGenOpts.VerifyModule)

clang/lib/Driver/SanitizerArgs.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,12 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
311311
std::make_pair(Leak, Memory), std::make_pair(KernelAddress, Address),
312312
std::make_pair(KernelAddress, Leak),
313313
std::make_pair(KernelAddress, Thread),
314-
std::make_pair(KernelAddress, Memory)};
314+
std::make_pair(KernelAddress, Memory),
315+
std::make_pair(Efficiency, Address),
316+
std::make_pair(Efficiency, Leak),
317+
std::make_pair(Efficiency, Thread),
318+
std::make_pair(Efficiency, Memory),
319+
std::make_pair(Efficiency, KernelAddress)};
315320
for (auto G : IncompatibleGroups) {
316321
SanitizerMask Group = G.first;
317322
if (Kinds & Group) {
@@ -695,6 +700,10 @@ SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A,
695700
if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
696701
0 == strcmp("all", Value))
697702
Kind = 0;
703+
// Similarly, don't accept -fsanitize=efficiency-all.
704+
else if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
705+
0 == strcmp("efficiency-all", Value))
706+
Kind = 0;
698707
else
699708
Kind = parseSanitizerValue(Value, /*AllowGroups=*/true);
700709

clang/lib/Driver/ToolChains.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
422422
/*AlwaysLink=*/true);
423423
AddLinkSanitizerLibArgs(Args, CmdArgs, "stats");
424424
}
425+
if (Sanitize.needsEsanRt())
426+
AddLinkSanitizerLibArgs(Args, CmdArgs, "esan");
425427

426428
// Otherwise link libSystem, then the dynamic runtime library, and finally any
427429
// target specific static runtime library.
@@ -4152,6 +4154,8 @@ SanitizerMask Linux::getSupportedSanitizers() const {
41524154
Res |= SanitizerKind::Thread;
41534155
if (IsX86_64 || IsMIPS64 || IsPowerPC64 || IsAArch64)
41544156
Res |= SanitizerKind::Memory;
4157+
if (IsX86_64)
4158+
Res |= SanitizerKind::Efficiency;
41554159
if (IsX86 || IsX86_64) {
41564160
Res |= SanitizerKind::Function;
41574161
}

clang/lib/Driver/Tools.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3013,6 +3013,8 @@ collectSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
30133013
NonWholeStaticRuntimes.push_back("stats");
30143014
RequiredSymbols.push_back("__sanitizer_stats_register");
30153015
}
3016+
if (SanArgs.needsEsanRt())
3017+
StaticRuntimes.push_back("esan");
30163018
}
30173019

30183020
// Should be called before we add system libraries (C++ ABI, libstdc++/libc++,

clang/lib/Lex/PPMacroExpansion.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,8 @@ static bool HasFeature(const Preprocessor &PP, StringRef Feature) {
10931093
.Case("memory_sanitizer", LangOpts.Sanitize.has(SanitizerKind::Memory))
10941094
.Case("thread_sanitizer", LangOpts.Sanitize.has(SanitizerKind::Thread))
10951095
.Case("dataflow_sanitizer", LangOpts.Sanitize.has(SanitizerKind::DataFlow))
1096+
.Case("efficiency_sanitizer",
1097+
LangOpts.Sanitize.hasOneOf(SanitizerKind::Efficiency))
10961098
// Objective-C features
10971099
.Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE?
10981100
.Case("objc_arc", LangOpts.ObjCAutoRefCount)

clang/test/Driver/esan.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %clang -target x86_64-unknown-linux -fsanitize=efficiency-cache-frag %s -S -emit-llvm -o - | FileCheck %s
2+
// RUN: %clang -O1 -target x86_64-unknown-linux -fsanitize=efficiency-cache-frag %s -S -emit-llvm -o - | FileCheck %s
3+
// RUN: %clang -O2 -target x86_64-unknown-linux -fsanitize=efficiency-cache-frag %s -S -emit-llvm -o - | FileCheck %s
4+
// RUN: %clang -O3 -target x86_64-unknown-linux -fsanitize=efficiency-cache-frag %s -S -emit-llvm -o - | FileCheck %s
5+
// RUN: %clang -target x86_64-unknown-linux -fsanitize=efficiency-cache-frag %s -S -emit-llvm -o - | FileCheck %s
6+
// Verify that -fsanitize=efficiency-cache-frag invokes esan instrumentation.
7+
8+
int foo(int *a) { return *a; }
9+
// CHECK: __esan_init

clang/test/Driver/fsanitize.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@
8383
// RUN: %clang -target x86_64-linux-gnu -fsanitize=kernel-address,leak -pie -fno-rtti %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SANKA-SANL
8484
// CHECK-SANKA-SANL: '-fsanitize=kernel-address' not allowed with '-fsanitize=leak'
8585

86+
// RUN: %clang -target x86_64-linux-gnu -fsanitize=efficiency-cache-frag,address -pie -fno-rtti %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SANE-SANA
87+
// CHECK-SANE-SANA: '-fsanitize=efficiency-cache-frag' not allowed with '-fsanitize=address'
88+
89+
// RUN: %clang -target x86_64-linux-gnu -fsanitize=efficiency-cache-frag,leak -pie -fno-rtti %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SANE-SANL
90+
// CHECK-SANE-SANL: '-fsanitize=efficiency-cache-frag' not allowed with '-fsanitize=leak'
91+
92+
// RUN: %clang -target x86_64-linux-gnu -fsanitize=efficiency-cache-frag,thread -pie -fno-rtti %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SANE-SANT
93+
// CHECK-SANE-SANT: '-fsanitize=efficiency-cache-frag' not allowed with '-fsanitize=thread'
94+
95+
// RUN: %clang -target x86_64-linux-gnu -fsanitize=efficiency-cache-frag,memory -pie -fno-rtti %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SANE-SANM
96+
// CHECK-SANE-SANM: '-fsanitize=efficiency-cache-frag' not allowed with '-fsanitize=memory'
97+
98+
// RUN: %clang -target x86_64-linux-gnu -fsanitize=efficiency-cache-frag,kernel-address -pie -fno-rtti %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SANE-SANKA
99+
// CHECK-SANE-SANKA: '-fsanitize=efficiency-cache-frag' not allowed with '-fsanitize=kernel-address'
100+
86101
// RUN: %clang -target x86_64-linux-gnu -fsanitize-memory-track-origins -pie %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ONLY-TRACK-ORIGINS
87102
// CHECK-ONLY-TRACK-ORIGINS: warning: argument unused during compilation: '-fsanitize-memory-track-origins'
88103

@@ -256,6 +271,26 @@
256271
// RUN: %clang -target i386-pc-openbsd -fsanitize=address %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ASAN-OPENBSD
257272
// CHECK-ASAN-OPENBSD: unsupported option '-fsanitize=address' for target 'i386-pc-openbsd'
258273

274+
// RUN: %clang -target i686-linux-gnu -fsanitize=efficiency-cache-frag %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ESAN-X86
275+
// CHECK-ESAN-X86: error: unsupported option '-fsanitize=efficiency-cache-frag' for target 'i686--linux-gnu'
276+
277+
// RUN: %clang -target x86_64-apple-darwin10 -fsanitize=efficiency-cache-frag %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ESAN-DARWIN
278+
// CHECK-ESAN-DARWIN: unsupported option '-fsanitize=efficiency-cache-frag' for target 'x86_64-apple-darwin10'
279+
280+
// RUN: %clang -target i386-apple-darwin -fsanitize=efficiency-cache-frag %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ESAN-I386-DARWIN
281+
// CHECK-ESAN-I386-DARWIN: unsupported option '-fsanitize=efficiency-cache-frag' for target 'i386-apple-darwin'
282+
283+
// RUN: %clang -target arm-apple-ios -fsanitize=efficiency-cache-frag %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ESAN-ARM-IOS
284+
// CHECK-ESAN-ARM-IOS: unsupported option '-fsanitize=efficiency-cache-frag' for target 'arm-apple-ios'
285+
286+
// RUN: %clang -target i386-apple-iossimulator -fsanitize=efficiency-cache-frag %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ESAN-I386-IOSSIMULATOR
287+
// CHECK-ESAN-I386-IOSSIMULATOR: unsupported option '-fsanitize=efficiency-cache-frag' for target 'i386-apple-iossimulator'
288+
289+
// RUN: %clang -target i386-apple-tvossimulator -fsanitize=efficiency-cache-frag %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ESAN-I386-TVOSSIMULATOR
290+
// CHECK-ESAN-I386-TVOSSIMULATOR: unsupported option '-fsanitize=efficiency-cache-frag' for target 'i386-apple-tvossimulator'
291+
292+
293+
259294
// RUN: %clang -target x86_64-linux-gnu -fsanitize=cfi -flto -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-CFI
260295
// RUN: %clang -target x86_64-apple-darwin10 -fsanitize=cfi -flto -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-CFI
261296
// RUN: %clang -target x86_64-linux-gnu -fsanitize=cfi-derived-cast -flto -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-CFI-DCAST
@@ -353,6 +388,8 @@
353388
// CHECK-MSAN-PS4: unsupported option '-fsanitize=memory' for target 'x86_64-scei-ps4'
354389
// RUN: %clang -target x86_64-scei-ps4 -fsanitize=thread %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-TSAN-PS4
355390
// CHECK-TSAN-PS4: unsupported option '-fsanitize=thread' for target 'x86_64-scei-ps4'
391+
// RUN: %clang -target x86_64-scei-ps4 -fsanitize=efficiency-cache-frag %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ESAN-PS4
392+
// CHECK-ESAN-PS4: unsupported option '-fsanitize=efficiency-cache-frag' for target 'x86_64-scei-ps4'
356393
// RUN: %clang -target x86_64-scei-ps4 -fsanitize=address %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-ASAN-PS4
357394
// Make sure there are no *.{o,bc} or -l passed before the ASan library.
358395
// CHECK-ASAN-PS4-NOT: {{(\.(o|bc)"? |-l).*-lSceDbgAddressSanitizer_stub_weak}}

clang/test/Driver/sanitize_unwind_tables.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
// RUN: %clang -target x86_64-linux-gnu -fsanitize=memory %s -### 2>&1 | FileCheck %s
88
// RUN: %clang -target x86_64-linux-gnu -fsanitize=thread %s -### 2>&1 | FileCheck %s
99
// RUN: %clang -target x86_64-linux-gnu -fsanitize=dataflow %s -### 2>&1 | FileCheck %s
10+
// RUN: %clang -target x86_64-linux-gnu -fsanitize=efficiency-cache-frag %s -### 2>&1 | FileCheck %s
1011

1112
// CHECK: -munwind-tables

clang/test/Driver/sanitizer-ld.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,3 +424,10 @@
424424
// RUN: | FileCheck --check-prefix=CHECK-AUBSAN-PS4 %s
425425
// CHECK-AUBSAN-PS4: "{{.*}}ld{{(.gold)?(.exe)?}}"
426426
// CHECK-AUBSAN-PS4: -lSceDbgAddressSanitizer_stub_weak
427+
428+
// RUN: %clang -fsanitize=efficiency-cache-frag %s -### -o %t.o 2>&1 \
429+
// RUN: -target x86_64-unknown-linux \
430+
// RUN: | FileCheck --check-prefix=CHECK-ESAN-LINUX %s
431+
//
432+
// CHECK-ESAN-LINUX: "{{(.*[^-.0-9A-Z_a-z])?}}ld{{(.exe)?}}"
433+
// CHECK-ESAN-LINUX: libclang_rt.esan-x86_64.a
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_cc1 -E -fsanitize=efficiency-cache-frag %s -o - | FileCheck --check-prefix=CHECK-ESAN %s
2+
// RUN: %clang_cc1 -E %s -o - | FileCheck --check-prefix=CHECK-NO-ESAN %s
3+
4+
#if __has_feature(efficiency_sanitizer)
5+
int EfficiencySanitizerEnabled();
6+
#else
7+
int EfficiencySanitizerDisabled();
8+
#endif
9+
10+
// CHECK-ESAN: EfficiencySanitizerEnabled
11+
// CHECK-NO-ESAN: EfficiencySanitizerDisabled

0 commit comments

Comments
 (0)