Skip to content

Commit 1efd3ad

Browse files
committed
utils,spad: add spad handholding
1 parent b9cd2de commit 1efd3ad

File tree

14 files changed

+197
-13
lines changed

14 files changed

+197
-13
lines changed

src/discof/rpcserver/fd_methods.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ void json_add_value(struct json_values* values, struct json_path* path, const vo
192192
values->buf_alloc <<= 1;
193193
} while (new_buf_sz > values->buf_alloc);
194194
char* newbuf = (char*)fd_spad_alloc( spad, 1, values->buf_alloc);
195+
if ( FD_UNLIKELY( NULL == newbuf ) ) {
196+
FD_LOG_ERR(( "out of spad scratch space" ));
197+
}
195198
fd_memcpy(newbuf, values->buf, values->buf_sz);
196199
values->buf = newbuf;
197200
}

src/discof/rpcserver/fd_rpc_history.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ fd_rpc_history_t *
9292
fd_rpc_history_create(fd_rpcserver_args_t * args) {
9393
fd_spad_t * spad = args->spad;
9494
fd_rpc_history_t * hist = (fd_rpc_history_t *)fd_spad_alloc( spad, alignof(fd_rpc_history_t), sizeof(fd_rpc_history_t) );
95+
if ( FD_UNLIKELY ( NULL == hist ) ) {
96+
FD_LOG_ERR(( "out of spad scratch space" ));
97+
}
9598
memset(hist, 0, sizeof(fd_rpc_history_t));
9699
hist->spad = spad;
97100

src/discof/rpcserver/fd_rpc_service.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,6 +2294,9 @@ void
22942294
fd_rpc_create_ctx(fd_rpcserver_args_t * args, fd_rpc_ctx_t ** ctx_p) {
22952295
fd_rpc_ctx_t * ctx = (fd_rpc_ctx_t *)fd_spad_alloc( args->spad, alignof(fd_rpc_ctx_t), sizeof(fd_rpc_ctx_t) );
22962296
fd_rpc_global_ctx_t * gctx = (fd_rpc_global_ctx_t *)fd_spad_alloc( args->spad, alignof(fd_rpc_global_ctx_t), sizeof(fd_rpc_global_ctx_t) );
2297+
if ( FD_UNLIKELY ( (NULL == ctx) || (NULL == gctx) ) ) {
2298+
FD_LOG_ERR(( "fd_spad_alloc failed" ));
2299+
}
22972300
fd_memset(ctx, 0, sizeof(fd_rpc_ctx_t));
22982301
fd_memset(gctx, 0, sizeof(fd_rpc_global_ctx_t));
22992302

src/discof/rpcserver/json_lex.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,10 @@ static char* json_lex_append_prepare(json_lex_state_t* lex, ulong sz) {
387387
} while (new_sz + 1 > lex->last_str_alloc);
388388
char* oldstr = lex->last_str;
389389
lex->last_str = (char*)fd_spad_alloc( lex->spad, 1, lex->last_str_alloc);
390+
if ( FD_UNLIKELY ( NULL == lex->last_str ) ) {
391+
// TODO: what should we do?
392+
return NULL;
393+
}
390394
// Copy the old content to the new space
391395
fd_memcpy(lex->last_str, oldstr, lex->last_str_sz);
392396
}

src/flamenco/runtime/fd_executor.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,9 @@ fd_executor_create_rollback_fee_payer_account( fd_exec_txn_ctx_t * txn_ctx,
919919

920920
ulong data_len = fd_txn_account_get_data_len( &txn_ctx->accounts[FD_FEE_PAYER_TXN_IDX] );
921921
void * fee_payer_data = fd_spad_alloc( txn_ctx->spad, FD_ACCOUNT_REC_ALIGN, sizeof(fd_account_meta_t) + data_len );
922+
if( FD_UNLIKELY( NULL == fee_payer_data ) ) {
923+
FD_LOG_CRIT(( "out of spad scratch space" ));
924+
}
922925
fd_memcpy( fee_payer_data, (uchar *)meta, sizeof(fd_account_meta_t) + data_len );
923926
if( FD_UNLIKELY( !fd_txn_account_join( fd_txn_account_new(
924927
txn_ctx->rollback_fee_payer_account,
@@ -1408,6 +1411,9 @@ fd_executor_setup_txn_account( fd_exec_txn_ctx_t * txn_ctx,
14081411
initialize a new metadata. */
14091412

14101413
uchar * new_raw_data = fd_spad_alloc( txn_ctx->spad, FD_ACCOUNT_REC_ALIGN, FD_ACC_TOT_SZ_MAX );
1414+
if( FD_UNLIKELY( NULL == new_raw_data ) ) {
1415+
FD_LOG_CRIT(( "out of spad scratch space" ));
1416+
}
14111417
ulong dlen = !!meta ? meta->dlen : 0UL;
14121418

14131419
if( FD_LIKELY( meta ) ) {
@@ -1432,6 +1438,9 @@ fd_executor_setup_txn_account( fd_exec_txn_ctx_t * txn_ctx,
14321438
data_wksp = fd_funk_wksp( txn_ctx->funk );
14331439
} else {
14341440
uchar * mem = fd_spad_alloc( txn_ctx->spad, FD_TXN_ACCOUNT_ALIGN, sizeof(fd_account_meta_t) );
1441+
if( FD_UNLIKELY( NULL == mem ) ) {
1442+
FD_LOG_CRIT(( "out of spad scratch space" ));
1443+
}
14351444
account_meta = (fd_account_meta_t *)mem;
14361445
data_wksp = txn_ctx->spad_wksp;
14371446
fd_account_meta_init( account_meta );

src/flamenco/runtime/fd_runtime.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2591,6 +2591,9 @@ fd_runtime_read_genesis( fd_exec_slot_ctx_t * slot_ctx,
25912591
longer spad-backed. */
25922592

25932593
uchar * buf = fd_spad_alloc( runtime_spad, alignof(ulong), (ulong)sbuf.st_size );
2594+
if( FD_UNLIKELY( NULL == buf ) ) {
2595+
FD_LOG_ERR(( "fd_spad_alloc failed" ));
2596+
}
25942597
ulong sz = 0UL;
25952598
int res = fd_io_read( fd, buf, (ulong)sbuf.st_size, (ulong)sbuf.st_size, &sz );
25962599
FD_TEST( res==0 );

src/flamenco/runtime/fd_txncache.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,9 @@ fd_txncache_get_entries( fd_txncache_t * tc,
10641064

10651065
status_pair->value.statuses_len = num_statuses;
10661066
status_pair->value.statuses = fd_spad_alloc( spad, FD_CACHE_STATUS_ALIGN, num_statuses * sizeof(fd_cache_status_t) );
1067+
if( FD_UNLIKELY( NULL == status_pair->value.statuses )) {
1068+
FD_LOG_CRIT(( "out of spad scratch space" ));
1069+
}
10671070
fd_memset( status_pair->value.statuses, 0, num_statuses * sizeof(fd_cache_status_t) );
10681071

10691072
/* Copy over every entry for the given slot into the slot deltas. */

src/flamenco/runtime/program/fd_system_program.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
static inline char *
2121
fd_log_address_type( fd_spad_t * spad, fd_pubkey_t const * pubkey, fd_pubkey_t const * base ) {
2222
char * out = fd_spad_alloc( spad, alignof(char), 125UL );
23+
if ( FD_UNLIKELY ( NULL == out ) ) {
24+
FD_LOG_ERR(( "out of spad scratch spaced during logging" ));
25+
}
2326
char base_addr[52];
2427
if( FD_UNLIKELY( base ) ) {
2528
snprintf( base_addr, 52UL, "Some(%s)", FD_BASE58_ENC_32_ALLOCA( base ) );

src/flamenco/runtime/tests/fd_dump_pb.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ dump_vote_accounts( fd_exec_slot_ctx_t const * slot_ctx,
201201
fd_exec_test_vote_account_t * vote_account_out = fd_spad_alloc( spad,
202202
alignof(fd_exec_test_vote_account_t),
203203
vote_account_t_cnt * sizeof(fd_exec_test_vote_account_t) );
204+
if( FD_UNLIKELY( NULL == vote_account_out ) ) {
205+
FD_LOG_CRIT(( "out of spad scratch space" ));
206+
}
204207

205208
for( fd_vote_accounts_pair_global_t_mapnode_t const * curr = fd_vote_accounts_pair_global_t_map_minimum_const(
206209
vote_accounts_pool,
@@ -270,6 +273,9 @@ dump_sanitized_transaction( fd_funk_t * funk,
270273
fd_acct_addr_t const * account_keys = fd_txn_get_acct_addrs( txn_descriptor, txn_payload );
271274
for( ulong i = 0; i < txn_descriptor->acct_addr_cnt; i++ ) {
272275
pb_bytes_array_t * account_key = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE(sizeof(fd_pubkey_t)) );
276+
if( FD_UNLIKELY( NULL == account_key ) ) {
277+
FD_LOG_ERR(( "fd_spad_alloc failed" ));
278+
}
273279
account_key->size = sizeof(fd_pubkey_t);
274280
memcpy( account_key->bytes, &account_keys[i], sizeof(fd_pubkey_t) );
275281
message->account_keys[i] = account_key;
@@ -278,6 +284,10 @@ dump_sanitized_transaction( fd_funk_t * funk,
278284
/* Transaction Context -> tx -> message -> recent_blockhash */
279285
uchar const * recent_blockhash = fd_txn_get_recent_blockhash( txn_descriptor, txn_payload );
280286
message->recent_blockhash = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE(sizeof(fd_hash_t)) );
287+
if( FD_UNLIKELY( NULL == message->recent_blockhash ) ) {
288+
FD_LOG_ERR(( "spad_alloc_alloc failed" ));
289+
}
290+
281291
message->recent_blockhash->size = sizeof(fd_hash_t);
282292
memcpy( message->recent_blockhash->bytes, recent_blockhash, sizeof(fd_hash_t) );
283293

@@ -354,6 +364,9 @@ dump_sanitized_transaction( fd_funk_t * funk,
354364
fd_ed25519_sig_t const * signatures = fd_txn_get_signatures( txn_descriptor, txn_payload );
355365
for( uchar i = 0; i < txn_descriptor->signature_cnt; ++i ) {
356366
pb_bytes_array_t * signature = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE(sizeof(fd_ed25519_sig_t)) );
367+
if( FD_UNLIKELY( NULL == signature ) ) {
368+
FD_LOG_ERR(( "fd_spad_alloc failed" ));
369+
}
357370
signature->size = sizeof(fd_ed25519_sig_t);
358371
memcpy( signature->bytes, &signatures[i], sizeof(fd_ed25519_sig_t) );
359372
sanitized_transaction->signatures[i] = signature;
@@ -375,6 +388,9 @@ dump_blockhash_queue( fd_blockhashes_t const * queue,
375388
iter=fd_blockhash_deq_iter_prev( queue->d.deque, iter ) ) {
376389
fd_blockhash_info_t const * ele = fd_blockhash_deq_iter_ele_const( queue->d.deque, iter );
377390
pb_bytes_array_t * output_blockhash = fd_spad_alloc( spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE(sizeof(fd_hash_t)) );
391+
if( FD_UNLIKELY( NULL == output_blockhash ) ) {
392+
FD_LOG_ERR(( "fd_spad_alloc failed" ));
393+
}
378394
output_blockhash->size = sizeof(fd_hash_t);
379395
fd_memcpy( output_blockhash->bytes, &ele->hash, sizeof(fd_hash_t) );
380396
output_blockhash_queue[ cnt ] = output_blockhash;
@@ -577,6 +593,9 @@ create_block_context_protobuf_from_block_tx_only( fd_exec_test_block_context_t *
577593
/* BlockContext -> txns */
578594
block_context->txns_count = 0U;
579595
block_context->txns = fd_spad_alloc( spad, alignof(fd_exec_test_sanitized_transaction_t), block_info->txn_cnt * sizeof(fd_exec_test_sanitized_transaction_t) );
596+
if( FD_UNLIKELY( NULL == block_context->txns ) ) {
597+
FD_LOG_ERR(( "fd_spad_alloc failed" ));
598+
}
580599
fd_memset( block_context->txns, 0, block_info->txn_cnt * sizeof(fd_exec_test_sanitized_transaction_t) );
581600

582601
/* BlockContext -> acct_states
@@ -1041,6 +1060,10 @@ fd_dump_block_to_protobuf_tx_only( fd_runtime_block_info_t const * block_info,
10411060
/* Output to file */
10421061
ulong out_buf_size = 5UL<<30UL; /* 5 GB */
10431062
uint8_t * out = fd_spad_alloc( spad, alignof(uint8_t), out_buf_size );
1063+
if( FD_UNLIKELY( NULL == out ) ) {
1064+
FD_LOG_CRIT(( "out of spad scratch space" ));
1065+
}
1066+
10441067
pb_ostream_t stream = pb_ostream_from_buffer( out, out_buf_size );
10451068
if( pb_encode( &stream, FD_EXEC_TEST_BLOCK_CONTEXT_FIELDS, block_context_msg ) ) {
10461069
char output_filepath[256]; fd_memset( output_filepath, 0, sizeof(output_filepath) );
@@ -1092,6 +1115,9 @@ FD_SPAD_FRAME_BEGIN( vm->instr_ctx->txn_ctx->spad ) {
10921115

10931116
/* SyscallContext -> vm_ctx -> rodata */
10941117
sys_ctx.vm_ctx.rodata = fd_spad_alloc( vm->instr_ctx->txn_ctx->spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( vm->rodata_sz ) );
1118+
if( FD_UNLIKELY( NULL == sys_ctx.vm_ctx.rodata )) {
1119+
FD_LOG_ERR(( "fd_spad_alloc failed " ));
1120+
}
10951121
sys_ctx.vm_ctx.rodata->size = (pb_size_t) vm->rodata_sz;
10961122
fd_memcpy( sys_ctx.vm_ctx.rodata->bytes, vm->rodata, vm->rodata_sz );
10971123

@@ -1123,11 +1149,17 @@ FD_SPAD_FRAME_BEGIN( vm->instr_ctx->txn_ctx->spad ) {
11231149

11241150
/* SyscallContext -> vm_ctx -> return_data -> data */
11251151
sys_ctx.vm_ctx.return_data.data = fd_spad_alloc( vm->instr_ctx->txn_ctx->spad, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( vm->instr_ctx->txn_ctx->return_data.len ) );
1152+
if( FD_UNLIKELY( NULL == sys_ctx.vm_ctx.return_data.data )) {
1153+
FD_LOG_ERR(( "fd_spad_alloc failed " ));
1154+
}
11261155
sys_ctx.vm_ctx.return_data.data->size = (pb_size_t)vm->instr_ctx->txn_ctx->return_data.len;
11271156
fd_memcpy( sys_ctx.vm_ctx.return_data.data->bytes, vm->instr_ctx->txn_ctx->return_data.data, vm->instr_ctx->txn_ctx->return_data.len );
11281157

11291158
/* SyscallContext -> vm_ctx -> return_data -> program_id */
11301159
sys_ctx.vm_ctx.return_data.program_id = fd_spad_alloc( vm->instr_ctx->txn_ctx->spad, alignof(pb_bytes_array_t), sizeof(fd_pubkey_t) );
1160+
if( FD_UNLIKELY( NULL == sys_ctx.vm_ctx.return_data.program_id )) {
1161+
FD_LOG_ERR(( "fd_spad_alloc failed " ));
1162+
}
11311163
sys_ctx.vm_ctx.return_data.program_id->size = sizeof(fd_pubkey_t);
11321164
fd_memcpy( sys_ctx.vm_ctx.return_data.program_id->bytes, vm->instr_ctx->txn_ctx->return_data.program_id.key, sizeof(fd_pubkey_t) );
11331165

@@ -1227,6 +1259,9 @@ FD_SPAD_FRAME_BEGIN( txn_ctx->spad ) {
12271259
/* Output to file */
12281260
ulong out_buf_size = 1UL<<29UL; /* 128 MB */
12291261
uint8_t * out = fd_spad_alloc( txn_ctx->spad, alignof(uint8_t), out_buf_size );
1262+
if( FD_UNLIKELY( NULL == out ) ) {
1263+
FD_LOG_CRIT(( "out of spad scratch space" ));
1264+
}
12301265
pb_ostream_t stream = pb_ostream_from_buffer( out, out_buf_size );
12311266
if( pb_encode( &stream, FD_EXEC_TEST_ELF_LOADER_CTX_FIELDS, &elf_ctx ) ) {
12321267
FILE * file = fopen(filename, "wb");

src/flamenco/runtime/tests/harness/fd_block_harness.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ fd_runtime_fuzz_block_register_vote_account( fd_exec_slot_ctx_t *
8383
node_to_insert->elem.value.data_len = account_dlen;
8484

8585
uchar * data = fd_spad_alloc( spad, alignof(uchar), account_dlen );
86+
if( FD_UNLIKELY( NULL == data ) ) {
87+
FD_LOG_ERR(( "fd_spad_alloc failed" ));
88+
}
8689
memcpy( data, fd_txn_account_get_data( acc ), account_dlen );
8790
fd_solana_account_data_update( &node_to_insert->elem.value, data );
8891

@@ -172,6 +175,9 @@ fd_runtime_fuzz_block_update_prev_epoch_votes_cache( fd_vote_accounts_pair_globa
172175
fd_memcpy( &vote_node->elem.value.owner, vote_account->owner, sizeof(fd_pubkey_t) );
173176

174177
uchar * data = fd_spad_alloc( spad, alignof(uchar), vote_account->data->size );
178+
if( FD_UNLIKELY( NULL == data ) ) {
179+
FD_LOG_ERR(( "fd_spad_alloc failed" ));
180+
}
175181
memcpy( data, vote_account->data->bytes, vote_account->data->size );
176182
fd_solana_account_data_update( &vote_node->elem.value, data );
177183

@@ -428,6 +434,9 @@ fd_runtime_fuzz_block_ctx_create( fd_runtime_fuzz_runner_t * runner,
428434
fd_runtime_block_info_t * block_info = fd_spad_alloc( runner->spad, alignof(fd_runtime_block_info_t), sizeof(fd_runtime_block_info_t) );
429435
fd_microblock_batch_info_t * batch_info = fd_spad_alloc( runner->spad, alignof(fd_microblock_batch_info_t), sizeof(fd_microblock_batch_info_t) );
430436
fd_microblock_info_t * microblock_info = fd_spad_alloc( runner->spad, alignof(fd_microblock_info_t), sizeof(fd_microblock_info_t) );
437+
if( FD_UNLIKELY( ( NULL == block_info) | ( NULL == batch_info) | ( NULL == microblock_info ) ) ) {
438+
FD_LOG_ERR(( "fd_spad_alloc failed" ));
439+
}
431440
fd_memset( block_info, 0, sizeof(fd_runtime_block_info_t) );
432441
fd_memset( batch_info, 0, sizeof(fd_microblock_batch_info_t) );
433442
fd_memset( microblock_info, 0, sizeof(fd_microblock_info_t) );

0 commit comments

Comments
 (0)