Line data Source code
1 : #include "fd_vm_syscall.h"
2 : #include "../../runtime/program/fd_vote_program.h"
3 : #include "../../runtime/context/fd_exec_instr_ctx.h"
4 : #include "../../runtime/fd_system_ids.h"
5 : #include "fd_vm_syscall_macros.h"
6 :
7 : /* FIXME: In the original version of this code, there was an FD_TEST
8 : to check if the VM was attached to an instruction context (that
9 : would have crashed anyway because of pointer chasing). If the VM
10 : is being run outside the Solana runtime, it should never invoke
11 : this syscall in the first place. So we treat this as a SIGCALL in
12 : a non-crashing way for the time being. */
13 :
14 : int
15 : fd_vm_syscall_sol_get_clock_sysvar( /**/ void * _vm,
16 : /**/ ulong out_vaddr,
17 : FD_PARAM_UNUSED ulong r2,
18 : FD_PARAM_UNUSED ulong r3,
19 : FD_PARAM_UNUSED ulong r4,
20 : FD_PARAM_UNUSED ulong r5,
21 0 : /**/ ulong * _ret ) {
22 0 : fd_vm_t * vm = _vm;
23 0 : fd_exec_instr_ctx_t const * instr_ctx = vm->instr_ctx;
24 0 : if( FD_UNLIKELY( !instr_ctx ) ) return FD_VM_SYSCALL_ERR_OUTSIDE_RUNTIME;
25 :
26 0 : FD_VM_CU_UPDATE( vm, fd_ulong_sat_add( FD_VM_SYSVAR_BASE_COST, sizeof(fd_sol_sysvar_clock_t) ) );
27 :
28 0 : if( FD_UNLIKELY( vm->stricter_abi_and_runtime_constraints && out_vaddr>=FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
29 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
30 0 : return FD_VM_ERR_INVAL;
31 0 : }
32 :
33 0 : fd_vm_haddr_query_t var_query = {
34 0 : .vaddr = out_vaddr,
35 0 : .align = FD_VM_ALIGN_RUST_SYSVAR_CLOCK,
36 0 : .sz = sizeof(fd_sol_sysvar_clock_t),
37 0 : .is_slice = 0,
38 0 : };
39 :
40 0 : fd_vm_haddr_query_t * queries[] = { &var_query };
41 0 : FD_VM_TRANSLATE_MUT( vm, queries );
42 :
43 0 : fd_sol_sysvar_clock_t clock = fd_sysvar_cache_clock_read_nofail( instr_ctx->sysvar_cache );
44 0 : memcpy( var_query.haddr, &clock, sizeof(fd_sol_sysvar_clock_t) );
45 :
46 0 : *_ret = 0UL;
47 0 : return FD_VM_SUCCESS;
48 0 : }
49 :
50 : int
51 : fd_vm_syscall_sol_get_epoch_schedule_sysvar( /**/ void * _vm,
52 : /**/ ulong out_vaddr,
53 : FD_PARAM_UNUSED ulong r2,
54 : FD_PARAM_UNUSED ulong r3,
55 : FD_PARAM_UNUSED ulong r4,
56 : FD_PARAM_UNUSED ulong r5,
57 0 : /**/ ulong * _ret ) {
58 0 : fd_vm_t * vm = _vm;
59 0 : fd_exec_instr_ctx_t const * instr_ctx = vm->instr_ctx;
60 0 : if( FD_UNLIKELY( !instr_ctx ) ) return FD_VM_SYSCALL_ERR_OUTSIDE_RUNTIME;
61 :
62 0 : FD_VM_CU_UPDATE( vm, fd_ulong_sat_add( FD_VM_SYSVAR_BASE_COST, sizeof(fd_epoch_schedule_t) ) );
63 :
64 0 : if( FD_UNLIKELY( vm->stricter_abi_and_runtime_constraints && out_vaddr>=FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
65 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
66 0 : return FD_VM_ERR_INVAL;
67 0 : }
68 :
69 0 : fd_vm_haddr_query_t var_query = {
70 0 : .vaddr = out_vaddr,
71 0 : .align = FD_VM_ALIGN_RUST_SYSVAR_EPOCH_SCHEDULE,
72 0 : .sz = sizeof(fd_epoch_schedule_t),
73 0 : .is_slice = 0,
74 0 : };
75 :
76 0 : fd_vm_haddr_query_t * queries[] = { &var_query };
77 0 : FD_VM_TRANSLATE_MUT( vm, queries );
78 :
79 0 : fd_epoch_schedule_t schedule;
80 0 : if( FD_UNLIKELY( !fd_sysvar_cache_epoch_schedule_read( instr_ctx->sysvar_cache, &schedule ) ) ) {
81 0 : FD_TXN_ERR_FOR_LOG_INSTR( vm->instr_ctx->txn_out, FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR, vm->instr_ctx->txn_out->err.exec_err_idx );
82 0 : return FD_VM_ERR_INVAL;
83 0 : }
84 0 : memcpy( var_query.haddr, &schedule, sizeof(fd_epoch_schedule_t) );
85 :
86 0 : *_ret = 0UL;
87 0 : return FD_VM_SUCCESS;
88 0 : }
89 :
90 : int
91 : fd_vm_syscall_sol_get_rent_sysvar( /**/ void * _vm,
92 : /**/ ulong out_vaddr,
93 : FD_PARAM_UNUSED ulong r2,
94 : FD_PARAM_UNUSED ulong r3,
95 : FD_PARAM_UNUSED ulong r4,
96 : FD_PARAM_UNUSED ulong r5,
97 0 : /**/ ulong * _ret ) {
98 0 : fd_vm_t * vm = _vm;
99 :
100 : /* Unreachable in a real SVM, used for testing */
101 :
102 0 : fd_exec_instr_ctx_t const * instr_ctx = vm->instr_ctx;
103 0 : if( FD_UNLIKELY( !instr_ctx ) ) return FD_VM_SYSCALL_ERR_OUTSIDE_RUNTIME;
104 :
105 0 : FD_VM_CU_UPDATE( vm, fd_ulong_sat_add( FD_VM_SYSVAR_BASE_COST, sizeof(fd_rent_t) ) );
106 :
107 0 : if( FD_UNLIKELY( vm->stricter_abi_and_runtime_constraints && out_vaddr>=FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
108 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
109 0 : return FD_VM_ERR_INVAL;
110 0 : }
111 :
112 0 : fd_vm_haddr_query_t var_query = {
113 0 : .vaddr = out_vaddr,
114 0 : .align = FD_VM_ALIGN_RUST_SYSVAR_RENT,
115 0 : .sz = sizeof(fd_rent_t),
116 0 : .is_slice = 0,
117 0 : };
118 :
119 0 : fd_vm_haddr_query_t * queries[] = { &var_query };
120 0 : FD_VM_TRANSLATE_MUT( vm, queries );
121 :
122 0 : fd_rent_t rent = fd_sysvar_cache_rent_read_nofail( instr_ctx->sysvar_cache );
123 0 : memcpy( var_query.haddr, &rent, sizeof(fd_rent_t) );
124 :
125 0 : *_ret = 0UL;
126 0 : return FD_VM_SUCCESS;
127 0 : }
128 :
129 : /* https://github.com/anza-xyz/agave/blob/v2.3.2/programs/bpf_loader/src/syscalls/sysvar.rs#L149 */
130 : int
131 : fd_vm_syscall_sol_get_last_restart_slot_sysvar( /**/ void * _vm,
132 : /**/ ulong out_vaddr,
133 : FD_PARAM_UNUSED ulong r2,
134 : FD_PARAM_UNUSED ulong r3,
135 : FD_PARAM_UNUSED ulong r4,
136 : FD_PARAM_UNUSED ulong r5,
137 0 : /**/ ulong * _ret ) {
138 0 : fd_vm_t * vm = _vm;
139 0 : fd_exec_instr_ctx_t const * instr_ctx = vm->instr_ctx;
140 0 : if( FD_UNLIKELY( !instr_ctx ) ) return FD_VM_SYSCALL_ERR_OUTSIDE_RUNTIME;
141 :
142 0 : FD_VM_CU_UPDATE( vm, fd_ulong_sat_add( FD_VM_SYSVAR_BASE_COST, sizeof(fd_sol_sysvar_last_restart_slot_t) ) );
143 :
144 0 : if( FD_UNLIKELY( vm->stricter_abi_and_runtime_constraints && out_vaddr>=FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
145 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
146 0 : return FD_VM_ERR_INVAL;
147 0 : }
148 :
149 0 : fd_vm_haddr_query_t var_query = {
150 0 : .vaddr = out_vaddr,
151 0 : .align = FD_VM_ALIGN_RUST_SYSVAR_LAST_RESTART_SLOT,
152 0 : .sz = sizeof(fd_sol_sysvar_last_restart_slot_t),
153 0 : .is_slice = 0,
154 0 : };
155 :
156 0 : fd_vm_haddr_query_t * queries[] = { &var_query };
157 0 : FD_VM_TRANSLATE_MUT( vm, queries );
158 :
159 0 : fd_sol_sysvar_last_restart_slot_t last_restart_slot;
160 0 : if( FD_UNLIKELY( !fd_sysvar_cache_last_restart_slot_read( vm->instr_ctx->sysvar_cache, &last_restart_slot ) ) ) {
161 0 : FD_TXN_ERR_FOR_LOG_INSTR( vm->instr_ctx->txn_out, FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR, vm->instr_ctx->txn_out->err.exec_err_idx );
162 0 : return FD_VM_ERR_INVAL;
163 0 : }
164 :
165 0 : memcpy( var_query.haddr, &last_restart_slot, sizeof(fd_sol_sysvar_last_restart_slot_t) );
166 :
167 0 : *_ret = 0UL;
168 0 : return FD_VM_SUCCESS;
169 0 : }
170 :
171 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/sysvar.rs#L167-L232 */
172 : int
173 : fd_vm_syscall_sol_get_sysvar( /**/ void * _vm,
174 : /**/ ulong sysvar_id_vaddr,
175 : /**/ ulong out_vaddr,
176 : /**/ ulong offset,
177 : /**/ ulong sz,
178 : FD_PARAM_UNUSED ulong r5,
179 0 : /**/ ulong * _ret ) {
180 0 : fd_vm_t * vm = _vm;
181 0 : fd_exec_instr_ctx_t const * instr_ctx = vm->instr_ctx;
182 0 : if( FD_UNLIKELY( !instr_ctx ) ) return FD_VM_SYSCALL_ERR_OUTSIDE_RUNTIME;
183 :
184 : /* sysvar_id_cost seems to just always be 32 / 250 = 0...
185 : https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/sysvar.rs#L190-L197 */
186 0 : ulong sysvar_buf_cost = sz / FD_VM_CPI_BYTES_PER_UNIT;
187 0 : FD_VM_CU_UPDATE( vm, fd_ulong_sat_add( FD_VM_SYSVAR_BASE_COST, fd_ulong_max( sysvar_buf_cost, FD_VM_MEM_OP_BASE_COST ) ) );
188 :
189 0 : if( FD_UNLIKELY( vm->stricter_abi_and_runtime_constraints && out_vaddr>=FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
190 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
191 0 : return FD_VM_ERR_INVAL;
192 0 : }
193 :
194 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/sysvar.rs#L207-L211 */
195 0 : fd_vm_haddr_query_t var_query = {
196 0 : .vaddr = out_vaddr,
197 0 : .align = FD_VM_ALIGN_RUST_U8,
198 0 : .sz = sz,
199 0 : .is_slice = 1,
200 0 : };
201 :
202 0 : fd_vm_haddr_query_t * queries[] = { &var_query };
203 0 : FD_VM_TRANSLATE_MUT( vm, queries );
204 :
205 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/sysvar.rs#L199-L200 */
206 0 : const fd_pubkey_t * sysvar_id = FD_VM_MEM_HADDR_LD( vm, sysvar_id_vaddr, FD_VM_ALIGN_RUST_PUBKEY, FD_PUBKEY_FOOTPRINT );
207 :
208 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/sysvar.rs#L205-L208 */
209 0 : ulong offset_length;
210 0 : int err = fd_int_if( __builtin_uaddl_overflow( offset, sz, &offset_length ), FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW, FD_EXECUTOR_INSTR_SUCCESS );
211 0 : if( FD_UNLIKELY( err ) ) {
212 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, err );
213 0 : return FD_VM_SYSCALL_ERR_ABORT;
214 0 : }
215 :
216 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/sysvar.rs#L210-L213
217 : We don't need this, we already checked we can store in out_vaddr with requested sz. */
218 :
219 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/sysvar.rs#L215-L221 */
220 0 : if( FD_UNLIKELY( memcmp( sysvar_id->uc, fd_sysvar_clock_id.uc, FD_PUBKEY_FOOTPRINT ) &&
221 0 : memcmp( sysvar_id->uc, fd_sysvar_epoch_schedule_id.uc, FD_PUBKEY_FOOTPRINT ) &&
222 0 : memcmp( sysvar_id->uc, fd_sysvar_epoch_rewards_id.uc, FD_PUBKEY_FOOTPRINT ) &&
223 0 : memcmp( sysvar_id->uc, fd_sysvar_rent_id.uc, FD_PUBKEY_FOOTPRINT ) &&
224 0 : memcmp( sysvar_id->uc, fd_sysvar_slot_hashes_id.uc, FD_PUBKEY_FOOTPRINT ) &&
225 0 : memcmp( sysvar_id->uc, fd_sysvar_stake_history_id.uc, FD_PUBKEY_FOOTPRINT ) &&
226 0 : memcmp( sysvar_id->uc, fd_sysvar_last_restart_slot_id.uc, FD_PUBKEY_FOOTPRINT ) ) ) {
227 0 : *_ret = 2UL;
228 0 : return FD_VM_SUCCESS;
229 0 : }
230 :
231 0 : ulong sysvar_buf_len;
232 0 : uchar const * sysvar_buf =
233 0 : fd_sysvar_cache_data_query( vm->instr_ctx->sysvar_cache, sysvar_id, &sysvar_buf_len );
234 0 : if( FD_UNLIKELY( !sysvar_buf ) ) {
235 0 : *_ret = 2UL;
236 0 : return FD_VM_SUCCESS;
237 0 : }
238 :
239 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/sysvar.rs#L223-L228
240 : Note the length check is at the very end to fail after performing sufficient checks. */
241 :
242 0 : if( FD_UNLIKELY( offset_length>sysvar_buf_len ) ) {
243 0 : *_ret = 1UL;
244 0 : return FD_VM_SUCCESS;
245 0 : }
246 :
247 0 : if( FD_UNLIKELY( sz==0UL ) ) {
248 0 : *_ret = 0UL;
249 0 : return FD_VM_SUCCESS;
250 0 : }
251 :
252 0 : fd_memcpy( var_query.haddr, sysvar_buf + offset, sz );
253 0 : *_ret = 0;
254 0 : return FD_VM_SUCCESS;
255 0 : }
256 :
257 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/mod.rs#L2043-L2118 */
258 : int
259 : fd_vm_syscall_sol_get_epoch_stake( /**/ void * _vm,
260 : /**/ ulong var_addr,
261 : FD_PARAM_UNUSED ulong r2,
262 : FD_PARAM_UNUSED ulong r3,
263 : FD_PARAM_UNUSED ulong r4,
264 : FD_PARAM_UNUSED ulong r5,
265 0 : /**/ ulong * _ret ) {
266 0 : fd_vm_t * vm = (fd_vm_t *)_vm;
267 :
268 : /* Var addr of 0 returns the total active stake on the cluster.
269 :
270 : https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/mod.rs#L2057-L2075 */
271 0 : if( FD_UNLIKELY( var_addr==0UL ) ) {
272 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/mod.rs#L2065-L2066 */
273 0 : FD_VM_CU_UPDATE( vm, FD_VM_SYSCALL_BASE_COST );
274 :
275 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/mod.rs#L2074 */
276 0 : *_ret = fd_bank_total_epoch_stake_get( vm->instr_ctx->bank );
277 0 : return FD_VM_SUCCESS;
278 0 : }
279 :
280 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/mod.rs#L2083-L2091
281 : FD_PUBKEY_FOOTPRINT/FD_VM_CPI_BYTES_PER_UNIT is always 32/250 = 0,
282 : so we can omit it */
283 :
284 0 : FD_VM_CU_UPDATE( vm, FD_VM_MEM_OP_BASE_COST + FD_VM_SYSCALL_BASE_COST );
285 :
286 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/mod.rs#L2103-L2104 */
287 0 : const fd_pubkey_t * vote_address = FD_VM_MEM_HADDR_LD( vm, var_addr, FD_VM_ALIGN_RUST_PUBKEY, FD_PUBKEY_FOOTPRINT );
288 :
289 : /* https://github.com/anza-xyz/agave/blob/v2.2.14/runtime/src/bank.rs#L6954 */
290 0 : fd_vote_states_t const * vote_states = fd_bank_vote_states_prev_query( vm->instr_ctx->bank );
291 0 : fd_vote_state_ele_t const * vote_state_ele = fd_vote_states_query_const( vote_states, vote_address );
292 0 : *_ret = vote_state_ele ? vote_state_ele->stake : 0UL;
293 :
294 0 : return FD_VM_SUCCESS;
295 0 : }
296 :
297 : int
298 : fd_vm_syscall_sol_get_stack_height( /**/ void * _vm,
299 : FD_PARAM_UNUSED ulong r1,
300 : FD_PARAM_UNUSED ulong r2,
301 : FD_PARAM_UNUSED ulong r3,
302 : FD_PARAM_UNUSED ulong r4,
303 : FD_PARAM_UNUSED ulong r5,
304 0 : /**/ ulong * _ret ) {
305 : /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mod.rs#L1547 */
306 0 : fd_vm_t * vm = (fd_vm_t *)_vm;
307 :
308 0 : FD_VM_CU_UPDATE( vm, FD_VM_SYSCALL_BASE_COST );
309 :
310 0 : *_ret = vm->instr_ctx->runtime->instr.stack_sz;
311 0 : return FD_VM_SUCCESS;
312 0 : }
313 :
314 : int
315 : fd_vm_syscall_sol_get_return_data( /**/ void * _vm,
316 : /**/ ulong return_data_vaddr,
317 : /**/ ulong sz,
318 : /**/ ulong program_id_vaddr,
319 : FD_PARAM_UNUSED ulong r4,
320 : FD_PARAM_UNUSED ulong r5,
321 0 : /**/ ulong * _ret ) {
322 0 : fd_vm_t * vm = (fd_vm_t *)_vm;
323 :
324 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1465 */
325 0 : FD_VM_CU_UPDATE( vm, FD_VM_SYSCALL_BASE_COST );
326 :
327 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1467 */
328 0 : fd_txn_return_data_t const * return_data = &vm->instr_ctx->txn_out->details.return_data;
329 :
330 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1468 */
331 0 : ulong length = fd_ulong_min( return_data->len, sz );
332 :
333 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1469-L1492 */
334 0 : if( FD_LIKELY( length ) ) {
335 :
336 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1470-L1474 */
337 0 : FD_VM_CU_UPDATE( vm, fd_ulong_sat_add( length, sizeof(fd_pubkey_t) ) / FD_VM_CPI_BYTES_PER_UNIT );
338 :
339 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1476-L1481 */
340 0 : fd_vm_haddr_query_t return_data_query = {
341 0 : .vaddr = return_data_vaddr,
342 0 : .align = FD_VM_ALIGN_RUST_U8,
343 0 : .sz = length,
344 0 : .is_slice = 1
345 0 : };
346 :
347 0 : fd_vm_haddr_query_t program_id_query = {
348 0 : .vaddr = program_id_vaddr,
349 0 : .align = FD_VM_ALIGN_RUST_PUBKEY,
350 0 : .sz = sizeof(fd_pubkey_t),
351 0 : .is_slice = 0
352 0 : };
353 :
354 0 : fd_vm_haddr_query_t * queries[] = { &return_data_query, &program_id_query };
355 0 : FD_VM_TRANSLATE_MUT( vm, queries );
356 :
357 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1490-L1491 */
358 0 : memcpy( return_data_query.haddr, return_data->data, length );
359 0 : memcpy( program_id_query.haddr, &return_data->program_id, sizeof(fd_pubkey_t) );
360 0 : }
361 :
362 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1495 */
363 0 : *_ret = return_data->len;
364 0 : return FD_VM_SUCCESS;
365 0 : }
366 :
367 : int
368 : fd_vm_syscall_sol_set_return_data( /**/ void * _vm,
369 : /**/ ulong src_vaddr,
370 : /**/ ulong src_sz,
371 : FD_PARAM_UNUSED ulong r3,
372 : FD_PARAM_UNUSED ulong r4,
373 : FD_PARAM_UNUSED ulong r5,
374 0 : /**/ ulong * _ret ) {
375 : /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mod.rs#L1297 */
376 0 : fd_vm_t * vm = (fd_vm_t *)_vm;
377 :
378 : /* In the original version of this code, there was an FD_TEST
379 : to check if the VM was attached to an instruction context (that
380 : would have crashed anyway because of pointer chasing). If the VM
381 : is being run outside the Solana runtime, it should never invoke
382 : this syscall in the first place. So we treat this as a SIGCALL in
383 : a non-crashing way for the time being. */
384 0 : fd_exec_instr_ctx_t const * instr_ctx = vm->instr_ctx;
385 0 : if( FD_UNLIKELY( !instr_ctx ) ) return FD_VM_SYSCALL_ERR_OUTSIDE_RUNTIME;
386 :
387 0 : FD_VM_CU_UPDATE( vm, fd_ulong_sat_add( FD_VM_SYSCALL_BASE_COST, src_sz / FD_VM_CPI_BYTES_PER_UNIT ) );
388 :
389 : /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mod.rs#L1316 */
390 0 : if( FD_UNLIKELY( src_sz>FD_VM_RETURN_DATA_MAX ) ) {
391 : /* TODO: this is a bit annoying, we may want to unify return codes...
392 : - FD_VM_SYSCALL_ERR_RETURN_DATA_TOO_LARGE is Agave's return code,
393 : also used for logging */
394 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_RETURN_DATA_TOO_LARGE );
395 0 : return FD_VM_SYSCALL_ERR_RETURN_DATA_TOO_LARGE;
396 0 : }
397 :
398 : /* src_sz == 0 is ok */
399 0 : void const * src = FD_VM_MEM_SLICE_HADDR_LD( vm, src_vaddr, FD_VM_ALIGN_RUST_U8, src_sz );
400 :
401 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/programs/bpf_loader/src/syscalls/mod.rs#L1480-L1484 */
402 0 : fd_pubkey_t const * program_id = NULL;
403 0 : int err = fd_exec_instr_ctx_get_last_program_key( vm->instr_ctx, &program_id );
404 0 : if( FD_UNLIKELY( err ) ) {
405 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, err );
406 0 : return err;
407 0 : }
408 :
409 0 : fd_txn_return_data_t * return_data = &instr_ctx->txn_out->details.return_data;
410 :
411 0 : return_data->len = src_sz;
412 0 : if( FD_LIKELY( src_sz!=0UL ) ) {
413 0 : fd_memcpy( return_data->data, src, src_sz );
414 0 : }
415 0 : return_data->program_id = *program_id;
416 :
417 0 : *_ret = 0;
418 0 : return FD_VM_SUCCESS;
419 0 : }
420 :
421 : /* Used to query and convey information about the sibling instruction
422 : https://github.com/anza-xyz/agave/blob/70089cce5119c9afaeb2986e2ecaa6d4505ec15d/sdk/program/src/instruction.rs#L676
423 :
424 : */
425 : struct fd_vm_syscall_processed_sibling_instruction {
426 : /* Length of the instruction data */
427 : ulong data_len;
428 : /* Number of accounts */
429 : ulong accounts_len;
430 : };
431 : typedef struct fd_vm_syscall_processed_sibling_instruction fd_vm_syscall_processed_sibling_instruction_t;
432 :
433 0 : #define FD_VM_SYSCALL_PROCESSED_SIBLING_INSTRUCTION_SIZE (16UL)
434 0 : #define FD_VM_SYSCALL_PROCESSED_SIBLING_INSTRUCTION_ALIGN (8UL )
435 :
436 : /* https://github.com/anza-xyz/agave/blob/70089cce5119c9afaeb2986e2ecaa6d4505ec15d/programs/bpf_loader/src/syscalls/mod.rs#L1402 */
437 : int
438 : fd_vm_syscall_sol_get_processed_sibling_instruction(
439 : void * _vm,
440 : ulong index,
441 : ulong result_meta_vaddr,
442 : ulong result_program_id_vaddr,
443 : ulong result_data_vaddr,
444 : ulong result_accounts_vaddr,
445 : ulong * _ret
446 0 : ) {
447 0 : fd_vm_t * vm = (fd_vm_t *)_vm;
448 :
449 : /* Consume base compute cost
450 : https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1513 */
451 0 : FD_VM_CU_UPDATE( vm, FD_VM_SYSCALL_BASE_COST );
452 :
453 : /* Get the current instruction stack height. This value is 1-indexed
454 : (top level instruction has a stack height of 1).
455 : https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1517 */
456 0 : ulong stack_height = vm->instr_ctx->runtime->instr.stack_sz;
457 :
458 : /* Reverse iterate through the instruction trace, ignoring anything except instructions on the same level.
459 : https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1518-L1522 */
460 0 : ulong instruction_trace_length = vm->instr_ctx->runtime->instr.trace_length;
461 0 : ulong reverse_index_at_stack_height = 0UL;
462 0 : fd_instr_info_t * found_instruction_context = NULL;
463 0 : for( ulong index_in_trace=instruction_trace_length; index_in_trace>0UL; index_in_trace-- ) {
464 :
465 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1524-L1526
466 : This error can never happen */
467 :
468 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1527-L1529 */
469 0 : fd_instr_info_t * instruction_context = &vm->instr_ctx->runtime->instr.trace[ index_in_trace-1UL ];
470 0 : if( FD_LIKELY( instruction_context->stack_height<stack_height ) ) {
471 0 : break;
472 0 : }
473 :
474 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1530-L1536 */
475 0 : if( FD_UNLIKELY( instruction_context->stack_height==stack_height ) ) {
476 0 : if( FD_UNLIKELY( fd_ulong_sat_add( index, 1UL )==reverse_index_at_stack_height ) ) {
477 0 : found_instruction_context = instruction_context;
478 0 : break;
479 0 : }
480 0 : reverse_index_at_stack_height = fd_ulong_sat_add( reverse_index_at_stack_height, 1UL );
481 0 : }
482 0 : }
483 :
484 : /* If we have found an entry, then copy the instruction into the
485 : result addresses.
486 : https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1539-L1588
487 : */
488 0 : if( FD_LIKELY( found_instruction_context != NULL ) ) {
489 0 : fd_vm_haddr_query_t result_header_query = {
490 0 : .vaddr = result_meta_vaddr,
491 0 : .align = FD_VM_SYSCALL_PROCESSED_SIBLING_INSTRUCTION_ALIGN,
492 0 : .sz = FD_VM_SYSCALL_PROCESSED_SIBLING_INSTRUCTION_SIZE,
493 0 : .is_slice = 0,
494 0 : };
495 :
496 0 : fd_vm_haddr_query_t * queries[] = { &result_header_query };
497 0 : FD_VM_TRANSLATE_MUT( vm, queries );
498 :
499 0 : fd_vm_syscall_processed_sibling_instruction_t * result_header = result_header_query.haddr;
500 :
501 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1546-L1583 */
502 0 : if( result_header->data_len==found_instruction_context->data_sz && result_header->accounts_len==found_instruction_context->acct_cnt ) {
503 0 : fd_vm_haddr_query_t program_id_query = {
504 0 : .vaddr = result_program_id_vaddr,
505 0 : .align = FD_VM_ALIGN_RUST_PUBKEY,
506 0 : .sz = sizeof(fd_pubkey_t),
507 0 : .is_slice = 0,
508 0 : };
509 :
510 0 : fd_vm_haddr_query_t data_query = {
511 0 : .vaddr = result_data_vaddr,
512 0 : .align = FD_VM_ALIGN_RUST_U8,
513 0 : .sz = result_header->data_len,
514 0 : .is_slice = 1,
515 0 : };
516 :
517 0 : fd_vm_haddr_query_t accounts_query = {
518 0 : .vaddr = result_accounts_vaddr,
519 0 : .align = FD_VM_RUST_ACCOUNT_META_ALIGN,
520 0 : .sz = fd_ulong_sat_mul( result_header->accounts_len, FD_VM_RUST_ACCOUNT_META_SIZE ),
521 0 : .is_slice = 1,
522 0 : };
523 :
524 0 : fd_vm_haddr_query_t * queries[] = { &program_id_query, &data_query, &accounts_query, &result_header_query };
525 0 : FD_VM_TRANSLATE_MUT( vm, queries );
526 :
527 0 : fd_pubkey_t * program_id = program_id_query.haddr;
528 0 : uchar * data = data_query.haddr;
529 0 : fd_vm_rust_account_meta_t * accounts = accounts_query.haddr;
530 :
531 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1561-L1562 */
532 0 : fd_pubkey_t const * instr_ctx_program_id = NULL;
533 0 : int err = fd_runtime_get_key_of_account_at_index(
534 0 : vm->instr_ctx->txn_out,
535 0 : found_instruction_context->program_id,
536 0 : &instr_ctx_program_id
537 0 : );
538 0 : if( FD_UNLIKELY( err ) ) {
539 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, err );
540 0 : return err;
541 0 : }
542 0 : fd_memcpy( program_id, instr_ctx_program_id, sizeof(fd_pubkey_t) );
543 :
544 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1563 */
545 0 : fd_memcpy( data, found_instruction_context->data, found_instruction_context->data_sz );
546 :
547 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1564-L1581 */
548 0 : for( ushort i=0; i<found_instruction_context->acct_cnt; i++ ) {
549 0 : fd_pubkey_t const * account_key;
550 0 : ushort txn_idx = found_instruction_context->accounts[ i ].index_in_transaction;
551 0 : err = fd_runtime_get_key_of_account_at_index( vm->instr_ctx->txn_out, txn_idx, &account_key );
552 0 : if( FD_UNLIKELY( err ) ) {
553 0 : FD_VM_ERR_FOR_LOG_INSTR( vm, err );
554 0 : return err;
555 0 : }
556 :
557 0 : fd_memcpy( accounts[ i ].pubkey, account_key, sizeof(fd_pubkey_t) );
558 0 : accounts[ i ].is_signer = !!(found_instruction_context->accounts[ i ].is_signer );
559 0 : accounts[ i ].is_writable = !!(found_instruction_context->accounts[ i ].is_writable );
560 0 : }
561 0 : } else {
562 : /* Copy the actual metadata into the result meta struct
563 : https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1584-L1586 */
564 0 : result_header->data_len = found_instruction_context->data_sz;
565 0 : result_header->accounts_len = found_instruction_context->acct_cnt;
566 0 : }
567 :
568 : /* Return true as we found a sibling instruction
569 : https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1588 */
570 0 : *_ret = 1UL;
571 0 : return FD_VM_SUCCESS;
572 0 : }
573 :
574 : /* Return false if we didn't find a sibling instruction
575 : https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/mod.rs#L1590 */
576 0 : *_ret = 0UL;
577 0 : return FD_VM_SUCCESS;
578 0 : }
579 :
580 : /* https://github.com/anza-xyz/agave/blob/v2.3.1/programs/bpf_loader/src/syscalls/sysvar.rs#L80 */
581 : int
582 : fd_vm_syscall_sol_get_epoch_rewards_sysvar( /**/ void * _vm,
583 : /**/ ulong out_vaddr,
584 : FD_PARAM_UNUSED ulong r2,
585 : FD_PARAM_UNUSED ulong r3,
586 : FD_PARAM_UNUSED ulong r4,
587 : FD_PARAM_UNUSED ulong r5,
588 0 : /**/ ulong * _ret ) {
589 0 : fd_vm_t * vm = _vm;
590 0 : fd_exec_instr_ctx_t const * instr_ctx = vm->instr_ctx;
591 0 : if( FD_UNLIKELY( !instr_ctx ) ) return FD_VM_SYSCALL_ERR_OUTSIDE_RUNTIME;
592 :
593 0 : FD_VM_CU_UPDATE( vm, fd_ulong_sat_add( FD_VM_SYSVAR_BASE_COST, sizeof(fd_sysvar_epoch_rewards_t) ) );
594 :
595 0 : if( FD_UNLIKELY( vm->stricter_abi_and_runtime_constraints && out_vaddr>=FD_VM_MEM_MAP_INPUT_REGION_START ) ) {
596 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_POINTER );
597 0 : return FD_VM_ERR_INVAL;
598 0 : }
599 :
600 0 : uchar * out = FD_VM_MEM_HADDR_ST( vm, out_vaddr, FD_VM_ALIGN_RUST_SYSVAR_EPOCH_REWARDS, sizeof(fd_sysvar_epoch_rewards_t) );
601 :
602 0 : fd_sysvar_epoch_rewards_t epoch_rewards;
603 0 : if( FD_UNLIKELY( !fd_sysvar_cache_epoch_rewards_read( instr_ctx->sysvar_cache, &epoch_rewards ) ) ) {
604 0 : FD_TXN_ERR_FOR_LOG_INSTR( vm->instr_ctx->txn_out, FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR, vm->instr_ctx->txn_out->err.exec_err_idx );
605 0 : return FD_VM_ERR_INVAL;
606 0 : }
607 0 : memcpy( out, &epoch_rewards, sizeof(fd_sysvar_epoch_rewards_t) );
608 0 : memset( out+81, 0, 15 ); /* padding */
609 :
610 0 : *_ret = 0UL;
611 0 : return FD_VM_SUCCESS;
612 0 : }
|