LCOV - code coverage report
Current view: top level - flamenco/vm - fd_vm_tool.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 196 0.0 %
Date: 2025-11-30 04:57:15 Functions: 0 7 0.0 %

          Line data    Source code
       1             : #define _GNU_SOURCE
       2             : 
       3             : #include "fd_vm_base.h"
       4             : #include "fd_vm_private.h"
       5             : 
       6             : #include <stdio.h>
       7             : #include <stdlib.h>
       8             : #include <errno.h>
       9             : #include <sys/stat.h>
      10             : 
      11             : struct fd_vm_tool_prog {
      12             :   void *               bin_buf;
      13             :   fd_sbpf_program_t *  prog;
      14             :   fd_sbpf_syscalls_t * syscalls;
      15             : };
      16             : 
      17             : typedef struct fd_vm_tool_prog fd_vm_tool_prog_t;
      18             : 
      19             : static fd_vm_tool_prog_t *
      20             : fd_vm_tool_prog_create( fd_vm_tool_prog_t * tool_prog,
      21           0 :                         char const *        bin_path ) {
      22             : 
      23             :   /* Open file */
      24             : 
      25           0 :   FILE * bin_file = fopen( bin_path, "r" );
      26           0 :   if( FD_UNLIKELY( !bin_file ) )
      27           0 :     FD_LOG_ERR(( "fopen(\"%s\") failed (%i-%s)", bin_path, errno, fd_io_strerror( errno ) ));
      28             : 
      29           0 :   struct stat bin_stat;
      30           0 :   if( FD_UNLIKELY( 0!=fstat( fileno( bin_file ), &bin_stat ) ) )
      31           0 :     FD_LOG_ERR(( "fstat() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
      32           0 :   if( FD_UNLIKELY( !S_ISREG( bin_stat.st_mode ) ) )
      33           0 :     FD_LOG_ERR(( "File \"%s\" not a regular file", bin_path ));
      34             : 
      35             :   /* Allocate file buffer */
      36             : 
      37           0 :   ulong  bin_sz  = (ulong)bin_stat.st_size;
      38           0 :   void * bin_buf = malloc( bin_sz+8UL );
      39           0 :   if( FD_UNLIKELY( !bin_buf ) )
      40           0 :     FD_LOG_ERR(( "malloc(%#lx) failed (%i-%s)", bin_sz, errno, fd_io_strerror( errno ) ));
      41             : 
      42             :   /* Read program */
      43             : 
      44           0 :   if( FD_UNLIKELY( fread( bin_buf, bin_sz, 1UL, bin_file )!=1UL ) )
      45           0 :     FD_LOG_ERR(( "fread() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
      46           0 :   FD_TEST( 0==fclose( bin_file ) );
      47             : 
      48             :   /* Extract ELF info */
      49             : 
      50           0 :   fd_sbpf_elf_info_t elf_info;
      51           0 :   fd_sbpf_loader_config_t config = { 0 };
      52           0 :   config.elf_deploy_checks = 0;
      53           0 :   config.sbpf_min_version = FD_SBPF_V0;
      54           0 :   config.sbpf_max_version = FD_SBPF_V3;
      55           0 :   FD_TEST( fd_sbpf_elf_peek( &elf_info, bin_buf, bin_sz, &config )>=0 );
      56             : 
      57             :   /* Allocate rodata segment */
      58             : 
      59           0 :   void * rodata = malloc( elf_info.bin_sz );
      60           0 :   FD_TEST( rodata );
      61             : 
      62             :   /* Allocate program buffer */
      63             : 
      64           0 :   ulong  prog_align     = fd_sbpf_program_align();
      65           0 :   ulong  prog_footprint = fd_sbpf_program_footprint( &elf_info );
      66           0 :   fd_sbpf_program_t * prog = fd_sbpf_program_new( aligned_alloc( prog_align, prog_footprint ), &elf_info, rodata );
      67           0 :   FD_TEST( prog );
      68             : 
      69             :   /* Allocate syscalls */
      70           0 :   fd_sbpf_syscalls_t * syscalls = fd_sbpf_syscalls_new(
      71           0 :       aligned_alloc( fd_sbpf_syscalls_align(), fd_sbpf_syscalls_footprint() ) );
      72           0 :   FD_TEST( syscalls );
      73             : 
      74           0 :   fd_vm_syscall_register_all( syscalls, 0 );
      75             : 
      76             :   /* Load program */
      77           0 :   void * scratch  = malloc( bin_sz );
      78           0 :   int    load_err = fd_sbpf_program_load( prog, bin_buf, bin_sz, syscalls, &config, scratch, bin_sz );
      79           0 :   free( scratch );
      80           0 :   if( FD_UNLIKELY( 0!=load_err ) ) {
      81           0 :     FD_LOG_ERR(( "fd_sbpf_program_load() failed" ));
      82           0 :   }
      83             : 
      84           0 :   tool_prog->bin_buf  = bin_buf;
      85           0 :   tool_prog->prog     = prog;
      86           0 :   tool_prog->syscalls = syscalls;
      87             : 
      88           0 :   return tool_prog;
      89           0 : }
      90             : 
      91             : static void
      92           0 : fd_vm_tool_prog_free( fd_vm_tool_prog_t * prog ) {
      93           0 :   free( prog->prog->rodata );
      94           0 :   free( prog->bin_buf      );
      95           0 :   free( fd_sbpf_program_delete ( prog->prog     ) );
      96           0 :   free( fd_sbpf_syscalls_delete( prog->syscalls ) );
      97           0 : }
      98             : 
      99             : int
     100           0 : cmd_disasm( char const * bin_path ) {
     101           0 :   fd_vm_tool_prog_t tool_prog;
     102           0 :   fd_vm_tool_prog_create( &tool_prog, bin_path ); /* FIXME: RENAME INIT? */
     103             : 
     104             :   /* FIXME: DOES DISASM NEED THE TEXT_OFF TOO FOR CALLS? */
     105             :   /* FIXME: WOULD DISASM BENEFIT BY ANNOTATING THE ENTRY PC AND/OR THE
     106             :      CALLDESTS? */
     107             : 
     108           0 :   ulong  out_max = 128UL*tool_prog.prog->info.text_cnt; /* FIXME: OVERFLOW */
     109           0 :   ulong  out_len = 0UL;
     110           0 :   char * out     = (char *)malloc( out_max ); /* FIXME: GROSS */
     111           0 :   if( FD_UNLIKELY( !out ) ) FD_LOG_ERR(( "malloc failed" ));
     112           0 :   out[0] = '\0';
     113             : 
     114           0 :   int err = fd_vm_disasm_program( tool_prog.prog->text, tool_prog.prog->info.text_cnt, tool_prog.syscalls, out, out_max, &out_len );
     115             : 
     116           0 :   puts( out );
     117             : 
     118           0 :   free( out ); /* FIXME: GROSS */
     119             : 
     120           0 :   fd_vm_tool_prog_free( &tool_prog ); /* FIXME: RENAME DESTROY (OR MAYBE FINI)*/
     121             : 
     122           0 :   return err;
     123           0 : }
     124             : 
     125             : int
     126           0 : cmd_validate( char const * bin_path ) {
     127             : 
     128           0 :   fd_vm_tool_prog_t tool_prog;
     129           0 :   fd_vm_tool_prog_create( &tool_prog, bin_path );
     130             : 
     131           0 :   fd_vm_t vm = {
     132           0 :     .rodata                = tool_prog.prog->rodata,
     133           0 :     .rodata_sz             = tool_prog.prog->rodata_sz,
     134           0 :     .text                  = tool_prog.prog->text,
     135           0 :     .text_cnt              = tool_prog.prog->info.text_cnt,
     136           0 :     .text_sz               = tool_prog.prog->info.text_sz,
     137           0 :     .text_off              = tool_prog.prog->info.text_off,
     138           0 :     .entry_pc              = tool_prog.prog->entry_pc,
     139           0 :     .calldests             = tool_prog.prog->calldests,
     140           0 :     .syscalls              = tool_prog.syscalls,
     141           0 :     .trace                 = NULL
     142           0 :   };
     143             : 
     144             :   /* FIXME: DO WE REALLY NEED THE WHOLE VM TO VALIDATE? */
     145             : 
     146           0 :   int err = fd_vm_validate( &vm );
     147             : 
     148           0 :   fd_vm_tool_prog_free( &tool_prog );
     149             : 
     150           0 :   return err;
     151           0 : }
     152             : 
     153             : static uchar *
     154           0 : read_input_file( char const * input_path, ulong * _input_sz ) {
     155           0 :   if( FD_UNLIKELY( !_input_sz ) ) FD_LOG_ERR(( "input_sz cannot be NULL" ));
     156             : 
     157             :   /* Open file */
     158             : 
     159           0 :   FILE * input_file = fopen( input_path, "r" );
     160           0 :   if( FD_UNLIKELY( !input_file ) )
     161           0 :     FD_LOG_ERR(( "fopen(\"%s\") failed (%i-%s)", input_path, errno, fd_io_strerror( errno ) ));
     162             : 
     163           0 :   struct stat input_stat;
     164           0 :   if( FD_UNLIKELY( 0!=fstat( fileno( input_file ), &input_stat ) ) )
     165           0 :     FD_LOG_ERR(( "fstat() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     166           0 :   if( FD_UNLIKELY( !S_ISREG( input_stat.st_mode ) ) )
     167           0 :     FD_LOG_ERR(( "File \"%s\" not a regular file", input_path ));
     168             : 
     169             :   /* Allocate file buffer */
     170             : 
     171           0 :   ulong input_sz  = (ulong)input_stat.st_size;
     172           0 :   void * input_buf = malloc( input_sz );
     173           0 :   if( FD_UNLIKELY( !input_buf ) )
     174           0 :     FD_LOG_ERR(( "malloc(%#lx) failed (%i-%s)", input_sz, errno, fd_io_strerror( errno ) ));
     175             : 
     176             :   /* Read input */
     177             : 
     178           0 :   if( FD_UNLIKELY( fread( input_buf, input_sz, 1UL, input_file )!=1UL ) )
     179           0 :     FD_LOG_ERR(( "fread() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
     180           0 :   FD_TEST( 0==fclose( input_file ) );
     181             : 
     182           0 :   *_input_sz = input_sz;
     183             : 
     184           0 :   return input_buf;
     185           0 : }
     186             : 
     187             : int
     188             : cmd_trace( char const * bin_path,
     189           0 :            char const * input_path ) {
     190             : 
     191           0 :   fd_vm_tool_prog_t tool_prog;
     192           0 :   fd_vm_tool_prog_create( &tool_prog, bin_path );
     193             : 
     194           0 :   ulong   input_sz = 0UL;
     195           0 :   uchar * input    = read_input_file( input_path, &input_sz ); /* FIXME: WHERE IS INPUT FREED? */
     196             : 
     197             :   /* Turn input into a single memory region */
     198           0 :   fd_vm_input_region_t input_region = {
     199           0 :     .vaddr_offset = 0UL,
     200           0 :     .haddr        = (ulong)input,
     201           0 :     .region_sz    = (uint)input_sz,
     202           0 :     .is_writable  = 1U
     203           0 :   };
     204             : 
     205             : 
     206           0 :   ulong event_max      = 1UL<<30; /* 1 GiB default storage */
     207           0 :   ulong event_data_max = 2048UL;  /* 2 KiB memory range captures by default */
     208           0 :   fd_vm_trace_t * trace = fd_vm_trace_join( fd_vm_trace_new( aligned_alloc(
     209           0 :     fd_vm_trace_align(), fd_vm_trace_footprint( event_max, event_data_max ) ), event_max, event_data_max ) ); /* logs details */
     210           0 :   if( FD_UNLIKELY( !trace ) ) {
     211           0 :     FD_LOG_WARNING(( "unable to create trace" ));
     212           0 :     return FD_VM_ERR_INVAL; /* FIXME: ERR CODE */
     213           0 :   }
     214             : 
     215           0 :   fd_sha256_t _sha[1];
     216           0 :   fd_sha256_t * sha = fd_sha256_join( fd_sha256_new( _sha ) );
     217             : 
     218             :   /* FIXME: Gross init */
     219           0 :   fd_vm_t vm = {
     220           0 :     .instr_ctx             = NULL, /* FIXME */
     221           0 :     .heap_max              = FD_VM_HEAP_DEFAULT, /* FIXME: CONFIGURE */
     222           0 :     .entry_cu              = FD_VM_COMPUTE_UNIT_LIMIT, /* FIXME: CONFIGURE */
     223           0 :     .rodata                = tool_prog.prog->rodata,
     224           0 :     .rodata_sz             = tool_prog.prog->rodata_sz,
     225           0 :     .text                  = tool_prog.prog->text,
     226           0 :     .text_cnt              = tool_prog.prog->info.text_cnt,
     227           0 :     .text_off              = (ulong)tool_prog.prog->text - (ulong)tool_prog.prog->rodata,
     228           0 :     .entry_pc              = tool_prog.prog->entry_pc,
     229           0 :     .calldests             = tool_prog.prog->calldests,
     230           0 :     .syscalls              = tool_prog.syscalls,
     231           0 :     .input_mem_regions     = &input_region,
     232           0 :     .input_mem_regions_cnt = 1U,
     233           0 :     .trace                 = trace,
     234           0 :     .sha                   = sha,
     235           0 :   };
     236             : 
     237             :   /* FIXME: MOVE TO EXEC */
     238           0 :   vm.reg[ 1] = FD_VM_MEM_MAP_INPUT_REGION_START;
     239           0 :   vm.reg[10] = FD_VM_MEM_MAP_STACK_REGION_START + 0x1000;
     240             : 
     241           0 :   long dt = -fd_log_wallclock();
     242           0 :   int exec_err = fd_vm_exec( &vm );
     243           0 :   dt += fd_log_wallclock();
     244             : 
     245           0 :   printf( "Frame 0\n" );
     246           0 :   int err = fd_vm_trace_printf( vm.trace, vm.syscalls ); /* logs details */
     247           0 :   if( FD_UNLIKELY( err ) ) FD_LOG_WARNING(( "fd_vm_trace_printf failed (%i-%s)", err, fd_vm_strerror( err ) ));
     248             : 
     249           0 :   printf( "Interp_res:          %i (%s)\n", exec_err, fd_vm_strerror( exec_err ) );
     250           0 :   printf( "Return value:        %lu\n",     vm.reg[0]                            );
     251           0 :   printf( "Instruction counter: %lu\n",     vm.ic                                );
     252           0 :   printf( "Time:                %ld\n",     dt                                   );
     253             : 
     254           0 :   free( fd_vm_trace_delete( fd_vm_trace_leave( trace ) ) ); /* logs details */
     255             : 
     256           0 :   return err;
     257           0 : }
     258             : 
     259             : int
     260             : cmd_run( char const * bin_path,
     261           0 :          char const * input_path ) {
     262             : 
     263           0 :   fd_vm_tool_prog_t tool_prog;
     264           0 :   fd_vm_tool_prog_create( &tool_prog, bin_path );
     265             : 
     266           0 :   ulong   input_sz = 0UL;
     267           0 :   uchar * input    = read_input_file( input_path, &input_sz ); /* FIXME: WHERE IS INPUT FREED? */
     268             : 
     269             :   /* Turn input into a single memory region */
     270           0 :   fd_vm_input_region_t input_region = {
     271           0 :     .vaddr_offset = 0UL,
     272           0 :     .haddr        = (ulong)input,
     273           0 :     .region_sz    = (uint)input_sz,
     274           0 :     .is_writable  = 1U
     275           0 :   };
     276             : 
     277           0 :   fd_sha256_t _sha[1];
     278           0 :   fd_sha256_t * sha = fd_sha256_join( fd_sha256_new( _sha ) );
     279             : 
     280           0 :   fd_vm_t vm = {
     281           0 :     .instr_ctx             = NULL, /* FIXME */
     282           0 :     .heap_max              = FD_VM_HEAP_DEFAULT, /* FIXME: CONFIGURE */
     283           0 :     .entry_cu              = FD_VM_COMPUTE_UNIT_LIMIT, /* FIXME: CONFIGURE */
     284           0 :     .rodata                = tool_prog.prog->rodata,
     285           0 :     .rodata_sz             = tool_prog.prog->rodata_sz,
     286           0 :     .text                  = tool_prog.prog->text,
     287           0 :     .text_cnt              = tool_prog.prog->info.text_cnt,
     288           0 :     .text_off              = (ulong)tool_prog.prog->text - (ulong)tool_prog.prog->rodata,
     289           0 :     .entry_pc              = tool_prog.prog->entry_pc,
     290           0 :     .calldests             = tool_prog.prog->calldests,
     291           0 :     .syscalls              = tool_prog.syscalls,
     292           0 :     .input_mem_regions     = &input_region,
     293           0 :     .input_mem_regions_cnt = 1U,
     294           0 :     .trace                 = NULL,
     295           0 :     .sha                   = sha,
     296           0 :   };
     297             : 
     298             :   /* FIXME: MOVE TO EXEC */
     299           0 :   vm.reg[ 1] = FD_VM_MEM_MAP_INPUT_REGION_START;
     300           0 :   vm.reg[10] = FD_VM_MEM_MAP_STACK_REGION_START + 0x1000;
     301             : 
     302           0 :   long dt = -fd_log_wallclock();
     303           0 :   int exec_err = fd_vm_exec( &vm );
     304           0 :   dt += fd_log_wallclock();
     305             : 
     306           0 :   printf( "Interp_res:          %i (%s)\n", exec_err, fd_vm_strerror( exec_err ) );
     307           0 :   printf( "Return value:        %lu\n",     vm.reg[0]                            );
     308           0 :   printf( "Instruction counter: %lu\n",     vm.ic                                );
     309           0 :   printf( "Time:                %ld\n",     dt                                   );
     310             : 
     311           0 :   return FD_VM_SUCCESS;
     312           0 : }
     313             : 
     314             : int
     315             : main( int     argc,
     316             :       char ** argv ) {
     317             :   fd_boot( &argc, &argv );
     318             : 
     319             :   char const * cmd = fd_env_strip_cmdline_cstr( &argc, &argv, "--cmd", NULL, NULL );
     320             :   if( FD_UNLIKELY( !cmd ) ) FD_LOG_ERR(( "Please specify a command" ));
     321             : 
     322             :   if( !strcmp( cmd, "disasm" ) ) {
     323             : 
     324             :     char const * program_file = fd_env_strip_cmdline_cstr( &argc, &argv, "--program-file", NULL, NULL );
     325             : 
     326             :     if( FD_UNLIKELY( program_file==NULL ) ) FD_LOG_ERR(( "Please specify a --program-file" ));
     327             : 
     328             :     FD_LOG_NOTICE(( "disasm --program-file %s", program_file ));
     329             : 
     330             :     int err = cmd_disasm( program_file );
     331             :     if( FD_UNLIKELY( err ) ) FD_LOG_ERR(( "disasm failed (%i-%s)", err, fd_vm_strerror( err ) ));
     332             : 
     333             :     FD_LOG_NOTICE(( "disasm success" ));
     334             : 
     335             :   } else if( !strcmp( cmd, "validate" ) ) {
     336             : 
     337             :     char const * program_file = fd_env_strip_cmdline_cstr( &argc, &argv, "--program-file", NULL, NULL );
     338             : 
     339             :     if( FD_UNLIKELY( !program_file ) ) FD_LOG_ERR(( "Please specify a --program-file" ));
     340             : 
     341             :     FD_LOG_NOTICE(( "validate --program-file %s", program_file ));
     342             : 
     343             :     int err = cmd_validate( program_file );
     344             :     if( FD_UNLIKELY( err ) ) FD_LOG_ERR(( "validate failed (%i-%s)", err, fd_vm_strerror( err ) ));
     345             : 
     346             :     FD_LOG_NOTICE(( "validate success" ));
     347             : 
     348             :   } else if( !strcmp( cmd, "trace" ) ) {
     349             : 
     350             :     char const * program_file = fd_env_strip_cmdline_cstr( &argc, &argv, "--program-file", NULL, NULL );
     351             :     char const * input_file   = fd_env_strip_cmdline_cstr( &argc, &argv, "--input-file", NULL, NULL );
     352             : 
     353             :     if( FD_UNLIKELY( !program_file ) ) FD_LOG_ERR(( "Please specify a --program-file" ));
     354             :     if( FD_UNLIKELY( !input_file   ) ) FD_LOG_ERR(( "Please specify a --input-file"   ));
     355             : 
     356             :     FD_LOG_NOTICE(( "trace --program-file %s --input-file %s", program_file, input_file ));
     357             : 
     358             :     int err = cmd_trace( program_file, input_file );
     359             :     if( FD_UNLIKELY( err ) ) FD_LOG_ERR(( "trace failed (%i-%s)", err, fd_vm_strerror( err ) ));
     360             : 
     361             :     FD_LOG_NOTICE(( "trace success" ));
     362             : 
     363             :   } else if( !strcmp( cmd, "run" ) ) {
     364             : 
     365             :     char const * program_file = fd_env_strip_cmdline_cstr( &argc, &argv, "--program-file", NULL, NULL );
     366             :     char const * input_file   = fd_env_strip_cmdline_cstr( &argc, &argv, "--input-file",   NULL, NULL );
     367             : 
     368             :     if( FD_UNLIKELY( !program_file ) ) FD_LOG_ERR(( "Please specify a --program-file" ));
     369             :     if( FD_UNLIKELY( !input_file   ) ) FD_LOG_ERR(( "Please specify a --input-file"   ));
     370             : 
     371             :     FD_LOG_NOTICE(( "run --program-file %s --input-file %s", program_file, input_file ));
     372             : 
     373             :     int err = cmd_run( program_file, input_file );
     374             :     if( FD_UNLIKELY( err ) ) FD_LOG_ERR(( "run failed (%i-%s)", err, fd_vm_strerror( err ) ));
     375             : 
     376             :     FD_LOG_NOTICE(( "run success" ));
     377             : 
     378             :   } else {
     379             : 
     380             :     FD_LOG_ERR(( "unknown command: %s", cmd ));
     381             : 
     382             :   }
     383             : 
     384             :   fd_halt();
     385             :   return 0;
     386             : }

Generated by: LCOV version 1.14