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_sha256.h" 12 : 13 : #define BATCH_CNT 32UL /* must be at least 1UL */ 14 : 15 : static fd_sha256_batch_t batch_sha[1]; 16 : static uchar hash1 [ FD_SHA256_HASH_SZ ]; 17 : static uchar hash2 [ FD_SHA256_HASH_SZ ]; 18 : static uchar ref_hash [ FD_SHA256_HASH_SZ ]; 19 : static uchar hash_mem [ FD_SHA256_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 : fd_log_level_core_set(3); /* crash on warning log */ 32 18 : return 0; 33 18 : } 34 : 35 : int 36 : LLVMFuzzerTestOneInput( uchar const * fuzz_data, 37 : ulong fuzz_sz ) { 38 : // hash single message 39 : char const * msg = ( char const * ) fuzz_data; 40 : 41 : fd_sha256_t sha[1]; 42 : assert( fd_sha256_init( sha ) == sha ); 43 : assert( fd_sha256_append( sha, msg, fuzz_sz ) == sha ); 44 : assert( fd_sha256_fini( sha, hash1 ) == hash1 ); 45 : assert( fd_sha256_hash( fuzz_data, fuzz_sz, hash2 ) == hash2 ); 46 : assert( !memcmp( hash1, hash2, FD_SHA256_HASH_SZ ) ); 47 : 48 : // batch hashing 49 : if( fuzz_sz>=BATCH_CNT ) { 50 : FD_FUZZ_MUST_BE_COVERED; 51 : 52 : assert( fd_sha256_batch_init( batch_sha ) == batch_sha ); 53 : 54 : ulong entry_sz = fuzz_sz/BATCH_CNT; 55 : for( ulong batch_idx=0UL; batch_idx<BATCH_CNT; batch_idx++ ) { 56 : FD_FUZZ_MUST_BE_COVERED; 57 : hashes [ batch_idx ] = hash_mem + FD_SHA256_HASH_SZ*batch_idx; 58 : messages [ batch_idx ] = (char const *) fuzz_data + entry_sz*batch_idx; 59 : msg_sizes[ batch_idx ] = batch_idx<BATCH_CNT-1UL ? entry_sz : entry_sz+fuzz_sz%BATCH_CNT; 60 : assert( fd_sha256_batch_add( batch_sha, messages[ batch_idx ], msg_sizes[ batch_idx ], hashes[ batch_idx ] ) == batch_sha ); 61 : } 62 : 63 : assert( fd_sha256_batch_fini( batch_sha ) == batch_sha ); 64 : 65 : for( ulong batch_idx=0UL; batch_idx<BATCH_CNT; batch_idx++ ) { 66 : FD_FUZZ_MUST_BE_COVERED; 67 : assert( !memcmp( fd_sha256_hash( messages[ batch_idx ], msg_sizes[ batch_idx ], ref_hash ), hashes[ batch_idx ], FD_SHA256_HASH_SZ ) ); 68 : } 69 : } else { 70 : FD_FUZZ_MUST_BE_COVERED; 71 : } 72 : 73 : return 0; 74 : }