File: stack_trace_compressor_fuzzer.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (49 lines) | stat: -rw-r--r-- 1,718 bytes parent folder | download | duplicates (29)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <vector>

#include "gwp_asan/stack_trace_compressor.h"

constexpr size_t kBytesForLargestVarInt = (sizeof(uintptr_t) * 8) / 7 + 1;

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  size_t BufferSize = kBytesForLargestVarInt * Size / sizeof(uintptr_t);
  std::vector<uint8_t> Buffer(BufferSize);
  std::vector<uint8_t> Buffer2(BufferSize);

  // Unpack the fuzz bytes.
  gwp_asan::compression::unpack(Data, Size,
                                reinterpret_cast<uintptr_t *>(Buffer2.data()),
                                BufferSize / sizeof(uintptr_t));

  // Pack the fuzz bytes.
  size_t BytesWritten = gwp_asan::compression::pack(
      reinterpret_cast<const uintptr_t *>(Data), Size / sizeof(uintptr_t),
      Buffer.data(), BufferSize);

  // Unpack the compressed buffer.
  size_t DecodedElements = gwp_asan::compression::unpack(
      Buffer.data(), BytesWritten,
      reinterpret_cast<uintptr_t *>(Buffer2.data()),
      BufferSize / sizeof(uintptr_t));

  // Ensure that every element was encoded and decoded properly.
  if (DecodedElements != Size / sizeof(uintptr_t))
    abort();

  // Ensure that the compression and uncompression resulted in the same trace.
  const uintptr_t *FuzzPtrs = reinterpret_cast<const uintptr_t *>(Data);
  const uintptr_t *DecodedPtrs =
      reinterpret_cast<const uintptr_t *>(Buffer2.data());
  for (size_t i = 0; i < Size / sizeof(uintptr_t); ++i) {
    if (FuzzPtrs[i] != DecodedPtrs[i]) {
      fprintf(stderr, "FuzzPtrs[%zu] != DecodedPtrs[%zu] (0x%zx vs. 0x%zx)", i,
              i, FuzzPtrs[i], DecodedPtrs[i]);
      abort();
    }
  }

  return 0;
}