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 5, 2024
1 parent 00a4ef0 commit d61fc94
Show file tree
Hide file tree
Showing 24 changed files with 150 additions and 119 deletions.
6 changes: 3 additions & 3 deletions contrib/test/run_test_vectors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ 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
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
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
4 changes: 4 additions & 0 deletions src/ballet/utf8/fd_utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ fd_utf8_verify( char const * str,
ulong sz ) {

uchar const * cur = (uchar const *)str;
if( FD_UNLIKELY( cur==NULL ) ) {
return 1;
}

uchar const * const end = cur+sz;

while( cur<end ) {
Expand Down
4 changes: 2 additions & 2 deletions src/flamenco/runtime/fd_acc_mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ fd_acc_mgr_modify_raw( fd_acc_mgr_t * acc_mgr,

fd_funk_rec_key_t id = fd_acc_funk_key( pubkey );

if (((pubkey->ul[0] == 0) & (pubkey->ul[1] == 0) & (pubkey->ul[2] == 0) & (pubkey->ul[3] == 0)))
FD_LOG_WARNING(( "null pubkey (system program?) is being modified" ));
// if (((pubkey->ul[0] == 0) & (pubkey->ul[1] == 0) & (pubkey->ul[2] == 0) & (pubkey->ul[3] == 0)))
// FD_LOG_WARNING(( "null pubkey (system program?) is being modified" ));

//#ifdef VLOG
// ulong rec_cnt = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,9 @@ fd_address_lookup_table_program_execute( fd_exec_instr_ctx_t _ctx ) {
fd_exec_instr_ctx_t * ctx = &_ctx;
uchar const * instr_data = ctx->instr->data;
ulong instr_data_sz = ctx->instr->data_sz;
if( FD_UNLIKELY( instr_data==NULL ) ) {
return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
}

FD_SCRATCH_SCOPE_BEGIN {

Expand Down
6 changes: 3 additions & 3 deletions src/flamenco/runtime/program/fd_bpf_loader_v3_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fd_bpf_loader_v3_is_executable( fd_exec_slot_ctx_t * slot_ctx,

fd_bpf_upgradeable_loader_state_t loader_state = {0};
if( FD_UNLIKELY( fd_bpf_upgradeable_loader_state_decode( &loader_state, &ctx ) ) ) {
FD_LOG_WARNING(( "fd_bpf_upgradeable_loader_state_decode failed" ));
// FD_LOG_WARNING(( "fd_bpf_upgradeable_loader_state_decode failed" ));
return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
}

Expand Down Expand Up @@ -105,7 +105,7 @@ read_bpf_upgradeable_loader_state_for_program( fd_exec_txn_ctx_t *
};

if( FD_UNLIKELY( fd_bpf_upgradeable_loader_state_decode( result, &ctx ) ) ) {
FD_LOG_WARNING(( "fd_bpf_upgradeable_loader_state_decode failed" ));
// FD_LOG_WARNING(( "fd_bpf_upgradeable_loader_state_decode failed" ));
*opt_err = FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
return NULL;
}
Expand Down Expand Up @@ -1652,7 +1652,7 @@ fd_bpf_loader_v3_program_execute( fd_exec_instr_ctx_t ctx ) {

fd_sbpf_validated_program_t * prog = NULL;
if( FD_UNLIKELY( fd_bpf_load_cache_entry( ctx.slot_ctx, &ctx.instr->program_id_pubkey, &prog ) ) ) {
FD_LOG_WARNING(( "Program cache load for program failed" ));
// FD_LOG_WARNING(( "Program cache load for program failed" ));
return FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA;
}

Expand Down
2 changes: 1 addition & 1 deletion src/flamenco/runtime/program/fd_bpf_program_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fd_bpf_get_executable_program_content_for_upgradeable_loader( fd_exec_slot_ctx_t
}

if( FD_UNLIKELY( programdata_rec->const_meta->dlen < PROGRAMDATA_METADATA_SIZE ) ) {
FD_LOG_WARNING(( "programdata_rec->const_meta->dlen < PROGRAMDATA_METADATA_SIZE (%lu<%lu)", programdata_rec->const_meta->dlen, PROGRAMDATA_METADATA_SIZE ));
// FD_LOG_WARNING(( "programdata_rec->const_meta->dlen < PROGRAMDATA_METADATA_SIZE (%lu<%lu)", programdata_rec->const_meta->dlen, PROGRAMDATA_METADATA_SIZE ));
return -1;
}

Expand Down
3 changes: 3 additions & 0 deletions src/flamenco/runtime/program/fd_config_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ _process_config_instr( fd_exec_instr_ctx_t ctx ) {

/* Deserialize the Config Program instruction data, which consists only of the ConfigKeys
https://github.com/solana-labs/solana/blob/v1.17.17/programs/config/src/config_processor.rs#L21 */
if( FD_UNLIKELY( ctx.instr->data==NULL ) ) {
return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
}

fd_bincode_decode_ctx_t decode =
{ .valloc = fd_scratch_virtual(),
Expand Down
3 changes: 3 additions & 0 deletions src/flamenco/runtime/program/fd_stake_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -2466,6 +2466,9 @@ fd_stake_program_execute( fd_exec_instr_ctx_t ctx ) {
fd_instr_get_signers( ctx.instr, signers );

/* https://github.com/solana-labs/solana/blob/v1.18.9/programs/stake/src/stake_instruction.rs#L72 */
if( FD_UNLIKELY( ctx.instr->data==NULL ) ) {
return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
}

fd_valloc_t valloc = fd_scratch_virtual();
fd_bincode_decode_ctx_t decode =
Expand Down
3 changes: 3 additions & 0 deletions src/flamenco/runtime/program/fd_system_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,9 @@ fd_system_program_execute( fd_exec_instr_ctx_t ctx ) {

/* Deserialize the SystemInstruction enum */
uchar * data = ctx.instr->data;
if( FD_UNLIKELY( data==NULL ) ) {
return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
}

fd_system_program_instruction_t instruction;
fd_bincode_decode_ctx_t decode =
Expand Down
3 changes: 3 additions & 0 deletions src/flamenco/runtime/program/fd_vote_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -2369,6 +2369,9 @@ fd_vote_program_execute( fd_exec_instr_ctx_t ctx ) {
fd_instr_get_signers( ctx.instr, signers );

// https://github.com/anza-xyz/agave/blob/v2.0.1/programs/vote/src/vote_processor.rs#L70
if( FD_UNLIKELY( ctx.instr->data==NULL ) ) {
return FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA;
}
fd_vote_instruction_t instruction;
fd_bincode_decode_ctx_t decode = {
.data = ctx.instr->data,
Expand Down
1 change: 0 additions & 1 deletion src/flamenco/runtime/program/fd_zk_elgamal_proof_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
} while(0);

#define FD_RUNTIME_LOG_APPEND( ctx, log ) do { \
FD_LOG_NOTICE(( log )); \
} while(0);

/* FD_ZKSDK_INSTR_{...} identify ZK ElGamal Proof Program instructions.
Expand Down
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
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 @@ -1265,11 +1265,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 +1377,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 +1499,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 +1614,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 +1766,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
Loading

0 comments on commit d61fc94

Please sign in to comment.