forked from irungentoo/toxcore
-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add some support functions to make writing fuzzers easier.
- Loading branch information
Showing
5 changed files
with
95 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "fuzz_support.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* SPDX-License-Identifier: GPL-3.0-or-later | ||
* Copyright © 2021 The TokTok team. | ||
*/ | ||
|
||
#ifndef C_TOXCORE_TESTING_FUZZING_FUZZ_SUPPORT_H | ||
#define C_TOXCORE_TESTING_FUZZING_FUZZ_SUPPORT_H | ||
|
||
#include <cstdint> | ||
#include <cstdlib> | ||
|
||
struct Fuzz_Data { | ||
const uint8_t *data; | ||
std::size_t size; | ||
|
||
uint8_t consume1() { | ||
const uint8_t val = data[0]; | ||
++data; | ||
--size; | ||
return val; | ||
} | ||
|
||
const uint8_t *consume(std::size_t count) { | ||
const uint8_t *val = data; | ||
data += count; | ||
size -= count; | ||
return val; | ||
} | ||
}; | ||
|
||
#define CONSUME1(DECL, INPUT) \ | ||
if (INPUT.size < 1) { \ | ||
return; \ | ||
} \ | ||
DECL = INPUT.consume1() | ||
|
||
#define CONSUME(DECL, INPUT, SIZE) \ | ||
if (INPUT.size < SIZE) { \ | ||
return; \ | ||
} \ | ||
DECL = INPUT.consume(SIZE) | ||
|
||
inline void fuzz_select_target(uint8_t selector, Fuzz_Data input) | ||
{ | ||
// The selector selected no function, so we do nothing and rely on the | ||
// fuzzer to come up with a better selector. | ||
} | ||
|
||
template<typename Arg, typename ...Args> | ||
void fuzz_select_target(uint8_t selector, Fuzz_Data input, Arg fn, Args ...args) | ||
{ | ||
if (selector == sizeof...(Args)) { | ||
return fn(input); | ||
} | ||
return fuzz_select_target(selector - 1, input, args...); | ||
} | ||
|
||
template<typename ...Args> | ||
void fuzz_select_target(const uint8_t *data, std::size_t size, Args ...args) | ||
{ | ||
Fuzz_Data input{data, size}; | ||
|
||
CONSUME1(uint8_t selector, input); | ||
return fuzz_select_target(selector, input, args...); | ||
} | ||
|
||
#endif // C_TOXCORE_TESTING_FUZZING_FUZZ_SUPPORT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters