[libFuzzer] make trace-based fuzzing not crash in presence of threads
[oota-llvm.git] / lib / Fuzzer / test / ThreadedTest.cpp
1 // Threaded test for a fuzzer. The fuzzer should not crash.
2 #include <assert.h>
3 #include <cstdint>
4 #include <cstddef>
5 #include <cstring>
6 #include <thread>
7
8 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
9   if (Size < 8) return 0;
10   assert(Data);
11   auto C = [&] {
12     size_t Res = 0;
13     for (size_t i = 0; i < Size / 2; i++)
14       Res += memcmp(Data, Data + Size / 2, 4);
15     return Res;
16   };
17   std::thread T[] = {std::thread(C), std::thread(C), std::thread(C),
18                      std::thread(C), std::thread(C), std::thread(C)};
19   for (auto &X : T)
20     X.join();
21   return 0;
22 }
23