Skip to content

Commit

Permalink
flamenco, vm: (syscall err refactor part 1) refactor syscalls impls t…
Browse files Browse the repository at this point in the history
…o emit syscall errors instead of vm errors
  • Loading branch information
ravyu-jump committed Dec 6, 2024
1 parent 6950ff2 commit 49f5f14
Show file tree
Hide file tree
Showing 18 changed files with 242 additions and 235 deletions.
8 changes: 4 additions & 4 deletions src/flamenco/runtime/tests/fd_exec_instr_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1806,10 +1806,10 @@ fd_exec_vm_syscall_test_run( fd_exec_instr_test_runner_t * runner,
access violations in Agave. The agave_access_violation_mask bitset sets
the error codes that are expected to be access violations in Agave. */
if( is_cpi &&
( syscall_err == FD_VM_ERR_SYSCALL_TOO_MANY_SIGNERS ||
syscall_err == FD_VM_ERR_SYSCALL_INSTRUCTION_TOO_LARGE ||
syscall_err == FD_VM_ERR_SYSCALL_MAX_INSTRUCTION_ACCOUNTS_EXCEEDED ||
syscall_err == FD_VM_ERR_SYSCALL_MAX_INSTRUCTION_ACCOUNT_INFOS_EXCEEDED ) ) {
( syscall_err == FD_VM_SYSCALL_ERR_TOO_MANY_SIGNERS ||
syscall_err == FD_VM_SYSCALL_ERR_INSTRUCTION_TOO_LARGE ||
syscall_err == FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNTS_EXCEEDED ||
syscall_err == FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNT_INFOS_EXCEEDED ) ) {

/* FD performs pre-flight checks that manifest as access violations in Agave */
vm->instr_ctx->txn_ctx->exec_err = FD_VM_ERR_EBPF_ACCESS_VIOLATION;
Expand Down
90 changes: 46 additions & 44 deletions src/flamenco/vm/fd_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,37 @@ fd_vm_syscall_strerror( int err ) {

switch( err ) {

case FD_VM_ERR_SYSCALL_INVALID_STRING: return "invalid utf-8 sequence"; // truncated
case FD_VM_ERR_SYSCALL_ABORT: return "SBF program panicked";
case FD_VM_ERR_SYSCALL_PANIC: return "SBF program Panicked in..."; // truncated
case FD_VM_ERR_SYSCALL_INVOKE_CONTEXT_BORROW_FAILED: return "Cannot borrow invoke context";
case FD_VM_ERR_SYSCALL_MALFORMED_SIGNER_SEED: return "Malformed signer seed"; // truncated
case FD_VM_ERR_SYSCALL_BAD_SEEDS: return "Could not create program address with signer seeds"; // truncated
case FD_VM_ERR_SYSCALL_PROGRAM_NOT_SUPPORTED: return "Program not supported by inner instructions"; // truncated
case FD_VM_ERR_SYSCALL_UNALIGNED_POINTER: return "Unaligned pointer";
case FD_VM_ERR_SYSCALL_TOO_MANY_SIGNERS: return "Too many signers";
case FD_VM_ERR_SYSCALL_INSTRUCTION_TOO_LARGE: return "Instruction passed to inner instruction is too large"; // truncated
case FD_VM_ERR_SYSCALL_TOO_MANY_ACCOUNTS: return "Too many accounts passed to inner instruction";
case FD_VM_ERR_SYSCALL_COPY_OVERLAPPING: return "Overlapping copy";
case FD_VM_ERR_SYSCALL_RETURN_DATA_TOO_LARGE: return "Return data too large"; // truncated
case FD_VM_ERR_SYSCALL_TOO_MANY_SLICES: return "Hashing too many sequences";
case FD_VM_ERR_SYSCALL_INVALID_LENGTH: return "InvalidLength";
case FD_VM_ERR_SYSCALL_MAX_INSTRUCTION_DATA_LEN_EXCEEDED: return "Invoked an instruction with data that is too large"; // truncated
case FD_VM_ERR_SYSCALL_MAX_INSTRUCTION_ACCOUNTS_EXCEEDED: return "Invoked an instruction with too many accounts"; // truncated
case FD_VM_ERR_SYSCALL_MAX_INSTRUCTION_ACCOUNT_INFOS_EXCEEDED: return "Invoked an instruction with too many account info's"; // truncated
case FD_VM_ERR_SYSCALL_INVALID_ATTRIBUTE: return "InvalidAttribute";
case FD_VM_ERR_SYSCALL_INVALID_POINTER: return "Invalid pointer";
case FD_VM_ERR_SYSCALL_ARITHMETIC_OVERFLOW: return "Arithmetic overflow";

case FD_VM_ERR_SYSCALL_POSEIDON_INVALID_PARAMS: return "Syscall error: Invalid parameters.";
case FD_VM_ERR_SYSCALL_POSEIDON_INVALID_ENDIANNESS: return "Syscall error: Invalid endianness.";
case FD_VM_SYSCALL_ERR_INVALID_STRING: return "invalid utf-8 sequence"; // truncated
case FD_VM_SYSCALL_ERR_ABORT: return "SBF program panicked";
case FD_VM_SYSCALL_ERR_PANIC: return "SBF program Panicked in..."; // truncated
case FD_VM_SYSCALL_ERR_INVOKE_CONTEXT_BORROW_FAILED: return "Cannot borrow invoke context";
case FD_VM_SYSCALL_ERR_MALFORMED_SIGNER_SEED: return "Malformed signer seed"; // truncated
case FD_VM_SYSCALL_ERR_BAD_SEEDS: return "Could not create program address with signer seeds"; // truncated
case FD_VM_SYSCALL_ERR_PROGRAM_NOT_SUPPORTED: return "Program not supported by inner instructions"; // truncated
case FD_VM_SYSCALL_ERR_UNALIGNED_POINTER: return "Unaligned pointer";
case FD_VM_SYSCALL_ERR_TOO_MANY_SIGNERS: return "Too many signers";
case FD_VM_SYSCALL_ERR_INSTRUCTION_TOO_LARGE: return "Instruction passed to inner instruction is too large"; // truncated
case FD_VM_SYSCALL_ERR_TOO_MANY_ACCOUNTS: return "Too many accounts passed to inner instruction";
case FD_VM_SYSCALL_ERR_COPY_OVERLAPPING: return "Overlapping copy";
case FD_VM_SYSCALL_ERR_RETURN_DATA_TOO_LARGE: return "Return data too large"; // truncated
case FD_VM_SYSCALL_ERR_TOO_MANY_SLICES: return "Hashing too many sequences";
case FD_VM_SYSCALL_ERR_INVALID_LENGTH: return "InvalidLength";
case FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_DATA_LEN_EXCEEDED: return "Invoked an instruction with data that is too large"; // truncated
case FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNTS_EXCEEDED: return "Invoked an instruction with too many accounts"; // truncated
case FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNT_INFOS_EXCEEDED: return "Invoked an instruction with too many account info's"; // truncated
case FD_VM_SYSCALL_ERR_INVALID_ATTRIBUTE: return "InvalidAttribute";
case FD_VM_SYSCALL_ERR_INVALID_POINTER: return "Invalid pointer";
case FD_VM_SYSCALL_ERR_ARITHMETIC_OVERFLOW: return "Arithmetic overflow";

case FD_VM_SYSCALL_ERR_INSTR_ERR: return "Instruction error";
case FD_VM_SYSCALL_ERR_INVALID_PDA: return "Invalid PDA";
case FD_VM_SYSCALL_ERR_COMPUTE_BUDGET_EXCEEDED: return "Compute budget exceeded";
case FD_VM_SYSCALL_ERR_SEGFAULT: return "Segmentation fault";
case FD_VM_SYSCALL_ERR_OUTSIDE_RUNTIME: return "Syscall executed outside runtime";


case FD_VM_SYSCALL_ERR_POSEIDON_INVALID_PARAMS: return "Syscall error: Invalid parameters.";
case FD_VM_SYSCALL_ERR_POSEIDON_INVALID_ENDIANNESS: return "Syscall error: Invalid endianness.";

default: break;
}
Expand Down Expand Up @@ -100,27 +107,22 @@ fd_vm_strerror( int err ) {

/* VM exec error codes */

case FD_VM_ERR_SIGTEXT: return "SIGTEXT illegal program counter";
case FD_VM_ERR_SIGSPLIT: return "SIGSPLIT split multiword instruction";
case FD_VM_ERR_SIGCALL: return "unsupported BPF instruction";
case FD_VM_ERR_SIGSTACK: return "SIGSTACK call depth limit exceeded";
case FD_VM_ERR_SIGILL: return "SIGILL illegal instruction";
case FD_VM_ERR_SIGSEGV: return "SIGSEGV illegal memory address";
case FD_VM_ERR_SIGBUS: return "SIGBUS misaligned memory address";
case FD_VM_ERR_SIGRDONLY: return "SIGRDONLY illegal write";
case FD_VM_ERR_SIGCOST: return "SIGCOST compute unit limit exceeded";
case FD_VM_ERR_SIGFPE: return "SIGFPE division by zero";
case FD_VM_ERR_SIGFPE_OF: return "SIGFPE division overflow";

/* VM syscall error codes */
/* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/mod.rs#L81 */

case FD_VM_ERR_ABORT: return "SBF program panicked";
case FD_VM_ERR_PANIC: return "PANIC"; /* FIXME: description */
case FD_VM_ERR_SIGTEXT: return "SIGTEXT illegal program counter";
case FD_VM_ERR_SIGSPLIT: return "SIGSPLIT split multiword instruction";
case FD_VM_ERR_SIGCALL: return "unsupported BPF instruction";
case FD_VM_ERR_SIGSTACK: return "SIGSTACK call depth limit exceeded";
case FD_VM_ERR_SIGILL: return "SIGILL illegal instruction";
case FD_VM_ERR_SIGSEGV: return "SIGSEGV illegal memory address";
case FD_VM_ERR_SIGBUS: return "SIGBUS misaligned memory address";
case FD_VM_ERR_SIGRDONLY: return "SIGRDONLY illegal write";
case FD_VM_ERR_SIGCOST: return "SIGCOST compute unit limit exceeded";
case FD_VM_ERR_SIGFPE: return "SIGFPE division by zero";
case FD_VM_ERR_SIGFPE_OF: return "SIGFPE division overflow";
case FD_VM_ERR_SIGSYSCALL: return "SIGSYSCALL syscall error";
case FD_VM_ERR_SIGABORT: return "SIGABORT abort error";

/* (Deprecated) Syscall errors */
case FD_VM_ERR_MEM_OVERLAP: return "MEM_OVERLAP"; /* FIXME: description */
case FD_VM_ERR_INSTR_ERR: return "INSTR_ERR"; /* FIXME: description */
case FD_VM_ERR_RETURN_DATA_TOO_LARGE: return "RETURN_DATA_TOO_LARGE"; /* FIXME: description */
case FD_VM_ERR_INVOKE_CONTEXT_BORROW_FAILED: return "INVOKE_CONTEXT_BORROW_FAILED"; /* FIXME: description */

/* VM validate error codes */

Expand Down
83 changes: 43 additions & 40 deletions src/flamenco/vm/fd_vm_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,22 @@
#define FD_VM_ERR_SIGSEGV (-13) /* illegal memory address (e.g. read/write to an address not backed by any memory) */
#define FD_VM_ERR_SIGBUS (-14) /* misaligned memory address (e.g. read/write to an address with inappropriate alignment) */
#define FD_VM_ERR_SIGRDONLY (-15) /* illegal write (e.g. write to a read only address) */
#define FD_VM_ERR_SIGCOST (-16) /* compute unit limit exceeded (syscalls that exceed their budget should use this too) */
#define FD_VM_ERR_INVALID_PDA (-17) /* the computed pda was not a valid ed25519 point */
#define FD_VM_ERR_SIGCOST (-16) /* compute unit limit exceeded */
// #define FD_VM_ERR_INVALID_PDA (-17) /* (deprecated, moved to syscall error) the computed pda was not a valid ed25519 point */
#define FD_VM_ERR_SIGFPE (-18) /* divide by zero */
#define FD_VM_ERR_SIGFPE_OF (-118)/* divide overflow */
#define FD_VM_ERR_SIGFPE_OF (-19) /* divide overflow */
#define FD_VM_ERR_SIGSYSCALL (-20) /* Generic syscall error */
#define FD_VM_ERR_SIGABORT (-21) /* Generic abort error (used in JIT) */

/* FIXME: Are these exact matches to Solana? If so, provide link, if
not, document and refine name / consolidate further. */
/* (DEPRECATED) VM syscall error codes. These are only produced by fd_vm_syscall
implementations. */

/* VM syscall error codes. These are only produced by fd_vm_syscall
implementations. FIXME: Consider having syscalls return standard
error codes and then provide detail like this through an info arg.
FIXME: Are these exact matches to Solana? If so, provide link? If
not document and refine names / consolidate further. */

#define FD_VM_ERR_ABORT (-19) /* FIXME: description */
#define FD_VM_ERR_PANIC (-20) /* FIXME: description */
#define FD_VM_ERR_MEM_OVERLAP (-21) /* FIXME: description */
#define FD_VM_ERR_INSTR_ERR (-22) /* FIXME: description */
#define FD_VM_ERR_INVOKE_CONTEXT_BORROW_FAILED (-23) /* FIXME: description */
#define FD_VM_ERR_RETURN_DATA_TOO_LARGE (-24) /* FIXME: description */
// #define FD_VM_ERR_ABORT (-119) /* FIXME: description PENDING SYSVAR SYSCALL */
// #define FD_VM_ERR_PANIC (-120) /* FIXME: description PENDING TESTS refactor */
#define FD_VM_ERR_MEM_OVERLAP (-121) /* FIXME: description PENDING FD_VM_MEM_CHECK_NON_OVERLAPPING refactor */
// #define FD_VM_ERR_INSTR_ERR (-22) /* FIXME: description DONE */
// #define FD_VM_ERR_INVOKE_CONTEXT_BORROW_FAILED (-23) /* FIXME: description DONE (unused) */
// #define FD_VM_ERR_RETURN_DATA_TOO_LARGE (-24) /* FIXME: description DONE */

/* sBPF validation error codes. These are only produced by
fd_vm_validate. FIXME: Consider having fd_vm_validate return
Expand All @@ -83,31 +79,38 @@
/* Syscall Errors
https://github.com/anza-xyz/agave/blob/v2.0.7/programs/bpf_loader/src/syscalls/mod.rs#L81 */

#define FD_VM_ERR_SYSCALL_INVALID_STRING (-1)
#define FD_VM_ERR_SYSCALL_ABORT (-2)
#define FD_VM_ERR_SYSCALL_PANIC (-3)
#define FD_VM_ERR_SYSCALL_INVOKE_CONTEXT_BORROW_FAILED (-4)
#define FD_VM_ERR_SYSCALL_MALFORMED_SIGNER_SEED (-5)
#define FD_VM_ERR_SYSCALL_BAD_SEEDS (-6)
#define FD_VM_ERR_SYSCALL_PROGRAM_NOT_SUPPORTED (-7)
#define FD_VM_ERR_SYSCALL_UNALIGNED_POINTER (-8)
#define FD_VM_ERR_SYSCALL_TOO_MANY_SIGNERS (-9)
#define FD_VM_ERR_SYSCALL_INSTRUCTION_TOO_LARGE (-10)
#define FD_VM_ERR_SYSCALL_TOO_MANY_ACCOUNTS (-11)
#define FD_VM_ERR_SYSCALL_COPY_OVERLAPPING (-12)
#define FD_VM_ERR_SYSCALL_RETURN_DATA_TOO_LARGE (-13)
#define FD_VM_ERR_SYSCALL_TOO_MANY_SLICES (-14)
#define FD_VM_ERR_SYSCALL_INVALID_LENGTH (-15)
#define FD_VM_ERR_SYSCALL_MAX_INSTRUCTION_DATA_LEN_EXCEEDED (-16)
#define FD_VM_ERR_SYSCALL_MAX_INSTRUCTION_ACCOUNTS_EXCEEDED (-17)
#define FD_VM_ERR_SYSCALL_MAX_INSTRUCTION_ACCOUNT_INFOS_EXCEEDED (-18)
#define FD_VM_ERR_SYSCALL_INVALID_ATTRIBUTE (-19)
#define FD_VM_ERR_SYSCALL_INVALID_POINTER (-20)
#define FD_VM_ERR_SYSCALL_ARITHMETIC_OVERFLOW (-21)
#define FD_VM_SYSCALL_ERR_INVALID_STRING (-1)
#define FD_VM_SYSCALL_ERR_ABORT (-2)
#define FD_VM_SYSCALL_ERR_PANIC (-3)
#define FD_VM_SYSCALL_ERR_INVOKE_CONTEXT_BORROW_FAILED (-4)
#define FD_VM_SYSCALL_ERR_MALFORMED_SIGNER_SEED (-5)
#define FD_VM_SYSCALL_ERR_BAD_SEEDS (-6)
#define FD_VM_SYSCALL_ERR_PROGRAM_NOT_SUPPORTED (-7)
#define FD_VM_SYSCALL_ERR_UNALIGNED_POINTER (-8)
#define FD_VM_SYSCALL_ERR_TOO_MANY_SIGNERS (-9)
#define FD_VM_SYSCALL_ERR_INSTRUCTION_TOO_LARGE (-10)
#define FD_VM_SYSCALL_ERR_TOO_MANY_ACCOUNTS (-11)
#define FD_VM_SYSCALL_ERR_COPY_OVERLAPPING (-12)
#define FD_VM_SYSCALL_ERR_RETURN_DATA_TOO_LARGE (-13)
#define FD_VM_SYSCALL_ERR_TOO_MANY_SLICES (-14)
#define FD_VM_SYSCALL_ERR_INVALID_LENGTH (-15)
#define FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_DATA_LEN_EXCEEDED (-16)
#define FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNTS_EXCEEDED (-17)
#define FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNT_INFOS_EXCEEDED (-18)
#define FD_VM_SYSCALL_ERR_INVALID_ATTRIBUTE (-19)
#define FD_VM_SYSCALL_ERR_INVALID_POINTER (-20)
#define FD_VM_SYSCALL_ERR_ARITHMETIC_OVERFLOW (-21)

/* These syscall errors are unique to Firedancer and do not have an Agave equivalent. */
#define FD_VM_SYSCALL_ERR_INSTR_ERR (-22)
#define FD_VM_SYSCALL_ERR_INVALID_PDA (-23) /* the computed pda was not a valid ed25519 point */
#define FD_VM_SYSCALL_ERR_COMPUTE_BUDGET_EXCEEDED (-24) /* compute unit limit exceeded in syscall */
#define FD_VM_SYSCALL_ERR_SEGFAULT (-25) /* illegal memory address (e.g. read/write to an address not backed by any memory) in syscall */
#define FD_VM_SYSCALL_ERR_OUTSIDE_RUNTIME (-26) /* syscall called with vm not running in solana runtime */

/* Poseidon returns custom errors for some reason */
#define FD_VM_ERR_SYSCALL_POSEIDON_INVALID_PARAMS (1)
#define FD_VM_ERR_SYSCALL_POSEIDON_INVALID_ENDIANNESS (2)
#define FD_VM_SYSCALL_ERR_POSEIDON_INVALID_PARAMS (1)
#define FD_VM_SYSCALL_ERR_POSEIDON_INVALID_ENDIANNESS (2)

/* EbpfError
https://github.com/solana-labs/rbpf/blob/v0.8.5/src/error.rs#L17 */
Expand Down
Loading

0 comments on commit 49f5f14

Please sign in to comment.