1 #include <cstddef> 2 #include <cstdint> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <vector> 6 7 #include "gwp_asan/stack_trace_compressor.h" 8 9 constexpr size_t kBytesForLargestVarInt = (sizeof(uintptr_t) * 8) / 7 + 1; 10 11 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { 12 size_t BufferSize = kBytesForLargestVarInt * Size / sizeof(uintptr_t); 13 std::vector<uint8_t> Buffer(BufferSize); 14 std::vector<uint8_t> Buffer2(BufferSize); 15 16 // Unpack the fuzz bytes. 17 gwp_asan::compression::unpack(Data, Size, 18 reinterpret_cast<uintptr_t *>(Buffer2.data()), 19 BufferSize / sizeof(uintptr_t)); 20 21 // Pack the fuzz bytes. 22 size_t BytesWritten = gwp_asan::compression::pack( 23 reinterpret_cast<const uintptr_t *>(Data), Size / sizeof(uintptr_t), 24 Buffer.data(), BufferSize); 25 26 // Unpack the compressed buffer. 27 size_t DecodedElements = gwp_asan::compression::unpack( 28 Buffer.data(), BytesWritten, 29 reinterpret_cast<uintptr_t *>(Buffer2.data()), 30 BufferSize / sizeof(uintptr_t)); 31 32 // Ensure that every element was encoded and decoded properly. 33 if (DecodedElements != Size / sizeof(uintptr_t)) 34 abort(); 35 36 // Ensure that the compression and uncompression resulted in the same trace. 37 const uintptr_t *FuzzPtrs = reinterpret_cast<const uintptr_t *>(Data); 38 const uintptr_t *DecodedPtrs = 39 reinterpret_cast<const uintptr_t *>(Buffer2.data()); 40 for (size_t i = 0; i < Size / sizeof(uintptr_t); ++i) { 41 if (FuzzPtrs[i] != DecodedPtrs[i]) { 42 fprintf(stderr, "FuzzPtrs[%zu] != DecodedPtrs[%zu] (0x%zx vs. 0x%zx)", i, 43 i, FuzzPtrs[i], DecodedPtrs[i]); 44 abort(); 45 } 46 } 47 48 return 0; 49 } 50