Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.openwrt.org/project/libubox.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPetr Štetiar <ynezz@true.cz>2019-12-08 17:11:02 +0300
committerPetr Štetiar <ynezz@true.cz>2019-12-25 12:31:58 +0300
commit436d6363a10bbb41ab92602b4eb0030992bb1785 (patch)
tree8ae96268433e5f772b03c48170e4fa2c6bde6e54 /tests/fuzz/test-fuzz.c
parentbf680707acfdadcd6301657448dcf3bd8c8fa60c (diff)
tests: add libFuzzer based tests
LibFuzzer is in-process, coverage-guided, evolutionary fuzzing engine. LibFuzzer is linked with the library under test, and feeds fuzzed inputs to the library via a specific fuzzing entrypoint (aka "target function"); the fuzzer then tracks which areas of the code are reached, and generates mutations on the corpus of input data in order to maximize the code coverage. Lets use libFuzzer to fuzz blob and blobmsg parsing for the start. Ref: https://llvm.org/docs/LibFuzzer.html Signed-off-by: Petr Štetiar <ynezz@true.cz>
Diffstat (limited to 'tests/fuzz/test-fuzz.c')
-rw-r--r--tests/fuzz/test-fuzz.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/tests/fuzz/test-fuzz.c b/tests/fuzz/test-fuzz.c
new file mode 100644
index 0000000..7153847
--- /dev/null
+++ b/tests/fuzz/test-fuzz.c
@@ -0,0 +1,76 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stddef.h>
+
+#include "blob.h"
+#include "blobmsg.h"
+
+static void fuzz_blobmsg_parse(const uint8_t *data, size_t size)
+{
+ enum {
+ FOO_MESSAGE,
+ FOO_LIST,
+ FOO_TESTDATA,
+ __FOO_MAX
+ };
+
+ static const struct blobmsg_policy foo_policy[] = {
+ [FOO_MESSAGE] = {
+ .name = "message",
+ .type = BLOBMSG_TYPE_STRING,
+ },
+ [FOO_LIST] = {
+ .name = "list",
+ .type = BLOBMSG_TYPE_ARRAY,
+ },
+ [FOO_TESTDATA] = {
+ .name = "testdata",
+ .type = BLOBMSG_TYPE_TABLE,
+ },
+ };
+
+ struct blob_attr *tb[__FOO_MAX];
+
+ blobmsg_parse(foo_policy, __FOO_MAX, tb, (uint8_t *)data, size);
+ blobmsg_parse_array(foo_policy, __FOO_MAX, tb, (uint8_t *)data, size);
+}
+
+static void fuzz_blob_parse(const uint8_t *data, size_t size)
+{
+ enum {
+ FOO_ATTR_NESTED,
+ FOO_ATTR_BINARY,
+ FOO_ATTR_STRING,
+ FOO_ATTR_INT8,
+ FOO_ATTR_INT16,
+ FOO_ATTR_INT32,
+ FOO_ATTR_INT64,
+ FOO_ATTR_DOUBLE,
+ __FOO_ATTR_MAX
+ };
+
+
+ static const struct blob_attr_info foo_policy[__FOO_ATTR_MAX] = {
+ [FOO_ATTR_NESTED] = { .type = BLOB_ATTR_NESTED },
+ [FOO_ATTR_BINARY] = { .type = BLOB_ATTR_BINARY },
+ [FOO_ATTR_STRING] = { .type = BLOB_ATTR_STRING },
+ [FOO_ATTR_INT8] = { .type = BLOB_ATTR_INT8 },
+ [FOO_ATTR_INT16] = { .type = BLOB_ATTR_INT16 },
+ [FOO_ATTR_INT32] = { .type = BLOB_ATTR_INT32 },
+ [FOO_ATTR_INT64] = { .type = BLOB_ATTR_INT64 },
+ [FOO_ATTR_DOUBLE] = { .type = BLOB_ATTR_DOUBLE },
+ };
+
+ struct blob_attr *foo[__FOO_ATTR_MAX];
+ struct blob_attr *buf = (struct blob_attr *)data;
+
+ blob_parse(buf, foo, foo_policy, __FOO_ATTR_MAX);
+}
+
+int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
+{
+ fuzz_blob_parse(data, size);
+ fuzz_blobmsg_parse(data, size);
+
+ return 0;
+}