Line data Source code
1 : #if !FD_HAS_HOSTED 2 : #error "This target requires FD_HAS_HOSTED" 3 : #endif 4 : 5 : #include <assert.h> 6 : #include <stdio.h> 7 : #include <stdlib.h> 8 : 9 : #include "../../util/fd_util.h" 10 : #include "../../util/sanitize/fd_fuzz.h" 11 : #include "fd_siphash13.h" 12 : 13 : static fd_siphash13_t sip[1]; 14 : static fd_siphash13_t sip_fast[1]; 15 : 16 : int 17 : LLVMFuzzerInitialize( int * pargc, 18 12 : char *** pargv ) { 19 : /* Set up shell without signal handlers */ 20 12 : putenv( "FD_LOG_BACKTRACE=0" ); 21 12 : setenv( "FD_LOG_PATH", "", 0 ); 22 12 : fd_boot( pargc, pargv ); 23 12 : atexit( fd_halt ); 24 12 : fd_log_level_core_set(3); /* crash on warning log */ 25 12 : return 0; 26 12 : } 27 : 28 : #define FAST_HASH_CHUNK_SZ (32UL) /* MUST be a multiple of 8 */ 29 : 30 : struct fuzz_siphash13 31 : { 32 : ulong k0; 33 : ulong k1; 34 : uchar flex []; 35 : }; 36 : 37 : 38 : int 39 : LLVMFuzzerTestOneInput( uchar const * fuzz_data, 40 : ulong fuzz_data_sz ) { 41 : 42 : if (fuzz_data_sz < sizeof(struct fuzz_siphash13)) { 43 : return -1; 44 : }; 45 : 46 : // Clear buffers on the heap 47 : memset( sip, 0, sizeof(fd_siphash13_t) ); 48 : memset( sip_fast, 0, sizeof(fd_siphash13_t) ); 49 : 50 : 51 : struct fuzz_siphash13 * testcase = (struct fuzz_siphash13 *)(fuzz_data); 52 : ulong flex_sz = fuzz_data_sz - sizeof(struct fuzz_siphash13); 53 : 54 : assert( sip == fd_siphash13_init ( sip, testcase->k0, testcase->k1 ) ); 55 : assert( sip == fd_siphash13_append( sip, testcase->flex, flex_sz ) ); 56 : 57 : ulong hash = fd_siphash13_fini( sip ); 58 : assert( hash == fd_siphash13_hash( testcase->flex, flex_sz, testcase->k0, testcase->k1 ) ); 59 : 60 : // fuzz fast hashing 61 : assert( sip_fast == fd_siphash13_init( sip_fast, testcase->k0, testcase->k1 ) ); 62 : 63 : uchar const * data_chunk = testcase->flex; 64 : for( ulong rem = flex_sz/FAST_HASH_CHUNK_SZ; rem > 0; rem-- ) { 65 : assert( sip_fast == fd_siphash13_append_fast( sip_fast, data_chunk, FAST_HASH_CHUNK_SZ ) ); 66 : data_chunk += FAST_HASH_CHUNK_SZ; 67 : } 68 : assert( sip_fast == fd_siphash13_append( sip_fast, data_chunk, flex_sz%FAST_HASH_CHUNK_SZ ) ); 69 : ulong hash_fast = fd_siphash13_fini( sip_fast ); 70 : assert( hash == hash_fast ); 71 : 72 : FD_FUZZ_MUST_BE_COVERED; 73 : return 0; 74 : }