• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef __FUZZ_CMN_H__
2 #define __FUZZ_CMN_H__
3 
4 #include <base/command_line.h>
5 #include <base/logging.h>
6 #include <errno.h>
7 #include <semaphore.h>
8 
9 #include <nfc_api.h>
10 #include <nfc_int.h>
11 
12 #include <map>
13 #include <vector>
14 
15 #include <android-base/stringprintf.h>
16 using android::base::StringAppendF;
17 using android::base::StringPrintf;
18 
19 #define FUZZLOG(...)               \
20   DLOG_IF(INFO, nfc_debug_enabled) \
21       << __func__ << ":" << StringPrintf(__VA_ARGS__);
22 
23 extern bool nfc_debug_enabled;
24 
25 typedef std::vector<uint8_t> bytes_t;
26 
27 std::string BytesToHex(const uint8_t* data, size_t size);
28 std::string BytesToHex(const bytes_t& data);
29 bytes_t FuzzSeqGen(size_t minLen, size_t maxLen);
30 
31 extern void GKI_shutdown();
32 
33 extern "C" int LLVMFuzzerInitialize(int*, char***);
34 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size);
35 extern "C" size_t LLVMFuzzerMutate(uint8_t* Data, size_t Size, size_t MaxSize);
36 
37 extern "C" void __gcov_flush(void) __attribute__((weak));
38 
39 class Fuzz_Context {
40   std::vector<std::unique_ptr<uint8_t[]>> _ScratchPtrs;
41 
42  public:
43   uint8_t SubType;
44   const std::vector<bytes_t> Data;
45 
46  public:
Fuzz_Context(uint8_t FuzzSubType,const std::vector<bytes_t> & Packets)47   Fuzz_Context(uint8_t FuzzSubType, const std::vector<bytes_t>& Packets)
48       : SubType(FuzzSubType), Data(Packets) {}
49 
50   uint8_t* GetBuffer(size_t size, const void* init_data = nullptr) {
51     auto ptr = std::make_unique<uint8_t[]>(size);
52     uint8_t* p = (uint8_t*)ptr.get();
53     if (init_data) {
54       memcpy(p, init_data, size);
55     } else {
56       memset(p, 0, size);
57     }
58 
59     _ScratchPtrs.push_back(std::move(ptr));
60     return p;
61   }
62 
~Fuzz_Context()63   ~Fuzz_Context() {}
64 };
65 
66 extern const char fuzzer_name[];
67 extern void Fuzz_FixPackets(std::vector<bytes_t>& Packets, uint Seed);
68 extern void Fuzz_RunPackets(const std::vector<bytes_t>& Packets);
69 
70 #endif
71