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