Skip to content

Commit 717951c

Browse files
flamenco, vm: implement provide_instruction_data_offset_in_vm_r2
1 parent c9e6e6c commit 717951c

15 files changed

+128
-95
lines changed

src/flamenco/features/fd_features_generated.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,12 @@ fd_feature_id_t const ids[] = {
16911691
.name = "poseidon_enforce_padding",
16921692
.cleaned_up = {UINT_MAX, UINT_MAX, UINT_MAX} },
16931693

1694+
{ .index = offsetof(fd_features_t, provide_instruction_data_offset_in_vm_r2)>>3,
1695+
.id = {"\x49\xa8\x3f\xba\xb8\x02\x48\x7c\x34\x1e\x07\x66\xab\xd9\x59\xba\x04\xfd\xce\xcc\xe6\xbf\xdf\xd4\xc6\xe1\x62\xec\x0b\x10\x26\x6f"},
1696+
/* 5xXZc66h4UdB6Yq7FzdBxBiRAFMMScMLwHxk2QZDaNZL */
1697+
.name = "provide_instruction_data_offset_in_vm_r2",
1698+
.cleaned_up = {UINT_MAX, UINT_MAX, UINT_MAX} },
1699+
16941700
{ .index = ULONG_MAX }
16951701
};
16961702
/* TODO replace this with fd_map_perfect */
@@ -1944,6 +1950,7 @@ fd_feature_id_query( ulong prefix ) {
19441950
case 0x520c5e674243fab5: return &ids[ 244 ];
19451951
case 0xf08a42c3c040e908: return &ids[ 245 ];
19461952
case 0x8c7bee4552d93e0c: return &ids[ 246 ];
1953+
case 0x7c4802b8ba3fa849: return &ids[ 247 ];
19471954
default: break;
19481955
}
19491956
return NULL;
@@ -2196,4 +2203,5 @@ FD_STATIC_ASSERT( offsetof( fd_features_t, stricter_abi_and_runtime_constraints
21962203
FD_STATIC_ASSERT( offsetof( fd_features_t, account_data_direct_mapping )>>3==244UL, layout );
21972204
FD_STATIC_ASSERT( offsetof( fd_features_t, fix_alt_bn128_pairing_length_check )>>3==245UL, layout );
21982205
FD_STATIC_ASSERT( offsetof( fd_features_t, poseidon_enforce_padding )>>3==246UL, layout );
2206+
FD_STATIC_ASSERT( offsetof( fd_features_t, provide_instruction_data_offset_in_vm_r2 )>>3==247UL, layout );
21992207
FD_STATIC_ASSERT( sizeof( fd_features_t )>>3==FD_FEATURE_ID_CNT, layout );

src/flamenco/features/fd_features_generated.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
#endif
99

1010
/* FEATURE_ID_CNT is the number of features in ids */
11-
#define FD_FEATURE_ID_CNT (247UL)
11+
#define FD_FEATURE_ID_CNT (248UL)
1212

1313
/* Feature set ID calculated from all feature names */
14-
#define FD_FEATURE_SET_ID (2146234083U)
14+
#define FD_FEATURE_SET_ID (3038020643U)
1515

1616
union fd_features {
1717
ulong f[ FD_FEATURE_ID_CNT ];
@@ -263,5 +263,6 @@ union fd_features {
263263
/* 0x520c5e674243fab5 */ ulong account_data_direct_mapping;
264264
/* 0xf08a42c3c040e908 */ ulong fix_alt_bn128_pairing_length_check;
265265
/* 0x8c7bee4552d93e0c */ ulong poseidon_enforce_padding;
266+
/* 0x7c4802b8ba3fa849 */ ulong provide_instruction_data_offset_in_vm_r2;
266267
};
267268
};

src/flamenco/features/feature_map.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,5 +245,6 @@
245245
{"name":"stricter_abi_and_runtime_constraints","pubkey":"sD3uVpaavUXQRvDXrMFCQ2CqLqnbz5mK8ttWNXbtD3r","old":"CxeBn9PVeeXbmjbNwLv6U4C6svNxnC4JX6mfkvgeMocM"},
246246
{"name":"account_data_direct_mapping","pubkey":"DFN8MyKpQqFW31qczcahgnnxcAHQc6P94wtTEX5EP1RA","old":"9s3RKimHWS44rJcJ9P1rwCmn2TvMqtZQBmz815ZUUHqJ"},
247247
{"name":"fix_alt_bn128_pairing_length_check","pubkey":"bnYzodLwmybj7e1HAe98yZrdJTd7we69eMMLgCXqKZm"},
248-
{"name":"poseidon_enforce_padding","pubkey":"poUdAqRXXsNmfqAZ6UqpjbeYgwBygbfQLEvWSqVhSnb"}
248+
{"name":"poseidon_enforce_padding","pubkey":"poUdAqRXXsNmfqAZ6UqpjbeYgwBygbfQLEvWSqVhSnb"},
249+
{"name":"provide_instruction_data_offset_in_vm_r2","pubkey":"5xXZc66h4UdB6Yq7FzdBxBiRAFMMScMLwHxk2QZDaNZL"}
249250
]

src/flamenco/progcache/fd_progcache_rec.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ fd_progcache_rec_new( void * mem,
8989
0,
9090
FD_FEATURE_ACTIVE( load_slot, features, account_data_direct_mapping ),
9191
FD_FEATURE_ACTIVE( load_slot, features, stricter_abi_and_runtime_constraints ),
92-
0 );
92+
0,
93+
0UL );
9394
if( FD_UNLIKELY( !vm ) ) FD_LOG_CRIT(( "fd_vm_init failed" ));
9495

