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 : return 0; 21 18 : } 22 : 23 : /* checks if given encoding of sz chars is valid, sz must be even 24 : returns sz on success, index of first invalid char on failure */ 25 : static inline ulong 26 0 : check_hex_encoding( char const * enc, ulong sz ) { 27 0 : ulong i; 28 0 : for( i=0; i<sz; i++ ) { 29 0 : char c = enc[i]; 30 0 : if( c>='0' && c<='9' ) continue; 31 0 : if( c>='a' && c<='f' ) continue; 32 0 : if( c>='A' && c<='F' ) continue; 33 0 : return i; 34 0 : } 35 0 : return sz; 36 0 : } 37 : 38 : #define MAX_DATA_SZ 4096UL 39 : #define MAX_DECODED_SZ ( MAX_DATA_SZ / 2UL ) 40 : 41 : int 42 : LLVMFuzzerTestOneInput( uchar const * data, 43 : ulong size ) { 44 : if( FD_UNLIKELY( size > MAX_DATA_SZ ) ) return -1; 45 : char const * encoded = ( char const * ) data; 46 : 47 : size = size & ~1UL; /* ignore last char of encoding if size is odd */ 48 : 49 : uchar decoded[ MAX_DECODED_SZ ]; 50 : ulong decoded_sz = fd_hex_decode( decoded, encoded, size/2UL ); 51 : 52 : assert( decoded_sz == ( check_hex_encoding( encoded, size )/2UL ) ); 53 : 54 : FD_FUZZ_MUST_BE_COVERED; 55 : return 0; 56 : }