Skip to content

Commit 12d1124

Browse files
committed
[libFuzzer] Disable implicit builtin knowledge about memcmp-like functions when -fsanitize=fuzzer-no-link is given.
Summary: This patch disables implicit builtin knowledge about memcmp-like functions when compiling the program for fuzzing, i.e., when -fsanitize=fuzzer(-no-link) is given. This allows libFuzzer to always intercept memcmp-like functions as it effectively disables optimizing calls to such functions into different forms. This is done by adding a set of flags (-fno-builtin-memcmp and others) in the clang driver. Individual -fno-builtin-* flags previously used in several libFuzzer tests are now removed, as it is now done automatically in the clang driver. Reviewers: morehouse, hctim Subscribers: cfe-commits, #sanitizers Tags: #clang, #sanitizers Differential Revision: https://reviews.llvm.org/D83987
1 parent 2f99059 commit 12d1124

File tree

6 files changed

+21
-5
lines changed

6 files changed

+21
-5
lines changed

clang/lib/Driver/SanitizerArgs.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,22 @@ void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
10881088
Sanitizers.has(SanitizerKind::Address))
10891089
CmdArgs.push_back("-fno-assume-sane-operator-new");
10901090

1091+
// libFuzzer wants to intercept calls to certain library functions, so the
1092+
// following -fno-builtin-* flags force the compiler to emit interposable
1093+
// libcalls to these functions. Other sanitizers effectively do the same thing
1094+
// by marking all library call sites with NoBuiltin attribute in their LLVM
1095+
// pass. (see llvm::maybeMarkSanitizerLibraryCallNoBuiltin)
1096+
if (Sanitizers.has(SanitizerKind::FuzzerNoLink)) {
1097+
CmdArgs.push_back("-fno-builtin-memcmp");
1098+
CmdArgs.push_back("-fno-builtin-strncmp");
1099+
CmdArgs.push_back("-fno-builtin-strcmp");
1100+
CmdArgs.push_back("-fno-builtin-strncasecmp");
1101+
CmdArgs.push_back("-fno-builtin-strcasecmp");
1102+
CmdArgs.push_back("-fno-builtin-strstr");
1103+
CmdArgs.push_back("-fno-builtin-strcasestr");
1104+
CmdArgs.push_back("-fno-builtin-memmem");
1105+
}
1106+
10911107
// Require -fvisibility= flag on non-Windows when compiling if vptr CFI is
10921108
// enabled.
10931109
if (Sanitizers.hasOneOf(CFIClasses) && !TC.getTriple().isOSWindows() &&

compiler-rt/test/fuzzer/memcmp.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ UNSUPPORTED: freebsd
22
RUN: %cpp_compiler %S/MemcmpTest.cpp -o %t-MemcmpTest
33
RUN: not %run %t-MemcmpTest -seed=1 -runs=10000000 2>&1 | FileCheck %s
44

5-
RUN: %cpp_compiler -fno-sanitize=address -fno-builtin-memcmp %S/MemcmpTest.cpp -o %t-NoAsanMemcmpTest
5+
RUN: %cpp_compiler -fno-sanitize=address %S/MemcmpTest.cpp -o %t-NoAsanMemcmpTest
66
RUN: not %run %t-MemcmpTest -seed=1 -runs=10000000 2>&1 | FileCheck %s
77

88
CHECK: BINGO

compiler-rt/test/fuzzer/memcmp64.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ UNSUPPORTED: freebsd
22
RUN: %cpp_compiler %S/Memcmp64BytesTest.cpp -o %t-Memcmp64BytesTest
33
RUN: not %run %t-Memcmp64BytesTest -seed=1 -runs=1000000 2>&1 | FileCheck %s
44

5-
RUN: %cpp_compiler -fno-sanitize=address -fno-builtin-memcmp %S/Memcmp64BytesTest.cpp -o %t-NoAsanMemcmp64BytesTest
5+
RUN: %cpp_compiler -fno-sanitize=address %S/Memcmp64BytesTest.cpp -o %t-NoAsanMemcmp64BytesTest
66
RUN: not %run %t-Memcmp64BytesTest -seed=1 -runs=1000000 2>&1 | FileCheck %s
77

88
CHECK: BINGO

compiler-rt/test/fuzzer/strcmp.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ UNSUPPORTED: freebsd
22
RUN: %cpp_compiler %S/StrcmpTest.cpp -o %t-StrcmpTest
33
RUN: not %run %t-StrcmpTest -seed=1 -runs=2000000 2>&1 | FileCheck %s
44

5-
RUN: %cpp_compiler -fno-sanitize=address -fno-builtin-strcmp %S/StrcmpTest.cpp -o %t-NoAsanStrcmpTest
5+
RUN: %cpp_compiler -fno-sanitize=address %S/StrcmpTest.cpp -o %t-NoAsanStrcmpTest
66
RUN: not %run %t-StrcmpTest -seed=1 -runs=2000000 2>&1 | FileCheck %s
77

88
CHECK: BINGO

compiler-rt/test/fuzzer/strncmp.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ UNSUPPORTED: freebsd
22
RUN: %cpp_compiler %S/StrncmpTest.cpp -o %t-StrncmpTest
33
RUN: not %run %t-StrncmpTest -seed=2 -runs=10000000 2>&1 | FileCheck %s
44

5-
RUN: %cpp_compiler -fno-sanitize=address -fno-builtin-strncmp %S/StrncmpTest.cpp -o %t-NoAsanStrncmpTest
5+
RUN: %cpp_compiler -fno-sanitize=address %S/StrncmpTest.cpp -o %t-NoAsanStrncmpTest
66
RUN: not %run %t-StrncmpTest -seed=2 -runs=10000000 2>&1 | FileCheck %s
77

88
CHECK: BINGO

compiler-rt/test/fuzzer/strstr.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ UNSUPPORTED: freebsd
22
RUN: %cpp_compiler %S/StrstrTest.cpp -o %t-StrstrTest
33
RUN: not %run %t-StrstrTest -seed=1 -runs=2000000 2>&1 | FileCheck %s
44

5-
RUN: %cpp_compiler -fno-sanitize=address -fno-builtin-strstr %S/StrstrTest.cpp -o %t-NoAsanStrstrTest
5+
RUN: %cpp_compiler -fno-sanitize=address %S/StrstrTest.cpp -o %t-NoAsanStrstrTest
66
RUN: not %run %t-StrstrTest -seed=1 -runs=2000000 2>&1 | FileCheck %s
77

88
CHECK: BINGO

0 commit comments

Comments
 (0)