Skip to content

Commit bbc611b

Browse files
flamenco, vm: implement increase_cpi_account_info_limit
1 parent 4a5957a commit bbc611b

File tree

7 files changed

+63
-7
lines changed

7 files changed

+63
-7
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, increase_cpi_account_info_limit)>>3,
1695+
.id = {"\xef\x31\xcf\xa8\x88\x28\x79\x55\xd9\xb1\x72\x19\xda\x33\x5d\x8e\x8b\x8f\x5e\x3f\x41\x49\xe5\x9a\x94\x65\xa6\x44\xef\x9c\xa2\xf6"},
1696+
/* H6iVbVaDZgDphcPbcZwc5LoznMPWQfnJ1AM7L1xzqvt5 */
1697+
.name = "increase_cpi_account_info_limit",
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 0x55792888a8cf31ef: 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, increase_cpi_account_info_limit )>>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 (3667030906U)
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+
/* 0x55792888a8cf31ef */ ulong increase_cpi_account_info_limit;
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":"increase_cpi_account_info_limit","pubkey":"H6iVbVaDZgDphcPbcZwc5LoznMPWQfnJ1AM7L1xzqvt5"}
249250
]

src/flamenco/vm/fd_vm_base.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,26 @@ FD_PROTOTYPES_END
196196
#define FD_VM_CREATE_PROGRAM_ADDRESS_UNITS ( 1500UL)
197197

198198
/* FD_VM_INVOKE_UNITS is the number of compute units consumed by an
199-
invoke call (not including the cost incurred by the called program) */
199+
invoke call (not including the cost incurred by the called program)
200+
https://github.com/anza-xyz/agave/blob/v3.1.2/program-runtime/src/execution_budget.rs#L20-L21 */
200201

201202
#define FD_VM_INVOKE_UNITS ( 1000UL)
202203

204+
/* FD_VM_INVOKE_UNITS_SIMD_0339 is the number of compute units consumed by
205+
an invoke call (not including the cost incurred by the called program)
206+
with SIMD-0339 (increase_cpi_account_info_limit) active.
207+
https://github.com/anza-xyz/agave/blob/v3.1.2/program-runtime/src/execution_budget.rs#L22-L23 */
208+
#define FD_VM_INVOKE_UNITS_SIMD_0339 ( 946UL)
209+
210+
/* SIMD-0339 uses a fixed size (80 bytes) to bill each account info:
211+
- 32 bytes for account address
212+
- 32 bytes for owner address
213+
- 8 bytes for lamports
214+
- 8 bytes for data length
215+
https://github.com/anza-xyz/agave/blob/v3.1.2/program-runtime/src/cpi.rs#L63-L68
216+
*/
217+
#define FD_VM_ACCOUNT_INFO_BYTE_SIZE ( 80UL)
218+
203219
/* FD_VM_MAX_INVOKE_STACK_HEIGHT is the maximum program instruction
204220
invocation stack height. Invocation stack height starts at 1 for
205221
transaction instructions and the stack height is incremented each

src/flamenco/vm/syscall/fd_vm_syscall_cpi.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,32 @@ fd_vm_prepare_instruction( fd_instr_info_t * callee_instr,
239239
https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/programs/bpf_loader/src/syscalls/cpi.rs#L1011 */
240240

241241
#define FD_CPI_MAX_ACCOUNT_INFOS (128UL)
242+
#define FD_CPI_MAX_ACCOUNT_INFOS_SIMD_0339 (255UL)
243+
242244
/* This is just encoding what Agave says in their code comments into a
243245
compile-time check, so if anyone ever inadvertently changes one of
244246
the limits, they will have to take a look. */
245247
FD_STATIC_ASSERT( FD_CPI_MAX_ACCOUNT_INFOS==MAX_TX_ACCOUNT_LOCKS, cpi_max_account_info );
248+
249+
/* https://github.com/anza-xyz/agave/blob/v3.1.2/program-runtime/src/cpi.rs#L168-L180 */
246250
static inline ulong
247251
get_cpi_max_account_infos( fd_bank_t * bank ) {
248-
return fd_ulong_if( FD_FEATURE_ACTIVE_BANK( bank, increase_tx_account_lock_limit ), FD_CPI_MAX_ACCOUNT_INFOS, 64UL );
252+
if( FD_LIKELY( FD_FEATURE_ACTIVE_BANK( bank, increase_cpi_account_info_limit ) ) ) {
253+
return FD_CPI_MAX_ACCOUNT_INFOS_SIMD_0339;
254+
} else if( FD_LIKELY( FD_FEATURE_ACTIVE_BANK( bank, increase_tx_account_lock_limit ) ) ) {
255+
return FD_CPI_MAX_ACCOUNT_INFOS;
256+
} else {
257+
return 64UL;
258+
}
259+
}
260+
261+
/* https://github.com/anza-xyz/agave/blob/v3.1.2/program-runtime/src/execution_budget.rs#L25-L31 */
262+
static inline ulong
263+
get_cpi_invoke_unit_cost( fd_bank_t * bank ) {
264+
return fd_ulong_if(
265+
FD_FEATURE_ACTIVE_BANK( bank, increase_cpi_account_info_limit ),
266+
FD_VM_INVOKE_UNITS_SIMD_0339,
267+
FD_VM_INVOKE_UNITS );
249268
}
250269

