misc.rs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. // SPDX-License-Identifier: (Apache-2.0 OR MIT)
  2. // Copyright 2016 6WIND S.A. <quentin.monnet@6wind.com>
  3. // There are unused mut warnings due to unsafe code.
  4. #![allow(unused_mut)]
  5. #![cfg_attr(feature = "cargo-clippy", allow(clippy::unreadable_literal))]
  6. // This crate would be needed to load bytecode from a BPF-compiled object file. Since the crate
  7. // is not used anywhere else in the library, it is deactivated: we do not want to load and compile
  8. // it just for the tests. If you want to use it, do not forget to add the following
  9. // dependency to your Cargo.toml file:
  10. //
  11. // ---
  12. // elf = "0.0.10"
  13. // ---
  14. //
  15. // extern crate elf;
  16. // use std::path::PathBuf;
  17. extern crate rbpf;
  18. use std::io::{Error, ErrorKind};
  19. use rbpf::assembler::assemble;
  20. use rbpf::helpers;
  21. // The following two examples have been compiled from C with the following command:
  22. //
  23. // ```bash
  24. // clang -O2 -emit-llvm -c <file.c> -o - | llc -march=bpf -filetype=obj -o <file.o>
  25. // ```
  26. //
  27. // The C source code was the following:
  28. //
  29. // ```c
  30. // #include <linux/ip.h>
  31. // #include <linux/in.h>
  32. // #include <linux/tcp.h>
  33. // #include <linux/bpf.h>
  34. //
  35. // #define ETH_ALEN 6
  36. // #define ETH_P_IP 0x0008 /* htons(0x0800) */
  37. // #define TCP_HDR_LEN 20
  38. //
  39. // #define BLOCKED_TCP_PORT 0x9999
  40. //
  41. // struct eth_hdr {
  42. // unsigned char h_dest[ETH_ALEN];
  43. // unsigned char h_source[ETH_ALEN];
  44. // unsigned short h_proto;
  45. // };
  46. //
  47. // #define SEC(NAME) __attribute__((section(NAME), used))
  48. // SEC(".classifier")
  49. // int handle_ingress(struct __sk_buff *skb)
  50. // {
  51. // void *data = (void *)(long)skb->data;
  52. // void *data_end = (void *)(long)skb->data_end;
  53. // struct eth_hdr *eth = data;
  54. // struct iphdr *iph = data + sizeof(*eth);
  55. // struct tcphdr *tcp = data + sizeof(*eth) + sizeof(*iph);
  56. //
  57. // /* single length check */
  58. // if (data + sizeof(*eth) + sizeof(*iph) + sizeof(*tcp) > data_end)
  59. // return 0;
  60. // if (eth->h_proto != ETH_P_IP)
  61. // return 0;
  62. // if (iph->protocol != IPPROTO_TCP)
  63. // return 0;
  64. // if (tcp->source == BLOCKED_TCP_PORT || tcp->dest == BLOCKED_TCP_PORT)
  65. // return -1;
  66. // return 0;
  67. // }
  68. // char _license[] SEC(".license") = "GPL";
  69. // ```
  70. //
  71. // This program, once compiled, can be injected into Linux kernel, with tc for instance. Sadly, we
  72. // need to bring some modifications to the generated bytecode in order to run it: the three
  73. // instructions with opcode 0x61 load data from a packet area as 4-byte words, where we need to
  74. // load it as 8-bytes double words (0x79). The kernel does the same kind of translation before
  75. // running the program, but rbpf does not implement this.
  76. //
  77. // In addition, the offset at which the pointer to the packet data is stored must be changed: since
  78. // we use 8 bytes instead of 4 for the start and end addresses of the data packet, we cannot use
  79. // the offsets produced by clang (0x4c and 0x50), the addresses would overlap. Instead we can use,
  80. // for example, 0x40 and 0x50. See comments on the bytecode below to see the modifications.
  81. //
  82. // Once the bytecode has been (manually, in our case) edited, we can load the bytecode directly
  83. // from the ELF object file. This is easy to do, but requires the addition of two crates in the
  84. // Cargo.toml file (see comments above), so here we use just the hardcoded bytecode instructions
  85. // instead.
  86. #[test]
  87. fn test_vm_block_port() {
  88. // To load the bytecode from an object file instead of using the hardcoded instructions,
  89. // use the additional crates commented at the beginning of this file (and also add them to your
  90. // Cargo.toml). See comments above.
  91. //
  92. // ---
  93. // let filename = "my_ebpf_object_file.o";
  94. //
  95. // let path = PathBuf::from(filename);
  96. // let file = match elf::File::open_path(&path) {
  97. // Ok(f) => f,
  98. // Err(e) => panic!("Error: {:?}", e),
  99. // };
  100. //
  101. // let text_scn = match file.get_section(".classifier") {
  102. // Some(s) => s,
  103. // None => panic!("Failed to look up .classifier section"),
  104. // };
  105. //
  106. // let prog = &text_scn.data;
  107. // ---
  108. let prog = &[
  109. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  110. 0x79, 0x12, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  111. 0x79, 0x11, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61, 0x40 i.o. 0x4c
  112. 0xbf, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  113. 0x07, 0x03, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00,
  114. 0x2d, 0x23, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00,
  115. 0x69, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
  116. 0x55, 0x02, 0x10, 0x00, 0x08, 0x00, 0x00, 0x00,
  117. 0x71, 0x12, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00,
  118. 0x55, 0x02, 0x0e, 0x00, 0x06, 0x00, 0x00, 0x00,
  119. 0x18, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  120. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  121. 0x79, 0x11, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  122. 0xbf, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  123. 0x57, 0x02, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
  124. 0x15, 0x02, 0x08, 0x00, 0x99, 0x99, 0x00, 0x00,
  125. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
  126. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  127. 0x5f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  128. 0xb7, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  129. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0x99, 0x99,
  130. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  131. 0x1d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  132. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  133. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  134. ];
  135. let packet = &mut [
  136. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
  137. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
  138. 0x08, 0x00, // ethertype
  139. 0x45, 0x00, 0x00, 0x3b, // start ip_hdr
  140. 0xa6, 0xab, 0x40, 0x00,
  141. 0x40, 0x06, 0x96, 0x0f,
  142. 0x7f, 0x00, 0x00, 0x01,
  143. 0x7f, 0x00, 0x00, 0x01,
  144. // Program matches the next two bytes: 0x9999 returns 0xffffffff, else return 0.
  145. 0x99, 0x99, 0xc6, 0xcc, // start tcp_hdr
  146. 0xd1, 0xe5, 0xc4, 0x9d,
  147. 0xd4, 0x30, 0xb5, 0xd2,
  148. 0x80, 0x18, 0x01, 0x56,
  149. 0xfe, 0x2f, 0x00, 0x00,
  150. 0x01, 0x01, 0x08, 0x0a, // start data
  151. 0x00, 0x23, 0x75, 0x89,
  152. 0x00, 0x23, 0x63, 0x2d,
  153. 0x71, 0x64, 0x66, 0x73,
  154. 0x64, 0x66, 0x0au8
  155. ];
  156. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x40, 0x50).unwrap();
  157. vm.register_helper(helpers::BPF_TRACE_PRINTK_IDX, helpers::bpf_trace_printf).unwrap();
  158. let res = vm.execute_program(packet).unwrap();
  159. println!("Program returned: {:?} ({:#x})", res, res);
  160. assert_eq!(res, 0xffffffff);
  161. }
  162. #[cfg(not(windows))]
  163. #[test]
  164. fn test_jit_block_port() {
  165. // To load the bytecode from an object file instead of using the hardcoded instructions,
  166. // use the additional crates commented at the beginning of this file (and also add them to your
  167. // Cargo.toml). See comments above.
  168. //
  169. // ---
  170. // let filename = "my_ebpf_object_file.o";
  171. //
  172. // let path = PathBuf::from(filename);
  173. // let file = match elf::File::open_path(&path) {
  174. // Ok(f) => f,
  175. // Err(e) => panic!("Error: {:?}", e),
  176. // };
  177. //
  178. // let text_scn = match file.get_section(".classifier") {
  179. // Some(s) => s,
  180. // None => panic!("Failed to look up .classifier section"),
  181. // };
  182. //
  183. // let prog = &text_scn.data;
  184. // ---
  185. let prog = &[
  186. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  187. 0x79, 0x12, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  188. 0x79, 0x11, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61, 0x40 i.o. 0x4c
  189. 0xbf, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  190. 0x07, 0x03, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00,
  191. 0x2d, 0x23, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00,
  192. 0x69, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
  193. 0x55, 0x02, 0x10, 0x00, 0x08, 0x00, 0x00, 0x00,
  194. 0x71, 0x12, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00,
  195. 0x55, 0x02, 0x0e, 0x00, 0x06, 0x00, 0x00, 0x00,
  196. 0x18, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  197. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  198. 0x79, 0x11, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  199. 0xbf, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  200. 0x57, 0x02, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
  201. 0x15, 0x02, 0x08, 0x00, 0x99, 0x99, 0x00, 0x00,
  202. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
  203. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  204. 0x5f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  205. 0xb7, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  206. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0x99, 0x99,
  207. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  208. 0x1d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  209. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  210. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  211. ];
  212. let packet = &mut [
  213. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
  214. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
  215. 0x08, 0x00, // ethertype
  216. 0x45, 0x00, 0x00, 0x3b, // start ip_hdr
  217. 0xa6, 0xab, 0x40, 0x00,
  218. 0x40, 0x06, 0x96, 0x0f,
  219. 0x7f, 0x00, 0x00, 0x01,
  220. 0x7f, 0x00, 0x00, 0x01,
  221. // Program matches the next two bytes: 0x9999 returns 0xffffffff, else return 0.
  222. 0x99, 0x99, 0xc6, 0xcc, // start tcp_hdr
  223. 0xd1, 0xe5, 0xc4, 0x9d,
  224. 0xd4, 0x30, 0xb5, 0xd2,
  225. 0x80, 0x18, 0x01, 0x56,
  226. 0xfe, 0x2f, 0x00, 0x00,
  227. 0x01, 0x01, 0x08, 0x0a, // start data
  228. 0x00, 0x23, 0x75, 0x89,
  229. 0x00, 0x23, 0x63, 0x2d,
  230. 0x71, 0x64, 0x66, 0x73,
  231. 0x64, 0x66, 0x0au8
  232. ];
  233. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x40, 0x50).unwrap();
  234. vm.register_helper(helpers::BPF_TRACE_PRINTK_IDX, helpers::bpf_trace_printf).unwrap();
  235. vm.jit_compile().unwrap();
  236. unsafe {
  237. let res = vm.execute_program_jit(packet).unwrap();
  238. println!("Program returned: {:?} ({:#x})", res, res);
  239. assert_eq!(res, 0xffffffff);
  240. }
  241. }
  242. // Program and memory come from uBPF test ldxh.
  243. #[test]
  244. fn test_vm_mbuff() {
  245. let prog = &[
  246. // Load mem from mbuff into R1
  247. 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  248. // ldhx r1[2], r0
  249. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  250. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  251. ];
  252. let mem = &[
  253. 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
  254. ];
  255. let mbuff = [0u8; 32];
  256. unsafe {
  257. let mut data = mbuff.as_ptr().offset(8) as *mut u64;
  258. let mut data_end = mbuff.as_ptr().offset(24) as *mut u64;
  259. *data = mem.as_ptr() as u64;
  260. *data_end = mem.as_ptr() as u64 + mem.len() as u64;
  261. }
  262. let vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
  263. assert_eq!(vm.execute_program(mem, &mbuff).unwrap(), 0x2211);
  264. }
  265. // Program and memory come from uBPF test ldxh.
  266. #[test]
  267. fn test_vm_mbuff_with_rust_api() {
  268. use rbpf::insn_builder::*;
  269. let mut program = BpfCode::new();
  270. program
  271. .load_x(MemSize::DoubleWord).set_dst(0x01).set_src(0x01).set_off(0x00_08).push()
  272. .load_x(MemSize::HalfWord).set_dst(0x00).set_src(0x01).set_off(0x00_02).push()
  273. .exit().push();
  274. let mem = &[
  275. 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
  276. ];
  277. let mbuff = [0u8; 32];
  278. unsafe {
  279. let mut data = mbuff.as_ptr().offset(8) as *mut u64;
  280. let mut data_end = mbuff.as_ptr().offset(24) as *mut u64;
  281. *data = mem.as_ptr() as u64;
  282. *data_end = mem.as_ptr() as u64 + mem.len() as u64;
  283. }
  284. let vm = rbpf::EbpfVmMbuff::new(Some(program.into_bytes())).unwrap();
  285. assert_eq!(vm.execute_program(mem, &mbuff).unwrap(), 0x2211);
  286. }
  287. // Program and memory come from uBPF test ldxh.
  288. #[cfg(not(windows))]
  289. #[test]
  290. fn test_jit_mbuff() {
  291. let prog = &[
  292. // Load mem from mbuff into R1
  293. 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  294. // ldhx r1[2], r0
  295. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  296. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  297. ];
  298. let mem = &mut [
  299. 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
  300. ];
  301. let mut mbuff = [0u8; 32];
  302. unsafe {
  303. let mut data = mbuff.as_ptr().offset(8) as *mut u64;
  304. let mut data_end = mbuff.as_ptr().offset(24) as *mut u64;
  305. *data = mem.as_ptr() as u64;
  306. *data_end = mem.as_ptr() as u64 + mem.len() as u64;
  307. }
  308. unsafe {
  309. let mut vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
  310. vm.jit_compile().unwrap();
  311. assert_eq!(vm.execute_program_jit(mem, &mut mbuff).unwrap(), 0x2211);
  312. }
  313. }
  314. #[cfg(not(windows))]
  315. #[test]
  316. fn test_vm_jit_ldabsb() {
  317. let prog = &[
  318. 0x30, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  319. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  320. ];
  321. let mem = &mut [
  322. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  323. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  324. ];
  325. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  326. assert_eq!(vm.execute_program(mem).unwrap(), 0x33);
  327. vm.jit_compile().unwrap();
  328. unsafe {
  329. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x33);
  330. };
  331. }
  332. #[cfg(not(windows))]
  333. #[test]
  334. fn test_vm_jit_ldabsh() {
  335. let prog = &[
  336. 0x28, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  337. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  338. ];
  339. let mem = &mut [
  340. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  341. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  342. ];
  343. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  344. assert_eq!(vm.execute_program(mem).unwrap(), 0x4433);
  345. vm.jit_compile().unwrap();
  346. unsafe {
  347. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x4433);
  348. };
  349. }
  350. #[cfg(not(windows))]
  351. #[test]
  352. fn test_vm_jit_ldabsw() {
  353. let prog = &[
  354. 0x20, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  355. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  356. ];
  357. let mem = &mut [
  358. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  359. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  360. ];
  361. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  362. assert_eq!(vm.execute_program(mem).unwrap(), 0x66554433);
  363. vm.jit_compile().unwrap();
  364. unsafe {
  365. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x66554433);
  366. };
  367. }
  368. #[cfg(not(windows))]
  369. #[test]
  370. fn test_vm_jit_ldabsdw() {
  371. let prog = &[
  372. 0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  373. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  374. ];
  375. let mem = &mut [
  376. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  377. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  378. ];
  379. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  380. assert_eq!(vm.execute_program(mem).unwrap(), 0xaa99887766554433);
  381. vm.jit_compile().unwrap();
  382. unsafe {
  383. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0xaa99887766554433);
  384. };
  385. }
  386. #[test]
  387. #[should_panic(expected = "Error: out of bounds memory load (insn #1),")]
  388. fn test_vm_err_ldabsb_oob() {
  389. let prog = &[
  390. 0x38, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
  391. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  392. ];
  393. let mem = &mut [
  394. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  395. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  396. ];
  397. let vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  398. vm.execute_program(mem).unwrap();
  399. // Memory check not implemented for JIT yet.
  400. }
  401. #[test]
  402. #[should_panic(expected = "Error: out of bounds memory load (insn #1),")]
  403. fn test_vm_err_ldabsb_nomem() {
  404. let prog = &[
  405. 0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  406. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  407. ];
  408. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  409. vm.execute_program().unwrap();
  410. // Memory check not implemented for JIT yet.
  411. }
  412. #[cfg(not(windows))]
  413. #[test]
  414. fn test_vm_jit_ldindb() {
  415. let prog = &[
  416. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  417. 0x50, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  418. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  419. ];
  420. let mem = &mut [
  421. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  422. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  423. ];
  424. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  425. assert_eq!(vm.execute_program(mem).unwrap(), 0x88);
  426. vm.jit_compile().unwrap();
  427. unsafe {
  428. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x88);
  429. };
  430. }
  431. #[cfg(not(windows))]
  432. #[test]
  433. fn test_vm_jit_ldindh() {
  434. let prog = &[
  435. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  436. 0x48, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  437. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  438. ];
  439. let mem = &mut [
  440. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  441. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  442. ];
  443. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  444. assert_eq!(vm.execute_program(mem).unwrap(), 0x9988);
  445. vm.jit_compile().unwrap();
  446. unsafe {
  447. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x9988);
  448. };
  449. }
  450. #[cfg(not(windows))]
  451. #[test]
  452. fn test_vm_jit_ldindw() {
  453. let prog = &[
  454. 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  455. 0x40, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  456. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  457. ];
  458. let mem = &mut [
  459. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  460. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  461. ];
  462. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  463. assert_eq!(vm.execute_program(mem).unwrap(), 0x88776655);
  464. vm.jit_compile().unwrap();
  465. unsafe {
  466. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x88776655);
  467. };
  468. }
  469. #[cfg(not(windows))]
  470. #[test]
  471. fn test_vm_jit_ldinddw() {
  472. let prog = &[
  473. 0xb7, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  474. 0x58, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  475. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  476. ];
  477. let mem = &mut [
  478. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  479. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  480. ];
  481. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  482. assert_eq!(vm.execute_program(mem).unwrap(), 0xccbbaa9988776655);
  483. vm.jit_compile().unwrap();
  484. unsafe {
  485. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0xccbbaa9988776655);
  486. };
  487. }
  488. #[test]
  489. #[should_panic(expected = "Error: out of bounds memory load (insn #2),")]
  490. fn test_vm_err_ldindb_oob() {
  491. let prog = &[
  492. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  493. 0x38, 0x10, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
  494. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  495. ];
  496. let mem = &mut [
  497. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  498. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  499. ];
  500. let vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  501. vm.execute_program(mem).unwrap();
  502. // Memory check not implemented for JIT yet.
  503. }
  504. #[test]
  505. #[should_panic(expected = "Error: out of bounds memory load (insn #2),")]
  506. fn test_vm_err_ldindb_nomem() {
  507. let prog = &[
  508. 0xb7, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  509. 0x38, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  510. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  511. ];
  512. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  513. vm.execute_program().unwrap();
  514. // Memory check not implemented for JIT yet.
  515. }
  516. #[test]
  517. #[should_panic(expected = "Error: No program set, call prog_set() to load one")]
  518. fn test_vm_exec_no_program() {
  519. let vm = rbpf::EbpfVmNoData::new(None).unwrap();
  520. assert_eq!(vm.execute_program().unwrap(), 0xBEE);
  521. }
  522. fn verifier_success(_prog: &[u8]) -> Result<(), Error> {
  523. Ok(())
  524. }
  525. fn verifier_fail(_prog: &[u8]) -> Result<(), Error> {
  526. Err(Error::new(ErrorKind::Other,
  527. "Gaggablaghblagh!"))
  528. }
  529. #[test]
  530. fn test_verifier_success() {
  531. let prog = assemble(
  532. "mov32 r0, 0xBEE
  533. exit",
  534. ).unwrap();
  535. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  536. vm.set_verifier(verifier_success).unwrap();
  537. vm.set_program(&prog).unwrap();
  538. assert_eq!(vm.execute_program().unwrap(), 0xBEE);
  539. }
  540. #[test]
  541. #[should_panic(expected = "Gaggablaghblagh!")]
  542. fn test_verifier_fail() {
  543. let prog = assemble(
  544. "mov32 r0, 0xBEE
  545. exit",
  546. ).unwrap();
  547. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  548. vm.set_verifier(verifier_fail).unwrap();
  549. vm.set_program(&prog).unwrap();
  550. assert_eq!(vm.execute_program().unwrap(), 0xBEE);
  551. }