diff options
author | sudden6 <sudden6@gmx.at> | 2019-08-02 00:18:58 +0200 |
---|---|---|
committer | sudden6 <sudden6@gmx.at> | 2020-03-24 16:49:41 +0100 |
commit | 6732e5ef2fde4adc7db65880ff866111f1d1cbc8 (patch) | |
tree | e33cca67d04011499994e585a7c9c4fc538f602a | |
parent | ef7058422eec1c8b90208bb3522fce28374feb58 (diff) |
Add basic test adapter for AFL
-rw-r--r-- | CMakeLists.txt | 3 | ||||
-rw-r--r-- | testing/BUILD.bazel | 8 | ||||
-rw-r--r-- | testing/afl_testdata/tox_saves/david.tox | bin | 0 -> 1979 bytes | |||
-rw-r--r-- | testing/afl_toxsave.c | 54 | ||||
-rwxr-xr-x | testing/run_afl.sh | 14 |
5 files changed, 79 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 9afe167d..7fe6185b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
@@ -510,4 +510,7 @@ if (BUILD_MISC_TESTS) | |||
510 | add_executable(save-generator | 510 | add_executable(save-generator |
511 | other/fun/save-generator.c) | 511 | other/fun/save-generator.c) |
512 | target_link_modules(save-generator toxcore misc_tools) | 512 | target_link_modules(save-generator toxcore misc_tools) |
513 | add_executable(afl_toxsave | ||
514 | testing/afl_toxsave.c) | ||
515 | target_link_modules(afl_toxsave toxcore) | ||
513 | endif() | 516 | endif() |
diff --git a/testing/BUILD.bazel b/testing/BUILD.bazel index 0db8afa1..34f2d44c 100644 --- a/testing/BUILD.bazel +++ b/testing/BUILD.bazel | |||
@@ -35,3 +35,11 @@ cc_binary( | |||
35 | "//c-toxcore/toxcore", | 35 | "//c-toxcore/toxcore", |
36 | ], | 36 | ], |
37 | ) | 37 | ) |
38 | |||
39 | cc_binary( | ||
40 | name = "afl_toxsave", | ||
41 | srcs = ["afl_toxsave.c"], | ||
42 | deps = [ | ||
43 | "//c-toxcore/toxcore", | ||
44 | ], | ||
45 | ) | ||
diff --git a/testing/afl_testdata/tox_saves/david.tox b/testing/afl_testdata/tox_saves/david.tox new file mode 100644 index 00000000..391cb6a3 --- /dev/null +++ b/testing/afl_testdata/tox_saves/david.tox | |||
Binary files differ | |||
diff --git a/testing/afl_toxsave.c b/testing/afl_toxsave.c new file mode 100644 index 00000000..ddfa569f --- /dev/null +++ b/testing/afl_toxsave.c | |||
@@ -0,0 +1,54 @@ | |||
1 | #include <malloc.h> | ||
2 | #include <stdio.h> | ||
3 | |||
4 | #include "../toxcore/tox.h" | ||
5 | |||
6 | int main(int argc, char **argv) | ||
7 | { | ||
8 | if (argc != 2) { | ||
9 | return -1; | ||
10 | } | ||
11 | |||
12 | // determine file size | ||
13 | FILE *fileptr = fopen(argv[1], "rb"); | ||
14 | fseek(fileptr, 0, SEEK_END); | ||
15 | long filelen = ftell(fileptr); | ||
16 | rewind(fileptr); | ||
17 | |||
18 | // read file into buffer | ||
19 | uint8_t *buffer = (uint8_t *)malloc(filelen * sizeof(uint8_t)); | ||
20 | size_t bytes_read = fread(buffer, filelen, 1, fileptr); | ||
21 | |||
22 | if (bytes_read != filelen) { | ||
23 | free(buffer); | ||
24 | return -1; | ||
25 | } | ||
26 | |||
27 | fclose(fileptr); | ||
28 | |||
29 | Tox_Err_Options_New error_options; | ||
30 | |||
31 | struct Tox_Options *tox_options = tox_options_new(&error_options); | ||
32 | |||
33 | if (error_options != TOX_ERR_OPTIONS_NEW_OK) { | ||
34 | free(buffer); | ||
35 | return -1; | ||
36 | } | ||
37 | |||
38 | // pass test data to Tox | ||
39 | tox_options_set_savedata_data(tox_options, buffer, filelen); | ||
40 | tox_options_set_savedata_type(tox_options, TOX_SAVEDATA_TYPE_TOX_SAVE); | ||
41 | |||
42 | Tox_Err_New error_new; | ||
43 | Tox *tox = tox_new(tox_options, &error_new); | ||
44 | |||
45 | if (!tox || error_new != TOX_ERR_NEW_OK) { | ||
46 | free(buffer); | ||
47 | return -1; | ||
48 | } | ||
49 | |||
50 | tox_kill(tox); | ||
51 | free(buffer); | ||
52 | |||
53 | return 0; | ||
54 | } | ||
diff --git a/testing/run_afl.sh b/testing/run_afl.sh new file mode 100755 index 00000000..c7a3bbc6 --- /dev/null +++ b/testing/run_afl.sh | |||
@@ -0,0 +1,14 @@ | |||
1 | #! /bin/sh | ||
2 | |||
3 | # move to repo root | ||
4 | cd ../ | ||
5 | rm -R _afl_build | ||
6 | mkdir _afl_build | ||
7 | cd _afl_build | ||
8 | |||
9 | # build c-toxcore using afl instrumentation | ||
10 | cmake -DCMAKE_C_COMPILER=afl-clang -DBUILD_MISC_TESTS=ON .. | ||
11 | make | ||
12 | |||
13 | # start fuzzing | ||
14 | afl-fuzz -i ../testing/afl_testdata/tox_saves/ -o afl_out/ ./afl_toxsave @@ | ||