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