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 6795 : #define FD_VM_ALIGN_RUST_U8 (1UL)
44 : #define FD_VM_ALIGN_RUST_U32 (4UL)
45 469 : #define FD_VM_ALIGN_RUST_I32 (4UL)
46 0 : #define FD_VM_ALIGN_RUST_U64 (8UL)
47 : #define FD_VM_ALIGN_RUST_SLICE_U8_REF (8UL)
48 1055 : #define FD_VM_ALIGN_RUST_POD_U8_ARRAY (1UL)
49 35 : #define FD_VM_ALIGN_RUST_PUBKEY (1UL)
50 104 : #define FD_VM_ALIGN_RUST_SYSVAR_CLOCK (8UL)
51 71 : #define FD_VM_ALIGN_RUST_SYSVAR_EPOCH_SCHEDULE (8UL)
52 131 : #define FD_VM_ALIGN_RUST_SYSVAR_RENT (8UL)
53 86 : #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 0 : #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 : Vice versa, 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 5553865 : #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 : /* SIMD-0174 */
88 789941 : #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 */
89 63248 : #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 */
90 31624 : #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 */
91 19887 : #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 */
92 : /* SIMD-0173 */
93 4075 : #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 */
94 39774 : #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 */
95 19887 : #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 */
96 477288 : #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 */
97 : /* SIMD-0178 */
98 15812 : #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 */
99 : /* SIMD-0189 */
100 : #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 */
101 31113 : #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 */
102 : /* SIMD-0377 */
103 351939 : #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 */
104 2879 : #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 */
105 :
106 684905 : #define FD_VM_OFFSET_MASK (0xffffffffUL)
107 :
108 : /* https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L32 */
109 0 : #define FD_MAX_ACCOUNT_DATA_GROWTH_PER_TRANSACTION ((long)(FD_RUNTIME_ACC_SZ_MAX * 2UL))
110 :
111 : /* Kept equal to the canonical constant in fd_runtime_const.h, which the
112 : bpf loader serialization buffer bound relies on. */
113 : FD_STATIC_ASSERT( FD_MAX_ACCOUNT_DATA_GROWTH_PER_TRANSACTION==(long)FD_RUNTIME_ACC_DATA_GROWTH_MAX_PER_TXN, account_data_growth_cap );
114 :
115 : FD_PROTOTYPES_BEGIN
116 :
117 : /* Error logging handholding assertions */
118 :
119 : #ifdef FD_RUNTIME_ERR_HANDHOLDING
120 : /* Asserts that the error and error kind are populated (non-zero) */
121 : #define FD_VM_TEST_ERR_EXISTS( vm ) \
122 : FD_TEST( vm->instr_ctx->txn_out->err.exec_err ); \
123 : FD_TEST( vm->instr_ctx->txn_out->err.exec_err_kind )
124 :
125 : /* Used prior to a FD_VM_ERR_FOR_LOG_INSTR call to deliberately
126 : bypass overwrite handholding checks.
127 : Only use this if you know what you're doing. */
128 : #define FD_VM_PREPARE_ERR_OVERWRITE( vm ) \
129 : vm->instr_ctx->txn_out->err.exec_err = 0; \
130 : vm->instr_ctx->txn_out->err.exec_err_kind = 0
131 :
132 : /* Asserts that the error and error kind are not populated (zero) */
133 : #define FD_VM_TEST_ERR_OVERWRITE( vm ) \
134 : FD_TEST( !vm->instr_ctx->txn_out->err.exec_err ); \
135 : FD_TEST( !vm->instr_ctx->txn_out->err.exec_err_kind )
136 : #else
137 920 : #define FD_VM_TEST_ERR_EXISTS( vm ) ( ( void )0 )
138 2398 : #define FD_VM_PREPARE_ERR_OVERWRITE( vm ) ( ( void )0 )
139 4658 : #define FD_VM_TEST_ERR_OVERWRITE( vm ) ( ( void )0 )
140 : #endif
141 :
142 : /* Log error within the instr_ctx to match Agave/Rust error. */
143 :
144 2919 : #define FD_VM_ERR_FOR_LOG_EBPF( vm, err_ ) (__extension__({ \
145 2919 : FD_VM_TEST_ERR_OVERWRITE( vm ); \
146 2919 : vm->instr_ctx->txn_out->err.exec_err = err_; \
147 2919 : vm->instr_ctx->txn_out->err.exec_err_kind = FD_EXECUTOR_ERR_KIND_EBPF; \
148 2919 : }))
149 :
150 1002 : #define FD_VM_ERR_FOR_LOG_SYSCALL( vm, err_ ) (__extension__({ \
151 1002 : FD_VM_TEST_ERR_OVERWRITE( vm ); \
152 1002 : vm->instr_ctx->txn_out->err.exec_err = err_; \
153 1002 : vm->instr_ctx->txn_out->err.exec_err_kind = FD_EXECUTOR_ERR_KIND_SYSCALL; \
154 1002 : }))
155 :
156 737 : #define FD_VM_ERR_FOR_LOG_INSTR( vm, err_ ) (__extension__({ \
157 737 : FD_VM_TEST_ERR_OVERWRITE( vm ); \
158 737 : vm->instr_ctx->txn_out->err.exec_err = err_; \
159 737 : vm->instr_ctx->txn_out->err.exec_err_kind = FD_EXECUTOR_ERR_KIND_INSTR; \
160 737 : }))
161 :
162 684221 : #define FD_VADDR_TO_REGION( _vaddr ) fd_ulong_min( (_vaddr) >> FD_VM_MEM_MAP_REGION_VIRT_ADDR_BITS, FD_VM_HIGH_REGION )
163 :
164 : /* fd_vm_instr APIs ***************************************************/
165 :
166 : /* FIXME: MIGRATE FD_SBPF_INSTR_T STUFF TO THIS API */
167 :
168 : /* fd_vm_instr returns the SBPF instruction word corresponding to the
169 : given fields. */
170 :
171 : FD_FN_CONST static inline ulong
172 : fd_vm_instr( ulong opcode, /* Assumed valid */
173 : ulong dst, /* Assumed in [0,FD_VM_REG_CNT) */
174 : ulong src, /* Assumed in [0,FD_VM_REG_CNT) */
175 : short offset,
176 0 : uint imm ) {
177 0 : return opcode | (dst<<8) | (src<<12) | (((ulong)(ushort)offset)<<16) | (((ulong)imm)<<32);
178 0 : }
179 :
180 : /* fd_vm_instr_* return the SBPF instruction field for the given word.
181 : fd_vm_instr_{normal,mem}_* only apply to {normal,mem} opclass
182 : instructions. */
183 :
184 3217635 : FD_FN_CONST static inline ulong fd_vm_instr_opcode( ulong instr ) { return instr & 255UL; } /* In [0,256) */
185 3217103 : FD_FN_CONST static inline ulong fd_vm_instr_dst ( ulong instr ) { return ((instr>> 8) & 15UL); } /* In [0,16) */
186 3216900 : FD_FN_CONST static inline ulong fd_vm_instr_src ( ulong instr ) { return ((instr>>12) & 15UL); } /* In [0,16) */
187 3216885 : FD_FN_CONST static inline ulong fd_vm_instr_offset( ulong instr ) { return (ulong)(long)(short)(ushort)(instr>>16); }
188 3524927 : FD_FN_CONST static inline uint fd_vm_instr_imm ( ulong instr ) { return (uint)(instr>>32); }
189 :
190 0 : FD_FN_CONST static inline ulong fd_vm_instr_opclass ( ulong instr ) { return instr & 7UL; } /* In [0,8) */
191 0 : FD_FN_CONST static inline ulong fd_vm_instr_normal_opsrc ( ulong instr ) { return (instr>>3) & 1UL; } /* In [0,2) */
192 0 : FD_FN_CONST static inline ulong fd_vm_instr_normal_opmode ( ulong instr ) { return (instr>>4) & 15UL; } /* In [0,16) */
193 0 : FD_FN_CONST static inline ulong fd_vm_instr_mem_opsize ( ulong instr ) { return (instr>>3) & 3UL; } /* In [0,4) */
194 0 : FD_FN_CONST static inline ulong fd_vm_instr_mem_opaddrmode( ulong instr ) { return (instr>>5) & 7UL; } /* In [0,8) */
195 :
196 : /* fd_vm_mem API ******************************************************/
197 :
198 : /* fd_vm_mem APIs support the fast mapping of virtual address ranges to
199 : host address ranges. The SBPF virtual address space consists of
200 : 5 consecutive 4 GiB regions (see fd_vm_base.h for layout). The
201 : mappable size of each region is less than 4 GiB (as implied by
202 : FD_VM_MEM_MAP_REGION_SZ==2^32-1 and that Solana protocol limits are
203 : much smaller still), so a valid virtual address range cannot span
204 : multiple regions. */
205 :
206 : /* fd_vm_mem_cfg configures the vm's tlb arrays. Assumes vm is valid
207 : and vm already has configured the rodata, stack, heap and input
208 : regions. Returns vm. */
209 :
210 : static inline fd_vm_t *
211 15565 : fd_vm_mem_cfg( fd_vm_t * vm ) {
212 15565 : if( FD_VM_SBPF_ENABLE_LOWER_RODATA_VADDR( vm->sbpf_version ) ) {
213 : /* In SBPF V3, rodata is at vaddr 0:
214 : [rodata@0, empty@0x100000000, stack@0x200000000, heap@0x300000000, input@0x400000000]
215 :
216 : This is so that we don't need to do any relocations - all rodata
217 : accesses are direct offsets from 0.
218 :
219 : https://github.com/anza-xyz/sbpf/blob/v0.14.4/src/elf.rs#L358-L362
220 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.4/syscalls/src/lib.rs#L346 */
221 1420 : vm->region_haddr[0] = (ulong)vm->rodata; vm->region_ld_sz[0] = (uint)vm->rodata_sz; vm->region_st_sz[0] = (uint)0UL;
222 1420 : 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;
223 14145 : } else {
224 : /* V0-V2: region 0 unused, rodata at region 1 (vaddr 0x100000000) */
225 14145 : vm->region_haddr[0] = 0UL; vm->region_ld_sz[0] = (uint)0UL; vm->region_st_sz[0] = (uint)0UL;
226 14145 : 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;
227 14145 : }
228 15565 : vm->region_haddr[FD_VM_STACK_REGION] = (ulong)vm->stack; vm->region_ld_sz[FD_VM_STACK_REGION] = (uint)vm->stack_clean; vm->region_st_sz[FD_VM_STACK_REGION] = (uint)vm->stack_clean;
229 15565 : vm->region_haddr[FD_VM_HEAP_REGION] = (ulong)vm->heap; vm->region_ld_sz[FD_VM_HEAP_REGION] = (uint)vm->heap_clean; vm->region_st_sz[FD_VM_HEAP_REGION] = (uint)vm->heap_clean;
230 15565 : vm->region_haddr[5] = 0UL; vm->region_ld_sz[5] = (uint)0UL; vm->region_st_sz[5] = (uint)0UL;
231 15565 : return vm;
232 15565 : }
233 :
234 : /* Simplified version of Agave's `generate_access_violation()` function
235 : that simply returns either FD_VM_ERR_EBPF_ACCESS_VIOLATION or
236 : FD_VM_ERR_EBPF_STACK_ACCESS_VIOLATION. This has no consensus
237 : effects and is purely for logging purposes for fuzzing. Returns
238 : FD_VM_ERR_EBPF_STACK_ACCESS_VIOLATION if the provided vaddr is in the
239 : stack (0x200000000) and FD_VM_ERR_EBPF_ACCESS_VIOLATION otherwise.
240 :
241 : https://github.com/anza-xyz/sbpf/blob/v0.11.1/src/memory_region.rs#L834-L869 */
242 : static FD_FN_PURE inline int
243 1507 : fd_vm_generate_access_violation( ulong vaddr, ulong sbpf_version ) {
244 : /* rel_offset can be negative because there is an edge case where the
245 : first "frame" right before the stack region should also throw a
246 : stack access violation. */
247 1507 : long rel_offset = fd_long_sat_sub( (long)vaddr, (long)FD_VM_MEM_MAP_STACK_REGION_START );
248 1507 : long stack_frame = rel_offset / (long)FD_VM_STACK_FRAME_SZ;
249 1507 : if( !fd_sbpf_manual_stack_frame_bump_enabled( sbpf_version ) &&
250 1507 : stack_frame>=-1L && stack_frame<=(long)FD_VM_MAX_CALL_DEPTH ) {
251 11 : return FD_VM_ERR_EBPF_STACK_ACCESS_VIOLATION;
252 11 : }
253 1496 : return FD_VM_ERR_EBPF_ACCESS_VIOLATION;
254 1507 : }
255 :
256 : /* fd_vm_mem_haddr translates the vaddr range [vaddr,vaddr+sz) (in
257 : infinite precision math) into the non-wrapping haddr range
258 : [haddr,haddr+sz). On success, returns haddr and every byte in the
259 : haddr range is a valid address. On failure, returns sentinel and
260 : there was at least one byte in the virtual address range that did not
261 : have a corresponding byte in the host address range.
262 :
263 : IMPORTANT SAFETY TIP! When sz==0, the return value currently is
264 : arbitrary. This is often fine as there should be no
265 : actual accesses to a sz==0 region. However, this also means that
266 : testing return for sentinel is insufficient to tell if mapping
267 : failed. That is, assuming sentinel is a location that could never
268 : happen on success:
269 :
270 : sz!=0 and ret!=sentinel -> success
271 : sz!=0 and ret==sentinel -> failure
272 : sz==0 -> ignore ret, application specific handling
273 :
274 : With ~O(2) extra fast branchless instructions, the below could be
275 : tweaked in the sz==0 case to return NULL or return a non-NULL
276 : sentinel value. What is most optimal practically depends on how
277 : empty ranges and NULL vaddr handling is defined in the application. */
278 :
279 :
280 : /* fd_vm_get_input_mem_region_idx returns the index into the input memory
281 : region array with the largest region offset that is <= the offset that
282 : is passed in. This function makes NO guarantees about the input being
283 : a valid input region offset; the caller is responsible for safely handling
284 : it. */
285 : static inline ulong
286 27502 : fd_vm_get_input_mem_region_idx( fd_vm_t const * vm, ulong offset ) {
287 27502 : uint left = 0U;
288 27502 : uint right = vm->input_mem_regions_cnt - 1U;
289 27502 : uint mid = 0U;
290 :
291 139834 : while( left<right ) {
292 112332 : mid = (left+right) / 2U;
293 112332 : if( offset>=vm->input_mem_regions[ mid ].vaddr_offset+vm->input_mem_regions[ mid ].address_space_reserved ) {
294 42857 : left = mid + 1U;
295 69475 : } else {
296 69475 : right = mid;
297 69475 : }
298 112332 : }
299 27502 : return left;
300 27502 : }
301 :
302 : /* If the region is an account, handle the resizing logic. This logic
303 : corresponds to
304 : solana_transaction_context::TransactionContext::access_violation_handler
305 :
306 : https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L510-L581 */
307 : static inline void
308 : fd_vm_handle_input_mem_region_oob( fd_vm_t const * vm,
309 : ulong offset,
310 : ulong sz,
311 : ulong region_idx,
312 405 : uchar write ) {
313 : /* If the access is not a write, we don't need to do anything
314 : https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L523-L525 */
315 405 : if( FD_UNLIKELY( !write ) ) {
316 233 : return;
317 233 : }
318 :
319 172 : fd_vm_input_region_t * region = &vm->input_mem_regions[ region_idx ];
320 : /* If the region is not writable, we don't need to do anything
321 : https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L526-L529 */
322 172 : if( FD_UNLIKELY( !region->is_writable ) ) {
323 101 : return;
324 101 : }
325 :
326 : /* Calculate the requested length
327 : https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L532-L535 */
328 71 : ulong requested_len = fd_ulong_sat_sub( fd_ulong_sat_add( offset, sz ), region->vaddr_offset );
329 71 : if( FD_UNLIKELY( requested_len > region->address_space_reserved ) ) {
330 71 : return;
331 71 : }
332 :
333 : /* Calculate the remaining allowed growth
334 : https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L549-L551 */
335 0 : long remaining_growth_signed = fd_long_sat_sub(
336 0 : FD_MAX_ACCOUNT_DATA_GROWTH_PER_TRANSACTION,
337 0 : vm->instr_ctx->txn_out->details.accounts_resize_delta );
338 0 : ulong remaining_allowed_growth = (remaining_growth_signed > 0L)
339 1 : ? (ulong)remaining_growth_signed
340 >1844*10^16 : : 0UL;
341 :
342 : /* If the requested length is greater than the size of the region,
343 : resize the region
344 : https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L553-L571 */
345 1 : if( FD_UNLIKELY( requested_len > region->region_sz ) ) {
346 : /* Calculate the new region size
347 : https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L558-L560 */
348 1 : ulong new_region_sz = fd_ulong_min(
349 1 : fd_ulong_min( region->address_space_reserved, FD_RUNTIME_ACC_SZ_MAX ),
350 1 : fd_ulong_sat_add( region->region_sz, remaining_allowed_growth ) );
351 :
352 : /* Resize the account and the region
353 : https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L569-L570 */
354 1 : if( FD_UNLIKELY( new_region_sz > region->region_sz ) ) {
355 : /* Safe because new_region_sz > region->region_sz */
356 1 : long growth = (long)(new_region_sz - region->region_sz);
357 1 : vm->instr_ctx->txn_out->details.accounts_resize_delta = fd_long_sat_add(
358 1 : vm->instr_ctx->txn_out->details.accounts_resize_delta, growth );
359 :
360 1 : ulong memset_sz = fd_ulong_sat_sub( new_region_sz, vm->acc_region_metas[ region->acc_region_meta_idx ].acc->data_len );
361 1 : fd_memset( vm->acc_region_metas[ region->acc_region_meta_idx ].acc->data+vm->acc_region_metas[ region->acc_region_meta_idx ].acc->data_len, 0, memset_sz );
362 1 : vm->acc_region_metas[ region->acc_region_meta_idx ].acc->data_len = new_region_sz;
363 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.1/transaction-context/src/transaction.rs#L546 */
364 1 : *vm->acc_region_metas[ region->acc_region_meta_idx ].touched = 1;
365 1 : region->region_sz = (uint)new_region_sz;
366 1 : }
367 1 : }
368 0 : }
369 :
370 : /* fd_vm_find_input_mem_region returns the translated haddr for a given
371 : offset into the input region. If an offset/sz is invalid or if an
372 : illegal write is performed, the sentinel value is returned. The
373 : access must fit within a single input memory region. */
374 : static inline ulong
375 : fd_vm_find_input_mem_region( fd_vm_t const * vm,
376 : ulong offset,
377 : ulong sz,
378 : uchar write,
379 27503 : ulong sentinel ) {
380 27503 : if( FD_UNLIKELY( vm->input_mem_regions_cnt==0 ) ) {
381 0 : return sentinel; /* Access is too large */
382 0 : }
383 :
384 : /* Binary search to find the correct memory region. If direct mapping is not
385 : enabled, then there is only 1 memory region which spans the input region. */
386 27503 : ulong region_idx = fd_vm_get_input_mem_region_idx( vm, offset );
387 27503 : if( FD_UNLIKELY( region_idx>=vm->input_mem_regions_cnt ) ) {
388 0 : return sentinel; /* Region not found */
389 0 : }
390 :
391 27503 : ulong bytes_in_region = fd_ulong_sat_sub( vm->input_mem_regions[ region_idx ].region_sz,
392 27503 : fd_ulong_sat_sub( offset, vm->input_mem_regions[ region_idx ].vaddr_offset ) );
393 :
394 : /* If the access is out of bounds, invoke the callback to handle the out of bounds access.
395 : This potentially resizes the region if necessary. */
396 27503 : if( FD_UNLIKELY( sz>bytes_in_region ) ) {
397 405 : fd_vm_handle_input_mem_region_oob( vm, offset, sz, region_idx, write );
398 405 : }
399 :
400 : /* After potentially resizing, re-check the bounds */
401 27503 : bytes_in_region = fd_ulong_sat_sub( vm->input_mem_regions[ region_idx ].region_sz,
402 27503 : fd_ulong_sat_sub( offset, vm->input_mem_regions[ region_idx ].vaddr_offset ) );
403 : /* If the access is still out of bounds, return the sentinel */
404 27503 : if( FD_UNLIKELY( sz>bytes_in_region ) ) {
405 405 : return sentinel;
406 405 : }
407 :
408 27098 : if( FD_UNLIKELY( write && vm->input_mem_regions[ region_idx ].is_writable==0U ) ) {
409 635 : return sentinel; /* Illegal write */
410 635 : }
411 :
412 : /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.1/transaction-context/src/transaction.rs#L546 */
413 26463 : if( FD_UNLIKELY( write && vm->acc_region_metas &&
414 26463 : vm->input_mem_regions[ region_idx ].acc_region_meta_idx!=ULONG_MAX ) ) {
415 662 : uchar * touched = vm->acc_region_metas[ vm->input_mem_regions[ region_idx ].acc_region_meta_idx ].touched;
416 662 : if( FD_LIKELY( touched ) ) *touched = 1;
417 662 : }
418 :
419 26463 : ulong start_region_idx = region_idx;
420 :
421 26463 : ulong adjusted_haddr = vm->input_mem_regions[ start_region_idx ].haddr + offset - vm->input_mem_regions[ start_region_idx ].vaddr_offset;
422 26463 : return adjusted_haddr;
423 27098 : }
424 :
425 : /* FD_VM_GROW_MIN amortizes {stack,heap} growth by rounding up request
426 : sizes. */
427 :
428 19910 : #define FD_VM_GROW_MIN (16384UL)
429 :
430 : /* fd_vm_{stack,heap}_grow lazily grow the stack and heap */
431 :
432 : static inline void
433 : fd_vm_region_grow( fd_vm_t * vm,
434 : ulong region,
435 : uchar * base,
436 : ulong * pclean,
437 : ulong cap,
438 19925 : ulong sz ) {
439 19925 : ulong clean = *pclean;
440 19925 : if( FD_LIKELY( sz<=clean ) ) return; /* nothing to do */
441 :
442 19910 : sz = fd_ulong_min( fd_ulong_align_up( sz, FD_VM_GROW_MIN ), cap );
443 :
444 19910 : fd_memset( base + clean, 0, sz-clean );
445 :
446 19910 : *pclean = sz;
447 19910 : vm->region_ld_sz[ region ] = (uint)sz;
448 19910 : vm->region_st_sz[ region ] = (uint)sz;
449 19910 : }
450 :
451 : static inline void
452 : fd_vm_stack_grow( fd_vm_t * vm,
453 10026 : ulong sz ) {
454 10026 : fd_vm_region_grow( vm, FD_VM_STACK_REGION, vm->stack, &vm->stack_clean, FD_VM_STACK_MAX, sz );
455 10026 : }
456 :
457 : static inline void
458 : fd_vm_heap_grow( fd_vm_t * vm,
459 9912 : ulong sz ) {
460 9912 : fd_vm_region_grow( vm, FD_VM_HEAP_REGION, vm->heap, &vm->heap_clean, vm->heap_max, sz );
461 9912 : }
462 :
463 : /* fd_vm_mem_init_full eagerly initializes the whole stack and heap. */
464 :
465 : static inline void
466 7526 : fd_vm_mem_init_full( fd_vm_t * vm ) {
467 7526 : fd_vm_stack_grow( vm, FD_VM_STACK_MAX );
468 7526 : fd_vm_heap_grow ( vm, vm->heap_max );
469 7526 : }
470 :
471 : static inline ulong
472 : fd_vm_mem_haddr( fd_vm_t * vm,
473 : ulong vaddr,
474 : ulong sz,
475 : ulong const * vm_region_haddr, /* indexed [0,6) */
476 : uint const * vm_region_sz, /* indexed [0,6) */
477 : uchar write, /* 1 if the access is a write, 0 if it is a read */
478 684221 : ulong sentinel ) {
479 684221 : ulong region = FD_VADDR_TO_REGION( vaddr );
480 684221 : ulong offset = vaddr & FD_VM_OFFSET_MASK;
481 :
482 : /* If the region is an account, handle the resizing logic. This logic corresponds to
483 : solana_transaction_context::TransactionContext::access_violation_handler
484 :
485 : https://github.com/anza-xyz/agave/blob/v3.0.1/transaction-context/src/lib.rs#L510-L581 */
486 684221 : if( region==FD_VM_INPUT_REGION ) {
487 27500 : return fd_vm_find_input_mem_region( vm, offset, sz, write, sentinel );
488 27500 : }
489 :
490 656721 : ulong region_sz = (ulong)vm_region_sz[ region ];
491 656721 : ulong sz_max = region_sz - fd_ulong_min( offset, region_sz );
492 :
493 656721 : if( FD_UNLIKELY( sz>sz_max && (region==FD_VM_STACK_REGION || region==FD_VM_HEAP_REGION) ) ) {
494 : /* {stack,heap} are lazily initialized. faults here mean that we
495 : need to do some zero initialization work. */
496 4944 : ulong need = fd_ulong_sat_add( offset, sz );
497 4944 : ulong cap = fd_ulong_if( region==FD_VM_STACK_REGION, FD_VM_STACK_MAX, vm->heap_max );
498 4944 : if( FD_LIKELY( need<=cap ) ) {
499 4837 : if( region==FD_VM_STACK_REGION ) fd_vm_stack_grow( vm, need );
500 2341 : else fd_vm_heap_grow ( vm, need );
501 4837 : region_sz = (ulong)vm_region_sz[ region ]; /* republished by grow */
502 4837 : sz_max = region_sz - fd_ulong_min( offset, region_sz );
503 4837 : }
504 4944 : }
505 :
506 656721 : return fd_ulong_if( sz<=sz_max, vm_region_haddr[ region ] + offset, sentinel );
507 684221 : }
508 :
509 :
510 69147 : FD_FN_PURE static inline ulong fd_vm_mem_ld_1( ulong haddr ) {
511 69147 : return (ulong)*(uchar const *)haddr;
512 69147 : }
513 :
514 871 : FD_FN_PURE static inline ulong fd_vm_mem_ld_2( ulong haddr ) {
515 871 : ushort t;
516 871 : memcpy( &t, (void const *)haddr, sizeof(ushort) );
517 871 : return (ulong)t;
518 871 : }
519 :
520 3861 : FD_FN_PURE static inline ulong fd_vm_mem_ld_4( ulong haddr ) {
521 3861 : uint t;
522 3861 : memcpy( &t, (void const *)haddr, sizeof(uint) );
523 3861 : return (ulong)t;
524 3861 : }
525 :
526 107994 : FD_FN_PURE static inline ulong fd_vm_mem_ld_8( ulong haddr ) {
527 107994 : ulong t;
528 107994 : memcpy( &t, (void const *)haddr, sizeof(ulong) );
529 107994 : return t;
530 107994 : }
531 :
532 49705 : static inline void fd_vm_mem_st_1( ulong haddr, uchar val ) {
533 49705 : *(uchar *)haddr = val;
534 49705 : }
535 :
536 : static inline void fd_vm_mem_st_2( ulong haddr,
537 953 : ushort val ) {
538 953 : memcpy( (void *)haddr, &val, sizeof(ushort) );
539 953 : }
540 :
541 : static inline void fd_vm_mem_st_4( ulong haddr,
542 3151 : uint val ) {
543 3151 : memcpy( (void *)haddr, &val, sizeof(uint) );
544 3151 : }
545 :
546 : static inline void fd_vm_mem_st_8( ulong haddr,
547 392877 : ulong val ) {
548 392877 : memcpy( (void *)haddr, &val, sizeof(ulong) );
549 392877 : }
550 :
551 : FD_PROTOTYPES_END
552 :
553 : #endif /* HEADER_fd_src_flamenco_vm_fd_vm_private_h */
|