LCOV - code coverage report
Current view: top level - flamenco/runtime/tests - fd_vm_harness.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 275 0.0 %
Date: 2026-02-28 05:18:01 Functions: 0 3 0.0 %

          Line data    Source code
       1             : #include "fd_instr_harness.h"
       2             : #include "../fd_executor.h"
       3             : #include "../fd_runtime.h"
       4             : #include "../fd_system_ids.h"
       5             : #include "../../log_collector/fd_log_collector.h"
       6             : #include "../program/fd_bpf_loader_serialization.h"
       7             : #include "../../../ballet/sbpf/fd_sbpf_loader.h"
       8             : #include "../../vm/fd_vm.h"
       9             : #include "../../vm/test_vm_util.h"
      10             : #include "generated/vm.pb.h"
      11             : #include "../fd_bank.h"
      12             : 
      13             : static fd_sbpf_syscalls_t *
      14             : fd_solfuzz_vm_syscall_lookup_func( fd_sbpf_syscalls_t * syscalls,
      15             :                                    const char *         syscall_name,
      16           0 :                                    size_t               len) {
      17           0 :   ulong i;
      18             : 
      19           0 :   if (!syscall_name) return NULL;
      20             : 
      21           0 :   for (i = 0; i < fd_sbpf_syscalls_slot_cnt(); ++i) {
      22           0 :     if (!fd_sbpf_syscalls_key_inval(syscalls[i].key) && syscalls[i].name && strlen(syscalls[i].name) == len) {
      23           0 :       if (!memcmp(syscalls[i].name, syscall_name, len)) {
      24           0 :         return syscalls + i;
      25           0 :       }
      26           0 :     }
      27           0 :   }
      28             : 
      29           0 :   return NULL;
      30           0 : }
      31             : 
      32             : static ulong
      33             : fd_solfuzz_vm_load_from_input_regions( fd_vm_input_region_t const *        input,
      34             :                                        uint                                input_count,
      35             :                                        fd_exec_test_input_data_region_t ** output,
      36             :                                        pb_size_t *                         output_count,
      37             :                                        void *                              output_buf,
      38           0 :                                        ulong                               output_bufsz ) {
      39             :   /* pre-flight checks on output buffer size*/
      40           0 :   ulong input_regions_total_sz = 0;
      41           0 :   for( ulong i=0; i<input_count; i++ ) {
      42           0 :     input_regions_total_sz += input[i].region_sz;
      43           0 :   }
      44             : 
      45           0 :   if( FD_UNLIKELY(   input_regions_total_sz == 0
      46           0 :                   || output_bufsz < input_regions_total_sz ) ) {
      47           0 :     *output = NULL;
      48           0 :     *output_count = 0;
      49           0 :     return 0;
      50           0 :   }
      51             : 
      52           0 :   FD_SCRATCH_ALLOC_INIT( l, output_buf );
      53           0 :   *output = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_input_data_region_t),
      54           0 :                                       input_count * sizeof (fd_exec_test_input_data_region_t) );
      55           0 :   FD_TEST( *output );
      56           0 :   *output_count = input_count;
      57             : 
      58           0 :   for( ulong i=0; i<input_count; i++ ) {
      59           0 :     fd_vm_input_region_t const * vm_region = &input[i];
      60           0 :     fd_exec_test_input_data_region_t * out_region = &(*output)[i];
      61           0 :     out_region->is_writable = vm_region->is_writable;
      62           0 :     out_region->offset = vm_region->vaddr_offset;
      63             : 
      64           0 :     if( vm_region->region_sz > 0 ) {
      65           0 :       out_region->content = FD_SCRATCH_ALLOC_APPEND( l, alignof(pb_bytes_array_t),
      66           0 :                                                  PB_BYTES_ARRAY_T_ALLOCSIZE(vm_region->region_sz) );
      67           0 :       FD_TEST( out_region->content );
      68           0 :       out_region->content->size = vm_region->region_sz;
      69           0 :       fd_memcpy( out_region->content->bytes, (void *)vm_region->haddr, vm_region->region_sz );
      70           0 :     } else {
      71           0 :       out_region->content = NULL;
      72           0 :     }
      73           0 :   }
      74             : 
      75           0 :   ulong end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
      76           0 :   return end - (ulong)output_buf; /* return the number of bytes written */
      77           0 : }
      78             : 
      79             : 
      80             : ulong
      81             : fd_solfuzz_pb_syscall_run( fd_solfuzz_runner_t * runner,
      82             :                            void const *          input_,
      83             :                            void **               output_,
      84             :                            void *                output_buf,
      85           0 :                            ulong                 output_bufsz ) {
      86           0 :   fd_exec_test_syscall_context_t const * input =  fd_type_pun_const( input_ );
      87           0 :   fd_exec_test_syscall_effects_t **      output = fd_type_pun( output_ );
      88             : 
      89             :   /* Create execution context */
      90           0 :   const fd_exec_test_instr_context_t * input_instr_ctx = &input->instr_ctx;
      91           0 :   fd_exec_instr_ctx_t ctx[1];
      92             :   // Skip extra checks for non-CPI syscalls
      93           0 :   int is_cpi            = !strncmp( (const char *)input->syscall_invocation.function_name.bytes, "sol_invoke_signed", 17 );
      94           0 :   int skip_extra_checks = !is_cpi;
      95             : 
      96           0 :   fd_solfuzz_pb_instr_ctx_create( runner, ctx, input_instr_ctx, skip_extra_checks );
      97             : 
      98           0 :   ctx->txn_out->err.exec_err = 0;
      99           0 :   ctx->txn_out->err.exec_err_kind = FD_EXECUTOR_ERR_KIND_NONE;
     100           0 :   ctx->bank = runner->bank;
     101             : 
     102             :   /* Capture outputs */
     103           0 :   ulong output_end = (ulong)output_buf + output_bufsz;
     104           0 :   FD_SCRATCH_ALLOC_INIT( l, output_buf );
     105           0 :   fd_exec_test_syscall_effects_t * effects =
     106           0 :     FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_syscall_effects_t),
     107           0 :                                 sizeof (fd_exec_test_syscall_effects_t) );
     108           0 :   if( FD_UNLIKELY( _l > output_end ) ) {
     109           0 :     goto error;
     110           0 :   }
     111             : 
     112           0 :   if( input->vm_ctx.return_data.program_id && input->vm_ctx.return_data.program_id->size == sizeof(fd_pubkey_t) ) {
     113           0 :     fd_memcpy( ctx->txn_out->details.return_data.program_id.uc, input->vm_ctx.return_data.program_id->bytes, sizeof(fd_pubkey_t) );
     114           0 :   }
     115             : 
     116           0 :   if( input->vm_ctx.return_data.data && input->vm_ctx.return_data.data->size>0U ) {
     117           0 :     ctx->txn_out->details.return_data.len = input->vm_ctx.return_data.data->size;
     118           0 :     fd_memcpy( ctx->txn_out->details.return_data.data, input->vm_ctx.return_data.data->bytes, ctx->txn_out->details.return_data.len );
     119           0 :   }
     120             : 
     121           0 :   *effects = (fd_exec_test_syscall_effects_t) FD_EXEC_TEST_SYSCALL_EFFECTS_INIT_ZERO;
     122             : 
     123             :   /* Set up the VM instance */
     124           0 :   fd_spad_t * spad = runner->spad;
     125           0 :   fd_sha256_t _sha[1];
     126           0 :   fd_sha256_t * sha = fd_sha256_join( fd_sha256_new( _sha ) );
     127           0 :   fd_sbpf_syscalls_t * syscalls = fd_sbpf_syscalls_new( fd_spad_alloc_check( spad, fd_sbpf_syscalls_align(), fd_sbpf_syscalls_footprint() ) );
     128           0 :   fd_vm_syscall_register_all( syscalls, 0 );
     129             : 
     130             :   /* Pull out the memory regions */
     131           0 :   if( !input->has_vm_ctx ) {
     132           0 :     goto error;
     133           0 :   }
     134             : 
     135           0 :   ulong rodata_sz = input->vm_ctx.rodata ? input->vm_ctx.rodata->size : 0UL;
     136           0 :   uchar * rodata = fd_spad_alloc_check( spad, 8UL, rodata_sz );
     137           0 :   if ( input->vm_ctx.rodata != NULL ) {
     138           0 :     fd_memcpy( rodata, input->vm_ctx.rodata->bytes, rodata_sz );
     139           0 :   }
     140             : 
     141           0 :   if( input->vm_ctx.heap_max > FD_VM_HEAP_MAX ) {
     142           0 :     goto error;
     143           0 :   }
     144             : 
     145           0 :   fd_vm_t * vm = fd_vm_join( fd_vm_new( fd_spad_alloc_check( spad, fd_vm_align(), fd_vm_footprint() ) ) );
     146           0 :   if ( !vm ) {
     147           0 :     goto error;
     148           0 :   }
     149             : 
     150             :   /* If the program ID account owner is the v1 BPF loader, then alignment is disabled (controlled by
     151             :      the `is_deprecated` flag) */
     152             : 
     153           0 :   ulong                   input_sz                               = 0UL;
     154           0 :   ulong                   pre_lens[256]                          = {0};
     155           0 :   fd_vm_input_region_t    input_mem_regions[1000]                = {0}; /* We can have a max of (3 * num accounts + 1) regions */
     156           0 :   fd_vm_acc_region_meta_t acc_region_metas[256]                  = {0}; /* instr acc idx to idx */
     157           0 :   uint                    input_mem_regions_cnt                  = 0U;
     158           0 :   int                     direct_mapping                         = FD_FEATURE_ACTIVE_BANK( ctx->bank, account_data_direct_mapping );
     159           0 :   int                     stricter_abi_and_runtime_constraints   = FD_FEATURE_ACTIVE_BANK( ctx->bank, stricter_abi_and_runtime_constraints );
     160             : 
     161           0 :   uchar               program_id_idx = ctx->instr->program_id;
     162           0 :   fd_account_meta_t * program_acc    = ctx->txn_out->accounts.account[program_id_idx].meta;
     163           0 :   uchar               is_deprecated  = ( program_id_idx < ctx->txn_out->accounts.cnt ) &&
     164           0 :                                       ( !memcmp( program_acc->owner, fd_solana_bpf_loader_deprecated_program_id.key, sizeof(fd_pubkey_t) ) );
     165             : 
     166             :   /* Push the instruction onto the stack. This may also modify the sysvar instructions account, if its present. */
     167           0 :   int stack_push_err = fd_instr_stack_push( ctx->runtime, ctx->txn_in, ctx->txn_out, (fd_instr_info_t *)ctx->instr );
     168           0 :   if( FD_UNLIKELY( stack_push_err ) ) {
     169           0 :       FD_LOG_WARNING(( "instr stack push err" ));
     170           0 :       goto error;
     171           0 :   }
     172             : 
     173           0 :   ulong instr_data_offset = 0UL;
     174           0 :   int err = fd_bpf_loader_input_serialize_parameters( ctx,
     175           0 :                                                       pre_lens,
     176           0 :                                                       input_mem_regions,
     177           0 :                                                       &input_mem_regions_cnt,
     178           0 :                                                       acc_region_metas,
     179           0 :                                                       stricter_abi_and_runtime_constraints,
     180           0 :                                                       direct_mapping,
     181           0 :                                                       is_deprecated,
     182           0 :                                                       &instr_data_offset,
     183           0 :                                                       &input_sz );
     184           0 :   if( FD_UNLIKELY( err ) ) {
     185           0 :     FD_LOG_WARNING(( "bpf loader input serialize parameters err" ));
     186           0 :     goto error;
     187           0 :   }
     188             : 
     189           0 :   fd_vm_init( vm,
     190           0 :               ctx,
     191           0 :               input->vm_ctx.heap_max,
     192           0 :               ctx->txn_out->details.compute_budget.compute_meter,
     193           0 :               rodata,
     194           0 :               rodata_sz,
     195           0 :               NULL, // TODO
     196           0 :               0, // TODO
     197           0 :               0, // TODO
     198           0 :               0, // TODO, text_sz
     199           0 :               0, // TODO
     200           0 :               NULL, // TODO
     201           0 :               TEST_VM_DEFAULT_SBPF_VERSION,
     202           0 :               syscalls,
     203           0 :               NULL, // TODO
     204           0 :               sha,
     205           0 :               input_mem_regions,
     206           0 :               input_mem_regions_cnt,
     207           0 :               acc_region_metas,
     208           0 :               is_deprecated,
     209           0 :               direct_mapping,
     210           0 :               stricter_abi_and_runtime_constraints,
     211           0 :               0 /* dump_syscall_to_pb */,
     212           0 :               0UL /* r2 is set by the fuzzer below */ );
     213             : 
     214             :   // Override some execution state values from the syscall fuzzer input
     215             :   // This is so we can test if the syscall mutates any of these erroneously
     216           0 :   vm->reg[0] = input->vm_ctx.r0;
     217           0 :   vm->reg[1] = input->vm_ctx.r1;
     218           0 :   vm->reg[2] = input->vm_ctx.r2;
     219           0 :   vm->reg[3] = input->vm_ctx.r3;
     220           0 :   vm->reg[4] = input->vm_ctx.r4;
     221           0 :   vm->reg[5] = input->vm_ctx.r5;
     222           0 :   vm->reg[6] = input->vm_ctx.r6;
     223           0 :   vm->reg[7] = input->vm_ctx.r7;
     224           0 :   vm->reg[8] = input->vm_ctx.r8;
     225           0 :   vm->reg[9] = input->vm_ctx.r9;
     226           0 :   vm->reg[10] = input->vm_ctx.r10;
     227           0 :   vm->reg[11] = input->vm_ctx.r11;
     228             : 
     229             :   // Override initial part of the heap, if specified the syscall fuzzer input
     230           0 :   if( input->syscall_invocation.heap_prefix ) {
     231           0 :     fd_memcpy( vm->heap, input->syscall_invocation.heap_prefix->bytes,
     232           0 :                fd_ulong_min(input->syscall_invocation.heap_prefix->size, vm->heap_max) );
     233           0 :   }
     234             : 
     235             :   // Override initial part of the stack, if specified the syscall fuzzer input
     236           0 :   if( input->syscall_invocation.stack_prefix ) {
     237           0 :     fd_memcpy( vm->stack, input->syscall_invocation.stack_prefix->bytes,
     238           0 :                fd_ulong_min(input->syscall_invocation.stack_prefix->size, FD_VM_STACK_MAX) );
     239           0 :   }
     240             : 
     241             :   // Look up the syscall to execute
     242           0 :   char * syscall_name = (char *)input->syscall_invocation.function_name.bytes;
     243           0 :   fd_sbpf_syscalls_t const * syscall = fd_solfuzz_vm_syscall_lookup_func(syscalls, syscall_name, input->syscall_invocation.function_name.size);
     244           0 :   if( !syscall ) {
     245           0 :     goto error;
     246           0 :   }
     247             : 
     248             :   /* There's an instr ctx struct embedded in the txn ctx instr stack. */
     249           0 :   fd_exec_instr_ctx_t * instr_ctx = &ctx->runtime->instr.stack[ ctx->runtime->instr.stack_sz - 1 ];
     250           0 :   *instr_ctx = (fd_exec_instr_ctx_t) {
     251           0 :     .instr   = ctx->instr,
     252           0 :     .txn_out = ctx->txn_out,
     253           0 :     .runtime = ctx->runtime,
     254           0 :   };
     255             : 
     256             :   /* Actually invoke the syscall */
     257           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] );
     258           0 :   int stack_pop_err = fd_instr_stack_pop( ctx->runtime, ctx->txn_out, ctx->instr );
     259           0 :   if( FD_UNLIKELY( stack_pop_err ) ) {
     260           0 :       FD_LOG_WARNING(( "instr stack pop err" ));
     261           0 :       goto error;
     262           0 :   }
     263           0 :   if( syscall_err ) {
     264           0 :     fd_log_collector_program_failure( vm->instr_ctx );
     265           0 :   }
     266             : 
     267             :   /* Capture the effects */
     268           0 :   int exec_err = vm->instr_ctx->txn_out->err.exec_err;
     269           0 :   effects->error = 0;
     270           0 :   if( syscall_err ) {
     271           0 :     if( exec_err==0 ) {
     272           0 :       FD_LOG_WARNING(( "TODO: syscall returns error, but exec_err not set. this is probably missing a log." ));
     273           0 :       effects->error = -1;
     274           0 :     } else {
     275           0 :       effects->error = (exec_err <= 0) ? -exec_err : -1;
     276             : 
     277             :       /* Map error kind, equivalent to:
     278             :           effects->error_kind = (fd_exec_test_err_kind_t)(vm->instr_ctx->txn_ctx->err.exec_err_kind); */
     279           0 :       switch (vm->instr_ctx->txn_out->err.exec_err_kind) {
     280           0 :         case FD_EXECUTOR_ERR_KIND_EBPF:
     281           0 :           effects->error_kind = FD_EXEC_TEST_ERR_KIND_EBPF;
     282           0 :           break;
     283           0 :         case FD_EXECUTOR_ERR_KIND_SYSCALL:
     284           0 :           effects->error_kind = FD_EXEC_TEST_ERR_KIND_SYSCALL;
     285           0 :           break;
     286           0 :         case FD_EXECUTOR_ERR_KIND_INSTR:
     287           0 :           effects->error_kind = FD_EXEC_TEST_ERR_KIND_INSTRUCTION;
     288           0 :           break;
     289           0 :         default:
     290           0 :           effects->error_kind = FD_EXEC_TEST_ERR_KIND_UNSPECIFIED;
     291           0 :           break;
     292           0 :       }
     293           0 :     }
     294           0 :   }
     295           0 :   effects->r0 = syscall_err ? 0 : vm->reg[0]; // Save only on success
     296           0 :   effects->cu_avail = (ulong)vm->cu;
     297             : 
     298           0 :   if( vm->heap_max ) {
     299           0 :     effects->heap = FD_SCRATCH_ALLOC_APPEND(
     300           0 :       l, alignof(uint), PB_BYTES_ARRAY_T_ALLOCSIZE( vm->heap_max ) );
     301           0 :     if( FD_UNLIKELY( _l > output_end ) ) {
     302           0 :       goto error;
     303           0 :     }
     304           0 :     effects->heap->size = (uint)vm->heap_max;
     305           0 :     fd_memcpy( effects->heap->bytes, vm->heap, vm->heap_max );
     306           0 :   } else {
     307           0 :     effects->heap = NULL;
     308           0 :   }
     309             : 
     310           0 :   effects->stack = FD_SCRATCH_ALLOC_APPEND(
     311           0 :     l, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( FD_VM_STACK_MAX ) );
     312           0 :     if( FD_UNLIKELY( _l > output_end ) ) {
     313           0 :       goto error;
     314           0 :     }
     315           0 :   effects->stack->size = (uint)FD_VM_STACK_MAX;
     316           0 :   fd_memcpy( effects->stack->bytes, vm->stack, FD_VM_STACK_MAX );
     317             : 
     318           0 :   if( vm->rodata_sz ) {
     319           0 :     effects->rodata = FD_SCRATCH_ALLOC_APPEND(
     320           0 :       l, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( rodata_sz ) );
     321           0 :     if( FD_UNLIKELY( _l > output_end ) ) {
     322           0 :       goto error;
     323           0 :     }
     324           0 :     effects->rodata->size = (uint)rodata_sz;
     325           0 :     fd_memcpy( effects->rodata->bytes, vm->rodata, rodata_sz );
     326           0 :   } else {
     327           0 :     effects->rodata = NULL;
     328           0 :   }
     329             : 
     330           0 :   effects->frame_count = vm->frame_cnt;
     331             : 
     332           0 :   fd_log_collector_t * log = vm->instr_ctx->runtime->log.log_collector;
     333             :   /* Only collect log on valid errors (i.e., != -1). Follows
     334             :      https://github.com/firedancer-io/solfuzz-agave/blob/99758d3c4f3a342d56e2906936458d82326ae9a8/src/utils/err_map.rs#L148 */
     335           0 :   if( effects->error != -1 && log->buf_sz ) {
     336           0 :     effects->log = FD_SCRATCH_ALLOC_APPEND(
     337           0 :       l, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( log->buf_sz ) );
     338           0 :     if( FD_UNLIKELY( _l > output_end ) ) {
     339           0 :       goto error;
     340           0 :     }
     341           0 :     effects->log->size = (uint)fd_log_collector_debug_sprintf( log, (char *)effects->log->bytes, 0 );
     342           0 :   } else {
     343           0 :     effects->log = NULL;
     344           0 :   }
     345             : 
     346             :   /* Capture input regions */
     347           0 :   ulong tmp_end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     348           0 :   ulong input_regions_size = fd_solfuzz_vm_load_from_input_regions(
     349           0 :       vm->input_mem_regions,
     350           0 :       vm->input_mem_regions_cnt,
     351           0 :       &effects->input_data_regions,
     352           0 :       &effects->input_data_regions_count,
     353           0 :       (void *)tmp_end,
     354           0 :       fd_ulong_sat_sub( output_end, tmp_end )
     355           0 :   );
     356             : 
     357           0 :   if( !!vm->input_mem_regions_cnt && !effects->input_data_regions ) {
     358           0 :     goto error;
     359           0 :   }
     360             : 
     361             :   /* Return the effects */
     362           0 :   ulong actual_end = tmp_end + input_regions_size;
     363           0 :   fd_solfuzz_pb_instr_ctx_destroy( runner, ctx );
     364             : 
     365           0 :   *output = effects;
     366           0 :   return actual_end - (ulong)output_buf;
     367             : 
     368           0 : error:
     369           0 :   fd_solfuzz_pb_instr_ctx_destroy( runner, ctx );
     370           0 :   return 0;
     371           0 : }

Generated by: LCOV version 1.14