Skip to content
Myr

Extend fuzzing set up

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Adds a parse function ins `jv_parse.c` that enables parsing using custom flags for the parser. This is then used by two fuzzers added as well. This is to make sure fuzzing hits various code parts currently not fuzzed, e.g. `stream_token`: https://storage.googleapis.com/oss-fuzz-coverage/jq/reports/20231125/linux/src/jq/src/jv_parse.c.html#L241

authored by David Korczynski · committed by Emanuele Torre · parent 88f01a7 · Browse files · 4 files+94 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: David Korczynski <david@adalogics.com>

Diff stats

4 files+94 added−0 removed

  • tests:+50 added−0 removed
  • other:+44 added−0 removed

4 files+94 added−0 removed

modified:src/jv_parse.c+43 added
899 unmodified lines
900901jv jv_parse(const char* string) {902 return jv_parse_sized(string, strlen(string));903}904905jv jv_parse_sized_custom_flags(const char* string, int length, int flags) {906 struct jv_parser parser;907 parser_init(&parser, flags);908 jv_parser_set_buf(&parser, string, length, 0);909 jv value = jv_parser_next(&parser);910 if (jv_is_valid(value)) {911 jv next = jv_parser_next(&parser);912 if (jv_is_valid(next)) {913 // multiple JSON values, we only wanted one914 jv_free(value);915 jv_free(next);916 value = jv_invalid_with_msg(jv_string("Unexpected extra JSON values"));917 } else if (jv_invalid_has_msg(jv_copy(next))) {918 // parser error after the first JSON value919 jv_free(value);920 value = next;921 } else {922 // a single valid JSON value923 jv_free(next);924 }925 } else if (jv_invalid_has_msg(jv_copy(value))) {926 // parse error, we'll return it927 } else {928 // no value at all929 jv_free(value);930 value = jv_invalid_with_msg(jv_string("Expected JSON value"));931 }932 parser_free(&parser);933934 if (!jv_is_valid(value) && jv_invalid_has_msg(jv_copy(value))) {935 jv msg = jv_invalid_get_msg(value);936 value = jv_invalid_with_msg(jv_string_fmt("%s (while parsing '%s')",937 jv_string_value(msg),938 string));939 jv_free(msg);940 }941 return value;942}943944jv jv_parse_custom_flags(const char* string, int flags) {945 return jv_parse_sized_custom_flags(string, strlen(string), flags);946}
modified:src/jv.h+1 added
232 unmodified lines
233};234235jv jv_parse(const char* string);236jv jv_parse_sized(const char* string, int length);237jv jv_parse_custom_flags(const char* string, int flags);238239typedef void (*jv_nomem_handler_f)(void *);240void jv_nomem_handler(jv_nomem_handler_f, void *);241
45 unmodified lines
added:tests/jq_fuzz_parse_extended.c+29 added
1#include <stdint.h>2#include <stdlib.h>3#include <string.h>45#include "jv.h"67int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {8 if (size < 4) {9 return 0;10 }1112 int fuzz_flags = *(int*)data;13 data += 4;14 size -= 4;1516 // Creat null-terminated string17 char *null_terminated = (char *)malloc(size + 1);18 memcpy(null_terminated, (char *)data, size);19 null_terminated[size] = '\0';2021 // Fuzzer entrypoint22 jv res = jv_parse_custom_flags(null_terminated, fuzz_flags);23 jv_free(res);24 25 // Free the null-terminated string26 free(null_terminated);2728 return 0;29}
added:tests/jq_fuzz_parse_stream.c+21 added
1#include <stdint.h>2#include <stdlib.h>3#include <string.h>45#include "jv.h"67int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {8 // Creat null-terminated string9 char *null_terminated = (char *)malloc(size + 1);10 memcpy(null_terminated, (char *)data, size);11 null_terminated[size] = '\0';1213 // Fuzzer entrypoint14 jv res = jv_parse_custom_flags(null_terminated, JV_PARSE_STREAMING);15 jv_free(res);16 17 // Free the null-terminated string18 free(null_terminated);1920 return 0;21}