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_sha512.h" 12 : 13 : #define BATCH_CNT 32UL /* must be at least 1UL */ 14 : 15 : static fd_sha512_batch_t batch_sha[1]; 16 : static uchar hash1 [ FD_SHA512_HASH_SZ ]; 17 : static uchar hash2 [ FD_SHA512_HASH_SZ ]; 18 : static uchar ref_hash [ FD_SHA512_HASH_SZ ]; 19 : static uchar hash_mem [ FD_SHA512_HASH_SZ * BATCH_CNT ]; 20 : static uchar * hashes [ BATCH_CNT ]; 21 : static char const * messages [ BATCH_CNT ]; 22 : static ulong msg_sizes[ BATCH_CNT ]; 23 : 24 : int 25 : LLVMFuzzerInitialize( int * argc, 26 18 : char *** argv ) { 27 : /* Set up shell without signal handlers */ 28 18 : putenv( "FD_LOG_BACKTRACE=0" ); 29 18 : fd_boot( argc, argv ); 30 18 : atexit( fd_halt ); 31 18 : return 0; 32 18 : } 33 : 34 : int 35 : LLVMFuzzerTestOneInput( uchar const * fuzz_data, 36 : ulong fuzz_sz ) { 37 : // hash single message 38 : char const * msg = ( char const * ) fuzz_data; 39 : 40 : fd_sha512_t sha[1]; 41 : assert( fd_sha512_init( sha ) == sha ); 42 : assert( fd_sha512_append( sha, msg, fuzz_sz ) == sha ); 43 : assert( fd_sha512_fini( sha, hash1 ) == hash1 ); 44 : assert( fd_sha512_hash( fuzz_data, fuzz_sz, hash2 ) == hash2 ); 45 : assert( !memcmp( hash1, hash2, FD_SHA512_HASH_SZ ) ); 46 : 47 : // batch hashing 48 : if( fuzz_sz>=BATCH_CNT ) { 49 : FD_FUZZ_MUST_BE_COVERED; 50 : 51 : assert( fd_sha512_batch_init( batch_sha ) == batch_sha ); 52 : 53 : ulong entry_sz = fuzz_sz/BATCH_CNT; 54 : for( ulong batch_idx=0UL; batch_idx<BATCH_CNT; batch_idx++ ) { 55 : FD_FUZZ_MUST_BE_COVERED; 56 : hashes [ batch_idx ] = hash_mem + FD_SHA512_HASH_SZ*batch_idx; 57 : messages [ batch_idx ] = (char const *) fuzz_data + entry_sz*batch_idx; 58 : msg_sizes[ batch_idx ] = batch_idx<BATCH_CNT-1UL ? entry_sz : entry_sz+fuzz_sz%BATCH_CNT; 59 : assert( fd_sha512_batch_add( batch_sha, messages[ batch_idx ], msg_sizes[ batch_idx ], hashes[ batch_idx ] ) == batch_sha ); 60 : } 61 : 62 : assert( fd_sha512_batch_fini( batch_sha ) == batch_sha ); 63 : 64 : for( ulong batch_idx=0UL; batch_idx<BATCH_CNT; batch_idx++ ) { 65 : FD_FUZZ_MUST_BE_COVERED; 66 : assert( !memcmp( fd_sha512_hash( messages[ batch_idx ], msg_sizes[ batch_idx ], ref_hash ), hashes[ batch_idx ], FD_SHA512_HASH_SZ ) ); 67 : } 68 : } 69 : 70 : FD_FUZZ_MUST_BE_COVERED; 71 : return 0; 72 : }