@@ -86,6 +86,21 @@ use rbpf::lib::{Error, ErrorKind};
86
86
// Cargo.toml file (see comments above), so here we use just the hardcoded bytecode instructions
87
87
// instead.
88
88
89
+ #[ cfg( all( not( windows) , not( feature="std" ) ) ) ]
90
+ fn alloc_exec_memory ( ) -> Box < [ u8 ] > {
91
+ let size = 4096 ;
92
+ let layout = std:: alloc:: Layout :: from_size_align ( size, 4096 ) . unwrap ( ) ;
93
+ unsafe {
94
+ let ptr = std:: alloc:: alloc ( layout) ;
95
+ assert ! ( !ptr. is_null( ) , "Failed to allocate memory" ) ;
96
+
97
+ libc:: mprotect ( ptr. cast ( ) , size, libc:: PROT_EXEC | libc:: PROT_WRITE ) ;
98
+
99
+ let slice = std:: slice:: from_raw_parts_mut ( ptr, size) ;
100
+ Box :: from_raw ( slice)
101
+ }
102
+ }
103
+
89
104
#[ test]
90
105
#[ cfg( feature = "std" ) ]
91
106
fn test_vm_block_port ( ) {
@@ -320,7 +335,7 @@ fn test_vm_mbuff_with_rust_api() {
320
335
321
336
// Program and memory come from uBPF test ldxh.
322
337
#[ test]
323
- #[ cfg( all ( not( windows) , feature = "std" ) ) ]
338
+ #[ cfg( not( windows) ) ]
324
339
fn test_jit_mbuff ( ) {
325
340
#[ rustfmt:: skip]
326
341
let prog = & [
@@ -342,12 +357,16 @@ fn test_jit_mbuff() {
342
357
343
358
unsafe {
344
359
let mut vm = rbpf:: EbpfVmMbuff :: new ( Some ( prog) ) . unwrap ( ) ;
360
+ #[ cfg( not( feature = "std" ) ) ]
361
+ let mut exec_mem = alloc_exec_memory ( ) ;
362
+ #[ cfg( not( feature = "std" ) ) ]
363
+ vm. set_jit_exec_memory ( & mut exec_mem) . unwrap ( ) ;
345
364
vm. jit_compile ( ) . unwrap ( ) ;
346
365
assert_eq ! ( vm. execute_program_jit( mem, & mut mbuff) . unwrap( ) , 0x2211 ) ;
347
366
}
348
367
}
349
368
350
- #[ cfg( all ( not( windows) , feature = "std" ) ) ]
369
+ #[ cfg( not( windows) ) ]
351
370
#[ test]
352
371
fn test_vm_jit_ldabsb ( ) {
353
372
#[ rustfmt:: skip]
@@ -363,13 +382,17 @@ fn test_vm_jit_ldabsb() {
363
382
let mut vm = rbpf:: EbpfVmRaw :: new ( Some ( prog) ) . unwrap ( ) ;
364
383
assert_eq ! ( vm. execute_program( mem) . unwrap( ) , 0x33 ) ;
365
384
385
+ #[ cfg( not( feature = "std" ) ) ]
386
+ let mut exec_mem = alloc_exec_memory ( ) ;
387
+ #[ cfg( not( feature = "std" ) ) ]
388
+ vm. set_jit_exec_memory ( & mut exec_mem) . unwrap ( ) ;
366
389
vm. jit_compile ( ) . unwrap ( ) ;
367
390
unsafe {
368
391
assert_eq ! ( vm. execute_program_jit( mem) . unwrap( ) , 0x33 ) ;
369
392
} ;
370
393
}
371
394
372
- #[ cfg( all ( not( windows) , feature = "std" ) ) ]
395
+ #[ cfg( not( windows) ) ]
373
396
#[ test]
374
397
fn test_vm_jit_ldabsh ( ) {
375
398
#[ rustfmt:: skip]
@@ -385,13 +408,17 @@ fn test_vm_jit_ldabsh() {
385
408
let mut vm = rbpf:: EbpfVmRaw :: new ( Some ( prog) ) . unwrap ( ) ;
386
409
assert_eq ! ( vm. execute_program( mem) . unwrap( ) , 0x4433 ) ;
387
410
411
+ #[ cfg( not( feature = "std" ) ) ]
412
+ let mut exec_mem = alloc_exec_memory ( ) ;
413
+ #[ cfg( not( feature = "std" ) ) ]
414
+ vm. set_jit_exec_memory ( & mut exec_mem) . unwrap ( ) ;
388
415
vm. jit_compile ( ) . unwrap ( ) ;
389
416
unsafe {
390
417
assert_eq ! ( vm. execute_program_jit( mem) . unwrap( ) , 0x4433 ) ;
391
418
} ;
392
419
}
393
420
394
- #[ cfg( all ( not( windows) , feature = "std" ) ) ]
421
+ #[ cfg( not( windows) ) ]
395
422
#[ test]
396
423
fn test_vm_jit_ldabsw ( ) {
397
424
#[ rustfmt:: skip]
@@ -406,14 +433,18 @@ fn test_vm_jit_ldabsw() {
406
433
] ;
407
434
let mut vm = rbpf:: EbpfVmRaw :: new ( Some ( prog) ) . unwrap ( ) ;
408
435
assert_eq ! ( vm. execute_program( mem) . unwrap( ) , 0x66554433 ) ;
436
+ #[ cfg( not( feature = "std" ) ) ]
437
+ let mut exec_mem = alloc_exec_memory ( ) ;
438
+ #[ cfg( not( feature = "std" ) ) ]
439
+ vm. set_jit_exec_memory ( & mut exec_mem) . unwrap ( ) ;
409
440
vm. jit_compile ( ) . unwrap ( ) ;
410
441
411
442
unsafe {
412
443
assert_eq ! ( vm. execute_program_jit( mem) . unwrap( ) , 0x66554433 ) ;
413
444
} ;
414
445
}
415
446
416
- #[ cfg( all ( not( windows) , feature = "std" ) ) ]
447
+ #[ cfg( not( windows) ) ]
417
448
#[ test]
418
449
fn test_vm_jit_ldabsdw ( ) {
419
450
#[ rustfmt:: skip]
@@ -428,6 +459,10 @@ fn test_vm_jit_ldabsdw() {
428
459
] ;
429
460
let mut vm = rbpf:: EbpfVmRaw :: new ( Some ( prog) ) . unwrap ( ) ;
430
461
assert_eq ! ( vm. execute_program( mem) . unwrap( ) , 0xaa99887766554433 ) ;
462
+ #[ cfg( not( feature = "std" ) ) ]
463
+ let mut exec_mem = alloc_exec_memory ( ) ;
464
+ #[ cfg( not( feature = "std" ) ) ]
465
+ vm. set_jit_exec_memory ( & mut exec_mem) . unwrap ( ) ;
431
466
vm. jit_compile ( ) . unwrap ( ) ;
432
467
433
468
unsafe {
@@ -468,7 +503,7 @@ fn test_vm_err_ldabsb_nomem() {
468
503
// Memory check not implemented for JIT yet.
469
504
}
470
505
471
- #[ cfg( all ( not( windows) , feature = "std" ) ) ]
506
+ #[ cfg( not( windows) ) ]
472
507
#[ test]
473
508
fn test_vm_jit_ldindb ( ) {
474
509
#[ rustfmt:: skip]
@@ -485,13 +520,17 @@ fn test_vm_jit_ldindb() {
485
520
let mut vm = rbpf:: EbpfVmRaw :: new ( Some ( prog) ) . unwrap ( ) ;
486
521
assert_eq ! ( vm. execute_program( mem) . unwrap( ) , 0x88 ) ;
487
522
523
+ #[ cfg( not( feature = "std" ) ) ]
524
+ let mut exec_mem = alloc_exec_memory ( ) ;
525
+ #[ cfg( not( feature = "std" ) ) ]
526
+ vm. set_jit_exec_memory ( & mut exec_mem) . unwrap ( ) ;
488
527
vm. jit_compile ( ) . unwrap ( ) ;
489
528
unsafe {
490
529
assert_eq ! ( vm. execute_program_jit( mem) . unwrap( ) , 0x88 ) ;
491
530
} ;
492
531
}
493
532
494
- #[ cfg( all ( not( windows) , feature = "std" ) ) ]
533
+ #[ cfg( not( windows) ) ]
495
534
#[ test]
496
535
fn test_vm_jit_ldindh ( ) {
497
536
#[ rustfmt:: skip]
@@ -508,13 +547,17 @@ fn test_vm_jit_ldindh() {
508
547
let mut vm = rbpf:: EbpfVmRaw :: new ( Some ( prog) ) . unwrap ( ) ;
509
548
assert_eq ! ( vm. execute_program( mem) . unwrap( ) , 0x9988 ) ;
510
549
550
+ #[ cfg( not( feature = "std" ) ) ]
551
+ let mut exec_mem = alloc_exec_memory ( ) ;
552
+ #[ cfg( not( feature = "std" ) ) ]
553
+ vm. set_jit_exec_memory ( & mut exec_mem) . unwrap ( ) ;
511
554
vm. jit_compile ( ) . unwrap ( ) ;
512
555
unsafe {
513
556
assert_eq ! ( vm. execute_program_jit( mem) . unwrap( ) , 0x9988 ) ;
514
557
} ;
515
558
}
516
559
517
- #[ cfg( all ( not( windows) , feature = "std" ) ) ]
560
+ #[ cfg( not( windows) ) ]
518
561
#[ test]
519
562
fn test_vm_jit_ldindw ( ) {
520
563
#[ rustfmt:: skip]
@@ -530,14 +573,18 @@ fn test_vm_jit_ldindw() {
530
573
] ;
531
574
let mut vm = rbpf:: EbpfVmRaw :: new ( Some ( prog) ) . unwrap ( ) ;
532
575
assert_eq ! ( vm. execute_program( mem) . unwrap( ) , 0x88776655 ) ;
576
+ #[ cfg( not( feature = "std" ) ) ]
577
+ let mut exec_mem = alloc_exec_memory ( ) ;
578
+ #[ cfg( not( feature = "std" ) ) ]
579
+ vm. set_jit_exec_memory ( & mut exec_mem) . unwrap ( ) ;
533
580
vm. jit_compile ( ) . unwrap ( ) ;
534
581
535
582
unsafe {
536
583
assert_eq ! ( vm. execute_program_jit( mem) . unwrap( ) , 0x88776655 ) ;
537
584
} ;
538
585
}
539
586
540
- #[ cfg( all ( not( windows) , feature = "std" ) ) ]
587
+ #[ cfg( not( windows) ) ]
541
588
#[ test]
542
589
fn test_vm_jit_ldinddw ( ) {
543
590
#[ rustfmt:: skip]
@@ -553,6 +600,10 @@ fn test_vm_jit_ldinddw() {
553
600
] ;
554
601
let mut vm = rbpf:: EbpfVmRaw :: new ( Some ( prog) ) . unwrap ( ) ;
555
602
assert_eq ! ( vm. execute_program( mem) . unwrap( ) , 0xccbbaa9988776655 ) ;
603
+ #[ cfg( not( feature = "std" ) ) ]
604
+ let mut exec_mem = alloc_exec_memory ( ) ;
605
+ #[ cfg( not( feature = "std" ) ) ]
606
+ vm. set_jit_exec_memory ( & mut exec_mem) . unwrap ( ) ;
556
607
vm. jit_compile ( ) . unwrap ( ) ;
557
608
558
609
unsafe {
@@ -662,7 +713,7 @@ fn test_vm_bpf_to_bpf_call() {
662
713
assert_eq ! ( vm_res, 0x10 ) ;
663
714
}
664
715
665
- #[ cfg( all ( not( windows) , feature = "std" ) ) ]
716
+ #[ cfg( not( windows) ) ]
666
717
#[ test]
667
718
fn test_vm_jit_bpf_to_bpf_call ( ) {
668
719
let test_code = assemble (
@@ -684,6 +735,10 @@ fn test_vm_jit_bpf_to_bpf_call() {
684
735
)
685
736
. unwrap ( ) ;
686
737
let mut vm = rbpf:: EbpfVmNoData :: new ( Some ( & test_code) ) . unwrap ( ) ;
738
+ #[ cfg( not( feature = "std" ) ) ]
739
+ let mut exec_mem = alloc_exec_memory ( ) ;
740
+ #[ cfg( not( feature = "std" ) ) ]
741
+ vm. set_jit_exec_memory ( & mut exec_mem) . unwrap ( ) ;
687
742
vm. jit_compile ( ) . unwrap ( ) ;
688
743
let vm_res = unsafe { vm. execute_program_jit ( ) . unwrap ( ) } ;
689
744
assert_eq ! ( vm_res, 0x10 ) ;
@@ -715,7 +770,7 @@ fn test_vm_other_type_call() {
715
770
vm. execute_program ( ) . unwrap ( ) ;
716
771
}
717
772
718
- #[ cfg( all ( not( windows) , feature = "std" ) ) ]
773
+ #[ cfg( not( windows) ) ]
719
774
#[ test]
720
775
#[ should_panic( expected = "[JIT] Error: unexpected call type #2 (insn #0)" ) ]
721
776
fn test_vm_jit_other_type_call ( ) {
@@ -727,6 +782,10 @@ fn test_vm_jit_other_type_call() {
727
782
let mut vm = rbpf:: EbpfVmNoData :: new ( None ) . unwrap ( ) ;
728
783
vm. set_verifier ( |_| Ok ( ( ) ) ) . unwrap ( ) ;
729
784
vm. set_program ( prog) . unwrap ( ) ;
785
+ #[ cfg( not( feature = "std" ) ) ]
786
+ let mut exec_mem = alloc_exec_memory ( ) ;
787
+ #[ cfg( not( feature = "std" ) ) ]
788
+ vm. set_jit_exec_memory ( & mut exec_mem) . unwrap ( ) ;
730
789
vm. jit_compile ( ) . unwrap ( ) ;
731
790
unsafe { vm. execute_program_jit ( ) . unwrap ( ) } ;
732
791
}
0 commit comments