Line data Source code
1 : #ifndef HEADER_fd_src_flamenco_vm_fd_vm_private_h
2 : #define HEADER_fd_src_flamenco_vm_fd_vm_private_h
3 :
4 : #include "fd_vm.h"
5 :
6 : #include "../../ballet/sbpf/fd_sbpf_instr.h"
7 : #include "../../ballet/sbpf/fd_sbpf_opcodes.h"
8 : #include "../../ballet/murmur3/fd_murmur3.h"
9 : #include "../runtime/context/fd_exec_txn_ctx.h"
10 : #include "../runtime/fd_runtime_const.h"
11 : #include "../features/fd_features.h"
12 : #include "fd_vm_base.h"
13 :
14 : /* FD_VM_ALIGN_RUST_{} define the alignments for relevant rust types.
15 : Alignments are derived with std::mem::align_of::<T>() and are enforced
16 : by the VM (with the exception of v1 loader).
17 :
18 : In our implementation, when calling FD_VM_MEM_HADDR_ST / FD_VM_MEM_HADDR_LD,
19 : we need to make sure we're passing the correct alignment based on the Rust
20 : type in the corresponding mapping in Agave.
21 :
22 : FD_VM_ALIGN_RUST_{} has been generated with this Rust code:
23 : ```rust
24 : pub type Epoch = u64;
25 : pub struct Pubkey(pub [u8; 32]);
26 : pub struct AccountMeta {
27 : pub lamports: u64,
28 : pub rent_epoch: Epoch,
29 : pub owner: Pubkey,
30 : pub executable: bool,
31 : }
32 :
33 : pub struct PodScalar(pub [u8; 32]);
34 :
35 : fn main() {
36 : println!("u8: {}", std::mem::align_of::<u8>());
37 : println!("u32: {}", std::mem::align_of::<u32>());
38 : println!("u64: {}", std::mem::align_of::<u64>());
39 : println!("u128: {}", std::mem::align_of::<u128>());
40 : println!("&[u8]: {}", std::mem::align_of::<&[u8]>());
41 : println!("AccountMeta: {}", std::mem::align_of::<AccountMeta>());
42 : println!("PodScalar: {}", std::mem::align_of::<PodScalar>());
43 : println!("Pubkey: {}", std::mem::align_of::<Pubkey>());
44 : }
45 : ``` */
46 :
47 93 : #define FD_VM_ALIGN_RUST_U8 (1UL)
48 : #define FD_VM_ALIGN_RUST_U32 (4UL)
49 15 : #define FD_VM_ALIGN_RUST_I32 (4UL)
50 : #define FD_VM_ALIGN_RUST_U64 (8UL)
51 : #define FD_VM_ALIGN_RUST_U128 (16UL)
52 : #define FD_VM_ALIGN_RUST_SLICE_U8_REF (8UL)
53 18 : #define FD_VM_ALIGN_RUST_POD_U8_ARRAY (1UL)
54 0 : #define FD_VM_ALIGN_RUST_PUBKEY (1UL)
55 0 : #define FD_VM_ALIGN_RUST_SYSVAR_CLOCK (8UL)
56 0 : #define FD_VM_ALIGN_RUST_SYSVAR_EPOCH_SCHEDULE (8UL)
57 0 : #define FD_VM_ALIGN_RUST_SYSVAR_RENT (8UL)
58 0 : #define FD_VM_ALIGN_RUST_SYSVAR_LAST_RESTART_SLOT (8UL)
59 : #define FD_VM_ALIGN_RUST_SYSVAR_EPOCH_REWARDS (16UL)
60 : #define FD_VM_ALIGN_RUST_STABLE_INSTRUCTION (8UL)
61 :
62 : /* fd_vm_vec_t is the in-memory representation of a vector descriptor.
63 : Equal in layout to the Rust slice header &[_] and various vector
64 : types in the C version of the syscall API. */
65 : /* FIXME: WHEN IS VADDR NULL AND/OR SZ 0 OKAY? */
66 : /* FIXME: MOVE FD_VM_RUST_VEC_T FROM SYSCALL/FD_VM_CPI.H HERE TOO? */
67 :
68 : #define FD_VM_VEC_ALIGN FD_VM_ALIGN_RUST_SLICE_U8_REF
69 : #define FD_VM_VEC_SIZE (16UL)
70 :
71 : struct __attribute__((packed)) fd_vm_vec {
72 : ulong addr; /* FIXME: NAME -> VADDR */
73 : ulong len; /* FIXME: NAME -> SZ */
74 : };
75 :
76 : typedef struct fd_vm_vec fd_vm_vec_t;
77 :
78 : FD_STATIC_ASSERT( sizeof(fd_vm_vec_t)==FD_VM_VEC_SIZE, fd_vm_vec size mismatch );
79 :
80 : /* SBPF version and features
81 : https://github.com/anza-xyz/sbpf/blob/v0.12.2/src/program.rs#L28
82 : Note: SIMDs enable or disable features, e.g. BPF instructions.
83 : If we have macros with names ENABLE vs DISABLE, we have the advantage that
84 : the condition is always pretty clear: sbpf_version <= activation_version,
85 : but the disadvantage of inconsistent names.
86 : Viceversa, calling everything ENABLE has the risk to invert a <= with a >=
87 : and create a huge mess.
88 : We define both, so hopefully it's foolproof. */
89 :
90 : #define FD_VM_SBPF_REJECT_RODATA_STACK_OVERLAP(v) ( v != FD_SBPF_V0 )
91 : #define FD_VM_SBPF_ENABLE_ELF_VADDR(v) ( v != FD_SBPF_V0 )
92 : /* SIMD-0166 */
93 805477215 : #define FD_VM_SBPF_DYNAMIC_STACK_FRAMES(v) ( v >= FD_SBPF_V1 )
94 : /* SIMD-0173 */
95 7878 : #define FD_VM_SBPF_CALLX_USES_SRC_REG(v) ( v >= FD_SBPF_V2 )
96 : #define FD_VM_SBPF_DISABLE_LDDW(v) ( v >= FD_SBPF_V2 )
97 77988 : #define FD_VM_SBPF_ENABLE_LDDW(v) ( v < FD_SBPF_V2 )
98 : #define FD_VM_SBPF_DISABLE_LE(v) ( v >= FD_SBPF_V2 )
99 38994 : #define FD_VM_SBPF_ENABLE_LE(v) ( v < FD_SBPF_V2 )
100 935856 : #define FD_VM_SBPF_MOVE_MEMORY_IX_CLASSES(v) ( v >= FD_SBPF_V2 )
101 : /* SIMD-0174 */
102 1052838 : #define FD_VM_SBPF_ENABLE_PQR(v) ( v >= FD_SBPF_V2 )
103 : #define FD_VM_SBPF_DISABLE_NEG(v) ( v >= FD_SBPF_V2 )
104 38994 : #define FD_VM_SBPF_ENABLE_NEG(v) ( v < FD_SBPF_V2 )
105 62232 : #define FD_VM_SBPF_SWAP_SUB_REG_IMM_OPERANDS(v) ( v >= FD_SBPF_V2 )
106 124464 : #define FD_VM_SBPF_EXPLICIT_SIGN_EXT(v) ( v >= FD_SBPF_V2 )
107 : /* SIMD-0178 + SIMD-0179 */
108 155976 : #define FD_VM_SBPF_STATIC_SYSCALLS(v) ( v >= FD_SBPF_V3 )
109 : /* SIMD-0189 */
110 : #define FD_VM_SBPF_ENABLE_LOWER_BYTECODE_VADDR(v) ( v >= FD_SBPF_V3 )
111 : /* enable_strict_elf_headers is defined in fd_sbpf_loader.h because it's needed
112 : by the ELF loader, not really by the VM
113 : #define FD_VM_SBPF_ENABLE_STRICTER_ELF_HEADERS(v) ( v >= FD_SBPF_V3 ) */
114 :
115 789 : #define FD_VM_OFFSET_MASK (0xffffffffUL)
116 :
117 : /* https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L32 */
118 0 : #define FD_MAX_ACCOUNT_DATA_GROWTH_PER_TRANSACTION (FD_RUNTIME_ACC_SZ_MAX * 2UL)
119 :
120 : FD_PROTOTYPES_BEGIN
121 :
122 : /* Error logging handholding assertions */
123 :
124 : #ifdef FD_RUNTIME_ERR_HANDHOLDING
125 : /* Asserts that the error and error kind are populated (non-zero) */
126 : #define FD_VM_TEST_ERR_EXISTS( vm ) \
127 : FD_TEST( vm->instr_ctx->txn_ctx->exec_err ); \
128 : FD_TEST( vm->instr_ctx->txn_ctx->exec_err_kind )
129 :
130 : /* Used prior to a FD_VM_ERR_FOR_LOG_INSTR call to deliberately
131 : bypass overwrite handholding checks.
132 : Only use this if you know what you're doing. */
133 : #define FD_VM_PREPARE_ERR_OVERWRITE( vm ) \
134 : vm->instr_ctx->txn_ctx->exec_err = 0; \
135 : vm->instr_ctx->txn_ctx->exec_err_kind = 0
136 :
137 : /* Asserts that the error and error kind are not populated (zero) */
138 : #define FD_VM_TEST_ERR_OVERWRITE( vm ) \
139 : FD_TEST( !vm->instr_ctx->txn_ctx->exec_err ); \
140 : FD_TEST( !vm->instr_ctx->txn_ctx->exec_err_kind )
141 : #else
142 0 : #define FD_VM_TEST_ERR_EXISTS( vm ) ( ( void )0 )
143 0 : #define FD_VM_PREPARE_ERR_OVERWRITE( vm ) ( ( void )0 )
144 81 : #define FD_VM_TEST_ERR_OVERWRITE( vm ) ( ( void )0 )
145 : #endif
146 :
147 : /* Log error within the instr_ctx to match Agave/Rust error. */
148 :
149 57 : #define FD_VM_ERR_FOR_LOG_EBPF( vm, err ) (__extension__({ \
150 57 : FD_VM_TEST_ERR_OVERWRITE( vm ); \
151 57 : vm->instr_ctx->txn_ctx->exec_err = err; \
152 57 : vm->instr_ctx->txn_ctx->exec_err_kind = FD_EXECUTOR_ERR_KIND_EBPF; \
153 57 : }))
154 :
155 24 : #define FD_VM_ERR_FOR_LOG_SYSCALL( vm, err ) (__extension__({ \
156 24 : FD_VM_TEST_ERR_OVERWRITE( vm ); \
157 24 : vm->instr_ctx->txn_ctx->exec_err = err; \
158 24 : vm->instr_ctx->txn_ctx->exec_err_kind = FD_EXECUTOR_ERR_KIND_SYSCALL; \
159 24 : }))
160 :
161 0 : #define FD_VM_ERR_FOR_LOG_INSTR( vm, err ) (__extension__({ \
162 0 : FD_VM_TEST_ERR_OVERWRITE( vm ); \
163 0 : vm->instr_ctx->txn_ctx->exec_err = err; \
164 0 : vm->instr_ctx->txn_ctx->exec_err_kind = FD_EXECUTOR_ERR_KIND_INSTR; \
165 0 : }))
166 :
167 747 : #define FD_VADDR_TO_REGION( _vaddr ) fd_ulong_min( (_vaddr) >> FD_VM_MEM_MAP_REGION_VIRT_ADDR_BITS, FD_VM_HIGH_REGION )
168 :
169 : /* fd_vm_instr APIs ***************************************************/
170 :
171 : /* FIXME: MIGRATE FD_SBPF_INSTR_T STUFF TO THIS API */
172 :
173 : /* fd_vm_instr returns the SBPF instruction word corresponding to the
174 : given fields. */
175 :
176 : FD_FN_CONST static inline ulong
177 : fd_vm_instr( ulong opcode, /* Assumed valid */
178 : ulong dst, /* Assumed in [0,FD_VM_REG_CNT) */
179 : ulong src, /* Assumed in [0,FD_VM_REG_CNT) */
180 : short offset,
181 16587 : uint imm ) {
182 16587 : return opcode | (dst<<8) | (src<<12) | (((ulong)(ushort)offset)<<16) | (((ulong)imm)<<32);
183 16587 : }
184 :
185 : /* fd_vm_instr_* return the SBPF instruction field for the given word.
186 : fd_vm_instr_{normal,mem}_* only apply to {normal,mem} opclass
187 : instructions. */
188 :
189 381843 : FD_FN_CONST static inline ulong fd_vm_instr_opcode( ulong instr ) { return instr & 255UL; } /* In [0,256) */
190 381843 : FD_FN_CONST static inline ulong fd_vm_instr_dst ( ulong instr ) { return ((instr>> 8) & 15UL); } /* In [0,16) */
191 381843 : FD_FN_CONST static inline ulong fd_vm_instr_src ( ulong instr ) { return ((instr>>12) & 15UL); } /* In [0,16) */
192 381843 : FD_FN_CONST static inline ulong fd_vm_instr_offset( ulong instr ) { return (ulong)(long)(short)(ushort)(instr>>16); }
193 381960 : FD_FN_CONST static inline uint fd_vm_instr_imm ( ulong instr ) { return (uint)(instr>>32); }
194 :
195 0 : FD_FN_CONST static inline ulong fd_vm_instr_opclass ( ulong instr ) { return instr & 7UL; } /* In [0,8) */
196 0 : FD_FN_CONST static inline ulong fd_vm_instr_normal_opsrc ( ulong instr ) { return (instr>>3) & 1UL; } /* In [0,2) */
197 0 : FD_FN_CONST static inline ulong fd_vm_instr_normal_opmode ( ulong instr ) { return (instr>>4) & 15UL; } /* In [0,16) */
198 0 : FD_FN_CONST static inline ulong fd_vm_instr_mem_opsize ( ulong instr ) { return (instr>>3) & 3UL; } /* In [0,4) */
199 0 : FD_FN_CONST static inline ulong fd_vm_instr_mem_opaddrmode( ulong instr ) { return (instr>>5) & 7UL; } /* In [0,16) */
200 :
201 : /* fd_vm_mem API ******************************************************/
202 :
203 : /* fd_vm_mem APIs support the fast mapping of virtual address ranges to
204 : host address ranges. Since the SBPF virtual address space consists
205 : of 4 consecutive 4GiB regions and the mapable size of each region is
206 : less than 4 GiB (as implied by FD_VM_MEM_MAP_REGION_SZ==2^32-1 and
207 : that Solana protocol limits are much smaller still), it is impossible
208 : for a valid virtual address range to span multiple regions. */
209 :
210 : /* fd_vm_mem_cfg configures the vm's tlb arrays. Assumes vm is valid
211 : and vm already has configured the rodata, stack, heap and input
212 : regions. Returns vm. */
213 :
214 : static inline fd_vm_t *
215 8064 : fd_vm_mem_cfg( fd_vm_t * vm ) {
216 8064 : vm->region_haddr[0] = 0UL; vm->region_ld_sz[0] = (uint)0UL; vm->region_st_sz[0] = (uint)0UL;
217 8064 : vm->region_haddr[FD_VM_PROG_REGION] = (ulong)vm->rodata; vm->region_ld_sz[FD_VM_PROG_REGION] = (uint)vm->rodata_sz; vm->region_st_sz[FD_VM_PROG_REGION] = (uint)0UL;
218 8064 : vm->region_haddr[FD_VM_STACK_REGION] = (ulong)vm->stack; vm->region_ld_sz[FD_VM_STACK_REGION] = (uint)FD_VM_STACK_MAX; vm->region_st_sz[FD_VM_STACK_REGION] = (uint)FD_VM_STACK_MAX;
219 8064 : vm->region_haddr[FD_VM_HEAP_REGION] = (ulong)vm->heap; vm->region_ld_sz[FD_VM_HEAP_REGION] = (uint)vm->heap_max; vm->region_st_sz[FD_VM_HEAP_REGION] = (uint)vm->heap_max;
220 8064 : vm->region_haddr[5] = 0UL; vm->region_ld_sz[5] = (uint)0UL; vm->region_st_sz[5] = (uint)0UL;
221 8064 : if( vm->direct_mapping || !vm->input_mem_regions_cnt ) {
222 : /* When direct mapping is enabled, we don't use these fields because
223 : the load and stores are fragmented. */
224 408 : vm->region_haddr[FD_VM_INPUT_REGION] = 0UL;
225 408 : vm->region_ld_sz[FD_VM_INPUT_REGION] = 0U;
226 408 : vm->region_st_sz[FD_VM_INPUT_REGION] = 0U;
227 7656 : } else {
228 7656 : vm->region_haddr[FD_VM_INPUT_REGION] = vm->input_mem_regions[0].haddr;
229 7656 : vm->region_ld_sz[FD_VM_INPUT_REGION] = vm->input_mem_regions[0].region_sz;
230 7656 : vm->region_st_sz[FD_VM_INPUT_REGION] = vm->input_mem_regions[0].region_sz;
231 7656 : }
232 8064 : return vm;
233 8064 : }
234 :
235 : /* Simplified version of Agave's `generate_access_violation()` function
236 : that simply returns either FD_VM_ERR_EBPF_ACCESS_VIOLATION or
237 : FD_VM_ERR_EBPF_STACK_ACCESS_VIOLATION. This has no consensus
238 : effects and is purely for logging purposes for fuzzing. Returns
239 : FD_VM_ERR_EBPF_STACK_ACCESS_VIOLATION if the provided vaddr is in the
240 : stack (0x200000000) and FD_VM_ERR_EBPF_ACCESS_VIOLATION otherwise.
241 :
242 : https://github.com/anza-xyz/sbpf/blob/v0.11.1/src/memory_region.rs#L834-L869 */
243 : static FD_FN_PURE inline int
244 255 : fd_vm_generate_access_violation( ulong vaddr, ulong sbpf_version ) {
245 : /* rel_offset can be negative because there is an edge case where the
246 : first "frame" right before the stack region should also throw a
247 : stack access violation. */
248 255 : long rel_offset = fd_long_sat_sub( (long)vaddr, (long)FD_VM_MEM_MAP_STACK_REGION_START );
249 255 : long stack_frame = rel_offset / (long)FD_VM_STACK_FRAME_SZ;
250 255 : if( !fd_sbpf_dynamic_stack_frames_enabled( sbpf_version ) &&
251 255 : stack_frame>=-1L && stack_frame<=(long)FD_VM_MAX_CALL_DEPTH ) {
252 0 : return FD_VM_ERR_EBPF_STACK_ACCESS_VIOLATION;
253 0 : }
254 255 : return FD_VM_ERR_EBPF_ACCESS_VIOLATION;
255 255 : }
256 :
257 : /* fd_vm_mem_haddr translates the vaddr range [vaddr,vaddr+sz) (in
258 : infinite precision math) into the non-wrapping haddr range
259 : [haddr,haddr+sz). On success, returns haddr and every byte in the
260 : haddr range is a valid address. On failure, returns sentinel and
261 : there was at least one byte in the virtual address range that did not
262 : have a corresponding byte in the host address range.
263 :
264 : IMPORTANT SAFETY TIP! When sz==0, the return value currently is
265 : arbitrary. This is often fine as there should be no
266 : actual accesses to a sz==0 region. However, this also means that
267 : testing return for sentinel is insufficient to tell if mapping
268 : failed. That is, assuming sentinel is a location that could never
269 : happen on success:
270 :
271 : sz!=0 and ret!=sentinel -> success
272 : sz!=0 and ret==sentinel -> failure
273 : sz==0 -> ignore ret, application specific handling
274 :
275 : With ~O(2) extra fast branchless instructions, the below could be
276 : tweaked in the sz==0 case to return NULL or return a non-NULL
277 : sentinel value. What is most optimal practically depends on how
278 : empty ranges and NULL vaddr handling is defined in the application.
279 :
280 : Requires ~O(10) fast branchless assembly instructions with 2 L1 cache
281 : hit loads and pretty good ILP.
282 :
283 : fd_vm_mem_haddr_fast is when the vaddr is for use when it is already
284 : known that the vaddr region has a valid mapping.
285 :
286 : These assumptions don't hold if direct mapping is enabled since input
287 : region lookups become O(log(n)). */
288 :
289 :
290 : /* fd_vm_get_input_mem_region_idx returns the index into the input memory
291 : region array with the largest region offset that is <= the offset that
292 : is passed in. This function makes NO guarantees about the input being
293 : a valid input region offset; the caller is responsible for safely handling
294 : it. */
295 : static inline ulong
296 348 : fd_vm_get_input_mem_region_idx( fd_vm_t const * vm, ulong offset ) {
297 348 : uint left = 0U;
298 348 : uint right = vm->input_mem_regions_cnt - 1U;
299 348 : uint mid = 0U;
300 :
301 576 : while( left<right ) {
302 228 : mid = (left+right) / 2U;
303 228 : if( offset>=vm->input_mem_regions[ mid ].vaddr_offset+vm->input_mem_regions[ mid ].address_space_reserved ) {
304 51 : left = mid + 1U;
305 177 : } else {
306 177 : right = mid;
307 177 : }
308 228 : }
309 348 : return left;
310 348 : }
311 :
312 : /* If the region is an account, handle the resizing logic. This logic
313 : corresponds to
314 : solana_transaction_context::TransactionContext::access_violation_handler
315 :
316 : https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L510-L581 */
317 : static inline void
318 : fd_vm_handle_input_mem_region_oob( fd_vm_t const * vm,
319 : ulong offset,
320 : ulong sz,
321 : ulong region_idx,
322 120 : uchar write ) {
323 : /* If stricter_abi_and_runtime_constraints is not enabled, we don't need to
324 : do anything */
325 120 : if( FD_UNLIKELY( !vm->stricter_abi_and_runtime_constraints ) ) {
326 102 : return;
327 102 : }
328 :
329 : /* If the access is not a write, we don't need to do anything
330 : https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L523-L525 */
331 18 : if( FD_UNLIKELY( !write ) ) {
332 0 : return;
333 0 : }
334 :
335 18 : fd_vm_input_region_t * region = &vm->input_mem_regions[ region_idx ];
336 : /* If the region is not writable, we don't need to do anything
337 : https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L526-L529 */
338 18 : if( FD_UNLIKELY( !region->is_writable ) ) {
339 0 : return;
340 0 : }
341 :
342 : /* Calculate the requested length
343 : https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L532-L535 */
344 18 : ulong requested_len = fd_ulong_sat_sub( fd_ulong_sat_add( offset, sz ), region->vaddr_offset );
345 18 : if( FD_UNLIKELY( requested_len > region->address_space_reserved ) ) {
346 18 : return;
347 18 : }
348 :
349 : /* Calculate the remaining allowed growth
350 : https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L549-L551 */
351 0 : ulong remaining_allowed_growth = fd_ulong_sat_sub(
352 0 : FD_MAX_ACCOUNT_DATA_GROWTH_PER_TRANSACTION,
353 0 : vm->instr_ctx->txn_ctx->accounts_resize_delta );
354 :
355 : /* If the requested length is greater than the size of the region,
356 : resize the region
357 : https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L553-L571 */
358 0 : if( FD_UNLIKELY( requested_len > region->region_sz ) ) {
359 : /* Calculate the new region size
360 : https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L558-L560 */
361 0 : ulong new_region_sz = fd_ulong_min(
362 0 : fd_ulong_min( region->address_space_reserved, FD_RUNTIME_ACC_SZ_MAX ),
363 0 : fd_ulong_sat_add( region->region_sz, remaining_allowed_growth ) );
364 :
365 : /* Resize the account and the region
366 : https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L569-L570 */
367 0 : if( FD_UNLIKELY( new_region_sz > region->region_sz ) ) {
368 0 : vm->instr_ctx->txn_ctx->accounts_resize_delta = fd_ulong_sat_sub(
369 0 : fd_ulong_sat_add( vm->instr_ctx->txn_ctx->accounts_resize_delta, new_region_sz ),
370 0 : region->region_sz );
371 :
372 0 : fd_txn_account_resize( vm->acc_region_metas[ region->acc_region_meta_idx ].acct, requested_len );
373 0 : region->region_sz = (uint)new_region_sz;
374 0 : }
375 0 : }
376 0 : }
377 :
378 : /* fd_vm_find_input_mem_region returns the translated haddr for a given
379 : offset into the input region. If an offset/sz is invalid or if an
380 : illegal write is performed, the sentinel value is returned. If the offset
381 : provided is too large, it will choose the upper-most region as the
382 : region_idx. However, it will get caught for being too large of an access
383 : in the multi-region checks. */
384 : static inline ulong
385 : fd_vm_find_input_mem_region( fd_vm_t const * vm,
386 : ulong offset,
387 : ulong sz,
388 : uchar write,
389 348 : ulong sentinel ) {
390 348 : if( FD_UNLIKELY( vm->input_mem_regions_cnt==0 ) ) {
391 0 : return sentinel; /* Access is too large */
392 0 : }
393 :
394 : /* Binary search to find the correct memory region. If direct mapping is not
395 : enabled, then there is only 1 memory region which spans the input region. */
396 348 : ulong region_idx = fd_vm_get_input_mem_region_idx( vm, offset );
397 348 : if( FD_UNLIKELY( region_idx>=vm->input_mem_regions_cnt ) ) {
398 0 : return sentinel; /* Region not found */
399 0 : }
400 :
401 348 : ulong bytes_in_region = fd_ulong_sat_sub( vm->input_mem_regions[ region_idx ].region_sz,
402 348 : fd_ulong_sat_sub( offset, vm->input_mem_regions[ region_idx ].vaddr_offset ) );
403 :
404 : /* If the access is out of bounds, invoke the callback to handle the out of bounds access.
405 : This potentially resizes the region if necessary. */
406 348 : if( FD_UNLIKELY( sz>bytes_in_region ) ) {
407 120 : fd_vm_handle_input_mem_region_oob( vm, offset, sz, region_idx, write );
408 120 : }
409 :
410 : /* After potentially resizing, re-check the bounds */
411 348 : bytes_in_region = fd_ulong_sat_sub( vm->input_mem_regions[ region_idx ].region_sz,
412 348 : fd_ulong_sat_sub( offset, vm->input_mem_regions[ region_idx ].vaddr_offset ) );
413 : /* If the access is still out of bounds, return the sentinel */
414 348 : if( FD_UNLIKELY( sz>bytes_in_region ) ) {
415 120 : return sentinel;
416 120 : }
417 :
418 228 : if( FD_UNLIKELY( write && vm->input_mem_regions[ region_idx ].is_writable==0U ) ) {
419 0 : return sentinel; /* Illegal write */
420 0 : }
421 :
422 228 : ulong start_region_idx = region_idx;
423 :
424 228 : ulong adjusted_haddr = vm->input_mem_regions[ start_region_idx ].haddr + offset - vm->input_mem_regions[ start_region_idx ].vaddr_offset;
425 228 : return adjusted_haddr;
426 228 : }
427 :
428 :
429 : static inline ulong
430 : fd_vm_mem_haddr( fd_vm_t const * vm,
431 : ulong vaddr,
432 : ulong sz,
433 : ulong const * vm_region_haddr, /* indexed [0,6) */
434 : uint const * vm_region_sz, /* indexed [0,6) */
435 : uchar write, /* 1 if the access is a write, 0 if it is a read */
436 747 : ulong sentinel ) {
437 747 : ulong region = FD_VADDR_TO_REGION( vaddr );
438 747 : ulong offset = vaddr & FD_VM_OFFSET_MASK;
439 :
440 : /* Stack memory regions have 4kB unmapped "gaps" in-between each frame, which only exist if...
441 : - dynamic stack frames are not enabled (!(SBPF version >= SBPF_V1))
442 : https://github.com/anza-xyz/agave/blob/v2.2.12/programs/bpf_loader/src/lib.rs#L344-L351
443 : */
444 747 : if( FD_UNLIKELY( region==FD_VM_STACK_REGION &&
445 747 : !fd_sbpf_dynamic_stack_frames_enabled( vm->sbpf_version ) ) ) {
446 : /* If an access starts in a gap region, that is an access violation */
447 0 : if( FD_UNLIKELY( !!(vaddr & 0x1000) ) ) {
448 0 : return sentinel;
449 0 : }
450 :
451 : /* To account for the fact that we have gaps in the virtual address space but not in the
452 : physical address space, we need to subtract from the offset the size of all the virtual
453 : gap frames underneath it.
454 :
455 : https://github.com/solana-labs/rbpf/blob/b503a1867a9cfa13f93b4d99679a17fe219831de/src/memory_region.rs#L147-L149 */
456 0 : ulong gap_mask = 0xFFFFFFFFFFFFF000;
457 0 : offset = ( ( offset & gap_mask ) >> 1 ) | ( offset & ~gap_mask );
458 0 : }
459 :
460 747 : ulong region_sz = (ulong)vm_region_sz[ region ];
461 747 : ulong sz_max = region_sz - fd_ulong_min( offset, region_sz );
462 :
463 : /* If the region is an account, handle the resizing logic. This logic corresponds to
464 : solana_transaction_context::TransactionContext::access_violation_handler
465 :
466 : https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L510-L581 */
467 747 : if( region==FD_VM_INPUT_REGION ) {
468 348 : return fd_vm_find_input_mem_region( vm, offset, sz, write, sentinel );
469 348 : }
470 :
471 : # ifdef FD_VM_INTERP_MEM_TRACING_ENABLED
472 : if ( FD_LIKELY( sz<=sz_max ) ) {
473 : fd_vm_trace_event_mem( vm->trace, write, vaddr, sz, vm_region_haddr[ region ] + offset );
474 : }
475 : # endif
476 399 : return fd_ulong_if( sz<=sz_max, vm_region_haddr[ region ] + offset, sentinel );
477 747 : }
478 :
479 : static inline ulong
480 : fd_vm_mem_haddr_fast( fd_vm_t const * vm,
481 : ulong vaddr,
482 0 : ulong const * vm_region_haddr ) { /* indexed [0,6) */
483 0 : ulong region = FD_VADDR_TO_REGION( vaddr );
484 0 : ulong offset = vaddr & FD_VM_OFFSET_MASK;
485 0 : if( FD_UNLIKELY( region==FD_VM_INPUT_REGION ) ) {
486 0 : return fd_vm_find_input_mem_region( vm, offset, 1UL, 0, 0UL );
487 0 : }
488 0 : return vm_region_haddr[ region ] + offset;
489 0 : }
490 :
491 54 : FD_FN_PURE static inline ulong fd_vm_mem_ld_1( ulong haddr ) {
492 54 : return (ulong)*(uchar const *)haddr;
493 54 : }
494 :
495 60 : FD_FN_PURE static inline ulong fd_vm_mem_ld_2( ulong haddr ) {
496 60 : ushort t;
497 60 : memcpy( &t, (void const *)haddr, sizeof(ushort) );
498 60 : return (ulong)t;
499 60 : }
500 :
501 60 : FD_FN_PURE static inline ulong fd_vm_mem_ld_4( ulong haddr ) {
502 60 : uint t;
503 60 : memcpy( &t, (void const *)haddr, sizeof(uint) );
504 60 : return (ulong)t;
505 60 : }
506 :
507 42 : FD_FN_PURE static inline ulong fd_vm_mem_ld_8( ulong haddr ) {
508 42 : ulong t;
509 42 : memcpy( &t, (void const *)haddr, sizeof(ulong) );
510 42 : return t;
511 42 : }
512 :
513 6 : static inline void fd_vm_mem_st_1( ulong haddr, uchar val ) {
514 6 : *(uchar *)haddr = val;
515 6 : }
516 :
517 : static inline void fd_vm_mem_st_2( ulong haddr,
518 6 : ushort val ) {
519 6 : memcpy( (void *)haddr, &val, sizeof(ushort) );
520 6 : }
521 :
522 : static inline void fd_vm_mem_st_4( ulong haddr,
523 6 : uint val ) {
524 6 : memcpy( (void *)haddr, &val, sizeof(uint) );
525 6 : }
526 :
527 : static inline void fd_vm_mem_st_8( ulong haddr,
528 6 : ulong val ) {
529 6 : memcpy( (void *)haddr, &val, sizeof(ulong) );
530 6 : }
531 :
532 : FD_PROTOTYPES_END
533 :
534 : #endif /* HEADER_fd_src_flamenco_vm_fd_vm_private_h */
|