LCOV - code coverage report
Current view: top level - flamenco/types - fuzz_types_decode.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 12 67 17.9 %
Date: 2024-11-13 11:58:15 Functions: 1 5 20.0 %

          Line data    Source code
       1             : #if !FD_HAS_HOSTED
       2             : #error "This target requires FD_HAS_HOSTED"
       3             : #endif
       4             : 
       5             : #define _GNU_SOURCE
       6             : #include <dlfcn.h>
       7             : 
       8             : #include <stdio.h>
       9             : #include <stdlib.h>
      10             : #include <assert.h>
      11             : 
      12             : #include "fd_types_meta.h"
      13             : #include "../fd_flamenco.h"
      14             : #include "fd_types.h"
      15             : 
      16           0 : ulong foo_lkasjdf( void ) {
      17           0 :   return fd_vote_state_versioned_footprint();
      18           0 : }
      19             : 
      20           0 : int fd_flamenco_type_lookup(const char *type, fd_types_funcs_t * t) {
      21           0 :   char fp[255];
      22             : 
      23           0 : #pragma GCC diagnostic ignored "-Wpedantic"
      24           0 :   sprintf(fp, "%s_footprint", type);
      25           0 :   t->footprint_fun = dlsym(RTLD_DEFAULT, fp);
      26             : 
      27           0 :   sprintf(fp, "%s_align", type);
      28           0 :   t->align_fun =  dlsym(RTLD_DEFAULT, fp);
      29             : 
      30           0 :   sprintf(fp, "%s_new", type);
      31           0 :   t->new_fun =  dlsym(RTLD_DEFAULT, fp);
      32             : 
      33           0 :   sprintf(fp, "%s_decode", type);
      34           0 :   t->decode_fun =  dlsym(RTLD_DEFAULT, fp);
      35             : 
      36           0 :   sprintf(fp, "%s_walk", type);
      37           0 :   t->walk_fun =  dlsym(RTLD_DEFAULT, fp);
      38             : 
      39           0 :   sprintf(fp, "%s_encode", type);
      40           0 :   t->encode_fun =  dlsym(RTLD_DEFAULT, fp);
      41             : 
      42           0 :   sprintf(fp, "%s_destroy", type);
      43           0 :   t->destroy_fun =  dlsym(RTLD_DEFAULT, fp);
      44             : 
      45           0 :   sprintf(fp, "%s_size", type);
      46           0 :   t->size_fun =  dlsym(RTLD_DEFAULT, fp);
      47             : 
      48           0 :   if ((  t->footprint_fun == NULL) ||
      49           0 :       (  t->align_fun == NULL) ||
      50           0 :       (  t->new_fun == NULL) ||
      51           0 :       (  t->decode_fun == NULL) ||
      52           0 :       (  t->walk_fun == NULL) ||
      53           0 :       (  t->encode_fun == NULL) ||
      54           0 :       (  t->destroy_fun == NULL) ||
      55           0 :       (  t->size_fun == NULL))
      56           0 :     return -1;
      57           0 :   return 0;
      58           0 : }
      59             : 
      60             : static inline void
      61           0 : fd_scratch_detach_null( void ) {
      62           0 :   fd_scratch_detach( NULL );
      63           0 : }
      64             : 
      65             : int
      66             : LLVMFuzzerInitialize( int  *   argc,
      67          18 :                       char *** argv ) {
      68             :   /* Set up shell without signal handlers */
      69          18 :   putenv( "FD_LOG_BACKTRACE=0" );
      70          18 :   fd_boot( argc, argv );
      71          18 :   fd_flamenco_boot( argc, argv );
      72             : 
      73             :   /* Set up scrath memory */
      74          18 :   static uchar scratch_mem [ 1UL<<30 ];  /* 1 GB */
      75          18 :   static ulong scratch_fmem[ 4UL ] __attribute((aligned(FD_SCRATCH_FMEM_ALIGN)));
      76          18 :   fd_scratch_attach( scratch_mem, scratch_fmem, 1UL<<30, 4UL );
      77             : 
      78          18 :   atexit( fd_halt );
      79          18 :   atexit( fd_flamenco_halt );
      80          18 :   atexit( fd_scratch_detach_null );
      81          18 :   return 0;
      82          18 : }
      83             : 
      84             : static void
      85             : fd_decode_fuzz_data( char  const * type_name,
      86             :                      uchar const * data,
      87           0 :                      ulong         size ) {
      88             : 
      89           0 :   FD_SCRATCH_SCOPE_BEGIN {
      90           0 :     fd_types_funcs_t type_meta;
      91           0 :     if( fd_flamenco_type_lookup( type_name, &type_meta ) != 0 ) {
      92           0 :       FD_LOG_ERR (( "Failed to lookup type %s", type_name ));
      93           0 :     }
      94             : 
      95           0 :     fd_bincode_decode_ctx_t decode_ctx = {
      96           0 :       .data    = data,
      97           0 :       .dataend = data + size,
      98           0 :       .valloc  = fd_scratch_virtual()
      99           0 :     };
     100           0 :     void * decoded = fd_scratch_alloc( type_meta.align_fun(), type_meta.footprint_fun() );
     101           0 :     if( FD_UNLIKELY( decoded == NULL ) ) {
     102           0 :       FD_LOG_ERR (( "Failed to alloc memory for decoded type %s", type_name ));
     103           0 :     }
     104           0 :     int err = type_meta.decode_fun( decoded, &decode_ctx );
     105           0 :     __asm__ volatile( "" : "+m,r"(err) : : "memory" ); /* prevent optimization */
     106             : 
     107           0 :   } FD_SCRATCH_SCOPE_END;
     108           0 : }
     109             : 
     110             : #include "fd_type_names.c"
     111             : 
     112             : int
     113             : LLVMFuzzerTestOneInput( uchar const * data,
     114             :                         ulong         size ) {
     115             :   if ( FD_UNLIKELY( size == 0 ) ) {
     116             :     return 0;
     117             :   }
     118             : 
     119             :   assert( FD_TYPE_NAME_COUNT < 256 );
     120             :   ulong i = data[0] % FD_TYPE_NAME_COUNT;
     121             :   data = data + 1;
     122             :   size = size - 1;
     123             : 
     124             :   /* fd_pubkey is a #define alias for fd_hash.  It is therefore already
     125             :      fuzzed. Furthermore, dlsym will not be able to find a #define. */
     126             :   if ( FD_UNLIKELY( strcmp( fd_type_names[i], "fd_pubkey" ) == 0 ) ) {
     127             :     return -1;
     128             :   }
     129             : 
     130             :   fd_decode_fuzz_data( fd_type_names[i], data, size );
     131             : 
     132             :   return 0;
     133             : }

Generated by: LCOV version 1.14