Coverage Report

Created: 2025-08-11 06:17

/src/fuzz_header.h
Line
Count
Source
1
/* Copyright 2025 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 <stdint.h>
14
#include <stdio.h>
15
#include <stdlib.h>
16
17
uint8_t *global_fuzz_data;
18
size_t global_size;
19
20
char *callback1 = NULL;
21
char *callback2 = NULL;
22
char *callback3 = NULL;
23
char *callback4 = NULL;
24
25
134
void fuzz_init(const uint8_t *data, size_t size) {
26
134
    global_fuzz_data = (uint8_t *)data;
27
134
    global_size = size;
28
    
29
134
    callback1 = NULL;
30
134
    callback2 = NULL;
31
134
    callback3 = NULL;
32
134
    callback4 = NULL;
33
134
}
34
35
#ifdef ENABLE_FILE_CALLBACK
36
void create_callback_file(const char *filename) {
37
  fprintf(stderr, "create_callback_file: Creating file %s\n", filename);
38
  FILE *fp = fopen(filename, "wb");
39
  if (!fp) {
40
    return;
41
  }
42
  fwrite(global_fuzz_data, global_size, 1, fp);
43
  fclose(fp);
44
45
  if (callback1 == NULL) {
46
    callback1 = strdup(filename);
47
  } else if (callback2 == NULL) {
48
    callback2 = strdup(filename);
49
  } else if (callback3 == NULL) {
50
    callback3 = strdup(filename);
51
  } else if (callback4 == NULL) {
52
    callback4 = strdup(filename);
53
  } else {
54
    fprintf(stderr, "create_callback_file: Too many callbacks created, ignoring %s\n", filename);
55
  }
56
}
57
58
#else
59
4.74k
void create_callback_file(const char *filename) {}
60
#endif
61
62
134
void fuzz_cleanup() {
63
134
  if (callback1) {
64
116
 unlink(callback1);    
65
116
    free(callback1);
66
   
67
116
    callback1 = NULL;
68
116
  }
69
134
  if (callback2) {
70
95
 unlink(callback2);    
71
95
    free(callback2);
72
95
    callback2 = NULL;
73
95
  }
74
134
  if (callback3) {
75
53
 unlink(callback3);      
76
53
    free(callback3);
77
53
    callback3 = NULL;
78
53
  }
79
134
  if (callback4) {
80
23
     unlink(callback4);
81
23
    free(callback4);
82
23
    callback4 = NULL;
83
23
  }
84
134
}