9596
if( FD_UNLIKELY( fd_vm_validate( vm )!=FD_VM_SUCCESS ) ) return NULL;

src/flamenco/runtime/program/fd_bpf_loader_program.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ fd_deploy_program( fd_exec_instr_ctx_t * instr_ctx,
204204
/* is_deprecated */ 0,
205205
/* direct mapping */ direct_mapping,
206206
/* stricter_abi_and_runtime_constraints */ stricter_abi_and_runtime_constraints,
207-
/* dump_syscall_to_pb */ 0 );
207+
/* dump_syscall_to_pb */ 0,
208+
/* r2_initial_value */ 0UL );
208209
if ( FD_UNLIKELY( vm == NULL ) ) {
209210
FD_LOG_WARNING(( "NULL vm" ));
210211
return FD_EXECUTOR_INSTR_ERR_PROGRAM_ENVIRONMENT_SETUP_FAILURE;
@@ -391,18 +392,21 @@ fd_bpf_execute( fd_exec_instr_ctx_t * instr_ctx,
391392
0 );
392393

393394
/* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/programs/bpf_loader/src/lib.rs#L1362-L1368 */
394-
ulong input_sz = 0UL;
395-
ulong pre_lens[256] = {0};
396-
fd_vm_input_region_t input_mem_regions[1000] = {0}; /* We can have a max of (3 * num accounts + 1) regions */
397-
fd_vm_acc_region_meta_t acc_region_metas[256] = {0}; /* instr acc idx to idx */
398-
uint input_mem_regions_cnt = 0U;
399-
int direct_mapping = FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, account_data_direct_mapping );
400-
int stricter_abi_and_runtime_constraints = FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, stricter_abi_and_runtime_constraints );
401-
395+
ulong input_sz = 0UL;
396+
ulong pre_lens[256] = {0};
397+
fd_vm_input_region_t input_mem_regions[1000] = {0}; /* We can have a max of (3 * num accounts + 1) regions */
398+
fd_vm_acc_region_meta_t acc_region_metas[256] = {0}; /* instr acc idx to idx */
399+
uint input_mem_regions_cnt = 0U;
400+
int direct_mapping = FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, account_data_direct_mapping );
401+
int stricter_abi_and_runtime_constraints = FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, stricter_abi_and_runtime_constraints );
402+
int provide_instruction_data_offset_in_vm_r2 = FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, provide_instruction_data_offset_in_vm_r2 );
403+
404+
ulong instruction_data_offset = 0UL;
402405
uchar * input = NULL;
403406
err = fd_bpf_loader_input_serialize_parameters( instr_ctx, &input_sz, pre_lens,
404407
input_mem_regions, &input_mem_regions_cnt,
405-
acc_region_metas, stricter_abi_and_runtime_constraints, direct_mapping, is_deprecated, &input );
408+
acc_region_metas, stricter_abi_and_runtime_constraints, direct_mapping, is_deprecated,
409+
&instruction_data_offset, &input );
406410
if( FD_UNLIKELY( err ) ) {
407411
return err;
408412
}
@@ -432,6 +436,9 @@ fd_bpf_execute( fd_exec_instr_ctx_t * instr_ctx,
432436
fd_bank_slot_get( instr_ctx->bank ) >= instr_ctx->runtime->log.capture_ctx->dump_proto_start_slot &&
433437
instr_ctx->runtime->log.capture_ctx->dump_syscall_to_pb;
434438

439+
/* https://github.com/anza-xyz/agave/blob/v3.1.1/programs/bpf_loader/src/lib.rs#L1525-L1528 */
440+
ulong r2_initial_value = provide_instruction_data_offset_in_vm_r2 ? FD_VM_MEM_MAP_INPUT_REGION_START + instruction_data_offset : 0UL;
441+
435442
/* TODO: (topointon): correctly set check_size in vm setup */
436443
vm = fd_vm_init(
437444
/* vm */ vm,
@@ -456,7 +463,8 @@ fd_bpf_execute( fd_exec_instr_ctx_t * instr_ctx,
456463
/* is_deprecated */ is_deprecated,
457464
/* direct_mapping */ direct_mapping,
458465
/* stricter_abi_and_runtime_constraints */ stricter_abi_and_runtime_constraints,
459-
/* dump_syscall_to_pb */ dump_syscall_to_pb );
466+
/* dump_syscall_to_pb */ dump_syscall_to_pb,
467+
/* r2_initial_value */ r2_initial_value );
460468
if( FD_UNLIKELY( !vm ) ) {
461469
/* We throw an error here because it could be the case that the given heap_size > HEAP_MAX.
462470
In this case, Agave fails the transaction but does not error out.

src/flamenco/runtime/program/fd_bpf_loader_serialization.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ fd_bpf_loader_input_serialize_aligned( fd_exec_instr_ctx_t * ctx,
262262
uint * input_mem_regions_cnt,
263263
fd_vm_acc_region_meta_t * acc_region_metas,
264264
int stricter_abi_and_runtime_constraints,
265-
int direct_mapping ) {
265+
int direct_mapping,
266+
ulong * instr_data_offset ) {
266267
fd_pubkey_t * txn_accs = ctx->txn_out->accounts.account_keys;
267268

268269
uchar acc_idx_seen[ FD_INSTR_ACCT_MAX ] = {0};
@@ -381,6 +382,14 @@ fd_bpf_loader_input_serialize_aligned( fd_exec_instr_ctx_t * ctx,
381382
FD_STORE( ulong, serialized_params, instr_data_len );
382383
serialized_params += sizeof(ulong);
383384

385+
/* https://github.com/anza-xyz/agave/blob/v3.1.1/program-runtime/src/serialization.rs#L568 */
386+
ulong region_vaddr_offset = 0UL;
387+
if( *input_mem_regions_cnt > 0 ) {
388+
region_vaddr_offset = input_mem_regions[*input_mem_regions_cnt-1U].vaddr_offset +
389+
input_mem_regions[*input_mem_regions_cnt-1U].address_space_reserved;
390+
}
391+
*instr_data_offset = region_vaddr_offset + (ulong)(serialized_params - curr_serialized_params_start);
392+
384393
/* https://github.com/anza-xyz/agave/blob/v3.0.0/program-runtime/src/serialization.rs#L559 */
385394
uchar * instr_data = ctx->instr->data;
386395
fd_memcpy( serialized_params, instr_data, instr_data_len );
@@ -538,7 +547,8 @@ fd_bpf_loader_input_serialize_unaligned( fd_exec_instr_ctx_t * ctx,
538547
uint * input_mem_regions_cnt,
539548
fd_vm_acc_region_meta_t * acc_region_metas,
540549
int stricter_abi_and_runtime_constraints,
541-
int direct_mapping ) {
550+
int direct_mapping,
551+
ulong * instr_data_offset ) {
542552
fd_pubkey_t const * txn_accs = ctx->txn_out->accounts.account_keys;
543553

544554
uchar acc_idx_seen[FD_INSTR_ACCT_MAX] = {0};
@@ -623,6 +633,14 @@ fd_bpf_loader_input_serialize_unaligned( fd_exec_instr_ctx_t * ctx,
623633
FD_STORE( ulong, serialized_params, instr_data_len );
624634
serialized_params += sizeof(ulong);
625635

636+
/* https://github.com/anza-xyz/agave/blob/v3.1.1/program-runtime/src/serialization.rs#L400 */
637+
ulong region_vaddr_offset = 0UL;
638+
if( *input_mem_regions_cnt > 0 ) {
639+
region_vaddr_offset = input_mem_regions[*input_mem_regions_cnt-1U].vaddr_offset +
640+
input_mem_regions[*input_mem_regions_cnt-1U].address_space_reserved;
641+
}
642+
*instr_data_offset = region_vaddr_offset + (ulong)(serialized_params - curr_serialized_params_start);
643+
626644
uchar * instr_data = (uchar *)ctx->instr->data;
627645
fd_memcpy( serialized_params, instr_data, instr_data_len );
628646
serialized_params += instr_data_len;
@@ -740,6 +758,7 @@ fd_bpf_loader_input_serialize_parameters( fd_exec_instr_ctx_t * instr_ctx,
740758
int stricter_abi_and_runtime_constraints,
741759
int direct_mapping,
742760
uchar is_deprecated,
761+
ulong * instr_data_offset,
743762
uchar ** out /* output */ ) {
744763

745764
/* https://github.com/anza-xyz/agave/blob/v3.0.0/program-runtime/src/serialization.rs#L234-L237 */
@@ -755,12 +774,12 @@ fd_bpf_loader_input_serialize_parameters( fd_exec_instr_ctx_t * instr_ctx,
755774
*out = fd_bpf_loader_input_serialize_unaligned( instr_ctx, sz, pre_lens,
756775
input_mem_regions, input_mem_regions_cnt,
757776
acc_region_metas, stricter_abi_and_runtime_constraints,
758-
direct_mapping );
777+
direct_mapping, instr_data_offset );
759778
} else {
760779
*out = fd_bpf_loader_input_serialize_aligned( instr_ctx, sz, pre_lens,
761780
input_mem_regions, input_mem_regions_cnt,
762781
acc_region_metas, stricter_abi_and_runtime_constraints,
763-
direct_mapping );
782+
direct_mapping, instr_data_offset );
764783
}
765784

766785
return FD_EXECUTOR_INSTR_SUCCESS;

src/flamenco/runtime/program/fd_bpf_loader_serialization.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ fd_bpf_loader_input_serialize_parameters( fd_exec_instr_ctx_t * instr_ctx,
1818
int stricter_abi_and_runtime_constraints,
1919
int direct_mapping,
2020
uchar is_deprecated,
21+
ulong * instr_data_offset,
2122
uchar ** out /* output */ );
2223

2324
int

src/flamenco/runtime/tests/fd_vm_harness.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ do{
171171
}
172172

173173
/* Serialize accounts into input memory region. */
174+
ulong instr_data_offset = 0UL;
174175
int err = fd_bpf_loader_input_serialize_parameters( instr_ctx,
175176
&input_sz,
176177
pre_lens,
@@ -180,6 +181,7 @@ do{
180181
stricter_abi_and_runtime_constraints,
181182
direct_mapping,
182183
is_deprecated,
184+
&instr_data_offset,
183185
&input_ptr );
184186
if( FD_UNLIKELY( err ) ) {
185187
fd_solfuzz_pb_instr_ctx_destroy( runner, instr_ctx );
@@ -258,12 +260,13 @@ do{
258260
is_deprecated, /* is deprecated */
259261
direct_mapping, /* direct mapping */
260262
stricter_abi_and_runtime_constraints, /* stricter_abi_and_runtime_constraints */
261-
0 /* dump_syscall_to_pb */
263+
0 /* dump_syscall_to_pb */,
264+
0UL /* r2 is set by the fuzzer below */
262265
);
263266

264267
/* Setup registers.
265268
r1, r10, r11 are initialized by EbpfVm::new (r10) or EbpfVm::execute_program (r1, r11),
266-
or equivalently by fd_vm_init and fd_vm_setup_state_for_execution.
269+
or equivalently by fd_vm_init.
267270
Modifying them will most like break execution.
268271
In syscalls we allow override them (especially r1) because that simulates the fact
269272
that a program partially executed before reaching the syscall.
@@ -482,6 +485,7 @@ fd_solfuzz_pb_syscall_run( fd_solfuzz_runner_t * runner,
482485
}
483486

484487
/* Serialize accounts into input memory region. */
488+
ulong instr_data_offset = 0UL;
485489
int err = fd_bpf_loader_input_serialize_parameters( ctx,
486490
&input_sz,
487491
pre_lens,
@@ -491,6 +495,7 @@ fd_solfuzz_pb_syscall_run( fd_solfuzz_runner_t * runner,
491495
stricter_abi_and_runtime_constraints,
492496
direct_mapping,
493497
is_deprecated,
498+
&instr_data_offset,
494499
&input_ptr );
495500
if( FD_UNLIKELY( err ) ) {
496501
FD_LOG_WARNING(( "bpf loader input serialize parameters err" ));
@@ -519,7 +524,8 @@ fd_solfuzz_pb_syscall_run( fd_solfuzz_runner_t * runner,
519524
is_deprecated,
520525
direct_mapping,
521526
stricter_abi_and_runtime_constraints,
522-
0 /* dump_syscall_to_pb */ );
527+
0 /* dump_syscall_to_pb */,
528+
0UL /* r2 is set by the fuzzer below */ );
523529

524530
// Override some execution state values from the syscall fuzzer input
525531
// This is so we can test if the syscall mutates any of these erroneously

src/flamenco/runtime/tests/run_backtest_ci.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ src/flamenco/runtime/tests/run_ledger_backtest.sh -l mainnet-368528500-stricter-
3030
src/flamenco/runtime/tests/run_ledger_backtest.sh -l mainnet-368528500-direct-mapping -y 5 -m 2000000 -e 368528527 -o sD3uVpaavUXQRvDXrMFCQ2CqLqnbz5mK8ttWNXbtD3r,DFN8MyKpQqFW31qczcahgnnxcAHQc6P94wtTEX5EP1RA
3131
src/flamenco/runtime/tests/run_ledger_backtest.sh -l testnet-362107883-direct-mapping-2 -y 1 -m 2000000 -e 362219427 -o sD3uVpaavUXQRvDXrMFCQ2CqLqnbz5mK8ttWNXbtD3r,DFN8MyKpQqFW31qczcahgnnxcAHQc6P94wtTEX5EP1RA
3232
src/flamenco/runtime/tests/run_ledger_backtest.sh -l mainnet-378539412 -y 5 -m 2000000 -e 378539445
33+
src/flamenco/runtime/tests/run_ledger_backtest.sh -l mainnet-376969880-r2 -y 1 -m 2000000 -e 376969900 -o 5xXZc66h4UdB6Yq7FzdBxBiRAFMMScMLwHxk2QZDaNZL

src/flamenco/vm/fd_vm.c

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -574,29 +574,30 @@ fd_vm_delete( void * shmem ) {
574574

575575
fd_vm_t *
576576
fd_vm_init(
577-
fd_vm_t * vm,
578-
fd_exec_instr_ctx_t *instr_ctx,
579-
ulong heap_max,
580-
ulong entry_cu,
581-
uchar const * rodata,
582-
ulong rodata_sz,
583-
ulong const * text,
584-
ulong text_cnt,
585-
ulong text_off,
586-
ulong text_sz,
587-
ulong entry_pc,
588-
ulong const * calldests,
589-
ulong sbpf_version,
590-
fd_sbpf_syscalls_t * syscalls,
591-
fd_vm_trace_t * trace,
592-
fd_sha256_t * sha,
593-
fd_vm_input_region_t * mem_regions,
594-
uint mem_regions_cnt,
577+
fd_vm_t * vm,
578+
fd_exec_instr_ctx_t * instr_ctx,
579+
ulong heap_max,
580+
ulong entry_cu,
581+
uchar const * rodata,
582+
ulong rodata_sz,
583+
ulong const * text,
584+
ulong text_cnt,
585+
ulong text_off,
586+
ulong text_sz,
587+
ulong entry_pc,
588+
ulong const * calldests,
589+
ulong sbpf_version,
590+
fd_sbpf_syscalls_t * syscalls,
591+
fd_vm_trace_t * trace,
592+
fd_sha256_t * sha,
593+
fd_vm_input_region_t * mem_regions,
594+
uint mem_regions_cnt,
595595
fd_vm_acc_region_meta_t * acc_region_metas,
596-
uchar is_deprecated,
597-
int direct_mapping,
598-
int stricter_abi_and_runtime_constraints,
599-
int dump_syscall_to_pb ) {
596+
uchar is_deprecated,
597+
int direct_mapping,
598+
int stricter_abi_and_runtime_constraints,
599+
int dump_syscall_to_pb,
600+
ulong r2_initial_value ) {
600601

601602
if ( FD_UNLIKELY( vm == NULL ) ) {
602603
FD_LOG_WARNING(( "NULL vm" ));
@@ -648,30 +649,14 @@ fd_vm_init(
648649
vm->segv_access_type = 0;
649650
vm->dump_syscall_to_pb = dump_syscall_to_pb;
650651

651-
/* Unpack the configuration */
652-
int err = fd_vm_setup_state_for_execution( vm );
653-
if( FD_UNLIKELY( err != FD_VM_SUCCESS ) ) {
654-
return NULL;
655-
}
656-
657-
return vm;
658-
}
659-
660-
int
661-
fd_vm_setup_state_for_execution( fd_vm_t * vm ) {
662-
663-
if ( FD_UNLIKELY( !vm ) ) {
664-
FD_LOG_WARNING(( "NULL vm" ));
665-
return FD_VM_ERR_INVAL;
666-
}
667-
668652
/* Unpack input and rodata */
669653
fd_vm_mem_cfg( vm );
670654

671655
/* Initialize registers */
672656
/* FIXME: Zero out shadow, stack and heap here? */
673657
fd_memset( vm->reg, 0, FD_VM_REG_MAX * sizeof(ulong) );
674658
vm->reg[ 1] = FD_VM_MEM_MAP_INPUT_REGION_START;
659+
vm->reg[ 2] = r2_initial_value;
675660
/* https://github.com/solana-labs/rbpf/blob/4ad935be45e5663be23b30cfc750b1ae1ad03c44/src/vm.rs#L326-L333 */
676661
vm->reg[10] = FD_VM_MEM_MAP_STACK_REGION_START +
677662
( FD_VM_SBPF_DYNAMIC_STACK_FRAMES( vm->sbpf_version ) ? FD_VM_STACK_MAX : FD_VM_STACK_FRAME_SZ );
@@ -687,5 +672,5 @@ fd_vm_setup_state_for_execution( fd_vm_t * vm ) {
687672

688673
/* Do NOT reset logs */
689674

690-
return FD_VM_SUCCESS;
675+
return vm;
691676
}

0 commit comments

Comments
 (0)