251270
/* Maximum CPI instruction data size. 10 KiB was chosen to ensure that CPI

src/flamenco/vm/syscall/fd_vm_syscall_cpi_common.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,8 @@ VM_SYSCALL_CPI_ENTRYPOINT( void * _vm,
677677

678678
fd_vm_t * vm = (fd_vm_t *)_vm;
679679

680-
FD_VM_CU_UPDATE( vm, FD_VM_INVOKE_UNITS );
680+
/* https://github.com/anza-xyz/agave/blob/v3.1.2/program-runtime/src/cpi.rs#L815-L818 */
681+
FD_VM_CU_UPDATE( vm, get_cpi_invoke_unit_cost( vm->instr_ctx->bank ) );
681682

682683
/* Translate instruction ********************************************/
683684
/* translate_instruction is the first thing that agave does
@@ -833,7 +834,15 @@ VM_SYSCALL_CPI_ENTRYPOINT( void * _vm,
833834
return FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNT_INFOS_EXCEEDED;
834835
}
835836

836-
fd_pubkey_t const * acct_info_keys[ FD_CPI_MAX_ACCOUNT_INFOS ];
837+
/* Consume compute units proportional to the number of account infos, if
838+
increase_cpi_account_info_limit is active.
839+
https://github.com/anza-xyz/agave/blob/v3.1.2/program-runtime/src/cpi.rs#L968-L980 */
840+
if( FD_FEATURE_ACTIVE_BANK( vm->instr_ctx->bank, increase_cpi_account_info_limit ) ) {
841+
ulong account_infos_bytes = fd_ulong_sat_mul( acct_info_cnt, VM_SYSCALL_CPI_ACC_INFO_SIZE );
842+
FD_VM_CU_UPDATE( vm, account_infos_bytes / FD_VM_CPI_BYTES_PER_UNIT );
843+
}
844+
845+
fd_pubkey_t const * acct_info_keys[ FD_CPI_MAX_ACCOUNT_INFOS_SIMD_0339 ];
837846
for( ulong acct_idx = 0UL; acct_idx < acct_info_cnt; acct_idx++ ) {
838847
/* Translate each pubkey address specified in account_infos.
839848
Failed translation should lead to an access violation and

src/flamenco/vm/test_vm_base.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ FD_STATIC_ASSERT( FD_VM_COMPUTE_UNIT_LIMIT == 1400
5151
FD_STATIC_ASSERT( FD_VM_LOG_64_UNITS == 100UL, vm_cu );
5252
FD_STATIC_ASSERT( FD_VM_CREATE_PROGRAM_ADDRESS_UNITS == 1500UL, vm_cu );
5353
FD_STATIC_ASSERT( FD_VM_INVOKE_UNITS == 1000UL, vm_cu );
54+
FD_STATIC_ASSERT( FD_VM_INVOKE_UNITS_SIMD_0339 == 946UL, vm_cu );
5455
FD_STATIC_ASSERT( FD_VM_MAX_INVOKE_STACK_HEIGHT == 5UL, vm_cu );
5556
FD_STATIC_ASSERT( FD_VM_MAX_INSTRUCTION_TRACE_LENGTH == 64UL, vm_cu );
5657
FD_STATIC_ASSERT( FD_VM_SHA256_BASE_COST == 85UL, vm_cu );
@@ -60,6 +61,7 @@ FD_STATIC_ASSERT( FD_VM_MAX_CALL_DEPTH ==
6061
FD_STATIC_ASSERT( FD_VM_STACK_FRAME_SIZE == 4096UL, vm_cu );
6162
FD_STATIC_ASSERT( FD_VM_LOG_PUBKEY_UNITS == 100UL, vm_cu );
6263
FD_STATIC_ASSERT( FD_VM_MAX_CPI_INSTRUCTION_SIZE == 1280UL, vm_cu );
64+
FD_STATIC_ASSERT( FD_VM_ACCOUNT_INFO_BYTE_SIZE == 80UL, vm_cu );
6365
FD_STATIC_ASSERT( FD_VM_CPI_BYTES_PER_UNIT == 250UL, vm_cu );
6466
FD_STATIC_ASSERT( FD_VM_SYSVAR_BASE_COST == 100UL, vm_cu );
6567
FD_STATIC_ASSERT( FD_VM_SECP256K1_RECOVER_COST == 25000UL, vm_cu );

0 commit comments

Comments
 (0)