7 #include "gwp_asan/stack_trace_compressor.h"
9 constexpr size_t kBytesForLargestVarInt
= (sizeof(uintptr_t) * 8) / 7 + 1;
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
);
16 // Unpack the fuzz bytes.
17 gwp_asan::compression::unpack(Data
, Size
,
18 reinterpret_cast<uintptr_t *>(Buffer2
.data()),
19 BufferSize
/ sizeof(uintptr_t));
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
);
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));
32 // Ensure that every element was encoded and decoded properly.
33 if (DecodedElements
!= Size
/ sizeof(uintptr_t))
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
]);