From 6732e5ef2fde4adc7db65880ff866111f1d1cbc8 Mon Sep 17 00:00:00 2001 From: sudden6 Date: Fri, 2 Aug 2019 00:18:58 +0200 Subject: [PATCH] Add basic test adapter for AFL --- CMakeLists.txt | 3 ++ testing/BUILD.bazel | 8 ++++ testing/afl_testdata/tox_saves/david.tox | Bin 0 -> 1979 bytes testing/afl_toxsave.c | 54 +++++++++++++++++++++++ testing/run_afl.sh | 14 ++++++ 5 files changed, 79 insertions(+) create mode 100644 testing/afl_testdata/tox_saves/david.tox create mode 100644 testing/afl_toxsave.c create mode 100755 testing/run_afl.sh 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) add_executable(save-generator other/fun/save-generator.c) target_link_modules(save-generator toxcore misc_tools) + add_executable(afl_toxsave + testing/afl_toxsave.c) + target_link_modules(afl_toxsave toxcore) 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( "//c-toxcore/toxcore", ], ) + +cc_binary( + name = "afl_toxsave", + srcs = ["afl_toxsave.c"], + deps = [ + "//c-toxcore/toxcore", + ], +) diff --git a/testing/afl_testdata/tox_saves/david.tox b/testing/afl_testdata/tox_saves/david.tox new file mode 100644 index 0000000000000000000000000000000000000000..391cb6a375d211de629e759bace996964db3928e GIT binary patch literal 1979 zcmZQzU|^7!ek;tfwp>bFac1kkh5kF`t_Uqpu!@LM&FnB@TGFD`+q7~8 z$Hil-1>aw&xp2_q=x*WTUk@>FjbEH3m${p}VeOUbNG9cXb2OTku3%C8qqTXvjB4<` z=N}$sSFO7cvg!6B&w}z9?GLv9i+6ySwnAOv;=C!VirCziU-MJ8zb$$6_dmV}UY|`& zx>xtGfBvb(l)LOgSkqGFS4Fvp-k;AbF8Uzq|3a@XA$hK0Ztv-%3H905>{?R;n1XB+ zZUUA0ix*V6)#>T*D{Xd_GPu7^D!|M|Le4a;!~1`{^h^V$tG~M?nwEw*G`@)x5$d@A zBQE`(>6&G4cdWVgeC?Hz?v4Y+NyV4hn3`Ji_?niw$uBO?`|v9@UW(Pd_T%PlBHyoV zRQY~e_R2pSjt$l+{7fC6dv`PL&U^Ul-u%!rl@`iA2Wr|J8vi8Z_CMbq_;!)_r;Ly6 zU6upmHgIH7EqJLL^+cvOBIA^l^hP-K4oC{2YGeUR3kGIj7H0+0!0gSKl313R!UyEA z0y!c16`6VI3i){og+L}FP?QZQ%AgKpZ~+-j#6^t-P$M@`In!M8b;<0$Yd560{!EUH zP895N{`SkJ^4GWjyEB+e4c&Yb&m9fy&mlGk5SiDn&R8_7Y3aIq*0032WOC%*PFCUc z=l<25&vnLm{w~u{&Gh-Pr?12kTU_}s%!p`Ox^nro&n?pWEupMt8u^|{Ir(>ZMdrG8 zKi@u2!}4w2?Ib2+{^0JWrE83Kv0KdfoA~k?*RM^{7kpRQ?)tIPptk4M1jC)rq~~iB YStI~Mn?VE=?m+M#N`u&=V3>pe0P$_IS^xk5 literal 0 HcmV?d00001 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 @@ +#include +#include + +#include "../toxcore/tox.h" + +int main(int argc, char **argv) +{ + if (argc != 2) { + return -1; + } + + // determine file size + FILE *fileptr = fopen(argv[1], "rb"); + fseek(fileptr, 0, SEEK_END); + long filelen = ftell(fileptr); + rewind(fileptr); + + // read file into buffer + uint8_t *buffer = (uint8_t *)malloc(filelen * sizeof(uint8_t)); + size_t bytes_read = fread(buffer, filelen, 1, fileptr); + + if (bytes_read != filelen) { + free(buffer); + return -1; + } + + fclose(fileptr); + + Tox_Err_Options_New error_options; + + struct Tox_Options *tox_options = tox_options_new(&error_options); + + if (error_options != TOX_ERR_OPTIONS_NEW_OK) { + free(buffer); + return -1; + } + + // pass test data to Tox + tox_options_set_savedata_data(tox_options, buffer, filelen); + tox_options_set_savedata_type(tox_options, TOX_SAVEDATA_TYPE_TOX_SAVE); + + Tox_Err_New error_new; + Tox *tox = tox_new(tox_options, &error_new); + + if (!tox || error_new != TOX_ERR_NEW_OK) { + free(buffer); + return -1; + } + + tox_kill(tox); + free(buffer); + + return 0; +} 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 @@ +#! /bin/sh + +# move to repo root +cd ../ +rm -R _afl_build +mkdir _afl_build +cd _afl_build + +# build c-toxcore using afl instrumentation +cmake -DCMAKE_C_COMPILER=afl-clang -DBUILD_MISC_TESTS=ON .. +make + +# start fuzzing +afl-fuzz -i ../testing/afl_testdata/tox_saves/ -o afl_out/ ./afl_toxsave @@