Line data Source code
1 : #include "fd_instr_harness.h"
2 : #include "fd_solfuzz_private.h"
3 : #include "../fd_executor.h"
4 : #include "../fd_runtime.h"
5 : #include "../fd_system_ids.h"
6 : #include "../../log_collector/fd_log_collector.h"
7 : #include "../program/fd_bpf_loader_serialization.h"
8 : #include "../../../ballet/sbpf/fd_sbpf_loader.h"
9 : #include "../../../ballet/murmur3/fd_murmur3.h"
10 : #include "../../vm/fd_vm.h"
11 : #include "../../vm/fd_vm_private.h"
12 : #include "generated/vm.pb.h"
13 : #include "generated/vm_serialization.pb.h"
14 : #include "../fd_bank.h"
15 :
16 : static fd_vm_syscall_t const *
17 : fd_solfuzz_vm_syscall_lookup_func( const char * syscall_name,
18 0 : size_t len ) {
19 0 : if( !syscall_name ) return NULL;
20 0 : fd_vm_syscall_t const * syscall = fd_vm_syscall_query( fd_murmur3_32( syscall_name, len, 0U ), FD_SBPF_SYSCALL_ALL );
21 0 : if( !syscall || strlen( syscall->name )!=len || memcmp( syscall->name, syscall_name, len ) ) return NULL;
22 0 : return syscall;
23 0 : }
24 :
25 : static ulong
26 : fd_solfuzz_vm_load_from_input_regions( fd_vm_input_region_t const * input,
27 : uint input_count,
28 : fd_exec_test_input_data_region_t ** output,
29 : uint * output_count,
30 : void * output_buf,
31 0 : ulong output_bufsz ) {
32 : /* Each region emits a fixed-size record holding a hash of its contents,
33 : so the output size depends on the region count, not their contents. */
34 0 : ulong output_sz = input_count * sizeof(fd_exec_test_input_data_region_t)
35 0 : + alignof(fd_exec_test_input_data_region_t);
36 :
37 0 : if( FD_UNLIKELY( !input_count || output_bufsz < output_sz ) ) {
38 0 : *output = NULL;
39 0 : *output_count = 0;
40 0 : return 0;
41 0 : }
42 :
43 0 : FD_SCRATCH_ALLOC_INIT( l, output_buf );
44 0 : *output = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_input_data_region_t),
45 0 : input_count * sizeof (fd_exec_test_input_data_region_t) );
46 0 : FD_TEST( *output );
47 0 : *output_count = input_count;
48 :
49 0 : for( ulong i=0; i<input_count; i++ ) {
50 0 : fd_vm_input_region_t const * vm_region = &input[i];
51 0 : fd_exec_test_input_data_region_t * out_region = &(*output)[i];
52 0 : out_region->is_writable = vm_region->is_writable;
53 0 : out_region->offset = vm_region->vaddr_offset;
54 :
55 0 : out_region->content_hash = fd_solfuzz_hash( (void *)vm_region->haddr, vm_region->region_sz );
56 0 : }
57 :
58 0 : ulong end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
59 0 : return end - (ulong)output_buf; /* return the number of bytes written */
60 0 : }
61 :
62 :
63 : ulong
64 : fd_solfuzz_pb_syscall_run( fd_solfuzz_runner_t * runner,
65 : void const * input_,
66 : void ** output_,
67 : void * output_buf,
68 0 : ulong output_bufsz ) {
69 0 : fd_exec_test_syscall_context_t const * input = fd_type_pun_const( input_ );
70 0 : fd_exec_test_syscall_effects_t ** output = fd_type_pun( output_ );
71 :
72 : /* Create execution context */
73 0 : const fd_exec_test_instr_context_t * input_instr_ctx = &input->instr_ctx;
74 0 : fd_exec_instr_ctx_t ctx[1];
75 0 : fd_solfuzz_pb_instr_ctx_create( runner, ctx, input_instr_ctx );
76 0 : ulong frame_level = ULONG_MAX;
77 :
78 0 : ctx->txn_out->err.exec_err = 0;
79 0 : ctx->txn_out->err.exec_err_kind = FD_EXECUTOR_ERR_KIND_NONE;
80 0 : ctx->bank = runner->bank;
81 :
82 : /* Capture outputs */
83 0 : ulong output_end = (ulong)output_buf + output_bufsz;
84 0 : FD_SCRATCH_ALLOC_INIT( l, output_buf );
85 0 : fd_exec_test_syscall_effects_t * effects =
86 0 : FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_syscall_effects_t),
87 0 : sizeof (fd_exec_test_syscall_effects_t) );
88 0 : if( FD_UNLIKELY( _l > output_end ) ) {
89 0 : goto error;
90 0 : }
91 :
92 0 : if( input->vm_ctx.return_data.program_id && input->vm_ctx.return_data.program_id->size == sizeof(fd_pubkey_t) ) {
93 0 : fd_memcpy( ctx->txn_out->details.return_data.program_id.uc, input->vm_ctx.return_data.program_id->bytes, sizeof(fd_pubkey_t) );
94 0 : }
95 :
96 0 : if( input->vm_ctx.return_data.data && input->vm_ctx.return_data.data->size>0U ) {
97 0 : if( FD_UNLIKELY( input->vm_ctx.return_data.data->size>sizeof(ctx->txn_out->details.return_data.data) ) ) goto error;
98 0 : ctx->txn_out->details.return_data.len = input->vm_ctx.return_data.data->size;
99 0 : fd_memcpy( ctx->txn_out->details.return_data.data, input->vm_ctx.return_data.data->bytes, ctx->txn_out->details.return_data.len );
100 0 : }
101 :
102 0 : *effects = (fd_exec_test_syscall_effects_t){0};
103 :
104 : /* Set up the VM instance */
105 0 : fd_spad_t * spad = runner->spad;
106 0 : fd_sha256_t _sha[1];
107 0 : fd_sha256_t * sha = fd_sha256_join( fd_sha256_new( _sha ) );
108 :
109 : /* Pull out the memory regions */
110 0 : if( !input->has_vm_ctx ) {
111 0 : goto error;
112 0 : }
113 :
114 0 : ulong rodata_sz = input->vm_ctx.rodata ? input->vm_ctx.rodata->size : 0UL;
115 0 : uchar * rodata = fd_spad_alloc_check( spad, 8UL, rodata_sz );
116 0 : if ( input->vm_ctx.rodata != NULL ) {
117 0 : fd_memcpy( rodata, input->vm_ctx.rodata->bytes, rodata_sz );
118 0 : }
119 :
120 0 : if( input->vm_ctx.heap_max > FD_VM_HEAP_MAX ) {
121 0 : goto error;
122 0 : }
123 :
124 0 : FD_TEST( input->vm_ctx.sbpf_version<=FD_SBPF_V3 );
125 :
126 0 : fd_vm_t * vm = fd_vm_join( fd_vm_new( fd_spad_alloc_check( spad, fd_vm_align(), fd_vm_footprint() ) ) );
127 0 : if ( !vm ) {
128 0 : goto error;
129 0 : }
130 :
131 : /* If the program ID account owner is the v1 BPF loader, then alignment is disabled (controlled by
132 : the `is_deprecated` flag) */
133 :
134 0 : ulong input_sz = 0UL;
135 0 : ulong pre_lens[256] = {0};
136 0 : fd_vm_input_region_t input_mem_regions[1000] = {0}; /* At most (2 * num accounts + 1) regions */
137 0 : fd_vm_acc_region_meta_t acc_region_metas[256] = {0}; /* instr acc idx to idx */
138 0 : uint input_mem_regions_cnt = 0U;
139 0 : int direct_account_pointers_in_program_input = FD_FEATURE_ACTIVE_BANK( ctx->bank, direct_account_pointers_in_program_input );
140 :
141 0 : uchar program_id_idx = ctx->instr->program_id;
142 0 : fd_acc_t * program_acc = ctx->txn_out->accounts.account[program_id_idx];
143 0 : uchar is_deprecated = ( program_id_idx < ctx->txn_out->accounts.cnt ) &&
144 0 : ( !memcmp( program_acc->owner, fd_solana_bpf_loader_deprecated_program_id.key, sizeof(fd_pubkey_t) ) );
145 :
146 : /* Push the instruction onto the stack. This may also modify the sysvar instructions account, if its present. */
147 0 : int stack_push_err = fd_instr_stack_push( ctx->runtime, ctx->bank, ctx->txn_in, ctx->txn_out, (fd_instr_info_t *)ctx->instr );
148 0 : if( FD_UNLIKELY( stack_push_err ) ) {
149 0 : FD_LOG_WARNING(( "instr stack push err" ));
150 0 : goto error;
151 0 : }
152 0 : frame_level = ctx->runtime->instr.stack_sz-1UL;
153 0 : ctx->runtime->bpf_loader_serialization.frame[ frame_level ] = fd_bpf_frames_acquire( ctx->runtime->bpf_frames, 0 );
154 :
155 0 : ulong instr_data_offset = 0UL;
156 0 : ulong account_pointers_offset = 0UL;
157 0 : int err = fd_bpf_loader_input_serialize_parameters( ctx,
158 0 : pre_lens,
159 0 : input_mem_regions,
160 0 : &input_mem_regions_cnt,
161 0 : acc_region_metas,
162 0 : direct_account_pointers_in_program_input,
163 0 : is_deprecated,
164 0 : &instr_data_offset,
165 0 : &account_pointers_offset,
166 0 : &input_sz );
167 0 : if( FD_UNLIKELY( err ) ) {
168 0 : FD_LOG_WARNING(( "bpf loader input serialize parameters err" ));
169 0 : goto error;
170 0 : }
171 :
172 0 : fd_vm_init( vm,
173 0 : ctx,
174 0 : input->vm_ctx.heap_max,
175 0 : ctx->txn_out->details.compute_budget.compute_meter,
176 0 : rodata,
177 0 : rodata_sz,
178 0 : NULL, // TODO
179 0 : 0, // TODO
180 0 : 0, // TODO
181 0 : 0, // TODO, text_sz
182 0 : 0, // TODO
183 0 : NULL, // TODO
184 0 : input->vm_ctx.sbpf_version,
185 0 : NULL, // TODO
186 0 : sha,
187 0 : input_mem_regions,
188 0 : input_mem_regions_cnt,
189 0 : acc_region_metas,
190 0 : is_deprecated,
191 0 : 0 /* dump_syscall_to_pb */,
192 0 : (ulong[ FD_VM_REG_CNT ]){ 0 } /* r2 is set by the fuzzer below */ );
193 :
194 : // Override some execution state values from the syscall fuzzer input
195 : // This is so we can test if the syscall mutates any of these erroneously
196 0 : vm->reg[0] = input->vm_ctx.r0;
197 0 : vm->reg[1] = input->vm_ctx.r1;
198 0 : vm->reg[2] = input->vm_ctx.r2;
199 0 : vm->reg[3] = input->vm_ctx.r3;
200 0 : vm->reg[4] = input->vm_ctx.r4;
201 0 : vm->reg[5] = input->vm_ctx.r5;
202 0 : vm->reg[6] = input->vm_ctx.r6;
203 0 : vm->reg[7] = input->vm_ctx.r7;
204 0 : vm->reg[8] = input->vm_ctx.r8;
205 0 : vm->reg[9] = input->vm_ctx.r9;
206 0 : vm->reg[10] = input->vm_ctx.r10;
207 0 : vm->reg[11] = input->vm_ctx.r11;
208 :
209 0 : fd_vm_mem_init_full( vm );
210 :
211 : // Override initial part of the heap, if specified the syscall fuzzer input
212 0 : if( input->syscall_invocation.heap_prefix ) {
213 0 : fd_memcpy( vm->heap, input->syscall_invocation.heap_prefix->bytes,
214 0 : fd_ulong_min( input->syscall_invocation.heap_prefix->size, vm->heap_max ) );
215 0 : }
216 :
217 : // Override initial part of the stack, if specified the syscall fuzzer input
218 0 : if( input->syscall_invocation.stack_prefix ) {
219 0 : fd_memcpy( vm->stack, input->syscall_invocation.stack_prefix->bytes,
220 0 : fd_ulong_min( input->syscall_invocation.stack_prefix->size, FD_VM_STACK_MAX ) );
221 0 : }
222 :
223 : // Look up the syscall to execute
224 0 : char * syscall_name = (char *)input->syscall_invocation.function_name.bytes;
225 0 : fd_vm_syscall_t const * syscall = fd_solfuzz_vm_syscall_lookup_func(syscall_name, input->syscall_invocation.function_name.size);
226 0 : if( !syscall ) {
227 0 : goto error;
228 0 : }
229 :
230 : /* There's an instr ctx struct embedded in the txn ctx instr stack. */
231 0 : fd_exec_instr_ctx_t * instr_ctx = &ctx->runtime->instr.stack[ ctx->runtime->instr.stack_sz - 1 ];
232 0 : *instr_ctx = (fd_exec_instr_ctx_t) {
233 0 : .instr = ctx->instr,
234 0 : .txn_out = ctx->txn_out,
235 0 : .runtime = ctx->runtime,
236 0 : };
237 :
238 : /* Actually invoke the syscall */
239 0 : int syscall_err = syscall->func( vm, vm->reg[1], vm->reg[2], vm->reg[3], vm->reg[4], vm->reg[5] );
240 0 : int instr_end_err = fd_execute_instr_end( vm->instr_ctx, ctx->instr, syscall_err );
241 0 : if( instr_end_err ) {
242 0 : fd_log_collector_program_failure( vm->instr_ctx );
243 0 : }
244 :
245 : /* Capture the effects */
246 0 : int exec_err = vm->instr_ctx->txn_out->err.exec_err;
247 0 : effects->error = 0;
248 0 : if( instr_end_err ) {
249 0 : if( exec_err==0 ) {
250 0 : FD_LOG_WARNING(( "TODO: syscall returns error, but exec_err not set. this is probably missing a log." ));
251 0 : effects->error = -1;
252 0 : } else {
253 0 : effects->error = (exec_err <= 0) ? -exec_err : -1;
254 :
255 : /* Map error kind, equivalent to:
256 : effects->error_kind = (fd_exec_test_err_kind_t)(vm->instr_ctx->txn_ctx->err.exec_err_kind); */
257 0 : switch (vm->instr_ctx->txn_out->err.exec_err_kind) {
258 0 : case FD_EXECUTOR_ERR_KIND_EBPF:
259 0 : effects->error_kind = FD_EXEC_TEST_ERR_KIND_EBPF;
260 0 : break;
261 0 : case FD_EXECUTOR_ERR_KIND_SYSCALL:
262 0 : effects->error_kind = FD_EXEC_TEST_ERR_KIND_SYSCALL;
263 0 : break;
264 0 : case FD_EXECUTOR_ERR_KIND_INSTR:
265 0 : effects->error_kind = FD_EXEC_TEST_ERR_KIND_INSTRUCTION;
266 0 : break;
267 0 : default:
268 0 : effects->error_kind = FD_EXEC_TEST_ERR_KIND_UNSPECIFIED;
269 0 : break;
270 0 : }
271 0 : }
272 0 : }
273 0 : effects->r0 = instr_end_err ? 0 : vm->reg[0]; // Save only on success
274 0 : effects->cu_avail = (ulong)vm->cu;
275 :
276 0 : effects->heap_hash = fd_solfuzz_hash( vm->heap, vm->heap_max );
277 0 : effects->stack_hash = fd_solfuzz_hash( vm->stack, FD_VM_STACK_MAX );
278 0 : effects->rodata_hash = fd_solfuzz_hash( vm->rodata, vm->rodata_sz ? rodata_sz : 0UL );
279 :
280 0 : effects->frame_count = vm->frame_cnt;
281 :
282 : /* Capture input regions */
283 0 : ulong tmp_end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
284 :
285 : /* Skip comparing input data regions if execution failed, because
286 : Agave leaks data into the input regions under these circumstances. */
287 0 : ulong input_regions_size = 0UL;
288 0 : if( syscall_err ) {
289 0 : effects->input_data_regions = NULL;
290 0 : effects->input_data_regions_count = 0UL;
291 0 : } else {
292 0 : input_regions_size = fd_solfuzz_vm_load_from_input_regions(
293 0 : vm->input_mem_regions,
294 0 : vm->input_mem_regions_cnt,
295 0 : &effects->input_data_regions,
296 0 : &effects->input_data_regions_count,
297 0 : (void *)tmp_end,
298 0 : fd_ulong_sat_sub( output_end, tmp_end )
299 0 : );
300 :
301 0 : if( !!vm->input_mem_regions_cnt && !effects->input_data_regions ) {
302 0 : goto error;
303 0 : }
304 0 : }
305 :
306 : /* Return the effects */
307 0 : ulong actual_end = tmp_end + input_regions_size;
308 0 : fd_bpf_frames_release( ctx->runtime->bpf_frames, ctx->runtime->bpf_loader_serialization.frame[ frame_level ] );
309 0 : fd_solfuzz_pb_instr_ctx_destroy( runner, ctx );
310 :
311 0 : *output = effects;
312 0 : return actual_end - (ulong)output_buf;
313 :
314 0 : error:
315 0 : if( frame_level!=ULONG_MAX ) fd_bpf_frames_release( ctx->runtime->bpf_frames, ctx->runtime->bpf_loader_serialization.frame[ frame_level ] );
316 0 : fd_solfuzz_pb_instr_ctx_destroy( runner, ctx );
317 0 : return 0;
318 0 : }
319 :
320 : ulong
321 : fd_solfuzz_pb_vm_serialize_run( fd_solfuzz_runner_t * runner,
322 : void const * input_,
323 : void ** output_,
324 : void * output_buf,
325 0 : ulong output_bufsz ) {
326 0 : fd_exec_test_instr_context_t const * input = fd_type_pun_const( input_ );
327 0 : fd_exec_test_vm_serialization_effects_t ** output = fd_type_pun( output_ );
328 :
329 : /* Create execution context */
330 0 : fd_exec_instr_ctx_t ctx[1];
331 0 : fd_solfuzz_pb_instr_ctx_create( runner, ctx, input );
332 :
333 0 : ctx->txn_out->err.exec_err = 0;
334 0 : ctx->txn_out->err.exec_err_kind = FD_EXECUTOR_ERR_KIND_NONE;
335 0 : ctx->bank = runner->bank;
336 :
337 : /* Capture outputs */
338 0 : ulong output_end = (ulong)output_buf + output_bufsz;
339 0 : FD_SCRATCH_ALLOC_INIT( l, output_buf );
340 0 : fd_exec_test_vm_serialization_effects_t * effects =
341 0 : FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_vm_serialization_effects_t),
342 0 : sizeof (fd_exec_test_vm_serialization_effects_t) );
343 0 : FD_TEST( _l <= output_end );
344 0 : *effects = (fd_exec_test_vm_serialization_effects_t){0};
345 :
346 : /* Determine feature flags and is_deprecated */
347 0 : int direct_account_pointers_in_program_input = FD_FEATURE_ACTIVE_BANK( ctx->bank, direct_account_pointers_in_program_input );
348 :
349 0 : uchar program_id_idx = ctx->instr->program_id;
350 0 : fd_acc_t * program_acc = ctx->txn_out->accounts.account[program_id_idx];
351 0 : uchar is_deprecated = ( program_id_idx < ctx->txn_out->accounts.cnt ) &&
352 0 : ( !memcmp( program_acc->owner, fd_solana_bpf_loader_deprecated_program_id.key, sizeof(fd_pubkey_t) ) );
353 :
354 : /* Push the instruction onto the stack */
355 0 : FD_TEST( !fd_instr_stack_push( ctx->runtime, ctx->bank, ctx->txn_in, ctx->txn_out, (fd_instr_info_t *)ctx->instr ) );
356 0 : ulong frame_level = ctx->runtime->instr.stack_sz-1UL;
357 0 : uchar * serialized_buf = fd_bpf_frames_acquire( ctx->runtime->bpf_frames, 0 );
358 0 : ctx->runtime->bpf_loader_serialization.frame[ frame_level ] = serialized_buf;
359 :
360 : /* Call serialize_parameters */
361 0 : ulong input_sz = 0UL;
362 0 : ulong pre_lens[256] = {0};
363 0 : fd_vm_input_region_t input_mem_regions[1000] = {0};
364 0 : fd_vm_acc_region_meta_t acc_region_metas[256] = {0};
365 0 : uint input_mem_regions_cnt = 0U;
366 0 : ulong instr_data_offset = 0UL;
367 0 : ulong account_pointers_offset = 0UL;
368 :
369 0 : int err = fd_bpf_loader_input_serialize_parameters(
370 0 : ctx,
371 0 : pre_lens,
372 0 : input_mem_regions,
373 0 : &input_mem_regions_cnt,
374 0 : acc_region_metas,
375 0 : direct_account_pointers_in_program_input,
376 0 : is_deprecated,
377 0 : &instr_data_offset,
378 0 : &account_pointers_offset,
379 0 : &input_sz
380 0 : );
381 :
382 0 : if( FD_UNLIKELY( err ) ) {
383 0 : effects->has_error = true;
384 0 : ulong actual_end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
385 0 : fd_bpf_frames_release( ctx->runtime->bpf_frames, ctx->runtime->bpf_loader_serialization.frame[ frame_level ] );
386 0 : fd_solfuzz_pb_instr_ctx_destroy( runner, ctx );
387 0 : *output = effects;
388 0 : return actual_end - (ulong)output_buf;
389 0 : }
390 :
391 : /* Hash the serialized memory buffer. */
392 0 : effects->serialized_memory_hash = fd_hash( 0UL, serialized_buf, input_sz );
393 :
394 : /* Populate vm_input_memory_regions */
395 0 : effects->vm_input_memory_regions_count = (uint)input_mem_regions_cnt;
396 0 : effects->vm_input_memory_regions =
397 0 : FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_vm_input_memory_region_t),
398 0 : input_mem_regions_cnt * sizeof(fd_exec_test_vm_input_memory_region_t) );
399 0 : FD_TEST( _l <= output_end );
400 :
401 0 : for( uint i=0; i<input_mem_regions_cnt; i++ ) {
402 0 : fd_vm_input_region_t const * region = &input_mem_regions[i];
403 0 : fd_exec_test_vm_input_memory_region_t * out = &effects->vm_input_memory_regions[i];
404 0 : out->vm_address = FD_VM_MEM_MAP_INPUT_REGION_START + region->vaddr_offset;
405 0 : out->region_size = region->region_sz;
406 0 : out->is_writable = region->is_writable;
407 0 : }
408 :
409 : /* Populate serialized_account_metadata */
410 0 : ulong num_ix_accounts = ctx->instr->acct_cnt;
411 0 : effects->serialized_account_metadata_count = (uint)num_ix_accounts;
412 0 : effects->serialized_account_metadata =
413 0 : FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_exec_test_vm_serialized_account_metadata_t),
414 0 : num_ix_accounts * sizeof(fd_exec_test_vm_serialized_account_metadata_t) );
415 0 : FD_TEST( _l <= output_end );
416 :
417 0 : for( ulong i=0; i<num_ix_accounts; i++ ) {
418 0 : fd_vm_acc_region_meta_t const * meta = &acc_region_metas[i];
419 0 : fd_exec_test_vm_serialized_account_metadata_t * out = &effects->serialized_account_metadata[i];
420 0 : out->original_data_len = meta->original_data_len;
421 0 : out->vm_data_addr = meta->vm_data_addr;
422 0 : out->vm_key_addr = meta->vm_key_addr;
423 0 : out->vm_lamports_addr = meta->vm_lamports_addr;
424 0 : out->vm_owner_addr = meta->vm_owner_addr;
425 0 : }
426 :
427 0 : ulong actual_end = FD_SCRATCH_ALLOC_FINI( l, 1UL );
428 0 : fd_bpf_frames_release( ctx->runtime->bpf_frames, ctx->runtime->bpf_loader_serialization.frame[ frame_level ] );
429 0 : fd_solfuzz_pb_instr_ctx_destroy( runner, ctx );
430 :
431 0 : *output = effects;
432 0 : return actual_end - (ulong)output_buf;
433 0 : }
|