Skip to content

Commit 1f73a2a

Browse files
committed
flamenco: clean up instr info + trace
1 parent fe45af7 commit 1f73a2a

25 files changed

+145
-184
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3040262e57e97af58ade353ac3f32c9f7f181d8b
1+
155ea679befde81a71977eaf8ebf20a23d6d1fc3

src/discof/exec/fd_exec_tile.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ returnable_frag( fd_exec_tile_ctx_t * ctx,
128128
fd_exec_txn_exec_msg_t * msg = fd_chunk_to_laddr( ctx->replay_in->mem, chunk );
129129
ctx->bank = fd_banks_bank_query( ctx->banks, msg->bank_idx );
130130
FD_TEST( ctx->bank );
131-
ctx->txn_in.txn = &msg->txn;
132-
ctx->txn_in.exec_accounts = &ctx->exec_accounts;
131+
ctx->txn_in.txn = &msg->txn;
132+
ctx->txn_in.exec_accounts = &ctx->exec_accounts;
133+
ctx->runtime->log.capture_ctx = ctx->capture_ctx;
133134

134135
fd_runtime_prepare_and_execute_txn( ctx->runtime, ctx->bank, &ctx->txn_in, &ctx->txn_out );
135136

src/flamenco/runtime/fd_executor.c

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -405,22 +405,6 @@ fd_executor_verify_transaction( fd_bank_t * bank,
405405
return FD_RUNTIME_EXECUTE_SUCCESS;
406406
}
407407

408-
static void
409-
fd_executor_setup_instr_infos_from_txn_instrs( fd_runtime_t * runtime,
410-
fd_bank_t * bank,
411-
fd_txn_in_t const * txn_in,
412-
fd_txn_out_t * txn_out ) {
413-
ushort instr_cnt = TXN( txn_in->txn )->instr_cnt;
414-
415-
/* Set up the instr infos for the transaction */
416-
for( ushort i=0; i<instr_cnt; i++ ) {
417-
fd_txn_instr_t const * instr = &TXN( txn_in->txn )->instr[i];
418-
fd_instr_info_init_from_txn_instr( &runtime->instr.infos[i], bank, txn_in, txn_out, instr );
419-
}
420-
421-
runtime->instr.info_cnt = instr_cnt;
422-
}
423-
424408
/* https://github.com/anza-xyz/agave/blob/v2.0.9/svm/src/account_loader.rs#L410-427 */
425409
static int
426410
accumulate_and_check_loaded_account_data_size( ulong acc_size,
@@ -456,8 +440,7 @@ accumulate_and_check_loaded_account_data_size( ulong acc_size,
456440
457441
https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L199-L228 */
458442
static ulong
459-
load_transaction_account( fd_runtime_t * runtime,
460-
fd_bank_t * bank,
443+
load_transaction_account( fd_bank_t * bank,
461444
fd_txn_in_t const * txn_in,
462445
fd_txn_out_t * txn_out,
463446
fd_txn_account_t * acct,
@@ -472,7 +455,7 @@ load_transaction_account( fd_runtime_t * runtime,
472455
constructed by the SVM and modified within each transaction's
473456
instruction execution only, so it incurs a loaded size cost
474457
of 0. */
475-
fd_sysvar_instructions_serialize_account( txn_in, txn_out, (fd_instr_info_t const *)runtime->instr.infos, TXN( txn_in->txn )->instr_cnt, txn_idx );
458+
fd_sysvar_instructions_serialize_account( bank, txn_in, txn_out, txn_idx );
476459
return 0UL;
477460
}
478461

@@ -549,7 +532,7 @@ fd_executor_load_transaction_accounts_old( fd_runtime_t * runtime,
549532
}
550533

551534
/* https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L733-L740 */
552-
ulong loaded_acc_size = load_transaction_account( runtime, bank, txn_in, txn_out, acct, is_writable, unknown_acc, i );
535+
ulong loaded_acc_size = load_transaction_account( bank, txn_in, txn_out, acct, is_writable, unknown_acc, i );
553536
int err = accumulate_and_check_loaded_account_data_size( loaded_acc_size,
554537
requested_loaded_accounts_data_size,
555538
&txn_out->details.loaded_accounts_data_size );
@@ -816,7 +799,7 @@ fd_executor_load_transaction_accounts_simd_186( fd_runtime_t * runtime,
816799

817800
/* Load and collect any remaining accounts
818801
https://github.com/anza-xyz/agave/blob/v2.3.1/svm/src/account_loader.rs#L652-L659 */
819-
ulong loaded_acc_size = load_transaction_account( runtime, bank, txn_in, txn_out, acct, is_writable, unknown_acc, i );
802+
ulong loaded_acc_size = load_transaction_account( bank, txn_in, txn_out, acct, is_writable, unknown_acc, i );
820803
int err = fd_collect_loaded_account(
821804
runtime,
822805
txn_out,
@@ -1160,11 +1143,13 @@ fd_txn_ctx_push( fd_runtime_t * runtime,
11601143
}
11611144
}
11621145

1163-
/* https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/sdk/src/transaction_context.rs#L347-L351 */
1164-
if( FD_UNLIKELY( runtime->instr.trace_length>=FD_MAX_INSTRUCTION_TRACE_LENGTH ) ) {
1146+
/* Note that we don't update the trace length here - since the caller
1147+
allocates out of the trace array, they are also responsible for
1148+
incrementing the trace length variable.
1149+
https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/sdk/src/transaction_context.rs#L347-L351 */
1150+
if( FD_UNLIKELY( runtime->instr.trace_length>FD_MAX_INSTRUCTION_TRACE_LENGTH ) ) {
11651151
return FD_EXECUTOR_INSTR_ERR_MAX_INSN_TRACE_LENS_EXCEEDED;
11661152
}
1167-
runtime->instr.trace_length++;
11681153

11691154
/* https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/sdk/src/transaction_context.rs#L352-L356 */
11701155
if( FD_UNLIKELY( runtime->instr.stack_sz>=FD_MAX_INSTRUCTION_STACK_DEPTH ) ) {
@@ -1342,11 +1327,6 @@ fd_execute_instr( fd_runtime_t * runtime,
13421327
};
13431328
fd_base58_encode_32( txn_out->accounts.accounts[ instr->program_id ].pubkey->uc, NULL, ctx->program_id_base58 );
13441329

1345-
runtime->instr.trace[ runtime->instr.trace_length - 1 ] = (fd_exec_instr_trace_entry_t) {
1346-
.instr_info = instr,
1347-
.stack_height = runtime->instr.stack_sz,
1348-
};
1349-
13501330
/* Look up the native program. We check for precompiles within the lookup function as well.
13511331
https://github.com/anza-xyz/agave/blob/v2.1.6/svm/src/message_processor.rs#L88 */
13521332
fd_exec_instr_fn_t native_prog_fn;
@@ -1588,9 +1568,6 @@ fd_executor_setup_accounts_for_txn( fd_runtime_t * runtime,
15881568

15891569
txn_out->accounts.nonce_idx_in_txn = ULONG_MAX;
15901570
runtime->executable.cnt = executable_idx;
1591-
1592-
/* Set up instr infos from the txn descriptor. No Agave equivalent to this function. */
1593-
fd_executor_setup_instr_infos_from_txn_instrs( runtime, bank, txn_in, txn_out );
15941571
}
15951572

15961573
int
@@ -1620,19 +1597,34 @@ fd_execute_txn( fd_runtime_t * runtime,
16201597
bool dump_insn = runtime->log.capture_ctx && fd_bank_slot_get( bank ) >= runtime->log.capture_ctx->dump_proto_start_slot && runtime->log.capture_ctx->dump_instr_to_pb;
16211598
(void)dump_insn;
16221599

1600+
fd_txn_t const * txn = TXN( txn_in->txn );
1601+
16231602
/* Initialize log collection. */
16241603
fd_log_collector_init( runtime->log.log_collector, runtime->log.enable_log_collector );
16251604

16261605
for( ushort i=0; i<TXN( txn_in->txn )->instr_cnt; i++ ) {
1627-
runtime->instr.current_idx = i;
1606+
/* Set up the instr info for the current instruction */
1607+
fd_instr_info_t * instr_info = &runtime->instr.trace[runtime->instr.trace_length++];
1608+
fd_instr_info_init_from_txn_instr(
1609+
instr_info,
1610+
bank,
1611+
txn_in,
1612+
txn_out,
1613+
&txn->instr[i]
1614+
);
1615+
16281616
# if FD_HAS_FLATCC
16291617
if( FD_UNLIKELY( dump_insn ) ) {
16301618
// Capture the input and convert it into a Protobuf message
1631-
fd_dump_instr_to_protobuf( runtime, bank, txn_in, txn_out, &runtime->instr.infos[i], i );
1619+
fd_dump_instr_to_protobuf( runtime, bank, txn_in, txn_out, instr_info, i );
16321620
}
16331621
# endif
16341622

1635-
int instr_exec_result = fd_execute_instr( runtime, bank, txn_in, txn_out, &runtime->instr.infos[i] );
1623+
/* Update the current executing instruction index */
1624+
runtime->instr.current_idx = i;
1625+
1626+
/* Execute the current instruction */
1627+
int instr_exec_result = fd_execute_instr( runtime, bank, txn_in, txn_out, instr_info );
16361628
if( FD_UNLIKELY( instr_exec_result!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
16371629
if( txn_out->err.exec_err_idx==INT_MAX ) {
16381630
txn_out->err.exec_err_idx = i;

src/flamenco/runtime/fd_runtime.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,6 @@ fd_runtime_prepare_and_execute_txn( fd_runtime_t * runtime,
13411341

13421342
runtime->executable.cnt = 0UL;
13431343
runtime->log.enable_log_collector = 0;
1344-
runtime->instr.info_cnt = 0UL;
13451344
runtime->instr.trace_length = 0UL;
13461345
runtime->instr.current_idx = 0;
13471346
runtime->instr.stack_sz = 0;

src/flamenco/runtime/fd_runtime.h

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,19 @@ struct fd_runtime {
1313
uchar stack_sz; /* Current depth of the instruction execution stack. */
1414
fd_exec_instr_ctx_t stack[ FD_MAX_INSTRUCTION_STACK_DEPTH ]; /* Instruction execution stack. */
1515
/* The memory for all of the instructions in the transaction
16-
(including CPI instructions) are preallocated. However, the order
17-
in which the instructions are executed does not match the order in
18-
which they are allocated. The instr_trace will instead be used to
19-
track the order in which the instructions are executed. We leave
20-
space for an extra instruction to account for the case where the
21-
transaction has too many instructions leading to
22-
FD_EXECUTOR_INSTR_ERR_MAX_INSN_TRACE_LENS_EXCEEDED.
23-
TODO: In reality, we should just be allocating instr_infos per
24-
instruction and not up front. The dependency on using instr_info
25-
for the sysvar instruction setup is not needed and should be
26-
removed. At this point, instr_info and instr_trace should be
27-
combined. */
28-
fd_instr_info_t infos[ FD_MAX_INSTRUCTION_TRACE_LENGTH * 2UL ];
29-
ulong info_cnt;
30-
fd_exec_instr_trace_entry_t trace[ FD_MAX_INSTRUCTION_TRACE_LENGTH ]; /* Instruction trace */
31-
ulong trace_length; /* Number of instructions in the trace */
16+
(including CPI instructions) are preallocated. However, the
17+
order in which the instructions are executed does not match the
18+
order in which they are allocated. The instr_trace will instead
19+
be used to track the order in which the instructions are
20+
executed. We add a +1 to allow any instructions past the max
21+
instr trace limit to be safely allocated, so that we can fail
22+
out like Agave does later at the stack push step within
23+
fd_execute_instr.
24+
25+
The caller is responsible for updating the trace_length for the
26+
callee. */
27+
fd_instr_info_t trace[ FD_MAX_INSTRUCTION_TRACE_LENGTH+1UL ];
28+
ulong trace_length;
3229
/* The current instruction index being executed */
3330
int current_idx;
3431
} instr;

src/flamenco/runtime/fd_runtime_helpers.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@ typedef struct fd_txn_return_data fd_txn_return_data_t;
3232

3333
/* fd_exec_txn_ctx_t is the context needed to execute a transaction. */
3434

35-
/* An entry in the instruction trace */
36-
struct fd_exec_instr_trace_entry {
37-
/* Metadata about the instruction */
38-
fd_instr_info_t * instr_info;
39-
/* Stack height when this instruction was pushed onto the stack (including itself)
40-
https://github.com/anza-xyz/agave/blob/d87e23d8d91c32d5f2be2bb3557c730bee1e9434/sdk/src/transaction_context.rs#L475-L480 */
41-
ulong stack_height;
42-
};
43-
typedef struct fd_exec_instr_trace_entry fd_exec_instr_trace_entry_t;
44-
4535
FD_PROTOTYPES_BEGIN
4636

4737
/* Returns 0 on success, and non zero otherwise. On failure, the out

src/flamenco/runtime/info/fd_instr_info.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ fd_instr_info_init_from_txn_instr( fd_instr_info_t * instr,
2828
fd_txn_t const * txn_descriptor = TXN( txn_in->txn );
2929
uchar * instr_acc_idxs = (uchar *)txn_in->txn->payload + txn_instr->acct_off;
3030

31+
/* Set the stack height to 1 (since this is a top-level instruction) */
32+
instr->stack_height = 1;
33+
34+
/* Set the program id */
3135
instr->program_id = txn_instr->program_id;
3236

3337
/* See note in fd_instr_info.h. TLDR: capping this value at 256
@@ -38,7 +42,7 @@ fd_instr_info_init_from_txn_instr( fd_instr_info_t * instr,
3842
the instr info. */
3943
instr->acct_cnt = fd_ushort_min( txn_instr->acct_cnt, FD_INSTR_ACCT_MAX );
4044
instr->data_sz = txn_instr->data_sz;
41-
instr->data = (uchar *)txn_in->txn->payload + txn_instr->data_off;
45+
memcpy( instr->data, txn_in->txn->payload+txn_instr->data_off, instr->data_sz );
4246

4347
uchar acc_idx_seen[ FD_INSTR_ACCT_MAX ] = {0};
4448

src/flamenco/runtime/info/fd_instr_info.h

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,17 @@ typedef struct fd_instruction_account fd_instruction_account_t;
3636

3737
struct fd_instr_info {
3838
uchar program_id;
39-
ushort data_sz;
4039
ushort acct_cnt;
4140

42-
uchar * data;
41+
uchar data[ 10<<10 ]; // 10KB
42+
ushort data_sz;
4343

4444
fd_instruction_account_t accounts[ FD_INSTR_ACCT_MAX ];
4545
uchar is_duplicate[ FD_INSTR_ACCT_MAX ];
4646

47+
/* Stack height when this instruction was pushed onto the stack (including itself) */
48+
uchar stack_height;
49+
4750
/* TODO: convert to fd_uwide_t representation of uint_128 */
4851
ulong starting_lamports_h;
4952
ulong starting_lamports_l;
@@ -157,20 +160,6 @@ fd_instr_info_sum_account_lamports( fd_instr_info_t const * instr,
157160
ulong * total_lamports_h,
158161
ulong * total_lamports_l );
159162

160-
static inline uchar
161-
fd_instr_get_acc_flags( fd_instr_info_t const * instr,
162-
ushort idx ) {
163-
if( FD_UNLIKELY( idx>=instr->acct_cnt ) ) {
164-
return 0;
165-
}
166-
167-
uchar flags = 0;
168-
if( instr->accounts[idx].is_signer ) flags |= FD_INSTR_ACCT_FLAGS_IS_SIGNER;
169-
if( instr->accounts[idx].is_writable ) flags |= FD_INSTR_ACCT_FLAGS_IS_WRITABLE;
170-
171-
return flags;
172-
}
173-
174163
FD_PROTOTYPES_END
175164

176165
#endif /* HEADER_fd_src_flamenco_runtime_info_fd_instr_info_h */

src/flamenco/runtime/program/fd_bpf_loader_program.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -909,13 +909,11 @@ common_extend_program( fd_exec_instr_ctx_t * instr_ctx,
909909
/* https://github.com/anza-xyz/agave/blob/77daab497df191ef485a7ad36ed291c1874596e5/programs/bpf_loader/src/lib.rs#L566-L1444 */
910910
static int
911911
process_loader_upgradeable_instruction( fd_exec_instr_ctx_t * instr_ctx ) {
912-
uchar const * data = instr_ctx->instr->data;
913-
914912
uchar __attribute__((aligned(FD_BPF_UPGRADEABLE_LOADER_PROGRAM_INSTRUCTION_ALIGN))) instruction_mem[ FD_BPF_UPGRADEABLE_LOADER_PROGRAM_INSTRUCTION_FOOTPRINT ] = {0};
915913
fd_bpf_upgradeable_loader_program_instruction_t * instruction = fd_bincode_decode_static_limited_deserialize(
916914
bpf_upgradeable_loader_program_instruction,
917915
instruction_mem,
918-
data,
916+
instr_ctx->instr->data,
919917
instr_ctx->instr->data_sz,
920918
FD_TXN_MTU,
921919
NULL );

src/flamenco/runtime/program/fd_bpf_loader_serialization.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,7 @@ fd_bpf_loader_input_serialize_aligned( fd_exec_instr_ctx_t * ctx,
391391
*instr_data_offset = FD_VM_MEM_MAP_INPUT_REGION_START + region_vaddr_offset + (ulong)(serialized_params - curr_serialized_params_start);
392392

393393
/* https://github.com/anza-xyz/agave/blob/v3.0.0/program-runtime/src/serialization.rs#L559 */
394-
uchar * instr_data = ctx->instr->data;
395-
fd_memcpy( serialized_params, instr_data, instr_data_len );
394+
fd_memcpy( serialized_params, ctx->instr->data, instr_data_len );
396395
serialized_params += instr_data_len;
397396

398397
/* https://github.com/anza-xyz/agave/blob/v3.0.0/program-runtime/src/serialization.rs#L560 */
@@ -641,8 +640,7 @@ fd_bpf_loader_input_serialize_unaligned( fd_exec_instr_ctx_t * ctx,
641640
}
642641
*instr_data_offset = FD_VM_MEM_MAP_INPUT_REGION_START + region_vaddr_offset + (ulong)(serialized_params - curr_serialized_params_start);
643642

644-
uchar * instr_data = (uchar *)ctx->instr->data;
645-
fd_memcpy( serialized_params, instr_data, instr_data_len );
643+
fd_memcpy( serialized_params, ctx->instr->data, instr_data_len );
646644
serialized_params += instr_data_len;
647645

648646
FD_STORE( fd_pubkey_t, serialized_params, txn_accs[ctx->instr->program_id] );

0 commit comments

Comments
 (0)