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_hex.h" 12 : 13 : int 14 : LLVMFuzzerInitialize( int * argc, 15 18 : char *** argv ) { 16 : /* Set up shell without signal handlers */ 17 18 : putenv( "FD_LOG_BACKTRACE=0" ); 18 18 : fd_boot( argc, argv ); 19 18 : atexit( fd_halt ); 20 18 : fd_log_level_core_set(3); /* crash on warning log */ 21 18 : return 0; 22 18 : } 23 : 24 : /* checks if given encoding of sz chars is valid, sz must be even 25 : returns sz on success, index of first invalid char on failure */ 26 : static inline ulong 27 0 : check_hex_encoding( char const * enc, ulong sz ) { 28 0 : ulong i; 29 0 : for( i=0; i<sz; i++ ) { 30 0 : char c = enc[i]; 31 0 : if( c>='0' && c<='9' ) continue; 32 0 : if( c>='a' && c<='f' ) continue; 33 0 : if( c>='A' && c<='F' ) continue; 34 0 : return i; 35 0 : } 36 0 : return sz; 37 0 : } 38 : 39 : #define MAX_DATA_SZ 4096UL 40 : #define MAX_DECODED_SZ ( MAX_DATA_SZ / 2UL ) 41 : 42 : int 43 : LLVMFuzzerTestOneInput( uchar const * data, 44 : ulong size ) { 45 : if( FD_UNLIKELY( size > MAX_DATA_SZ ) ) return -1; 46 : char const * encoded = ( char const * ) data; 47 : 48 : size = size & ~1UL; /* ignore last char of encoding if size is odd */ 49 : 50 : uchar decoded[ MAX_DECODED_SZ ]; 51 : ulong decoded_sz = fd_hex_decode( decoded, encoded, size/2UL ); 52 : 53 : assert( decoded_sz == ( check_hex_encoding( encoded, size )/2UL ) ); 54 : 55 : FD_FUZZ_MUST_BE_COVERED; 56 : return 0; 57 : }