Skip to content

Commit 9eb8879

Browse files
committed
add fuzzing harnesses
Toxsave harness ported to libFuzzer interface. New harness for bootstrap phase.
1 parent 210ea9e commit 9eb8879

File tree

4 files changed

+77
-59
lines changed

4 files changed

+77
-59
lines changed

CMakeLists.txt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,23 @@ if (BUILD_MISC_TESTS)
535535
target_link_libraries(cracker OpenMP::OpenMP_C)
536536
endif()
537537
endif()
538+
endif()
539+
540+
# Enabling this breaks all other tests and no network connections will be possible
541+
option(BUILD_FUZZ_TESTS "Build fuzzing harnesses" OFF)
542+
if (BUILD_FUZZ_TESTS)
543+
# For coverage tests
544+
target_compile_definitions(toxcore_static PUBLIC "FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION")
545+
546+
# Override network functions
547+
add_library(network_adapter testing/fuzzing/network_adapter.c)
538548

539-
add_executable(afl_toxsave
540-
testing/afl_toxsave.c)
541-
target_link_modules(afl_toxsave toxcore)
549+
# Fuzzes the toxsave API
550+
add_executable(toxsave_fuzzer testing/fuzzing/toxsave_harness.cc)
551+
target_link_libraries(toxsave_fuzzer toxcore_static network_adapter -fsanitize=fuzzer)
552+
553+
# Fuzzes the bootstrap process
554+
add_executable(bootstrap_fuzzer testing/fuzzing/bootstrap_harness.cc)
555+
target_link_libraries(bootstrap_fuzzer toxcore_static network_adapter -fsanitize=fuzzer)
542556
endif()
557+

testing/afl_toxsave.c

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "../../toxcore/tox.h"
2+
#include "network_adapter.h"
3+
4+
#include <cstring>
5+
#include <cassert>
6+
7+
8+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
9+
network_adapter_init(data, size);
10+
11+
Tox_Err_New error_new;
12+
Tox *tox = tox_new(NULL, &error_new);
13+
14+
assert(tox != nullptr);
15+
assert(error_new == TOX_ERR_NEW_OK);
16+
17+
uint8_t pub_key[TOX_PUBLIC_KEY_SIZE] = {0};
18+
19+
bool success = tox_bootstrap(tox, "127.0.0.1", 12345, pub_key, nullptr);
20+
assert(success);
21+
22+
/*
23+
* The iteration count here is a magic value in the literal sense, too small
24+
* and coverage will be bad, too big and fuzzing will not be efficient.
25+
* NOTE: This should be fine tuned after gathering some experience.
26+
*/
27+
28+
for (uint32_t i = 0; i < 100; ++i) {
29+
tox_iterate(tox, nullptr);
30+
}
31+
32+
tox_kill(tox);
33+
return 0; // Non-zero return values are reserved for future use.
34+
}

testing/fuzzing/toxsave_harness.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "../../toxcore/tox.h"
2+
3+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
4+
Tox_Err_Options_New error_options;
5+
6+
struct Tox_Options *tox_options = tox_options_new(&error_options);
7+
8+
assert(tox_options != nullptr);
9+
assert(error_options == TOX_ERR_OPTIONS_NEW_OK);
10+
11+
// pass test data to Tox
12+
tox_options_set_savedata_data(tox_options, data, size);
13+
tox_options_set_savedata_type(tox_options, TOX_SAVEDATA_TYPE_TOX_SAVE);
14+
15+
Tox_Err_New error_new;
16+
Tox *tox = tox_new(tox_options, &error_new);
17+
18+
assert(tox != nullptr);
19+
assert(error_new == TOX_ERR_NEW_OK);
20+
21+
tox_options_free(tox_options);
22+
23+
tox_kill(tox);
24+
return 0; // Non-zero return values are reserved for future use.
25+
}

0 commit comments

Comments
 (0)