Line data Source code
1 : #include "fd_vm_syscall.h"
2 :
3 : #include "../../../ballet/base64/fd_base64.h"
4 : #include "../../../ballet/utf8/fd_utf8.h"
5 : #include "../../runtime/sysvar/fd_sysvar.h"
6 : #include "../../runtime/sysvar/fd_sysvar_clock.h"
7 : #include "../../runtime/sysvar/fd_sysvar_epoch_schedule.h"
8 : #include "../../runtime/context/fd_exec_txn_ctx.h"
9 : #include "../../runtime/context/fd_exec_instr_ctx.h"
10 :
11 : int
12 : fd_vm_syscall_abort( /**/ void * _vm,
13 : FD_PARAM_UNUSED ulong r1,
14 : FD_PARAM_UNUSED ulong r2,
15 : FD_PARAM_UNUSED ulong r3,
16 : FD_PARAM_UNUSED ulong r4,
17 : FD_PARAM_UNUSED ulong r5,
18 0 : FD_PARAM_UNUSED ulong * _ret ) {
19 : /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/mod.rs#L630 */
20 0 : fd_vm_t * vm = (fd_vm_t *)_vm;
21 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_ABORT );
22 0 : return FD_VM_SYSCALL_ERR_ABORT;
23 0 : }
24 :
25 : /* FD_TRANSLATE_STRING returns a read only pointer to the host address of
26 : a valid utf8 string, or it errors.
27 :
28 : Analogous of Agave's translate_string_and_do().
29 : https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/mod.rs#L601
30 :
31 : As of v0.2.6, the only two usages are in syscall panic and syscall log. */
32 9 : #define FD_TRANSLATE_STRING( vm, vaddr, msg_sz ) (__extension__({ \
33 9 : char const * msg = FD_VM_MEM_SLICE_HADDR_LD( vm, vaddr, FD_VM_ALIGN_RUST_U8, msg_sz ); \
34 9 : if( FD_UNLIKELY( !fd_utf8_verify( msg, msg_sz ) ) ) { \
35 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_STRING ); \
36 0 : return FD_VM_SYSCALL_ERR_INVALID_STRING; \
37 0 : } \
38 9 : msg; \
39 9 : }))
40 :
41 : int
42 : fd_vm_syscall_sol_panic( /**/ void * _vm,
43 : /**/ ulong file_vaddr,
44 : /**/ ulong file_sz,
45 : /**/ ulong line,
46 : /**/ ulong column,
47 : FD_PARAM_UNUSED ulong r5,
48 0 : FD_PARAM_UNUSED ulong * _ret ) {
49 0 : fd_vm_t * vm = (fd_vm_t *)_vm;
50 :
51 : /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/mod.rs#L637
52 :
53 : Note: this syscall is not used by the Rust SDK, only by the C SDK.
54 : Rust transforms `panic!()` into a log, followed by an abort.
55 : It's unclear if this syscall actually makes any sense... */
56 0 : FD_VM_CU_UPDATE( vm, file_sz );
57 :
58 : /* Validate string */
59 0 : FD_TRANSLATE_STRING( vm, file_vaddr, file_sz );
60 :
61 : /* Note: we truncate the log, ignoring file, line, column.
62 : As mentioned above, it's unclear if anyone is even using this syscall,
63 : so dealing with the complexity of Agave's log is a waste of time. */
64 0 : (void)line;
65 0 : (void)column;
66 :
67 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_PANIC );
68 0 : return FD_VM_SYSCALL_ERR_PANIC;
69 0 : }
70 :
71 : int
72 : fd_vm_syscall_sol_log( /**/ void * _vm,
73 : /**/ ulong msg_vaddr,
74 : /**/ ulong msg_sz,
75 : FD_PARAM_UNUSED ulong r3,
76 : FD_PARAM_UNUSED ulong r4,
77 : FD_PARAM_UNUSED ulong r5,
78 9 : /**/ ulong * _ret ) {
79 9 : fd_vm_t * vm = (fd_vm_t *)_vm;
80 :
81 : /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L5 */
82 :
83 9 : FD_VM_CU_UPDATE( vm, fd_ulong_max( msg_sz, FD_VM_SYSCALL_BASE_COST ) );
84 :
85 : /* Note: when msg_sz==0, msg can be undefined. fd_log_collector_program_log() handles it.
86 : FIXME: Macro invocation in function invocation? */
87 9 : fd_log_collector_program_log( vm->instr_ctx, FD_TRANSLATE_STRING( vm, msg_vaddr, msg_sz ), msg_sz );
88 :
89 0 : *_ret = 0UL;
90 9 : return FD_VM_SUCCESS;
91 18 : }
92 :
93 : int
94 : fd_vm_syscall_sol_log_64( void * _vm,
95 : ulong r1,
96 : ulong r2,
97 : ulong r3,
98 : ulong r4,
99 : ulong r5,
100 3 : ulong * _ret ) {
101 3 : fd_vm_t * vm = (fd_vm_t *)_vm;
102 :
103 : /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L37 */
104 :
105 3 : FD_VM_CU_UPDATE( vm, FD_VM_LOG_64_UNITS );
106 :
107 : /* Max msg_sz: 46 - 15 + 16*5 = 111 < 127 => we can use printf */
108 0 : fd_log_collector_printf_dangerous_max_127( vm->instr_ctx,
109 3 : "Program log: 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx", r1, r2, r3, r4, r5 );
110 :
111 3 : *_ret = 0UL;
112 3 : return FD_VM_SUCCESS;
113 3 : }
114 :
115 : int
116 : fd_vm_syscall_sol_log_compute_units( /**/ void * _vm,
117 : FD_PARAM_UNUSED ulong r1,
118 : FD_PARAM_UNUSED ulong r2,
119 : FD_PARAM_UNUSED ulong r3,
120 : FD_PARAM_UNUSED ulong r4,
121 : FD_PARAM_UNUSED ulong r5,
122 0 : /**/ ulong * _ret ) {
123 0 : fd_vm_t * vm = (fd_vm_t *)_vm;
124 :
125 : /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L60 */
126 :
127 0 : FD_VM_CU_UPDATE( vm, FD_VM_SYSCALL_BASE_COST );
128 :
129 : /* Max msg_sz: 40 - 3 + 20 = 57 < 127 => we can use printf */
130 0 : fd_log_collector_printf_dangerous_max_127( vm->instr_ctx,
131 0 : "Program consumption: %lu units remaining", vm->cu );
132 :
133 0 : *_ret = 0UL;
134 0 : return FD_VM_SUCCESS;
135 0 : }
136 :
137 : int
138 : fd_vm_syscall_sol_log_pubkey( /**/ void * _vm,
139 : /**/ ulong pubkey_vaddr,
140 : FD_PARAM_UNUSED ulong r2,
141 : FD_PARAM_UNUSED ulong r3,
142 : FD_PARAM_UNUSED ulong r4,
143 : FD_PARAM_UNUSED ulong r5,
144 0 : /**/ ulong * _ret ) {
145 0 : fd_vm_t * vm = (fd_vm_t *)_vm;
146 :
147 : /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L84 */
148 :
149 0 : FD_VM_CU_UPDATE( vm, FD_VM_LOG_PUBKEY_UNITS );
150 :
151 0 : void const * pubkey = FD_VM_MEM_HADDR_LD( vm, pubkey_vaddr, FD_VM_ALIGN_RUST_PUBKEY, sizeof(fd_pubkey_t) );
152 :
153 0 : char msg[ FD_BASE58_ENCODED_32_SZ ]; ulong msg_sz;
154 0 : if( FD_UNLIKELY( fd_base58_encode_32( pubkey, &msg_sz, msg )==NULL ) ) {
155 0 : return FD_VM_SYSCALL_ERR_INVALID_STRING;
156 0 : }
157 :
158 0 : fd_log_collector_program_log( vm->instr_ctx, msg, msg_sz );
159 :
160 0 : *_ret = 0UL;
161 0 : return FD_VM_SUCCESS;
162 0 : }
163 :
164 : int
165 : fd_vm_syscall_sol_log_data( /**/ void * _vm,
166 : /**/ ulong slice_vaddr,
167 : /**/ ulong slice_cnt,
168 : FD_PARAM_UNUSED ulong r3,
169 : FD_PARAM_UNUSED ulong r4,
170 : FD_PARAM_UNUSED ulong r5,
171 3 : /**/ ulong * _ret ) {
172 3 : fd_vm_t * vm = (fd_vm_t *)_vm;
173 :
174 : /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L109
175 :
176 : Note: this is implemented following Agave's perverse behavior.
177 : We need to loop the slice multiple times to match the exact error,
178 : first compute budget, then memory mapping.
179 : And finally we can loop to log. */
180 :
181 : /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L121 */
182 :
183 3 : FD_VM_CU_UPDATE( vm, FD_VM_SYSCALL_BASE_COST );
184 :
185 : /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L123-L128 */
186 :
187 6 : fd_vm_vec_t const * slice = (fd_vm_vec_t const *)FD_VM_MEM_SLICE_HADDR_LD( vm, slice_vaddr, FD_VM_ALIGN_RUST_SLICE_U8_REF,
188 6 : fd_ulong_sat_mul( slice_cnt, sizeof(fd_vm_vec_t) ) );
189 :
190 : /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L130-L135 */
191 :
192 3 : FD_VM_CU_UPDATE( vm, fd_ulong_sat_mul( FD_VM_SYSCALL_BASE_COST, slice_cnt ) );
193 :
194 : /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L136-L141 */
195 :
196 18 : for( ulong i=0UL; i<slice_cnt; i++ ) {
197 15 : FD_VM_CU_UPDATE( vm, slice[i].len );
198 15 : }
199 :
200 : /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L145-L152 */
201 :
202 3 : ulong msg_sz = 14UL; /* "Program data: ", with space */
203 18 : for( ulong i=0UL; i<slice_cnt; i++ ) {
204 15 : ulong cur_len = slice[i].len;
205 : /* This fails the syscall in case of memory mapping issues */
206 30 : FD_VM_MEM_SLICE_HADDR_LD( vm, slice[i].addr, FD_VM_ALIGN_RUST_U8, cur_len );
207 : /* Every buffer will be base64 encoded + space separated */
208 0 : msg_sz += (slice[i].len + 2)/3*4 + (i > 0);
209 30 : }
210 :
211 : /* https://github.com/anza-xyz/agave/blob/v2.0.6/programs/bpf_loader/src/syscalls/logging.rs#L156 */
212 :
213 3 : char msg[ FD_LOG_COLLECTOR_MAX ];
214 3 : ulong bytes_written = fd_log_collector_check_and_truncate( &vm->instr_ctx->txn_ctx->log_collector, msg_sz );
215 3 : if( FD_LIKELY( bytes_written < ULONG_MAX ) ) {
216 3 : fd_memcpy( msg, "Program data: ", 14 );
217 3 : char * buf = msg + 14;
218 :
219 18 : for( ulong i=0UL; i<slice_cnt; i++ ) {
220 15 : ulong cur_len = slice[i].len;
221 30 : void const * bytes = FD_VM_MEM_SLICE_HADDR_LD( vm, slice[i].addr, FD_VM_ALIGN_RUST_U8, cur_len );
222 :
223 15 : if( i ) { *buf = ' '; ++buf; } /* skip first */
224 30 : buf += fd_base64_encode( buf, bytes, cur_len );
225 30 : }
226 3 : FD_TEST( (ulong)(buf-msg)==msg_sz );
227 :
228 3 : fd_log_collector_msg( vm->instr_ctx, msg, msg_sz );
229 3 : }
230 :
231 3 : *_ret = 0;
232 3 : return FD_VM_SUCCESS;
233 3 : }
234 :
235 : int
236 : fd_vm_syscall_sol_alloc_free( /**/ void * _vm,
237 : /**/ ulong sz,
238 : /**/ ulong free_vaddr,
239 : FD_PARAM_UNUSED ulong r3,
240 : FD_PARAM_UNUSED ulong r4,
241 : FD_PARAM_UNUSED ulong r5,
242 0 : /**/ ulong * _ret ) {
243 0 : fd_vm_t * vm = (fd_vm_t *)_vm;
244 :
245 : /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mod.rs#L666 */
246 :
247 : /* This syscall is ... uh ... problematic. But the community has
248 : already recognized this and deprecated it:
249 :
250 : https://github.com/solana-labs/solana/blob/v1.17.23/sdk/src/feature_set.rs#L846
251 :
252 : Unfortunately, old code never dies so, practically, this will need
253 : to be supported until the heat death of the universe.
254 :
255 : The most serious issue is that there is nothing to stop VM code
256 : making a decision based on the _location_ of the returned
257 : allocation. If different validator implementations use different
258 : allocator algorithms, though each implementation would behave
259 : functionally correct in isolation, the VM code that uses it would
260 : actually break consensus.
261 :
262 : As a result, every validator needs to use a bit-for-bit identical
263 : allocation algorithm. Fortunately, Solana is just using a basic
264 : bump allocator:
265 :
266 : https://github.com/solana-labs/solana/blob/v1.17.23/program-runtime/src/invoke_context.rs#L122-L148
267 :
268 : vm->heap_{sz,max} and the below replicate this exactly.
269 :
270 : Another major issue is that this alloc doesn't always conform
271 : typical malloc/free semantics (e.g. C/C++ requires malloc to have
272 : an alignment safe for primitive types ... 8 for the Solana machine
273 : model). This is clearly to support backward compat with older VM
274 : code (though ideally a malloc syscall should have behaved like ...
275 : well ... malloc from day 1). So the alignment behavior below is a
276 : bug-for-bug replication of that:
277 :
278 : https://github.com/solana-labs/solana/blob/v1.17.23/programs/bpf_loader/src/syscalls/mod.rs#L645-L681
279 : https://github.com/solana-labs/solana/blob/v1.17.23/sdk/program/src/entrypoint.rs#L265-L266
280 :
281 : More generally and already ranted about elsewhere, any code that
282 : uses malloc/free style dynamic allocation is inherently broken. So
283 : this syscall should have never existed in the first place ... it
284 : just feeds the trolls. The above is just additional implementation
285 : horror because people consistent think malloc/free is much simpler
286 : than it actually is. This is also an example of how quickly
287 : mistakes fossilize and become a thorn-in-the-side forever.
288 :
289 : IMPORTANT SAFETY TIP! heap_start must be non zero and both
290 : heap_start and heap_end should have an alignment of at least 8.
291 : This existing runtime policies around heap implicitly satisfy this.
292 :
293 : IMPORTANT SAFETY TIP! The specification for Rust's align_offset
294 : doesn't seem to provide a strong guarantee that it will return the
295 : minimal positive offset necessary to align pointers. It is
296 : possible for a "conforming" Rust compiler to break consensus by
297 : using a different align_offset implementation that aligned pointer
298 : between different compilations of the Solana validator and the
299 : below. */
300 :
301 : /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mod.rs#L676-L680 */
302 :
303 0 : ulong align = fd_vm_is_check_align_enabled( vm ) ? 8UL : FD_VM_ALIGN_RUST_U8;
304 :
305 : /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mod.rs#L681-L683
306 : Nothing to do. This section can't error, see:
307 : https://doc.rust-lang.org/1.81.0/src/core/alloc/layout.rs.html#70
308 : https://doc.rust-lang.org/1.81.0/src/core/alloc/layout.rs.html#100 */
309 :
310 :
311 : /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mod.rs#L684
312 : Nothing to do.
313 : TODO: unclear if it throw InstructionError::CallDepth
314 : https://github.com/anza-xyz/agave/blob/v2.0.8/program-runtime/src/invoke_context.rs#L662 */
315 :
316 : /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mod.rs#L685-L693 */
317 :
318 : /* Non-zero free address implies that this is a free() call. Since
319 : this is a bump allocator, free is a no-op. */
320 0 : if( FD_UNLIKELY( free_vaddr ) ) {
321 0 : *_ret = 0UL;
322 0 : return FD_VM_SUCCESS;
323 0 : }
324 :
325 :
326 0 : ulong heap_sz = fd_ulong_align_up( vm->heap_sz, align );
327 0 : ulong heap_vaddr = fd_ulong_sat_add ( heap_sz, FD_VM_MEM_MAP_HEAP_REGION_START );
328 0 : /**/ heap_sz = fd_ulong_sat_add ( heap_sz, sz );
329 :
330 0 : if( FD_UNLIKELY( heap_sz > vm->heap_max ) ) { /* Not enough free memory */
331 0 : *_ret = 0UL;
332 0 : return FD_VM_SUCCESS;
333 0 : }
334 :
335 0 : vm->heap_sz = heap_sz;
336 :
337 0 : *_ret = heap_vaddr;
338 0 : return FD_VM_SUCCESS;
339 0 : }
340 :
341 : /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mem_ops.rs#L145 */
342 : int
343 : fd_vm_memmove( fd_vm_t * vm,
344 : ulong dst_vaddr,
345 : ulong src_vaddr,
346 66 : ulong sz ) {
347 66 : if( FD_UNLIKELY( !sz ) ) {
348 0 : return FD_VM_SUCCESS;
349 0 : }
350 :
351 66 : if( !vm->direct_mapping ) {
352 24 : void * dst = FD_VM_MEM_HADDR_ST( vm, dst_vaddr, FD_VM_ALIGN_RUST_U8, sz );
353 48 : void const * src = FD_VM_MEM_HADDR_LD( vm, src_vaddr, FD_VM_ALIGN_RUST_U8, sz );
354 0 : memmove( dst, src, sz );
355 48 : } else {
356 : /* If the src and dst vaddrs overlap and src_vaddr < dst_vaddr, Agave iterates through input regions backwards
357 : to maintain correct memmove behavior for overlapping cases. Although this logic should only apply to the src and dst
358 : vaddrs being in the input data region (since that is the only possible case you could have overlapping, chunked-up memmoves),
359 : Agave will iterate backwards in ANY region. If it eventually reaches the end of a region after iterating backwards and
360 : hits an access violation, the bytes from [region_begin, start_vaddr] will still be written to, causing fuzzing mismatches.
361 : In this case, if we didn't have the reverse flag, we would have thrown an access violation before any bytes were copied.
362 : The same logic applies to memmoves that go past the high end of a region - reverse iteration logic would throw an access
363 : violation before any bytes were copied, while the current logic would copy the bytes until the end of the region.
364 : https://github.com/anza-xyz/agave/blob/v2.1.0/programs/bpf_loader/src/syscalls/mem_ops.rs#L184 */
365 42 : uchar reverse = !!( dst_vaddr >= src_vaddr && dst_vaddr - src_vaddr < sz );
366 :
367 : /* In reverse calculations, start from the rightmost vaddr that will be accessed (note the - 1). */
368 42 : ulong dst_vaddr_begin = reverse ? fd_ulong_sat_add( dst_vaddr, sz - 1UL ) : dst_vaddr;
369 42 : ulong src_vaddr_begin = reverse ? fd_ulong_sat_add( src_vaddr, sz - 1UL ) : src_vaddr;
370 :
371 : /* Find the correct src and dst haddrs to start operating from. If the src or dst vaddrs
372 : belong to the input data region (4), keep track of region statistics to memmove in chunks. */
373 42 : ulong dst_region = FD_VADDR_TO_REGION( dst_vaddr_begin );
374 42 : uchar dst_is_input_mem_region = ( dst_region==FD_VM_INPUT_REGION );
375 42 : ulong dst_offset = dst_vaddr_begin & FD_VM_OFFSET_MASK;
376 42 : ulong dst_region_idx = 0UL;
377 42 : ulong dst_bytes_rem_in_cur_region;
378 42 : uchar * dst_haddr;
379 42 : if( dst_is_input_mem_region ) {
380 18 : FD_VM_MEM_HADDR_AND_REGION_IDX_FROM_INPUT_REGION_CHECKED( vm, dst_offset, dst_region_idx, dst_haddr );
381 18 : if( FD_UNLIKELY( !vm->input_mem_regions[ dst_region_idx ].is_writable ) ) {
382 0 : FD_VM_ERR_FOR_LOG_EBPF( vm, FD_VM_ERR_EBPF_ACCESS_VIOLATION );
383 0 : return FD_VM_SYSCALL_ERR_SEGFAULT;
384 0 : }
385 18 : if( FD_UNLIKELY( reverse ) ) {
386 : /* Bytes remaining between region begin and current position (+ 1 for inclusive region beginning). */
387 6 : dst_bytes_rem_in_cur_region = fd_ulong_sat_sub( dst_offset + 1UL, vm->input_mem_regions[ dst_region_idx ].vaddr_offset );
388 12 : } else {
389 : /* Bytes remaining between current position and region end. */
390 12 : dst_bytes_rem_in_cur_region = fd_ulong_sat_sub( vm->input_mem_regions[ dst_region_idx ].region_sz, ( dst_offset - vm->input_mem_regions[ dst_region_idx ].vaddr_offset ) );
391 12 : }
392 24 : } else {
393 24 : dst_haddr = (uchar*)FD_VM_MEM_HADDR_ST_NO_SZ_CHECK( vm, dst_vaddr_begin, FD_VM_ALIGN_RUST_U8 );
394 :
395 18 : if( FD_UNLIKELY( reverse ) ) {
396 : /* Bytes remaining is minimum of the offset from the beginning of the current
397 : region (+1 for inclusive region beginning) and the number of storable bytes in the region. */
398 3 : dst_bytes_rem_in_cur_region = fd_ulong_min( vm->region_st_sz[ dst_region ], dst_offset + 1UL );
399 :
400 15 : } else {
401 : /* Bytes remaining is the number of writable bytes left in the region */
402 15 : dst_bytes_rem_in_cur_region = fd_ulong_sat_sub( vm->region_st_sz[ dst_region ], dst_offset );
403 15 : }
404 18 : }
405 :
406 : /* Logic for src vaddr translation is similar to above excluding any writable checks. */
407 36 : ulong src_region = FD_VADDR_TO_REGION( src_vaddr_begin );
408 36 : uchar src_is_input_mem_region = ( src_region==FD_VM_INPUT_REGION );
409 36 : ulong src_offset = src_vaddr_begin & FD_VM_OFFSET_MASK;
410 36 : ulong src_region_idx = 0UL;
411 36 : ulong src_bytes_rem_in_cur_region;
412 36 : uchar * src_haddr;
413 36 : if( src_is_input_mem_region ) {
414 18 : FD_VM_MEM_HADDR_AND_REGION_IDX_FROM_INPUT_REGION_CHECKED( vm, src_offset, src_region_idx, src_haddr );
415 18 : if( FD_UNLIKELY( reverse ) ) {
416 6 : src_bytes_rem_in_cur_region = fd_ulong_sat_sub( src_offset + 1UL, vm->input_mem_regions[ src_region_idx ].vaddr_offset );
417 12 : } else {
418 12 : src_bytes_rem_in_cur_region = fd_ulong_sat_sub( vm->input_mem_regions[ src_region_idx ].region_sz, ( src_offset - vm->input_mem_regions[ src_region_idx ].vaddr_offset ) );
419 12 : }
420 18 : } else {
421 54 : src_haddr = (uchar*)FD_VM_MEM_HADDR_LD_NO_SZ_CHECK( vm, src_vaddr_begin, FD_VM_ALIGN_RUST_U8 );
422 :
423 18 : if( FD_UNLIKELY( reverse ) ) {
424 3 : src_bytes_rem_in_cur_region = fd_ulong_min( vm->region_ld_sz[ src_region ], src_offset + 1UL );
425 :
426 15 : } else {
427 15 : src_bytes_rem_in_cur_region = fd_ulong_sat_sub( vm->region_ld_sz[ src_region ], src_offset );
428 15 : }
429 54 : }
430 :
431 : /* Short circuit: if the number of copyable bytes stays within all memory regions,
432 : just memmove and return. This is a majority case in mainnet, devnet, and testnet.
433 : Someone would have to be very crafty and clever to construct a transaction that
434 : deploys and invokes a custom program that does not fall into this branch. */
435 36 : if( FD_LIKELY( sz<=dst_bytes_rem_in_cur_region && sz<=src_bytes_rem_in_cur_region ) ) {
436 21 : if( FD_UNLIKELY( reverse ) ) {
437 : /* In the reverse iteration case, the haddrs point to the end of the region here. Since the
438 : above checks guarantee that there are enough bytes left in the src and dst regions to do
439 : a direct memmove, we can just subtract (sz-1) from the haddrs, memmove, and return. */
440 3 : memmove( dst_haddr - sz + 1UL, src_haddr - sz + 1UL, sz );
441 18 : } else {
442 : /* In normal iteration, the haddrs correspond to the correct starting point for the memcpy,
443 : so no further translation has to be done. */
444 18 : memmove( dst_haddr, src_haddr, sz );
445 18 : }
446 21 : return FD_VM_SUCCESS;
447 21 : }
448 :
449 : /* Copy over the bytes from each region in chunks. */
450 57 : while( sz>0UL ) {
451 : /* End of region case */
452 45 : if( FD_UNLIKELY( src_bytes_rem_in_cur_region==0UL ) ) {
453 : /* Same as above, except no writable checks. */
454 30 : if( FD_LIKELY( !reverse &&
455 30 : src_is_input_mem_region &&
456 30 : src_region_idx+1UL<vm->input_mem_regions_cnt ) ) {
457 12 : if( FD_UNLIKELY( vm->input_mem_regions[ src_region_idx+1UL ].is_acct_data != vm->input_mem_regions[ src_region_idx ].is_acct_data ) ) {
458 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_LENGTH );
459 0 : return FD_VM_SYSCALL_ERR_SEGFAULT;
460 0 : }
461 12 : src_region_idx++;
462 12 : src_haddr = (uchar*)vm->input_mem_regions[ src_region_idx ].haddr;
463 18 : } else if( FD_LIKELY( reverse && src_region_idx>0UL ) ) {
464 15 : if( FD_UNLIKELY( vm->input_mem_regions[ src_region_idx-1UL ].is_acct_data != vm->input_mem_regions[ src_region_idx ].is_acct_data ) ) {
465 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_LENGTH );
466 0 : return FD_VM_SYSCALL_ERR_SEGFAULT;
467 0 : }
468 15 : src_region_idx--;
469 15 : src_haddr = (uchar*)vm->input_mem_regions[ src_region_idx ].haddr + vm->input_mem_regions[ src_region_idx ].region_sz - 1UL;
470 15 : } else {
471 3 : FD_VM_ERR_FOR_LOG_EBPF( vm, FD_VM_ERR_EBPF_ACCESS_VIOLATION );
472 3 : return FD_VM_SYSCALL_ERR_SEGFAULT;
473 3 : }
474 27 : src_bytes_rem_in_cur_region = vm->input_mem_regions[ src_region_idx ].region_sz;
475 27 : }
476 42 : if( FD_UNLIKELY( dst_bytes_rem_in_cur_region==0UL ) ) {
477 : /* Only proceed if:
478 : - We are in the input memory region
479 : - There are remaining input memory regions to copy from (for both regular and reverse iteration orders)
480 : - The next input memory region is writable
481 : Fail otherwise. */
482 9 : if( FD_LIKELY( !reverse &&
483 9 : dst_is_input_mem_region &&
484 9 : dst_region_idx+1UL<vm->input_mem_regions_cnt &&
485 9 : vm->input_mem_regions[ dst_region_idx+1UL ].is_writable ) ) {
486 6 : if( FD_UNLIKELY( vm->input_mem_regions[ dst_region_idx+1UL ].is_acct_data != vm->input_mem_regions[ dst_region_idx ].is_acct_data ) ) {
487 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_LENGTH );
488 0 : return FD_VM_SYSCALL_ERR_SEGFAULT;
489 0 : }
490 : /* In normal iteration, we move the haddr to the beginning of the next region. */
491 6 : dst_region_idx++;
492 6 : dst_haddr = (uchar*)vm->input_mem_regions[ dst_region_idx ].haddr;
493 6 : } else if( FD_LIKELY( reverse &&
494 3 : dst_region_idx>0UL &&
495 3 : vm->input_mem_regions[ dst_region_idx-1UL ].is_writable ) ) {
496 3 : if( FD_UNLIKELY( vm->input_mem_regions[ dst_region_idx-1UL ].is_acct_data != vm->input_mem_regions[ dst_region_idx ].is_acct_data ) ) {
497 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_LENGTH );
498 0 : return FD_VM_SYSCALL_ERR_SEGFAULT;
499 0 : }
500 : /* Note that when reverse iterating, we set the haddr to the END of the PREVIOUS region. */
501 3 : dst_region_idx--;
502 3 : dst_haddr = (uchar*)vm->input_mem_regions[ dst_region_idx ].haddr + vm->input_mem_regions[ dst_region_idx ].region_sz - 1UL;
503 3 : } else {
504 0 : FD_VM_ERR_FOR_LOG_EBPF( vm, FD_VM_ERR_EBPF_ACCESS_VIOLATION );
505 0 : return FD_VM_SYSCALL_ERR_SEGFAULT;
506 0 : }
507 9 : dst_bytes_rem_in_cur_region = vm->input_mem_regions[ dst_region_idx ].region_sz;
508 9 : }
509 :
510 : /* Number of bytes to operate on in this iteration is the min of:
511 : - number of bytes left to copy
512 : - bytes left in the current src region
513 : - bytes left in the current dst region */
514 42 : ulong num_bytes_to_copy = fd_ulong_min( sz, fd_ulong_min( src_bytes_rem_in_cur_region, dst_bytes_rem_in_cur_region ) );
515 42 : if( FD_UNLIKELY( reverse ) ) {
516 21 : memmove( dst_haddr - num_bytes_to_copy + 1UL, src_haddr - num_bytes_to_copy + 1UL, num_bytes_to_copy );
517 21 : dst_haddr -= num_bytes_to_copy;
518 21 : src_haddr -= num_bytes_to_copy;
519 21 : } else {
520 21 : memmove( dst_haddr, src_haddr, num_bytes_to_copy );
521 21 : dst_haddr += num_bytes_to_copy;
522 21 : src_haddr += num_bytes_to_copy;
523 21 : }
524 :
525 : /* Update size trackers */
526 42 : sz -= num_bytes_to_copy;
527 42 : src_bytes_rem_in_cur_region -= num_bytes_to_copy;
528 42 : dst_bytes_rem_in_cur_region -= num_bytes_to_copy;
529 42 : }
530 15 : }
531 :
532 27 : return FD_VM_SUCCESS;
533 66 : }
534 :
535 : /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mem_ops.rs#L41 */
536 : int
537 : fd_vm_syscall_sol_memmove( /**/ void * _vm,
538 : /**/ ulong dst_vaddr,
539 : /**/ ulong src_vaddr,
540 : /**/ ulong sz,
541 : FD_PARAM_UNUSED ulong r4,
542 : FD_PARAM_UNUSED ulong r5,
543 36 : /**/ ulong * _ret ) {
544 36 : *_ret = 0;
545 36 : fd_vm_t * vm = (fd_vm_t *)_vm;
546 :
547 36 : FD_VM_CU_MEM_OP_UPDATE( vm, sz );
548 :
549 : /* No overlap check for memmove. */
550 0 : return fd_vm_memmove( vm, dst_vaddr, src_vaddr, sz );
551 36 : }
552 :
553 : /* https://github.com/anza-xyz/agave/blob/v2.0.8/programs/bpf_loader/src/syscalls/mem_ops.rs#L18 */
554 : int
555 : fd_vm_syscall_sol_memcpy( /**/ void * _vm,
556 : /**/ ulong dst_vaddr,
557 : /**/ ulong src_vaddr,
558 : /**/ ulong sz,
559 : FD_PARAM_UNUSED ulong r4,
560 : FD_PARAM_UNUSED ulong r5,
561 48 : /**/ ulong * _ret ) {
562 48 : *_ret = 0;
563 48 : fd_vm_t * vm = (fd_vm_t *)_vm;
564 :
565 48 : FD_VM_CU_MEM_OP_UPDATE( vm, sz );
566 :
567 : /* Exact same as memmove, except also check overlap.
568 : https://github.com/anza-xyz/agave/blob/master/programs/bpf_loader/src/syscalls/mem_ops.rs#L31 */
569 48 : FD_VM_MEM_CHECK_NON_OVERLAPPING( vm, src_vaddr, sz, dst_vaddr, sz );
570 :
571 30 : return fd_vm_memmove( vm, dst_vaddr, src_vaddr, sz );
572 48 : }
573 :
574 : int
575 : fd_vm_syscall_sol_memcmp( /**/ void * _vm,
576 : /**/ ulong m0_vaddr,
577 : /**/ ulong m1_vaddr,
578 : /**/ ulong sz,
579 : /**/ ulong out_vaddr,
580 : FD_PARAM_UNUSED ulong r5,
581 15 : /**/ ulong * _ret ) {
582 15 : *_ret = 0;
583 15 : fd_vm_t * vm = (fd_vm_t *)_vm;
584 :
585 : /* https://github.com/anza-xyz/agave/blob/master/programs/bpf_loader/src/syscalls/mem_ops.rs#L59 */
586 :
587 15 : FD_VM_CU_MEM_OP_UPDATE( vm, sz );
588 :
589 : /* Note: though this behaves like a normal C-style memcmp, we can't
590 : use the compilers / libc memcmp directly because the specification
591 : doesn't provide strong enough guarantees about the return value (it
592 : only promises the sign). */
593 :
594 15 : if( !vm->direct_mapping ) {
595 0 : uchar const * m0 = (uchar const *)FD_VM_MEM_SLICE_HADDR_LD( vm, m0_vaddr, FD_VM_ALIGN_RUST_U8, sz );
596 0 : uchar const * m1 = (uchar const *)FD_VM_MEM_SLICE_HADDR_LD( vm, m1_vaddr, FD_VM_ALIGN_RUST_U8, sz );
597 :
598 : /* Silly that this doesn't use r0 to return ... slower, more edge
599 : case, different from libc style memcmp, harder to callers to use,
600 : etc ... probably too late to do anything about it now ... sigh */
601 :
602 0 : void * _out = FD_VM_MEM_HADDR_ST( vm, out_vaddr, FD_VM_ALIGN_RUST_I32, 4UL );
603 :
604 0 : int out = 0;
605 0 : for( ulong i=0UL; i<sz; i++ ) {
606 0 : int i0 = (int)m0[i];
607 0 : int i1 = (int)m1[i];
608 0 : if( i0!=i1 ) {
609 0 : out = i0 - i1;
610 0 : break;
611 0 : }
612 0 : }
613 :
614 0 : fd_memcpy( _out, &out, 4UL ); /* Sigh ... see note above (and might be unaligned ... double sigh) */
615 :
616 0 : return FD_VM_SUCCESS;
617 15 : } else {
618 : /* In the case that direct mapping is enabled, the behavior for memcmps
619 : differ significantly from the non-dm case. The key difference is that
620 : invalid loads will instantly lead to errors in the non-dm case. However,
621 : when direct mapping is enabled, we will first try to memcmp the largest
622 : size valid chunk first, and will exit successfully if a difference is
623 : found without aborting from the VM. A chunk is defined as the largest
624 : valid vaddr range in both memory regions that doesn't span multiple
625 : regions.
626 :
627 : Example:
628 : fd_vm_syscall_sol_memcmp( vm, m0_addr : 0x4000, m1_vaddr : 0x2000, 0x200, ... );
629 : m0's region: m0_addr 0x4000 -> 0x4000 + 0x50 (region sz 0x50)
630 : m1's region: m1_addr 0x2000 -> 0x2000 + 0x100 (region sz 0x100)
631 : sz: 0x200
632 :
633 : Case 1: 0x4000 -> 0x4050 does have the same bytes as 0x2000 -> 0x2050
634 : Case 2: 0x4000 -> 0x4050 does NOT have the same bytes as 0x2000 -> 0x2050
635 :
636 : Pre-DM:
637 : This will fail out before any bytes are compared because the memory
638 : translation is done first.
639 :
640 : Post-DM:
641 : For case 1, the memcmp will return an error and the VM will exit because
642 : the memcmp will eventually try to access 0x4051 which is invalid. First
643 : 0x50 bytes are compared, but the next chunk will lead to an invalid
644 : access.
645 :
646 : For case 2, the memcmp will first translate the first 0x50 bytes and will
647 : see that the bytes are not the same. This will lead to the syscall
648 : exiting out successfully without detecting the access violation.
649 :
650 : https://github.com/anza-xyz/agave/blob/v2.0.10/programs/bpf_loader/src/syscalls/mem_ops.rs#L213
651 : */
652 :
653 15 : void * _out = FD_VM_MEM_HADDR_ST( vm, out_vaddr, FD_VM_ALIGN_RUST_I32, 4UL );
654 0 : int out = 0;
655 :
656 : /* Lookup host address chunks. Try to do a standard memcpy if the regions
657 : do not cross memory regions. The translation logic is different if the
658 : the virtual address region is the input region vs. not. See the comment
659 : in fd_bpf_loader_serialization for more details on how the input
660 : region is different from other regions. The input data region will try
661 : to lookup the number of remaining bytes in the specific data region. If
662 : the memory access is not in the input data region, assume the bytes in
663 : the current region are bound by the size of the remaining bytes in the
664 : region. */
665 :
666 12 : ulong m0_region = FD_VADDR_TO_REGION( m0_vaddr );
667 12 : ulong m0_offset = m0_vaddr & FD_VM_OFFSET_MASK;
668 12 : ulong m0_region_idx = 0UL;
669 12 : ulong m0_bytes_in_cur_region = sz;
670 12 : uchar * m0_haddr = NULL;
671 12 : if( m0_region==FD_VM_INPUT_REGION ) {
672 6 : m0_region_idx = fd_vm_get_input_mem_region_idx( vm, m0_offset );
673 6 : m0_haddr = (uchar*)(vm->input_mem_regions[ m0_region_idx ].haddr + m0_offset - vm->input_mem_regions[ m0_region_idx ].vaddr_offset);
674 6 : m0_bytes_in_cur_region = fd_ulong_min( sz, fd_ulong_sat_sub( vm->input_mem_regions[ m0_region_idx ].region_sz,
675 6 : ((ulong)m0_haddr - vm->input_mem_regions[ m0_region_idx ].haddr) ) );
676 6 : } else {
677 : /* We can safely load a slice of 1 byte here because we know that we will
678 : not ever read more than the number of bytes that are left in the
679 : region. */
680 6 : m0_bytes_in_cur_region = fd_ulong_min( sz, vm->region_ld_sz[ m0_region ] - m0_offset );
681 12 : m0_haddr = (uchar *)FD_VM_MEM_SLICE_HADDR_LD_SZ_UNCHECKED( vm, m0_vaddr, FD_VM_ALIGN_RUST_U8 );
682 12 : }
683 :
684 12 : ulong m1_region = FD_VADDR_TO_REGION( m1_vaddr );
685 12 : ulong m1_offset = m1_vaddr & FD_VM_OFFSET_MASK;
686 12 : ulong m1_region_idx = 0UL;
687 12 : ulong m1_bytes_in_cur_region = sz;
688 12 : uchar * m1_haddr = NULL;
689 12 : if( m1_region==FD_VM_INPUT_REGION ) {
690 6 : m1_region_idx = fd_vm_get_input_mem_region_idx( vm, m1_offset );
691 6 : m1_haddr = (uchar*)(vm->input_mem_regions[ m1_region_idx ].haddr + m1_offset - vm->input_mem_regions[ m1_region_idx ].vaddr_offset);
692 6 : m1_bytes_in_cur_region = fd_ulong_min( sz, fd_ulong_sat_sub( vm->input_mem_regions[ m1_region_idx ].region_sz,
693 6 : ((ulong)m1_haddr - vm->input_mem_regions[ m1_region_idx ].haddr) ) );
694 6 : } else {
695 6 : m1_bytes_in_cur_region = fd_ulong_min( sz, vm->region_ld_sz[ m1_region ] - m1_offset );
696 12 : m1_haddr = (uchar *)FD_VM_MEM_SLICE_HADDR_LD_SZ_UNCHECKED( vm, m1_vaddr, FD_VM_ALIGN_RUST_U8 );
697 12 : }
698 :
699 : /* Case where the operation spans multiple regions. Copy over the bytes
700 : from each region while iterating to the next one. */
701 : /* TODO: An optimization would be to memcmp chunks at once */
702 12 : ulong m0_idx = 0UL;
703 12 : ulong m1_idx = 0UL;
704 612 : for( ulong i=0UL; i<sz; i++ ) {
705 609 : if( FD_UNLIKELY( !m0_bytes_in_cur_region ) ) {
706 : /* If the memory is not in the input region or it is the last input
707 : memory region, that means that if we don't exit now we will have
708 : an access violation. */
709 6 : if( FD_UNLIKELY( m0_region!=FD_VM_INPUT_REGION || ++m0_region_idx>=vm->input_mem_regions_cnt ) ) {
710 0 : FD_VM_ERR_FOR_LOG_EBPF( vm, FD_VM_ERR_EBPF_ACCESS_VIOLATION );
711 0 : return FD_VM_SYSCALL_ERR_SEGFAULT;
712 0 : }
713 6 : if( FD_UNLIKELY( vm->input_mem_regions[ m0_region_idx-1UL ].is_acct_data != vm->input_mem_regions[ m0_region_idx ].is_acct_data ) ) {
714 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_LENGTH );
715 0 : return FD_VM_SYSCALL_ERR_SEGFAULT;
716 0 : }
717 : /* Otherwise, query the next input region. */
718 6 : m0_haddr = (uchar*)vm->input_mem_regions[ m0_region_idx ].haddr;
719 6 : m0_idx = 0UL;
720 6 : m0_bytes_in_cur_region = vm->input_mem_regions[ m0_region_idx ].region_sz;
721 6 : }
722 609 : if( FD_UNLIKELY( !m1_bytes_in_cur_region ) ) {
723 0 : if( FD_UNLIKELY( m1_region!=FD_VM_INPUT_REGION || ++m1_region_idx>=vm->input_mem_regions_cnt ) ) {
724 0 : FD_VM_ERR_FOR_LOG_EBPF( vm, FD_VM_ERR_EBPF_ACCESS_VIOLATION );
725 0 : return FD_VM_SYSCALL_ERR_SEGFAULT;
726 0 : }
727 0 : if( FD_UNLIKELY( vm->input_mem_regions[ m1_region_idx-1UL ].is_acct_data != vm->input_mem_regions[ m1_region_idx ].is_acct_data ) ) {
728 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_LENGTH );
729 0 : return FD_VM_SYSCALL_ERR_SEGFAULT;
730 0 : }
731 0 : m1_haddr = (uchar*)vm->input_mem_regions[ m1_region_idx ].haddr;
732 0 : m1_idx = 0UL;
733 0 : m1_bytes_in_cur_region = vm->input_mem_regions[ m1_region_idx ].region_sz;
734 0 : }
735 :
736 609 : int i0 = (int)m0_haddr[ m0_idx ];
737 609 : int i1 = (int)m1_haddr[ m1_idx ];
738 609 : if( i0!=i1 ) {
739 9 : out = i0 - i1;
740 9 : break;
741 9 : }
742 :
743 600 : m0_bytes_in_cur_region--;
744 600 : m1_bytes_in_cur_region--;
745 600 : m0_idx++;
746 600 : m1_idx++;
747 600 : }
748 12 : fd_memcpy( _out, &out, 4UL ); /* Sigh ... see note above (and might be unaligned ... double sigh) */
749 12 : return FD_VM_SUCCESS;
750 12 : }
751 15 : }
752 :
753 : int
754 : fd_vm_syscall_sol_memset( /**/ void * _vm,
755 : /**/ ulong dst_vaddr,
756 : /**/ ulong c,
757 : /**/ ulong sz,
758 : FD_PARAM_UNUSED ulong r4,
759 : FD_PARAM_UNUSED ulong r5,
760 27 : /**/ ulong * _ret ) {
761 27 : fd_vm_t * vm = (fd_vm_t *)_vm;
762 27 : *_ret = 0;
763 :
764 : /* https://github.com/anza-xyz/agave/blob/master/programs/bpf_loader/src/syscalls/mem_ops.rs#L115 */
765 :
766 27 : FD_VM_CU_MEM_OP_UPDATE( vm, sz );
767 :
768 27 : if( FD_UNLIKELY( !sz ) ) {
769 0 : return FD_VM_SUCCESS;
770 0 : }
771 :
772 27 : ulong region = FD_VADDR_TO_REGION( dst_vaddr );
773 27 : ulong offset = dst_vaddr & FD_VM_OFFSET_MASK;
774 27 : uchar * haddr;
775 :
776 27 : int b = (int)(c & 255UL);
777 :
778 27 : if( !vm->direct_mapping ) {
779 9 : haddr = FD_VM_MEM_HADDR_ST( vm, dst_vaddr, FD_VM_ALIGN_RUST_U8, sz );
780 0 : fd_memset( haddr, b, sz );
781 18 : } else if( region!=FD_VM_INPUT_REGION ) {
782 : /* Here we special case non-input region memsets: we try to memset
783 : as many bytes as possible until it reaches an unwritable section.
784 : This is done in order to ensure error-code conformance with
785 : Agave. */
786 9 : haddr = (uchar*)FD_VM_MEM_HADDR_ST_FAST( vm, dst_vaddr );
787 9 : ulong bytes_in_cur_region = fd_ulong_sat_sub( vm->region_st_sz[ region ], offset );
788 9 : ulong bytes_to_set = fd_ulong_min( sz, bytes_in_cur_region );
789 9 : fd_memset( haddr, b, bytes_to_set );
790 9 : if( FD_UNLIKELY( bytes_to_set<sz ) ) {
791 3 : FD_VM_ERR_FOR_LOG_EBPF( vm, FD_VM_ERR_EBPF_ACCESS_VIOLATION );
792 3 : return FD_VM_SYSCALL_ERR_SEGFAULT;
793 3 : }
794 9 : } else {
795 : /* In this case, we are in the input region AND direct mapping is
796 : enabled. Get the haddr and input region and check if it's
797 : writable. This means that we may potentially iterate over
798 : multiple regions. */
799 9 : ulong region_idx;
800 9 : FD_VM_MEM_HADDR_AND_REGION_IDX_FROM_INPUT_REGION_CHECKED( vm, offset, region_idx, haddr );
801 0 : ulong offset_in_cur_region = offset - vm->input_mem_regions[ region_idx ].vaddr_offset;
802 9 : ulong bytes_in_cur_region = fd_ulong_sat_sub( vm->input_mem_regions[ region_idx ].region_sz, offset_in_cur_region );
803 :
804 : /* Check that current region is writable */
805 9 : if( FD_UNLIKELY( !vm->input_mem_regions[ region_idx ].is_writable ) ) {
806 0 : FD_VM_ERR_FOR_LOG_EBPF( vm, FD_VM_ERR_EBPF_ACCESS_VIOLATION );
807 0 : return FD_VM_SYSCALL_ERR_SEGFAULT;
808 0 : }
809 :
810 : /* Memset goes into multiple regions. */
811 30 : while( sz>0UL ) {
812 :
813 : /* Memset bytes */
814 30 : ulong num_bytes_to_set = fd_ulong_min( sz, bytes_in_cur_region );
815 30 : fd_memset( haddr, b, num_bytes_to_set );
816 30 : sz -= num_bytes_to_set;
817 :
818 30 : if( !sz ) {
819 6 : break;
820 6 : }
821 :
822 : /* If no more regions left, break. */
823 24 : if( ++region_idx==vm->input_mem_regions_cnt ) {
824 0 : break;
825 0 : }
826 :
827 : /* Check that new region is writable. */
828 24 : if( FD_UNLIKELY( !vm->input_mem_regions[ region_idx ].is_writable ) ) {
829 3 : break;
830 3 : }
831 :
832 : /* If new region crosses into/out of account region, error out. */
833 21 : if( FD_UNLIKELY( vm->input_mem_regions[ region_idx ].is_acct_data !=
834 21 : vm->input_mem_regions[ region_idx-1UL ].is_acct_data ) ) {
835 0 : FD_VM_ERR_FOR_LOG_SYSCALL( vm, FD_VM_SYSCALL_ERR_INVALID_LENGTH );
836 0 : return FD_VM_SYSCALL_ERR_SEGFAULT;
837 0 : }
838 :
839 : /* Move haddr to next region. */
840 21 : haddr = (uchar*)vm->input_mem_regions[ region_idx ].haddr;
841 21 : bytes_in_cur_region = vm->input_mem_regions[ region_idx ].region_sz;
842 21 : }
843 :
844 : /* If we were not able to successfully set all the bytes, throw an error. */
845 9 : if( FD_UNLIKELY( sz>0 ) ) {
846 3 : FD_VM_ERR_FOR_LOG_EBPF( vm, FD_VM_ERR_EBPF_ACCESS_VIOLATION );
847 3 : return FD_VM_SYSCALL_ERR_SEGFAULT;
848 3 : }
849 9 : }
850 18 : return FD_VM_SUCCESS;
851 27 : }
|