LCOV - code coverage report
Current view: top level - flamenco/runtime/tests/harness - fd_vm_harness.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 487 0.0 %
Date: 2025-07-01 05:00:49 Functions: 0 5 0.0 %

          Line data    Source code
       1             : #include "fd_vm_harness.h"
       2             : 
       3             : static int
       4             : fd_runtime_fuzz_vm_syscall_noop( void * _vm,
       5             :                                  ulong arg0,
       6             :                                  ulong arg1,
       7             :                                  ulong arg2,
       8             :                                  ulong arg3,
       9             :                                  ulong arg4,
      10           0 :                                  ulong* _ret){
      11             :   /* TODO: have input message determine CUs to deduct?
      12             :   fd_vm_t * vm = (fd_vm_t *) _vm;
      13             :   vm->cu = vm->cu - 5;
      14             :   */
      15             : 
      16           0 :   (void) _vm;
      17           0 :   (void) arg0;
      18           0 :   (void) arg1;
      19           0 :   (void) arg2;
      20           0 :   (void) arg3;
      21           0 :   (void) arg4;
      22           0 :   *_ret = 0;
      23           0 :   return 0;
      24           0 : }
      25             : 
      26             : static fd_sbpf_syscalls_t *
      27             : fd_runtime_fuzz_lookup_syscall_func( fd_sbpf_syscalls_t * syscalls,
      28             :                                      const char *         syscall_name,
      29           0 :                                      size_t               len) {
      30           0 :   ulong i;
      31             : 
      32           0 :   if (!syscall_name) return NULL;
      33             : 
      34           0 :   for (i = 0; i < fd_sbpf_syscalls_slot_cnt(); ++i) {
      35           0 :     if (!fd_sbpf_syscalls_key_inval(syscalls[i].key) && syscalls[i].name && strlen(syscalls[i].name) == len) {
      36           0 :       if (!memcmp(syscalls[i].name, syscall_name, len)) {
      37           0 :         return syscalls + i;
      38           0 :       }
      39           0 :     }
      40           0 :   }
      41             : 
      42           0 :   return NULL;
      43           0 : }
      44             : 
      45             : static ulong
      46             : fd_runtime_fuzz_load_from_vm_input_regions( fd_vm_input_region_t const *        input,
      47             :                                             uint                                input_count,
      48             :                                             fd_exec_test_input_data_region_t ** output,
      49             :                                             pb_size_t *                         output_count,
      50             :                                             void *                              output_buf,
      51           0 :                                             ulong                               output_bufsz ) {
      52             :   /* pre-flight checks on output buffer size*/
      53           0 :   ulong input_regions_total_sz = 0;
      54           0 :   for( ulong i=0; i<input_count; i++ ) {
      55           0 :     input_regions_total_sz += input[i].region_sz;
      56           0 :   }
      57             : 
      58           0 :   if( FD_UNLIKELY(   input_regions_total_sz == 0
      59           0 :                   || output_bufsz < input_regions_total_sz ) ) {
      60           0 :     *output = NULL;
      61           0 :     *output_count = 0;
      62           0 :     return 0;
      63           0 :   }
      64             : 
      65           0 :   FD_SCRATCH_ALLOC_INIT( l, output_buf );
      66           0 :   *output = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_input_data_region_t),
      67           0 :                                       input_count * sizeof (fd_exec_test_input_data_region_t) );
      68           0 :   FD_TEST( *output );
      69           0 :   *output_count = input_count;
      70             : 
      71           0 :   for( ulong i=0; i<input_count; i++ ) {
      72           0 :     fd_vm_input_region_t const * vm_region = &input[i];
      73           0 :     fd_exec_test_input_data_region_t * out_region = &(*output)[i];
      74           0 :     out_region->is_writable = vm_region->is_writable;
      75           0 :     out_region->offset = vm_region->vaddr_offset;
      76             : 
      77           0 :     out_region->content = FD_SCRATCH_ALLOC_APPEND( l, alignof(pb_bytes_array_t),
      78           0 :                                                PB_BYTES_ARRAY_T_ALLOCSIZE(vm_region->region_sz) );
      79           0 :     FD_TEST( out_region->content );
      80           0 :     out_region->content->size = vm_region->region_sz;
      81           0 :     fd_memcpy( out_region->content->bytes, (void *)vm_region->haddr, vm_region->region_sz );
      82           0 :   }
      83             : 
      84           0 :   ulong end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
      85           0 :   return end - (ulong)output_buf; /* return the number of bytes written */
      86           0 : }
      87             : 
      88             : 
      89             : ulong
      90             : fd_runtime_fuzz_vm_interp_run( fd_runtime_fuzz_runner_t * runner,
      91             :                                void const *               input_,
      92             :                                void **                    output_,
      93             :                                void *                     output_buf,
      94           0 :                                ulong                      output_bufsz ) {
      95           0 :   fd_exec_test_syscall_context_t const * input = fd_type_pun_const( input_ );
      96           0 :   fd_exec_test_syscall_effects_t      ** output = fd_type_pun( output_ );
      97             : 
      98             :   /* Create execution context */
      99           0 :   const fd_exec_test_instr_context_t * input_instr_ctx = &input->instr_ctx;
     100           0 :   fd_exec_instr_ctx_t instr_ctx[1];
     101           0 :   if( !fd_runtime_fuzz_instr_ctx_create( runner, instr_ctx, input_instr_ctx, true /* is_syscall avoids certain checks we don't want */ ) ) {
     102           0 :     fd_runtime_fuzz_instr_ctx_destroy( runner, instr_ctx );
     103           0 :     return 0UL;
     104           0 :   }
     105             : 
     106           0 :   if( !( input->has_vm_ctx ) ) {
     107           0 :     fd_runtime_fuzz_instr_ctx_destroy( runner, instr_ctx );
     108           0 :     return 0UL;
     109           0 :   }
     110             : 
     111           0 :   fd_spad_t * spad   = runner->spad;
     112           0 :   fd_valloc_t valloc = fd_spad_virtual( spad );
     113             : 
     114             :   /* Create effects */
     115           0 :   ulong output_end = (ulong) output_buf + output_bufsz;
     116           0 :   FD_SCRATCH_ALLOC_INIT( l, output_buf );
     117           0 :   fd_exec_test_syscall_effects_t * effects =
     118           0 :     FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_syscall_effects_t),
     119           0 :                                 sizeof (fd_exec_test_syscall_effects_t) );
     120           0 :   *effects = (fd_exec_test_syscall_effects_t) FD_EXEC_TEST_SYSCALL_EFFECTS_INIT_ZERO;
     121             : 
     122           0 :   if( FD_UNLIKELY( _l > output_end ) ) {
     123           0 :     fd_runtime_fuzz_instr_ctx_destroy( runner, instr_ctx );
     124           0 :     return 0UL;
     125           0 :   }
     126             : 
     127           0 : do{
     128             :   /* Setup regions */
     129           0 :   if ( !input->vm_ctx.rodata ) {
     130           0 :     break;
     131           0 :   }
     132           0 :   ulong   rodata_sz = input->vm_ctx.rodata->size;
     133           0 :   uchar * rodata = fd_spad_alloc_check( spad, 8UL, rodata_sz );
     134           0 :   memcpy( rodata, input->vm_ctx.rodata->bytes, rodata_sz );
     135             : 
     136             :   /* Setup input region */
     137           0 :   ulong                    input_sz                                = 0UL;
     138           0 :   ulong                    pre_lens[256]                           = {0};
     139           0 :   fd_vm_input_region_t     input_mem_regions[1000]                 = {0}; /* We can have a max of (3 * num accounts + 1) regions */
     140           0 :   fd_vm_acc_region_meta_t  acc_region_metas[256]                   = {0}; /* instr acc idx to idx */
     141           0 :   uint                     input_mem_regions_cnt                   = 0U;
     142           0 :   int                      direct_mapping                          = FD_FEATURE_ACTIVE( instr_ctx->txn_ctx->slot, &instr_ctx->txn_ctx->features, bpf_account_data_direct_mapping );
     143           0 :   int                      mask_out_rent_epoch_in_vm_serialization = FD_FEATURE_ACTIVE( instr_ctx->txn_ctx->slot, &instr_ctx->txn_ctx->features, mask_out_rent_epoch_in_vm_serialization );
     144             : 
     145           0 :   uchar *                  input_ptr      = NULL;
     146           0 :   uchar                    program_id_idx = instr_ctx->instr->program_id;
     147           0 :   fd_txn_account_t const * program_acc    = &instr_ctx->txn_ctx->accounts[program_id_idx];
     148           0 :   uchar                    is_deprecated  = ( program_id_idx < instr_ctx->txn_ctx->accounts_cnt ) &&
     149           0 :                                             ( !memcmp( program_acc->vt->get_owner( program_acc ), fd_solana_bpf_loader_deprecated_program_id.key, sizeof(fd_pubkey_t) ) );
     150             : 
     151             :   /* Push the instruction onto the stack. This may also modify the sysvar instructions account, if its present. */
     152           0 :   int stack_push_err = fd_instr_stack_push( instr_ctx->txn_ctx, (fd_instr_info_t *)instr_ctx->instr );
     153           0 :   if( FD_UNLIKELY( stack_push_err ) ) {
     154           0 :     FD_LOG_WARNING(( "instr stack push err" ));
     155           0 :     fd_runtime_fuzz_instr_ctx_destroy( runner, instr_ctx );
     156           0 :     return 0;
     157           0 :   }
     158             : 
     159             :   /* Serialize accounts into input memory region. */
     160           0 :   int err = fd_bpf_loader_input_serialize_parameters( instr_ctx,
     161           0 :                                                       &input_sz,
     162           0 :                                                       pre_lens,
     163           0 :                                                       input_mem_regions,
     164           0 :                                                       &input_mem_regions_cnt,
     165           0 :                                                       acc_region_metas,
     166           0 :                                                       direct_mapping,
     167           0 :                                                       mask_out_rent_epoch_in_vm_serialization,
     168           0 :                                                       is_deprecated,
     169           0 :                                                       &input_ptr );
     170           0 :   if( FD_UNLIKELY( err ) ) {
     171           0 :     fd_runtime_fuzz_instr_ctx_destroy( runner, instr_ctx );
     172           0 :     return 0;
     173           0 :   }
     174             : 
     175           0 :   if( input->vm_ctx.heap_max>FD_VM_HEAP_DEFAULT ) {
     176           0 :     break;
     177           0 :   }
     178             : 
     179             :   /* Setup calldests from call_whitelist.
     180             :      Alloc calldests with the expected size (1 bit per ix, rounded up to ulong) */
     181           0 :   ulong max_pc = (rodata_sz + 7) / 8;
     182           0 :   ulong calldests_footprint = fd_sbpf_calldests_footprint( max_pc );
     183           0 :   void * calldests_mem = fd_valloc_malloc( valloc, fd_sbpf_calldests_align(), calldests_footprint );
     184           0 :   ulong * calldests = fd_sbpf_calldests_join( fd_sbpf_calldests_new( calldests_mem, max_pc ) );
     185           0 :   if( input->vm_ctx.call_whitelist && input->vm_ctx.call_whitelist->size > 0 ) {
     186           0 :     memcpy( calldests, input->vm_ctx.call_whitelist->bytes, input->vm_ctx.call_whitelist->size );
     187             :     /* Make sure bits over max_pc are all 0s. */
     188           0 :     ulong mask = (1UL << (max_pc % 64)) - 1UL;
     189           0 :     if ( max_pc % 64 != 0) {
     190           0 :       calldests[ max_pc / 64 ] &= mask;
     191           0 :     }
     192           0 :   }
     193           0 :   ulong entry_pc = fd_ulong_min( input->vm_ctx.entry_pc, rodata_sz / 8UL - 1UL );
     194           0 :   if( input->vm_ctx.sbpf_version >= FD_SBPF_V3 ) {
     195             :     /* in v3 we have to enable the entrypoint */
     196           0 :     calldests[ entry_pc / 64UL ] |= ( 1UL << ( entry_pc % 64UL ) );
     197           0 :   }
     198             : 
     199             :   /* Setup syscalls. Have them all be no-ops */
     200           0 :   fd_sbpf_syscalls_t * syscalls = fd_sbpf_syscalls_new( fd_valloc_malloc( valloc, fd_sbpf_syscalls_align(), fd_sbpf_syscalls_footprint() ) );
     201           0 :   fd_vm_syscall_register_slot( syscalls,
     202           0 :                                instr_ctx->txn_ctx->slot,
     203           0 :                                &instr_ctx->txn_ctx->features,
     204           0 :                                0 );
     205             : 
     206           0 :   for( ulong i=0; i< fd_sbpf_syscalls_slot_cnt(); i++ ){
     207           0 :     if( !fd_sbpf_syscalls_key_inval( syscalls[i].key ) ) {
     208           0 :       syscalls[i].func = fd_runtime_fuzz_vm_syscall_noop;
     209           0 :     }
     210           0 :   }
     211             : 
     212             :   /* Setup trace */
     213           0 :   const uint DUMP_TRACE = 0; // Set to 1 to dump trace to stdout
     214           0 :   uint tracing_enabled = input->vm_ctx.tracing_enabled;
     215           0 :   fd_vm_trace_t * trace = NULL;
     216           0 :   ulong event_max = 1UL<<20;
     217           0 :   ulong event_data_max = 2048UL;
     218             : 
     219           0 :   if (!!tracing_enabled) {
     220           0 :     trace = fd_vm_trace_new( fd_valloc_malloc( valloc, fd_vm_trace_align(), fd_vm_trace_footprint( event_max, event_data_max ) ), event_max, event_data_max );
     221           0 :   }
     222             : 
     223             :   /* Setup vm */
     224           0 :   fd_vm_t * vm = fd_vm_join( fd_vm_new( fd_valloc_malloc( valloc, fd_vm_align(), fd_vm_footprint() ) ) );
     225           0 :   FD_TEST( vm );
     226             : 
     227           0 :   fd_vm_init(
     228           0 :     vm,
     229           0 :     instr_ctx,
     230           0 :     input->vm_ctx.heap_max,
     231           0 :     input->has_instr_ctx ? input->instr_ctx.cu_avail : 0,
     232           0 :     rodata,
     233           0 :     rodata_sz,
     234           0 :     (ulong *) rodata, /* text*, same as rodata */
     235           0 :     rodata_sz / 8, /* text_cnt */
     236           0 :     0, /* text_off */
     237           0 :     rodata_sz, /* text_sz */
     238           0 :     entry_pc,
     239           0 :     calldests,
     240           0 :     input->vm_ctx.sbpf_version,
     241           0 :     syscalls,
     242           0 :     trace, /* trace */
     243           0 :     NULL, /* sha */
     244           0 :     input_mem_regions,
     245           0 :     input_mem_regions_cnt,
     246           0 :     acc_region_metas, /* vm_acc_region_meta*/
     247           0 :     is_deprecated, /* is deprecated */
     248           0 :     direct_mapping, /* direct mapping */
     249           0 :     0 /* dump_syscall_to_pb */
     250           0 :   );
     251             : 
     252             :   /* Setup registers.
     253             :      r1, r10, r11 are initialized by EbpfVm::new (r10) or EbpfVm::execute_program (r1, r11),
     254             :      or equivalently by fd_vm_init and fd_vm_setup_state_for_execution.
     255             :      Modifying them will most like break execution.
     256             :      In syscalls we allow override them (especially r1) because that simulates the fact
     257             :      that a program partially executed before reaching the syscall.
     258             :      Here we want to test what happens when the program starts from the beginning. */
     259           0 :   vm->reg[0]  = input->vm_ctx.r0;
     260             :   // vm->reg[1]  = input->vm_ctx.r1; // do not override
     261           0 :   vm->reg[2]  = input->vm_ctx.r2;
     262           0 :   vm->reg[3]  = input->vm_ctx.r3;
     263           0 :   vm->reg[4]  = input->vm_ctx.r4;
     264           0 :   vm->reg[5]  = input->vm_ctx.r5;
     265           0 :   vm->reg[6]  = input->vm_ctx.r6;
     266           0 :   vm->reg[7]  = input->vm_ctx.r7;
     267           0 :   vm->reg[8]  = input->vm_ctx.r8;
     268           0 :   vm->reg[9]  = input->vm_ctx.r9;
     269             :   // vm->reg[10]  = input->vm_ctx.r10; // do not override
     270             :   // vm->reg[11]  = input->vm_ctx.r11; // do not override
     271             : 
     272             :   // Validate the vm
     273           0 :   if( fd_vm_validate( vm ) != FD_VM_SUCCESS ) {
     274             :     // custom error, avoid -1 because we use it for "unknown error" in solfuzz-agave
     275           0 :     effects->error = -2;
     276           0 :     break;
     277           0 :   }
     278             : 
     279           0 :   if( input->syscall_invocation.stack_prefix ) {
     280           0 :     uchar * stack    = input->syscall_invocation.stack_prefix->bytes;
     281           0 :     ulong   stack_sz = fd_ulong_min(input->syscall_invocation.stack_prefix->size, FD_VM_STACK_MAX);
     282           0 :     fd_memcpy( vm->stack, stack, stack_sz );
     283           0 :   }
     284             : 
     285           0 :   if( input->syscall_invocation.heap_prefix ) {
     286           0 :     uchar * heap    = input->syscall_invocation.heap_prefix->bytes;
     287           0 :     ulong   heap_sz = fd_ulong_min(input->syscall_invocation.heap_prefix->size, FD_VM_HEAP_MAX);
     288           0 :     fd_memcpy( vm->heap, heap, heap_sz );
     289           0 :   }
     290             : 
     291             :   /* Run vm */
     292           0 :   int exec_res = 0;
     293           0 :   if (!!tracing_enabled) {
     294           0 :     exec_res = fd_vm_exec_trace( vm );
     295           0 :     if( DUMP_TRACE ) fd_vm_trace_printf( trace, syscalls );
     296           0 :     fd_vm_trace_delete( fd_vm_trace_leave( trace ) );
     297           0 :   } else {
     298           0 :     exec_res = fd_vm_exec_notrace( vm );
     299           0 :   }
     300             : 
     301             :   /* Agave does not have a SIGCALL error, and instead throws SIGILL */
     302           0 :   if( exec_res == FD_VM_ERR_SIGCALL ) exec_res = FD_VM_ERR_SIGILL;
     303           0 :   effects->error = -1 * exec_res;
     304             : 
     305             :   /* Capture outputs */
     306           0 :   effects->cu_avail    = vm->cu;
     307           0 :   effects->frame_count = vm->frame_cnt;
     308           0 :   /* Only capture registers if no error */;
     309           0 :   effects->r0          = exec_res ? 0 : vm->reg[0];
     310           0 :   effects->r1          = exec_res ? 0 : vm->reg[1];
     311           0 :   effects->r2          = exec_res ? 0 : vm->reg[2];
     312           0 :   effects->r3          = exec_res ? 0 : vm->reg[3];
     313           0 :   effects->r4          = exec_res ? 0 : vm->reg[4];
     314           0 :   effects->r5          = exec_res ? 0 : vm->reg[5];
     315           0 :   effects->r6          = exec_res ? 0 : vm->reg[6];
     316           0 :   effects->r7          = exec_res ? 0 : vm->reg[7];
     317           0 :   effects->r8          = exec_res ? 0 : vm->reg[8];
     318           0 :   effects->r9          = exec_res ? 0 : vm->reg[9];
     319           0 :   effects->r10         = exec_res ? 0 : vm->reg[10];
     320             : 
     321             :   /* skip logs since syscalls are stubbed */
     322             : 
     323             :   /* CU error is difficult to properly compare as there may have been
     324             :      valid writes to the memory regions prior to capturing the error. And
     325             :      the pc might be well past (by an arbitrary amount) the instruction
     326             :      where the CU error occurred. */
     327           0 :   if( exec_res == FD_VM_ERR_SIGCOST ) break;
     328             : 
     329           0 :   effects->pc = vm->pc;
     330             : 
     331           0 :   if( vm->heap_max > 0 ) {
     332           0 :     effects->heap       = FD_SCRATCH_ALLOC_APPEND(
     333           0 :       l, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( vm->heap_max ) );
     334           0 :     effects->heap->size = (uint)vm->heap_max;
     335           0 :     fd_memcpy( effects->heap->bytes, vm->heap, vm->heap_max );
     336           0 :   }
     337             : 
     338             :   /* Compress stack by removing right-most 0s.
     339             :      This reduces the total size of effects/fixtures when stack is not used,
     340             :      otherwise each would waste 256kB. */
     341           0 :   int rtrim_sz;
     342           0 :   for( rtrim_sz=FD_VM_STACK_MAX-1; rtrim_sz>=0; rtrim_sz-- ) {
     343           0 :     if( vm->stack[rtrim_sz] != 0 ) break;
     344           0 :   }
     345           0 :   if( rtrim_sz > 0 || (vm->stack[0] != 0) ) {
     346           0 :     effects->stack       = FD_SCRATCH_ALLOC_APPEND(
     347           0 :       l, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( FD_VM_STACK_MAX ) );
     348           0 :     effects->stack->size = (uint)rtrim_sz+1;
     349           0 :     fd_memcpy( effects->stack->bytes, vm->stack, (ulong)rtrim_sz+1 );
     350           0 :   }
     351             : 
     352           0 :   effects->rodata       = FD_SCRATCH_ALLOC_APPEND(
     353           0 :     l, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( rodata_sz ) );
     354           0 :   effects->rodata->size = (uint)rodata_sz;
     355           0 :   fd_memcpy( effects->rodata->bytes, rodata, rodata_sz );
     356             : 
     357             :   /* Capture input data regions */
     358           0 :   ulong tmp_end = FD_SCRATCH_ALLOC_FINI(l, 1UL);
     359           0 :   ulong input_data_regions_size = fd_runtime_fuzz_load_from_vm_input_regions( vm->input_mem_regions,
     360           0 :                                                                               vm->input_mem_regions_cnt,
     361           0 :                                                                               &effects->input_data_regions,
     362           0 :                                                                               &effects->input_data_regions_count,
     363           0 :                                                                               (void *) tmp_end,
     364           0 :                                                                               fd_ulong_sat_sub( output_end, tmp_end) );
     365           0 :   FD_SCRATCH_ALLOC_APPEND( l, 1UL, input_data_regions_size );
     366             : 
     367           0 : } while(0);
     368             : 
     369           0 :   ulong actual_end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     370           0 :   *output = effects;
     371           0 :   fd_runtime_fuzz_instr_ctx_destroy( runner, instr_ctx );
     372           0 :   return actual_end - (ulong)output_buf;
     373           0 : }
     374             : 
     375             : ulong
     376             : fd_runtime_fuzz_vm_syscall_run( fd_runtime_fuzz_runner_t * runner,
     377             :                                 void const *               input_,
     378             :                                 void **                    output_,
     379             :                                 void *                     output_buf,
     380           0 :                                 ulong                      output_bufsz ) {
     381           0 :   fd_exec_test_syscall_context_t const * input =  fd_type_pun_const( input_ );
     382           0 :   fd_exec_test_syscall_effects_t **      output = fd_type_pun( output_ );
     383             : 
     384             :   /* Create execution context */
     385           0 :   const fd_exec_test_instr_context_t * input_instr_ctx = &input->instr_ctx;
     386           0 :   fd_exec_instr_ctx_t ctx[1];
     387             :   // Skip extra checks for non-CPI syscalls
     388           0 :   int is_cpi            = !strncmp( (const char *)input->syscall_invocation.function_name.bytes, "sol_invoke_signed", 17 );
     389           0 :   int skip_extra_checks = !is_cpi;
     390             : 
     391           0 :   if( !fd_runtime_fuzz_instr_ctx_create( runner, ctx, input_instr_ctx, skip_extra_checks ) )
     392           0 :     goto error;
     393           0 :   fd_valloc_t valloc = fd_spad_virtual( runner->spad );
     394             : 
     395           0 :   ctx->txn_ctx->instr_trace[0].instr_info = (fd_instr_info_t *)ctx->instr;
     396           0 :   ctx->txn_ctx->instr_trace[0].stack_height = 1;
     397             : 
     398             :   /* Capture outputs */
     399           0 :   ulong output_end = (ulong)output_buf + output_bufsz;
     400           0 :   FD_SCRATCH_ALLOC_INIT( l, output_buf );
     401           0 :   fd_exec_test_syscall_effects_t * effects =
     402           0 :     FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_syscall_effects_t),
     403           0 :                                 sizeof (fd_exec_test_syscall_effects_t) );
     404           0 :   if( FD_UNLIKELY( _l > output_end ) ) {
     405           0 :     goto error;
     406           0 :   }
     407             : 
     408           0 :   if( input->vm_ctx.return_data.program_id && input->vm_ctx.return_data.program_id->size == sizeof(fd_pubkey_t) ) {
     409           0 :     fd_memcpy( ctx->txn_ctx->return_data.program_id.uc, input->vm_ctx.return_data.program_id->bytes, sizeof(fd_pubkey_t) );
     410           0 :   }
     411             : 
     412           0 :   if( input->vm_ctx.return_data.data && input->vm_ctx.return_data.data->size>0U ) {
     413           0 :     ctx->txn_ctx->return_data.len = input->vm_ctx.return_data.data->size;
     414           0 :     fd_memcpy( ctx->txn_ctx->return_data.data, input->vm_ctx.return_data.data->bytes, ctx->txn_ctx->return_data.len );
     415           0 :   }
     416             : 
     417           0 :   *effects = (fd_exec_test_syscall_effects_t) FD_EXEC_TEST_SYSCALL_EFFECTS_INIT_ZERO;
     418             : 
     419             :   /* Set up the VM instance */
     420           0 :   fd_sha256_t _sha[1];
     421           0 :   fd_sha256_t * sha = fd_sha256_join( fd_sha256_new( _sha ) );
     422           0 :   fd_sbpf_syscalls_t * syscalls = fd_sbpf_syscalls_new( fd_valloc_malloc( valloc, fd_sbpf_syscalls_align(), fd_sbpf_syscalls_footprint() ) );
     423           0 :   fd_vm_syscall_register_all( syscalls, 0 );
     424             : 
     425             :   /* Pull out the memory regions */
     426           0 :   if( !input->has_vm_ctx ) {
     427           0 :     goto error;
     428           0 :   }
     429             : 
     430           0 :   ulong rodata_sz = input->vm_ctx.rodata ? input->vm_ctx.rodata->size : 0UL;
     431           0 :   uchar * rodata = fd_valloc_malloc( valloc, 8UL, rodata_sz );
     432           0 :   if ( input->vm_ctx.rodata != NULL ) {
     433           0 :     fd_memcpy( rodata, input->vm_ctx.rodata->bytes, rodata_sz );
     434           0 :   }
     435             : 
     436           0 :   if( input->vm_ctx.heap_max > FD_VM_HEAP_MAX ) {
     437           0 :     goto error;
     438           0 :   }
     439             : 
     440           0 :   fd_vm_t * vm = fd_vm_join( fd_vm_new( fd_valloc_malloc( valloc, fd_vm_align(), fd_vm_footprint() ) ) );
     441           0 :   if ( !vm ) {
     442           0 :     goto error;
     443           0 :   }
     444             : 
     445             :   /* If the program ID account owner is the v1 BPF loader, then alignment is disabled (controlled by
     446             :      the `is_deprecated` flag) */
     447             : 
     448           0 :   ulong                   input_sz                                = 0UL;
     449           0 :   ulong                   pre_lens[256]                           = {0};
     450           0 :   fd_vm_input_region_t    input_mem_regions[1000]                 = {0}; /* We can have a max of (3 * num accounts + 1) regions */
     451           0 :   fd_vm_acc_region_meta_t acc_region_metas[256]                   = {0}; /* instr acc idx to idx */
     452           0 :   uint                    input_mem_regions_cnt                   = 0U;
     453           0 :   int                     direct_mapping                          = FD_FEATURE_ACTIVE( ctx->txn_ctx->slot, &ctx->txn_ctx->features, bpf_account_data_direct_mapping );
     454           0 :   int                     mask_out_rent_epoch_in_vm_serialization = FD_FEATURE_ACTIVE( ctx->txn_ctx->slot, &ctx->txn_ctx->features, mask_out_rent_epoch_in_vm_serialization );
     455             : 
     456           0 :   uchar *            input_ptr      = NULL;
     457           0 :   uchar              program_id_idx = ctx->instr->program_id;
     458           0 :   fd_txn_account_t * program_acc    = &ctx->txn_ctx->accounts[program_id_idx];
     459           0 :   uchar              is_deprecated  = ( program_id_idx < ctx->txn_ctx->accounts_cnt ) &&
     460           0 :                                       ( !memcmp( program_acc->vt->get_owner( program_acc ), fd_solana_bpf_loader_deprecated_program_id.key, sizeof(fd_pubkey_t) ) );
     461             : 
     462             :   /* Push the instruction onto the stack. This may also modify the sysvar instructions account, if its present. */
     463           0 :   int stack_push_err = fd_instr_stack_push( ctx->txn_ctx, (fd_instr_info_t *)ctx->instr );
     464           0 :   if( FD_UNLIKELY( stack_push_err ) ) {
     465           0 :       FD_LOG_WARNING(( "instr stack push err" ));
     466           0 :       goto error;
     467           0 :   }
     468             : 
     469             :   /* Serialize accounts into input memory region. */
     470           0 :   int err = fd_bpf_loader_input_serialize_parameters( ctx,
     471           0 :                                                       &input_sz,
     472           0 :                                                       pre_lens,
     473           0 :                                                       input_mem_regions,
     474           0 :                                                       &input_mem_regions_cnt,
     475           0 :                                                       acc_region_metas,
     476           0 :                                                       direct_mapping,
     477           0 :                                                       mask_out_rent_epoch_in_vm_serialization,
     478           0 :                                                       is_deprecated,
     479           0 :                                                       &input_ptr );
     480           0 :   if( FD_UNLIKELY( err ) ) {
     481           0 :     FD_LOG_WARNING(( "bpf loader input serialize parameters err" ));
     482           0 :     goto error;
     483           0 :   }
     484             : 
     485           0 :   fd_vm_init( vm,
     486           0 :               ctx,
     487           0 :               input->vm_ctx.heap_max,
     488           0 :               ctx->txn_ctx->compute_meter,
     489           0 :               rodata,
     490           0 :               rodata_sz,
     491           0 :               NULL, // TODO
     492           0 :               0, // TODO
     493           0 :               0, // TODO
     494           0 :               0, // TODO, text_sz
     495           0 :               0, // TODO
     496           0 :               NULL, // TODO
     497           0 :               TEST_VM_DEFAULT_SBPF_VERSION,
     498           0 :               syscalls,
     499           0 :               NULL, // TODO
     500           0 :               sha,
     501           0 :               input_mem_regions,
     502           0 :               input_mem_regions_cnt,
     503           0 :               acc_region_metas,
     504           0 :               is_deprecated,
     505           0 :               FD_FEATURE_ACTIVE( ctx->txn_ctx->slot, &ctx->txn_ctx->features, bpf_account_data_direct_mapping ),
     506           0 :               0 /* dump_syscall_to_pb */ );
     507             : 
     508             :   // Override some execution state values from the syscall fuzzer input
     509             :   // This is so we can test if the syscall mutates any of these erroneously
     510           0 :   vm->reg[0] = input->vm_ctx.r0;
     511           0 :   vm->reg[1] = input->vm_ctx.r1;
     512           0 :   vm->reg[2] = input->vm_ctx.r2;
     513           0 :   vm->reg[3] = input->vm_ctx.r3;
     514           0 :   vm->reg[4] = input->vm_ctx.r4;
     515           0 :   vm->reg[5] = input->vm_ctx.r5;
     516           0 :   vm->reg[6] = input->vm_ctx.r6;
     517           0 :   vm->reg[7] = input->vm_ctx.r7;
     518           0 :   vm->reg[8] = input->vm_ctx.r8;
     519           0 :   vm->reg[9] = input->vm_ctx.r9;
     520           0 :   vm->reg[10] = input->vm_ctx.r10;
     521           0 :   vm->reg[11] = input->vm_ctx.r11;
     522             : 
     523             :   // Override initial part of the heap, if specified the syscall fuzzer input
     524           0 :   if( input->syscall_invocation.heap_prefix ) {
     525           0 :     fd_memcpy( vm->heap, input->syscall_invocation.heap_prefix->bytes,
     526           0 :                fd_ulong_min(input->syscall_invocation.heap_prefix->size, vm->heap_max) );
     527           0 :   }
     528             : 
     529             :   // Override initial part of the stack, if specified the syscall fuzzer input
     530           0 :   if( input->syscall_invocation.stack_prefix ) {
     531           0 :     fd_memcpy( vm->stack, input->syscall_invocation.stack_prefix->bytes,
     532           0 :                fd_ulong_min(input->syscall_invocation.stack_prefix->size, FD_VM_STACK_MAX) );
     533           0 :   }
     534             : 
     535             :   // Look up the syscall to execute
     536           0 :   char * syscall_name = (char *)input->syscall_invocation.function_name.bytes;
     537           0 :   fd_sbpf_syscalls_t const * syscall = fd_runtime_fuzz_lookup_syscall_func(syscalls, syscall_name, input->syscall_invocation.function_name.size);
     538           0 :   if( !syscall ) {
     539           0 :     goto error;
     540           0 :   }
     541             : 
     542             :   /* There's an instr ctx struct embedded in the txn ctx instr stack. */
     543           0 :   fd_exec_instr_ctx_t * instr_ctx = &ctx->txn_ctx->instr_stack[ ctx->txn_ctx->instr_stack_sz - 1 ];
     544           0 :   *instr_ctx = (fd_exec_instr_ctx_t) {
     545           0 :     .instr     = ctx->instr,
     546           0 :     .txn_ctx   = ctx->txn_ctx,
     547           0 :     .depth     = 0U,
     548           0 :   };
     549             : 
     550             :   /* Actually invoke the syscall */
     551           0 :   int syscall_err = syscall->func( vm, vm->reg[1], vm->reg[2], vm->reg[3], vm->reg[4], vm->reg[5], &vm->reg[0] );
     552           0 :   int stack_pop_err = fd_instr_stack_pop( ctx->txn_ctx, ctx->instr );
     553           0 :   if( FD_UNLIKELY( stack_pop_err ) ) {
     554           0 :       FD_LOG_WARNING(( "instr stack pop err" ));
     555           0 :       goto error;
     556           0 :   }
     557           0 :   if( syscall_err ) {
     558           0 :     fd_log_collector_program_failure( vm->instr_ctx );
     559           0 :   }
     560             : 
     561             :   /* Capture the effects */
     562           0 :   int exec_err = vm->instr_ctx->txn_ctx->exec_err;
     563           0 :   effects->error = 0;
     564           0 :   if( syscall_err ) {
     565           0 :     if( exec_err==0 ) {
     566           0 :       FD_LOG_WARNING(( "TODO: syscall returns error, but exec_err not set. this is probably missing a log." ));
     567           0 :       effects->error = -1;
     568           0 :     } else {
     569           0 :       effects->error = (exec_err <= 0) ? -exec_err : -1;
     570             : 
     571             :       /* Map error kind, equivalent to:
     572             :           effects->error_kind = (fd_exec_test_err_kind_t)(vm->instr_ctx->txn_ctx->exec_err_kind); */
     573           0 :       switch (vm->instr_ctx->txn_ctx->exec_err_kind) {
     574           0 :         case FD_EXECUTOR_ERR_KIND_EBPF:
     575           0 :           effects->error_kind = FD_EXEC_TEST_ERR_KIND_EBPF;
     576           0 :           break;
     577           0 :         case FD_EXECUTOR_ERR_KIND_SYSCALL:
     578           0 :           effects->error_kind = FD_EXEC_TEST_ERR_KIND_SYSCALL;
     579           0 :           break;
     580           0 :         case FD_EXECUTOR_ERR_KIND_INSTR:
     581           0 :           effects->error_kind = FD_EXEC_TEST_ERR_KIND_INSTRUCTION;
     582           0 :           break;
     583           0 :         default:
     584           0 :           effects->error_kind = FD_EXEC_TEST_ERR_KIND_UNSPECIFIED;
     585           0 :           break;
     586           0 :       }
     587           0 :     }
     588           0 :   }
     589           0 :   effects->r0 = syscall_err ? 0 : vm->reg[0]; // Save only on success
     590           0 :   effects->cu_avail = (ulong)vm->cu;
     591             : 
     592           0 :   if( vm->heap_max ) {
     593           0 :     effects->heap = FD_SCRATCH_ALLOC_APPEND(
     594           0 :       l, alignof(uint), PB_BYTES_ARRAY_T_ALLOCSIZE( vm->heap_max ) );
     595           0 :     if( FD_UNLIKELY( _l > output_end ) ) {
     596           0 :       goto error;
     597           0 :     }
     598           0 :     effects->heap->size = (uint)vm->heap_max;
     599           0 :     fd_memcpy( effects->heap->bytes, vm->heap, vm->heap_max );
     600           0 :   } else {
     601           0 :     effects->heap = NULL;
     602           0 :   }
     603             : 
     604           0 :   effects->stack = FD_SCRATCH_ALLOC_APPEND(
     605           0 :     l, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( FD_VM_STACK_MAX ) );
     606           0 :     if( FD_UNLIKELY( _l > output_end ) ) {
     607           0 :       goto error;
     608           0 :     }
     609           0 :   effects->stack->size = (uint)FD_VM_STACK_MAX;
     610           0 :   fd_memcpy( effects->stack->bytes, vm->stack, FD_VM_STACK_MAX );
     611             : 
     612           0 :   if( vm->rodata_sz ) {
     613           0 :     effects->rodata = FD_SCRATCH_ALLOC_APPEND(
     614           0 :       l, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( rodata_sz ) );
     615           0 :     if( FD_UNLIKELY( _l > output_end ) ) {
     616           0 :       goto error;
     617           0 :     }
     618           0 :     effects->rodata->size = (uint)rodata_sz;
     619           0 :     fd_memcpy( effects->rodata->bytes, vm->rodata, rodata_sz );
     620           0 :   } else {
     621           0 :     effects->rodata = NULL;
     622           0 :   }
     623             : 
     624           0 :   effects->frame_count = vm->frame_cnt;
     625             : 
     626           0 :   fd_log_collector_t * log = &vm->instr_ctx->txn_ctx->log_collector;
     627             :   /* Only collect log on valid errors (i.e., != -1). Follows
     628             :      https://github.com/firedancer-io/solfuzz-agave/blob/99758d3c4f3a342d56e2906936458d82326ae9a8/src/utils/err_map.rs#L148 */
     629           0 :   if( effects->error != -1 && log->buf_sz ) {
     630           0 :     effects->log = FD_SCRATCH_ALLOC_APPEND(
     631           0 :       l, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( log->buf_sz ) );
     632           0 :     if( FD_UNLIKELY( _l > output_end ) ) {
     633           0 :       goto error;
     634           0 :     }
     635           0 :     effects->log->size = (uint)fd_log_collector_debug_sprintf( log, (char *)effects->log->bytes, 0 );
     636           0 :   } else {
     637           0 :     effects->log = NULL;
     638           0 :   }
     639             : 
     640             :   /* Capture input regions */
     641           0 :   effects->inputdata = NULL; /* Deprecated, using input_data_regions instead */
     642           0 :   ulong tmp_end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     643           0 :   ulong input_regions_size = fd_runtime_fuzz_load_from_vm_input_regions( vm->input_mem_regions,
     644           0 :                                                                          vm->input_mem_regions_cnt,
     645           0 :                                                                          &effects->input_data_regions,
     646           0 :                                                                          &effects->input_data_regions_count,
     647           0 :                                                                          (void *)tmp_end,
     648           0 :                                                                          fd_ulong_sat_sub( output_end, tmp_end ) );
     649             : 
     650           0 :   if( !!vm->input_mem_regions_cnt && !effects->input_data_regions ) {
     651           0 :     goto error;
     652           0 :   }
     653             : 
     654             :   /* Return the effects */
     655           0 :   ulong actual_end = tmp_end + input_regions_size;
     656           0 :   fd_runtime_fuzz_instr_ctx_destroy( runner, ctx );
     657             : 
     658           0 :   *output = effects;
     659           0 :   return actual_end - (ulong)output_buf;
     660             : 
     661           0 : error:
     662           0 :   fd_runtime_fuzz_instr_ctx_destroy( runner, ctx );
     663           0 :   return 0;
     664           0 : }

Generated by: LCOV version 1.14