LCOV - code coverage report
Current view: top level - flamenco/runtime/context - fd_exec_instr_ctx.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 53 100 53.0 %
Date: 2025-01-08 12:08:44 Functions: 6 7 85.7 %

          Line data    Source code
       1             : #include "fd_exec_instr_ctx.h"
       2             : #include "fd_exec_txn_ctx.h"
       3             : #include "../fd_acc_mgr.h"
       4             : 
       5             : void *
       6        7665 : fd_exec_instr_ctx_new( void * mem ) {
       7        7665 :   if( FD_UNLIKELY( !mem ) ) {
       8           0 :     FD_LOG_WARNING(( "NULL mem" ));
       9           0 :     return NULL;
      10           0 :   }
      11             : 
      12        7665 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)mem, FD_EXEC_INSTR_CTX_ALIGN ) ) ) {
      13           0 :     FD_LOG_WARNING(( "misaligned mem" ));
      14           0 :     return NULL;
      15           0 :   }
      16             : 
      17        7665 :   fd_memset(mem, 0, FD_EXEC_INSTR_CTX_FOOTPRINT);
      18             : 
      19        7665 :   fd_exec_instr_ctx_t * self = (fd_exec_instr_ctx_t *) mem;
      20             : 
      21        7665 :   FD_COMPILER_MFENCE();
      22        7665 :   self->magic = FD_EXEC_INSTR_CTX_MAGIC;
      23        7665 :   FD_COMPILER_MFENCE();
      24             : 
      25        7665 :   return mem;
      26        7665 : }
      27             : 
      28             : fd_exec_instr_ctx_t *
      29        7665 : fd_exec_instr_ctx_join( void * mem ) {
      30        7665 :   if( FD_UNLIKELY( !mem ) ) {
      31           0 :     FD_LOG_WARNING(( "NULL block" ));
      32           0 :     return NULL;
      33           0 :   }
      34             : 
      35        7665 :   fd_exec_instr_ctx_t * ctx = (fd_exec_instr_ctx_t *) mem;
      36             : 
      37        7665 :   if( FD_UNLIKELY( ctx->magic!=FD_EXEC_INSTR_CTX_MAGIC ) ) {
      38           0 :     FD_LOG_WARNING(( "bad magic" ));
      39           0 :     return NULL;
      40           0 :   }
      41             : 
      42        7665 :   return ctx;
      43        7665 : }
      44             : 
      45             : void *
      46        7665 : fd_exec_instr_ctx_leave( fd_exec_instr_ctx_t * ctx) {
      47        7665 :   if( FD_UNLIKELY( !ctx ) ) {
      48           0 :     FD_LOG_WARNING(( "NULL block" ));
      49           0 :     return NULL;
      50           0 :   }
      51             : 
      52        7665 :   if( FD_UNLIKELY( ctx->magic!=FD_EXEC_INSTR_CTX_MAGIC ) ) {
      53           0 :     FD_LOG_WARNING(( "bad magic" ));
      54           0 :     return NULL;
      55           0 :   }
      56             : 
      57        7665 :   return (void *) ctx;
      58        7665 : }
      59             : 
      60             : void *
      61        7665 : fd_exec_instr_ctx_delete( void * mem ) {
      62        7665 :   if( FD_UNLIKELY( !mem ) ) {
      63           0 :     FD_LOG_WARNING(( "NULL mem" ));
      64           0 :     return NULL;
      65           0 :   }
      66             : 
      67        7665 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)mem, FD_EXEC_INSTR_CTX_ALIGN) ) )  {
      68           0 :     FD_LOG_WARNING(( "misaligned mem" ));
      69           0 :     return NULL;
      70           0 :   }
      71             : 
      72        7665 :   fd_exec_instr_ctx_t * hdr = (fd_exec_instr_ctx_t *)mem;
      73        7665 :   if( FD_UNLIKELY( hdr->magic!=FD_EXEC_INSTR_CTX_MAGIC ) ) {
      74           0 :     FD_LOG_WARNING(( "bad magic" ));
      75           0 :     return NULL;
      76           0 :   }
      77             : 
      78        7665 :   FD_COMPILER_MFENCE();
      79        7665 :   FD_VOLATILE( hdr->magic ) = 0UL;
      80        7665 :   FD_COMPILER_MFENCE();
      81             : 
      82        7665 :   return mem;
      83        7665 : }
      84             : 
      85             : int
      86             : fd_instr_borrowed_account_view( fd_exec_instr_ctx_t * ctx,
      87             :                                 fd_pubkey_t const *      pubkey,
      88         768 :                                 fd_borrowed_account_t * * account ) {
      89       16842 :   for( ulong i = 0; i < ctx->instr->acct_cnt; i++ ) {
      90       16842 :     if( memcmp( pubkey->uc, ctx->instr->acct_pubkeys[i].uc, sizeof(fd_pubkey_t) )==0 ) {
      91             :       // TODO: check if readable???
      92         768 :       fd_borrowed_account_t * instr_account = ctx->instr->borrowed_accounts[i];
      93         768 :       *account = instr_account;
      94             : 
      95         768 :       if( FD_UNLIKELY( !instr_account ) ) {
      96           0 :         return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
      97           0 :       }
      98             : 
      99         768 :       return FD_ACC_MGR_SUCCESS;
     100         768 :     }
     101       16842 :   }
     102             : 
     103           0 :   return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
     104         768 : }
     105             : 
     106             : int
     107             : fd_instr_borrowed_account_modify_idx( fd_exec_instr_ctx_t const * ctx,
     108             :                                       ulong                       idx,
     109             :                                       ulong                       min_data_sz,
     110       32925 :                                       fd_borrowed_account_t **    account ) {
     111       32925 :   if( FD_UNLIKELY( idx >= ctx->instr->acct_cnt ) )
     112           0 :     return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
     113       32925 :   if( FD_UNLIKELY( !fd_instr_acc_is_writable_idx( ctx->instr, idx ) ) ) {
     114             :     /* FIXME: we should just handle the try_borrow_account semantics correctly */
     115         540 :     FD_LOG_DEBUG(( "unwritable account passed to fd_instr_borrowed_account_modify_idx (idx=%lu)", idx ));
     116         540 :   }
     117             : 
     118       32925 :   fd_borrowed_account_t * instr_account = ctx->instr->borrowed_accounts[idx];
     119       32925 :   if( min_data_sz>instr_account->const_meta->dlen ) {
     120        2493 :     fd_borrowed_account_resize( instr_account, min_data_sz );
     121        2493 :   }
     122             : 
     123             :   /* TODO: consider checking if account is writable */
     124       32925 :   *account = instr_account;
     125       32925 :   return FD_ACC_MGR_SUCCESS;
     126       32925 : }
     127             : 
     128             : int
     129             : fd_instr_borrowed_account_modify( fd_exec_instr_ctx_t *     ctx,
     130             :                                   fd_pubkey_t const *       pubkey,
     131             :                                   ulong                     min_data_sz,
     132           0 :                                   fd_borrowed_account_t * * account ) {
     133           0 :   for( ulong i = 0; i < ctx->instr->acct_cnt; i++ ) {
     134           0 :     if( memcmp( pubkey->uc, ctx->instr->acct_pubkeys[i].uc, sizeof(fd_pubkey_t) )==0 ) {
     135             :       // TODO: check if writable???
     136           0 :       if( FD_UNLIKELY( !fd_instr_acc_is_writable_idx( ctx->instr, (uchar)i ) ) ) {
     137             :         // FIXME: we should just handle the try_borrow_account semantics correctly
     138           0 :         FD_LOG_DEBUG(( "unwritable account passed to fd_instr_borrowed_account_modify_idx (idx=%lu, account=%s)", i, FD_BASE58_ENC_32_ALLOCA( pubkey ) ));
     139           0 :       }
     140           0 :       fd_borrowed_account_t * instr_account = ctx->instr->borrowed_accounts[i];
     141           0 :       if( min_data_sz > instr_account->const_meta->dlen ) {
     142           0 :         fd_borrowed_account_resize( instr_account, min_data_sz );
     143           0 :       }
     144           0 :       *account = instr_account;
     145           0 :       return FD_ACC_MGR_SUCCESS;
     146           0 :     }
     147           0 :   }
     148             : 
     149           0 :   return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
     150           0 : }

Generated by: LCOV version 1.14