Bump fuzzlib hash size from 32 to 64

Also have json_fuzzer choose at most one of the BACKSLASH_X quirks.
This commit is contained in:
Nigel Tao committed 2020-08-10 22:56:47 +10:00
1 parent 28ef6a7862
commit c7b90256b3
5 files changed
+42 -28

No files matched your search

+18 -7
View File
@@ -27,14 +27,12 @@ intentional_segfault() {
*ptr = 0; *ptr = 0;
} }
const char* // // jenkins_hash_u32 implements
fuzz(wuffs_base__io_buffer* src, uint32_t hash); // https://en.wikipedia.org/wiki/Jenkins_hash_function
static uint32_t //
static const char* // jenkins_hash_u32(const uint8_t* data, size_t size) {
llvmFuzzerTestOneInput(const uint8_t* data, size_t size) {
// Hash input as per https://en.wikipedia.org/wiki/Jenkins_hash_function
size_t i = 0;
uint32_t hash = 0; uint32_t hash = 0;
size_t i = 0;
while (i != size) { while (i != size) {
hash += data[i++]; hash += data[i++];
hash += hash << 10; hash += hash << 10;
@@ -43,6 +41,19 @@ llvmFuzzerTestOneInput(const uint8_t* data, size_t size) {
hash += hash << 3; hash += hash << 3;
hash ^= hash >> 11; hash ^= hash >> 11;
hash += hash << 15; hash += hash << 15;
return hash;
}
const char* //
fuzz(wuffs_base__io_buffer* src, uint64_t hash);
static const char* //
llvmFuzzerTestOneInput(const uint8_t* data, size_t size) {
// Make a 64-bit hash out of two 32-bit hashes, each on half of the data.
size_t s2 = size / 2;
uint32_t hash0 = jenkins_hash_u32(data, s2);
uint32_t hash1 = jenkins_hash_u32(data + s2, size - s2);
uint64_t hash = (((uint64_t)hash0) << 32) | ((uint64_t)hash1);
wuffs_base__io_buffer src = ((wuffs_base__io_buffer){ wuffs_base__io_buffer src = ((wuffs_base__io_buffer){
.data = ((wuffs_base__slice_u8){ .data = ((wuffs_base__slice_u8){
+6 -6
View File
@@ -218,7 +218,7 @@ fuzz_one_token(wuffs_base__token t,
} }
uint64_t // uint64_t //
buffer_limit(uint32_t hash_6_bits, uint64_t min, uint64_t max) { buffer_limit(uint64_t hash_6_bits, uint64_t min, uint64_t max) {
uint64_t n; uint64_t n;
if (hash_6_bits < 0x20) { if (hash_6_bits < 0x20) {
n = min + hash_6_bits; n = min + hash_6_bits;
@@ -234,14 +234,14 @@ buffer_limit(uint32_t hash_6_bits, uint64_t min, uint64_t max) {
} }
const char* // const char* //
fuzz_complex(wuffs_base__io_buffer* full_src, uint32_t hash_24_bits) { fuzz_complex(wuffs_base__io_buffer* full_src, uint64_t hash_56_bits) {
uint64_t tok_limit = buffer_limit( uint64_t tok_limit = buffer_limit(
hash_24_bits & 0x3F, WUFFS_CBOR__DECODER_DST_TOKEN_BUFFER_LENGTH_MIN_INCL, hash_56_bits & 0x3F, WUFFS_CBOR__DECODER_DST_TOKEN_BUFFER_LENGTH_MIN_INCL,
TOK_BUFFER_ARRAY_SIZE); TOK_BUFFER_ARRAY_SIZE);
uint32_t hash_18_bits = hash_24_bits >> 6; uint64_t hash_50_bits = hash_56_bits >> 6;
uint64_t src_limit = uint64_t src_limit =
buffer_limit(hash_18_bits & 0x3F, buffer_limit(hash_50_bits & 0x3F,
WUFFS_CBOR__DECODER_SRC_IO_BUFFER_LENGTH_MIN_INCL, 4096); WUFFS_CBOR__DECODER_SRC_IO_BUFFER_LENGTH_MIN_INCL, 4096);
// ---- // ----
@@ -391,7 +391,7 @@ fuzz_simple(wuffs_base__io_buffer* full_src) {
} }
const char* // const char* //
fuzz(wuffs_base__io_buffer* full_src, uint32_t hash) { fuzz(wuffs_base__io_buffer* full_src, uint64_t hash) {
// Send 99.6% of inputs to fuzz_complex and the remainder to fuzz_simple. The // Send 99.6% of inputs to fuzz_complex and the remainder to fuzz_simple. The
// 0xA5 constant is arbitrary but non-zero. If the hash function maps the // 0xA5 constant is arbitrary but non-zero. If the hash function maps the
// empty input to 0, this still sends the empty input to fuzz_complex. // empty input to 0, this still sends the empty input to fuzz_complex.
+1 -1
View File
@@ -61,7 +61,7 @@ It should print "PASS", amongst other information, and exit(0).
#include "../fuzzlib/fuzzlib.c" #include "../fuzzlib/fuzzlib.c"
const char* // const char* //
fuzz(wuffs_base__io_buffer* src, uint32_t hash) { fuzz(wuffs_base__io_buffer* src, uint64_t hash) {
const char* ret = NULL; const char* ret = NULL;
wuffs_base__slice_u8 pixbuf = ((wuffs_base__slice_u8){}); wuffs_base__slice_u8 pixbuf = ((wuffs_base__slice_u8){});
wuffs_base__slice_u8 workbuf = ((wuffs_base__slice_u8){}); wuffs_base__slice_u8 workbuf = ((wuffs_base__slice_u8){});
+16 -13
View File
@@ -209,7 +209,7 @@ fuzz_one_token(wuffs_base__token t,
} }
uint64_t // uint64_t //
buffer_limit(uint32_t hash_6_bits, uint64_t min, uint64_t max) { buffer_limit(uint64_t hash_6_bits, uint64_t min, uint64_t max) {
uint64_t n; uint64_t n;
if (hash_6_bits < 0x20) { if (hash_6_bits < 0x20) {
n = min + hash_6_bits; n = min + hash_6_bits;
@@ -224,7 +224,7 @@ buffer_limit(uint32_t hash_6_bits, uint64_t min, uint64_t max) {
return n; return n;
} }
void set_quirks(wuffs_json__decoder* dec, uint32_t hash_12_bits) { void set_quirks(wuffs_json__decoder* dec, uint64_t hash_44_bits) {
uint32_t quirks[] = { uint32_t quirks[] = {
WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_A, WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_A,
WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_CAPITAL_U, WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_CAPITAL_U,
@@ -232,8 +232,8 @@ void set_quirks(wuffs_json__decoder* dec, uint32_t hash_12_bits) {
WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_QUESTION_MARK, WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_QUESTION_MARK,
WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_SINGLE_QUOTE, WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_SINGLE_QUOTE,
WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_V, WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_V,
WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_X_AS_BYTES, (hash_44_bits & 1) ? WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_X_AS_BYTES
WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_X_AS_CODE_POINTS, : WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_X_AS_CODE_POINTS,
WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_ZERO, WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_ZERO,
WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK, WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK,
WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE, WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE,
@@ -246,26 +246,29 @@ void set_quirks(wuffs_json__decoder* dec, uint32_t hash_12_bits) {
0, 0,
}; };
// Shift off a bit for the ETC_BACKSLASH_X_ETC choice.
uint64_t hash_43_bits = hash_44_bits >> 1;
uint32_t i; uint32_t i;
for (i = 0; quirks[i]; i++) { for (i = 0; quirks[i]; i++) {
uint32_t bit = 1 << (i % 12); uint64_t bit = 1 << (i % 43);
if (hash_12_bits & bit) { if (hash_43_bits & bit) {
wuffs_json__decoder__set_quirk_enabled(dec, quirks[i], true); wuffs_json__decoder__set_quirk_enabled(dec, quirks[i], true);
} }
} }
} }
const char* // const char* //
fuzz_complex(wuffs_base__io_buffer* full_src, uint32_t hash_24_bits) { fuzz_complex(wuffs_base__io_buffer* full_src, uint64_t hash_56_bits) {
uint64_t tok_limit = buffer_limit( uint64_t tok_limit = buffer_limit(
hash_24_bits & 0x3F, WUFFS_JSON__DECODER_DST_TOKEN_BUFFER_LENGTH_MIN_INCL, hash_56_bits & 0x3F, WUFFS_JSON__DECODER_DST_TOKEN_BUFFER_LENGTH_MIN_INCL,
TOK_BUFFER_ARRAY_SIZE); TOK_BUFFER_ARRAY_SIZE);
uint32_t hash_18_bits = hash_24_bits >> 6; uint64_t hash_50_bits = hash_56_bits >> 6;
uint64_t src_limit = uint64_t src_limit =
buffer_limit(hash_18_bits & 0x3F, buffer_limit(hash_50_bits & 0x3F,
WUFFS_JSON__DECODER_SRC_IO_BUFFER_LENGTH_MIN_INCL, 4096); WUFFS_JSON__DECODER_SRC_IO_BUFFER_LENGTH_MIN_INCL, 4096);
uint32_t hash_12_bits = hash_18_bits >> 6; uint64_t hash_44_bits = hash_50_bits >> 6;
// ---- // ----
@@ -276,7 +279,7 @@ fuzz_complex(wuffs_base__io_buffer* full_src, uint32_t hash_24_bits) {
if (!wuffs_base__status__is_ok(&status)) { if (!wuffs_base__status__is_ok(&status)) {
return wuffs_base__status__message(&status); return wuffs_base__status__message(&status);
} }
set_quirks(&dec, hash_12_bits); set_quirks(&dec, hash_44_bits);
wuffs_base__token tok_array[TOK_BUFFER_ARRAY_SIZE]; wuffs_base__token tok_array[TOK_BUFFER_ARRAY_SIZE];
wuffs_base__token_buffer tok = ((wuffs_base__token_buffer){ wuffs_base__token_buffer tok = ((wuffs_base__token_buffer){
@@ -415,7 +418,7 @@ fuzz_simple(wuffs_base__io_buffer* full_src) {
} }
const char* // const char* //
fuzz(wuffs_base__io_buffer* full_src, uint32_t hash) { fuzz(wuffs_base__io_buffer* full_src, uint64_t hash) {
// Send 99.6% of inputs to fuzz_complex and the remainder to fuzz_simple. The // Send 99.6% of inputs to fuzz_complex and the remainder to fuzz_simple. The
// 0xA5 constant is arbitrary but non-zero. If the hash function maps the // 0xA5 constant is arbitrary but non-zero. If the hash function maps the
// empty input to 0, this still sends the empty input to fuzz_complex. // empty input to 0, this still sends the empty input to fuzz_complex.
+1 -1
View File
@@ -75,7 +75,7 @@ uint8_t g_work_buffer_array[1];
#endif #endif
const char* // const char* //
fuzz(wuffs_base__io_buffer* src, uint32_t hash) { fuzz(wuffs_base__io_buffer* src, uint64_t hash) {
wuffs_zlib__decoder dec; wuffs_zlib__decoder dec;
wuffs_base__status status = wuffs_zlib__decoder__initialize( wuffs_base__status status = wuffs_zlib__decoder__initialize(
&dec, sizeof dec, WUFFS_VERSION, &dec, sizeof dec, WUFFS_VERSION,