Skip to content

Commit

Permalink
test-vectors: fix asan/ubsan complains
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0ece committed Jul 30, 2024
1 parent 9b13515 commit 491e23c
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 104 deletions.
2 changes: 1 addition & 1 deletion contrib/test/run_test_vectors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ LOG=$LOG_PATH/test_exec_syscall
cat contrib/test/syscall-fixtures.list | xargs ./$OBJDIR/unit-test/test_exec_sol_compat --log-path $LOG

LOG=$LOG_PATH/test_exec_precompiles
cat contrib/test/precompile-fixtures.list | xargs ./$OBJDIR/unit-test/test_exec_sol_compat --log-path $LOG
cat contrib/test/precompile-fixtures.list | xargs ./$OBJDIR/unit-test/test_exec_sol_compat --log-path $LOG

zstd -df dump/test-vectors/elf_loader/fixtures/*.zst
LOG=$LOG_PATH/test_elf_loader
Expand Down
8 changes: 4 additions & 4 deletions src/ballet/ed25519/avx512/fd_f25519.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ fd_f25519_mul_121666( fd_f25519_t * r,
FD_25519_INLINE fd_f25519_t *
fd_f25519_frombytes( fd_f25519_t * r,
uchar const buf[ 32 ] ) {
ulong y0 = ((ulong *)buf)[0]; /* Bits 0- 63 */
ulong y1 = ((ulong *)buf)[1]; /* Bits 64-127 */
ulong y2 = ((ulong *)buf)[2]; /* Bits 128-191 */
ulong y3 = ((ulong *)buf)[3] & 0x7fffffffffffffff; /* Bits 192-254 */
ulong y0 = fd_ulong_load_8_fast( buf ); /* Bits 0- 63 */
ulong y1 = fd_ulong_load_8_fast( buf+8 ); /* Bits 64-127 */
ulong y2 = fd_ulong_load_8_fast( buf+16 ); /* Bits 128-191 */
ulong y3 = fd_ulong_load_8_fast( buf+24 ) & 0x7fffffffffffffff; /* Bits 192-254 */
r->el = fd_r43x6_unpack( wv( y0, y1, y2, y3 ) );
return r;
}
Expand Down
8 changes: 4 additions & 4 deletions src/ballet/ed25519/fd_curve25519_scalar.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ fd_curve25519_scalar_reduce( uchar out[ 32 ],

static inline uchar const *
fd_curve25519_scalar_validate( uchar const s[ 32 ] ) {
ulong s0 = *(ulong *)(&s[ 0 ]);
ulong s1 = *(ulong *)(&s[ 8 ]);
ulong s2 = *(ulong *)(&s[ 16 ]);
ulong s3 = *(ulong *)(&s[ 24 ]);
ulong s0 = fd_ulong_load_8_fast( s );
ulong s1 = fd_ulong_load_8_fast( s + 8 );
ulong s2 = fd_ulong_load_8_fast( s + 16 );
ulong s3 = fd_ulong_load_8_fast( s + 24 );
ulong l0 = *(ulong *)(&fd_curve25519_scalar_minus_one[ 0 ]);
ulong l1 = *(ulong *)(&fd_curve25519_scalar_minus_one[ 8 ]);
ulong l2 = *(ulong *)(&fd_curve25519_scalar_minus_one[ 16 ]);
Expand Down
57 changes: 27 additions & 30 deletions src/ballet/sha256/fd_sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,16 +349,15 @@ fd_sha256_fini( fd_sha256_t * sha,

/* Unpack the result into md (annoying bswaps here) */

uint * hash = (uint *)_hash;
hash[0] = fd_uint_bswap( state[0] );
hash[1] = fd_uint_bswap( state[1] );
hash[2] = fd_uint_bswap( state[2] );
hash[3] = fd_uint_bswap( state[3] );
hash[4] = fd_uint_bswap( state[4] );
hash[5] = fd_uint_bswap( state[5] );
hash[6] = fd_uint_bswap( state[6] );
hash[7] = fd_uint_bswap( state[7] );
return _hash;
state[0] = fd_uint_bswap( state[0] );
state[1] = fd_uint_bswap( state[1] );
state[2] = fd_uint_bswap( state[2] );
state[3] = fd_uint_bswap( state[3] );
state[4] = fd_uint_bswap( state[4] );
state[5] = fd_uint_bswap( state[5] );
state[6] = fd_uint_bswap( state[6] );
state[7] = fd_uint_bswap( state[7] );
return fd_memcpy( _hash, state, 32 );
}

void *
Expand Down Expand Up @@ -401,16 +400,15 @@ fd_sha256_hash( void const * _data,
FD_STORE( ulong, buf+FD_SHA256_PRIVATE_BUF_MAX-8UL, fd_ulong_bswap( bit_cnt ) );
fd_sha256_core( state, buf, 1UL );

uint * hash = (uint *)_hash;
hash[0] = fd_uint_bswap( state[0] );
hash[1] = fd_uint_bswap( state[1] );
hash[2] = fd_uint_bswap( state[2] );
hash[3] = fd_uint_bswap( state[3] );
hash[4] = fd_uint_bswap( state[4] );
hash[5] = fd_uint_bswap( state[5] );
hash[6] = fd_uint_bswap( state[6] );
hash[7] = fd_uint_bswap( state[7] );
return _hash;
state[0] = fd_uint_bswap( state[0] );
state[1] = fd_uint_bswap( state[1] );
state[2] = fd_uint_bswap( state[2] );
state[3] = fd_uint_bswap( state[3] );
state[4] = fd_uint_bswap( state[4] );
state[5] = fd_uint_bswap( state[5] );
state[6] = fd_uint_bswap( state[6] );
state[7] = fd_uint_bswap( state[7] );
return fd_memcpy( _hash, state, 32 );
}

void *
Expand Down Expand Up @@ -454,16 +452,15 @@ fd_sha256_hash_32( void const * _data,
FD_STORE( ulong, buf+FD_SHA256_PRIVATE_BUF_MAX-8UL, fd_ulong_bswap( bit_cnt ) );
fd_sha256_core( state, buf, 1UL );

uint * hash = (uint *)_hash;
hash[0] = fd_uint_bswap( state[0] );
hash[1] = fd_uint_bswap( state[1] );
hash[2] = fd_uint_bswap( state[2] );
hash[3] = fd_uint_bswap( state[3] );
hash[4] = fd_uint_bswap( state[4] );
hash[5] = fd_uint_bswap( state[5] );
hash[6] = fd_uint_bswap( state[6] );
hash[7] = fd_uint_bswap( state[7] );
return _hash;
state[0] = fd_uint_bswap( state[0] );
state[1] = fd_uint_bswap( state[1] );
state[2] = fd_uint_bswap( state[2] );
state[3] = fd_uint_bswap( state[3] );
state[4] = fd_uint_bswap( state[4] );
state[5] = fd_uint_bswap( state[5] );
state[6] = fd_uint_bswap( state[6] );
state[7] = fd_uint_bswap( state[7] );
return fd_memcpy( _hash, state, 32 );
}

#undef fd_sha256_core
54 changes: 32 additions & 22 deletions src/flamenco/runtime/tests/fd_exec_instr_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1268,11 +1268,13 @@ fd_exec_instr_fixture_run( fd_exec_instr_test_runner_t * runner,
}

ulong
fd_exec_instr_test_run( fd_exec_instr_test_runner_t * runner,
fd_exec_test_instr_context_t const * input,
fd_exec_test_instr_effects_t ** output,
void * output_buf,
ulong output_bufsz ) {
fd_exec_instr_test_run( fd_exec_instr_test_runner_t * runner,
void const * input_,
void ** output_,
void * output_buf,
ulong output_bufsz ) {
fd_exec_test_instr_context_t const * input = fd_type_pun_const( input_ );
fd_exec_test_instr_effects_t ** output = fd_type_pun( output_ );
fd_wksp_t * wksp = fd_wksp_attach( "wksp" );
fd_alloc_t * alloc = fd_alloc_join( fd_alloc_new( fd_wksp_alloc_laddr( wksp, fd_alloc_align(), fd_alloc_footprint(), 2 ), 2 ), 0 );

Expand Down Expand Up @@ -1378,11 +1380,14 @@ fd_exec_instr_test_run( fd_exec_instr_test_runner_t * runner,
}

ulong
fd_exec_txn_test_run( fd_exec_instr_test_runner_t * runner, // Runner only contains funk instance, so we can borrow instr test runner
fd_exec_test_txn_context_t const * input,
fd_exec_test_txn_result_t ** output,
void * output_buf,
ulong output_bufsz ) {
fd_exec_txn_test_run( fd_exec_instr_test_runner_t * runner, // Runner only contains funk instance, so we can borrow instr test runner
void const * input_,
void ** output_,
void * output_buf,
ulong output_bufsz ) {
fd_exec_test_txn_context_t const * input = fd_type_pun_const( input_ );
fd_exec_test_txn_result_t ** output = fd_type_pun( output_ );

FD_SCRATCH_SCOPE_BEGIN {
/* Initialize memory */
fd_wksp_t * wksp = fd_wksp_attach( "wksp" );
Expand Down Expand Up @@ -1497,10 +1502,13 @@ fd_exec_txn_test_run( fd_exec_instr_test_runner_t * runner, // Runner onl

ulong
fd_sbpf_program_load_test_run( FD_PARAM_UNUSED fd_exec_instr_test_runner_t * runner,
fd_exec_test_elf_loader_ctx_t const * input,
fd_exec_test_elf_loader_effects_t ** output,
void * output_buf,
ulong output_bufsz ){
void const * input_,
void ** output_,
void * output_buf,
ulong output_bufsz ) {
fd_exec_test_elf_loader_ctx_t const * input = fd_type_pun_const( input_ );
fd_exec_test_elf_loader_effects_t ** output = fd_type_pun( output_ );

fd_sbpf_elf_info_t info;
fd_valloc_t valloc = fd_scratch_virtual();

Expand Down Expand Up @@ -1609,11 +1617,13 @@ fd_sbpf_program_load_test_run( FD_PARAM_UNUSED fd_exec_instr_test_runner_t * run
}

ulong
fd_exec_vm_syscall_test_run( fd_exec_instr_test_runner_t * runner,
fd_exec_test_syscall_context_t const * input,
fd_exec_test_syscall_effects_t ** output,
void * output_buf,
ulong output_bufsz ) {
fd_exec_vm_syscall_test_run( fd_exec_instr_test_runner_t * runner,
void const * input_,
void ** output_,
void * output_buf,
ulong output_bufsz ) {
fd_exec_test_syscall_context_t const * input = fd_type_pun_const( input_ );
fd_exec_test_syscall_effects_t ** output = fd_type_pun( output_ );
fd_wksp_t * wksp = fd_wksp_attach( "wksp" );
fd_alloc_t * alloc = fd_alloc_join( fd_alloc_new( fd_wksp_alloc_laddr( wksp, fd_alloc_align(), fd_alloc_footprint(), 2 ), 2 ), 0 );

Expand Down Expand Up @@ -1748,18 +1758,18 @@ fd_exec_vm_syscall_test_run( fd_exec_instr_test_runner_t * runner,
effects->cu_avail = (ulong)vm->cu;

effects->heap = FD_SCRATCH_ALLOC_APPEND(
l, alignof(uchar), PB_BYTES_ARRAY_T_ALLOCSIZE( vm->heap_max ) );
l, alignof(uint), PB_BYTES_ARRAY_T_ALLOCSIZE( vm->heap_max ) );
effects->heap->size = (uint)vm->heap_max;
fd_memcpy( effects->heap->bytes, vm->heap, vm->heap_max );

effects->stack = FD_SCRATCH_ALLOC_APPEND(
l, alignof(uchar), PB_BYTES_ARRAY_T_ALLOCSIZE( FD_VM_STACK_MAX ) );
l, alignof(uint), PB_BYTES_ARRAY_T_ALLOCSIZE( FD_VM_STACK_MAX ) );
effects->stack->size = (uint)FD_VM_STACK_MAX;
fd_memcpy( effects->stack->bytes, vm->stack, FD_VM_STACK_MAX );

if( input_data_sz ) {
effects->inputdata = FD_SCRATCH_ALLOC_APPEND(
l, alignof(uchar), PB_BYTES_ARRAY_T_ALLOCSIZE( input_data_sz ) );
l, alignof(uint), PB_BYTES_ARRAY_T_ALLOCSIZE( input_data_sz ) );
effects->inputdata->size = (uint)input_data_sz;
fd_memcpy( effects->inputdata->bytes, vm->input, input_data_sz );
} else {
Expand Down
40 changes: 20 additions & 20 deletions src/flamenco/runtime/tests/fd_exec_instr_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,21 @@ fd_exec_instr_fixture_run( fd_exec_instr_test_runner_t * runner,
for failure. Reasons for failure include insufficient output_bufsz. */

ulong
fd_exec_instr_test_run( fd_exec_instr_test_runner_t * runner,
fd_exec_test_instr_context_t const * input,
fd_exec_test_instr_effects_t ** output,
void * output_buf,
ulong output_bufsz );
fd_exec_instr_test_run( fd_exec_instr_test_runner_t * runner,
void const * input_,
void ** output_,
void * output_buf,
ulong output_bufsz );

/*
Similar to above, but executes a txn given txn context (input)
*/
ulong
fd_exec_txn_test_run( fd_exec_instr_test_runner_t * runner, // Runner only contains funk instance, so we can borrow instr test runner
fd_exec_test_txn_context_t const * input,
fd_exec_test_txn_result_t ** output,
void * output_buf,
ulong output_bufsz );
fd_exec_txn_test_run( fd_exec_instr_test_runner_t * runner, // Runner only contains funk instance, so we can borrow instr test runner
void const * input_,
void ** output_,
void * output_buf,
ulong output_bufsz );

/* Loads an ELF binary (in input->elf.data()).
output_buf points to a memory region of output_bufsz bytes where the
Expand All @@ -96,18 +96,18 @@ fd_exec_txn_test_run( fd_exec_instr_test_runner_t * runner, // Runner onl
but output is incomplete/undefined.
*/
ulong
fd_sbpf_program_load_test_run( fd_exec_instr_test_runner_t * runner,
fd_exec_test_elf_loader_ctx_t const * input,
fd_exec_test_elf_loader_effects_t ** output,
void * output_buf,
ulong output_bufsz );
fd_sbpf_program_load_test_run( fd_exec_instr_test_runner_t * runner,
void const * input_,
void ** output_,
void * output_buf,
ulong output_bufsz );

ulong
fd_exec_vm_syscall_test_run( fd_exec_instr_test_runner_t * runner,
fd_exec_test_syscall_context_t const * input,
fd_exec_test_syscall_effects_t ** output,
void * output_buf,
ulong output_bufsz );
fd_exec_vm_syscall_test_run( fd_exec_instr_test_runner_t * runner,
void const * input_,
void ** output_,
void * output_buf,
ulong output_bufsz );


FD_PROTOTYPES_END
Expand Down
24 changes: 11 additions & 13 deletions src/flamenco/runtime/tests/fd_exec_sol_compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ sol_compat_instr_fixture( fd_exec_instr_test_runner_t * runner,

// Execute
void * output = NULL;
sol_compat_execute_wrapper( runner, &fixture->input, &output, (exec_test_run_fn_t *)fd_exec_instr_test_run );
sol_compat_execute_wrapper( runner, &fixture->input, &output, fd_exec_instr_test_run );

// Compare effects
int ok = sol_compat_cmp_binary_strict( output, &fixture->output, &fd_exec_test_instr_effects_t_msg );
Expand All @@ -290,7 +290,7 @@ sol_compat_precompile_fixture( fd_exec_instr_test_runner_t * runner,

// Execute
void * output = NULL;
sol_compat_execute_wrapper( runner, &fixture->input, &output, (exec_test_run_fn_t *)fd_exec_instr_test_run );
sol_compat_execute_wrapper( runner, &fixture->input, &output, fd_exec_instr_test_run );

// Compare effects
int ok = sol_compat_cmp_success_fail_only( output, &fixture->output );
Expand All @@ -314,7 +314,7 @@ sol_compat_elf_loader_fixture( fd_exec_instr_test_runner_t * runner,

// Execute
void * output = NULL;
sol_compat_execute_wrapper( runner, &fixture->input, &output, (exec_test_run_fn_t *)fd_sbpf_program_load_test_run );
sol_compat_execute_wrapper( runner, &fixture->input, &output, fd_sbpf_program_load_test_run );

// Compare effects
int ok = sol_compat_cmp_binary_strict( output, &fixture->output, &fd_exec_test_elf_loader_effects_t_msg );
Expand All @@ -337,7 +337,7 @@ sol_compat_syscall_fixture( fd_exec_instr_test_runner_t * runner,

// Execute
void * output = NULL;
sol_compat_execute_wrapper( runner, &fixture->input, &output, (exec_test_run_fn_t *)fd_exec_vm_syscall_test_run );
sol_compat_execute_wrapper( runner, &fixture->input, &output, fd_exec_vm_syscall_test_run );

// Compare effects
int ok = sol_compat_cmp_binary_strict( output, &fixture->output, &fd_exec_test_syscall_effects_t_msg );
Expand All @@ -360,10 +360,8 @@ sol_compat_validate_vm_fixture( fd_exec_instr_test_runner_t * runner,

// Execute
void * output = NULL;
sol_compat_execute_wrapper( runner,
&fixture->input,
&output,
(exec_test_run_fn_t *)fd_exec_vm_validate_test_run );
sol_compat_execute_wrapper( runner, &fixture->input, &output, fd_exec_vm_validate_test_run );

// Compare effects
int ok = sol_compat_cmp_binary_strict( output, &fixture->output, &fd_exec_test_validate_vm_effects_t_msg );

Expand Down Expand Up @@ -395,7 +393,7 @@ sol_compat_instr_execute_v1( uchar * out,

// Execute
void * output = NULL;
sol_compat_execute_wrapper( runner, input, &output, (exec_test_run_fn_t *)fd_exec_instr_test_run );
sol_compat_execute_wrapper( runner, input, &output, fd_exec_instr_test_run );

// Encode effects
int ok = 0;
Expand Down Expand Up @@ -432,7 +430,7 @@ sol_compat_txn_execute_v1( uchar * out,

// Execute
void * output = NULL;
sol_compat_execute_wrapper( runner, input, &output, (exec_test_run_fn_t *)fd_exec_txn_test_run );
sol_compat_execute_wrapper( runner, input, &output, fd_exec_txn_test_run );

// Encode effects
int ok = 0;
Expand Down Expand Up @@ -472,7 +470,7 @@ sol_compat_elf_loader_v1( uchar * out,
void * out0 = fd_scratch_prepare( 1UL );
assert( out_bufsz < fd_scratch_free() );
fd_scratch_publish( (void *)( (ulong)out0 + out_bufsz ) );
ulong out_used = fd_sbpf_program_load_test_run( NULL, input, &output, out0, out_bufsz );
ulong out_used = fd_sbpf_program_load_test_run( NULL, fd_type_pun_const( input ), fd_type_pun( &output ), out0, out_bufsz );
if( FD_UNLIKELY( !out_used ) ) {
output = NULL;
break;
Expand Down Expand Up @@ -524,7 +522,7 @@ sol_compat_vm_syscall_execute_v1( uchar * out,

// Execute
void * output = NULL;
sol_compat_execute_wrapper( runner, input, &output, (exec_test_run_fn_t *)fd_exec_vm_syscall_test_run );
sol_compat_execute_wrapper( runner, input, &output, fd_exec_vm_syscall_test_run );

// Encode effects
int ok = 0;
Expand Down Expand Up @@ -561,7 +559,7 @@ sol_compat_vm_validate_v1( uchar * out,

// Execute
void * output = NULL;
sol_compat_execute_wrapper( runner, input, &output, (exec_test_run_fn_t *)fd_exec_vm_validate_test_run );
sol_compat_execute_wrapper( runner, input, &output, fd_exec_vm_validate_test_run );

// Encode effects
int ok = 0;
Expand Down
13 changes: 8 additions & 5 deletions src/flamenco/runtime/tests/fd_vm_validate_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
#include "../../vm/test_vm_util.h"

ulong
fd_exec_vm_validate_test_run( fd_exec_instr_test_runner_t * runner,
fd_exec_test_full_vm_context_t const *input,
fd_exec_test_validate_vm_effects_t ** output,
void * output_buf,
ulong output_bufsz ) {
fd_exec_vm_validate_test_run( fd_exec_instr_test_runner_t * runner,
void const * input_,
void ** output_,
void * output_buf,
ulong output_bufsz ) {
(void) runner; /* unused, for wrapper compat */
fd_exec_test_full_vm_context_t const * input = fd_type_pun_const( input_ );
fd_exec_test_validate_vm_effects_t ** output = fd_type_pun( output_ );

if( FD_UNLIKELY( !input->has_vm_ctx ) ) {
return 0UL;
}
Expand Down
Loading

0 comments on commit 491e23c

Please sign in to comment.