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