LCOV - code coverage report
Current view: top level - flamenco/types - fuzz_types_decode.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 12 74 16.2 %
Date: 2025-03-20 12:08:36 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_footprint", type);
      34           0 :   t->decode_footprint_fun =  dlsym(RTLD_DEFAULT, fp);
      35             : 
      36           0 :   sprintf(fp, "%s_decode", type);
      37           0 :   t->decode_fun =  dlsym(RTLD_DEFAULT, fp);
      38             : 
      39           0 :   sprintf(fp, "%s_walk", type);
      40           0 :   t->walk_fun =  dlsym(RTLD_DEFAULT, fp);
      41             : 
      42           0 :   sprintf(fp, "%s_encode", type);
      43           0 :   t->encode_fun =  dlsym(RTLD_DEFAULT, fp);
      44             : 
      45           0 :   sprintf(fp, "%s_destroy", type);
      46           0 :   t->destroy_fun =  dlsym(RTLD_DEFAULT, fp);
      47             : 
      48           0 :   sprintf(fp, "%s_size", type);
      49           0 :   t->size_fun =  dlsym(RTLD_DEFAULT, fp);
      50             : 
      51           0 :   if ((  t->footprint_fun == NULL) ||
      52           0 :       (  t->align_fun == NULL) ||
      53           0 :       (  t->new_fun == NULL) ||
      54           0 :       (  t->decode_footprint_fun == NULL) ||
      55           0 :       (  t->decode_fun == NULL) ||
      56           0 :       (  t->walk_fun == NULL) ||
      57           0 :       (  t->encode_fun == NULL) ||
      58           0 :       (  t->destroy_fun == NULL) ||
      59           0 :       (  t->size_fun == NULL))
      60           0 :     return -1;
      61           0 :   return 0;
      62           0 : }
      63             : 
      64             : static inline void
      65           0 : fd_scratch_detach_null( void ) {
      66           0 :   fd_scratch_detach( NULL );
      67           0 : }
      68             : 
      69             : int
      70             : LLVMFuzzerInitialize( int  *   argc,
      71          15 :                       char *** argv ) {
      72             :   /* Set up shell without signal handlers */
      73          15 :   putenv( "FD_LOG_BACKTRACE=0" );
      74          15 :   fd_boot( argc, argv );
      75          15 :   fd_flamenco_boot( argc, argv );
      76             : 
      77             :   /* Set up scrath memory */
      78          15 :   static uchar scratch_mem [ 1UL<<30 ];  /* 1 GB */
      79          15 :   static ulong scratch_fmem[ 4UL ] __attribute((aligned(FD_SCRATCH_FMEM_ALIGN)));
      80          15 :   fd_scratch_attach( scratch_mem, scratch_fmem, 1UL<<30, 4UL );
      81             : 
      82          15 :   atexit( fd_halt );
      83          15 :   atexit( fd_flamenco_halt );
      84          15 :   atexit( fd_scratch_detach_null );
      85          15 :   return 0;
      86          15 : }
      87             : 
      88             : static void
      89             : fd_decode_fuzz_data( char  const * type_name,
      90             :                      uchar const * data,
      91           0 :                      ulong         size ) {
      92             : 
      93           0 :   FD_SCRATCH_SCOPE_BEGIN {
      94             : 
      95           0 :     fd_types_funcs_t type_meta;
      96           0 :     if( fd_flamenco_type_lookup( type_name, &type_meta ) != 0 ) {
      97           0 :       FD_LOG_ERR (( "Failed to lookup type %s", type_name ));
      98           0 :     }
      99             : 
     100           0 :     fd_bincode_decode_ctx_t decode_ctx = {
     101           0 :       .data    = data,
     102           0 :       .dataend = data + size,
     103           0 :     };
     104             : 
     105           0 :     ulong total_sz = 0UL;
     106           0 :     int   err      = type_meta.decode_footprint_fun( &decode_ctx, &total_sz );
     107           0 :     __asm__ volatile( "" : "+m,r"(err) : : "memory" ); /* prevent optimization */
     108             : 
     109           0 :     if( err ) {
     110           0 :       return;
     111           0 :     }
     112             : 
     113           0 :     void * decoded = fd_scratch_alloc( type_meta.align_fun(), total_sz );
     114           0 :     if( FD_UNLIKELY( decoded == NULL ) ) {
     115           0 :       FD_LOG_ERR (( "Failed to alloc memory for decoded type %s", type_name ));
     116           0 :     }
     117             : 
     118           0 :     type_meta.decode_fun( decoded, &decode_ctx );
     119             : 
     120           0 :   } FD_SCRATCH_SCOPE_END;
     121           0 : }
     122             : 
     123             : #include "fd_type_names.c"
     124             : 
     125             : int
     126             : LLVMFuzzerTestOneInput( uchar const * data,
     127             :                         ulong         size ) {
     128             :   if ( FD_UNLIKELY( size == 0 ) ) {
     129             :     return 0;
     130             :   }
     131             : 
     132             :   assert( FD_TYPE_NAME_COUNT < 256 );
     133             :   ulong i = data[0] % FD_TYPE_NAME_COUNT;
     134             :   data = data + 1;
     135             :   size = size - 1;
     136             : 
     137             :   /* fd_pubkey is a #define alias for fd_hash.  It is therefore already
     138             :      fuzzed. Furthermore, dlsym will not be able to find a #define. */
     139             :   if ( FD_UNLIKELY( strcmp( fd_type_names[i], "fd_pubkey" ) == 0 ) ) {
     140             :     return -1;
     141             :   }
     142             : 
     143             :   fd_decode_fuzz_data( fd_type_names[i], data, size );
     144             : 
     145             :   return 0;
     146             : }

Generated by: LCOV version 1.14