Skip to content

Commit 96a2640

Browse files
committed
feat: Enable running jit tests with --no-default-features
To run jit tests in --no-default-features mode, we rely on libc library for memory mapping. The `misc/test_jit_block_port` test is not modified because it depends on `helpers::bpf_trace_printf` internally. Signed-off-by: Godones <[email protected]>
1 parent e591bb9 commit 96a2640

File tree

3 files changed

+603
-13
lines changed

3 files changed

+603
-13
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ hashbrown = { version = "0.15", default-features = false, features = [
4747
] }
4848

4949
[dev-dependencies]
50-
50+
libc = { version = "0.2" }
5151
elf = "0.0.10"
5252
json = "0.11"
5353
hex = "0.4.3"

tests/misc.rs

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ use rbpf::lib::{Error, ErrorKind};
8686
// Cargo.toml file (see comments above), so here we use just the hardcoded bytecode instructions
8787
// instead.
8888

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+
89104
#[test]
90105
#[cfg(feature = "std")]
91106
fn test_vm_block_port() {
@@ -320,7 +335,7 @@ fn test_vm_mbuff_with_rust_api() {
320335

321336
// Program and memory come from uBPF test ldxh.
322337
#[test]
323-
#[cfg(all(not(windows), feature = "std"))]
338+
#[cfg(not(windows))]
324339
fn test_jit_mbuff() {
325340
#[rustfmt::skip]
326341
let prog = &[
@@ -342,12 +357,16 @@ fn test_jit_mbuff() {
342357

343358
unsafe {
344359
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();
345364
vm.jit_compile().unwrap();
346365
assert_eq!(vm.execute_program_jit(mem, &mut mbuff).unwrap(), 0x2211);
347366
}
348367
}
349368

350-
#[cfg(all(not(windows), feature = "std"))]
369+
#[cfg(not(windows))]
351370
#[test]
352371
fn test_vm_jit_ldabsb() {
353372
#[rustfmt::skip]
@@ -363,13 +382,17 @@ fn test_vm_jit_ldabsb() {
363382
let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
364383
assert_eq!(vm.execute_program(mem).unwrap(), 0x33);
365384

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();
366389
vm.jit_compile().unwrap();
367390
unsafe {
368391
assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x33);
369392
};
370393
}
371394

372-
#[cfg(all(not(windows), feature = "std"))]
395+
#[cfg(not(windows))]
373396
#[test]
374397
fn test_vm_jit_ldabsh() {
375398
#[rustfmt::skip]
@@ -385,13 +408,17 @@ fn test_vm_jit_ldabsh() {
385408
let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
386409
assert_eq!(vm.execute_program(mem).unwrap(), 0x4433);
387410

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();
388415
vm.jit_compile().unwrap();
389416
unsafe {
390417
assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x4433);
391418
};
392419
}
393420

394-
#[cfg(all(not(windows), feature = "std"))]
421+
#[cfg(not(windows))]
395422
#[test]
396423
fn test_vm_jit_ldabsw() {
397424
#[rustfmt::skip]
@@ -406,14 +433,18 @@ fn test_vm_jit_ldabsw() {
406433
];
407434
let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
408435
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();
409440
vm.jit_compile().unwrap();
410441

411442
unsafe {
412443
assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x66554433);
413444
};
414445
}
415446

416-
#[cfg(all(not(windows), feature = "std"))]
447+
#[cfg(not(windows))]
417448
#[test]
418449
fn test_vm_jit_ldabsdw() {
419450
#[rustfmt::skip]
@@ -428,6 +459,10 @@ fn test_vm_jit_ldabsdw() {
428459
];
429460
let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
430461
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();
431466
vm.jit_compile().unwrap();
432467

433468
unsafe {
@@ -468,7 +503,7 @@ fn test_vm_err_ldabsb_nomem() {
468503
// Memory check not implemented for JIT yet.
469504
}
470505

471-
#[cfg(all(not(windows), feature = "std"))]
506+
#[cfg(not(windows))]
472507
#[test]
473508
fn test_vm_jit_ldindb() {
474509
#[rustfmt::skip]
@@ -485,13 +520,17 @@ fn test_vm_jit_ldindb() {
485520
let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
486521
assert_eq!(vm.execute_program(mem).unwrap(), 0x88);
487522

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();
488527
vm.jit_compile().unwrap();
489528
unsafe {
490529
assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x88);
491530
};
492531
}
493532

494-
#[cfg(all(not(windows), feature = "std"))]
533+
#[cfg(not(windows))]
495534
#[test]
496535
fn test_vm_jit_ldindh() {
497536
#[rustfmt::skip]
@@ -508,13 +547,17 @@ fn test_vm_jit_ldindh() {
508547
let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
509548
assert_eq!(vm.execute_program(mem).unwrap(), 0x9988);
510549

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();
511554
vm.jit_compile().unwrap();
512555
unsafe {
513556
assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x9988);
514557
};
515558
}
516559

517-
#[cfg(all(not(windows), feature = "std"))]
560+
#[cfg(not(windows))]
518561
#[test]
519562
fn test_vm_jit_ldindw() {
520563
#[rustfmt::skip]
@@ -530,14 +573,18 @@ fn test_vm_jit_ldindw() {
530573
];
531574
let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
532575
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();
533580
vm.jit_compile().unwrap();
534581

535582
unsafe {
536583
assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x88776655);
537584
};
538585
}
539586

540-
#[cfg(all(not(windows), feature = "std"))]
587+
#[cfg(not(windows))]
541588
#[test]
542589
fn test_vm_jit_ldinddw() {
543590
#[rustfmt::skip]
@@ -553,6 +600,10 @@ fn test_vm_jit_ldinddw() {
553600
];
554601
let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
555602
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();
556607
vm.jit_compile().unwrap();
557608

558609
unsafe {
@@ -662,7 +713,7 @@ fn test_vm_bpf_to_bpf_call() {
662713
assert_eq!(vm_res, 0x10);
663714
}
664715

665-
#[cfg(all(not(windows), feature = "std"))]
716+
#[cfg(not(windows))]
666717
#[test]
667718
fn test_vm_jit_bpf_to_bpf_call() {
668719
let test_code = assemble(
@@ -684,6 +735,10 @@ fn test_vm_jit_bpf_to_bpf_call() {
684735
)
685736
.unwrap();
686737
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();
687742
vm.jit_compile().unwrap();
688743
let vm_res = unsafe { vm.execute_program_jit().unwrap() };
689744
assert_eq!(vm_res, 0x10);
@@ -715,7 +770,7 @@ fn test_vm_other_type_call() {
715770
vm.execute_program().unwrap();
716771
}
717772

718-
#[cfg(all(not(windows), feature = "std"))]
773+
#[cfg(not(windows))]
719774
#[test]
720775
#[should_panic(expected = "[JIT] Error: unexpected call type #2 (insn #0)")]
721776
fn test_vm_jit_other_type_call() {
@@ -727,6 +782,10 @@ fn test_vm_jit_other_type_call() {
727782
let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
728783
vm.set_verifier(|_| Ok(())).unwrap();
729784
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();
730789
vm.jit_compile().unwrap();
731790
unsafe { vm.execute_program_jit().unwrap() };
732791
}

0 commit comments

Comments
 (0)