Line data Source code
1 : #include "fd_instr_harness.h"
2 : #include "../fd_executor.h"
3 : #include "../fd_runtime.h"
4 : #include "../fd_system_ids.h"
5 : #include "../../log_collector/fd_log_collector.h"
6 : #include "../program/fd_bpf_loader_serialization.h"
7 : #include "../../../ballet/sbpf/fd_sbpf_loader.h"
8 : #include "../../vm/fd_vm.h"
9 : #include "../../vm/test_vm_util.h"
10 : #include "generated/vm.pb.h"
11 : #include "../fd_bank.h"
12 :
13 : static int
14 : fd_solfuzz_vm_syscall_noop( void * _vm,
15 : ulong arg0,
16 : ulong arg1,
17 : ulong arg2,
18 : ulong arg3,
19 : ulong arg4,
20 0 : ulong* _ret){
21 : /* TODO: have input message determine CUs to deduct?
22 : fd_vm_t * vm = (fd_vm_t *) _vm;
23 : vm->cu = vm->cu - 5;
24 : */
25 :
26 0 : (void) _vm;
27 0 : (void) arg0;
28 0 : (void) arg1;
29 0 : (void) arg2;
30 0 : (void) arg3;
31 0 : (void) arg4;
32 0 : *_ret = 0;
33 0 : return 0;
34 0 : }
35 :
36 : static fd_sbpf_syscalls_t *
37 : fd_solfuzz_vm_syscall_lookup_func( fd_sbpf_syscalls_t * syscalls,
38 : const char * syscall_name,
39 0 : size_t len) {
40 0 : ulong i;
41 :
42 0 : if (!syscall_name) return NULL;
43 :
44 0 : for (i = 0; i < fd_sbpf_syscalls_slot_cnt(); ++i) {
45 0 : if (!fd_sbpf_syscalls_key_inval(syscalls[i].key) && syscalls[i].name && strlen(syscalls[i].name) == len) {
46 0 : if (!memcmp(syscalls[i].name, syscall_name, len)) {
47 0 : return syscalls + i;
48 0 : }
49 0 : }
50 0 : }
51 :
52 0 : return NULL;
53 0 : }
54 :
55 : static ulong
56 : fd_solfuzz_vm_load_from_input_regions( fd_vm_input_region_t const * input,
57 : uint input_count,
58 : fd_exec_test_input_data_region_t ** output,
59 : pb_size_t * output_count,
60 : void * output_buf,
61 0 : ulong output_bufsz ) {
62 : /* pre-flight checks on output buffer size*/
63 0 : ulong input_regions_total_sz = 0;
64 0 : for( ulong i=0; i<input_count; i++ ) {
65 0 : input_regions_total_sz += input[i].region_sz;
66 0 : }
67 :
68 0 : if( FD_UNLIKELY( input_regions_total_sz == 0
69 0 : || output_bufsz < input_regions_total_sz ) ) {
70 0 : *output = NULL;
71 0 : *output_count = 0;
72 0 : return 0;
73 0 : }
74 :
75 0 : FD_SCRATCH_ALLOC_INIT( l, output_buf );
76 0 : *output = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_input_data_region_t),
77 0 : input_count * sizeof (fd_exec_test_input_data_region_t) );
78 0 : FD_TEST( *output );
79 0 : *output_count = input_count;
80 :
81 0 : for( ulong i=0; i<input_count; i++ ) {
82 0 : fd_vm_input_region_t const * vm_region = &input[i];
83 0 : fd_exec_test_input_data_region_t * out_region = &(*output)[i];
84 0 : out_region->is_writable = vm_region->is_writable;
85 0 : out_region->offset = vm_region->vaddr_offset;
86 :
87 0 : if( vm_region->region_sz > 0 ) {
88 0 : out_region->content = FD_SCRATCH_ALLOC_APPEND( l, alignof(pb_bytes_array_t),
89 0 : PB_BYTES_ARRAY_T_ALLOCSIZE(vm_region->region_sz) );
90 0 : FD_TEST( out_region->content );
91 0 : out_region->content->size = vm_region->region_sz;
92 0 : fd_memcpy( out_region->content->bytes, (void *)vm_region->haddr, vm_region->region_sz );
93 0 : } else {
94 0 : out_region->content = NULL;
95 0 : }
96 0 : }
97 :
98 0 : ulong end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
99 0 : return end - (ulong)output_buf; /* return the number of bytes written */
100 0 : }
101 :
102 :
103 : ulong
104 : fd_solfuzz_pb_vm_interp_run( fd_solfuzz_runner_t * runner,
105 : void const * input_,
106 : void ** output_,
107 : void * output_buf,
108 0 : ulong output_bufsz ) {
109 0 : fd_exec_test_syscall_context_t const * input = fd_type_pun_const( input_ );
110 0 : fd_exec_test_syscall_effects_t ** output = fd_type_pun( output_ );
111 :
112 : /* Create execution context */
113 0 : const fd_exec_test_instr_context_t * input_instr_ctx = &input->instr_ctx;
114 0 : fd_exec_instr_ctx_t instr_ctx[1];
115 0 : if( !fd_solfuzz_pb_instr_ctx_create( runner, instr_ctx, input_instr_ctx, true /* is_syscall avoids certain checks we don't want */ ) ) {
116 0 : fd_solfuzz_pb_instr_ctx_destroy( runner, instr_ctx );
117 0 : return 0UL;
118 0 : }
119 :
120 0 : if( !( input->has_vm_ctx ) ) {
121 0 : fd_solfuzz_pb_instr_ctx_destroy( runner, instr_ctx );
122 0 : return 0UL;
123 0 : }
124 :
125 0 : fd_spad_t * spad = runner->spad;
126 0 : instr_ctx->bank = runner->bank;
127 :
128 : /* Create effects */
129 0 : ulong output_end = (ulong) output_buf + output_bufsz;
130 0 : FD_SCRATCH_ALLOC_INIT( l, output_buf );
131 0 : fd_exec_test_syscall_effects_t * effects =
132 0 : FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_syscall_effects_t),
133 0 : sizeof (fd_exec_test_syscall_effects_t) );
134 0 : *effects = (fd_exec_test_syscall_effects_t) FD_EXEC_TEST_SYSCALL_EFFECTS_INIT_ZERO;
135 :
136 0 : if( FD_UNLIKELY( _l > output_end ) ) {
137 0 : fd_solfuzz_pb_instr_ctx_destroy( runner, instr_ctx );
138 0 : return 0UL;
139 0 : }
140 :
141 0 : do{
142 : /* Setup regions */
143 0 : if ( !input->vm_ctx.rodata ) {
144 0 : break;
145 0 : }
146 0 : ulong rodata_sz = input->vm_ctx.rodata->size;
147 0 : uchar * rodata = fd_spad_alloc_check( spad, 8UL, rodata_sz );
148 0 : memcpy( rodata, input->vm_ctx.rodata->bytes, rodata_sz );
149 :
150 : /* Setup input region */
151 0 : ulong input_sz = 0UL;
152 0 : ulong pre_lens[256] = {0};
153 0 : fd_vm_input_region_t input_mem_regions[1000] = {0}; /* We can have a max of (3 * num accounts + 1) regions */
154 0 : fd_vm_acc_region_meta_t acc_region_metas[256] = {0}; /* instr acc idx to idx */
155 0 : uint input_mem_regions_cnt = 0UL;
156 0 : int direct_mapping = FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, account_data_direct_mapping );
157 0 : int stricter_abi_and_runtime_constraints = FD_FEATURE_ACTIVE_BANK( instr_ctx->bank, stricter_abi_and_runtime_constraints );
158 :
159 0 : uchar * input_ptr = NULL;
160 0 : uchar program_id_idx = instr_ctx->instr->program_id;
161 0 : fd_txn_account_t const * program_acc = &instr_ctx->txn_out->accounts.accounts[program_id_idx];
162 0 : uchar is_deprecated = ( program_id_idx < instr_ctx->txn_out->accounts.accounts_cnt ) &&
163 0 : ( !memcmp( fd_txn_account_get_owner( program_acc ), fd_solana_bpf_loader_deprecated_program_id.key, sizeof(fd_pubkey_t) ) );
164 :
165 : /* Push the instruction onto the stack. This may also modify the sysvar instructions account, if its present. */
166 0 : int stack_push_err = fd_instr_stack_push( instr_ctx->runtime, instr_ctx->txn_in, instr_ctx->txn_out, (fd_instr_info_t *)instr_ctx->instr );
167 0 : if( FD_UNLIKELY( stack_push_err ) ) {
168 0 : FD_LOG_WARNING(( "instr stack push err" ));
169 0 : fd_solfuzz_pb_instr_ctx_destroy( runner, instr_ctx );
170 0 : return 0;
171 0 : }
172 :
173 : /* Serialize accounts into input memory region. */
174 0 : int err = fd_bpf_loader_input_serialize_parameters( instr_ctx,
175 0 : &input_sz,
176 0 : pre_lens,
177 0 : input_mem_regions,
178 0 : &input_mem_regions_cnt,
179 0 : acc_region_metas,
180 0 : stricter_abi_and_runtime_constraints,
181 0 : direct_mapping,
182 0 : is_deprecated,
183 0 : &input_ptr );
184 0 : if( FD_UNLIKELY( err ) ) {
185 0 : fd_solfuzz_pb_instr_ctx_destroy( runner, instr_ctx );
186 0 : return 0;
187 0 : }
188 :
189 0 : if( input->vm_ctx.heap_max>FD_VM_HEAP_DEFAULT ) {
190 0 : break;
191 0 : }
192 :
193 : /* Setup calldests from call_whitelist.
194 : Alloc calldests with the expected size (1 bit per ix, rounded up to ulong) */
195 0 : ulong max_pc = (rodata_sz + 7) / 8;
196 0 : ulong calldests_footprint = fd_sbpf_calldests_footprint( max_pc );
197 0 : void * calldests_mem = fd_spad_alloc_check( spad, fd_sbpf_calldests_align(), calldests_footprint );
198 0 : ulong * calldests = fd_sbpf_calldests_join( fd_sbpf_calldests_new( calldests_mem, max_pc ) );
199 0 : if( input->vm_ctx.call_whitelist && input->vm_ctx.call_whitelist->size > 0 ) {
200 0 : memcpy( calldests, input->vm_ctx.call_whitelist->bytes, input->vm_ctx.call_whitelist->size );
201 : /* Make sure bits over max_pc are all 0s. */
202 0 : ulong mask = (1UL << (max_pc % 64)) - 1UL;
203 0 : if ( max_pc % 64 != 0) {
204 0 : calldests[ max_pc / 64 ] &= mask;
205 0 : }
206 0 : }
207 0 : ulong entry_pc = fd_ulong_min( input->vm_ctx.entry_pc, rodata_sz / 8UL - 1UL );
208 0 : if( input->vm_ctx.sbpf_version >= FD_SBPF_V3 ) {
209 : /* in v3 we have to enable the entrypoint */
210 0 : calldests[ entry_pc / 64UL ] |= ( 1UL << ( entry_pc % 64UL ) );
211 0 : }
212 :
213 : /* Setup syscalls. Have them all be no-ops */
214 0 : fd_sbpf_syscalls_t * syscalls = fd_sbpf_syscalls_new( fd_spad_alloc_check( spad, fd_sbpf_syscalls_align(), fd_sbpf_syscalls_footprint() ) );
215 0 : fd_vm_syscall_register_slot( syscalls,
216 0 : fd_bank_slot_get( instr_ctx->bank ),
217 0 : fd_bank_features_query( instr_ctx->bank ),
218 0 : 0 );
219 :
220 0 : for( ulong i=0; i< fd_sbpf_syscalls_slot_cnt(); i++ ){
221 0 : if( !fd_sbpf_syscalls_key_inval( syscalls[i].key ) ) {
222 0 : syscalls[i].func = fd_solfuzz_vm_syscall_noop;
223 0 : }
224 0 : }
225 :
226 : /* Setup trace */
227 0 : const int enable_vm_tracing = runner->enable_vm_tracing;
228 0 : fd_vm_trace_t * trace = NULL;
229 :
230 0 : if ( FD_UNLIKELY( enable_vm_tracing ) ) {
231 0 : trace = fd_vm_trace_new( fd_spad_alloc_check( spad, fd_vm_trace_align(), fd_vm_trace_footprint( FD_RUNTIME_VM_TRACE_EVENT_MAX, FD_RUNTIME_VM_TRACE_EVENT_DATA_MAX ) ), FD_RUNTIME_VM_TRACE_EVENT_MAX, FD_RUNTIME_VM_TRACE_EVENT_DATA_MAX );
232 0 : }
233 :
234 : /* Setup vm */
235 0 : fd_vm_t * vm = fd_vm_join( fd_vm_new( fd_spad_alloc_check( spad, fd_vm_align(), fd_vm_footprint() ) ) );
236 0 : FD_TEST( vm );
237 :
238 0 : fd_vm_init(
239 0 : vm,
240 0 : instr_ctx,
241 0 : input->vm_ctx.heap_max,
242 0 : input->has_instr_ctx ? input->instr_ctx.cu_avail : 0,
243 0 : rodata,
244 0 : rodata_sz,
245 0 : (ulong *) rodata, /* text*, same as rodata */
246 0 : rodata_sz / 8, /* text_cnt */
247 0 : 0, /* text_off */
248 0 : rodata_sz, /* text_sz */
249 0 : entry_pc,
250 0 : calldests,
251 0 : input->vm_ctx.sbpf_version,
252 0 : syscalls,
253 0 : trace, /* trace */
254 0 : NULL, /* sha */
255 0 : input_mem_regions,
256 0 : input_mem_regions_cnt,
257 0 : acc_region_metas, /* vm_acc_region_meta*/
258 0 : is_deprecated, /* is deprecated */
259 0 : direct_mapping, /* direct mapping */
260 0 : stricter_abi_and_runtime_constraints, /* stricter_abi_and_runtime_constraints */
261 0 : 0 /* dump_syscall_to_pb */
262 0 : );
263 :
264 : /* Setup registers.
265 : r1, r10, r11 are initialized by EbpfVm::new (r10) or EbpfVm::execute_program (r1, r11),
266 : or equivalently by fd_vm_init and fd_vm_setup_state_for_execution.
267 : Modifying them will most like break execution.
268 : In syscalls we allow override them (especially r1) because that simulates the fact
269 : that a program partially executed before reaching the syscall.
270 : Here we want to test what happens when the program starts from the beginning. */
271 0 : vm->reg[0] = input->vm_ctx.r0;
272 : // vm->reg[1] = input->vm_ctx.r1; // do not override
273 0 : vm->reg[2] = input->vm_ctx.r2;
274 0 : vm->reg[3] = input->vm_ctx.r3;
275 0 : vm->reg[4] = input->vm_ctx.r4;
276 0 : vm->reg[5] = input->vm_ctx.r5;
277 0 : vm->reg[6] = input->vm_ctx.r6;
278 0 : vm->reg[7] = input->vm_ctx.r7;
279 0 : vm->reg[8] = input->vm_ctx.r8;
280 0 : vm->reg[9] = input->vm_ctx.r9;
281 : // vm->reg[10] = input->vm_ctx.r10; // do not override
282 : // vm->reg[11] = input->vm_ctx.r11; // do not override
283 :
284 : // Validate the vm
285 0 : if( fd_vm_validate( vm ) != FD_VM_SUCCESS ) {
286 : // custom error, avoid -1 because we use it for "unknown error" in solfuzz-agave
287 0 : effects->error = -2;
288 0 : break;
289 0 : }
290 :
291 0 : if( input->syscall_invocation.stack_prefix ) {
292 0 : uchar * stack = input->syscall_invocation.stack_prefix->bytes;
293 0 : ulong stack_sz = fd_ulong_min(input->syscall_invocation.stack_prefix->size, FD_VM_STACK_MAX);
294 0 : fd_memcpy( vm->stack, stack, stack_sz );
295 0 : }
296 :
297 0 : if( input->syscall_invocation.heap_prefix ) {
298 0 : uchar * heap = input->syscall_invocation.heap_prefix->bytes;
299 0 : ulong heap_sz = fd_ulong_min(input->syscall_invocation.heap_prefix->size, FD_VM_HEAP_MAX);
300 0 : fd_memcpy( vm->heap, heap, heap_sz );
301 0 : }
302 :
303 : /* Run vm */
304 0 : int exec_res = 0;
305 0 : if ( FD_UNLIKELY( enable_vm_tracing ) ) {
306 0 : exec_res = fd_vm_exec_trace( vm );
307 0 : if( enable_vm_tracing ) fd_vm_trace_printf( trace, syscalls );
308 0 : fd_vm_trace_delete( fd_vm_trace_leave( trace ) );
309 0 : } else {
310 0 : exec_res = fd_vm_exec_notrace( vm );
311 0 : }
312 :
313 0 : effects->error = -exec_res;
314 :
315 : /* We do not compare VM state on CU errors since CU accounting
316 : is non-conformant with the Agave JIT/Interpreter.
317 : CU consumption is not precisely defined when VM faults */
318 0 : if( FD_UNLIKELY( exec_res==FD_VM_ERR_EBPF_EXCEEDED_MAX_INSTRUCTIONS ) )
319 0 : break;
320 :
321 : /* Capture remaining outputs */
322 0 : effects->cu_avail = vm->cu;
323 0 : effects->frame_count = vm->frame_cnt;
324 : /* Only capture registers if no error */
325 0 : effects->r0 = exec_res ? 0 : vm->reg[0];
326 0 : effects->r1 = exec_res ? 0 : vm->reg[1];
327 0 : effects->r2 = exec_res ? 0 : vm->reg[2];
328 0 : effects->r3 = exec_res ? 0 : vm->reg[3];
329 0 : effects->r4 = exec_res ? 0 : vm->reg[4];
330 0 : effects->r5 = exec_res ? 0 : vm->reg[5];
331 0 : effects->r6 = exec_res ? 0 : vm->reg[6];
332 0 : effects->r7 = exec_res ? 0 : vm->reg[7];
333 0 : effects->r8 = exec_res ? 0 : vm->reg[8];
334 0 : effects->r9 = exec_res ? 0 : vm->reg[9];
335 0 : effects->r10 = exec_res ? 0 : vm->reg[10];
336 :
337 : /* skip logs since syscalls are stubbed */
338 :
339 0 : effects->pc = vm->pc;
340 :
341 0 : if( vm->heap_max > 0 ) {
342 0 : effects->heap = FD_SCRATCH_ALLOC_APPEND(
343 0 : l, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( vm->heap_max ) );
344 0 : effects->heap->size = (uint)vm->heap_max;
345 0 : fd_memcpy( effects->heap->bytes, vm->heap, vm->heap_max );
346 0 : }
347 :
348 : /* Compress stack by removing right-most 0s.
349 : This reduces the total size of effects/fixtures when stack is not used,
350 : otherwise each would waste 256kB. */
351 0 : int rtrim_sz;
352 0 : for( rtrim_sz=FD_VM_STACK_MAX-1; rtrim_sz>=0; rtrim_sz-- ) {
353 0 : if( vm->stack[rtrim_sz] != 0 ) break;
354 0 : }
355 0 : if( rtrim_sz > 0 || (vm->stack[0] != 0) ) {
356 0 : effects->stack = FD_SCRATCH_ALLOC_APPEND(
357 0 : l, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( FD_VM_STACK_MAX ) );
358 0 : effects->stack->size = (uint)rtrim_sz+1;
359 0 : fd_memcpy( effects->stack->bytes, vm->stack, (ulong)rtrim_sz+1 );
360 0 : }
361 :
362 0 : effects->rodata = FD_SCRATCH_ALLOC_APPEND(
363 0 : l, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( rodata_sz ) );
364 0 : effects->rodata->size = (uint)rodata_sz;
365 0 : fd_memcpy( effects->rodata->bytes, rodata, rodata_sz );
366 :
367 : /* Capture input data regions */
368 0 : ulong tmp_end = FD_SCRATCH_ALLOC_FINI(l, 1UL);
369 0 : ulong input_data_regions_size = fd_solfuzz_vm_load_from_input_regions(
370 0 : vm->input_mem_regions,
371 0 : vm->input_mem_regions_cnt,
372 0 : &effects->input_data_regions,
373 0 : &effects->input_data_regions_count,
374 0 : (void *) tmp_end,
375 0 : fd_ulong_sat_sub( output_end, tmp_end )
376 0 : );
377 0 : FD_SCRATCH_ALLOC_APPEND( l, 1UL, input_data_regions_size );
378 :
379 0 : } while(0);
380 :
381 0 : ulong actual_end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
382 0 : *output = effects;
383 0 : fd_solfuzz_pb_instr_ctx_destroy( runner, instr_ctx );
384 0 : return actual_end - (ulong)output_buf;
385 0 : }
386 :
387 : ulong
388 : fd_solfuzz_pb_syscall_run( fd_solfuzz_runner_t * runner,
389 : void const * input_,
390 : void ** output_,
391 : void * output_buf,
392 0 : ulong output_bufsz ) {
393 0 : fd_exec_test_syscall_context_t const * input = fd_type_pun_const( input_ );
394 0 : fd_exec_test_syscall_effects_t ** output = fd_type_pun( output_ );
395 :
396 : /* Create execution context */
397 0 : const fd_exec_test_instr_context_t * input_instr_ctx = &input->instr_ctx;
398 0 : fd_exec_instr_ctx_t ctx[1];
399 : // Skip extra checks for non-CPI syscalls
400 0 : int is_cpi = !strncmp( (const char *)input->syscall_invocation.function_name.bytes, "sol_invoke_signed", 17 );
401 0 : int skip_extra_checks = !is_cpi;
402 :
403 0 : if( !fd_solfuzz_pb_instr_ctx_create( runner, ctx, input_instr_ctx, skip_extra_checks ) )
404 0 : goto error;
405 :
406 0 : ctx->runtime->instr.trace[0].instr_info = (fd_instr_info_t *)ctx->instr;
407 0 : ctx->runtime->instr.trace[0].stack_height = 1;
408 0 : ctx->txn_out->err.exec_err = 0;
409 0 : ctx->txn_out->err.exec_err_kind = FD_EXECUTOR_ERR_KIND_NONE;
410 0 : ctx->bank = runner->bank;
411 :
412 : /* Capture outputs */
413 0 : ulong output_end = (ulong)output_buf + output_bufsz;
414 0 : FD_SCRATCH_ALLOC_INIT( l, output_buf );
415 0 : fd_exec_test_syscall_effects_t * effects =
416 0 : FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_syscall_effects_t),
417 0 : sizeof (fd_exec_test_syscall_effects_t) );
418 0 : if( FD_UNLIKELY( _l > output_end ) ) {
419 0 : goto error;
420 0 : }
421 :
422 0 : if( input->vm_ctx.return_data.program_id && input->vm_ctx.return_data.program_id->size == sizeof(fd_pubkey_t) ) {
423 0 : fd_memcpy( ctx->txn_out->details.return_data.program_id.uc, input->vm_ctx.return_data.program_id->bytes, sizeof(fd_pubkey_t) );
424 0 : }
425 :
426 0 : if( input->vm_ctx.return_data.data && input->vm_ctx.return_data.data->size>0U ) {
427 0 : ctx->txn_out->details.return_data.len = input->vm_ctx.return_data.data->size;
428 0 : fd_memcpy( ctx->txn_out->details.return_data.data, input->vm_ctx.return_data.data->bytes, ctx->txn_out->details.return_data.len );
429 0 : }
430 :
431 0 : *effects = (fd_exec_test_syscall_effects_t) FD_EXEC_TEST_SYSCALL_EFFECTS_INIT_ZERO;
432 :
433 : /* Set up the VM instance */
434 0 : fd_spad_t * spad = runner->spad;
435 0 : fd_sha256_t _sha[1];
436 0 : fd_sha256_t * sha = fd_sha256_join( fd_sha256_new( _sha ) );
437 0 : fd_sbpf_syscalls_t * syscalls = fd_sbpf_syscalls_new( fd_spad_alloc_check( spad, fd_sbpf_syscalls_align(), fd_sbpf_syscalls_footprint() ) );
438 0 : fd_vm_syscall_register_all( syscalls, 0 );
439 :
440 : /* Pull out the memory regions */
441 0 : if( !input->has_vm_ctx ) {
442 0 : goto error;
443 0 : }
444 :
445 0 : ulong rodata_sz = input->vm_ctx.rodata ? input->vm_ctx.rodata->size : 0UL;
446 0 : uchar * rodata = fd_spad_alloc_check( spad, 8UL, rodata_sz );
447 0 : if ( input->vm_ctx.rodata != NULL ) {
448 0 : fd_memcpy( rodata, input->vm_ctx.rodata->bytes, rodata_sz );
449 0 : }
450 :
451 0 : if( input->vm_ctx.heap_max > FD_VM_HEAP_MAX ) {
452 0 : goto error;
453 0 : }
454 :
455 0 : fd_vm_t * vm = fd_vm_join( fd_vm_new( fd_spad_alloc_check( spad, fd_vm_align(), fd_vm_footprint() ) ) );
456 0 : if ( !vm ) {
457 0 : goto error;
458 0 : }
459 :
460 : /* If the program ID account owner is the v1 BPF loader, then alignment is disabled (controlled by
461 : the `is_deprecated` flag) */
462 :
463 0 : ulong input_sz = 0UL;
464 0 : ulong pre_lens[256] = {0};
465 0 : fd_vm_input_region_t input_mem_regions[1000] = {0}; /* We can have a max of (3 * num accounts + 1) regions */
466 0 : fd_vm_acc_region_meta_t acc_region_metas[256] = {0}; /* instr acc idx to idx */
467 0 : uint input_mem_regions_cnt = 0U;
468 0 : int direct_mapping = FD_FEATURE_ACTIVE_BANK( ctx->bank, account_data_direct_mapping );
469 0 : int stricter_abi_and_runtime_constraints = FD_FEATURE_ACTIVE_BANK( ctx->bank, stricter_abi_and_runtime_constraints );
470 :
471 0 : uchar * input_ptr = NULL;
472 0 : uchar program_id_idx = ctx->instr->program_id;
473 0 : fd_txn_account_t * program_acc = &ctx->txn_out->accounts.accounts[program_id_idx];
474 0 : uchar is_deprecated = ( program_id_idx < ctx->txn_out->accounts.accounts_cnt ) &&
475 0 : ( !memcmp( fd_txn_account_get_owner( program_acc ), fd_solana_bpf_loader_deprecated_program_id.key, sizeof(fd_pubkey_t) ) );
476 :
477 : /* Push the instruction onto the stack. This may also modify the sysvar instructions account, if its present. */
478 0 : int stack_push_err = fd_instr_stack_push( ctx->runtime, ctx->txn_in, ctx->txn_out, (fd_instr_info_t *)ctx->instr );
479 0 : if( FD_UNLIKELY( stack_push_err ) ) {
480 0 : FD_LOG_WARNING(( "instr stack push err" ));
481 0 : goto error;
482 0 : }
483 :
484 : /* Serialize accounts into input memory region. */
485 0 : int err = fd_bpf_loader_input_serialize_parameters( ctx,
486 0 : &input_sz,
487 0 : pre_lens,
488 0 : input_mem_regions,
489 0 : &input_mem_regions_cnt,
490 0 : acc_region_metas,
491 0 : stricter_abi_and_runtime_constraints,
492 0 : direct_mapping,
493 0 : is_deprecated,
494 0 : &input_ptr );
495 0 : if( FD_UNLIKELY( err ) ) {
496 0 : FD_LOG_WARNING(( "bpf loader input serialize parameters err" ));
497 0 : goto error;
498 0 : }
499 :
500 0 : fd_vm_init( vm,
501 0 : ctx,
502 0 : input->vm_ctx.heap_max,
503 0 : ctx->txn_out->details.compute_budget.compute_meter,
504 0 : rodata,
505 0 : rodata_sz,
506 0 : NULL, // TODO
507 0 : 0, // TODO
508 0 : 0, // TODO
509 0 : 0, // TODO, text_sz
510 0 : 0, // TODO
511 0 : NULL, // TODO
512 0 : TEST_VM_DEFAULT_SBPF_VERSION,
513 0 : syscalls,
514 0 : NULL, // TODO
515 0 : sha,
516 0 : input_mem_regions,
517 0 : input_mem_regions_cnt,
518 0 : acc_region_metas,
519 0 : is_deprecated,
520 0 : direct_mapping,
521 0 : stricter_abi_and_runtime_constraints,
522 0 : 0 /* dump_syscall_to_pb */ );
523 :
524 : // Override some execution state values from the syscall fuzzer input
525 : // This is so we can test if the syscall mutates any of these erroneously
526 0 : vm->reg[0] = input->vm_ctx.r0;
527 0 : vm->reg[1] = input->vm_ctx.r1;
528 0 : vm->reg[2] = input->vm_ctx.r2;
529 0 : vm->reg[3] = input->vm_ctx.r3;
530 0 : vm->reg[4] = input->vm_ctx.r4;
531 0 : vm->reg[5] = input->vm_ctx.r5;
532 0 : vm->reg[6] = input->vm_ctx.r6;
533 0 : vm->reg[7] = input->vm_ctx.r7;
534 0 : vm->reg[8] = input->vm_ctx.r8;
535 0 : vm->reg[9] = input->vm_ctx.r9;
536 0 : vm->reg[10] = input->vm_ctx.r10;
537 0 : vm->reg[11] = input->vm_ctx.r11;
538 :
539 : // Override initial part of the heap, if specified the syscall fuzzer input
540 0 : if( input->syscall_invocation.heap_prefix ) {
541 0 : fd_memcpy( vm->heap, input->syscall_invocation.heap_prefix->bytes,
542 0 : fd_ulong_min(input->syscall_invocation.heap_prefix->size, vm->heap_max) );
543 0 : }
544 :
545 : // Override initial part of the stack, if specified the syscall fuzzer input
546 0 : if( input->syscall_invocation.stack_prefix ) {
547 0 : fd_memcpy( vm->stack, input->syscall_invocation.stack_prefix->bytes,
548 0 : fd_ulong_min(input->syscall_invocation.stack_prefix->size, FD_VM_STACK_MAX) );
549 0 : }
550 :
551 : // Look up the syscall to execute
552 0 : char * syscall_name = (char *)input->syscall_invocation.function_name.bytes;
553 0 : fd_sbpf_syscalls_t const * syscall = fd_solfuzz_vm_syscall_lookup_func(syscalls, syscall_name, input->syscall_invocation.function_name.size);
554 0 : if( !syscall ) {
555 0 : goto error;
556 0 : }
557 :
558 : /* There's an instr ctx struct embedded in the txn ctx instr stack. */
559 0 : fd_exec_instr_ctx_t * instr_ctx = &ctx->runtime->instr.stack[ ctx->runtime->instr.stack_sz - 1 ];
560 0 : *instr_ctx = (fd_exec_instr_ctx_t) {
561 0 : .instr = ctx->instr,
562 0 : .txn_out = ctx->txn_out,
563 0 : .runtime = ctx->runtime,
564 0 : };
565 :
566 : /* Actually invoke the syscall */
567 0 : int syscall_err = syscall->func( vm, vm->reg[1], vm->reg[2], vm->reg[3], vm->reg[4], vm->reg[5], &vm->reg[0] );
568 0 : int stack_pop_err = fd_instr_stack_pop( ctx->runtime, ctx->txn_out, ctx->instr );
569 0 : if( FD_UNLIKELY( stack_pop_err ) ) {
570 0 : FD_LOG_WARNING(( "instr stack pop err" ));
571 0 : goto error;
572 0 : }
573 0 : if( syscall_err ) {
574 0 : fd_log_collector_program_failure( vm->instr_ctx );
575 0 : }
576 :
577 : /* Capture the effects */
578 0 : int exec_err = vm->instr_ctx->txn_out->err.exec_err;
579 0 : effects->error = 0;
580 0 : if( syscall_err ) {
581 0 : if( exec_err==0 ) {
582 0 : FD_LOG_WARNING(( "TODO: syscall returns error, but exec_err not set. this is probably missing a log." ));
583 0 : effects->error = -1;
584 0 : } else {
585 0 : effects->error = (exec_err <= 0) ? -exec_err : -1;
586 :
587 : /* Map error kind, equivalent to:
588 : effects->error_kind = (fd_exec_test_err_kind_t)(vm->instr_ctx->txn_ctx->err.exec_err_kind); */
589 0 : switch (vm->instr_ctx->txn_out->err.exec_err_kind) {
590 0 : case FD_EXECUTOR_ERR_KIND_EBPF:
591 0 : effects->error_kind = FD_EXEC_TEST_ERR_KIND_EBPF;
592 0 : break;
593 0 : case FD_EXECUTOR_ERR_KIND_SYSCALL:
594 0 : effects->error_kind = FD_EXEC_TEST_ERR_KIND_SYSCALL;
595 0 : break;
596 0 : case FD_EXECUTOR_ERR_KIND_INSTR:
597 0 : effects->error_kind = FD_EXEC_TEST_ERR_KIND_INSTRUCTION;
598 0 : break;
599 0 : default:
600 0 : effects->error_kind = FD_EXEC_TEST_ERR_KIND_UNSPECIFIED;
601 0 : break;
602 0 : }
603 0 : }
604 0 : }
605 0 : effects->r0 = syscall_err ? 0 : vm->reg[0]; // Save only on success
606 0 : effects->cu_avail = (ulong)vm->cu;
607 :
608 0 : if( vm->heap_max ) {
609 0 : effects->heap = FD_SCRATCH_ALLOC_APPEND(
610 0 : l, alignof(uint), PB_BYTES_ARRAY_T_ALLOCSIZE( vm->heap_max ) );
611 0 : if( FD_UNLIKELY( _l > output_end ) ) {
612 0 : goto error;
613 0 : }
614 0 : effects->heap->size = (uint)vm->heap_max;
615 0 : fd_memcpy( effects->heap->bytes, vm->heap, vm->heap_max );
616 0 : } else {
617 0 : effects->heap = NULL;
618 0 : }
619 :
620 0 : effects->stack = FD_SCRATCH_ALLOC_APPEND(
621 0 : l, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( FD_VM_STACK_MAX ) );
622 0 : if( FD_UNLIKELY( _l > output_end ) ) {
623 0 : goto error;
624 0 : }
625 0 : effects->stack->size = (uint)FD_VM_STACK_MAX;
626 0 : fd_memcpy( effects->stack->bytes, vm->stack, FD_VM_STACK_MAX );
627 :
628 0 : if( vm->rodata_sz ) {
629 0 : effects->rodata = FD_SCRATCH_ALLOC_APPEND(
630 0 : l, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( rodata_sz ) );
631 0 : if( FD_UNLIKELY( _l > output_end ) ) {
632 0 : goto error;
633 0 : }
634 0 : effects->rodata->size = (uint)rodata_sz;
635 0 : fd_memcpy( effects->rodata->bytes, vm->rodata, rodata_sz );
636 0 : } else {
637 0 : effects->rodata = NULL;
638 0 : }
639 :
640 0 : effects->frame_count = vm->frame_cnt;
641 :
642 0 : fd_log_collector_t * log = vm->instr_ctx->runtime->log.log_collector;
643 : /* Only collect log on valid errors (i.e., != -1). Follows
644 : https://github.com/firedancer-io/solfuzz-agave/blob/99758d3c4f3a342d56e2906936458d82326ae9a8/src/utils/err_map.rs#L148 */
645 0 : if( effects->error != -1 && log->buf_sz ) {
646 0 : effects->log = FD_SCRATCH_ALLOC_APPEND(
647 0 : l, alignof(pb_bytes_array_t), PB_BYTES_ARRAY_T_ALLOCSIZE( log->buf_sz ) );
648 0 : if( FD_UNLIKELY( _l > output_end ) ) {
649 0 : goto error;
650 0 : }
651 0 : effects->log->size = (uint)fd_log_collector_debug_sprintf( log, (char *)effects->log->bytes, 0 );
652 0 : } else {
653 0 : effects->log = NULL;
654 0 : }
655 :
656 : /* Capture input regions */
657 0 : effects->inputdata = NULL; /* Deprecated, using input_data_regions instead */
658 0 : ulong tmp_end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
659 0 : ulong input_regions_size = fd_solfuzz_vm_load_from_input_regions(
660 0 : vm->input_mem_regions,
661 0 : vm->input_mem_regions_cnt,
662 0 : &effects->input_data_regions,
663 0 : &effects->input_data_regions_count,
664 0 : (void *)tmp_end,
665 0 : fd_ulong_sat_sub( output_end, tmp_end )
666 0 : );
667 :
668 0 : if( !!vm->input_mem_regions_cnt && !effects->input_data_regions ) {
669 0 : goto error;
670 0 : }
671 :
672 : /* Return the effects */
673 0 : ulong actual_end = tmp_end + input_regions_size;
674 0 : fd_solfuzz_pb_instr_ctx_destroy( runner, ctx );
675 :
676 0 : *output = effects;
677 0 : return actual_end - (ulong)output_buf;
678 :
679 0 : error:
680 0 : fd_solfuzz_pb_instr_ctx_destroy( runner, ctx );
681 0 : return 0;
682 0 : }
|