Skip to content

Commit 6bdd8fc

Browse files
committed
[libFuzzer] make sure we use the feedback from std::string operator ==
llvm-svn: 292835
1 parent 014d949 commit 6bdd8fc

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

llvm/lib/Fuzzer/FuzzerTracePC.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,12 @@ void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
214214
uint8_t B2[Word::kMaxSize];
215215
// Copy the data into locals in this non-msan-instrumented function
216216
// to avoid msan complaining further.
217+
size_t Hash = 0; // Compute some simple hash of both strings.
217218
for (size_t i = 0; i < Len; i++) {
218219
B1[i] = A1[i];
219220
B2[i] = A2[i];
221+
size_t T = B1[i];
222+
Hash ^= (T << 8) | B2[i];
220223
}
221224
size_t I = 0;
222225
for (; I < Len; I++)
@@ -225,7 +228,7 @@ void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
225228
size_t PC = reinterpret_cast<size_t>(caller_pc);
226229
size_t Idx = (PC & 4095) | (I << 12);
227230
TPC.HandleValueProfile(Idx);
228-
TORCW.Insert(Idx, Word(B1, Len), Word(B2, Len));
231+
TORCW.Insert(Idx ^ Hash, Word(B1, Len), Word(B2, Len));
229232
}
230233

231234
template <class T>

llvm/lib/Fuzzer/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ set(Tests
8383
CounterTest
8484
CustomCrossOverTest
8585
CustomMutatorTest
86+
CxxStringEqTest
8687
DivTest
8788
EmptyTest
8889
EquivalenceATest
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// This file is distributed under the University of Illinois Open Source
2+
// License. See LICENSE.TXT for details.
3+
4+
// Simple test for a fuzzer. Must find a specific string
5+
// used in std::string operator ==.
6+
#include <cstdint>
7+
#include <cstdlib>
8+
#include <cstddef>
9+
#include <string>
10+
#include <iostream>
11+
12+
static volatile int Sink;
13+
14+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
15+
std::string Str((const char*)Data, Size);
16+
bool Eq = Str == "FooBar";
17+
Sink = Str == "123456"; // Try to confuse the fuzzer
18+
if (Eq) {
19+
std::cout << "BINGO; Found the target, exiting\n";
20+
abort();
21+
}
22+
return 0;
23+
}
24+

llvm/lib/Fuzzer/test/cxxstring.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
RUN: not LLVMFuzzer-CxxStringEqTest -seed=1 -runs=1000000 2>&1 | FileCheck %s
2+
CHECK: BINGO

0 commit comments

Comments
 (0)