Skip to content

Commit 0aea95f

Browse files
runtime: gaddr aware types now use offsets
1 parent fc0a7f9 commit 0aea95f

12 files changed

+937
-579
lines changed

src/flamenco/runtime/fd_executor.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,8 +869,7 @@ fd_executor_setup_accessed_accounts_for_txn( fd_exec_txn_ctx_t * txn_ctx ) {
869869
return FD_RUNTIME_TXN_ERR_ACCOUNT_NOT_FOUND;
870870
}
871871

872-
fd_slot_hash_t * slot_hash = deq_fd_slot_hash_t_join( fd_wksp_laddr_fast( txn_ctx->runtime_pub_wksp,
873-
slot_hashes_global->hashes_gaddr ) );
872+
fd_slot_hash_t * slot_hash = deq_fd_slot_hash_t_join( (uchar *)slot_hashes_global + slot_hashes_global->hashes_offset );
874873

875874
fd_acct_addr_t * accts_alt = (fd_acct_addr_t *) fd_type_pun( &txn_ctx->account_keys[txn_ctx->accounts_cnt] );
876875
int err = fd_runtime_load_txn_address_lookup_tables( txn_ctx->txn_descriptor,

src/flamenco/runtime/program/fd_address_lookup_table_program.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,7 @@ create_lookup_table( fd_exec_instr_ctx_t * ctx,
265265
return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
266266
}
267267

268-
fd_slot_hash_t * slot_hash = deq_fd_slot_hash_t_join( fd_wksp_laddr_fast( ctx->txn_ctx->runtime_pub_wksp,
269-
slot_hashes_global->hashes_gaddr ) );
268+
fd_slot_hash_t * slot_hash = deq_fd_slot_hash_t_join( (uchar*)slot_hashes_global + slot_hashes_global->hashes_offset );
270269

271270
/* https://github.com/solana-labs/solana/blob/v1.17.4/programs/address-lookup-table/src/processor.rs#L97 */
272271
ulong is_recent_slot = slot_hashes_position( slot_hash, create->recent_slot )!=ULONG_MAX;
@@ -1005,8 +1004,7 @@ close_lookup_table( fd_exec_instr_ctx_t * ctx ) {
10051004
return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
10061005
}
10071006

1008-
fd_slot_hash_t * slot_hash = deq_fd_slot_hash_t_join( fd_wksp_laddr_fast( ctx->txn_ctx->runtime_pub_wksp,
1009-
slot_hashes_global->hashes_gaddr ) );
1007+
fd_slot_hash_t * slot_hash = deq_fd_slot_hash_t_join( (uchar*)slot_hashes_global + slot_hashes_global->hashes_offset );
10101008

10111009
/* https://github.com/solana-labs/solana/blob/v1.17.4/programs/address-lookup-table/src/processor.rs#L440 */
10121010
ulong remaining_blocks = 0UL;

src/flamenco/runtime/program/fd_system_program_nonce.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ require_acct_recent_blockhashes( fd_exec_instr_ctx_t * ctx,
5656
return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
5757
}
5858

59-
(*out)->hashes = deq_fd_block_block_hash_entry_t_join( fd_wksp_laddr_fast( ctx->txn_ctx->runtime_pub_wksp, rbh_global->hashes_gaddr ) );
59+
(*out)->hashes = deq_fd_block_block_hash_entry_t_join( (uchar*)rbh_global + rbh_global->hashes_offset );
6060

6161
return FD_EXECUTOR_INSTR_SUCCESS;
6262
}

