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