| ... | ... | @@ -1136,3 +1136,33 @@ pub fn refAllDeclsRecursive(comptime T: type) void { |
| 1136 | 1136 | _ = &@field(T, decl.name); |
| 1137 | 1137 | } |
| 1138 | 1138 | } |
| 1139 | |
| 1140 | const FuzzerSlice = extern struct { |
| 1141 | ptr: [*]const u8, |
| 1142 | len: usize, |
| 1143 | |
| 1144 | fn toSlice(s: FuzzerSlice) []const u8 { |
| 1145 | return s.ptr[0..s.len]; |
| 1146 | } |
| 1147 | }; |
| 1148 | |
| 1149 | extern fn fuzzer_next() FuzzerSlice; |
| 1150 | |
| 1151 | pub const FuzzInputOptions = struct { |
| 1152 | corpus: []const []const u8 = &.{}, |
| 1153 | }; |
| 1154 | |
| 1155 | pub fn fuzzInput(options: FuzzInputOptions) []const u8 { |
| 1156 | @disableInstrumentation(); |
| 1157 | if (builtin.fuzz) { |
| 1158 | return fuzzer_next().toSlice(); |
| 1159 | } else { |
| 1160 | if (options.corpus.len == 0) { |
| 1161 | return ""; |
| 1162 | } else { |
| 1163 | var prng = std.Random.DefaultPrng.init(std.testing.random_seed); |
| 1164 | const random = prng.random(); |
| 1165 | return options.corpus[random.uintLessThan(usize, options.corpus.len)]; |
| 1166 | } |
| 1167 | } |
| 1168 | } |