Skip to content

Commit 60ddc8f

Browse files
JSON fuzzer: use options for handling invalid UTF-8 and big strings (#23877)
Previously, the fuzzer only used the default options (`0`) and the `PHP_JSON_OBJECT_AS_ARRAY` option (`1`). After running the fuzzer on its existing corpus with `-reduce_inputs=0`, `-runs=100000`, and `-seed=1`, it failed to reach the code paths for handling big integers and strings, or for dealing with invalid UTF8, within the allotted 100,000 runs. Those 100,000 runs resulted in coverage of roughly 2,020 code blocks or edges, and roughly 5,400 "features". Expand the fuzzer to also run with options that include the `PHP_JSON_BIGINT_AS_STRING`, `PHP_JSON_INVALID_UTF8_IGNORE`, and `PHP_JSON_INVALID_UTF8_SUBSTITUTE` flags. The two flags for handling UTF-8 are not applied used together, since invalid UTF-8 can only be handled one way, but other than that all combinations of these flags and `PHP_JSON_OBJECT_AS_ARRAY` are now tested. Repeating the same fuzzing as earlier with the expanded options results in roughly 2,150 code blocks or edges, and roughly 5,700 "features", being covered.
1 parent 6f49ae8 commit 60ddc8f

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

‎sapi/fuzzer/fuzzer-json.c‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <stdlib.h>
2525

2626
#include "fuzzer-sapi.h"
27+
#include "ext/json/php_json.h"
2728
#include "ext/json/php_json_parser.h"
2829

2930
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
@@ -36,10 +37,25 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
3637
memcpy(data, Data, Size);
3738
data[Size] = '\0';
3839

39-
for (int option = 0; option <=1; ++option) {
40+
int options[12] = {
41+
0,
42+
PHP_JSON_OBJECT_AS_ARRAY,
43+
PHP_JSON_OBJECT_AS_ARRAY | PHP_JSON_BIGINT_AS_STRING,
44+
PHP_JSON_OBJECT_AS_ARRAY | PHP_JSON_BIGINT_AS_STRING | PHP_JSON_INVALID_UTF8_IGNORE,
45+
PHP_JSON_OBJECT_AS_ARRAY | PHP_JSON_BIGINT_AS_STRING | PHP_JSON_INVALID_UTF8_SUBSTITUTE,
46+
PHP_JSON_OBJECT_AS_ARRAY | PHP_JSON_INVALID_UTF8_IGNORE,
47+
PHP_JSON_OBJECT_AS_ARRAY | PHP_JSON_INVALID_UTF8_SUBSTITUTE,
48+
PHP_JSON_BIGINT_AS_STRING,
49+
PHP_JSON_BIGINT_AS_STRING | PHP_JSON_INVALID_UTF8_IGNORE,
50+
PHP_JSON_BIGINT_AS_STRING | PHP_JSON_INVALID_UTF8_SUBSTITUTE,
51+
PHP_JSON_INVALID_UTF8_IGNORE,
52+
PHP_JSON_INVALID_UTF8_SUBSTITUTE
53+
};
54+
55+
for (int index = 0; index < 12; ++index) {
4056
zval result;
4157
php_json_parser parser;
42-
php_json_parser_init(&parser, &result, data, Size, option, 10);
58+
php_json_parser_init(&parser, &result, data, Size, options[index], 10);
4359
if (php_json_yyparse(&parser) == SUCCESS) {
4460
zval_ptr_dtor(&result);
4561
}

0 commit comments

Comments
 (0)