Skip to content

Commit f7ffb12

Browse files
committed
[libFuzzer] Instrument bcmp
If we define memcmp in an archive, bcmp should be defined as well (many libc define bcmp/memcmp in one object file). Otherwise if the application calls bcmp or strcmp which gets optimized to bcmp (SimplifyLibCalls), the undefined reference may pull in an optimized bcmp/strcmp implementation (libc replacement) later on the linker command line. If both libFuzzer's memcmp and the optimized memcmp are strong => there will be a multiple definition error.
1 parent 99d03f0 commit f7ffb12

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

‎compiler-rt/lib/fuzzer/FuzzerInterceptors.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ static char *internal_strstr(const char *haystack, const char *needle) {
119119

120120
extern "C" {
121121

122+
DEFINE_REAL(int, bcmp, const void *, const void *, size_t)
122123
DEFINE_REAL(int, memcmp, const void *, const void *, size_t)
123124
DEFINE_REAL(int, strncmp, const char *, const char *, size_t)
124125
DEFINE_REAL(int, strcmp, const char *, const char *)
@@ -128,6 +129,14 @@ DEFINE_REAL(char *, strstr, const char *, const char *)
128129
DEFINE_REAL(char *, strcasestr, const char *, const char *)
129130
DEFINE_REAL(void *, memmem, const void *, size_t, const void *, size_t)
130131

132+
ATTRIBUTE_INTERFACE int bcmp(const char *s1, const char *s2, size_t n) {
133+
if (!FuzzerInited)
134+
return internal_memcmp(s1, s2, n);
135+
int result = REAL(bcmp)(s1, s2, n);
136+
__sanitizer_weak_hook_memcmp(GET_CALLER_PC(), s1, s2, n, result);
137+
return result;
138+
}
139+
131140
ATTRIBUTE_INTERFACE int memcmp(const void *s1, const void *s2, size_t n) {
132141
if (!FuzzerInited)
133142
return internal_memcmp(s1, s2, n);
@@ -200,6 +209,8 @@ static void fuzzerInit() {
200209
return;
201210
FuzzerInitIsRunning = true;
202211

212+
REAL(bcmp) = reinterpret_cast<memcmp_type>(
213+
getFuncAddr("bcmp", reinterpret_cast<uintptr_t>(&bcmp)));
203214
REAL(memcmp) = reinterpret_cast<memcmp_type>(
204215
getFuncAddr("memcmp", reinterpret_cast<uintptr_t>(&memcmp)));
205216
REAL(strncmp) = reinterpret_cast<strncmp_type>(

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88
#include <cstdlib>
99
#include <cstring>
1010

11+
#ifndef MEMCMP
12+
# define MEMCMP memcmp
13+
#endif
14+
1115
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
1216
// TODO: check other sizes.
13-
if (Size >= 8 && memcmp(Data, "01234567", 8) == 0) {
14-
if (Size >= 12 && memcmp(Data + 8, "ABCD", 4) == 0) {
15-
if (Size >= 14 && memcmp(Data + 12, "XY", 2) == 0) {
16-
if (Size >= 17 && memcmp(Data + 14, "KLM", 3) == 0) {
17-
if (Size >= 27 && memcmp(Data + 17, "ABCDE-GHIJ", 10) == 0){
17+
if (Size >= 8 && MEMCMP(Data, "01234567", 8) == 0) {
18+
if (Size >= 12 && MEMCMP(Data + 8, "ABCD", 4) == 0) {
19+
if (Size >= 14 && MEMCMP(Data + 12, "XY", 2) == 0) {
20+
if (Size >= 17 && MEMCMP(Data + 14, "KLM", 3) == 0) {
21+
if (Size >= 27 && MEMCMP(Data + 17, "ABCDE-GHIJ", 10) == 0){
1822
fprintf(stderr, "BINGO %zd\n", Size);
1923
for (size_t i = 0; i < Size; i++) {
2024
uint8_t C = Data[i];

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
UNSUPPORTED: freebsd
2+
RUN: %cpp_compiler -DMEMCMP=bcmp %S/MemcmpTest.cpp -o %t
3+
RUN: not %run %t -seed=1 -runs=10000000 2>&1 | FileCheck %s
4+
CHECK: BINGO

0 commit comments

Comments
 (0)