From 0af9a2118022d33fa0046f9a0a5ee0b1b69da43c Mon Sep 17 00:00:00 2001 From: Emanuele Cesena Date: Tue, 14 May 2024 20:51:16 +0000 Subject: [PATCH] vm: refactor fd_vm_log_appendf -> fd_vm_log_append_printf --- src/flamenco/vm/fd_vm_private.h | 39 +++++++++---------- .../vm/syscall/fd_vm_syscall_crypto.c | 4 +- src/flamenco/vm/syscall/fd_vm_syscall_hash.c | 6 +-- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/flamenco/vm/fd_vm_private.h b/src/flamenco/vm/fd_vm_private.h index 94b776c9e8..1a2ac5aea9 100644 --- a/src/flamenco/vm/fd_vm_private.h +++ b/src/flamenco/vm/fd_vm_private.h @@ -382,26 +382,25 @@ fd_vm_log_append( fd_vm_t * vm, return vm; } -/* fd_vm_log_appendf is a convenience wrapper to fd_vm_log_append that - allows to format strings like printf. - Returns NULL on formatting error, vm on success. */ - -#include -#include - -static inline fd_vm_t * -fd_vm_log_appendf( fd_vm_t * vm, - const char * format, - ... ) { - char msg[ FD_VM_LOG_TAIL+1 ]; - va_list args; - va_start (args, format); - int msg_sz = vsnprintf( msg, FD_VM_LOG_TAIL, format, args ); - if( FD_UNLIKELY( msg_sz<0 ) ) { - return NULL; - } - return fd_vm_log_append( vm, msg, (ulong)msg_sz ); -} +/* fd_vm_log_append_printf is analogous to fd_vm_log_append and allows + to format arguments like printf. + It cancels any VM log message in preparation on vm and appends the message + resulting from formatting args to the VM's log. The formatted message + includes a last byte set to 0. + Assumes vm is valid. + In case of formatting errors, no log is appended and the error is ignored + (internally, _vm->log[_vm->log_sz] is overwritten with 0). + Returns vm. */ + +#define fd_vm_log_append_printf( vm, fmt, ... ) (__extension__({ \ + fd_vm_t * _vm = (vm); \ + ulong _sz = _vm->log_sz; /* In [0,FD_VM_LOG_MAX] */ \ + ulong _len; \ + fd_cstr_printf_check( (char *)_vm->log + _sz, FD_VM_LOG_MAX + 1UL - _sz, \ + &_len, (fmt), __VA_ARGS__ ); \ + _vm->log_sz = _sz + _len; \ + (_vm); \ +})) FD_PROTOTYPES_END diff --git a/src/flamenco/vm/syscall/fd_vm_syscall_crypto.c b/src/flamenco/vm/syscall/fd_vm_syscall_crypto.c index 032ad1f8e0..7f1a14ef4f 100644 --- a/src/flamenco/vm/syscall/fd_vm_syscall_crypto.c +++ b/src/flamenco/vm/syscall/fd_vm_syscall_crypto.c @@ -213,7 +213,7 @@ fd_vm_syscall_sol_poseidon( void * _vm, /* https://github.com/anza-xyz/agave/blob/v1.18.12/programs/bpf_loader/src/syscalls/mod.rs#L1691-L1698 */ if( FD_UNLIKELY( vals_len > FD_VM_SYSCALL_SOL_POSEIDON_MAX_VALS ) ) { - fd_vm_log_appendf( vm, "Poseidon hashing %lu sequences is not supported", vals_len ); + fd_vm_log_append_printf( vm, "Poseidon hashing %lu sequences is not supported", vals_len ); return FD_VM_ERR_INVAL; /* SyscallError::InvalidLength */ } @@ -231,7 +231,7 @@ fd_vm_syscall_sol_poseidon( void * _vm, /* The following can never happen, left as comment for completeness. if( FD_UNLIKELY( cost == ULONG_MAX ) ) { - fd_vm_log_appendf( vm, "Overflow while calculating the compute cost" ); + fd_vm_log_append_printf( vm, "Overflow while calculating the compute cost" ); return FD_VM_ERR_INVAL; // SyscallError::ArithmeticOverflow } */ diff --git a/src/flamenco/vm/syscall/fd_vm_syscall_hash.c b/src/flamenco/vm/syscall/fd_vm_syscall_hash.c index 7920d36839..4ec1fa6071 100644 --- a/src/flamenco/vm/syscall/fd_vm_syscall_hash.c +++ b/src/flamenco/vm/syscall/fd_vm_syscall_hash.c @@ -36,7 +36,7 @@ fd_vm_syscall_sol_sha256( /**/ void * _vm, /* https://github.com/anza-xyz/agave/blob/v1.18.12/programs/bpf_loader/src/syscalls/mod.rs#L1911-L1920 */ if( FD_UNLIKELY( FD_VM_SHA256_MAX_SLICES < vals_len ) ) { - fd_vm_log_appendf( vm, "%s Hashing %d sequences in one syscall is over the limit %d", "Sha256", vals_len, FD_VM_SHA256_MAX_SLICES ); + fd_vm_log_append_printf( vm, "%s Hashing %lu sequences in one syscall is over the limit %lu", "Sha256", vals_len, FD_VM_SHA256_MAX_SLICES ); return FD_VM_ERR_INVAL; /* SyscallError::TooManySlices */ } @@ -89,7 +89,7 @@ fd_vm_syscall_sol_blake3( /**/ void * _vm, /* https://github.com/anza-xyz/agave/blob/v1.18.12/programs/bpf_loader/src/syscalls/mod.rs#L1911-L1920 */ if( FD_UNLIKELY( FD_VM_SHA256_MAX_SLICES < vals_len ) ) { - fd_vm_log_appendf( vm, "%s Hashing %d sequences in one syscall is over the limit %d", "Blake3", vals_len, FD_VM_SHA256_MAX_SLICES ); + fd_vm_log_append_printf( vm, "%s Hashing %lu sequences in one syscall is over the limit %lu", "Blake3", vals_len, FD_VM_SHA256_MAX_SLICES ); return FD_VM_ERR_INVAL; /* SyscallError::TooManySlices */ } @@ -142,7 +142,7 @@ fd_vm_syscall_sol_keccak256( /**/ void * _vm, /* https://github.com/anza-xyz/agave/blob/v1.18.12/programs/bpf_loader/src/syscalls/mod.rs#L1911-L1920 */ if( FD_UNLIKELY( FD_VM_SHA256_MAX_SLICES < vals_len ) ) { - fd_vm_log_appendf( vm, "%s Hashing %lu sequences in one syscall is over the limit %lu", "Keccak256", vals_len, FD_VM_SHA256_MAX_SLICES ); + fd_vm_log_append_printf( vm, "%s Hashing %lu sequences in one syscall is over the limit %lu", "Keccak256", vals_len, FD_VM_SHA256_MAX_SLICES ); return FD_VM_ERR_INVAL; /* SyscallError::TooManySlices */ }