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 Aug 2, 2024
1 parent 1459114 commit 81f8b2b
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 121 deletions.
20 changes: 10 additions & 10 deletions contrib/test/run_test_vectors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ else
cd ../..
fi

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_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
# LOG=$LOG_PATH/test_exec_precompiles
# 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
cat contrib/test/elf-loader-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
# cat contrib/test/elf-loader-fixtures.list | xargs ./$OBJDIR/unit-test/test_exec_sol_compat --log-path $LOG

LOG=$LOG_PATH/test_exec_instr
cat contrib/test/instr-fixtures.list | xargs ./$OBJDIR/unit-test/test_exec_instr --log-path $LOG --log-level-stderr 4
cat contrib/test/instr-fixtures.list | xargs ./$OBJDIR/unit-test/test_exec_instr --log-path $LOG

LOG=$LOG_PATH/test_vm_validate
xargs -a contrib/test/vm_validate-fixtures.list ./$OBJDIR/unit-test/test_exec_sol_compat --log-path $LOG --log-level-stderr 4
# LOG=$LOG_PATH/test_vm_validate
# xargs -a contrib/test/vm_validate-fixtures.list ./$OBJDIR/unit-test/test_exec_sol_compat --log-path $LOG

echo Test vectors success
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 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 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 memcpy( _hash, state, 32 );
}

#undef fd_sha256_core
2 changes: 1 addition & 1 deletion src/flamenco/runtime/program/zksdk/fd_zksdk.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ fd_zksdk_process_verify_proof( fd_exec_instr_ctx_t ctx ) {

/* https://github.com/anza-xyz/agave/blob/v2.0.1/programs/zk-elgamal-proof/src/lib.rs#L50-L61
Note: it doesn't look like the ref code can throw any error. */
uint proof_data_offset = *((uint *)(&instr_data[1]));
uint proof_data_offset = fd_uint_load_4_fast(&instr_data[1]);

/* https://github.com/anza-xyz/agave/blob/v2.0.1/programs/zk-elgamal-proof/src/lib.rs#L62-L65 */
if( proof_data_offset+proof_data_sz > proof_data_acc->meta->dlen ) {
Expand Down
60 changes: 33 additions & 27 deletions src/flamenco/runtime/tests/fd_exec_instr_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -912,11 +912,7 @@ _instr_context_destroy( fd_exec_instr_test_runner_t * runner,
// Free any allocated borrowed account data
for( ulong i = 0; i < ctx->txn_ctx->accounts_cnt; ++i ) {
fd_borrowed_account_t * acc = &ctx->txn_ctx->borrowed_accounts[i];
void * borrowed_account_mem = fd_borrowed_account_destroy( acc );
fd_wksp_t * belongs_to_wksp = fd_wksp_containing( borrowed_account_mem );
if( belongs_to_wksp ) {
fd_wksp_free_laddr( borrowed_account_mem );
}
fd_borrowed_account_destroy( acc );
}

// Free instr info pool (since its also wksp-allocated)
Expand Down Expand Up @@ -1265,11 +1261,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 @@ -1375,11 +1373,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 @@ -1494,10 +1495,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 @@ -1606,11 +1610,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 @@ -1756,18 +1762,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
Loading

0 comments on commit 81f8b2b

Please sign in to comment.