Skip to content

Commit 5317f2e

Browse files
committedMar 23, 2018
[libFuzzer] Use OptForFuzzing attribute with -fsanitize=fuzzer.
Summary: Disables certain CMP optimizations to improve fuzzing signal under -O1 and -O2. Switches all fuzzer tests to -O2 except for a few leak tests where the leak is optimized out under -O2. Reviewers: kcc, vitalybuka Reviewed By: vitalybuka Subscribers: cfe-commits, llvm-commits Differential Revision: https://reviews.llvm.org/D44798 llvm-svn: 328384
1 parent 953d843 commit 5317f2e

File tree

6 files changed

+19
-10
lines changed

6 files changed

+19
-10
lines changed
 

‎clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,10 @@ void CodeGenFunction::StartFunction(GlobalDecl GD,
862862
if (SanOpts.has(SanitizerKind::SafeStack))
863863
Fn->addFnAttr(llvm::Attribute::SafeStack);
864864

865+
// Apply fuzzing attribute to the function.
866+
if (SanOpts.hasOneOf(SanitizerKind::Fuzzer | SanitizerKind::FuzzerNoLink))
867+
Fn->addFnAttr(llvm::Attribute::OptForFuzzing);
868+
865869
// Ignore TSan memory acesses from within ObjC/ObjC++ dealloc, initialize,
866870
// .cxx_destruct, __destroy_helper_block_ and all of their calees at run time.
867871
if (SanOpts.has(SanitizerKind::Thread)) {

‎compiler-rt/test/fuzzer/SimpleCmpTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ bool PrintOnce(int Line) {
1717
}
1818

1919
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
20-
if (Size != 22) return 0;
20+
if (Size != 24) return 0;
2121
uint64_t x = 0;
2222
int64_t y = 0;
2323
int32_t z = 0;
24-
uint16_t a = 0;
24+
uint32_t a = 0;
2525
memcpy(&x, Data, 8); // 8
2626
memcpy(&y, Data + 8, 8); // 16
2727
memcpy(&z, Data + 16, sizeof(z)); // 20
28-
memcpy(&a, Data + 20, sizeof(a)); // 22
28+
memcpy(&a, Data + 20, sizeof(a)); // 24
2929
const bool k32bit = sizeof(void*) == 4;
3030

3131
if ((k32bit || x > 1234567890) && PrintOnce(__LINE__) &&

‎compiler-rt/test/fuzzer/SwapCmpTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
1111
if (Size < 14) return 0;
1212
uint64_t x = 0;
1313
uint32_t y = 0;
14-
uint16_t z = 0;
14+
uint32_t z = 0;
1515
memcpy(&x, Data, sizeof(x));
1616
memcpy(&y, Data + Size / 2, sizeof(y));
1717
memcpy(&z, Data + Size - sizeof(z), sizeof(z));
1818

1919
x = __builtin_bswap64(x);
2020
y = __builtin_bswap32(y);
21-
z = __builtin_bswap16(z);
21+
z = __builtin_bswap32(z);
2222
const bool k32bit = sizeof(void*) == 4;
2323

2424
if ((k32bit || x == 0x46555A5A5A5A5546ULL) &&
2525
z == 0x4F4B &&
2626
y == 0x66757A7A &&
2727
true
2828
) {
29-
if (Data[Size - 3] == 'z') {
29+
if (Data[Size - 5] == 'z') {
3030
fprintf(stderr, "BINGO; Found the target\n");
3131
exit(1);
3232
}

‎compiler-rt/test/fuzzer/fuzzer-leak.test

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
REQUIRES: lsan
2-
RUN: %cpp_compiler %S/LeakTest.cpp -o %t-LeakTest
3-
RUN: %cpp_compiler %S/ThreadedLeakTest.cpp -o %t-ThreadedLeakTest
2+
3+
// Avoid optimizing since it causes these leaks to go away.
4+
RUN: %cpp_compiler -O0 %S/LeakTest.cpp -o %t-LeakTest
5+
RUN: %cpp_compiler -O0 %S/ThreadedLeakTest.cpp -o %t-ThreadedLeakTest
6+
47
RUN: %cpp_compiler %S/LeakTimeoutTest.cpp -o %t-LeakTimeoutTest
58

69
RUN: rm -rf %t-corpus && mkdir -p %t-corpus

‎compiler-rt/test/fuzzer/lit.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def generate_compiler_cmd(is_cpp=True, fuzzer_enabled=True):
6464
sanitizers_cmd = ('-fsanitize=%s' % ','.join(sanitizers))
6565
isysroot_cmd = config.osx_sysroot_flag if config.osx_sysroot_flag else ''
6666
include_cmd = '-I%s' % libfuzzer_src_root
67-
return '%s %s %s -gline-tables-only %s %s %s' % (
67+
return '%s %s %s -O2 -gline-tables-only %s %s %s' % (
6868
compiler_cmd, std_cmd, link_cmd, isysroot_cmd, sanitizers_cmd, include_cmd)
6969

7070
config.substitutions.append(('%cpp_compiler',

‎compiler-rt/test/fuzzer/trace-malloc-threaded.test

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// printing a stack trace repeatedly
33
UNSUPPORTED: darwin
44

5-
RUN: %cpp_compiler %S/TraceMallocThreadedTest.cpp -o %t-TraceMallocThreadedTest
5+
// Avoid optimizing since it causes the malloc to go away.
6+
RUN: %cpp_compiler -O0 %S/TraceMallocThreadedTest.cpp -o \
7+
RUN: %t-TraceMallocThreadedTest
68

79
RUN: %t-TraceMallocThreadedTest -trace_malloc=2 -runs=1 2>&1 | FileCheck %s
810
CHECK: {{MALLOC\[[0-9]+] +0x[0-9]+ 5639}}

0 commit comments

Comments
 (0)
Please sign in to comment.