src/flamenco/runtime/program/fd_vote_program.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,7 +2647,7 @@ fd_vote_program_execute( fd_exec_instr_ctx_t * ctx ) {
26472647
fd_slot_hashes_global_t const * slot_hashes_global = fd_sysvar_from_instr_acct_slot_hashes( ctx, 1, &err );
26482648
fd_slot_hashes_t slot_hashes[1];
26492649
if( FD_LIKELY( slot_hashes_global ) ) {
2650-
slot_hashes->hashes = deq_fd_slot_hash_t_join( fd_wksp_laddr_fast( ctx->txn_ctx->runtime_pub_wksp, slot_hashes_global->hashes_gaddr ) );
2650+
slot_hashes->hashes = deq_fd_slot_hash_t_join( (uchar*)slot_hashes_global + slot_hashes_global->hashes_offset );
26512651
} else {
26522652
return err;
26532653
}
@@ -2701,7 +2701,7 @@ fd_vote_program_execute( fd_exec_instr_ctx_t * ctx ) {
27012701
fd_slot_hashes_global_t const * slot_hashes_global = fd_sysvar_cache_slot_hashes( ctx->txn_ctx->sysvar_cache, ctx->txn_ctx->runtime_pub_wksp );
27022702
fd_slot_hashes_t slot_hashes[1];
27032703
if( FD_LIKELY( slot_hashes_global ) ) {
2704-
slot_hashes->hashes = deq_fd_slot_hash_t_join( fd_wksp_laddr_fast( ctx->txn_ctx->runtime_pub_wksp, slot_hashes_global->hashes_gaddr ) );
2704+
slot_hashes->hashes = deq_fd_slot_hash_t_join( (uchar*)slot_hashes_global + slot_hashes_global->hashes_offset );
27052705
} else {
27062706
return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
27072707
}
@@ -2766,7 +2766,7 @@ fd_vote_program_execute( fd_exec_instr_ctx_t * ctx ) {
27662766
fd_slot_hashes_global_t const * slot_hashes_global = fd_sysvar_cache_slot_hashes( ctx->txn_ctx->sysvar_cache, ctx->txn_ctx->runtime_pub_wksp );
27672767
fd_slot_hashes_t slot_hashes[1];
27682768
if( FD_LIKELY( slot_hashes_global ) ) {
2769-
slot_hashes->hashes = deq_fd_slot_hash_t_join( fd_wksp_laddr_fast( ctx->txn_ctx->runtime_pub_wksp, slot_hashes_global->hashes_gaddr ) );
2769+
slot_hashes->hashes = deq_fd_slot_hash_t_join( (uchar*)slot_hashes_global + slot_hashes_global->hashes_offset );
27702770
} else {
27712771
return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR;
27722772
}
@@ -2803,7 +2803,7 @@ fd_vote_program_execute( fd_exec_instr_ctx_t * ctx ) {
28032803
fd_slot_hashes_global_t const * slot_hashes_global = fd_sysvar_cache_slot_hashes( ctx->txn_ctx->sysvar_cache, ctx->txn_ctx->runtime_pub_wksp );
28042804
fd_slot_hashes_t slot_hashes[1];
28052805
if( FD_LIKELY( slot_hashes_global ) ) {
2806-
slot_hashes->hashes = deq_fd_slot_hash_t_join( fd_wksp_laddr_fast( ctx->txn_ctx->runtime_pub_wksp, slot_hashes_global->hashes_gaddr ) );
2806+
slot_hashes->hashes = deq_fd_slot_hash_t_join( (uchar*)slot_hashes_global + slot_hashes_global->hashes_offset );
28072807
}
28082808

28092809
fd_sol_sysvar_clock_t const * clock = fd_sysvar_cache_clock( ctx->txn_ctx->sysvar_cache, ctx->txn_ctx->runtime_pub_wksp );

src/flamenco/runtime/sysvar/fd_sysvar_slot_hashes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fd_sysvar_slot_hashes_update( fd_exec_slot_ctx_t * slot_ctx, fd_spad_t * runtime
4949
FD_LOG_ERR(( "Unable to allocate memory for slot hashes" ));
5050
}
5151
} else {
52-
hashes = deq_fd_slot_hash_t_join( fd_wksp_laddr_fast( slot_ctx->runtime_wksp, slot_hashes_global->hashes_gaddr ) );
52+
hashes = deq_fd_slot_hash_t_join( (uchar*)slot_hashes_global + slot_hashes_global->hashes_offset );
5353
}
5454

5555
uchar found = 0;

src/flamenco/runtime/sysvar/fd_sysvar_slot_history.c

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ static const ulong bits_per_block = 8 * sizeof(ulong);
1616

1717
void
1818
fd_sysvar_slot_history_set( fd_slot_history_global_t * history,
19-
ulong i,
20-
fd_wksp_t * wksp ) {
19+
ulong i ) {
2120
if( FD_UNLIKELY( i > history->next_slot && i - history->next_slot >= slot_history_max_entries ) ) {
2221
FD_LOG_WARNING(( "Ignoring out of bounds (i=%lu next_slot=%lu)", i, history->next_slot ));
2322
return;
2423
}
2524

26-
fd_slot_history_inner_global_t * inner = fd_wksp_laddr_fast( wksp, history->bits.bits_gaddr );
27-
ulong * blocks = fd_wksp_laddr_fast( wksp, inner->blocks_gaddr );
25+
fd_slot_history_inner_global_t * inner = (fd_slot_history_inner_global_t *)((uchar*)&history->bits + history->bits.bits_offset);
26+
ulong * blocks = (ulong *)((uchar*)inner + inner->blocks_offset);
2827
ulong blocks_len = inner->blocks_len;
2928

3029
// Skipped slots, delete them from history
@@ -58,25 +57,28 @@ fd_sysvar_slot_history_write_history( fd_exec_slot_ctx_t * slot_ctx,
5857
void
5958
fd_sysvar_slot_history_init( fd_exec_slot_ctx_t * slot_ctx, fd_spad_t * runtime_spad ) {
6059
/* Create a new slot history instance */
61-
fd_slot_history_global_t history = {0};
6260

63-
fd_slot_history_inner_global_t * inner = fd_spad_alloc( runtime_spad,
64-
fd_slot_history_inner_align(),
65-
fd_slot_history_inner_footprint() );
61+
/* We need to construct the gaddr-aware slot history object */
62+
ulong total_sz = sizeof(fd_slot_history_global_t) + alignof(fd_slot_history_global_t) +
63+
sizeof(fd_slot_history_inner_global_t) + alignof(fd_slot_history_inner_global_t) +
64+
(sizeof(ulong) + alignof(ulong)) * blocks_len;
6665

67-
history.bits.bits_gaddr = fd_wksp_gaddr_fast( slot_ctx->runtime_wksp, inner );
68-
history.bits.len = slot_history_max_entries;
66+
uchar * mem = fd_spad_alloc( runtime_spad, alignof(fd_slot_history_global_t), total_sz );
67+
fd_slot_history_global_t * history = (fd_slot_history_global_t *)mem;
68+
fd_slot_history_inner_global_t * inner = (fd_slot_history_inner_global_t *)fd_ulong_align_up( (ulong)((uchar*)history + sizeof(fd_slot_history_global_t)), alignof(fd_slot_history_inner_global_t) );
69+
ulong * blocks = (ulong *)fd_ulong_align_up( (ulong)((uchar*)inner + sizeof(fd_slot_history_inner_global_t)), alignof(ulong) );
6970

70-
ulong * blocks = fd_spad_alloc( runtime_spad, alignof(ulong), sizeof(ulong) * blocks_len );
71+
history->next_slot = slot_ctx->slot_bank.slot + 1UL;
72+
history->bits.bits_offset = (ulong)((uchar*)inner - (uchar*)history);
73+
history->bits.len = slot_history_max_entries;
74+
75+
inner->blocks_len = blocks_len;
76+
inner->blocks_offset = (ulong)((uchar*)blocks - (uchar*)inner);
7177
memset( blocks, 0, sizeof(ulong) * blocks_len );
72-
inner->blocks_gaddr = fd_wksp_gaddr_fast( slot_ctx->runtime_wksp, blocks );
73-
inner->blocks_len = blocks_len;
7478

7579
/* TODO: handle slot != 0 init case */
76-
fd_sysvar_slot_history_set( &history, slot_ctx->slot_bank.slot, slot_ctx->runtime_wksp );
77-
history.next_slot = slot_ctx->slot_bank.slot + 1;
78-
79-
fd_sysvar_slot_history_write_history( slot_ctx, &history );
80+
fd_sysvar_slot_history_set( history, slot_ctx->slot_bank.slot );
81+
fd_sysvar_slot_history_write_history( slot_ctx, history );
8082
}
8183

8284
/* https://github.com/solana-labs/solana/blob/8f2c8b8388a495d2728909e30460aa40dcc5d733/runtime/src/bank.rs#L2345 */
@@ -111,7 +113,7 @@ fd_sysvar_slot_history_update( fd_exec_slot_ctx_t * slot_ctx, fd_spad_t * runtim
111113
fd_slot_history_global_t * history = fd_slot_history_decode_global( mem, &ctx );
112114

113115
/* https://github.com/solana-labs/solana/blob/8f2c8b8388a495d2728909e30460aa40dcc5d733/sdk/program/src/slot_history.rs#L48 */
114-
fd_sysvar_slot_history_set( history, slot_ctx->slot_bank.slot, slot_ctx->runtime_wksp );
116+
fd_sysvar_slot_history_set( history, slot_ctx->slot_bank.slot );
115117
history->next_slot = slot_ctx->slot_bank.slot + 1;
116118

117119
ulong sz = slot_history_min_account_size;
@@ -181,11 +183,12 @@ int
181183
fd_sysvar_slot_history_find_slot( fd_slot_history_global_t const * history,
182184
ulong slot,
183185
fd_wksp_t * wksp ) {
184-
fd_slot_history_inner_global_t * inner = fd_wksp_laddr_fast( wksp, history->bits.bits_gaddr );
186+
(void)wksp;
187+
fd_slot_history_inner_global_t * inner = (fd_slot_history_inner_global_t *)((uchar*)&history->bits + history->bits.bits_offset);
185188
if( FD_UNLIKELY( !inner ) ) {
186189
FD_LOG_ERR(( "Unable to find slot history inner" ));
187190
}
188-
ulong * blocks = fd_wksp_laddr_fast( wksp, inner->blocks_gaddr );
191+
ulong * blocks = (ulong *)((uchar*)inner + inner->blocks_offset);
189192
if( FD_UNLIKELY( !blocks ) ) {
190193
FD_LOG_ERR(( "Unable to find slot history blocks" ));
191194
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ fd_runtime_fuzz_block_ctx_create( fd_runtime_fuzz_runner_t * runner,
256256
fd_recent_block_hashes_global_t const * rbh_global = fd_sysvar_cache_recent_block_hashes( slot_ctx->sysvar_cache, runner->wksp );
257257
fd_recent_block_hashes_t rbh[1];
258258
if( rbh_global ) {
259-
rbh->hashes = deq_fd_block_block_hash_entry_t_join( fd_wksp_laddr_fast( fd_wksp_containing( runner->spad ), rbh_global->hashes_gaddr ) );
259+
rbh->hashes = deq_fd_block_block_hash_entry_t_join( (uchar*)rbh_global + rbh_global->hashes_offset );
260260
}
261261

262262
if( rbh_global && !deq_fd_block_block_hash_entry_t_empty( rbh->hashes ) ) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ fd_runtime_fuzz_instr_ctx_create( fd_runtime_fuzz_runner_t * runner,
287287
fd_recent_block_hashes_global_t const * rbh_global = fd_sysvar_cache_recent_block_hashes( slot_ctx->sysvar_cache, runner->wksp );
288288
fd_recent_block_hashes_t rbh[1];
289289
if( rbh_global ) {
290-
rbh->hashes = deq_fd_block_block_hash_entry_t_join( fd_wksp_laddr_fast( runtime_wksp, rbh_global->hashes_gaddr ) );
290+
rbh->hashes = deq_fd_block_block_hash_entry_t_join( (uchar*)rbh_global + rbh_global->hashes_offset );
291291
}
292292

293293
if( rbh_global && !deq_fd_block_block_hash_entry_t_empty( rbh->hashes ) ) {

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,21 @@ fd_runtime_fuzz_txn_ctx_create( fd_runtime_fuzz_runner_t * runner,
125125

126126
/* Provide default slot hashes of size 1 if not provided */
127127
if( !slot_ctx->sysvar_cache->has_slot_hashes ) {
128-
uchar * deque_mem = fd_spad_alloc( runner->spad, deq_fd_slot_hash_t_align(), deq_fd_slot_hash_t_footprint( 1 ) );
129-
fd_slot_hash_t * slot_hashes = deq_fd_slot_hash_t_join( deq_fd_slot_hash_t_new( deque_mem, 1 ) );
128+
/* The offseted gaddr aware types need the memory for the entire
129+
struct to be allocated out of a contiguous memory region. */
130+
ulong default_slot_hashes_sz = sizeof(fd_slot_hashes_global_t) +
131+
deq_fd_slot_hash_t_footprint( 1UL ) +
132+
deq_fd_slot_hash_t_align();
133+
uchar * slot_hashes_mem = fd_spad_alloc( runner->spad, alignof(fd_slot_hashes_global_t), default_slot_hashes_sz );
134+
fd_slot_hashes_global_t * default_slot_hashes_global = (fd_slot_hashes_global_t *)slot_hashes_mem;
135+
136+
uchar * slot_hash_mem = (uchar*)fd_ulong_align_up( (ulong)(slot_hashes_mem + sizeof(fd_slot_hashes_global_t)), deq_fd_slot_hash_t_align() );
137+
fd_slot_hash_t * slot_hashes = deq_fd_slot_hash_t_join( deq_fd_slot_hash_t_new( slot_hash_mem, 1 ) );
130138
fd_slot_hash_t * dummy_elem = deq_fd_slot_hash_t_push_tail_nocopy( slot_hashes );
131139
memset( dummy_elem, 0, sizeof(fd_slot_hash_t) );
132-
fd_slot_hashes_global_t default_slot_hashes_global = { .hashes_gaddr = fd_wksp_gaddr_fast( runner->wksp, deq_fd_slot_hash_t_leave( slot_hashes ) ) };
133-
fd_sysvar_slot_hashes_init( slot_ctx, &default_slot_hashes_global );
140+
141+
default_slot_hashes_global->hashes_offset = (ulong)deq_fd_slot_hash_t_leave( slot_hashes ) - (ulong)default_slot_hashes_global;
142+
fd_sysvar_slot_hashes_init( slot_ctx, default_slot_hashes_global );
134143
}
135144

136145
/* Provide default stake history if not provided */
@@ -202,7 +211,7 @@ fd_runtime_fuzz_txn_ctx_create( fd_runtime_fuzz_runner_t * runner,
202211
fd_recent_block_hashes_global_t const * rbh_global = fd_sysvar_cache_recent_block_hashes( slot_ctx->sysvar_cache, runner->wksp );
203212
fd_recent_block_hashes_t rbh[1];
204213
if( rbh_global ) {
205-
rbh->hashes = deq_fd_block_block_hash_entry_t_join( fd_wksp_laddr_fast( fd_wksp_containing( runner->spad ), rbh_global->hashes_gaddr ) );
214+
rbh->hashes = deq_fd_block_block_hash_entry_t_join( (uchar*)rbh_global + rbh_global->hashes_offset );
206215
}
207216

208217
if( rbh_global && !deq_fd_block_block_hash_entry_t_empty( rbh->hashes ) ) {

0 commit comments

Comments
 (0)