Coverage Report

Created: 2025-04-11 06:03

/src/fmt/test/fuzzing/fuzzer-common.h
Line
Count
Source
1
// Copyright (c) 2019, Paul Dreik
2
// For the license information refer to format.h.
3
4
#ifndef FUZZER_COMMON_H
5
#define FUZZER_COMMON_H
6
7
#include <fmt/base.h>
8
9
#include <cstdint>  // std::uint8_t
10
#include <cstring>  // memcpy
11
#include <vector>
12
13
// One can format to either a string, or a buffer. The latter is faster, but
14
// one may be interested in formatting to a string instead to verify it works
15
// as intended. To avoid a combinatoric explosion, select this at compile time
16
// instead of dynamically from the fuzz data.
17
#define FMT_FUZZ_FORMAT_TO_STRING 0
18
19
// If {fmt} is given a buffer that is separately allocated, chances that address
20
// sanitizer detects out of bound reads is much higher. However, it slows down
21
// the fuzzing.
22
#define FMT_FUZZ_SEPARATE_ALLOCATION 1
23
24
// The size of the largest possible type in use.
25
// To let the the fuzzer mutation be efficient at cross pollinating between
26
// different types, use a fixed size format. The same bit pattern, interpreted
27
// as another type, is likely interesting.
28
constexpr auto fixed_size = 16;
29
30
// Casts data to a char pointer.
31
85.9k
template <typename T> inline const char* as_chars(const T* data) {
32
85.9k
  return reinterpret_cast<const char*>(data);
33
85.9k
}
34
35
// Casts data to a byte pointer.
36
template <typename T> inline const std::uint8_t* as_bytes(const T* data) {
37
  return reinterpret_cast<const std::uint8_t*>(data);
38
}
39
40
// Blits bytes from data to form an (assumed trivially constructible) object
41
// of type Item.
42
126k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
126k
  auto item = Item();
44
126k
  std::memcpy(&item, data, sizeof(Item));
45
126k
  return item;
46
126k
}
char assign_from_buf<char>(unsigned char const*)
Line
Count
Source
42
7.24k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
7.24k
  auto item = Item();
44
7.24k
  std::memcpy(&item, data, sizeof(Item));
45
7.24k
  return item;
46
7.24k
}
signed char assign_from_buf<signed char>(unsigned char const*)
Line
Count
Source
42
6.54k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
6.54k
  auto item = Item();
44
6.54k
  std::memcpy(&item, data, sizeof(Item));
45
6.54k
  return item;
46
6.54k
}
unsigned char assign_from_buf<unsigned char>(unsigned char const*)
Line
Count
Source
42
6.12k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
6.12k
  auto item = Item();
44
6.12k
  std::memcpy(&item, data, sizeof(Item));
45
6.12k
  return item;
46
6.12k
}
short assign_from_buf<short>(unsigned char const*)
Line
Count
Source
42
6.53k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
6.53k
  auto item = Item();
44
6.53k
  std::memcpy(&item, data, sizeof(Item));
45
6.53k
  return item;
46
6.53k
}
unsigned short assign_from_buf<unsigned short>(unsigned char const*)
Line
Count
Source
42
6.37k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
6.37k
  auto item = Item();
44
6.37k
  std::memcpy(&item, data, sizeof(Item));
45
6.37k
  return item;
46
6.37k
}
int assign_from_buf<int>(unsigned char const*)
Line
Count
Source
42
9.10k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
9.10k
  auto item = Item();
44
9.10k
  std::memcpy(&item, data, sizeof(Item));
45
9.10k
  return item;
46
9.10k
}
unsigned int assign_from_buf<unsigned int>(unsigned char const*)
Line
Count
Source
42
8.30k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
8.30k
  auto item = Item();
44
8.30k
  std::memcpy(&item, data, sizeof(Item));
45
8.30k
  return item;
46
8.30k
}
long assign_from_buf<long>(unsigned char const*)
Line
Count
Source
42
13.9k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
13.9k
  auto item = Item();
44
13.9k
  std::memcpy(&item, data, sizeof(Item));
45
13.9k
  return item;
46
13.9k
}
unsigned long assign_from_buf<unsigned long>(unsigned char const*)
Line
Count
Source
42
8.62k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
8.62k
  auto item = Item();
44
8.62k
  std::memcpy(&item, data, sizeof(Item));
45
8.62k
  return item;
46
8.62k
}
float assign_from_buf<float>(unsigned char const*)
Line
Count
Source
42
14.1k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
14.1k
  auto item = Item();
44
14.1k
  std::memcpy(&item, data, sizeof(Item));
45
14.1k
  return item;
46
14.1k
}
double assign_from_buf<double>(unsigned char const*)
Line
Count
Source
42
19.4k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
19.4k
  auto item = Item();
44
19.4k
  std::memcpy(&item, data, sizeof(Item));
45
19.4k
  return item;
46
19.4k
}
long double assign_from_buf<long double>(unsigned char const*)
Line
Count
Source
42
14.7k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
14.7k
  auto item = Item();
44
14.7k
  std::memcpy(&item, data, sizeof(Item));
45
14.7k
  return item;
46
14.7k
}
long long assign_from_buf<long long>(unsigned char const*)
Line
Count
Source
42
4.17k
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
4.17k
  auto item = Item();
44
4.17k
  std::memcpy(&item, data, sizeof(Item));
45
4.17k
  return item;
46
4.17k
}
void* assign_from_buf<void*>(unsigned char const*)
Line
Count
Source
42
757
template <class Item> inline Item assign_from_buf(const std::uint8_t* data) {
43
757
  auto item = Item();
44
757
  std::memcpy(&item, data, sizeof(Item));
45
757
  return item;
46
757
}
47
48
// Reads a boolean value by looking at the first byte from data.
49
1.71k
template <> inline bool assign_from_buf<bool>(const std::uint8_t* data) {
50
1.71k
  return *data != 0;
51
1.71k
}
52
53
struct data_to_string {
54
#if FMT_FUZZ_SEPARATE_ALLOCATION
55
  std::vector<char> buffer;
56
57
  data_to_string(const uint8_t* data, size_t size, bool add_terminator = false)
58
38.0k
      : buffer(size + (add_terminator ? 1 : 0)) {
59
38.0k
    if (size) {
60
38.0k
      std::memcpy(buffer.data(), data, size);
61
38.0k
    }
62
38.0k
  }
63
64
38.0k
  fmt::string_view get() const { return {buffer.data(), buffer.size()}; }
65
#else
66
  fmt::string_view sv;
67
68
  data_to_string(const uint8_t* data, size_t size, bool = false)
69
      : str(as_chars(data), size) {}
70
71
  fmt::string_view get() const { return sv; }
72
#endif
73
74
10.6k
  const char* data() const { return get().data(); }
75
};
76
77
#endif  // FUZZER_COMMON_H