Line data Source code
1 : #ifndef HEADER_fd_src_flamenco_vm_fd_vm_base_h
2 : #define HEADER_fd_src_flamenco_vm_fd_vm_base_h
3 :
4 : /* FIXME: Headers included from other modules need cleanup. As it
5 : stands, flamenco_base brings in types/custom, types/meta,
6 : types/bincode, ballet/base58, ballet/sha256, ballet/sha512,
7 : ballet/ed25519, ballet/txnthis also brings in util, flamenco_base,
8 : ballet/base58, util and the optional util/net/ipv4 ballet/sha256,
9 : most of which is probably not necessary to use this module in a
10 : somewhat haphazard fashion (include no-no things that are only
11 : available in hosted environments like stdio and stdlib) */
12 :
13 : #include "../fd_flamenco_base.h"
14 : #include "../../ballet/sbpf/fd_sbpf_loader.h" /* FIXME: functionality needed from here probably should be moved here */
15 : #include "../features/fd_features.h"
16 :
17 : /* Defines the different VM access types */
18 102 : #define FD_VM_ACCESS_TYPE_LD (1)
19 48 : #define FD_VM_ACCESS_TYPE_ST (2)
20 :
21 : /* FD_VM_SUCCESS is zero and returned to indicate that an operation
22 : completed successfully. FD_VM_ERR_* are negative integers and
23 : returned to indicate an operation that failed and why. */
24 :
25 : /* "Standard" Firedancer error codes (FIXME: harmonize and consolidate) */
26 :
27 56253477 : #define FD_VM_SUCCESS ( 0) /* success */
28 1891752 : #define FD_VM_ERR_INVAL (-1) /* invalid request */
29 3 : #define FD_VM_ERR_AGAIN (-2) /* try again later */
30 3 : #define FD_VM_ERR_UNSUP (-3) /* unsupported request */
31 3 : #define FD_VM_ERR_PERM (-4) /* unauthorized request */
32 6 : #define FD_VM_ERR_FULL (-5) /* storage full */
33 3 : #define FD_VM_ERR_EMPTY (-6) /* nothing to do */
34 3 : #define FD_VM_ERR_IO (-7) /* input-output error */
35 :
36 : /* VM exec error codes: These are only produced by the VM itself. */
37 :
38 3 : #define FD_VM_ERR_SIGTEXT ( -8) /* illegal program counter (e.g. execution ran off end of program, jump to outside the program) */
39 3 : #define FD_VM_ERR_SIGSPLIT ( -9) /* split multiword instruction (e.g. jump into the middle of a multiword instruction) */
40 3 : #define FD_VM_ERR_SIGCALL (-10) /* illegal call (e.g. call target is not the start of function) */
41 3 : #define FD_VM_ERR_SIGSTACK (-11) /* call depth limit exceeded */
42 3 : #define FD_VM_ERR_SIGILL (-12) /* illegal instruction (e.g. opcode is not valid) */
43 3 : #define FD_VM_ERR_SIGSEGV (-13) /* illegal memory address (e.g. read/write to an address not backed by any memory) */
44 3 : #define FD_VM_ERR_SIGBUS (-14) /* misaligned memory address (e.g. read/write to an address with inappropriate alignment) */
45 3 : #define FD_VM_ERR_SIGRDONLY (-15) /* illegal write (e.g. write to a read only address) */
46 3 : #define FD_VM_ERR_SIGCOST (-16) /* compute unit limit exceeded */
47 : // #define FD_VM_ERR_INVALID_PDA (-17) /* (deprecated, moved to syscall error) the computed pda was not a valid ed25519 point */
48 60 : #define FD_VM_ERR_SIGFPE (-18) /* divide by zero */
49 0 : #define FD_VM_ERR_SIGFPE_OF (-19) /* divide overflow */
50 0 : #define FD_VM_ERR_SIGSYSCALL (-20) /* Generic syscall error */
51 0 : #define FD_VM_ERR_SIGABORT (-21) /* Generic abort error (used in JIT) */
52 :
53 : /* (DEPRECATED) VM syscall error codes. These are only produced by fd_vm_syscall
54 : implementations. */
55 :
56 : // #define FD_VM_ERR_ABORT (-119) /* FIXME: description */
57 : // #define FD_VM_ERR_PANIC (-120) /* FIXME: description */
58 : // #define FD_VM_ERR_MEM_OVERLAP (-121) /* FIXME: description */
59 : // #define FD_VM_ERR_INSTR_ERR (-22) /* FIXME: description */
60 : // #define FD_VM_ERR_INVOKE_CONTEXT_BORROW_FAILED (-23) /* FIXME: description */
61 : // #define FD_VM_ERR_RETURN_DATA_TOO_LARGE (-24) /* FIXME: description */
62 :
63 : /* sBPF validation error codes. These are only produced by
64 : fd_vm_validate. FIXME: Consider having fd_vm_validate return
65 : standard error codes and then provide detail like this through an
66 : info arg. FIXME: Are these exact matches to Solana? If so, provide
67 : link, if not, document and refine name / consolidate further. */
68 :
69 1179 : #define FD_VM_ERR_INVALID_OPCODE (-25) /* detected an invalid opcode */
70 393 : #define FD_VM_ERR_INVALID_SRC_REG (-26) /* detected an invalid source register */
71 585 : #define FD_VM_ERR_INVALID_DST_REG (-27) /* detected an invalid destination register */
72 3 : #define FD_VM_ERR_INF_LOOP (-28) /* detected an infinite loop */
73 141 : #define FD_VM_ERR_JMP_OUT_OF_BOUNDS (-29) /* detected an out of bounds jump */
74 3 : #define FD_VM_ERR_JMP_TO_ADDL_IMM (-30) /* detected a jump to an addl imm */
75 21 : #define FD_VM_ERR_INVALID_END_IMM (-31) /* detected an invalid immediate for an endianness conversion instruction */
76 3 : #define FD_VM_ERR_INCOMPLETE_LDQ (-32) /* detected an incomplete ldq at program end */
77 3 : #define FD_VM_ERR_LDQ_NO_ADDL_IMM (-33) /* detected a ldq without an addl imm following it */
78 3 : #define FD_VM_ERR_NO_SUCH_EXT_CALL (-34) /* detected a call imm with no function was registered for that immediate */
79 18 : #define FD_VM_ERR_INVALID_REG (-35) /* detected an invalid register */
80 0 : #define FD_VM_ERR_BAD_TEXT (-36) /* detected a bad text section (overflow, outside rodata boundary, etc.,)*/
81 72 : #define FD_VM_SH_OVERFLOW (-37) /* detected a shift overflow, equivalent to VeriferError::ShiftWithOverflow */
82 0 : #define FD_VM_TEXT_SZ_UNALIGNED (-38) /* detected a text section that is not a multiple of 8 */
83 0 : #define FD_VM_INVALID_FUNCTION (-39) /* detected an invalid function */
84 0 : #define FD_VM_INVALID_SYSCALL (-40) /* detected an invalid syscall */
85 :
86 : /* Syscall Errors
87 : https://github.com/anza-xyz/agave/blob/v2.0.7/programs/bpf_loader/src/syscalls/mod.rs#L81 */
88 :
89 0 : #define FD_VM_SYSCALL_ERR_INVALID_STRING (-1)
90 0 : #define FD_VM_SYSCALL_ERR_ABORT (-2)
91 0 : #define FD_VM_SYSCALL_ERR_PANIC (-3)
92 0 : #define FD_VM_SYSCALL_ERR_INVOKE_CONTEXT_BORROW_FAILED (-4)
93 0 : #define FD_VM_SYSCALL_ERR_MALFORMED_SIGNER_SEED (-5)
94 0 : #define FD_VM_SYSCALL_ERR_BAD_SEEDS (-6)
95 0 : #define FD_VM_SYSCALL_ERR_PROGRAM_NOT_SUPPORTED (-7)
96 0 : #define FD_VM_SYSCALL_ERR_UNALIGNED_POINTER (-8)
97 0 : #define FD_VM_SYSCALL_ERR_TOO_MANY_SIGNERS (-9)
98 0 : #define FD_VM_SYSCALL_ERR_INSTRUCTION_TOO_LARGE (-10)
99 0 : #define FD_VM_SYSCALL_ERR_TOO_MANY_ACCOUNTS (-11)
100 36 : #define FD_VM_SYSCALL_ERR_COPY_OVERLAPPING (-12)
101 0 : #define FD_VM_SYSCALL_ERR_RETURN_DATA_TOO_LARGE (-13)
102 0 : #define FD_VM_SYSCALL_ERR_TOO_MANY_SLICES (-14)
103 3 : #define FD_VM_SYSCALL_ERR_INVALID_LENGTH (-15)
104 0 : #define FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_DATA_LEN_EXCEEDED (-16)
105 0 : #define FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNTS_EXCEEDED (-17)
106 0 : #define FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNT_INFOS_EXCEEDED (-18)
107 3 : #define FD_VM_SYSCALL_ERR_INVALID_ATTRIBUTE (-19)
108 0 : #define FD_VM_SYSCALL_ERR_INVALID_POINTER (-20)
109 0 : #define FD_VM_SYSCALL_ERR_ARITHMETIC_OVERFLOW (-21)
110 :
111 : /* These syscall errors are unique to Firedancer and do not have an Agave equivalent. */
112 0 : #define FD_VM_SYSCALL_ERR_INSTR_ERR (-22)
113 0 : #define FD_VM_SYSCALL_ERR_INVALID_PDA (-23) /* the computed pda was not a valid ed25519 point */
114 0 : #define FD_VM_SYSCALL_ERR_COMPUTE_BUDGET_EXCEEDED (-24) /* compute unit limit exceeded in syscall */
115 72 : #define FD_VM_SYSCALL_ERR_SEGFAULT (-25) /* illegal memory address (e.g. read/write to an address not backed by any memory) in syscall */
116 0 : #define FD_VM_SYSCALL_ERR_OUTSIDE_RUNTIME (-26) /* syscall called with vm not running in solana runtime */
117 :
118 : /* Poseidon returns custom errors for some reason */
119 0 : #define FD_VM_SYSCALL_ERR_POSEIDON_INVALID_PARAMS (1)
120 0 : #define FD_VM_SYSCALL_ERR_POSEIDON_INVALID_ENDIANNESS (2)
121 :
122 : /* EbpfError
123 : https://github.com/solana-labs/rbpf/blob/v0.8.5/src/error.rs#L17 */
124 :
125 0 : #define FD_VM_ERR_EBPF_ELF_ERROR (-1)
126 0 : #define FD_VM_ERR_EBPF_FUNCTION_ALREADY_REGISTERED (-2)
127 0 : #define FD_VM_ERR_EBPF_CALL_DEPTH_EXCEEDED (-3)
128 0 : #define FD_VM_ERR_EBPF_EXIT_ROOT_CALL_FRAME (-4)
129 138 : #define FD_VM_ERR_EBPF_DIVIDE_BY_ZERO (-5)
130 36 : #define FD_VM_ERR_EBPF_DIVIDE_OVERFLOW (-6)
131 78 : #define FD_VM_ERR_EBPF_EXECUTION_OVERRUN (-7)
132 42 : #define FD_VM_ERR_EBPF_CALL_OUTSIDE_TEXT_SEGMENT (-8)
133 681 : #define FD_VM_ERR_EBPF_EXCEEDED_MAX_INSTRUCTIONS (-9)
134 0 : #define FD_VM_ERR_EBPF_JIT_NOT_COMPILED (-10)
135 0 : #define FD_VM_ERR_EBPF_INVALID_VIRTUAL_ADDRESS (-11)
136 0 : #define FD_VM_ERR_EBPF_INVALID_MEMORY_REGION (-12)
137 183 : #define FD_VM_ERR_EBPF_ACCESS_VIOLATION (-13)
138 0 : #define FD_VM_ERR_EBPF_STACK_ACCESS_VIOLATION (-14)
139 18 : #define FD_VM_ERR_EBPF_INVALID_INSTRUCTION (-15)
140 1182 : #define FD_VM_ERR_EBPF_UNSUPPORTED_INSTRUCTION (-16)
141 0 : #define FD_VM_ERR_EBPF_EXHAUSTED_TEXT_SEGMENT (-17)
142 0 : #define FD_VM_ERR_EBPF_LIBC_INVOCATION_FAILED (-18)
143 0 : #define FD_VM_ERR_EBPF_VERIFIER_ERROR (-19)
144 0 : #define FD_VM_ERR_EBPF_SYSCALL_ERROR (-20)
145 :
146 :
147 : FD_PROTOTYPES_BEGIN
148 :
149 : /* fd_vm_strerror converts an FD_VM_SUCCESS / FD_VM_ERR_* code into
150 : a human readable cstr. The lifetime of the returned pointer is
151 : infinite. The returned pointer is always to a non-NULL cstr. */
152 :
153 : FD_FN_CONST char const * fd_vm_strerror( int err );
154 :
155 : FD_PROTOTYPES_END
156 :
157 : /* fd_vm_limits API ***************************************************/
158 :
159 : /* FIXME: pretty good case these actually belong in ballet/sbpf */
160 : /* FIXME: DOCUMENT THESE / LINK TO SOLANA CODE / ETC */
161 :
162 : /* VM register constants */
163 :
164 330 : #define FD_VM_REG_CNT (11UL)
165 7854 : #define FD_VM_REG_MAX (16UL) /* Actual number of SBPF instruction src/dst register indices */
166 :
167 : #define FD_VM_SHADOW_REG_CNT (4UL)
168 :
169 : /* VM stack constants */
170 :
171 27963 : #define FD_VM_STACK_FRAME_MAX (64UL)
172 31959 : #define FD_VM_STACK_FRAME_SZ FD_VM_STACK_FRAME_SIZE
173 7842 : #define FD_VM_STACK_GUARD_SZ (0x1000UL)
174 20184 : #define FD_VM_STACK_MAX (FD_VM_STACK_FRAME_MAX*(FD_VM_STACK_FRAME_SZ))
175 :
176 : /* VM heap constants */
177 :
178 192 : #define FD_VM_HEAP_DEFAULT ( 32UL*1024UL) /* FIXME: SHOULD THIS MATCH FD_VM_HEAP_SIZE LIMIT BELOW? */
179 0 : #define FD_VM_HEAP_MAX (256UL*1024UL)
180 :
181 : /* VM log constants */
182 :
183 27 : #define FD_VM_LOG_MAX (10000UL)
184 : #define FD_VM_LOG_TAIL (128UL) /* Large enough to cover the worst case syscall log tail clobbering in string parsing */
185 :
186 : /* VM memory map constants */
187 :
188 : #define FD_VM_LO_REGION (0UL)
189 24102 : #define FD_VM_PROG_REGION (1UL)
190 24102 : #define FD_VM_STACK_REGION (2UL)
191 24102 : #define FD_VM_HEAP_REGION (3UL)
192 24906 : #define FD_VM_INPUT_REGION (4UL)
193 822 : #define FD_VM_HIGH_REGION (5UL)
194 :
195 54 : #define FD_VM_MEM_MAP_PROGRAM_REGION_START (0x100000000UL)
196 8037 : #define FD_VM_MEM_MAP_STACK_REGION_START (0x200000000UL)
197 180 : #define FD_VM_MEM_MAP_HEAP_REGION_START (0x300000000UL)
198 7929 : #define FD_VM_MEM_MAP_INPUT_REGION_START (0x400000000UL)
199 : #define FD_VM_MEM_MAP_REGION_SZ (0x0FFFFFFFFUL)
200 : #define FD_VM_MEM_MAP_REGION_MASK (~FD_VM_MEM_MAP_REGION_SZ)
201 822 : #define FD_VM_MEM_MAP_REGION_VIRT_ADDR_BITS (32)
202 :
203 : /* VM compute budget. Note: these names should match exactly the names
204 : used in existing Solana validator. See:
205 : https://github.com/anza-xyz/agave/blob/v1.18.5/program-runtime/src/compute_budget.rs#L19
206 : https://github.com/anza-xyz/agave/blob/v1.18.5/program-runtime/src/compute_budget.rs#L133 */
207 : /* FIXME: DOUBLE CHECK THESE */
208 :
209 : /* FD_VM_COMPUTE_UNIT_LIMIT is the number of compute units that a
210 : transaction or individual instruction is allowed to consume. Compute
211 : units are consumed by program execution, resources they use, etc ... */
212 :
213 186 : #define FD_VM_COMPUTE_UNIT_LIMIT ( 1400000UL)
214 :
215 : /* FD_VM_LOG_64_UNITS is the number of compute units consumed by a
216 : log_64 call */
217 :
218 : #define FD_VM_LOG_64_UNITS ( 100UL)
219 :
220 : /* FD_VM_CREATE_PROGRAM_ADDRESS_UNITS is the number of compute units
221 : consumed by a create_program_address call and a try_find_program_address_call */
222 :
223 : #define FD_VM_CREATE_PROGRAM_ADDRESS_UNITS ( 1500UL)
224 :
225 : /* FD_VM_INVOKE_UNITS is the number of compute units consumed by an
226 : invoke call (not including the cost incurred by the called program) */
227 :
228 : #define FD_VM_INVOKE_UNITS ( 1000UL)
229 :
230 : /* FD_VM_MAX_INVOKE_STACK_HEIGHT is the maximum program instruction
231 : invocation stack height. Invocation stack height starts at 1 for
232 : transaction instructions and the stack height is incremented each
233 : time a program invokes an instruction and decremented when a program
234 : returns */
235 :
236 : #define FD_VM_MAX_INVOKE_STACK_HEIGHT ( 5UL)
237 :
238 : /* FD_VM_MAX_INSTRUCTION_TRACE_LENGTH is the maximum cross-program
239 : invocation and instructions per transaction */
240 :
241 : #define FD_VM_MAX_INSTRUCTION_TRACE_LENGTH ( 64UL)
242 :
243 : /* FD_VM_SHA256_BASE_COST is the base number of compute units consumed
244 : to call SHA256 */
245 :
246 0 : #define FD_VM_SHA256_BASE_COST ( 85UL)
247 :
248 : /* FD_VM_SHA256_BYTE_COST is the incremental number of units consumed by
249 : SHA256 (based on bytes) */
250 :
251 0 : #define FD_VM_SHA256_BYTE_COST ( 1UL)
252 :
253 : /* FD_VM_SHA256_MAX_SLICES is the maximum number of slices hashed per
254 : syscall */
255 :
256 0 : #define FD_VM_SHA256_MAX_SLICES ( 20000UL)
257 :
258 : /* FD_VM_MAX_CALL_DEPTH is the maximum SBF to BPF call depth */
259 :
260 27 : #define FD_VM_MAX_CALL_DEPTH ( 64UL)
261 :
262 : /* FD_VM_STACK_FRAME_SIZE is the size of a stack frame in bytes, must
263 : match the size specified in the LLVM SBF backend */
264 :
265 31959 : #define FD_VM_STACK_FRAME_SIZE ( 4096UL)
266 :
267 : /* FD_VM_LOG_PUBKEY_UNITS is the number of compute units consumed by
268 : logging a `Pubkey` */
269 :
270 : #define FD_VM_LOG_PUBKEY_UNITS ( 100UL)
271 :
272 : /* FD_VM_MAX_CPI_INSTRUCTION_SIZE is the maximum cross-program
273 : invocation instruction size */
274 :
275 : #define FD_VM_MAX_CPI_INSTRUCTION_SIZE ( 1280UL) /* IPv6 Min MTU size */
276 :
277 : /* FD_VM_CPI_BYTES_PER_UNIT is the number of account data bytes per
278 : compute unit charged during a cross-program invocation */
279 :
280 0 : #define FD_VM_CPI_BYTES_PER_UNIT ( 250UL) /* ~50MB at 200,000 units */
281 :
282 : /* FD_VM_SYSVAR_BASE_COST is the base number of compute units consumed
283 : to get a sysvar */
284 :
285 : #define FD_VM_SYSVAR_BASE_COST ( 100UL)
286 :
287 : /* FD_VM_SECP256K1_RECOVER_COST is the number of compute units consumed
288 : to call secp256k1_recover */
289 :
290 : #define FD_VM_SECP256K1_RECOVER_COST ( 25000UL)
291 :
292 : /* FD_VM_SYSCALL_BASE_COST is the number of compute units consumed to do
293 : a syscall without any work */
294 :
295 0 : #define FD_VM_SYSCALL_BASE_COST ( 100UL)
296 :
297 : /* FD_VM_CURVE25519_EDWARDS_VALIDATE_POINT_COST is the number of compute
298 : units consumed to validate a curve25519 edwards point */
299 :
300 : #define FD_VM_CURVE25519_EDWARDS_VALIDATE_POINT_COST ( 159UL)
301 :
302 : /* FD_VM_CURVE25519_EDWARDS_ADD_COST is the number of compute units
303 : consumed to add two curve25519 edwards points */
304 :
305 0 : #define FD_VM_CURVE25519_EDWARDS_ADD_COST ( 473UL)
306 :
307 : /* FD_VM_CURVE25519_EDWARDS_SUBTRACT_COST is the number of compute units
308 : consumed to subtract two curve25519 edwards points */
309 :
310 0 : #define FD_VM_CURVE25519_EDWARDS_SUBTRACT_COST ( 475UL)
311 :
312 : /* FD_VM_CURVE25519_EDWARDS_MULTIPLY_COST is the number of compute units
313 : consumed to multiply a curve25519 edwards point */
314 :
315 0 : #define FD_VM_CURVE25519_EDWARDS_MULTIPLY_COST ( 2177UL)
316 :
317 : /* FD_VM_CURVE25519_EDWARDS_MSM_BASE_COST is the number of compute units
318 : consumed for a multiscalar multiplication (msm) of edwards points.
319 : The total cost is calculated as
320 : `msm_base_cost + (length - 1) * msm_incremental_cost` */
321 :
322 6 : #define FD_VM_CURVE25519_EDWARDS_MSM_BASE_COST ( 2273UL)
323 :
324 : /* FD_VM_CURVE25519_EDWARDS_MSM_INCREMENTAL_COST is the number of
325 : compute units consumed for a multiscalar multiplication (msm) of
326 : edwards points. The total cost is calculated as
327 : `msm_base_cost + (length - 1) * msm_incremental_cost` */
328 :
329 6 : #define FD_VM_CURVE25519_EDWARDS_MSM_INCREMENTAL_COST ( 758UL)
330 :
331 : /* FD_VM_CURVE25519_RISTRETTO_VALIDATE_POINT_COST is the number of
332 : compute units consumed to validate a curve25519 ristretto point */
333 :
334 : #define FD_VM_CURVE25519_RISTRETTO_VALIDATE_POINT_COST ( 169UL)
335 :
336 : /* FD_VM_CURVE25519_RISTRETTO_ADD_COST is the number of compute units
337 : consumed to add two curve25519 ristretto points */
338 :
339 6 : #define FD_VM_CURVE25519_RISTRETTO_ADD_COST ( 521UL)
340 :
341 : /* FD_VM_CURVE25519_RISTRETTO_SUBTRACT_COST is the number of compute
342 : units consumed to subtract two curve25519 ristretto points */
343 :
344 3 : #define FD_VM_CURVE25519_RISTRETTO_SUBTRACT_COST ( 519UL)
345 :
346 : /* FD_VM_CURVE25519_RISTRETTO_MULTIPLY_COST is the number of compute
347 : units consumed to multiply a curve25519 ristretto point */
348 :
349 3 : #define FD_VM_CURVE25519_RISTRETTO_MULTIPLY_COST ( 2208UL)
350 :
351 : /* FD_VM_CURVE25519_RISTRETTO_MSM_BASE_COST is the number of compute
352 : units consumed for a multiscalar multiplication (msm) of ristretto
353 : points. The total cost is calculated as
354 : `msm_base_cost + (length - 1) * msm_incremental_cost` */
355 :
356 3 : #define FD_VM_CURVE25519_RISTRETTO_MSM_BASE_COST ( 2303UL)
357 :
358 : /* FD_VM_CURVE25519_RISTRETTO_MSM_INCREMENTAL_COST is the number of
359 : compute units consumed for a multiscalar multiplication (msm) of
360 : ristretto points. The total cost is calculated as
361 : `msm_base_cost + (length - 1) * msm_incremental_cost` */
362 :
363 3 : #define FD_VM_CURVE25519_RISTRETTO_MSM_INCREMENTAL_COST ( 788UL)
364 :
365 : /* FD_VM_HEAP_SIZE is the program heap region size, default:
366 : solana_sdk::entrypoint::HEAP_LENGTH */
367 :
368 : #define FD_VM_HEAP_SIZE ( 32768UL)
369 :
370 : /* FD_VM_HEAP_COST is the number of compute units per additional 32k
371 : heap above the default (~.5 us per 32k at 15 units/us rounded up) */
372 :
373 0 : #define FD_VM_HEAP_COST ( 8UL) /* DEFAULT_HEAP_COST */
374 :
375 : /* FD_VM_MEM_OP_BASE_COST is the memory operation syscall base cost */
376 :
377 0 : #define FD_VM_MEM_OP_BASE_COST ( 10UL)
378 :
379 : /* FD_VM_ALT_BN128_ADDITION_COST is the number of compute units consumed
380 : to call alt_bn128_addition */
381 :
382 0 : #define FD_VM_ALT_BN128_ADDITION_COST ( 334UL)
383 :
384 : /* FD_VM_ALT_BN128_MULTIPLICATION_COST is the number of compute units
385 : consumed to call alt_bn128_multiplication */
386 :
387 0 : #define FD_VM_ALT_BN128_MULTIPLICATION_COST ( 3840UL)
388 :
389 : /* FD_VM_ALT_BN128_PAIRING_ONE_PAIR_COST_FIRST
390 : FD_VM_ALT_BN128_PAIRING_ONE_PAIR_COST_OTHER give the total cost as
391 : alt_bn128_pairing_one_pair_cost_first + alt_bn128_pairing_one_pair_cost_other * (num_elems - 1) */
392 :
393 0 : #define FD_VM_ALT_BN128_PAIRING_ONE_PAIR_COST_FIRST ( 36364UL)
394 0 : #define FD_VM_ALT_BN128_PAIRING_ONE_PAIR_COST_OTHER ( 12121UL)
395 :
396 : /* FD_VM_BIG_MODULAR_EXPONENTIATION_COST is the big integer modular
397 : exponentiation cost */
398 :
399 : #define FD_VM_BIG_MODULAR_EXPONENTIATION_COST ( 33UL)
400 :
401 : /* FD_VM_POSEIDON_COST_COEFFICIENT_A is the coefficient `a` of the
402 : quadratic function which determines the number of compute units
403 : consumed to call poseidon syscall for a given number of inputs */
404 :
405 0 : #define FD_VM_POSEIDON_COST_COEFFICIENT_A ( 61UL)
406 :
407 : /* FD_VM_POSEIDON_COST_COEFFICIENT_C is the coefficient `c` of the
408 : quadratic function which determines the number of compute units
409 : consumed to call poseidon syscall for a given number of inputs */
410 :
411 0 : #define FD_VM_POSEIDON_COST_COEFFICIENT_C ( 542UL)
412 :
413 : /* FD_VM_GET_REMAINING_COMPUTE_UNITS_COST is the number of compute units
414 : consumed for reading the remaining compute units */
415 :
416 : #define FD_VM_GET_REMAINING_COMPUTE_UNITS_COST ( 100UL)
417 :
418 : /* FD_VM_ALT_BN128_G1_COMPRESS is the number of compute units consumed
419 : to call alt_bn128_g1_compress */
420 :
421 0 : #define FD_VM_ALT_BN128_G1_COMPRESS ( 30UL)
422 :
423 : /* FD_VM_ALT_BN128_G1_DECOMPRESS is the number of compute units consumed
424 : to call alt_bn128_g1_decompress */
425 :
426 0 : #define FD_VM_ALT_BN128_G1_DECOMPRESS ( 398UL)
427 :
428 : /* FD_VM_ALT_BN128_G2_COMPRESS is the number of compute units consumed
429 : to call alt_bn128_g2_compress */
430 :
431 0 : #define FD_VM_ALT_BN128_G2_COMPRESS ( 86UL)
432 :
433 : /* FD_VM_ALT_BN128_G2_DECOMPRESS is the number of compute units consumed
434 : to call alt_bn128_g2_decompress */
435 :
436 0 : #define FD_VM_ALT_BN128_G2_DECOMPRESS ( 13610UL)
437 :
438 : /* FD_VM_LOADED_ACCOUNTS_DATA_SIZE_LIMIT is the maximum accounts data
439 : size, in bytes, that a transaction is allowed to load */
440 :
441 0 : #define FD_VM_LOADED_ACCOUNTS_DATA_SIZE_LIMIT (64UL*1024UL*1024UL) /* 64MiB */
442 :
443 : /* fd_vm_disasm API ***************************************************/
444 :
445 : /* FIXME: pretty good case this actually belongs in ballet/sbpf */
446 : /* FIXME: fd_sbpf_instr_t is nominally a ulong but implemented using
447 : bit-fields. Compilers tend to generate notoriously poor asm for bit
448 : fields ... check ASM here. */
449 :
450 : FD_PROTOTYPES_BEGIN
451 :
452 : /* fd_vm_disasm_{instr,program} appends to the *_out_len (in strlen
453 : sense) cstr in the out_max byte buffer out a pretty printed cstr of
454 : the {instruction,program}. If syscalls is non-NULL, syscalls will be
455 : annotated with the names from the provided syscall mapping.
456 :
457 : On input, *_out_len should be strlen(out) and in [0,out_max). For
458 : instr, pc is the program counter corresponding to text[0] (as such
459 : text_cnt should be positive) and text_cnt is the number of words
460 : available at text to support safely printing multiword instructions.
461 :
462 : Given a valid out on input, on output, *_out_len will be strlen(out)
463 : and in [0,out_max), even if there was an error.
464 :
465 : Returns:
466 :
467 : FD_VM_SUCCESS - out buffer and *_out_len updated.
468 :
469 : FD_VM_ERR_INVAL - Invalid input. For instr, out buffer and *_out_len
470 : are unchanged. For program, out buffer and *_out_len will have been
471 : updated up to the point where the error occurred.
472 :
473 : FD_VM_ERR_UNSUP - For program, too many functions and/or labels for
474 : the current implementation. out buffer and *_out_len unchanged.
475 :
476 : FD_VM_ERR_FULL - Not enough room in out to hold the result so output
477 : was truncated. out buffer and *_out_len updated.
478 :
479 : FD_VM_ERR_IO - An error occurred formatting the string to append. For
480 : instr, out_buffer and *_out_len unchanged. For program, out buffer
481 : and *_out_len will have been updated up to the point where the error
482 : occurred. In both cases, trailing bytes of out might have been
483 : clobbered. */
484 :
485 : int
486 : fd_vm_disasm_instr( ulong const * text, /* Indexed [0,text_cnt) */
487 : ulong text_cnt,
488 : ulong pc,
489 : fd_sbpf_syscalls_t const * syscalls,
490 : char * out, /* Indexed [0,out_max) */
491 : ulong out_max,
492 : ulong * _out_len );
493 :
494 : int
495 : fd_vm_disasm_program( ulong const * text, /* Indexed [0,text_cnt) */
496 : ulong text_cnt,
497 : fd_sbpf_syscalls_t const * syscalls,
498 : char * out, /* Indexed [0,out_max) */
499 : ulong out_max,
500 : ulong * _out_len );
501 :
502 : FD_PROTOTYPES_END
503 :
504 : /* fd_vm_trace API ****************************************************/
505 :
506 : /* FIXME: pretty good case this actually belongs in ballet/sbpf */
507 :
508 : /* A FD_VM_TRACE_EVENT_TYPE_* indicates how a fd_vm_trace_event_t should
509 : be interpreted. */
510 :
511 30 : #define FD_VM_TRACE_EVENT_TYPE_EXE (0)
512 24 : #define FD_VM_TRACE_EVENT_TYPE_READ (1)
513 24 : #define FD_VM_TRACE_EVENT_TYPE_WRITE (2)
514 :
515 : struct fd_vm_trace_event_exe {
516 : /* This point is aligned 8 */
517 : ulong info; /* Event info bit field */
518 : ulong pc; /* pc */
519 : ulong ic; /* ic */
520 : ulong cu; /* cu */
521 : ulong ic_correction; /* ic_correction */
522 : ulong frame_cnt; /* frame_cnt */
523 : ulong reg[ FD_VM_REG_CNT ]; /* registers */
524 : ulong text[ 2 ]; /* If the event has valid clear, this is actually text[1] */
525 : /* This point is aligned 8 */
526 : };
527 :
528 : typedef struct fd_vm_trace_event_exe fd_vm_trace_event_exe_t;
529 :
530 : struct fd_vm_trace_event_mem {
531 : /* This point is aligned 8 */
532 : ulong info; /* Event info bit field */
533 : ulong vaddr; /* VM address range associated with event */
534 : ulong sz;
535 : /* This point is aligned 8
536 : If event has valid set:
537 : min(sz,event_data_max) bytes user data bytes
538 : padding to aligned 8 */
539 : };
540 :
541 : typedef struct fd_vm_trace_event_mem fd_vm_trace_event_mem_t;
542 :
543 3 : #define FD_VM_TRACE_MAGIC (0xfdc377ace3a61c00UL) /* FD VM TRACE MAGIC version 0 */
544 :
545 : struct fd_vm_trace {
546 : /* This point is aligned 8 */
547 : ulong magic; /* ==FD_VM_TRACE_MAGIC */
548 : ulong event_max; /* Number bytes of event storage */
549 : ulong event_data_max; /* Max bytes to capture per data event */
550 : ulong event_sz; /* Used bytes of event storage */
551 : /* This point is aligned 8
552 : event_max bytes storage
553 : padding to aligned 8 */
554 : };
555 :
556 : typedef struct fd_vm_trace fd_vm_trace_t;
557 :
558 : FD_PROTOTYPES_BEGIN
559 :
560 : /* trace object structors */
561 : /* FIXME: DOCUMENT (USUAL CONVENTIONS) */
562 :
563 : FD_FN_CONST ulong
564 : fd_vm_trace_align( void );
565 :
566 : FD_FN_CONST ulong
567 : fd_vm_trace_footprint( ulong event_max, /* Maximum amount of event storage (<=1 EiB) */
568 : ulong event_data_max ); /* Maximum number of bytes that can be captured in an event (<=1 EiB) */
569 :
570 : void *
571 : fd_vm_trace_new( void * shmem,
572 : ulong event_max,
573 : ulong event_data_max );
574 :
575 : fd_vm_trace_t *
576 : fd_vm_trace_join( void * _trace );
577 :
578 : void *
579 : fd_vm_trace_leave( fd_vm_trace_t * trace );
580 :
581 : void *
582 : fd_vm_trace_delete( void * _trace );
583 :
584 : /* Given a current local join, fd_vm_trace_event returns the location in
585 : the caller's address space where trace events are stored and
586 : fd_vm_trace_event_sz returns number of bytes of trace events stored
587 : at that location. event_max is the number of bytes of event storage
588 : (value used to construct the trace) and event_data_max is the maximum
589 : number of data bytes that can be captured per event (value used to
590 : construct the trace). event will be aligned 8 and event_sz will be a
591 : multiple of 8 in [0,event_max]. The lifetime of the returned pointer
592 : is the lifetime of the current join. The first 8 bytes of an event
593 : are an info field used by trace inspection tools how to interpret the
594 : event. */
595 :
596 6 : FD_FN_CONST static inline void const * fd_vm_trace_event ( fd_vm_trace_t const * trace ) { return (void *)(trace+1); }
597 6 : FD_FN_CONST static inline ulong fd_vm_trace_event_sz ( fd_vm_trace_t const * trace ) { return trace->event_sz; }
598 3 : FD_FN_CONST static inline ulong fd_vm_trace_event_max ( fd_vm_trace_t const * trace ) { return trace->event_max; }
599 6 : FD_FN_CONST static inline ulong fd_vm_trace_event_data_max( fd_vm_trace_t const * trace ) { return trace->event_data_max; }
600 :
601 : /* fd_vm_trace_event_info returns the event info corresponding to the
602 : given (type,valid) tuple. Assumes type is a FD_VM_TRACE_EVENT_TYPE_*
603 : and that valid is in [0,1]. fd_vm_trace_event_info_{type,valid}
604 : extract from the given info {type,valid}. Assumes info is valid. */
605 :
606 45 : FD_FN_CONST static inline ulong fd_vm_trace_event_info( int type, int valid ) { return (ulong)((valid<<2) | type); }
607 :
608 45 : FD_FN_CONST static inline int fd_vm_trace_event_info_type ( ulong info ) { return (int)(info & 3UL); } /* EVENT_TYPE_* */
609 45 : FD_FN_CONST static inline int fd_vm_trace_event_info_valid( ulong info ) { return (int)(info >> 2); } /* In [0,1] */
610 :
611 : /* fd_vm_trace_reset frees all events in the trace. Returns
612 : FD_VM_SUCCESS (0) on success or FD_VM_ERR code (negative) on failure.
613 : Reasons for failure include NULL trace. */
614 :
615 : static inline int
616 0 : fd_vm_trace_reset( fd_vm_trace_t * trace ) {
617 0 : if( FD_UNLIKELY( !trace ) ) return FD_VM_ERR_INVAL;
618 0 : trace->event_sz = 0UL;
619 0 : return FD_VM_SUCCESS;
620 0 : }
621 :
622 : /* fd_vm_trace_event_exe records the current pc, ic, cu and
623 : register file of the VM and the instruction about to execute. Text
624 : points to the first word of the instruction about to execute and
625 : text_cnt points to the number of words available at that point.
626 : Returns FD_VM_SUCCESS (0) on success and a FD_VM_ERR code (negative)
627 : on failure. Reasons for failure include INVAL (trace NULL, reg NULL,
628 : text NULL, and/or text_cnt 0) and FULL (insufficient trace event
629 : storage available). */
630 :
631 : int
632 : fd_vm_trace_event_exe( fd_vm_trace_t * trace,
633 : ulong pc,
634 : ulong ic,
635 : ulong cu,
636 : ulong reg[ FD_VM_REG_CNT ],
637 : ulong const * text, /* Indexed [0,text_cnt) */
638 : ulong text_cnt,
639 : ulong ic_correction,
640 : ulong frame_cnt );
641 :
642 : /* fd_vm_trace_event_mem records an attempt to access the VM address
643 : range [vaddr,vaddr+sz). If write==0, it was a read attempt,
644 : otherwise, it was a write attempt. Data points to the location of
645 : the memory range in host memory or NULL if the range is invalid. If
646 : data is not NULL and sz is non-zero, this will record
647 : min(sz,event_data_max) of data for the event and mark the event has
648 : having valid data. Returns FD_VM_SUCCESS (0) on success and a
649 : FD_VM_ERR code (negative) on failure. Reasons for failure include
650 : INVAL (trace NULL) and FULL (insufficient trace event storage
651 : available to store the event). */
652 :
653 : int
654 : fd_vm_trace_event_mem( fd_vm_trace_t * trace,
655 : int write,
656 : ulong vaddr,
657 : ulong sz,
658 : void * data );
659 :
660 : /* fd_vm_trace_printf pretty prints the current trace to stdout. If
661 : syscalls is non-NULL, the trace will annotate syscalls in its
662 : disassembly according the syscall mapping. Returns FD_VM_SUCCESS (0)
663 : on success and a FD_VM_ERR code (negative) on failure. Reasons for
664 : failure include INVAL (NULL trace) and IO (corruption detected while
665 : parsing the trace events). FIXME: REVAMP THIS API FOR MORE GENERAL
666 : USE CASES. */
667 :
668 : int
669 : fd_vm_trace_printf( fd_vm_trace_t const * trace,
670 : fd_sbpf_syscalls_t const * syscalls );
671 :
672 : /* fd_vm_syscall API **************************************************/
673 :
674 : /* FIXME: fd_sbpf_syscalls_t and fd_sbpf_syscall_func_t probably should
675 : be moved from ballet/sbpf to here. */
676 :
677 : /* Note: the syscall map is kept separate from the fd_vm_t itself to
678 : support, for example, multiple fd_vm_t executing transactions
679 : concurrently for a slot. They could use the same syscalls for setup,
680 : memory and cache efficiency. */
681 :
682 : /* fd_vm_syscall_register inserts the syscall with the given cstr name
683 : into the given syscalls. The VM syscall implementation to use is
684 : given by func (NULL is fine though a VM itself may not accept such as
685 : valid). The caller promises there is room in the syscall map.
686 : Returns FD_VM_SUCCESS (0) on success or a FD_VM_ERR code (negative)
687 : on failure. Reasons for failure include INVAL (NULL syscalls, NULL
688 : name, name or the hash of name already in the map). On success,
689 : syscalls retains a read-only interest in name (e.g. use an infinite
690 : lifetime cstr here). (This function is exposed to allow VM users to
691 : add custom syscalls but most use cases probably should just call
692 : fd_vm_syscall_register_slot below.)
693 :
694 : IMPORTANT SAFETY TIP! See notes in syscall/fd_vm_syscall.h on what a
695 : syscall should expect to see and what to return. */
696 :
697 : int
698 : fd_vm_syscall_register( fd_sbpf_syscalls_t * syscalls,
699 : char const * name,
700 : fd_sbpf_syscall_func_t func );
701 :
702 : /* fd_vm_syscall_register_slot unmaps all syscalls in the current map
703 : (also ending any interest in the corresponding name cstr) and
704 : registers all syscalls appropriate for the slot described by
705 : slot_ctx. Returns FD_VM_SUCCESS (0) on success and FD_VM_ERR code
706 : (negative) on failure. Reasons for failure include INVAL (NULL
707 : syscalls) and FULL (tried to register too many system calls ...
708 : compile time map size needs to be adjusted). If slot_ctx is NULL,
709 : will register all fd_vm syscall implementations (whether or not that
710 : makes sense ... may change between Firedancer versions without
711 : warning). FIXME: probably better to pass the features for a slot
712 : than pass the whole slot_ctx.
713 :
714 : is_deploy should be 1 if the set of syscalls registered should be that
715 : used to verify programs before they are deployed, and 0 if it
716 : should be the set used to execute programs. */
717 :
718 : int
719 : fd_vm_syscall_register_slot( fd_sbpf_syscalls_t * syscalls,
720 : ulong slot,
721 : fd_features_t const * features,
722 : uchar is_deploy );
723 :
724 : /* fd_vm_syscall_register_all is a shorthand for registering all
725 : syscalls (see register slot). */
726 :
727 : static inline int
728 0 : fd_vm_syscall_register_all( fd_sbpf_syscalls_t * syscalls, uchar is_deploy ) {
729 0 : return fd_vm_syscall_register_slot( syscalls, 0UL, NULL, is_deploy );
730 0 : }
731 :
732 : FD_PROTOTYPES_END
733 :
734 : #endif /* HEADER_fd_src_flamenco_vm_fd_vm_base_h */
|