LCOV - code coverage report
Current view: top level - ballet/sbpf - fd_sbpf_loader.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 9 9 100.0 %
Date: 2024-11-13 11:58:15 Functions: 0 0 -

          Line data    Source code
       1             : #ifndef HEADER_fd_src_ballet_sbpf_fd_sbpf_loader_h
       2             : #define HEADER_fd_src_ballet_sbpf_fd_sbpf_loader_h
       3             : 
       4             : /* fd_sbpf_loader prepares an sBPF program for execution.  This involves
       5             :    parsing and dynamic relocation.
       6             : 
       7             :    Due to historical reasons, this loader is neither a pure static
       8             :    linker nor a real dynamic loader.  For instance, it will ignore the
       9             :    program header table and instead load specific sections at predefined
      10             :    addresses.  However, it will perform dynamic relocation. */
      11             : 
      12             : #include "../../util/fd_util_base.h"
      13             : #include "../elf/fd_elf64.h"
      14             : 
      15             : /* Error types ********************************************************/
      16             : 
      17             : /* FIXME make error types more specific */
      18             : #define FD_SBPF_ERR_INVALID_ELF (1)
      19        1284 : #define FD_SBPF_PROG_RODATA_ALIGN 8UL
      20             : 
      21             : /* Program struct *****************************************************/
      22             : 
      23             : /* fd_sbpf_calldests is a bit vector of valid call destinations.
      24             :    Should be configured to fit any possible program counter.  The max
      25             :    program counter is <size of ELF binary> divided by 8. */
      26             : 
      27             : #define SET_NAME fd_sbpf_calldests
      28             : #include "../../util/tmpl/fd_set_dynamic.c"
      29             : 
      30             : /* fd_sbpf_syscall_func_t is a callback implementing an sBPF syscall.
      31             :    vm is a handle to the running VM.  Returns 0 on suceess or an integer
      32             :    error code on failure.
      33             : 
      34             :    IMPORTANT SAFETY TIP!  See notes in
      35             :    flamenco/vm/syscall/fd_vm_syscall.h on what a syscall should expect
      36             :    to see and expect to return. */
      37             : 
      38             : /* FIXME: THIS BELONGS IN FLAMENCO/VM */
      39             : 
      40             : typedef int
      41             : (*fd_sbpf_syscall_func_t)( void *  vm,
      42             :                            ulong   arg0,
      43             :                            ulong   arg1,
      44             :                            ulong   arg2,
      45             :                            ulong   arg3,
      46             :                            ulong   arg4,
      47             :                            ulong * _ret );
      48             : 
      49             : /* fd_sbpf_syscalls_t maps syscall IDs => a name and a VM specific
      50             :    context.  FIXME: THIS ALSO PROBABLY BELONGS IN FLAMENCO/VM */
      51             : 
      52    10253787 : #define FD_SBPF_SYSCALLS_LG_SLOT_CNT (7)
      53             : #define FD_SBPF_SYSCALLS_SLOT_CNT    (1UL<<FD_SBPF_SYSCALLS_LG_SLOT_CNT)
      54             : 
      55             : struct fd_sbpf_syscalls {
      56             :   uint                   key;  /* Murmur3-32 hash of function name */
      57             :   fd_sbpf_syscall_func_t func; /* Function pointer */
      58             :   char const *           name; /* Infinite lifetime pointer to function name */
      59             : };
      60             : 
      61             : typedef struct fd_sbpf_syscalls fd_sbpf_syscalls_t;
      62             : 
      63             : #define MAP_NAME              fd_sbpf_syscalls
      64     4317282 : #define MAP_T                 fd_sbpf_syscalls_t
      65     5639586 : #define MAP_KEY_T             uint
      66     4578432 : #define MAP_KEY_NULL          0U
      67     6107211 : #define MAP_KEY_INVAL(k)      !(k)
      68     5104989 : #define MAP_KEY_EQUAL(k0,k1)  (k0)==(k1)
      69             : #define MAP_KEY_EQUAL_IS_SLOW 0
      70     4297611 : #define MAP_KEY_HASH(k)       (k)
      71             : #define MAP_MEMOIZE           0
      72    10253787 : #define MAP_LG_SLOT_CNT       FD_SBPF_SYSCALLS_LG_SLOT_CNT
      73             : #include "../../util/tmpl/fd_map.c"
      74             : 
      75             : /* fd_sbpf_elf_info_t contains basic information extracted from an ELF
      76             :    binary. Indicates how much scratch memory and buffer size is required
      77             :    to fully load the program. */
      78             : 
      79             : struct fd_sbpf_elf_info {
      80             :   uint text_off;    /* File offset of .text section (overlaps rodata segment) */
      81             :   uint text_cnt;    /* Instruction count */
      82             :   ulong text_sz;    /* Length of text segment */
      83             : 
      84             :   uint dynstr_off;  /* File offset of .dynstr section (0=missing) */
      85             :   uint dynstr_sz;   /* Dynstr char count */
      86             : 
      87             :   uint rodata_sz;         /* size of rodata segment */
      88             :   uint rodata_footprint;  /* size of ELF binary */
      89             : 
      90             :   /* Known section indices
      91             :      In [-1,USHORT_MAX) where -1 means "not found" */
      92             :   int shndx_text;
      93             :   int shndx_symtab;
      94             :   int shndx_strtab;
      95             :   int shndx_dyn;
      96             :   int shndx_dynstr;
      97             : 
      98             :   /* Known program header indices (like shndx_*) */
      99             :   int phndx_dyn;
     100             : 
     101             :   uint entry_pc;  /* Program counter of entry point
     102             :                      NOTE: MIGHT BE OUT OF BOUNDS! */
     103             : 
     104             :   /* Bitmap of sections to be loaded (LSB => MSB) */
     105             :   ulong loaded_sections[ 1024UL ];
     106             : };
     107             : typedef struct fd_sbpf_elf_info fd_sbpf_elf_info_t;
     108             : 
     109             : /* fd_sbpf_program_t describes a loaded program in memory.
     110             : 
     111             :    [rodata,rodata+rodata_sz) is an externally allocated buffer holding
     112             :    the read-only segment to be loaded into the VM.  WARNING: The rodata
     113             :    area required doing load (rodata_footprint) is larger than the area
     114             :    mapped into the VM (rodata_sz).
     115             : 
     116             :    [text,text+8*text_cnt) is a sub-region of the read-only segment
     117             :    containing executable code. */
     118             : 
     119             : struct __attribute__((aligned(32UL))) fd_sbpf_program {
     120             :   fd_sbpf_elf_info_t info;
     121             : 
     122             :   /* rodata segment to be mapped into VM memory */
     123             :   void * rodata;     /* rodata segment data */
     124             :   ulong  rodata_sz;  /* size of data */
     125             : 
     126             :   /* text section within rodata segment */
     127             :   ulong * text;
     128             :   ulong   text_cnt;  /* instruction count */
     129             :   ulong   text_off;  /* instruction offset for use in CALL_REG instructions */
     130             :   ulong   text_sz;   /* size of text segment */
     131             :   ulong   entry_pc;  /* entrypoint PC (at text[ entry_pc - start_pc ]) ... FIXME: HMMMM ... CODE SEEMS TO USE TEXT[ ENTRY_PC ] */
     132             : 
     133             :   /* Bit vector of valid call destinations (bit count is rodata_sz) */
     134             :   void * calldests_shmem;
     135             :   /* Local join to bit vector of valid call destinations */
     136             :   fd_sbpf_calldests_t * calldests;
     137             : };
     138             : typedef struct fd_sbpf_program fd_sbpf_program_t;
     139             : 
     140             : /* Prototypes *********************************************************/
     141             : 
     142             : FD_PROTOTYPES_BEGIN
     143             : 
     144             : /* fd_sbpf_elf_peek partially parses the given ELF file in memory region
     145             :    [bin,bin+bin_sz)  Populates `info`.  Returns `info` on success.  On
     146             :    failure, returns NULL.
     147             : 
     148             :    elf_deploy_checks: The Agave ELF loader introduced additional checks
     149             :    that would fail on (certain) existing mainnet programs. Since it is
     150             :    impossible to retroactively enforce these checks on already deployed programs,
     151             :    a guard flag is used to enable these checks only when deploying programs. */
     152             : 
     153             : fd_sbpf_elf_info_t *
     154             : fd_sbpf_elf_peek( fd_sbpf_elf_info_t * info,
     155             :                   void const *         bin,
     156             :                   ulong                bin_sz,
     157             :                   int                  elf_deploy_checks );
     158             : 
     159             : /* fd_sbpf_program_{align,footprint} return the alignment and size
     160             :    requirements of the memory region backing the fd_sbpf_program_t
     161             :    object. */
     162             : 
     163             : FD_FN_CONST ulong
     164             : fd_sbpf_program_align( void );
     165             : 
     166             : FD_FN_PURE ulong
     167             : fd_sbpf_program_footprint( fd_sbpf_elf_info_t const * info );
     168             : 
     169             : /* fd_sbpf_program_new formats prog_mem to hold an fd_sbpf_program_t.
     170             :    prog_mem must match footprint requirements of the given elf_info.
     171             :    elf_info may be deallocated on return.
     172             : 
     173             :    rodata is the read-only segment buffer that the program is configured
     174             :    against and must be valid for the lifetime of the program object. It
     175             :    should also meet the alignment requirements of the program object.
     176             :    */
     177             : 
     178             : fd_sbpf_program_t *
     179             : fd_sbpf_program_new( void *                     prog_mem,
     180             :                      fd_sbpf_elf_info_t const * elf_info,
     181             :                      void *                     rodata );
     182             : 
     183             : /* fd_sbpf_program_load loads an eBPF program for execution.
     184             : 
     185             :    prog is a program object allocated with fd_sbpf_program_new and must
     186             :    match the footprint requirements of this ELF file.
     187             : 
     188             :    Initializes and populates the program struct with information about
     189             :    the program and prepares the read-only segment provided in
     190             :    fd_sbpf_program_new.
     191             : 
     192             :    Memory region [bin,bin+bin_sz) contains the ELF file to be loaded.
     193             : 
     194             :    On success, returns 0.
     195             :    On error, returns FD_SBPF_ERR_* and leaves prog in an undefined
     196             :    state.
     197             : 
     198             :    ### Compliance
     199             : 
     200             :    This loader does not yet adhere to Solana protocol specs.
     201             :    It is mostly compatible with solana-labs/rbpf v0.3.0 with the
     202             :    following config:
     203             : 
     204             :      new_elf_parser:     true
     205             :      enable_elf_vaddr:   false
     206             :      reject_broken_elfs: elf_deploy_checks
     207             : 
     208             :    For documentation on these config params, see:
     209             :    https://github.com/solana-labs/rbpf/blob/v0.3.0/src/vm.rs#L198
     210             : 
     211             :    Solana/Agave equivalent:
     212             :    https://github.com/solana-labs/rbpf/blob/v0.8.0/src/elf.rs#L361
     213             :    */
     214             : 
     215             : int
     216             : fd_sbpf_program_load( fd_sbpf_program_t *  prog,
     217             :                       void const *         bin,
     218             :                       ulong                bin_sz,
     219             :                       fd_sbpf_syscalls_t * syscalls,
     220             :                       int                  elf_deploy_checks );
     221             : 
     222             : /* fd_sbpf_program_delete destroys the program object and unformats the
     223             :    memory regions holding it. */
     224             : 
     225             : void *
     226             : fd_sbpf_program_delete( fd_sbpf_program_t * program );
     227             : 
     228             : /* fd_csv_strerror: Returns a cstr describing the source line and error
     229             :    kind after the last call to `fd_sbpf_program_load` from the same
     230             :    thread returned non-zero.
     231             :    Always returns a valid cstr, though the content is undefined in case
     232             :    the last call to `fd_sbpf_program_load` returned zero (success). */
     233             : 
     234             : char const *
     235             : fd_sbpf_strerror( void );
     236             : 
     237             : FD_PROTOTYPES_END
     238             : 
     239             : #endif /* HEADER_fd_src_ballet_sbpf_fd_sbpf_loader_h */

Generated by: LCOV version 1.14