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/v3.0.4/syscalls/src/cpi.rs#L21-L38
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 0 : if( FD_UNLIKELY( vm_addr!=expected_vm_addr )) { \
27 0 : fd_log_collector_printf_dangerous_max_127( vm->instr_ctx, \
28 0 : "Invalid account info pointer `%s': %#lx != %#lx", field_name, vm_addr, expected_vm_addr ); \
29 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER ); \
30 0 : return FD_VM_SYSCALL_ERR_INVALID_POINTER; \
31 0 : }
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 : - signers: derived signers for this CPI call
49 : - signers_cnt: length of the signers list
50 : - cpi_instr_data: instruction data in host address space
51 :
52 : TODO: return codes/errors?
53 : */
54 36 : #define VM_SYSCALL_CPI_INSTRUCTION_TO_INSTR_FUNC FD_EXPAND_THEN_CONCAT2(fd_vm_syscall_cpi_instruction_to_instr_, VM_SYSCALL_CPI_ABI)
55 : static int
56 : VM_SYSCALL_CPI_INSTRUCTION_TO_INSTR_FUNC( fd_vm_t * vm,
57 : VM_SYSCALL_CPI_INSTR_T const * cpi_instr,
58 : VM_SYSCALL_CPI_ACC_META_T const * cpi_acct_metas,
59 : fd_pubkey_t const * program_id,
60 : uchar const * cpi_instr_data,
61 : fd_instr_info_t * out_instr,
62 36 : fd_pubkey_t out_instr_acct_keys[ FD_VM_CPI_MAX_INSTRUCTION_ACCOUNTS ] ) {
63 :
64 36 : out_instr->program_id = UCHAR_MAX;
65 36 : out_instr->stack_height = vm->instr_ctx->runtime->instr.stack_sz+1;
66 36 : out_instr->data_sz = (ushort)VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instr );
67 36 : out_instr->acct_cnt = (ushort)VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instr );
68 36 : memcpy( out_instr->data, cpi_instr_data, out_instr->data_sz );
69 :
70 : /* Find the index of the CPI instruction's program account in the transaction */
71 36 : int program_id_idx = fd_runtime_find_index_of_account( vm->instr_ctx->txn_out, program_id );
72 36 : if( FD_LIKELY( program_id_idx != -1 ) ) {
73 36 : out_instr->program_id = (uchar)program_id_idx;
74 36 : }
75 :
76 36 : uchar acc_idx_seen[ FD_TXN_ACCT_ADDR_MAX ] = {0};
77 :
78 108 : for( ushort i=0; i<VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instr ); i++ ) {
79 72 : VM_SYSCALL_CPI_ACC_META_T const * cpi_acct_meta = &cpi_acct_metas[i];
80 216 : fd_pubkey_t const * pubkey = fd_type_pun_const( VM_SYSCALL_CPI_ACC_META_PUBKEY( vm, cpi_acct_meta ) );
81 0 : out_instr_acct_keys[i] = *pubkey;
82 :
83 : /* The parent flag(s) for is writable/signer is checked in
84 : fd_vm_prepare_instruction. Signer privilege is allowed iff the account
85 : is a signer in the caller or if it is a derived signer. */
86 : /* TODO: error if flags are wrong */
87 216 : out_instr->accounts[i] = fd_instruction_account_init( USHORT_MAX,
88 216 : USHORT_MAX,
89 216 : USHORT_MAX,
90 216 : VM_SYSCALL_CPI_ACC_META_IS_WRITABLE( cpi_acct_meta ),
91 216 : VM_SYSCALL_CPI_ACC_META_IS_SIGNER( cpi_acct_meta ) );
92 :
93 : /* Use USHORT_MAX to indicate account not found
94 : https://github.com/anza-xyz/agave/blob/v3.0.4/program-runtime/src/invoke_context.rs#L395-L397 */
95 216 : int idx_in_txn = fd_runtime_find_index_of_account( vm->instr_ctx->txn_out, pubkey );
96 216 : int idx_in_caller = fd_exec_instr_ctx_find_idx_of_instr_account( vm->instr_ctx, pubkey );
97 :
98 216 : fd_instr_info_setup_instr_account( out_instr,
99 216 : acc_idx_seen,
100 216 : idx_in_txn!=-1 ? (ushort)idx_in_txn : USHORT_MAX,
101 216 : idx_in_caller!=-1 ? (ushort)idx_in_caller : USHORT_MAX,
102 216 : i,
103 216 : VM_SYSCALL_CPI_ACC_META_IS_WRITABLE( cpi_acct_meta ),
104 216 : VM_SYSCALL_CPI_ACC_META_IS_SIGNER( cpi_acct_meta ) );
105 :
106 216 : }
107 :
108 36 : return FD_VM_SUCCESS;
109 36 : }
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/v3.0.4/syscalls/src/cpi.rs#L1067-L1132
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 : Paramaters:
126 : - vm: pointer to the virtual machine handle
127 : - account_info: account info object
128 : - callee_acc_pubkey: pubkey of the account. this is used to look up the account in the borrowed accounts cache
129 : (TODO: this seems redundant? we can probably remove this, as the account_info contains the pubkey)
130 : */
131 48 : #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 48 : uchar instr_acc_idx ) {
136 48 : int err;
137 :
138 : /* Borrow the callee account.
139 : TODO: Agave borrows before this function call. Consider refactoring to borrow the account at the same place as Agave.
140 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L817 */
141 48 : fd_guarded_borrowed_account_t callee_acc = {0};
142 48 : err = fd_exec_instr_ctx_try_borrow_instr_account( vm->instr_ctx, instr_acc_idx, &callee_acc );
143 48 : if( FD_UNLIKELY( err ) ) {
144 : /* No need to do anything if the account is missing from the borrowed accounts cache */
145 0 : return FD_VM_SUCCESS;
146 0 : }
147 :
148 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1087-L1089 */
149 48 : if( fd_borrowed_account_get_lamports( &callee_acc )!=*(caller_account->lamports) ) {
150 0 : err = fd_borrowed_account_set_lamports( &callee_acc, *(caller_account->lamports) );
151 0 : if( FD_UNLIKELY( err ) ) {
152 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, err );
153 0 : return -1;
154 0 : }
155 0 : }
156 :
157 : /* With stricter_abi_and_runtime_constraints enabled, we validate account
158 : length changes and update the associated borrowed account with any
159 : changed made. If direct mapping is also enabled, we skip actually copying
160 : the data back to the borrowed account, as it is already updated in-place.
161 :
162 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1091-L1113 */
163 48 : if( vm->stricter_abi_and_runtime_constraints ) {
164 0 : ulong prev_len = fd_borrowed_account_get_data_len( &callee_acc );
165 0 : ulong post_len = *caller_account->ref_to_len_in_vm;
166 :
167 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1094-L1109 */
168 0 : if( FD_UNLIKELY( prev_len!=post_len ) ) {
169 0 : ulong address_space_reserved_for_account;
170 0 : if( vm->is_deprecated ) {
171 0 : address_space_reserved_for_account = caller_account->orig_data_len;
172 0 : } else {
173 0 : address_space_reserved_for_account = fd_ulong_sat_add( caller_account->orig_data_len, MAX_PERMITTED_DATA_INCREASE );
174 0 : }
175 :
176 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1103-L1105 */
177 0 : if( FD_UNLIKELY( post_len>address_space_reserved_for_account ) ) {
178 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC );
179 0 : return -1;
180 0 : }
181 :
182 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1106 */
183 0 : err = fd_borrowed_account_set_data_length( &callee_acc, post_len );
184 0 : if( FD_UNLIKELY( err ) ) {
185 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, err );
186 0 : return -1;
187 0 : }
188 0 : }
189 :
190 : /* Without direct mapping, we need to copy the account data from the VM's
191 : serialized buffer back to the borrowed account. With direct mapping,
192 : data is modified in-place so no copy is needed.
193 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1110-L1112 */
194 0 : int err;
195 0 : if( !vm->direct_mapping && fd_borrowed_account_can_data_be_changed( &callee_acc, &err ) ) {
196 0 : err = fd_borrowed_account_set_data_from_slice( &callee_acc, caller_account->serialized_data, caller_account->serialized_data_len );
197 0 : if( FD_UNLIKELY( err ) ) {
198 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, err );
199 0 : return -1;
200 0 : }
201 0 : }
202 48 : } else {
203 : /* Direct mapping is not enabled, so we need to copy the account data
204 : from the VM's serialized buffer back to the borrowed account.
205 :
206 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1114-L1121 */
207 48 : int err;
208 48 : if( fd_borrowed_account_can_data_be_resized( &callee_acc, caller_account->serialized_data_len, &err ) &&
209 48 : fd_borrowed_account_can_data_be_changed( &callee_acc, &err ) ) {
210 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1116 */
211 48 : err = fd_borrowed_account_set_data_from_slice( &callee_acc, caller_account->serialized_data, caller_account->serialized_data_len );
212 48 : if( FD_UNLIKELY( err ) ) {
213 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, err );
214 0 : return -1;
215 0 : }
216 48 : } else if( FD_UNLIKELY( caller_account->serialized_data_len!=fd_borrowed_account_get_data_len( &callee_acc ) ||
217 0 : (caller_account->serialized_data_len &&
218 0 : memcmp( fd_borrowed_account_get_data( &callee_acc ), caller_account->serialized_data, caller_account->serialized_data_len )) ) ) {
219 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1117-L1119 */
220 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, err );
221 0 : return -1;
222 0 : }
223 48 : }
224 :
225 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1124-L1129 */
226 48 : if( FD_UNLIKELY( memcmp( fd_borrowed_account_get_owner( &callee_acc ), caller_account->owner, sizeof(fd_pubkey_t) ) ) ) {
227 0 : err = fd_borrowed_account_set_owner( &callee_acc, caller_account->owner );
228 0 : if( FD_UNLIKELY( err ) ) {
229 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, err );
230 0 : return -1;
231 0 : }
232 0 : }
233 :
234 48 : return FD_VM_SUCCESS;
235 48 : }
236 :
237 : /*
238 : fd_vm_syscall_cpi_translate_and_update_accounts_ mirrors the behaviour of
239 : solana_bpf_loader_program::syscalls::cpi::translate_and_update_accounts:
240 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L767-L892
241 :
242 : It translates the caller accounts to the host address space, and then calls
243 : fd_vm_syscall_cpi_update_callee_acc to update the callee borrowed account with any changes
244 : the caller has made to the account during execution before this CPI call.
245 :
246 : It also populates the out_callee_indices and out_caller_indices arrays:
247 : - out_callee_indices: indices of the callee accounts in the transaction
248 : - out_caller_indices: indices of the caller accounts in the account_infos array
249 :
250 : Parameters:
251 : - vm: pointer to the virtual machine handle
252 : - instruction_accounts: array of instruction accounts
253 : - instruction_accounts_cnt: length of the instruction_accounts array
254 : - account_infos: array of account infos
255 : - account_infos_length: length of the account_infos array
256 :
257 : Populates the given out_callee_indices and out_caller_indices arrays:
258 : - out_callee_indices: indices of the callee accounts in the transaction
259 : - out_caller_indices: indices of the caller accounts in the account_infos array
260 : - out_len: length of the out_callee_indices and out_caller_indices arrays
261 : */
262 24 : #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)
263 : static int
264 : VM_SYSCALL_CPI_TRANSLATE_AND_UPDATE_ACCOUNTS_FUNC(
265 : fd_vm_t * vm,
266 : fd_instruction_account_t const * instruction_accounts,
267 : ulong const instruction_accounts_cnt,
268 : ulong acct_infos_va,
269 : fd_pubkey_t const * * account_info_keys, /* same length as account_infos_length */
270 : VM_SYSCALL_CPI_ACC_INFO_T const * account_infos,
271 : ulong const account_infos_length,
272 : ushort * out_callee_indices,
273 : ushort * out_caller_indices,
274 : fd_vm_cpi_caller_account_t * caller_accounts,
275 24 : ulong * out_len ) {
276 72 : for( ulong i=0UL; i<instruction_accounts_cnt; i++ ) {
277 48 : if( i!=instruction_accounts[i].index_in_callee ) {
278 : /* Skip duplicate accounts */
279 0 : continue;
280 0 : }
281 :
282 : /* `fd_vm_prepare_instruction()` will always set up a valid index for `index_in_caller`, so we can access the borrowed account directly.
283 : A borrowed account will always have non-NULL meta (if the account doesn't exist, `fd_executor_setup_accounts_for_txn()`
284 : will set its meta up) */
285 :
286 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L817 */
287 48 : fd_guarded_borrowed_account_t callee_acct = {0};
288 48 : FD_TRY_BORROW_INSTR_ACCOUNT_DEFAULT_ERR_CHECK( vm->instr_ctx, instruction_accounts[i].index_in_caller, &callee_acct );
289 :
290 48 : fd_pubkey_t const * account_key = callee_acct.pubkey;
291 48 : fd_account_meta_t const * acc_meta = fd_borrowed_account_get_acc_meta( &callee_acct );
292 :
293 : /* If the account is known and executable, we only need to consume the compute units.
294 : Executable accounts can't be modified, so we don't need to update the callee account. */
295 48 : if( fd_borrowed_account_is_executable( &callee_acct ) ) {
296 : // FIXME: should this be FD_VM_CU_MEM_UPDATE? Changing this changes the CU behaviour from main (because of the base cost)
297 0 : FD_VM_CU_UPDATE( vm, acc_meta->dlen / FD_VM_CPI_BYTES_PER_UNIT );
298 0 : continue;
299 0 : }
300 :
301 : /* FIXME: we should not need to drop the account here to avoid a double borrow.
302 : Instead, we should borrow the account before entering this function. */
303 48 : fd_borrowed_account_drop( &callee_acct );
304 :
305 : /* Find the indicies of the account in the caller and callee instructions */
306 48 : uint found = 0;
307 120 : for( ushort j=0; j<account_infos_length && !found; j++ ) {
308 72 : fd_pubkey_t const * acct_addr = account_info_keys[ j ];
309 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L832
310 : */
311 72 : if( memcmp( account_key->uc, acct_addr->uc, sizeof(fd_pubkey_t) ) != 0 ) {
312 24 : continue;
313 24 : }
314 :
315 : /* The next iteration will overwrite this if it turns out that we
316 : do not need to preserve this for update_caller().
317 : */
318 48 : fd_vm_cpi_caller_account_t * caller_account = caller_accounts + *out_len;
319 : /* Record the indicies of this account */
320 48 : ushort index_in_caller = instruction_accounts[i].index_in_caller;
321 48 : if( vm->stricter_abi_and_runtime_constraints || instruction_accounts[i].is_writable ) {
322 48 : out_callee_indices[*out_len] = index_in_caller;
323 48 : out_caller_indices[*out_len] = j;
324 48 : (*out_len)++;
325 48 : }
326 48 : found = 1;
327 :
328 : /* Logically this check isn't ever going to fail due to how the
329 : account_info_keys array is set up. We replicate the check for
330 : clarity and also to guard against accidental violation of the
331 : assumed invariant in the future.
332 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L846-L849
333 : */
334 48 : if( FD_UNLIKELY( j >= account_infos_length ) ) {
335 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_LENGTH );
336 0 : return FD_VM_SYSCALL_ERR_INVALID_LENGTH;
337 0 : }
338 :
339 : /* The following implements the checks in from_account_info which
340 : is invoked as do_translate() in translate_and_update_accounts()
341 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L850-L861
342 : */
343 : ////// BEGIN from_account_info
344 :
345 48 : fd_vm_acc_region_meta_t * acc_region_meta = &vm->acc_region_metas[index_in_caller];
346 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L138 */
347 48 : if( FD_LIKELY( vm->stricter_abi_and_runtime_constraints ) ) {
348 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L139-L144 */
349 0 : ulong expected_pubkey_vaddr = FD_VM_MEM_MAP_INPUT_REGION_START +
350 0 : vm->input_mem_regions[acc_region_meta->region_idx].vaddr_offset +
351 0 : acc_region_meta->expected_pubkey_offset;
352 : /* Max msg_sz: 40 + 18 + 18 = 76 < 127 */
353 0 : VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(vm, account_infos[j].pubkey_addr, expected_pubkey_vaddr, "key");
354 :
355 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L145-L150 */
356 0 : ulong expected_owner_vaddr = FD_VM_MEM_MAP_INPUT_REGION_START +
357 0 : vm->input_mem_regions[acc_region_meta->region_idx].vaddr_offset +
358 0 : acc_region_meta->expected_owner_offset;
359 : /* Max msg_sz: 42 + 18 + 18 = 78 < 127 */
360 0 : VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(vm, account_infos[j].owner_addr, expected_owner_vaddr, "owner");
361 0 : }
362 :
363 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L155-L175 */
364 48 : VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_VADDR( vm, (account_infos + j), lamports_vaddr );
365 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L162-L173 */
366 48 : if( FD_LIKELY( vm->stricter_abi_and_runtime_constraints ) ) {
367 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L163-L165
368 : Check that the account's lamports Rc<RefCell<&mut u64>> is not
369 : stored in the account region. Because a refcell is only present if
370 : the Rust SDK is used, we only need to check this for the Rust ABI. */
371 : #ifdef VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_RC_REFCELL_VADDR
372 0 : VM_SYSCALL_CPI_ACC_INFO_LAMPORTS_RC_REFCELL_VADDR( vm, (account_infos + j), lamports_rc_vaddr )
373 0 : if ( FD_UNLIKELY( lamports_rc_vaddr >= FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
374 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
375 0 : return FD_VM_SYSCALL_ERR_INVALID_POINTER;
376 0 : }
377 0 : #endif
378 :
379 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L167-L172 */
380 0 : ulong expected_lamports_vaddr = FD_VM_MEM_MAP_INPUT_REGION_START +
381 0 : vm->input_mem_regions[acc_region_meta->region_idx].vaddr_offset +
382 0 : acc_region_meta->expected_lamports_offset;
383 : /* Max msg_sz: 45 + 18 + 18 = 81 < 127 */
384 0 : VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(vm, lamports_vaddr, expected_lamports_vaddr, "lamports");
385 0 : }
386 :
387 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L153-L175
388 : */
389 96 : VM_SYSCALL_CPI_ACC_INFO_LAMPORTS( vm, (account_infos + j), lamports_haddr );
390 96 : caller_account->lamports = lamports_haddr;
391 :
392 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L177-L181
393 : */
394 96 : caller_account->owner = FD_VM_MEM_HADDR_ST( vm, (account_infos + j)->owner_addr, alignof(uchar), sizeof(fd_pubkey_t) );
395 :
396 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L190-L203
397 : */
398 48 : VM_SYSCALL_CPI_ACC_INFO_DATA_VADDR( vm, (account_infos + j), data_vaddr );
399 :
400 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L196-L203 */
401 48 : if( vm->stricter_abi_and_runtime_constraints ) {
402 0 : fd_vm_input_region_t * region = &vm->input_mem_regions[ acc_region_meta->region_idx ];
403 0 : ulong expected_data_vaddr = FD_VM_MEM_MAP_INPUT_REGION_START +
404 0 : region->vaddr_offset + region->address_space_reserved;
405 0 : VM_SYSCALL_CPI_CHECK_ACCOUNT_INFO_POINTER_FIELD_MAX_54(vm, data_vaddr, expected_data_vaddr, "data");
406 0 : }
407 :
408 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L205-L210
409 : */
410 48 : VM_SYSCALL_CPI_SET_ACC_INFO_DATA_GET_LEN( vm, (account_infos + j), data_vaddr );
411 48 : FD_VM_CU_UPDATE( vm, data_vaddr_len / FD_VM_CPI_BYTES_PER_UNIT );
412 :
413 : #ifdef VM_SYSCALL_CPI_ACC_INFO_DATA_LEN_VADDR
414 : /* Rust ABI
415 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L212-L221 */
416 0 : VM_SYSCALL_CPI_ACC_INFO_DATA_LEN_VADDR( vm, (account_infos + j), data_len_vaddr );
417 0 : if( FD_UNLIKELY( vm->stricter_abi_and_runtime_constraints && data_len_vaddr >= FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
418 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
419 0 : return FD_VM_SYSCALL_ERR_INVALID_POINTER;
420 0 : }
421 0 : (void)acct_infos_va;
422 : #else
423 : /* C ABI
424 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L310-L316 */
425 0 : ulong data_len_vaddr = vm_syscall_cpi_data_len_vaddr_c(
426 48 : fd_ulong_sat_add( acct_infos_va, fd_ulong_sat_mul( j, VM_SYSCALL_CPI_ACC_INFO_SIZE ) ),
427 : (ulong)&((account_infos + j)->data_sz),
428 : (ulong)(account_infos + j)
429 : );
430 : #endif
431 :
432 : /* Rust ABI: https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L226
433 : C ABI: https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L324 */
434 0 : caller_account->vm_data_vaddr = data_vaddr;
435 :
436 : /* Rust ABI: https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L224-L230
437 : C ABI: https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L302-L308
438 :
439 : Both ABIs call CallerAccount::get_serialized_data:
440 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L90-L123
441 :
442 : With both stricter_abi_and_runtime_constraints and direct_mapping,
443 : account data is modified in-place so we don't track the
444 : serialized_data pointer.
445 :
446 : With stricter_abi only (no direct_mapping), data was copied into the input
447 : region buffer. We don't apply the extra memory translation checks, as
448 : we have checked the data pointer is valid above. So instead we add
449 : the vaddr to the start of the input region address space - copying
450 : this logic from Agave.
451 :
452 : In legacy mode, we translate the data pointer directly, as it just
453 : maps to a location in the single input region. */
454 48 : if( vm->stricter_abi_and_runtime_constraints && vm->direct_mapping ) {
455 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L97-L99 */
456 0 : caller_account->serialized_data = NULL;
457 0 : caller_account->serialized_data_len = 0UL;
458 48 : } else if( vm->stricter_abi_and_runtime_constraints ) {
459 : /* Skip translation checks here, following the Agave logic:
460 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L99-L115 */
461 0 : uchar * serialization_ptr = (uchar *)FD_VM_MEM_SLICE_HADDR_ST( vm, FD_VM_MEM_MAP_INPUT_REGION_START, alignof(uchar), 1UL );
462 0 : caller_account->serialized_data = serialization_ptr + fd_ulong_sat_sub( data_vaddr, FD_VM_MEM_MAP_INPUT_REGION_START );
463 0 : caller_account->serialized_data_len = data_vaddr_len;
464 48 : } else {
465 : /* https://github.com/anza-xyz/agave/blob/v3.0.1/syscalls/src/cpi.rs#L115-L122 */
466 96 : VM_SYSCALL_CPI_ACC_INFO_DATA( vm, (account_infos + j), data_haddr );
467 96 : (void)data_haddr_vm_addr;
468 96 : caller_account->serialized_data = data_haddr;
469 96 : caller_account->serialized_data_len = data_haddr_len;
470 96 : }
471 :
472 : /* Rust ABI: https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L237
473 : C ABI: https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L322 */
474 48 : caller_account->orig_data_len = acc_region_meta->original_data_len;
475 :
476 : /* Rust ABI: https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L222
477 : C ABI: https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L317 */
478 48 : ulong * data_len = FD_VM_MEM_HADDR_ST( vm, data_len_vaddr, 1UL, sizeof(ulong) );
479 0 : caller_account->ref_to_len_in_vm = data_len;
480 :
481 : ////// END from_account_info
482 :
483 : // TODO We should be able to cache the results of translation and reuse them in the update function.
484 : /* Update the callee account to reflect any changes the caller has made.
485 : This code is split out under stricter_abi_and_runtime_constraints
486 : https://github.com/anza-xyz/agave/blob/v3.1.0-beta.0/program-runtime/src/cpi.rs#L1092-L1106 */
487 48 : if( !vm->stricter_abi_and_runtime_constraints ) {
488 48 : int err = VM_SYCALL_CPI_UPDATE_CALLEE_ACC_FUNC( vm, caller_account, (uchar)index_in_caller );
489 48 : if( FD_UNLIKELY( err ) ) {
490 0 : return err;
491 0 : }
492 48 : }
493 48 : }
494 :
495 48 : if( !found ) {
496 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L882-L887 */
497 0 : FD_BASE58_ENCODE_32_BYTES( account_key->uc, id_b58 );
498 0 : fd_log_collector_msg_many( vm->instr_ctx, 2, "Instruction references an unknown account ", 42UL, id_b58, id_b58_len );
499 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_MISSING_ACC );
500 0 : return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
501 0 : }
502 48 : }
503 :
504 24 : return FD_VM_SUCCESS;
505 24 : }
506 :
507 : /* fd_vm_cpi_update_caller_acc_{rust/c} mirrors the behaviour of
508 : solana_bpf_loader_program::syscalls::cpi::update_caller_account:
509 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1171-L1268
510 :
511 : This method should be called after a CPI instruction execution has
512 : returned. It updates the given caller account info with any changes the callee
513 : has made to this account during execution, so that those changes are
514 : reflected in the rest of the caller's execution.
515 :
516 : Those changes will be in the instructions borrowed accounts cache.
517 :
518 : Paramaters:
519 : - vm: handle to the vm
520 : - caller_acc_info: caller account info object, which should be updated
521 : - pubkey: pubkey of the account
522 :
523 : TODO: error codes
524 : */
525 48 : #define VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC FD_EXPAND_THEN_CONCAT2(fd_vm_cpi_update_caller_acc_, VM_SYSCALL_CPI_ABI)
526 : static int
527 : VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC( fd_vm_t * vm,
528 : VM_SYSCALL_CPI_ACC_INFO_T const * caller_acc_info FD_FN_UNUSED,
529 : fd_vm_cpi_caller_account_t * caller_account,
530 : uchar instr_acc_idx FD_FN_UNUSED,
531 48 : fd_pubkey_t const * pubkey ) {
532 48 : int err;
533 :
534 : /* Look up the borrowed account from the instruction context, which will contain
535 : the callee's changes.
536 : TODO: Agave borrows before entering this function. We should consider doing the same.
537 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1033-L1034 */
538 48 : fd_guarded_borrowed_account_t borrowed_callee_acc = {0};
539 48 : err = fd_exec_instr_ctx_try_borrow_instr_account_with_key( vm->instr_ctx, pubkey, &borrowed_callee_acc );
540 48 : if( FD_UNLIKELY( err && ( err != FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT ) ) ) {
541 0 : return 1;
542 0 : }
543 :
544 48 : fd_account_meta_t * callee_meta = borrowed_callee_acc.meta;
545 : /* Update the caller account lamports with the value from the callee
546 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1191 */
547 48 : *(caller_account->lamports) = callee_meta->lamports;
548 :
549 : /* Update the caller account owner with the value from the callee
550 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1192 */
551 48 : fd_pubkey_t const * updated_owner = (fd_pubkey_t const *)callee_meta->owner;
552 48 : if( updated_owner ) *caller_account->owner = *updated_owner;
553 0 : else fd_memset( caller_account->owner, 0, sizeof(fd_pubkey_t) );
554 :
555 : /* Update the caller account data with the value from the callee
556 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1194-L1195 */
557 48 : ulong prev_len = *caller_account->ref_to_len_in_vm;
558 48 : ulong post_len = callee_meta->dlen;
559 :
560 : /* Calculate the address space reserved for the account. With stricter_abi_and_runtime_constraints
561 : and deprecated loader, the reserved space equals original length (no realloc space).
562 : Otherwise, we add MAX_PERMITTED_DATA_INCREASE for reallocation.
563 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1197-L1204 */
564 48 : ulong address_space_reserved_for_account;
565 48 : if( vm->stricter_abi_and_runtime_constraints && vm->is_deprecated ) {
566 0 : address_space_reserved_for_account = caller_account->orig_data_len;
567 48 : } else {
568 48 : address_space_reserved_for_account = fd_ulong_sat_add( caller_account->orig_data_len, MAX_PERMITTED_DATA_INCREASE );
569 48 : }
570 :
571 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1206-L1216 */
572 48 : if( post_len > address_space_reserved_for_account &&
573 48 : ( vm->stricter_abi_and_runtime_constraints || prev_len != post_len ) ) {
574 0 : ulong max_increase = fd_ulong_sat_sub( address_space_reserved_for_account, caller_account->orig_data_len );
575 0 : fd_log_collector_printf_dangerous_max_127( vm->instr_ctx, "Account data size realloc limited to %lu in inner instructions", max_increase );
576 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC );
577 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC;
578 0 : }
579 :
580 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1218-L1252 */
581 48 : if( prev_len != post_len ) {
582 :
583 : /* Without direct mapping, we need to adjust the serialized data buffer
584 : when the length changes.
585 :
586 : With direct mapping, data is mapped in-place so no buffer manipulation
587 : is needed.
588 :
589 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1219-L1239 */
590 0 : if( !( vm->stricter_abi_and_runtime_constraints && vm->direct_mapping ) ) {
591 :
592 : /* If the account has shrunk, zero out memory that was previously used
593 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1222-L1230 */
594 0 : if( post_len < prev_len ) {
595 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1227-L1228 */
596 0 : if( caller_account->serialized_data_len < post_len ) {
597 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL );
598 0 : return FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL;
599 0 : }
600 :
601 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1225-L1229 */
602 0 : fd_memset( caller_account->serialized_data + post_len, 0, caller_account->serialized_data_len - post_len );
603 0 : }
604 :
605 : /* Set caller_account.serialized_data to post_len.
606 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1231-L1238 */
607 0 : if( vm->stricter_abi_and_runtime_constraints ) {
608 : /* Calculate the serialized data pointer from the input region base,
609 : as described above.
610 :
611 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L99-L115 */
612 0 : uchar * serialization_ptr = (uchar *)FD_VM_MEM_SLICE_HADDR_ST( vm, FD_VM_MEM_MAP_INPUT_REGION_START, alignof(uchar), 1UL );
613 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1234 */
614 0 : caller_account->serialized_data = serialization_ptr + fd_ulong_sat_sub( caller_account->vm_data_vaddr, FD_VM_MEM_MAP_INPUT_REGION_START );
615 0 : caller_account->serialized_data_len = post_len;
616 0 : } else {
617 : /* Translate the data pointer directly from the VM address, if
618 : stricter_abi_and_runtime_constraints (or direct mapping) is not
619 : enabled.
620 :
621 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L115-L122 */
622 0 : caller_account->serialized_data = (uchar *)FD_VM_MEM_SLICE_HADDR_ST( vm, caller_account->vm_data_vaddr, alignof(uchar), post_len );
623 0 : caller_account->serialized_data_len = post_len;
624 0 : }
625 0 : }
626 :
627 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1240-L1241 */
628 0 : *caller_account->ref_to_len_in_vm = post_len;
629 :
630 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1243-L1251 */
631 0 : ulong * caller_len = FD_VM_MEM_HADDR_ST( vm, fd_ulong_sat_sub(caller_account->vm_data_vaddr, sizeof(ulong)), alignof(ulong), sizeof(ulong) );
632 0 : *caller_len = post_len;
633 0 : }
634 :
635 : /* Without direct mapping, copy the updated account data from the callee's
636 : account back to the caller's serialized data buffer. With direct mapping,
637 : data was modified in-place so no copy is needed.
638 :
639 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1254-L1265 */
640 48 : if( !(vm->stricter_abi_and_runtime_constraints && vm->direct_mapping) ) {
641 48 : fd_memcpy( caller_account->serialized_data, fd_account_data( callee_meta ), post_len );
642 48 : }
643 :
644 :
645 48 : return FD_VM_SUCCESS;
646 48 : }
647 :
648 : /* fd_vm_syscall_cpi_{rust/c} is the entrypoint for the sol_invoke_signed_{rust/c} syscalls.
649 :
650 : The bulk of the high-level logic mirrors Solana's cpi_common entrypoint function at
651 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L964-L1065
652 : The only differences should be in the order of the error checks, which does not affect consensus.
653 :
654 : 100-foot flow:
655 : - Translate the CPI ABI structures to the FD runtime's instruction format
656 : - Update the callee accounts with any changes made by the caller prior to this CPI instruction
657 : - Dispatch the instruction to the FD runtime (actually making the CPI call)
658 : - Update the caller accounts with any changes made by the callee during CPI execution
659 :
660 : Paramaters:
661 : - vm: pointer to the virtual machine handle
662 : - instruction_va: vm address of the instruction to execute, which will be in the language-specific ABI format.
663 : - acct_infos_va: vm address of the account infos, which will be in the language-specific ABI format.
664 : - acct_info_cnt: number of account infos
665 : - signers_seeds_va: vm address of the signers seeds
666 : - signers_seeds_cnt: number of signers seeds
667 : - _ret: pointer to the return value
668 : */
669 : #define VM_SYSCALL_CPI_ENTRYPOINT FD_EXPAND_THEN_CONCAT2(fd_vm_syscall_cpi_, VM_SYSCALL_CPI_ABI)
670 : int
671 : VM_SYSCALL_CPI_ENTRYPOINT( void * _vm,
672 : ulong instruction_va,
673 : ulong acct_infos_va,
674 : ulong acct_info_cnt,
675 : ulong signers_seeds_va,
676 : ulong signers_seeds_cnt,
677 36 : ulong * _ret ) {
678 36 : long const regime0 = fd_tickcount();
679 :
680 36 : fd_vm_t * vm = (fd_vm_t *)_vm;
681 :
682 : /* https://github.com/anza-xyz/agave/blob/v3.1.2/program-runtime/src/cpi.rs#L815-L818 */
683 36 : FD_VM_CU_UPDATE( vm, get_cpi_invoke_unit_cost( vm->instr_ctx->bank ) );
684 :
685 : /* Translate instruction ********************************************/
686 : /* translate_instruction is the first thing that agave does
687 : https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/programs/bpf_loader/src/syscalls/cpi.rs#L1089 */
688 :
689 : /* Translating the CPI instruction
690 : https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/programs/bpf_loader/src/syscalls/cpi.rs#L420-L424 */
691 36 : VM_SYSCALL_CPI_INSTR_T const * cpi_instruction =
692 108 : FD_VM_MEM_HADDR_LD( vm, instruction_va, VM_SYSCALL_CPI_INSTR_ALIGN, VM_SYSCALL_CPI_INSTR_SIZE );
693 :
694 : /* This needs to be here for the C ABI
695 : https://github.com/anza-xyz/agave/blob/v2.1.6/programs/bpf_loader/src/syscalls/cpi.rs#L655
696 : */
697 108 : fd_pubkey_t const * program_id = (fd_pubkey_t *)VM_SYSCALL_CPI_INSTR_PROGRAM_ID( vm, cpi_instruction );
698 :
699 : /* Translate CPI account metas *************************************************/
700 36 : VM_SYSCALL_CPI_ACC_META_T const * cpi_account_metas =
701 108 : FD_VM_MEM_SLICE_HADDR_LD( vm, VM_SYSCALL_CPI_INSTR_ACCS_ADDR( cpi_instruction ),
702 72 : VM_SYSCALL_CPI_ACC_META_ALIGN,
703 72 : fd_ulong_sat_mul( VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instruction ), VM_SYSCALL_CPI_ACC_META_SIZE ) );
704 :
705 : /* Translate instruction data *************************************************/
706 :
707 72 : uchar const * data = FD_VM_MEM_SLICE_HADDR_LD(
708 72 : vm, VM_SYSCALL_CPI_INSTR_DATA_ADDR( cpi_instruction ),
709 72 : FD_VM_ALIGN_RUST_U8,
710 72 : VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instruction ));
711 :
712 :
713 : /* Instruction checks ***********************************************/
714 :
715 36 : int err = fd_vm_syscall_cpi_check_instruction( VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instruction ), VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instruction ) );
716 72 : if( FD_UNLIKELY( err ) ) {
717 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, err );
718 0 : return err;
719 0 : }
720 :
721 : /* Agave consumes CU in translate_instruction
722 : https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/programs/bpf_loader/src/syscalls/cpi.rs#L445 */
723 36 : ulong total_cu_translation_cost = VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instruction ) / FD_VM_CPI_BYTES_PER_UNIT;
724 :
725 : /* https://github.com/anza-xyz/agave/blob/v3.1.2/program-runtime/src/cpi.rs#L686-L699 */
726 36 : if( FD_FEATURE_ACTIVE_BANK( vm->instr_ctx->bank, increase_cpi_account_info_limit ) ) {
727 : /* Agave bills the same regardless of ABI */
728 18 : ulong account_meta_translation_cost =
729 18 : fd_ulong_sat_mul(
730 18 : VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instruction ),
731 18 : FD_VM_RUST_ACCOUNT_META_SIZE ) /
732 18 : FD_VM_CPI_BYTES_PER_UNIT;
733 18 : total_cu_translation_cost = fd_ulong_sat_add( total_cu_translation_cost, account_meta_translation_cost );
734 18 : }
735 :
736 36 : FD_VM_CU_UPDATE( vm, total_cu_translation_cost );
737 :
738 : /* Final checks for translate_instruction
739 : */
740 108 : for( ulong i=0UL; i<VM_SYSCALL_CPI_INSTR_ACCS_LEN( cpi_instruction ); i++ ) {
741 72 : VM_SYSCALL_CPI_ACC_META_T const * cpi_acct_meta = &cpi_account_metas[i];
742 72 : if( FD_UNLIKELY( cpi_acct_meta->is_signer > 1U || cpi_acct_meta->is_writable > 1U ) ) {
743 : /* https://github.com/anza-xyz/agave/blob/v2.1.6/programs/bpf_loader/src/syscalls/cpi.rs#L471
744 : https://github.com/anza-xyz/agave/blob/v2.1.6/programs/bpf_loader/src/syscalls/cpi.rs#L698
745 : */
746 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, FD_EXECUTOR_INSTR_ERR_INVALID_ARG );
747 0 : return FD_EXECUTOR_INSTR_ERR_INVALID_ARG;
748 0 : }
749 : /* https://github.com/anza-xyz/agave/blob/v2.1.6/programs/bpf_loader/src/syscalls/cpi.rs#L700
750 : */
751 216 : (void)VM_SYSCALL_CPI_ACC_META_PUBKEY( vm, cpi_acct_meta );
752 216 : }
753 :
754 : /* Derive PDA signers ************************************************/
755 36 : fd_pubkey_t signers[ FD_CPI_MAX_SIGNER_CNT ] = {0};
756 36 : fd_pubkey_t * caller_program_id = &vm->instr_ctx->txn_out->accounts.keys[ vm->instr_ctx->instr->program_id ];
757 : /* This is the equivalent of translate_slice in translate_signers:
758 : https://github.com/solana-labs/solana/blob/dbf06e258ae418097049e845035d7d5502fe1327/programs/bpf_loader/src/syscalls/cpi.rs#L595 */
759 36 : if( FD_LIKELY( signers_seeds_cnt > 0UL ) ) {
760 0 : 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 ) );
761 : /* Right after translating, Agave checks against MAX_SIGNERS:
762 : https://github.com/solana-labs/solana/blob/dbf06e258ae418097049e845035d7d5502fe1327/programs/bpf_loader/src/syscalls/cpi.rs#L602 */
763 0 : if( FD_UNLIKELY( signers_seeds_cnt > FD_CPI_MAX_SIGNER_CNT ) ) {
764 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_TOO_MANY_SIGNERS );
765 0 : return FD_VM_SYSCALL_ERR_TOO_MANY_SIGNERS;
766 0 : }
767 :
768 0 : for( ulong i=0UL; i<signers_seeds_cnt; i++ ) {
769 :
770 : /* This function will precompute the memory translation required and do
771 : some preflight checks. */
772 0 : void const * signer_seed_haddrs[ FD_VM_PDA_SEEDS_MAX ];
773 0 : ulong signer_seed_lens [ FD_VM_PDA_SEEDS_MAX ];
774 :
775 0 : int err = fd_vm_translate_and_check_program_address_inputs( vm,
776 0 : signers_seeds[i].addr,
777 0 : signers_seeds[i].len,
778 0 : 0UL,
779 0 : signer_seed_haddrs,
780 0 : signer_seed_lens ,
781 0 : NULL,
782 0 : 0U );
783 0 : if( FD_UNLIKELY( err ) ) {
784 0 : return err;
785 0 : }
786 :
787 0 : err = fd_vm_derive_pda( vm, caller_program_id, signer_seed_haddrs, signer_seed_lens, signers_seeds[i].len, NULL, &signers[i] );
788 0 : if( FD_UNLIKELY( err ) ) {
789 0 : FD_TXN_PREPARE_ERR_OVERWRITE( vm->instr_ctx->txn_out );
790 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_BAD_SEEDS );
791 0 : return FD_VM_SYSCALL_ERR_BAD_SEEDS;
792 0 : }
793 0 : }
794 0 : }
795 :
796 : /* Create the instruction to execute (in the input format the FD runtime expects) from
797 : the translated CPI ABI inputs. */
798 36 : fd_pubkey_t cpi_instr_acct_keys[ FD_VM_CPI_MAX_INSTRUCTION_ACCOUNTS ];
799 36 : fd_instr_info_t * instruction_to_execute = &vm->instr_ctx->runtime->instr.trace[ vm->instr_ctx->runtime->instr.trace_length++ ];
800 :
801 36 : err = VM_SYSCALL_CPI_INSTRUCTION_TO_INSTR_FUNC( vm, cpi_instruction, cpi_account_metas, program_id, data, instruction_to_execute, cpi_instr_acct_keys );
802 36 : if( FD_UNLIKELY( err ) ) {
803 0 : return err;
804 0 : }
805 :
806 : /* Authorized program check *************************************************/
807 :
808 36 : if( FD_UNLIKELY( !fd_vm_syscall_cpi_check_authorized_program( program_id, vm->instr_ctx->bank, data, VM_SYSCALL_CPI_INSTR_DATA_LEN( cpi_instruction ) ) ) ) {
809 : /* https://github.com/solana-labs/solana/blob/2afde1b028ed4593da5b6c735729d8994c4bfac6/programs/bpf_loader/src/syscalls/cpi.rs#L1054 */
810 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_PROGRAM_NOT_SUPPORTED );
811 0 : return FD_VM_SYSCALL_ERR_PROGRAM_NOT_SUPPORTED;
812 0 : }
813 :
814 : /* Prepare the instruction for execution in the runtime. This is required by the runtime
815 : before we can pass an instruction to the executor. */
816 36 : fd_instruction_account_t instruction_accounts[ FD_VM_CPI_MAX_INSTRUCTION_ACCOUNTS ];
817 36 : ulong instruction_accounts_cnt;
818 36 : 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 );
819 : /* Errors are propagated in the function itself. */
820 36 : if( FD_UNLIKELY( err ) ) {
821 0 : return err;
822 0 : }
823 :
824 : /* Translate account infos ******************************************/
825 :
826 : /* With stricter_abi_and_runtime_constraints, verify that the account_infos array
827 : is not inside the input region. This prevents programs from passing pointers to
828 : the serialized account data region as account_infos, which would allow them to
829 : bypass pointer validation checks.
830 : https://github.com/anza-xyz/agave/blob/v3.0.1/syscalls/src/cpi.rs#L735-L744 */
831 36 : ulong acc_info_total_sz = fd_ulong_sat_mul( acct_info_cnt, VM_SYSCALL_CPI_ACC_INFO_SIZE );
832 36 : if( vm->stricter_abi_and_runtime_constraints ) {
833 0 : if( FD_UNLIKELY( fd_ulong_sat_add( acct_infos_va, acc_info_total_sz ) >= FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
834 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
835 0 : return FD_VM_SYSCALL_ERR_INVALID_POINTER;
836 0 : }
837 0 : }
838 :
839 : /* This is the equivalent of translate_slice in translate_account_infos:
840 : https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/programs/bpf_loader/src/syscalls/cpi.rs#L816 */
841 72 : 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 );
842 :
843 : /* Right after translating, Agave checks the number of account infos:
844 : https://github.com/anza-xyz/agave/blob/v3.0.1/syscalls/src/cpi.rs#L752 */
845 36 : if( FD_UNLIKELY( acct_info_cnt > get_cpi_max_account_infos( vm->instr_ctx->bank ) ) ) {
846 12 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNT_INFOS_EXCEEDED );
847 12 : return FD_VM_SYSCALL_ERR_MAX_INSTRUCTION_ACCOUNT_INFOS_EXCEEDED;
848 12 : }
849 :
850 : /* Consume compute units proportional to the number of account infos, if
851 : increase_cpi_account_info_limit is active.
852 : https://github.com/anza-xyz/agave/blob/v3.1.2/program-runtime/src/cpi.rs#L968-L980 */
853 24 : if( FD_FEATURE_ACTIVE_BANK( vm->instr_ctx->bank, increase_cpi_account_info_limit ) ) {
854 12 : ulong account_infos_bytes = fd_ulong_sat_mul( acct_info_cnt, FD_VM_ACCOUNT_INFO_BYTE_SIZE );
855 12 : FD_VM_CU_UPDATE( vm, account_infos_bytes / FD_VM_CPI_BYTES_PER_UNIT );
856 12 : }
857 :
858 24 : fd_pubkey_t const * acct_info_keys[ FD_CPI_MAX_ACCOUNT_INFOS_SIMD_0339 ];
859 2250 : for( ulong acct_idx = 0UL; acct_idx < acct_info_cnt; acct_idx++ ) {
860 : /* Translate each pubkey address specified in account_infos.
861 : Failed translation should lead to an access violation and
862 : implies that obviously bad account_info has been supplied.
863 : https://github.com/anza-xyz/agave/blob/v2.1.6/programs/bpf_loader/src/syscalls/cpi.rs#L833-L841 */
864 6678 : acct_info_keys[ acct_idx ] = FD_VM_MEM_HADDR_LD( vm, acc_infos[ acct_idx ].pubkey_addr, alignof(uchar), sizeof(fd_pubkey_t) );
865 6678 : }
866 :
867 : /* translate_and_update_accounts ************************************************************
868 : Update the callee accounts with any changes made by the caller prior to this CPI execution
869 :
870 : https://github.com/anza-xyz/agave/blob/v3.0.1/syscalls/src/cpi.rs#L767-L892 */
871 24 : fd_vm_cpi_caller_account_t caller_accounts[ FD_VM_CPI_MAX_INSTRUCTION_ACCOUNTS ];
872 24 : ushort callee_account_keys[ FD_VM_CPI_MAX_INSTRUCTION_ACCOUNTS ];
873 24 : ushort caller_accounts_to_update[ FD_VM_CPI_MAX_INSTRUCTION_ACCOUNTS ];
874 24 : ulong caller_accounts_to_update_len = 0;
875 24 : err = VM_SYSCALL_CPI_TRANSLATE_AND_UPDATE_ACCOUNTS_FUNC(
876 24 : vm,
877 24 : instruction_accounts,
878 24 : instruction_accounts_cnt,
879 24 : acct_infos_va,
880 24 : acct_info_keys,
881 24 : acc_infos,
882 24 : acct_info_cnt,
883 24 : callee_account_keys,
884 24 : caller_accounts_to_update,
885 24 : caller_accounts,
886 24 : &caller_accounts_to_update_len
887 24 : );
888 : /* errors are propagated in the function itself. */
889 24 : if( FD_UNLIKELY( err ) ) return err;
890 :
891 : /* Before stricter_abi_and_runtime_constraints, this happens in
892 : VM_SYSCALL_CPI_TRANSLATE_AND_UPDATE_ACCOUNTS_FUNC.
893 : https://github.com/anza-xyz/agave/blob/v3.1.0-beta.0/program-runtime/src/cpi.rs#L856-L876 */
894 24 : if( vm->stricter_abi_and_runtime_constraints ) {
895 0 : for( ulong i=0UL; i<caller_accounts_to_update_len; i++ ) {
896 : /* Update the callee account to reflect any changes the caller has made
897 : https://github.com/anza-xyz/agave/blob/v3.1.0-beta.0/program-runtime/src/cpi.rs#L866-L872 */
898 0 : err = VM_SYCALL_CPI_UPDATE_CALLEE_ACC_FUNC( vm, caller_accounts + i, (uchar)callee_account_keys[i] );
899 0 : if( FD_UNLIKELY( err ) ) {
900 0 : return err;
901 0 : }
902 0 : }
903 0 : }
904 :
905 : /* Set the transaction compute meter to be the same as the VM's compute meter,
906 : so that the callee cannot use compute units that the caller has already used. */
907 24 : vm->instr_ctx->txn_out->details.compute_budget.compute_meter = vm->cu;
908 :
909 24 : long const regime1 = fd_tickcount();
910 :
911 : /* Execute the CPI instruction in the runtime */
912 24 : 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 );
913 24 : ulong instr_exec_res = (ulong)err_exec;
914 :
915 24 : long const regime2 = fd_tickcount();
916 24 : vm->instr_ctx->runtime->metrics.cpi_setup_cum_ticks += (ulong)( regime1-regime0 );
917 :
918 : /* Set the CU meter to the instruction context's transaction context's compute meter,
919 : so that the caller can't use compute units that the callee has already used. */
920 24 : vm->cu = vm->instr_ctx->txn_out->details.compute_budget.compute_meter;
921 :
922 24 : *_ret = instr_exec_res;
923 :
924 : /* Errors are propagated in fd_execute_instr. */
925 24 : if( FD_UNLIKELY( err_exec ) ) return err_exec;
926 :
927 : /* Update the caller accounts with any changes made by the callee during CPI execution */
928 72 : for( ulong i=0UL; i<caller_accounts_to_update_len; i++ ) {
929 : /* https://github.com/firedancer-io/solana/blob/508f325e19c0fd8e16683ea047d7c1a85f127e74/programs/bpf_loader/src/syscalls/cpi.rs#L939-L943 */
930 : /* We only want to update the writable accounts, because the non-writable
931 : caller accounts can't be changed during a CPI execution. */
932 48 : if( fd_instr_acc_is_writable_idx( vm->instr_ctx->instr, callee_account_keys[i] ) ) {
933 48 : ushort idx_in_txn = vm->instr_ctx->instr->accounts[ callee_account_keys[i] ].index_in_transaction;
934 48 : fd_pubkey_t const * callee = &vm->instr_ctx->txn_out->accounts.keys[ idx_in_txn ];
935 48 : err = VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC(vm, &acc_infos[ caller_accounts_to_update[i] ], caller_accounts + i, (uchar)callee_account_keys[i], callee);
936 48 : if( FD_UNLIKELY( err ) ) {
937 0 : return err;
938 0 : }
939 48 : }
940 48 : }
941 :
942 : /* With stricter_abi_and_runtime_constraints, update the caller's memory regions
943 : to reflect any changes the callee made to account data. This ensures the caller's
944 : view of account regions (tracked in acc_region_metas) remains consistent.
945 : https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1047-L1061 */
946 24 : if( vm->stricter_abi_and_runtime_constraints ) {
947 0 : for( ulong i=0UL; i<caller_accounts_to_update_len; i++ ) {
948 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1033-L1034 */
949 0 : fd_guarded_borrowed_account_t borrowed_callee_acc = {0};
950 0 : ushort idx_in_txn = vm->instr_ctx->instr->accounts[ callee_account_keys[i] ].index_in_transaction;
951 0 : fd_pubkey_t const * callee = &vm->instr_ctx->txn_out->accounts.keys[ idx_in_txn ];
952 0 : err = fd_exec_instr_ctx_try_borrow_instr_account_with_key( vm->instr_ctx, callee, &borrowed_callee_acc );
953 0 : if( FD_UNLIKELY( err && ( err != FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT ) ) ) {
954 0 : return 1;
955 0 : }
956 :
957 : /* https://github.com/anza-xyz/agave/blob/v3.0.4/syscalls/src/cpi.rs#L1052-L1058 */
958 0 : err = fd_vm_cpi_update_caller_account_region( vm, (ulong)callee_account_keys[i], caller_accounts + i, &borrowed_callee_acc );
959 0 : if( FD_UNLIKELY( err ) ) {
960 0 : return err;
961 0 : }
962 0 : }
963 0 : }
964 :
965 24 : long const regime3 = fd_tickcount();
966 24 : vm->instr_ctx->runtime->metrics.cpi_commit_cum_ticks += (ulong)( regime3-regime2 );
967 :
968 24 : return FD_VM_SUCCESS;
969 24 : }
970 :
971 : #undef VM_SYSCALL_CPI_UPDATE_CALLER_ACC_FUNC
972 : #undef VM_SYSCALL_CPI_FROM_ACC_INFO_FUNC
973 : #undef VM_SYSCALL_CPI_TRANSLATE_AND_UPDATE_ACCOUNTS_FUNC
974 : #undef VM_SYSCALL_CPI_INSTRUCTION_TO_INSTR_FUNC
975 : #undef VM_SYSCALL_CPI_FUNC
|