6e300aa44e1b894578a46cbeb31a731064d95255
[oota-llvm.git] / lib / Fuzzer / test / SwitchTest.cpp
1 // Simple test for a fuzzer. The fuzzer must find the interesting switch value.
2 #include <cstdint>
3 #include <cstdlib>
4 #include <cstring>
5 #include <cstddef>
6 #include <iostream>
7
8 static volatile int Sink;
9
10 template<class T>
11 bool Switch(const uint8_t *Data, size_t Size) {
12   T X;
13   if (Size < sizeof(X)) return false;
14   memcpy(&X, Data, sizeof(X));
15   switch (X) {
16     case 1: Sink = __LINE__; break;
17     case 101: Sink = __LINE__; break;
18     case 1001: Sink = __LINE__; break;
19     case 10001: Sink = __LINE__; break;
20     case 100001: Sink = __LINE__; break;
21     case 1000001: Sink = __LINE__; break;
22     case 10000001: Sink = __LINE__; break;
23     case 100000001: return true;
24   }
25   return false;
26 }
27
28 extern "C" void LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
29   if (Switch<int>(Data, Size) && Size >= 12 &&
30       Switch<uint64_t>(Data + 4, Size - 4)) {
31     std::cout << "BINGO; Found the target, exiting\n";
32     exit(1);
33   }
34 }
35