/src/net-snmp/testing/fuzzing/ada_fuzz_header.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, Net-snmp authors |
3 | | * All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that the following conditions are met: |
7 | | * |
8 | | * * Redistributions of source code must retain the above copyright notice, this |
9 | | * list of conditions and the following disclaimer. |
10 | | * |
11 | | * * Redistributions in binary form must reproduce the above copyright notice, |
12 | | * this list of conditions and the following disclaimer in the documentation |
13 | | * and/or other materials provided with the distribution. |
14 | | * |
15 | | * * Neither the name of the copyright holder nor the names of its |
16 | | * contributors may be used to endorse or promote products derived from |
17 | | * this software without specific prior written permission. |
18 | | * |
19 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
20 | | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
22 | | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
23 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
25 | | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
26 | | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
27 | | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | | */ |
30 | | |
31 | | /* |
32 | | * Declare functions. |
33 | | */ |
34 | | void af_gb_init(void); |
35 | | void af_gb_cleanup(void); |
36 | | |
37 | | // Simple garbage collector |
38 | 3.12M | #define GB_SIZE 100 |
39 | | |
40 | | void *pointer_arr[GB_SIZE]; |
41 | | int pointer_idx; |
42 | | |
43 | | // If the garbage collector is used then this must be called as first thing |
44 | | // during a fuzz run. |
45 | 15.4k | void af_gb_init() { |
46 | 15.4k | pointer_idx = 0; |
47 | | |
48 | 1.56M | for (int i = 0; i < GB_SIZE; i++) { |
49 | 1.54M | pointer_arr[i] = NULL; |
50 | 1.54M | } |
51 | 15.4k | } |
52 | | |
53 | 15.4k | void af_gb_cleanup() { |
54 | 1.56M | for(int i = 0; i < GB_SIZE; i++) { |
55 | 1.54M | if (pointer_arr[i] != NULL) { |
56 | 72.5k | free(pointer_arr[i]); |
57 | 72.5k | } |
58 | 1.54M | } |
59 | 15.4k | } |
60 | | |
61 | 73.4k | void *af_get_null_terminated(const uint8_t **data, size_t *size) { |
62 | 470k | #define STR_SIZE 75 |
63 | 73.4k | if (*size < STR_SIZE || (int)*size < 0) { |
64 | 8.85k | return NULL; |
65 | 8.85k | } |
66 | | |
67 | 64.6k | void *new_s = malloc(STR_SIZE + 1); |
68 | 64.6k | memcpy(new_s, *data, STR_SIZE); |
69 | 64.6k | ((uint8_t *)new_s)[STR_SIZE] = '\0'; |
70 | | |
71 | 64.6k | *data = *data+STR_SIZE; |
72 | 64.6k | *size -= STR_SIZE; |
73 | 64.6k | return new_s; |
74 | 73.4k | } |
75 | | |
76 | 9.61k | void *af_gb_get_random_data(const uint8_t **data, size_t *size, size_t to_get) { |
77 | 9.61k | if (*size < to_get || (int)*size < 0) { |
78 | 1.33k | return NULL; |
79 | 1.33k | } |
80 | | |
81 | 8.28k | void *new_s = malloc(to_get); |
82 | 8.28k | memcpy(new_s, *data, to_get); |
83 | | |
84 | 8.28k | pointer_arr[pointer_idx++] = new_s; |
85 | | |
86 | 8.28k | *data = *data + to_get; |
87 | 8.28k | *size -= to_get; |
88 | | |
89 | 8.28k | return new_s; |
90 | 9.61k | } |
91 | | |
92 | 70.2k | void *af_gb_get_null_terminated(const uint8_t **data, size_t *size) { |
93 | | |
94 | 70.2k | void *nstr = af_get_null_terminated(data, size); |
95 | 70.2k | if (nstr == NULL) { |
96 | 5.97k | return NULL; |
97 | 5.97k | } |
98 | 64.2k | pointer_arr[pointer_idx++] = nstr; |
99 | 64.2k | return nstr; |
100 | 70.2k | } |
101 | | |
102 | 0 | void *af_gb_alloc_data(size_t len) { |
103 | 0 | void *ptr = calloc(1, len); |
104 | 0 | pointer_arr[pointer_idx++] = ptr; |
105 | | |
106 | 0 | return ptr; |
107 | 0 | } |
108 | | |
109 | 19.8k | short af_get_short(const uint8_t **data, size_t *size) { |
110 | 19.8k | if (*size <= 0) return 0; |
111 | 19.3k | short c = (short)(*data)[0]; |
112 | 19.3k | *data += 1; |
113 | 19.3k | *size -= 1; |
114 | 19.3k | return c; |
115 | 19.8k | } |
116 | | |
117 | 29.8k | int af_get_int(const uint8_t **data, size_t *size) { |
118 | 29.8k | if (*size <= 4) return 0; |
119 | 19.0k | const uint8_t *ptr = *data; |
120 | 19.0k | int val = *((const int*)ptr); |
121 | 19.0k | *data += 4; |
122 | 19.0k | *size -= 4; |
123 | 19.0k | return val; |
124 | 29.8k | } |
125 | | // end simple garbage collector. |