Line data Source code
1 : #ifndef HEADER_fd_src_flamenco_vm_fd_vm_h
2 : #define HEADER_fd_src_flamenco_vm_fd_vm_h
3 :
4 : #include "fd_vm_base.h"
5 : #include "../../ballet/sha256/fd_sha256.h"
6 :
7 : /* A fd_vm_t is an opaque handle of a virtual machine that can execute
8 : sBPF programs. */
9 :
10 : struct fd_vm;
11 : typedef struct fd_vm fd_vm_t;
12 :
13 : /**********************************************************************/
14 : /* FIXME: MOVE TO FD_VM_PRIVATE WHEN CONSTRUCTORS READY */
15 :
16 : /* A fd_vm_shadow_t holds stack frame information not accessible from
17 : within a program. */
18 :
19 : struct fd_vm_shadow { ulong r6; ulong r7; ulong r8; ulong r9; ulong r10; ulong pc; };
20 : typedef struct fd_vm_shadow fd_vm_shadow_t;
21 :
22 : /* fd_vm_input_region_t holds information about fragmented memory regions
23 : within the larger input region. */
24 :
25 : struct __attribute__((aligned(8UL))) fd_vm_input_region {
26 : ulong vaddr_offset; /* Represents offset from the start of the input region. */
27 : ulong haddr; /* Host address corresponding to the start of the mem region. */
28 : uint region_sz; /* Size of the memory region. */
29 : ulong address_space_reserved; /* The amount of address space reserved for the region. */
30 : uchar is_writable; /* If the region can be written to or is read-only */
31 : ulong acc_region_meta_idx; /* Index of the acc_region_meta_t struct for the account corresponding to this region. */
32 : };
33 : typedef struct fd_vm_input_region fd_vm_input_region_t;
34 :
35 : /* fd_vm_acc_region_meta_t holds metadata about a given account. An array of these
36 : structs will map an instruction account index to its respective input memory
37 : region location. */
38 :
39 : struct __attribute((aligned(8UL))) fd_vm_acc_region_meta {
40 : /* Metadata region index, or UINT_MAX without virtual address space
41 : adjustments. The data region follows at region_idx+1 if address
42 : space is reserved for account data. */
43 : uint region_idx;
44 : /* FIXME: We can get rid of this field once DM is activated. This is
45 : only a hack to make the non-DM code path happy. When DM is
46 : activated, we could query the input_mem_region array for the
47 : original data len. */
48 : ulong original_data_len;
49 : /* The transaction account corresponding to this account. */
50 : fd_acc_t * acc;
51 : /* The transaction's touched flag for this account. */
52 : uchar * touched;
53 :
54 : /* The expected virtual addresses of the serialized pubkey, lamports, owner,
55 : and data for this account in VM address space.
56 : Used for CPI security checks. */
57 : ulong vm_addr;
58 : ulong vm_key_addr;
59 : ulong vm_lamports_addr;
60 : ulong vm_owner_addr;
61 : ulong vm_data_addr;
62 : };
63 : typedef struct fd_vm_acc_region_meta fd_vm_acc_region_meta_t;
64 :
65 : /* In Agave, all the regions are 16-byte aligned in host address space. There is then an alignment check
66 : which is done inside each syscall memory translation, checking if the data is aligned in host address
67 : space. This is a layering violation, as it leaks the host address layout into the consensus model.
68 :
69 : In the future we will change this alignment check in the vm to purely operate on the virtual address space,
70 : taking advantage of the fact that Agave regions are known to be aligned. For now, we align our regions to
71 : either 8 or 16 bytes, as there are no 16-byte alignment translations in the syscalls currently:
72 : stack: 16 byte aligned
73 : heap: 16 byte aligned
74 : input: 8 byte aligned
75 : rodata: 8 byte aligned
76 :
77 : https://github.com/solana-labs/rbpf/blob/cd19a25c17ec474e6fa01a3cc3efa325f44cd111/src/ebpf.rs#L39-L40 */
78 255947 : #define FD_VM_HOST_REGION_ALIGN (16UL)
79 :
80 : struct __attribute__((aligned(FD_VM_HOST_REGION_ALIGN))) fd_vm {
81 :
82 : /* registers, indexed [0,FD_VM_REG_CNT), with padding
83 : Keeping these at the top of vm helps vm->reg[i] instruction coding,
84 : and uses less uops. */
85 : ulong reg[ FD_VM_REG_MAX ];
86 :
87 : /* VM configuration */
88 :
89 : /* FIXME: suspect these three should be replaced by some kind of VM
90 : enabled feature struct (though syscalls do seem to make additional
91 : non-trivial use of instr_ctx). */
92 :
93 : fd_exec_instr_ctx_t * instr_ctx; /* FIXME: DOCUMENT */
94 :
95 : /* FIXME: frame_max should be run time configurable by compute budget.
96 : If there is no reasonable upper bound on this, shadow and stack
97 : will need to be provided by users. */
98 :
99 : //ulong frame_max; /* Maximum number of stack frames, in [0,FD_VM_STACK_FRAME_MAX] */
100 : ulong heap_max; /* Maximum amount of heap in bytes, in [0,FD_VM_HEAP_MAX] */
101 : ulong entry_cu; /* Initial number of compute units for this program, in [0,FD_VM_COMPUTE_UNIT_LIMIT] */
102 :
103 : /* FIXME: The below are practically an exact match to the
104 : fields of an fd_sbpf_program_t (sans ELF info) */
105 :
106 : uchar const * rodata; /* Program read only data, indexed [0,rodata_sz), aligned 8 */
107 : ulong rodata_sz; /* Program read only data size in bytes, FIXME: BOUNDS? */
108 : ulong const * text; /* Program sBPF words, indexed [0,text_cnt), aligned 8 */
109 : ulong text_cnt; /* Program sBPF word count, all text words are inside the rodata */
110 : ulong text_off; /* CALLX virtual address offset in bytes (NOT words).
111 : SBPF V0-V2: ==(ulong)text - (ulong)rodata (file offset of text within ELF).
112 : SBPF V3: ==0 (bytecode starts at vaddr 0x100000000 exactly) */
113 : ulong text_sz; /* Program sBPF size in bytes, == text_cnt*8 */
114 :
115 : ulong entry_pc; /* Initial program counter, in [0,text_cnt)
116 : FIXME: MAKE SURE NOT INTO MW INSTRUCTION, MAKE SURE VALID CALLDEST? */
117 : ulong const * calldests; /* Bit vector of local functions that can be called into, bit indexed in [0,text_cnt) */
118 : /* FIXME: ADD BIT VECTOR OF FORBIDDEN BRANCH TARGETS (E.G.
119 : INTO THE MIDDLE OF A MULTIWORD INSTRUCTION) */
120 :
121 : uint syscall_set; /* FD_SBPF_SYSCALL_* bit set */
122 :
123 : fd_vm_trace_t * trace; /* Location to stream traces (no tracing if NULL) */
124 :
125 : /* VM execution and syscall state */
126 :
127 : /* These are used to communicate the execution and syscall state to
128 : users and syscalls. These are initialized based on the above when
129 : a program starts executing. When program halts or faults, these
130 : provide precise execution diagnostics to the user (and potential
131 : breakpoint/continue functionality in the future). When the vm
132 : makes a syscall, the vm will set these precisely and, when a
133 : syscall returns, the vm will update its internal execution state
134 : appropriately. */
135 :
136 : /* IMPORTANT SAFETY TIP! THE BEHAVIOR OF THE SYSCALL ALLOCATOR FOR
137 : HEAP_SZ MUST EXACTLY MATCH THE SOLANA VALIDATOR ALLOCATOR:
138 :
139 : https://github.com/solana-labs/solana/blob/v1.17.23/program-runtime/src/invoke_context.rs#L122-L148
140 :
141 : BIT-FOR-BIT AND BUG-FOR-BUG. SEE THE SYSCALL_ALLOC_FREE FOR MORE
142 : DETAILS. */
143 :
144 : ulong pc; /* The current instruction, in [0,text_cnt) in normal execution, may be out of bounds in a fault */
145 : ulong ic; /* The number of instructions which have been executed */
146 : ulong cu; /* The remaining CUs left for the transaction */
147 : ulong frame_cnt; /* The current number of stack frames pushed, in [0,frame_max] */
148 :
149 : ulong heap_sz; /* Heap size in bytes, in [0,heap_max] */
150 : ulong heap_clean; /* Initialized part of the heap in bytes, in [0,heap_max] */
151 : ulong stack_clean; /* Initialized part of the stack in bytes, in [0,FD_VM_STACK_MAX] */
152 :
153 : /* VM memory */
154 :
155 : /* The vm classifies the 64-bit vm address space into 6 regions:
156 :
157 : 0 - unmapped lo
158 : 1 - program -> [FD_VM_MEM_MAP_PROGRAM_REGION_START,FD_VM_MEM_MAP_PROGRAM_REGION_START+4GiB)
159 : 2 - stack -> [FD_VM_MEM_MAP_STACK_REGION_START, FD_VM_MEM_MAP_STACK_REGION_START +4GiB)
160 : 3 - heap -> [FD_VM_MEM_MAP_HEAP_REGION_START, FD_VM_MEM_MAP_HEAP_REGION_START +4GiB)
161 : 4 - input -> [FD_VM_MEM_MAP_INPUT_REGION_START, FD_VM_MEM_MAP_INPUT_REGION_START +4GiB)
162 : 5 - unmapped hi
163 :
164 : These mappings are encoded in a software TLB consisting of three
165 : 6-element arrays: region_haddr, region_ld_sz and region_st_sz.
166 :
167 : region_haddr[i] + offset gives the host address of offset within
168 : region i for offset in [region_voff[i],region_voff[i]+region_{ld,st}_sz[i])
169 : (region_voff[i] is zero for regions 0..3). region_{ld,st}_sz[i]
170 : gives the number of mappable bytes for {loads,stores}. Note that
171 : region_{ld,st}_sz[i]<2^32, and the mapped host range does not wrap
172 : around in host address space or overlap with any other usages.
173 :
174 : region_{ld,st}_sz[0] and region_{ld,st}_sz[5] are zero such that
175 : requests to access data from a positive sz range in these regions
176 : will fail, making regions 0 and 5 unreadable and unwritable. As
177 : such, region_haddr[0] and region_haddr[5] are arbitrary; NULL is
178 : used as the obvious default.
179 :
180 : region_st_sz[1] is also zero such that requests to store data to
181 : any positive sz range in this region will fail, making region 1
182 : unwritable.
183 :
184 : The entries at FD_VM_INPUT_REGION cache the most recently used
185 : input sub-region. region_voff gives its start offset within the
186 : input address space and is zero for regions 0..3. A zero
187 : region_st_sz forces the first write to an untouched account through
188 : the slow path. */
189 :
190 : /* FIXME: If accessing memory beyond the end of the current heap
191 : region is not allowed, sol_alloc_free will need to update the tlb
192 : arrays during program execution (this is trivial). At the same
193 : time, given sol_alloc_free is deprecated, this is unlikely to be
194 : the case. */
195 :
196 : ulong region_haddr[6];
197 : uint region_ld_sz[6];
198 : uint region_st_sz[6];
199 :
200 : /* fd_vm_input_region_t and fd_vm_acc_region_meta_t arrays are passed in by the bpf
201 : loaders into fd_vm_init.
202 : TODO: It might make more sense to allocate space for these in the VM. */
203 : fd_vm_input_region_t * input_mem_regions; /* An array of input mem regions represent the input region.
204 : The virtual addresses of each region are contiguous and
205 : strictly increasing. */
206 : uint input_mem_regions_cnt;
207 : uint region_voff[5]; /* Start offset of each cached mapping; zero for regions 0..3 */
208 : uint input_cur_idx; /* Cached input sub-region index */
209 : uint input_prev_idx; /* Previous input sub-region index */
210 : fd_vm_acc_region_meta_t * acc_region_metas; /* Represents a mapping from the instruction account indices
211 : from the instruction context to the input memory region index
212 : of the account's metadata region in the input space. */
213 :
214 : fd_sha256_t * sha; /* Pre-joined SHA instance. This should be re-initialised before every use. */
215 :
216 : ulong magic; /* ==FD_VM_MAGIC */
217 :
218 : ulong stack_frame_sz; /* Stack frame size in bytes; 0 for manually adjusted stacks */
219 :
220 : /* Agave uses the segv vaddr in several different cases, including:
221 : - Determining whether or not to return a regular or stack access violation
222 : - Determining the instruction error code to return on account
223 : data access violations. */
224 : ulong segv_vaddr;
225 : ulong segv_access_len;
226 :
227 : ulong sbpf_version; /* SBPF version, SIMD-0161 */
228 :
229 : uchar is_deprecated; /* Additional CPI checks if initialized by a deprecated loader */
230 : uchar segv_access_type;
231 : int dump_syscall_to_pb; /* If true, syscalls will be dumped to the specified output directory */
232 :
233 : /* fd_vm_new zero-initializes up to this point only */
234 :
235 : /* shadow stack, indexed [0,frame_cnt), if frame_cnt>0, 0/frame_cnt-1 is
236 : bottom/top. Lazily initialized. */
237 : fd_vm_shadow_t shadow[ FD_VM_STACK_FRAME_MAX ] __attribute__((aligned(16)));
238 :
239 : /* Stack storage, indexed [0,FD_VM_STACK_MAX). Fixed stack frames
240 : occupy FD_VM_STACK_FRAME_SZ bytes each.
241 : reg[10] holds a VM stack address, not an offset into this array. */
242 : uchar stack[ FD_VM_STACK_MAX ] __attribute__((aligned(FD_VM_HOST_REGION_ALIGN)));
243 :
244 : /* syscall heap, [0,heap_sz) used, [heap_sz,heap_max) free. */
245 : uchar heap[ FD_VM_HEAP_MAX ] __attribute__((aligned(FD_VM_HOST_REGION_ALIGN)));
246 : };
247 :
248 : /* FIXME: MOVE ABOVE INTO PRIVATE WHEN CONSTRUCTORS READY */
249 : /**********************************************************************/
250 :
251 : FD_PROTOTYPES_BEGIN
252 :
253 : /* FIXME: FD_VM_T NEEDS PROPER CONSTRUCTORS */
254 :
255 : /* FD_VM_{ALIGN,FOOTPRINT} describe the alignment and footprint needed
256 : for a memory region to hold a fd_vm_t. ALIGN is a positive
257 : integer power of 2. FOOTPRINT is a multiple of align.
258 : These are provided to facilitate compile time declarations. */
259 255947 : #define FD_VM_ALIGN FD_VM_HOST_REGION_ALIGN
260 180 : #define FD_VM_FOOTPRINT (527856UL)
261 :
262 : /* fd_vm_{align,footprint} give the needed alignment and footprint
263 : of a memory region suitable to hold an fd_vm_t.
264 : Declaration / aligned_alloc / fd_alloca friendly (e.g. a memory
265 : region declared as "fd_vm_t _vm[1];", or created by
266 : "aligned_alloc(alignof(fd_vm_t),sizeof(fd_vm_t))" or created
267 : by "fd_alloca(alignof(fd_vm_t),sizeof(fd_vm_t))" will all
268 : automatically have the needed alignment and footprint).
269 : fd_vm_{align,footprint} return the same value as
270 : FD_VM_{ALIGN,FOOTPRINT}. */
271 : FD_FN_CONST ulong
272 : fd_vm_align( void );
273 :
274 : FD_FN_CONST ulong
275 : fd_vm_footprint( void );
276 :
277 127876 : #define FD_VM_MAGIC (0xF17EDA2CEF0) /* FIREDANCE SBPF V0 */
278 :
279 : /* fd_vm_new formats memory region with suitable alignment and
280 : footprint suitable for holding a fd_vm_t. Assumes
281 : shmem points on the caller to the first byte of the memory region
282 : owned by the caller to use. Returns shmem on success and NULL on
283 : failure (logs details). The memory region will be owned by the state
284 : on successful return. The caller is not joined on return. */
285 :
286 : void *
287 : fd_vm_new( void * shmem );
288 :
289 : /* fd_vm_join joins the caller to a vm.
290 : Assumes shmem points to the first byte of the memory region holding
291 : the vm. Returns a local handle to the join on success (this is
292 : not necessarily a simple cast of the address) and NULL on failure
293 : (logs details). */
294 : fd_vm_t *
295 : fd_vm_join( void * shmem );
296 :
297 : /* fd_vm_init initializes the given fd_vm_t struct, checking that it is
298 : not null and has the correct magic value.
299 :
300 : It modifies the vm object and also returns the object for convenience.
301 :
302 : FIXME: we should split out the memory mapping setup from this function
303 : to handle those errors separately. */
304 : fd_vm_t *
305 : fd_vm_init(
306 : fd_vm_t * vm,
307 : fd_exec_instr_ctx_t * instr_ctx,
308 : ulong heap_max,
309 : ulong entry_cu,
310 : uchar const * rodata,
311 : ulong rodata_sz,
312 : ulong const * text,
313 : ulong text_cnt,
314 : ulong text_off,
315 : ulong text_sz,
316 : ulong entry_pc,
317 : ulong const * calldests,
318 : ulong sbpf_version,
319 : fd_vm_trace_t * trace,
320 : fd_sha256_t * sha,
321 : fd_vm_input_region_t * mem_regions,
322 : uint mem_regions_cnt,
323 : fd_vm_acc_region_meta_t * acc_region_metas,
324 : uchar is_deprecated,
325 : int dump_syscall_to_pb,
326 : ulong const reg[ FD_VM_REG_CNT ] );
327 :
328 : /* fd_vm_leave leaves the caller's current local join to a vm.
329 : Returns a pointer to the memory region holding the vm on success
330 : (this is not necessarily a simple cast of the
331 : address) and NULL on failure (logs details). The caller is not
332 : joined on successful return. */
333 : void *
334 : fd_vm_leave( fd_vm_t * vm );
335 :
336 : /* fd_vm_delete unformats a memory region that holds a vm.
337 : Assumes shmem points on the caller to the first
338 : byte of the memory region holding the state and that nobody is
339 : joined. Returns a pointer to the memory region on success and NULL
340 : on failure (logs details). The caller has ownership of the memory
341 : region on successful return. */
342 : void *
343 : fd_vm_delete( void * shmem );
344 :
345 : /* fd_vm_validate validates the sBPF program in the given vm. Returns
346 : success or an error code. Called before executing a sBPF program.
347 : FIXME: DOCUMENT BETTER */
348 :
349 : FD_FN_PURE int
350 : fd_vm_validate( fd_vm_t const * vm );
351 :
352 : /* fd_vm_is_check_align_enabled returns 1 if the vm should check alignment
353 : when doing memory translation. */
354 : FD_FN_PURE static inline int
355 47427 : fd_vm_is_check_align_enabled( fd_vm_t const * vm ) {
356 47427 : return !vm->is_deprecated;
357 47427 : }
358 :
359 : /* fd_vm_is_check_size_enabled returns 1 if the vm should check size
360 : when doing memory translation. */
361 : FD_FN_PURE static inline int
362 0 : fd_vm_is_check_size_enabled( fd_vm_t const * vm ) {
363 0 : return !vm->is_deprecated;
364 0 : }
365 :
366 : /* fd_vm_exec runs vm from program start to program halt or program
367 : fault, appending an execution trace if vm is attached to a trace.
368 : vm must be successfully initialized with fd_vm_init before each call.
369 :
370 : Returns FD_VM_SUCCESS on success and an FD_VM_ERR_EBPF_* code on
371 : failure. A syscall failure returns FD_VM_ERR_EBPF_SYSCALL_ERROR.
372 :
373 : On failure, pc, ic and cu reflect the interpreter halt path. A
374 : compute-budget failure sets cu to zero. The reported ic need not
375 : count every instruction attempted before a budget failure. Syscalls
376 : may have partially modified VM state before returning an error.
377 :
378 : fd_vm_exec_trace runs with tracing and requires vm to be attached to
379 : a trace. fd_vm_exec_notrace runs without tracing even if vm
380 : is attached to a trace. */
381 :
382 : int
383 : fd_vm_exec_trace( fd_vm_t * vm );
384 :
385 : int
386 : fd_vm_exec_notrace( fd_vm_t * vm );
387 :
388 : static inline int
389 1452 : fd_vm_exec( fd_vm_t * vm ) {
390 1452 : if( FD_UNLIKELY( vm->trace ) ) return fd_vm_exec_trace ( vm );
391 1437 : else return fd_vm_exec_notrace( vm );
392 1452 : }
393 :
394 : FD_PROTOTYPES_END
395 :
396 : #endif /* HEADER_fd_src_flamenco_vm_fd_vm_h */
|