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