LCOV - code coverage report
Current view: top level - flamenco/progcache - fd_progcache_rec.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 169 179 94.4 %
Date: 2026-11-26 02:16:43 Functions: 8 10 80.0 %

          Line data    Source code
       1             : #include "fd_progcache.h"
       2             : #include "fd_progcache_clock.h"
       3             : #include "../../util/racesan/fd_racesan_target.h"
       4             : #include "../vm/fd_vm.h" /* fd_vm_syscall_register_slot, fd_vm_validate */
       5             : 
       6             : /* A free record is always write-locked: fd_progcache_rec_release leaves it so
       7             :    and rec_init_inflight hands it to the acquirer read-locked, so a stale
       8             :    speculative reader can never lock a free record. */
       9             : 
      10             : /* rec_init_inflight turns a write-locked record of class c into an in-flight
      11             :    one owned by the caller.  Callers hold the write lock and have the record
      12             :    out of the map. */
      13             : 
      14             : static fd_progcache_rec_t *
      15             : rec_init_inflight( fd_progcache_join_t * join,
      16             :                    ulong                 idx,
      17        4448 :                    ulong                 c ) {
      18        4448 :   fd_progcache_shmem_t * pc  = join->shmem;
      19        4448 :   fd_progcache_rec_t *   rec = join->rec.ele + idx;
      20             : 
      21             :   /* Not written here: the lock (stays write-locked; a stale speculative reader
      22             :      must never see it clear), free_next (a stale popper may be reading it) and
      23             :      the fork-list links (owned by the fork lock). */
      24        4448 :   memset( rec, 0, offsetof(fd_progcache_rec_t, lock) );
      25        4448 :   rec->txn_idx       = UINT_MAX;
      26        4448 :   rec->entry_pc      = 0U;
      27        4448 :   rec->text_cnt      = 0U;
      28        4448 :   rec->text_off      = 0U;
      29        4448 :   rec->text_sz       = 0U;
      30        4448 :   rec->rodata_sz     = 0U;
      31        4448 :   rec->calldests_off = UINT_MAX;
      32        4448 :   rec->rodata_off    = 0U;
      33        4448 :   rec->sbpf_version  = 0;
      34        4448 :   rec->exists        = 1;
      35        4448 :   rec->size_class    = c & 0x7UL; /* c<FD_PROGCACHE_CACHE_CLASS_CNT, checked by callers */
      36             : 
      37             :   /* Attach value storage: the record's own arena slot */
      38        4448 :   ulong slot_sz   = fd_progcache_cache_slot_sz[ c ];
      39        4448 :   rec->data_gaddr = pc->cache.arena_gaddr[ c ] + (ulong)( idx - pc->cache.rec_base[ c ] )*slot_sz;
      40        4448 :   rec->data_max   = (uint)slot_sz;
      41             : 
      42        4448 :   fd_prog_state_clear( join->rec.ele, idx );
      43        4448 :   atomic_store_explicit( &rec->lock.value, (ushort)1, memory_order_release ); /* write-locked -> read-locked by caller */
      44        4448 :   return rec;
      45        4448 : }
      46             : 
      47             : /* free_push returns record idx to class c's free list.  free_pop takes one, or
      48             :    returns UINT_MAX when the class is full.  Both use atomic free-list links
      49             :    and a versioned top word to detect intervening changes and prevent ABA. */
      50             : 
      51             : static void
      52             : free_push( fd_progcache_join_t * join,
      53             :            ulong                 c,
      54        4451 :            ulong                 idx ) {
      55        4451 :   fd_progcache_shmem_t * pc  = join->shmem;
      56        4451 :   fd_progcache_rec_t *   rec = join->rec.ele + idx;
      57        4451 :   for(;;) {
      58        4451 :     ulong old_vt  = __atomic_load_n( &pc->cache.free_top[ c ].ver_top, __ATOMIC_RELAXED );
      59        4451 :     uint  old_top = (uint)( old_vt & (ulong)UINT_MAX );
      60        4451 :     uint  old_ver = (uint)( old_vt >> 32 );
      61        4451 :     __atomic_store_n( &rec->free_next, old_top, __ATOMIC_RELAXED );
      62        4451 :     FD_COMPILER_MFENCE();
      63        4451 :     ulong new_vt = ( (ulong)(uint)( old_ver+1U ) << 32 ) | (ulong)(uint)idx;
      64        4451 :     fd_racesan_hook( "prog_free_push:pre_cas" );
      65        4451 :     if( FD_LIKELY( __atomic_compare_exchange_n( &pc->cache.free_top[ c ].ver_top, &old_vt, new_vt,
      66        4451 :                                                 0, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED ) ) ) {
      67        4451 :       __atomic_fetch_add( &pc->cache.free_cnt[ c ].val, 1UL, __ATOMIC_RELAXED );
      68        4451 :       return;
      69        4451 :     }
      70           0 :     FD_SPIN_PAUSE();
      71           0 :   }
      72        4451 : }
      73             : 
      74             : static uint
      75             : free_pop( fd_progcache_join_t * join,
      76        4448 :           ulong                 c ) {
      77        4448 :   fd_progcache_shmem_t * pc = join->shmem;
      78        4448 :   for(;;) {
      79        4448 :     ulong old_vt  = __atomic_load_n( &pc->cache.free_top[ c ].ver_top, __ATOMIC_RELAXED );
      80        4448 :     uint  old_top = (uint)( old_vt & (ulong)UINT_MAX );
      81        4448 :     if( FD_UNLIKELY( old_top==UINT_MAX ) ) return UINT_MAX; /* class full */
      82        4448 :     uint  old_ver = (uint)( old_vt >> 32 );
      83        4448 :     uint  next    = __atomic_load_n( &join->rec.ele[ old_top ].free_next, __ATOMIC_RELAXED );
      84        4448 :     ulong new_vt  = ( (ulong)(uint)( old_ver+1U ) << 32 ) | (ulong)next;
      85        4448 :     fd_racesan_hook( "prog_free_pop:pre_cas" );
      86        4448 :     if( FD_LIKELY( __atomic_compare_exchange_n( &pc->cache.free_top[ c ].ver_top, &old_vt, new_vt,
      87        4448 :                                                 0, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED ) ) ) {
      88        4445 :       __atomic_fetch_sub( &pc->cache.free_cnt[ c ].val, 1UL, __ATOMIC_RELAXED );
      89        4445 :       return old_top;
      90        4445 :     }
      91           3 :     FD_SPIN_PAUSE();
      92           3 :   }
      93        4448 : }
      94             : 
      95             : fd_progcache_rec_t *
      96             : fd_progcache_rec_acquire( fd_progcache_join_t * join,
      97        4449 :                           ulong                 val_footprint ) {
      98        4449 :   ulong c = fd_progcache_cache_class( val_footprint );
      99        4449 :   FD_TEST( c<FD_PROGCACHE_CACHE_CLASS_CNT );
     100             : 
     101             :   /* Pop a free record from the class fitting val_footprint. */
     102        4449 :   uint idx = free_pop( join, c );
     103        4449 :   if( FD_UNLIKELY( idx==UINT_MAX ) ) return NULL; /* class full */
     104             : 
     105        4449 :   return rec_init_inflight( join, idx, c );
     106        4449 : }
     107             : 
     108             : fd_progcache_rec_t *
     109             : fd_progcache_rec_reinit( fd_progcache_join_t * join,
     110           0 :                          fd_progcache_rec_t *  rec ) {
     111           0 :   return rec_init_inflight( join, (ulong)( rec - join->rec.ele ), rec->size_class );
     112           0 : }
     113             : 
     114             : void
     115             : fd_progcache_rec_release( fd_progcache_join_t * join,
     116        4451 :                           fd_progcache_rec_t *  rec ) {
     117        4451 :   ulong idx = (ulong)( rec - join->rec.ele );
     118        4451 :   ulong c   = rec->size_class;
     119             : 
     120        4451 :   rec->exists     = 0;
     121        4451 :   rec->data_gaddr = 0UL;
     122        4451 :   rec->data_max   = 0U;
     123             :   /* lock stays write-locked (the free-record invariant) */
     124             : 
     125        4451 :   fd_prog_state_clear( join->rec.ele, idx );
     126        4451 :   free_push( join, c, idx );
     127        4451 : }
     128             : 
     129             : void
     130             : fd_progcache_rec_abandon( fd_progcache_join_t * join,
     131           0 :                           fd_progcache_rec_t *  rec ) {
     132             :   /* Never in the map, so no new reader can find it, but a stale reader of a
     133             :      previous incarnation may hold a transient tryread: trade our read lock for
     134             :      the write lock to drain them. */
     135           0 :   fd_rwlock_unread( &rec->lock );
     136           0 :   fd_rwlock_write( &rec->lock );
     137           0 :   fd_progcache_rec_release( join, rec );
     138           0 : }
     139             : 
     140             : FD_FN_PURE ulong
     141        7126 : fd_progcache_val_footprint( fd_sbpf_elf_info_t const * elf_info ) {
     142        7126 :   int   has_calldests = !fd_sbpf_enable_stricter_elf_headers_enabled( elf_info->sbpf_version );
     143        7126 :   ulong pc_max        = fd_ulong_max( 1UL, elf_info->text_cnt );
     144             : 
     145             :   /* load_buf_sz is the exact buffer the loader needs (peek-computed):
     146             :      text_off+text_sz for strict, the rodata image for lenient-fast, or bin_sz
     147             :      for legacy lenient. */
     148        7126 :   ulong l = FD_LAYOUT_INIT;
     149        7126 :   if( has_calldests ) {
     150        6421 :     l = FD_LAYOUT_APPEND( l, fd_sbpf_calldests_align(), fd_sbpf_calldests_footprint( pc_max ) );
     151        6421 :   }
     152        7126 :   l = FD_LAYOUT_APPEND( l, 8UL, elf_info->load_buf_sz );
     153        7126 :   return FD_LAYOUT_FINI( l, fd_progcache_val_align() );
     154        7126 : }
     155             : 
     156             : /* Program loader wrapper */
     157             : 
     158             : fd_progcache_rec_t *
     159             : fd_progcache_rec_load( fd_progcache_rec_t *            rec,
     160             :                        fd_wksp_t *                     wksp,
     161             :                        fd_sbpf_elf_info_t const *      elf_info,
     162             :                        fd_sbpf_loader_config_t const * config,
     163             :                        ulong                           load_slot,
     164             :                        fd_features_t const *           features,
     165             :                        void const *                    progdata,
     166             :                        ulong                           progdata_sz,
     167             :                        void *                          scratch,
     168        3915 :                        ulong                           scratch_sz ) {
     169             : 
     170             :   /* Format object */
     171             : 
     172        3915 :   int has_calldests = !fd_sbpf_enable_stricter_elf_headers_enabled( elf_info->sbpf_version );
     173             : 
     174        3915 :   void * val           = fd_wksp_laddr_fast( wksp, rec->data_gaddr );
     175        3915 :   void * calldests_mem = NULL;
     176        3915 :   void * rodata_mem;
     177        3915 :   if( has_calldests ) {
     178             :     /* Lenient (v0-v2): [ calldests | rodata ] laid out inside val.  The rodata
     179             :        buffer is load_buf_sz (rodata image on the fast path, bin_sz on the
     180             :        legacy path); must match fd_progcache_val_footprint. */
     181        3209 :     FD_SCRATCH_ALLOC_INIT( l, val );
     182        3209 :     calldests_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_sbpf_calldests_align(), fd_sbpf_calldests_footprint( fd_ulong_max( 1UL, elf_info->text_cnt ) ) );
     183        3209 :     rodata_mem    = FD_SCRATCH_ALLOC_APPEND( l, 8UL, elf_info->load_buf_sz );
     184        3209 :     FD_SCRATCH_ALLOC_FINI( l, fd_progcache_val_align() );
     185        3209 :     FD_TEST( _l-(ulong)val == fd_progcache_val_footprint( elf_info ) );
     186        3209 :   } else {
     187             :     /* Strict (v3+): no calldests, so rodata is just the start of val
     188             :        (val is fd_progcache_val_align()-aligned, which is >= 8). */
     189         706 :     rodata_mem = val;
     190         706 :   }
     191             : 
     192        3915 :   rec->calldests_off = has_calldests ? (uint)( (ulong)calldests_mem - (ulong)val ) : UINT_MAX;
     193        3915 :   rec->rodata_off    = (uint)( (ulong)rodata_mem - (ulong)val );
     194        3915 :   rec->entry_pc      = 0;
     195        3915 :   rec->rodata_sz     = 0;
     196             : 
     197        3915 :   rec->text_cnt      = elf_info->text_cnt;
     198        3915 :   rec->text_off      = elf_info->text_off;
     199        3915 :   rec->text_sz       = (uint)elf_info->text_sz;
     200        3915 :   rec->sbpf_version  = (uchar)elf_info->sbpf_version;
     201             : 
     202             :   /* Set up sbpf_loader (redirect writes into progcache_rec object) */
     203             : 
     204        3915 :   fd_sbpf_program_t prog[1] = {{
     205        3915 :     .info     = *elf_info,
     206        3915 :     .rodata   = rodata_mem,
     207        3915 :     .text     = (ulong *)((ulong)rodata_mem + elf_info->text_off), /* FIXME: WHAT IF MISALIGNED */
     208        3915 :     .entry_pc = ULONG_MAX
     209        3915 :   }};
     210        3915 :   if( has_calldests && elf_info->text_cnt>0UL ) {
     211        3206 :     prog->calldests_shmem = calldests_mem;
     212        3206 :     prog->calldests = fd_sbpf_calldests_join( fd_sbpf_calldests_new( calldests_mem, elf_info->text_cnt ) );
     213        3206 :   }
     214             : 
     215             :   /* Loader requires syscall table */
     216             : 
     217        3915 :   fd_sbpf_syscalls_t _syscalls[ FD_SBPF_SYSCALLS_SLOT_CNT ];
     218        3915 :   fd_sbpf_syscalls_t * syscalls = fd_sbpf_syscalls_join( fd_sbpf_syscalls_new( _syscalls ) );
     219        3915 :   int syscalls_err = fd_vm_syscall_register_slot( syscalls, load_slot, features, /* is_deploy */ 0 );
     220        3915 :   if( FD_UNLIKELY( syscalls_err!=FD_VM_SUCCESS ) ) FD_LOG_CRIT(( "fd_vm_syscall_register_slot failed" ));
     221             : 
     222             :   /* Run ELF loader.
     223             : 
     224             :      Scratch is needed only by the lenient (v0-v2) fallback path, which
     225             :      assembles the rodata segment via a scratch buffer.  The lenient fast
     226             :      path and strict (v3+) loads write directly into the destination buffer;
     227             :      passing NULL both selects the loader's fast/no-scratch path and faults
     228             :      loudly if it ever starts relying on scratch. */
     229             : 
     230        3915 :   int    use_scratch     = fd_sbpf_loader_is_legacy_lenient( elf_info );
     231        3915 :   void * load_scratch    = use_scratch ? scratch    : NULL;
     232        3915 :   ulong  load_scratch_sz = use_scratch ? scratch_sz : 0UL;
     233             : 
     234        3915 :   if( FD_UNLIKELY( 0!=fd_sbpf_program_load( prog, progdata, progdata_sz, syscalls, config, load_scratch, load_scratch_sz ) ) ) {
     235          24 :     return NULL;
     236          24 :   }
     237             : 
     238        3891 :   rec->entry_pc  = (uint)prog->entry_pc;
     239        3891 :   rec->rodata_sz = (uint)prog->rodata_sz;
     240             : 
     241             :   /* Run bytecode validator */
     242             : 
     243        3891 :   fd_vm_t _vm[1];
     244        3891 :   fd_vm_t * vm = fd_vm_join( fd_vm_new( _vm ) );
     245        3891 :   if( FD_UNLIKELY( !vm ) ) FD_LOG_CRIT(( "fd_vm_new failed" ));
     246        3891 :   vm = fd_vm_init( vm,
     247        3891 :                    NULL, /* OK since unused in `fd_vm_validate()` */
     248        3891 :                    0UL,
     249        3891 :                    0UL,
     250        3891 :                    prog->rodata,
     251        3891 :                    prog->rodata_sz,
     252        3891 :                    prog->text,
     253        3891 :                    prog->info.text_cnt,
     254        3891 :                    prog->info.text_off,
     255        3891 :                    prog->info.text_sz,
     256        3891 :                    prog->entry_pc,
     257        3891 :                    prog->calldests,
     258        3891 :                    elf_info->sbpf_version,
     259        3891 :                    syscalls,
     260        3891 :                    NULL,
     261        3891 :                    NULL,
     262        3891 :                    NULL,
     263        3891 :                    0U,
     264        3891 :                    NULL,
     265        3891 :                    0,
     266        3891 :                    0,
     267        3891 :                    (ulong[ FD_VM_REG_CNT ]){ 0 } );
     268        3891 :   if( FD_UNLIKELY( !vm ) ) FD_LOG_CRIT(( "fd_vm_init failed" ));
     269             : 
     270        3891 :   if( FD_UNLIKELY( fd_vm_validate( vm )!=FD_VM_SUCCESS ) ) return NULL;
     271             : 
     272        3869 :   return rec;
     273        3891 : }
     274             : 
     275             : fd_progcache_rec_t *
     276         577 : fd_progcache_rec_nx( fd_progcache_rec_t * rec ) {
     277         577 :   rec->data_gaddr    = 0UL;
     278         577 :   rec->data_max      = 0U;
     279         577 :   rec->entry_pc      = 0;
     280         577 :   rec->text_cnt      = 0;
     281         577 :   rec->text_off      = 0;
     282         577 :   rec->text_sz       = 0;
     283         577 :   rec->rodata_sz     = 0;
     284         577 :   rec->calldests_off = UINT_MAX;
     285         577 :   rec->rodata_off    = 0;
     286         577 :   rec->sbpf_version  = 0;
     287         577 :   return rec;
     288         577 : }

Generated by: LCOV version 1.14