Line data Source code
1 : /* This file contains all the logic that is common to both the C and Rust
2 : CPI syscalls (sol_invoke_signed_{rust/c}). As such, all of the functions in
3 : here are templated and will be instantiated for both the C and Rust CPI ABIs.
4 :
5 : The only difference between the C and Rust CPI syscalls is the ABI data layout
6 : of the parameters to these calls - all the logic is identical. As such, we have
7 : defined a series of macros to abstract away the ABI differences from the CPI implementation.
8 :
9 : The entry-point for these syscalls is VM_SYSCALL_CPI_ENTRYPOINT.
10 :
11 : Note that the code for these syscalls could be simplified somewhat, but we have opted to keep
12 : it as close to the Solana code as possible to make it easier to audit that we execute equivalently.
13 : Most of the top-level functions in this file correspond directly to functions in the Solana codebase
14 : and links to the source have been provided.
15 : */
16 :
17 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L126-L144
18 :
19 : This is used for checking that the account info pointers given by the
20 : user match up with the addresses in the serialized account metadata.
21 :
22 : Field name length is restricted to 54 because
23 : 127 - (37 + 18 + 18) leaves 54 characters for the field name
24 : */
25 : #define VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(vm, vm_addr, expected_vm_addr, field_name) \
26 4962 : if( FD_UNLIKELY( vm_addr!=expected_vm_addr )) { \
27 48 : fd_log_collector_printf_dangerous_max_127( vm->instr_ctx, \
28 48 : "Invalid account info pointer `%s': 0x%lx != 0x%lx", field_name, vm_addr, expected_vm_addr ); \
29 48 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER ); \
30 48 : return FD_VM_SYSCALL_ERR_INVALID_POINTER; \
31 48 : }
32 :
33 : /* fd_vm_syscall_cpi_instruction_to_instr_{c/rust} takes the translated
34 : CPI ABI structures (instruction and account meta list), and uses these
35 : to populate a fd_instr_info_t struct. This struct can then be given to the
36 : FD runtime for execution.
37 :
38 : WARNING: out_instr will be partially filled if there are unmatched account
39 : metas (i.e,. no corresponding entry in the transaction accounts list). This
40 : is not an error condition. fd_vm_prepare_instruction has to handle that case
41 : in order to match Agave's behavior of checking presence in both transaction
42 : accounts list and caller instruction accounts list in a single loop iteration.
43 :
44 : Parameters:
45 : - vm: handle to the vm
46 : - cpi_instr: instruction to execute laid out in the CPI ABI format (Rust or C)
47 : - cpi_acc_metas: list of account metas, again in the CPI ABI format
48 : - program_id: translated program pubkey
49 : - out_instr: output instruction
50 : - out_instr_acct_keys: output instruction account pubkeys
51 : - cpi_instr_data: instruction data in host address space
52 :
53 : TODO: return codes/errors?
54 : */
55 1083 : #define VM_SYSCALL_CPI_INSTRUCTION_TO_INSTR_FUNC FD_EXPAND_THEN_CONCAT2(fd_vm_syscall_cpi_instruction_to_instr_, VM_SYSCALL_CPI_ABI)
56 : static int
57 : VM_SYSCALL_CPI_INSTRUCTION_TO_INSTR_FUNC( fd_vm_t * vm,
58 : VM_SYSCALL_CPI_INSTR_T const * cpi_instr,
59 : VM_SYSCALL_CPI_ACC_META_T const * cpi_acct_metas,
60 : fd_pubkey_t const * program_id,
61 : uchar const * cpi_instr_data,
62 : fd_instr_info_t * out_instr,
63 1083 : fd_pubkey_t out_instr_acct_keys[ FD_TXN_INSTR_ACCT_MAX ] ) {
64 :
65 1083 : out_instr->program_id = UCHAR_MAX;
66 1083 : out_instr->stack_height = (uchar)( vm->instr_ctx->runtime->instr.stack_sz+1 );
67 1083 : out_instr->data_sz = (ushort)VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instr );
68 1083 : out_instr->acct_cnt = (ushort)VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instr );
69 1083 : if( FD_LIKELY( out_instr->data_sz ) ) memcpy( out_instr->data, cpi_instr_data, out_instr->data_sz );
70 :
71 : /* Find the index of the CPI instruction's program account in the transaction */
72 1083 : ulong program_id_idx = fd_runtime_find_index_of_account( vm->instr_ctx->txn_out, program_id );
73 1083 : if( FD_LIKELY( program_id_idx!=ULONG_MAX ) ) out_instr->program_id = (uchar)program_id_idx;
74 :
75 1083 : uchar acc_idx_seen[ FD_TXN_ACCT_ADDR_MAX ] = {0};
76 1083 : ushort caller_map[ MAX_TX_ACCOUNT_LOCKS ];
77 1083 : fd_exec_instr_ctx_instr_account_map( vm->instr_ctx, caller_map );
78 :
79 2457 : for( ushort i=0; i<VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instr ); i++ ) {
80 1374 : VM_SYSCALL_CPI_ACC_META_T const * cpi_acct_meta = &cpi_acct_metas[i];
81 1917 : fd_pubkey_t const * pubkey = fd_type_pun_const( VM_SYSCALL_CPI_ACC_META_PUBKEY( vm, cpi_acct_meta ) );
82 1917 : out_instr_acct_keys[i] = *pubkey;
83 :
84 : /* The parent flag(s) for is writable/signer is checked in
85 : fd_vm_prepare_instruction. Signer privilege is allowed iff the account
86 : is a signer in the caller or if it is a derived signer. */
87 : /* TODO: error if flags are wrong */
88 1917 : out_instr->accounts[i] = fd_instruction_account_init( USHORT_MAX,
89 1917 : USHORT_MAX,
90 1917 : USHORT_MAX,
91 1917 : VM_SYSCALL_CPI_ACC_META_IS_WRITABLE( cpi_acct_meta ),
92 1917 : VM_SYSCALL_CPI_ACC_META_IS_SIGNER( cpi_acct_meta ) );
93 :
94 : /* Use USHORT_MAX to indicate account not found
95 : https://github.com/anza-xyz/agave/blob/v3.0.4/program-runtime/src/invoke_context.rs#L395-L397 */
96 1917 : ulong idx_in_txn = fd_runtime_find_index_of_account( vm->instr_ctx->txn_out, pubkey );
97 :
98 1917 : fd_instr_info_setup_instr_account( out_instr,
99 1917 : acc_idx_seen,
100 1917 : idx_in_txn!=ULONG_MAX ? (ushort)idx_in_txn : USHORT_MAX,
101 1917 : idx_in_txn!=ULONG_MAX ? caller_map[ idx_in_txn ] : USHORT_MAX,
102 1917 : i,
103 1917 : VM_SYSCALL_CPI_ACC_META_IS_WRITABLE( cpi_acct_meta ),
104 1917 : VM_SYSCALL_CPI_ACC_META_IS_SIGNER( cpi_acct_meta ) );
105 :
106 1917 : }
107 :
108 1083 : return FD_VM_SUCCESS;
109 1083 : }
110 :
111 : /*
112 : fd_vm_syscall_cpi_update_callee_acc_{rust/c} corresponds to solana_bpf_loader_program::syscalls::cpi::update_callee_account:
113 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1211-L1273
114 :
115 : (the copy of the account stored in the instruction context's
116 : borrowed accounts cache)
117 :
118 : This function should be called before the CPI instruction is executed. Its purpose is to
119 : update the callee account's view of the given account with any changes the caller may made
120 : to the account before the CPI instruction is executed.
121 :
122 : The callee's view of the account is the borrowed accounts cache, so to update the
123 : callee account we look up the account in the borrowed accounts cache and update it.
124 :
125 : Parameters:
126 : - vm: pointer to the virtual machine handle
127 : - caller_account: translated caller account
128 : - callee_acc: borrowed callee account
129 : - out_must_update_caller: set when caller metadata must be updated after CPI
130 : */
131 1194 : #define VM_SYCALL_CPI_UPDATE_CALLEE_ACC_FUNC FD_EXPAND_THEN_CONCAT2(fd_vm_syscall_cpi_update_callee_acc_, VM_SYSCALL_CPI_ABI)
132 : static int
133 : VM_SYCALL_CPI_UPDATE_CALLEE_ACC_FUNC( fd_vm_t * vm,
134 : fd_vm_cpi_caller_account_t const * caller_account,
135 : fd_borrowed_account_t * callee_acc,
136 1194 : uchar * out_must_update_caller ) {
137 1194 : int err;
138 1194 : *out_must_update_caller = 0;
139 :
140 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1222-L1224 */
141 1194 : if( fd_borrowed_account_get_lamports( callee_acc )!=FD_LOAD( ulong, caller_account->lamports ) ) {
142 87 : err = fd_borrowed_account_set_lamports( callee_acc, FD_LOAD( ulong, caller_account->lamports ) );
143 87 : if( FD_UNLIKELY( err ) ) {
144 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, err );
145 0 : return -1;
146 0 : }
147 87 : }
148 :
149 : /* Account data is modified in place; propagate the length.
150 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1226-L1255 */
151 1194 : ulong prev_len = fd_borrowed_account_get_data_len( callee_acc );
152 1194 : ulong post_len = FD_LOAD( ulong, caller_account->ref_to_len_in_vm );
153 :
154 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1229-L1251 */
155 1194 : if( FD_UNLIKELY( prev_len!=post_len ) ) {
156 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1248 */
157 0 : err = fd_borrowed_account_set_data_length( callee_acc, post_len );
158 0 : if( FD_UNLIKELY( err ) ) {
159 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, err );
160 0 : return -1;
161 0 : }
162 : /* Pointer to data may have changed, caller must be updated.
163 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1248-L1250 */
164 0 : *out_must_update_caller = 1;
165 0 : }
166 :
167 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1266-L1271 */
168 1194 : if( FD_UNLIKELY( memcmp( fd_borrowed_account_get_owner( callee_acc ), caller_account->owner, sizeof(fd_pubkey_t) ) ) ) {
169 81 : err = fd_borrowed_account_set_owner( callee_acc, caller_account->owner );
170 81 : if( FD_UNLIKELY( err ) ) {
171 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, err );
172 0 : return -1;
173 0 : }
174 : /* Caller gave ownership and thus write access away, so caller must be updated.
175 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1268-L1270 */
176 81 : *out_must_update_caller = 1;
177 81 : }
178 :
179 1194 : return FD_VM_SUCCESS;
180 1194 : }
181 :
182 : /*
183 : fd_vm_syscall_cpi_translate_and_update_accounts_ mirrors the behaviour of
184 : solana_program_runtime::cpi::SyscallInvokeSigned::translate_accounts_common:
185 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1049-L1193
186 :
187 : It translates the caller accounts to the host address space. The
188 : syscall entrypoint updates the callee accounts after translation.
189 :
190 : Parameters:
191 : - vm: pointer to the virtual machine handle
192 : - instruction_accounts: array of instruction accounts
193 : - instruction_accounts_cnt: length of the instruction_accounts array
194 : - account_infos: array of account infos
195 : - account_infos_length: length of the account_infos array
196 :
197 : Populates:
198 : - translated_accounts: the translated account entries
199 : - out_len: number of translated account entries
200 : */
201 1041 : #define VM_SYSCALL_CPI_TRANSLATE_AND_UPDATE_ACCOUNTS_FUNC FD_EXPAND_THEN_CONCAT2(fd_vm_syscall_cpi_translate_and_update_accounts_, VM_SYSCALL_CPI_ABI)
202 : static int
203 : VM_SYSCALL_CPI_TRANSLATE_AND_UPDATE_ACCOUNTS_FUNC(
204 : fd_vm_t * vm,
205 : fd_instruction_account_t const * instruction_accounts,
206 : ulong const instruction_accounts_cnt,
207 : ulong acct_infos_va,
208 : fd_pubkey_t const * * account_info_keys, /* same length as account_infos_length */
209 : VM_SYSCALL_CPI_ACC_INFO_T const * account_infos,
210 : ulong const account_infos_length,
211 : fd_vm_cpi_translated_account_t * translated_accounts,
212 1041 : ulong * out_len ) {
213 2295 : for( ulong i=0UL; i<instruction_accounts_cnt; i++ ) {
214 1332 : if( i!=instruction_accounts[i].index_in_callee ) {
215 : /* Skip duplicate accounts */
216 36 : continue;
217 36 : }
218 :
219 : /* `fd_vm_prepare_instruction()` will always set up a valid index for `index_in_caller`, so we can access the borrowed account directly.
220 : A borrowed account will always have non-NULL meta (if the account doesn't exist, `fd_executor_setup_accounts_for_txn()`
221 : will set its meta up) */
222 :
223 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1102 */
224 1296 : fd_guarded_borrowed_account_t callee_acct = {0};
225 1296 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( vm->instr_ctx, instruction_accounts[i].index_in_caller, &callee_acct );
226 :
227 1290 : fd_pubkey_t const * account_key = (fd_pubkey_t*)callee_acct.acc->pubkey;
228 :
229 : /* If the account is known and executable, we only need to consume the compute units.
230 : Executable accounts can't be modified, so we don't need to update the callee account. */
231 1290 : if( fd_borrowed_account_is_executable( &callee_acct ) ) {
232 : // FIXME: should this be FD_VM_CU_MEM_OP_UPDATE? Changing this changes the CU behaviour from main (because of the base cost)
233 24 : FD_VM_CU_UPDATE( vm, callee_acct.acc->data_len / FD_VM_CPI_BYTES_PER_UNIT );
234 24 : continue;
235 24 : }
236 :
237 : /* FIXME: we should not need to drop the account here to avoid a double borrow.
238 : Instead, we should borrow the account before entering this function. */
239 1266 : fd_borrowed_account_drop( &callee_acct );
240 :
241 : /* Find the indices of the account in the caller and callee instructions */
242 1266 : uint found = 0;
243 3243 : for( ushort j=0; j<account_infos_length && !found; j++ ) {
244 2037 : fd_pubkey_t const * acct_addr = account_info_keys[ j ];
245 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1117
246 : */
247 2037 : if( memcmp( account_key->uc, acct_addr->uc, sizeof(fd_pubkey_t) ) != 0 ) {
248 783 : continue;
249 783 : }
250 :
251 1254 : fd_vm_cpi_translated_account_t * translated_account = translated_accounts + *out_len;
252 1254 : fd_vm_cpi_caller_account_t * caller_account = &translated_account->caller_account;
253 1254 : ushort index_in_caller = instruction_accounts[i].index_in_caller;
254 1254 : translated_account->index_in_caller = index_in_caller;
255 1254 : translated_account->update_caller_account_info = (uchar)!!instruction_accounts[i].is_writable;
256 1254 : found = 1;
257 :
258 : /* Logically this check isn't ever going to fail due to how the
259 : account_info_keys array is set up. We replicate the check for
260 : clarity and also to guard against accidental violation of the
261 : assumed invariant in the future.
262 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1131-L1134
263 : */
264 1254 : if( FD_UNLIKELY( j >= account_infos_length ) ) {
265 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_LENGTH );
266 0 : return FD_VM_SYSCALL_ERR_INVALID_LENGTH;
267 0 : }
268 :
269 : /* The following implements the checks in from_account_info which
270 : is invoked as do_translate() in translate_and_update_accounts()
271 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1135-L1146
272 : */
273 : ////// BEGIN from_account_info
274 :
275 1254 : fd_vm_acc_region_meta_t * acc_region_meta = &vm->acc_region_metas[index_in_caller];
276 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L321-L334 */
277 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L322-L327 */
278 1254 : ulong expected_pubkey_vaddr = acc_region_meta->vm_key_addr;
279 : /* Max msg_sz: 40 + 18 + 18 = 76 < 127 */
280 1254 : VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(vm, account_infos[j].pubkey_addr, expected_pubkey_vaddr, "key");
281 :
282 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L328-L333 */
283 1254 : ulong expected_owner_vaddr = acc_region_meta->vm_owner_addr;
284 : /* Max msg_sz: 42 + 18 + 18 = 78 < 127 */
285 1254 : VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(vm, account_infos[j].owner_addr, expected_owner_vaddr, "owner");
286 :
287 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L336-L358 */
288 2007 : VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_VADDR( vm, (account_infos + j), lamports_vaddr );
289 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L345-L356 */
290 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L346-L348
291 : Check that the account's lamports Rc<RefCell<&mut u64>> is not
292 : stored in the account region. Because a refcell is only present if
293 : the Rust SDK is used, we only need to check this for the Rust ABI. */
294 : #ifdef VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_RC_REFCELL_VADDR
295 669 : VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_RC_REFCELL_VADDR( vm, (account_infos + j), lamports_rc_vaddr )
296 669 : if ( FD_UNLIKELY( lamports_rc_vaddr >= FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
297 6 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
298 6 : return FD_VM_SYSCALL_ERR_INVALID_POINTER;
299 6 : }
300 663 : #endif
301 :
302 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L350-L355 */
303 663 : ulong expected_lamports_vaddr = acc_region_meta->vm_lamports_addr;
304 : /* Max msg_sz: 45 + 18 + 18 = 81 < 127 */
305 1236 : VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(vm, lamports_vaddr, expected_lamports_vaddr, "lamports");
306 :
307 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L357
308 : */
309 3195 : VM_SYSCALL_CPI_ACC_INFO_LAMPORTS( vm, (account_infos + j), lamports_haddr );
310 3195 : caller_account->lamports = lamports_haddr;
311 :
312 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L360-L364
313 : */
314 3195 : caller_account->owner = FD_VM_MEM_HADDR_ST( vm, (account_infos + j)->owner_addr, alignof(uchar), sizeof(fd_pubkey_t) );
315 :
316 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L367-L378
317 : */
318 2604 : VM_SYSCALL_CPI_ACC_INFO_DATA_VADDR( vm, (account_infos + j), data_vaddr );
319 :
320 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L379-L386 */
321 2604 : VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(
322 2604 : vm, data_vaddr, acc_region_meta->vm_data_addr, "data");
323 :
324 : #ifdef VM_SYSCALL_CPI_ACC_INFO_DATA_LEN_VADDR
325 : /* Rust ABI
326 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L395-L404 */
327 639 : VM_SYSCALL_CPI_ACC_INFO_DATA_LEN_VADDR( vm, (account_infos + j), data_len_vaddr );
328 : (void)acct_infos_va;
329 : #else
330 : /* C ABI
331 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L508-L514 */
332 : ulong data_len_vaddr = vm_syscall_cpi_data_len_vaddr_c(
333 555 : fd_ulong_sat_add( acct_infos_va, fd_ulong_sat_mul( j, VM_SYSCALL_CPI_ACC_INFO_SIZE ) ),
334 : (ulong)&((account_infos + j)->data_sz),
335 : (ulong)(account_infos + j)
336 : );
337 : #endif
338 :
339 : /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L397-L404
340 : C ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L515-L522 */
341 1194 : if( FD_UNLIKELY( data_len_vaddr >= FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
342 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
343 0 : return FD_VM_SYSCALL_ERR_INVALID_POINTER;
344 0 : }
345 :
346 : /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L411
347 : C ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L545 */
348 1194 : caller_account->vm_data_vaddr = data_vaddr;
349 :
350 : /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L405-L406
351 : C ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L523-L524 */
352 1194 : void * data_len = FD_VM_MEM_HADDR_ST( vm, data_len_vaddr, 1UL, sizeof(ulong) );
353 1194 : caller_account->ref_to_len_in_vm = data_len;
354 :
355 : /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L408-L421
356 : C ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L525-L538
357 :
358 : Both ABIs call CallerAccount::get_serialized_data:
359 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L250-L299 */
360 :
361 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L262-L272 */
362 1194 : ulong address_space_reserved_for_account;
363 1194 : if( vm->is_deprecated ) {
364 468 : address_space_reserved_for_account = acc_region_meta->original_data_len;
365 726 : } else {
366 726 : address_space_reserved_for_account = fd_ulong_sat_add( acc_region_meta->original_data_len, MAX_PERMITTED_DATA_INCREASE );
367 726 : }
368 1194 : if( FD_UNLIKELY( FD_LOAD( ulong, data_len ) > address_space_reserved_for_account ) ) {
369 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC );
370 0 : return -1;
371 0 : }
372 :
373 : /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L428
374 : C ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L428 */
375 1194 : caller_account->orig_data_len = acc_region_meta->original_data_len;
376 :
377 : ////// END from_account_info
378 :
379 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1148-L1156 */
380 1194 : FD_VM_CU_UPDATE( vm, FD_LOAD( ulong, data_len ) / FD_VM_CPI_BYTES_PER_UNIT );
381 1194 : (*out_len)++;
382 1194 : }
383 :
384 1206 : if( !found ) {
385 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.3/program-runtime/src/cpi.rs#L1183-L1188 */
386 12 : FD_BASE58_ENCODE_32_BYTES( account_key->uc, id_b58 );
387 12 : fd_log_collector_msg_many( vm->instr_ctx, 2, "Instruction references an unknown account ", 42UL, id_b58, id_b58_len );
388 12 : FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_MISSING_ACC );
389 12 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
390 12 : }
391 1206 : }
392 :
393 963 : return FD_VM_SUCCESS;
394 1041 : }
395 :
396 : /* fd_vm_cpi_update_caller_acc_{rust/c} mirrors the behaviour of
397 : solana_bpf_loader_program::syscalls::cpi::update_caller_account:
398 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1171-L1268
399 :
400 : This method should be called after a CPI instruction execution has
401 : returned. It updates the given caller account info with any changes the callee
402 : has made to this account during execution, so that those changes are
403 : reflected in the rest of the caller's execution.
404 :
405 : Those changes will be in the instructions borrowed accounts cache.
406 :
407 : Parameters:
408 : - vm: handle to the vm
409 : - caller_account: translated caller account to update
410 : - borrowed_callee_acc: already-borrowed callee account
411 : */
412 894 : #define VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC FD_EXPAND_THEN_CONCAT2(fd_vm_cpi_update_caller_acc_, VM_SYSCALL_CPI_ABI)
413 : static int
414 : VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC( fd_vm_t * vm,
415 : fd_vm_cpi_caller_account_t * caller_account,
416 894 : fd_borrowed_account_t * borrowed_callee_acc ) {
417 :
418 : /* Update the caller account lamports with the value from the callee
419 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1191 */
420 894 : FD_STORE( ulong, caller_account->lamports, borrowed_callee_acc->acc->lamports );
421 :
422 : /* Update the caller account owner with the value from the callee
423 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1192 */
424 894 : fd_memcpy( caller_account->owner, borrowed_callee_acc->acc->owner, sizeof(fd_pubkey_t) );
425 :
426 : /* Update the caller account data with the value from the callee
427 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1194-L1195 */
428 894 : ulong prev_len = FD_LOAD( ulong, caller_account->ref_to_len_in_vm );
429 894 : ulong post_len = borrowed_callee_acc->acc->data_len;
430 :
431 : /* With the deprecated loader, the reserved space equals the original
432 : length.
433 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1197-L1204 */
434 894 : ulong address_space_reserved_for_account;
435 894 : if( vm->is_deprecated ) {
436 348 : address_space_reserved_for_account = caller_account->orig_data_len;
437 546 : } else {
438 546 : address_space_reserved_for_account = fd_ulong_sat_add( caller_account->orig_data_len, MAX_PERMITTED_DATA_INCREASE );
439 546 : }
440 :
441 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1206-L1216 */
442 894 : if( post_len > address_space_reserved_for_account ) {
443 54 : ulong max_increase = fd_ulong_sat_sub( address_space_reserved_for_account, caller_account->orig_data_len );
444 54 : fd_log_collector_printf_dangerous_max_127( vm->instr_ctx, "Account data size realloc limited to %lu in inner instructions", max_increase );
445 54 : FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC );
446 54 : return FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC;
447 54 : }
448 :
449 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1218-L1252 */
450 840 : if( prev_len != post_len ) {
451 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1240-L1241 */
452 201 : FD_STORE( ulong, caller_account->ref_to_len_in_vm, post_len );
453 :
454 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1243-L1251 */
455 201 : void * caller_len = FD_VM_MEM_HADDR_ST( vm, fd_ulong_sat_sub(caller_account->vm_data_vaddr, sizeof(ulong)), alignof(ulong), sizeof(ulong) );
456 201 : FD_STORE( ulong, caller_len, post_len );
457 201 : }
458 :
459 840 : return FD_VM_SUCCESS;
460 840 : }
461 :
462 : /* fd_vm_syscall_cpi_{rust/c} is the entrypoint for the sol_invoke_signed_{rust/c} syscalls.
463 :
464 : The bulk of the high-level logic mirrors Solana's cpi_common entrypoint function at
465 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L847-L977
466 :
467 : 100-foot flow:
468 : - Translate the CPI ABI structures to the FD runtime's instruction format
469 : - Update the callee accounts with any changes made by the caller prior to this CPI instruction
470 : - Dispatch the instruction to the FD runtime (actually making the CPI call)
471 : - Update the caller accounts with any changes made by the callee during CPI execution
472 :
473 : Parameters:
474 : - vm: pointer to the virtual machine handle
475 : - instruction_va: vm address of the instruction to execute, which will be in the language-specific ABI format.
476 : - acct_infos_va: vm address of the account infos, which will be in the language-specific ABI format.
477 : - acct_info_cnt: number of account infos
478 : - signers_seeds_va: vm address of the signers seeds
479 : - signers_seeds_cnt: number of signers seeds
480 : */
481 : #define VM_SYSCALL_CPI_ENTRYPOINT FD_EXPAND_THEN_CONCAT2(fd_vm_syscall_cpi_, VM_SYSCALL_CPI_ABI)
482 : int
483 : VM_SYSCALL_CPI_ENTRYPOINT( void * _vm,
484 : ulong instruction_va,
485 : ulong acct_infos_va,
486 : ulong acct_info_cnt,
487 : ulong signers_seeds_va,
488 1113 : ulong signers_seeds_cnt ) {
489 1113 : long const regime0 = fd_tickcount();
490 :
491 1113 : fd_vm_t * vm = (fd_vm_t *)_vm;
492 :
493 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L859-L864 */
494 1113 : FD_VM_CU_UPDATE( vm, get_cpi_invoke_unit_cost() );
495 :
496 : /* Translate instruction ********************************************/
497 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L878-L883
498 : Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L575-L636
499 : C ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L709-L774 */
500 :
501 : /* Translating the CPI instruction
502 : Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L581
503 : C ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L715 */
504 1113 : VM_SYSCALL_CPI_INSTR_T const * cpi_instruction =
505 3339 : FD_VM_MEM_HADDR_LD( vm, instruction_va, VM_SYSCALL_CPI_INSTR_ALIGN, VM_SYSCALL_CPI_INSTR_SIZE );
506 :
507 : /* This needs to be here for the C ABI
508 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L717
509 : */
510 3339 : fd_pubkey_t const * program_id = (fd_pubkey_t *)VM_SYSCALL_CPI_INSTR_PROGRAM_ID( vm, cpi_instruction );
511 :
512 : /* Translate CPI account metas
513 : Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L582-L587
514 : C ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L718-L723 */
515 1530 : VM_SYSCALL_CPI_ACC_META_T const * cpi_account_metas =
516 2226 : FD_VM_MEM_SLICE_HADDR_LD( vm, VM_SYSCALL_CPI_INSTR_ACCS_ADDR( cpi_instruction ),
517 2226 : VM_SYSCALL_CPI_ACC_META_ALIGN,
518 2226 : fd_ulong_sat_mul( VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instruction ), VM_SYSCALL_CPI_ACC_META_SIZE ) );
519 :
520 : /* Translate instruction data
521 : Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L588-L593
522 : C ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L724 */
523 :
524 2226 : uchar const * data = FD_VM_MEM_SLICE_HADDR_LD(
525 2226 : vm, VM_SYSCALL_CPI_INSTR_DATA_ADDR( cpi_instruction ),
526 2226 : FD_VM_ALIGN_RUST_U8,
527 2226 : VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instruction ));
528 :
529 :
530 : /* Instruction checks
531 : Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L595
532 : C ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L726 */
533 :
534 2226 : int err = fd_vm_syscall_cpi_check_instruction( VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instruction ), VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instruction ) );
535 2226 : if( FD_UNLIKELY( err ) ) {
536 6 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, err );
537 6 : return err;
538 6 : }
539 :
540 : /* Agave consumes CU in translate_instruction
541 : Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L597-L599
542 : C ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L728-L730 */
543 1107 : ulong total_cu_translation_cost = VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instruction ) / FD_VM_CPI_BYTES_PER_UNIT;
544 :
545 : /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L601-L613
546 : C ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L732-L745 */
547 : /* Agave bills the same regardless of ABI */
548 1107 : ulong account_meta_translation_cost =
549 1107 : fd_ulong_sat_mul(
550 1107 : VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instruction ),
551 1107 : FD_VM_RUST_ACCOUNT_META_SIZE ) /
552 1107 : FD_VM_CPI_BYTES_PER_UNIT;
553 1107 : total_cu_translation_cost = fd_ulong_sat_add( total_cu_translation_cost, account_meta_translation_cost );
554 1107 : FD_VM_CU_UPDATE( vm, total_cu_translation_cost );
555 :
556 : /* Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L617-L629
557 : C ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L749-L767 */
558 2505 : for( ulong i=0UL; i<VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instruction ); i++ ) {
559 1398 : VM_SYSCALL_CPI_ACC_META_T const * cpi_acct_meta = &cpi_account_metas[i];
560 1398 : if( FD_UNLIKELY( cpi_acct_meta->is_signer > 1U || cpi_acct_meta->is_writable > 1U ) ) {
561 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_INVALID_ARG );
562 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
563 0 : }
564 : /* Rust ABI: no-op
565 : C ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L760-L761
566 : */
567 1953 : (void)VM_SYSCALL_CPI_ACC_META_PUBKEY( vm, cpi_acct_meta );
568 1953 : }
569 :
570 : /* Derive PDA signers
571 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L887-L893
572 : Rust ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L665-L707
573 : C ABI: https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L803-L845
574 :
575 : Note that we don't need any ABI-specific logic here, because the two ABIs are actually identical for the seeds.*/
576 1107 : fd_pubkey_t signers[ FD_CPI_MAX_SIGNER_CNT ] = {0};
577 1107 : fd_pubkey_t * caller_program_id = &vm->instr_ctx->txn_out->accounts.keys[ vm->instr_ctx->instr->program_id ];
578 1107 : if( FD_LIKELY( signers_seeds_cnt > 0UL ) ) {
579 108 : fd_vm_vec_t const * signers_seeds = FD_VM_MEM_SLICE_HADDR_LD( vm, signers_seeds_va, FD_VM_ALIGN_RUST_SLICE_U8_REF, fd_ulong_sat_mul( signers_seeds_cnt, FD_VM_VEC_SIZE ) );
580 108 : if( FD_UNLIKELY( signers_seeds_cnt > FD_CPI_MAX_SIGNER_CNT ) ) {
581 6 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_TOO_MANY_SIGNERS );
582 6 : return FD_VM_SYSCALL_ERR_TOO_MANY_SIGNERS;
583 6 : }
584 :
585 84 : for( ulong i=0UL; i<signers_seeds_cnt; i++ ) {
586 :
587 : /* This function will precompute the memory translation required and do
588 : some preflight checks. */
589 48 : void const * signer_seed_haddrs[ FD_VM_PDA_SEEDS_MAX ];
590 48 : ulong signer_seed_lens [ FD_VM_PDA_SEEDS_MAX ];
591 :
592 48 : int err = fd_vm_translate_and_check_program_address_inputs( vm,
593 48 : signers_seeds[i].addr,
594 48 : signers_seeds[i].len,
595 48 : 0UL,
596 48 : signer_seed_haddrs,
597 48 : signer_seed_lens ,
598 48 : NULL,
599 48 : 0U );
600 48 : if( FD_UNLIKELY( err ) ) {
601 6 : return err;
602 6 : }
603 :
604 42 : err = fd_vm_derive_pda( vm, caller_program_id, signer_seed_haddrs, signer_seed_lens, signers_seeds[i].len, NULL, &signers[i] );
605 42 : if( FD_UNLIKELY( err ) ) {
606 6 : FD_TXN_PREPARE_ERR_OVERWRITE( vm->instr_ctx->txn_out );
607 6 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_BAD_SEEDS );
608 6 : return FD_VM_SYSCALL_ERR_BAD_SEEDS;
609 6 : }
610 42 : }
611 48 : }
612 :
613 : /* Authorized program check
614 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L894 */
615 1089 : if( FD_UNLIKELY( !fd_vm_syscall_cpi_check_authorized_program( program_id, data, VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instruction ) ) ) ) {
616 6 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_PROGRAM_NOT_SUPPORTED );
617 6 : return FD_VM_SYSCALL_ERR_PROGRAM_NOT_SUPPORTED;
618 6 : }
619 :
620 : /* Create the instruction to execute (in the input format the FD runtime expects) from
621 : the translated CPI ABI inputs.
622 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L895 */
623 1083 : fd_pubkey_t cpi_instr_acct_keys[ FD_TXN_INSTR_ACCT_MAX ];
624 1083 : fd_instr_info_t * instruction_to_execute = &vm->instr_ctx->runtime->instr.trace[ vm->instr_ctx->runtime->instr.trace_length++ ];
625 :
626 1083 : err = VM_SYSCALL_CPI_INSTRUCTION_TO_INSTR_FUNC( vm, cpi_instruction, cpi_account_metas, program_id, data, instruction_to_execute, cpi_instr_acct_keys );
627 1083 : if( FD_UNLIKELY( err ) ) {
628 0 : return err;
629 0 : }
630 :
631 : /* Prepare the instruction for execution in the runtime. This is required by the runtime
632 : before we can pass an instruction to the executor. */
633 1083 : fd_instruction_account_t instruction_accounts[ FD_TXN_INSTR_ACCT_MAX ];
634 1083 : ulong instruction_accounts_cnt;
635 1083 : err = fd_vm_prepare_instruction( instruction_to_execute, vm->instr_ctx, program_id, cpi_instr_acct_keys, instruction_accounts, &instruction_accounts_cnt, signers, signers_seeds_cnt );
636 : /* Errors are propagated in the function itself. */
637 1083 : if( FD_UNLIKELY( err ) ) {
638 24 : return err;
639 24 : }
640 :
641 : /* Translate account infos
642 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L897-L903
643 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L987-L1047 */
644 :
645 : /* Verify that the account_infos array is not inside the input
646 : region. This prevents programs from passing pointers to
647 : the serialized account data region as account_infos, which would allow them to
648 : bypass pointer validation checks.
649 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1002-L1011 */
650 1059 : ulong acc_info_total_sz = fd_ulong_sat_mul( acct_info_cnt, VM_SYSCALL_CPI_ACC_INFO_SIZE );
651 1059 : if( FD_UNLIKELY( fd_ulong_sat_add( acct_infos_va, acc_info_total_sz ) >= FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
652 12 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
653 12 : return FD_VM_SYSCALL_ERR_INVALID_POINTER;
654 12 : }
655 :
656 : /* This is the equivalent of translate_slice in translate_account_infos */
657 2094 : VM_SYSCALL_CPI_ACC_INFO_T const * acc_infos = FD_VM_MEM_SLICE_HADDR_LD( vm, acct_infos_va, VM_SYSCALL_CPI_ACC_INFO_ALIGN, acc_info_total_sz );
658 :
659 : /* Right after translating, Agave checks the number of account infos */
660 2094 : if( FD_UNLIKELY( acct_info_cnt > get_cpi_max_account_infos() ) ) {
661 6 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNT_INFOS_EXCEEDED );
662 6 : return FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNT_INFOS_EXCEEDED;
663 6 : }
664 :
665 : /* Consume compute units proportional to the number of account infos */
666 1041 : ulong account_infos_bytes = fd_ulong_sat_mul( acct_info_cnt, FD_VM_ACCOUNT_INFO_BYTE_SIZE );
667 1041 : FD_VM_CU_UPDATE( vm, account_infos_bytes / FD_VM_CPI_BYTES_PER_UNIT );
668 :
669 1041 : fd_pubkey_t const * acct_info_keys[ FD_CPI_MAX_ACCOUNT_INFOS ];
670 2361 : for( ulong acct_idx = 0UL; acct_idx < acct_info_cnt; acct_idx++ ) {
671 : /* Translate each pubkey address specified in account_infos.
672 : Failed translation should lead to an access violation and
673 : implies that obviously bad account_info has been supplied. */
674 3960 : acct_info_keys[ acct_idx ] = FD_VM_MEM_HADDR_LD( vm, acc_infos[ acct_idx ].pubkey_addr, alignof(uchar), sizeof(fd_pubkey_t) );
675 3960 : }
676 :
677 : /* translate_accounts_common ***************************************************************
678 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L1049-L1193 */
679 1041 : fd_vm_cpi_translated_account_t translated_accounts[ FD_TXN_INSTR_ACCT_MAX ];
680 1041 : ulong translated_accounts_len = 0UL;
681 1041 : err = VM_SYSCALL_CPI_TRANSLATE_AND_UPDATE_ACCOUNTS_FUNC(
682 1041 : vm,
683 1041 : instruction_accounts,
684 1041 : instruction_accounts_cnt,
685 1041 : acct_infos_va,
686 1041 : acct_info_keys,
687 1041 : acc_infos,
688 1041 : acct_info_cnt,
689 1041 : translated_accounts,
690 1041 : &translated_accounts_len
691 1041 : );
692 : /* errors are propagated in the function itself. */
693 1041 : if( FD_UNLIKELY( err ) ) return err;
694 :
695 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L905-L928 */
696 2157 : for( ulong i=0UL; i<translated_accounts_len; i++ ) {
697 1194 : fd_vm_cpi_translated_account_t * translated_account = &translated_accounts[i];
698 1194 : fd_guarded_borrowed_account_t callee_acc = {0};
699 1194 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( vm->instr_ctx, translated_account->index_in_caller, &callee_acc );
700 1194 : uchar update_caller = 0;
701 1194 : err = VM_SYCALL_CPI_UPDATE_CALLEE_ACC_FUNC( vm, &translated_account->caller_account, &callee_acc, &update_caller );
702 1194 : if( FD_UNLIKELY( err ) ) {
703 0 : return err;
704 0 : }
705 1194 : translated_account->update_caller_account_region =
706 1194 : (uchar)( translated_account->update_caller_account_info || update_caller );
707 1194 : }
708 :
709 : /* Set the transaction compute meter to be the same as the VM's compute meter,
710 : so that the callee cannot use compute units that the caller has already used. */
711 963 : vm->instr_ctx->txn_out->details.compute_budget.compute_meter = vm->cu;
712 :
713 963 : long const regime1 = fd_tickcount();
714 :
715 : /* Execute the CPI instruction in the runtime */
716 963 : int err_exec = fd_execute_instr( vm->instr_ctx->runtime, vm->instr_ctx->bank, vm->instr_ctx->txn_in, vm->instr_ctx->txn_out, instruction_to_execute );
717 963 : ulong instr_exec_res = (ulong)err_exec;
718 :
719 963 : long const regime2 = fd_tickcount();
720 963 : vm->instr_ctx->runtime->metrics.cpi_setup_cum_ticks += (ulong)( regime1-regime0 );
721 :
722 : /* Set the CU meter to the instruction context's transaction context's compute meter,
723 : so that the caller can't use compute units that the callee has already used. */
724 963 : vm->cu = vm->instr_ctx->txn_out->details.compute_budget.compute_meter;
725 :
726 963 : vm->reg[0] = instr_exec_res;
727 :
728 : /* Errors are propagated in fd_execute_instr. */
729 963 : if( FD_UNLIKELY( err_exec ) ) return err_exec;
730 :
731 : /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L942-L957 */
732 1695 : for( ulong i=0UL; i<translated_accounts_len; i++ ) {
733 990 : fd_vm_cpi_translated_account_t * translated_account = &translated_accounts[i];
734 990 : fd_guarded_borrowed_account_t callee_acc = {0};
735 990 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( vm->instr_ctx, translated_account->index_in_caller, &callee_acc );
736 990 : if( !translated_account->update_caller_account_info ) continue;
737 894 : err = VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC( vm, &translated_account->caller_account, &callee_acc );
738 894 : if( FD_UNLIKELY( err ) ) {
739 54 : return err;
740 54 : }
741 894 : }
742 :
743 : /* Update the caller's memory regions to reflect any changes the
744 : callee made to account data.
745 : https://github.com/anza-xyz/agave/blob/v4.0.0-beta.7/program-runtime/src/cpi.rs#L959-L973 */
746 1635 : for( ulong i=0UL; i<translated_accounts_len; i++ ) {
747 930 : fd_vm_cpi_translated_account_t * translated_account = &translated_accounts[i];
748 930 : fd_guarded_borrowed_account_t borrowed_callee_acc = {0};
749 930 : err = fd_exec_instr_ctx_try_borrow_instr_account( vm->instr_ctx, translated_account->index_in_caller, &borrowed_callee_acc );
750 930 : if( FD_UNLIKELY( err ) ) return err;
751 930 : if( !translated_account->update_caller_account_region ) continue;
752 :
753 858 : err = fd_vm_cpi_update_caller_account_region( vm, translated_account, &borrowed_callee_acc );
754 858 : if( FD_UNLIKELY( err ) ) {
755 0 : return err;
756 0 : }
757 858 : }
758 :
759 705 : long const regime3 = fd_tickcount();
760 705 : vm->instr_ctx->runtime->metrics.cpi_commit_cum_ticks += (ulong)( regime3-regime2 );
761 :
762 705 : return FD_VM_SUCCESS;
763 705 : }
764 :
765 : #undef VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC
766 : #undef VM_SYSCALL_CPI_FROM_ACC_INFO_FUNC
767 : #undef VM_SYSCALL_CPI_TRANSLATE_AND_UPDATE_ACCOUNTS_FUNC
768 : #undef VM_SYSCALL_CPI_INSTRUCTION_TO_INSTR_FUNC
769 : #undef VM_SYSCALL_CPI_FUNC
|