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