Coverage Report

Created: 2023-05-30 07:02

/src/fuzz_randomizer.cpp
Line
Count
Source (jump to first uncovered line)
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
355
extern "C" void fuzz_random_init(const uint8_t *data, size_t size) {
19
355
   assert(prov == NULL);
20
0
   prov = new FuzzedDataProvider(data, size);
21
355
}
22
23
355
extern "C" void fuzz_random_destroy() {
24
355
   assert(prov != NULL);
25
0
  delete prov;
26
355
  prov = NULL;
27
355
}
28
29
345
extern "C" char *get_random_string() {
30
345
   assert(prov != NULL);
31
32
0
   std::string s1 = prov->ConsumeRandomLengthString();
33
345
   char *tmp = (char *)malloc(s1.size() + 1);
34
345
   memcpy(tmp, s1.c_str(), s1.size());
35
345
   tmp[s1.size()] = '\0';
36
345
   return tmp;
37
345
}
38
39
1.05k
extern "C" int fuzz_randomizer_get_int(int min, int max) {
40
1.05k
   assert(prov != NULL);
41
0
   return prov->ConsumeIntegralInRange<int>(min, max);
42
1.05k
} 
43
44
0
extern "C" char *fuzz_random_get_string_max_length(int max_len) {
45
0
  assert(prov != NULL);
46
47
0
  std::string s1 = prov->ConsumeBytesAsString(
48
0
                           prov->ConsumeIntegralInRange<uint32_t>(1, max_len));
49
0
  char *tmp123 = (char*)malloc(s1.size()+1);
50
0
  memcpy(tmp123, s1.c_str(), s1.size());
51
0
  tmp123[s1.size()] = '\0';
52
53
0
  return tmp123;
54
0
}
55
56
0
extern "C" size_t fuzz_get_random_data(void *buf, size_t len) {
57
0
  assert(prov != NULL);
58
0
  size_t ret_val;
59
0
  char *cbuf = (char*)buf;
60
61
0
  if (prov->remaining_bytes() == 0) {
62
0
    return -1;
63
0
  }
64
65
0
  double prob = prov->ConsumeProbability<double>();
66
0
  if (prob < 0.05) {
67
0
    return 0;
68
0
  }
69
70
  //if (len == 1) {
71
  //  ret_val = prov->ConsumeData(buf, 1);
72
  //  return ret_val;
73
  //}
74
0
  ret_val = prov->ConsumeData(buf, len);
75
0
  return ret_val;
76
0
}
77
 
78
79
// Simple garbage collector
80
0
#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
0
extern "C" void gb_init() {
87
0
  pointer_idx = 0;
88
89
0
   for (int i = 0; i < GB_SIZE; i++) {
90
0
     pointer_arr[i] = NULL;
91
0
   }
92
0
}
93
94
0
extern "C" void gb_cleanup() {
95
0
  for(int i = 0; i < GB_SIZE; i++) {
96
0
    if (pointer_arr[i] != NULL) {
97
0
      free(pointer_arr[i]);
98
0
    }
99
0
  }
100
0
}
101
102
0
extern "C" char *gb_get_random_string() {
103
0
  char *tmp = get_random_string();
104
0
  pointer_arr[pointer_idx++] = (void*)tmp;
105
0
  return tmp;
106
0
}
107