123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- extern crate rbpf;
- use rbpf::helpers;
- fn main() {
- let prog1 = &[
- 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x0c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- ];
-
-
-
-
- let hkey = helpers::BPF_KTIME_GETNS_IDX as u8;
- let prog2 = &[
- 0xb7, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x85, 0x00, 0x00, 0x00, hkey, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- ];
-
- let mut vm = rbpf::EbpfVmNoData::new(Some(prog1)).unwrap();
-
- assert_eq!(vm.execute_program().unwrap(), 0x3);
-
-
-
-
-
-
-
- vm.set_program(prog2).unwrap();
- vm.register_helper(helpers::BPF_KTIME_GETNS_IDX, helpers::bpf_time_getns)
- .unwrap();
- let time;
- #[cfg(all(not(windows), feature = "std"))]
- {
- vm.jit_compile().unwrap();
- time = unsafe { vm.execute_program_jit().unwrap() };
- }
- #[cfg(any(windows, not(feature = "std")))]
- {
- time = vm.execute_program().unwrap();
- }
- let days = time / 10u64.pow(9) / 60 / 60 / 24;
- let hours = (time / 10u64.pow(9) / 60 / 60) % 24;
- let minutes = (time / 10u64.pow(9) / 60) % 60;
- let seconds = (time / 10u64.pow(9)) % 60;
- let nanosec = time % 10u64.pow(9);
- println!(
- "Uptime: {:#x} ns == {} days {:02}:{:02}:{:02}, {} ns",
- time, days, hours, minutes, seconds, nanosec
- );
- }
|