Coverage Report

Created: 2026-02-14 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fuzz_randomizer.cpp
Line
Count
Source
1
/* Copyright 2021 Google LLC
2
Licensed under the Apache License, Version 2.0 (the "License");
3
you may not use this file except in compliance with the License.
4
You may obtain a copy of the License at
5
      http://www.apache.org/licenses/LICENSE-2.0
6
Unless required by applicable law or agreed to in writing, software
7
distributed under the License is distributed on an "AS IS" BASIS,
8
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
See the License for the specific language governing permissions and
10
limitations under the License.
11
*/
12
13
#include <fuzzer/FuzzedDataProvider.h>
14
#include <assert.h>
15
16
FuzzedDataProvider *prov = NULL;
17
18
3.45k
extern "C" void fuzz_random_init(const uint8_t *data, size_t size) {
19
3.45k
   assert(prov == NULL);
20
3.45k
   prov = new FuzzedDataProvider(data, size);
21
3.45k
}
22
23
3.45k
extern "C" void fuzz_random_destroy() {
24
3.45k
   assert(prov != NULL);
25
3.45k
  delete prov;
26
3.45k
  prov = NULL;
27
3.45k
}
28
29
17.5k
extern "C" char *get_random_string() {
30
17.5k
   assert(prov != NULL);
31
32
17.5k
   std::string s1 = prov->ConsumeRandomLengthString();
33
17.5k
   char *tmp = (char *)malloc(s1.size() + 1);
34
17.5k
   memcpy(tmp, s1.c_str(), s1.size());
35
17.5k
   tmp[s1.size()] = '\0';
36
17.5k
   return tmp;
37
17.5k
}
38
39
34.1k
extern "C" int fuzz_randomizer_get_int(int min, int max) {
40
34.1k
   assert(prov != NULL);
41
34.1k
   return prov->ConsumeIntegralInRange<int>(min, max);
42
34.1k
} 
43
44
929
extern "C" char *fuzz_random_get_string_max_length(int max_len) {
45
929
  assert(prov != NULL);
46
47
929
  std::string s1 = prov->ConsumeBytesAsString(
48
929
                           prov->ConsumeIntegralInRange<uint32_t>(1, max_len));
49
929
  char *tmp123 = (char*)malloc(s1.size()+1);
50
929
  memcpy(tmp123, s1.c_str(), s1.size());
51
929
  tmp123[s1.size()] = '\0';
52
53
929
  return tmp123;
54
929
}
55
56
2.80k
extern "C" size_t fuzz_get_random_data(void *buf, size_t len) {
57
2.80k
  assert(prov != NULL);
58
2.80k
  size_t ret_val;
59
2.80k
  char *cbuf = (char*)buf;
60
61
2.80k
  if (prov->remaining_bytes() == 0) {
62
333
    return -1;
63
333
  }
64
65
2.47k
  double prob = prov->ConsumeProbability<double>();
66
2.47k
  if (prob < 0.05) {
67
1.15k
    return 0;
68
1.15k
  }
69
70
  //if (len == 1) {
71
  //  ret_val = prov->ConsumeData(buf, 1);
72
  //  return ret_val;
73
  //}
74
1.31k
  ret_val = prov->ConsumeData(buf, len);
75
1.31k
  return ret_val;
76
2.47k
}
77
 
78
79
// Simple garbage collector
80
70.4k
#define GB_SIZE 100
81
void *pointer_arr[GB_SIZE];
82
static int pointer_idx = 0;
83
84
// If the garbage collector is used then this must be called as first thing
85
// during a fuzz run.
86
349
extern "C" void gb_init() {
87
349
  pointer_idx = 0;
88
89
35.2k
   for (int i = 0; i < GB_SIZE; i++) {
90
34.9k
     pointer_arr[i] = NULL;
91
34.9k
   }
92
349
}
93
94
349
extern "C" void gb_cleanup() {
95
35.2k
  for(int i = 0; i < GB_SIZE; i++) {
96
34.9k
    if (pointer_arr[i] != NULL) {
97
7.26k
      free(pointer_arr[i]);
98
7.26k
    }
99
34.9k
  }
100
349
}
101
102
7.26k
extern "C" char *gb_get_random_string() {
103
7.26k
  char *tmp = get_random_string();
104
7.26k
  pointer_arr[pointer_idx++] = (void*)tmp;
105
7.26k
  return tmp;
106
7.26k
}
107