From 1996de769bd022de56144bb14c7d0573c8d320af Mon Sep 17 00:00:00 2001 From: jangko Date: Tue, 26 Dec 2023 09:38:34 +0700 Subject: [PATCH] Add fuzz test --- .gitignore | 3 ++- config.nims | 12 ++++++++++-- fuzzer/fuzz_lexer.nim | 23 +++++++++++++++++++++++ fuzzer/fuzz_parser.nim | 29 +++++++++++++++++++++++++++++ fuzzer/readme.md | 36 ++++++++++++++++++++++++++++++++++++ json_serialization.nimble | 2 +- 6 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 fuzzer/fuzz_lexer.nim create mode 100644 fuzzer/fuzz_parser.nim create mode 100644 fuzzer/readme.md diff --git a/.gitignore b/.gitignore index 77d2d27..e2e4256 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ nimcache *.exe nimble.develop -nimble.paths +nimble-win.paths +nimble-linux.paths build/ vendor/ diff --git a/config.nims b/config.nims index 86bd4ef..265febe 100644 --- a/config.nims +++ b/config.nims @@ -8,6 +8,14 @@ # those terms. # begin Nimble config (version 1) -when fileExists("nimble.paths"): - include "nimble.paths" +when defined(windows): + when fileExists("nimble-win.paths"): + include "nimble-win.paths" + elif fileExists("nimble.paths"): + include "nimble.paths" +elif defined(linux): + when fileExists("nimble-win.paths"): + include "nimble-linux.paths" + elif fileExists("nimble.paths"): + include "nimble.paths" # end Nimble config diff --git a/fuzzer/fuzz_lexer.nim b/fuzzer/fuzz_lexer.nim new file mode 100644 index 0000000..dbe6fdc --- /dev/null +++ b/fuzzer/fuzz_lexer.nim @@ -0,0 +1,23 @@ +# json-serialization +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + +import + testutils/fuzzing, + faststreams, + ../json_serialization/lexer + +template prepareLexer(T: type, payload: untyped) = + var stream = unsafeMemoryInput(payload) + var lex = init(JsonLexer, stream) + var value: JsonValueRef[T] + lex.scanValue(value) + +test: + prepareLexer(string, payload) + prepareLexer(uint64, payload) diff --git a/fuzzer/fuzz_parser.nim b/fuzzer/fuzz_parser.nim new file mode 100644 index 0000000..2c3f005 --- /dev/null +++ b/fuzzer/fuzz_parser.nim @@ -0,0 +1,29 @@ +# json-serialization +# Copyright (c) 2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + +import + testutils/fuzzing, + faststreams, + ../json_serialization/parser + +func toReader(input: openArray[byte]): JsonReader[DefaultFlavor] = + var stream = unsafeMemoryInput(input) + JsonReader[DefaultFlavor].init(stream) + +proc executeParser(payload: openArray[byte]) = + try: + var r = toReader(payload) + let z = r.parseValue(uint64) + discard z + except JsonReaderError: + discard + +test: + executeParser(payload) + \ No newline at end of file diff --git a/fuzzer/readme.md b/fuzzer/readme.md new file mode 100644 index 0000000..202b766 --- /dev/null +++ b/fuzzer/readme.md @@ -0,0 +1,36 @@ +## Fuzz test manual + +You need to install [testutils](https://github.com/status-im/nim-testutils) and +[chronicles](https://github.com/status-im/nim-chronicles). +Then read documentation over there to prepare your execution environment. + +### Compatibility + +These fuzzers can be compiled with Nim v1.6.16 or newer. + +### Available fuzz test + +* fuzz_lexer +* fuzz_parser + +### Manually with libFuzzer/llvmFuzer +#### Compiling +```sh +nim c -d:llvmFuzzer -d:release -d:chronicles_log_level=FATAL --noMain --cc=clang --passC="-fsanitize=fuzzer" --passL="-fsanitize=fuzzer" fuzzer/fuzz_lexer +``` + +#### Starting the Fuzzer +Starting the fuzzer is as simple as running the compiled program: +```sh +./fuzz_lexer corpus_dir -runs=1000000 +``` + +To see the available options: +```sh +./fuzz_lexer test=1 +``` + +You can also use the application to verify a specific test case: +```sh +./fuzz_lexer input_file +``` diff --git a/json_serialization.nimble b/json_serialization.nimble index 2d6d34c..29be04d 100644 --- a/json_serialization.nimble +++ b/json_serialization.nimble @@ -14,7 +14,7 @@ version = "0.2.0" author = "Status Research & Development GmbH" description = "Flexible JSON serialization not relying on run-time type information" license = "Apache License 2.0" -skipDirs = @["tests"] +skipDirs = @["tests", "fuzzer"] requires "nim >= 1.6.0", "serialization",