misc.rs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. // SPDX-License-Identifier: (Apache-2.0 OR MIT)
  2. // Copyright 2016 6WIND S.A. <[email protected]>
  3. // There are unused mut warnings due to unsafe code.
  4. #![allow(unused_mut)]
  5. #![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. #[cfg(feature = "std")]
  19. use rbpf::helpers;
  20. use rbpf::{assembler::assemble, Error, ErrorKind};
  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. #[cfg(feature = "std")]
  88. fn test_vm_block_port() {
  89. // To load the bytecode from an object file instead of using the hardcoded instructions,
  90. // use the additional crates commented at the beginning of this file (and also add them to your
  91. // Cargo.toml). See comments above.
  92. //
  93. // ---
  94. // let filename = "my_ebpf_object_file.o";
  95. //
  96. // let path = PathBuf::from(filename);
  97. // let file = match elf::File::open_path(&path) {
  98. // Ok(f) => f,
  99. // Err(e) => panic!("Error: {:?}", e),
  100. // };
  101. //
  102. // let text_scn = match file.get_section(".classifier") {
  103. // Some(s) => s,
  104. // None => panic!("Failed to look up .classifier section"),
  105. // };
  106. //
  107. // let prog = &text_scn.data;
  108. // ---
  109. let prog = &[
  110. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79, 0x12, 0x50, 0x00, 0x00, 0x00, 0x00,
  111. 0x00, // 0x79 instead of 0x61
  112. 0x79, 0x11, 0x40, 0x00, 0x00, 0x00, 0x00,
  113. 0x00, // 0x79 instead of 0x61, 0x40 i.o. 0x4c
  114. 0xbf, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x03, 0x00, 0x00, 0x36, 0x00, 0x00,
  115. 0x00, 0x2d, 0x23, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x12, 0x0c, 0x00, 0x00, 0x00,
  116. 0x00, 0x00, 0x55, 0x02, 0x10, 0x00, 0x08, 0x00, 0x00, 0x00, 0x71, 0x12, 0x17, 0x00, 0x00,
  117. 0x00, 0x00, 0x00, 0x55, 0x02, 0x0e, 0x00, 0x06, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
  118. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79, 0x11, 0x22,
  119. 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  120. 0xbf, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57, 0x02, 0x00, 0x00, 0xff, 0xff, 0x00,
  121. 0x00, 0x15, 0x02, 0x08, 0x00, 0x99, 0x99, 0x00, 0x00, 0x18, 0x02, 0x00, 0x00, 0x00, 0x00,
  122. 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5f, 0x21, 0x00, 0x00, 0x00,
  123. 0x00, 0x00, 0x00, 0xb7, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x18, 0x02, 0x00, 0x00,
  124. 0x00, 0x00, 0x99, 0x99, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1d, 0x21, 0x01,
  125. 0x00, 0x00, 0x00, 0x00, 0x00, 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x95, 0x00,
  126. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  127. ];
  128. let packet = &mut [
  129. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x08,
  130. 0x00, // ethertype
  131. 0x45, 0x00, 0x00, 0x3b, // start ip_hdr
  132. 0xa6, 0xab, 0x40, 0x00, 0x40, 0x06, 0x96, 0x0f, 0x7f, 0x00, 0x00, 0x01, 0x7f, 0x00, 0x00,
  133. 0x01,
  134. // Program matches the next two bytes: 0x9999 returns 0xffffffff, else return 0.
  135. 0x99, 0x99, 0xc6, 0xcc, // start tcp_hdr
  136. 0xd1, 0xe5, 0xc4, 0x9d, 0xd4, 0x30, 0xb5, 0xd2, 0x80, 0x18, 0x01, 0x56, 0xfe, 0x2f, 0x00,
  137. 0x00, 0x01, 0x01, 0x08, 0x0a, // start data
  138. 0x00, 0x23, 0x75, 0x89, 0x00, 0x23, 0x63, 0x2d, 0x71, 0x64, 0x66, 0x73, 0x64, 0x66, 0x0au8,
  139. ];
  140. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x40, 0x50).unwrap();
  141. vm.register_helper(helpers::BPF_TRACE_PRINTK_IDX, helpers::bpf_trace_printf)
  142. .unwrap();
  143. let res = vm.execute_program(packet).unwrap();
  144. println!("Program returned: {res:?} ({res:#x})");
  145. assert_eq!(res, 0xffffffff);
  146. }
  147. #[test]
  148. #[cfg(all(not(windows), feature = "std"))]
  149. fn test_jit_block_port() {
  150. // To load the bytecode from an object file instead of using the hardcoded instructions,
  151. // use the additional crates commented at the beginning of this file (and also add them to your
  152. // Cargo.toml). See comments above.
  153. //
  154. // ---
  155. // let filename = "my_ebpf_object_file.o";
  156. //
  157. // let path = PathBuf::from(filename);
  158. // let file = match elf::File::open_path(&path) {
  159. // Ok(f) => f,
  160. // Err(e) => panic!("Error: {:?}", e),
  161. // };
  162. //
  163. // let text_scn = match file.get_section(".classifier") {
  164. // Some(s) => s,
  165. // None => panic!("Failed to look up .classifier section"),
  166. // };
  167. //
  168. // let prog = &text_scn.data;
  169. // ---
  170. let prog = &[
  171. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79, 0x12, 0x50, 0x00, 0x00, 0x00, 0x00,
  172. 0x00, // 0x79 instead of 0x61
  173. 0x79, 0x11, 0x40, 0x00, 0x00, 0x00, 0x00,
  174. 0x00, // 0x79 instead of 0x61, 0x40 i.o. 0x4c
  175. 0xbf, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x03, 0x00, 0x00, 0x36, 0x00, 0x00,
  176. 0x00, 0x2d, 0x23, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x12, 0x0c, 0x00, 0x00, 0x00,
  177. 0x00, 0x00, 0x55, 0x02, 0x10, 0x00, 0x08, 0x00, 0x00, 0x00, 0x71, 0x12, 0x17, 0x00, 0x00,
  178. 0x00, 0x00, 0x00, 0x55, 0x02, 0x0e, 0x00, 0x06, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
  179. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79, 0x11, 0x22,
  180. 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  181. 0xbf, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57, 0x02, 0x00, 0x00, 0xff, 0xff, 0x00,
  182. 0x00, 0x15, 0x02, 0x08, 0x00, 0x99, 0x99, 0x00, 0x00, 0x18, 0x02, 0x00, 0x00, 0x00, 0x00,
  183. 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5f, 0x21, 0x00, 0x00, 0x00,
  184. 0x00, 0x00, 0x00, 0xb7, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x18, 0x02, 0x00, 0x00,
  185. 0x00, 0x00, 0x99, 0x99, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1d, 0x21, 0x01,
  186. 0x00, 0x00, 0x00, 0x00, 0x00, 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x95, 0x00,
  187. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  188. ];
  189. let packet = &mut [
  190. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x08,
  191. 0x00, // ethertype
  192. 0x45, 0x00, 0x00, 0x3b, // start ip_hdr
  193. 0xa6, 0xab, 0x40, 0x00, 0x40, 0x06, 0x96, 0x0f, 0x7f, 0x00, 0x00, 0x01, 0x7f, 0x00, 0x00,
  194. 0x01,
  195. // Program matches the next two bytes: 0x9999 returns 0xffffffff, else return 0.
  196. 0x99, 0x99, 0xc6, 0xcc, // start tcp_hdr
  197. 0xd1, 0xe5, 0xc4, 0x9d, 0xd4, 0x30, 0xb5, 0xd2, 0x80, 0x18, 0x01, 0x56, 0xfe, 0x2f, 0x00,
  198. 0x00, 0x01, 0x01, 0x08, 0x0a, // start data
  199. 0x00, 0x23, 0x75, 0x89, 0x00, 0x23, 0x63, 0x2d, 0x71, 0x64, 0x66, 0x73, 0x64, 0x66, 0x0au8,
  200. ];
  201. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x40, 0x50).unwrap();
  202. vm.register_helper(helpers::BPF_TRACE_PRINTK_IDX, helpers::bpf_trace_printf)
  203. .unwrap();
  204. vm.jit_compile().unwrap();
  205. unsafe {
  206. let res = vm.execute_program_jit(packet).unwrap();
  207. println!("Program returned: {res:?} ({res:#x})");
  208. assert_eq!(res, 0xffffffff);
  209. }
  210. }
  211. // Program and memory come from uBPF test ldxh.
  212. #[test]
  213. fn test_vm_mbuff() {
  214. let prog = &[
  215. // Load mem from mbuff into R1
  216. 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, // ldhx r1[2], r0
  217. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  218. 0x00,
  219. ];
  220. let mem = &[0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd];
  221. let mbuff = [0u8; 32];
  222. unsafe {
  223. let mut data = mbuff.as_ptr().offset(8) as *mut u64;
  224. let mut data_end = mbuff.as_ptr().offset(24) as *mut u64;
  225. data.write_unaligned(mem.as_ptr() as u64);
  226. data_end.write_unaligned(mem.as_ptr() as u64 + mem.len() as u64);
  227. }
  228. let vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
  229. assert_eq!(vm.execute_program(mem, &mbuff).unwrap(), 0x2211);
  230. }
  231. // Program and memory come from uBPF test ldxh.
  232. #[test]
  233. fn test_vm_mbuff_with_rust_api() {
  234. use rbpf::insn_builder::*;
  235. let mut program = BpfCode::new();
  236. program
  237. .load_x(MemSize::DoubleWord)
  238. .set_dst(0x01)
  239. .set_src(0x01)
  240. .set_off(0x00_08)
  241. .push()
  242. .load_x(MemSize::HalfWord)
  243. .set_dst(0x00)
  244. .set_src(0x01)
  245. .set_off(0x00_02)
  246. .push()
  247. .exit()
  248. .push();
  249. let mem = &[0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd];
  250. let mbuff = [0u8; 32];
  251. unsafe {
  252. let mut data = mbuff.as_ptr().offset(8) as *mut u64;
  253. let mut data_end = mbuff.as_ptr().offset(24) as *mut u64;
  254. *data = mem.as_ptr() as u64;
  255. *data_end = mem.as_ptr() as u64 + mem.len() as u64;
  256. }
  257. let vm = rbpf::EbpfVmMbuff::new(Some(program.into_bytes())).unwrap();
  258. assert_eq!(vm.execute_program(mem, &mbuff).unwrap(), 0x2211);
  259. }
  260. // Program and memory come from uBPF test ldxh.
  261. #[test]
  262. #[cfg(all(not(windows), feature = "std"))]
  263. fn test_jit_mbuff() {
  264. let prog = &[
  265. // Load mem from mbuff into R1
  266. 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, // ldhx r1[2], r0
  267. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  268. 0x00,
  269. ];
  270. let mem = &mut [0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd];
  271. let mut mbuff = [0u8; 32];
  272. unsafe {
  273. let mut data = mbuff.as_ptr().offset(8) as *mut u64;
  274. let mut data_end = mbuff.as_ptr().offset(24) as *mut u64;
  275. *data = mem.as_ptr() as u64;
  276. *data_end = mem.as_ptr() as u64 + mem.len() as u64;
  277. }
  278. unsafe {
  279. let mut vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
  280. vm.jit_compile().unwrap();
  281. assert_eq!(vm.execute_program_jit(mem, &mut mbuff).unwrap(), 0x2211);
  282. }
  283. }
  284. #[cfg(all(not(windows), feature = "std"))]
  285. #[test]
  286. fn test_vm_jit_ldabsb() {
  287. let prog = &[
  288. 0x30, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  289. 0x00,
  290. ];
  291. let mem = &mut [
  292. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
  293. 0xff,
  294. ];
  295. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  296. assert_eq!(vm.execute_program(mem).unwrap(), 0x33);
  297. vm.jit_compile().unwrap();
  298. unsafe {
  299. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x33);
  300. };
  301. }
  302. #[cfg(all(not(windows), feature = "std"))]
  303. #[test]
  304. fn test_vm_jit_ldabsh() {
  305. let prog = &[
  306. 0x28, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  307. 0x00,
  308. ];
  309. let mem = &mut [
  310. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
  311. 0xff,
  312. ];
  313. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  314. assert_eq!(vm.execute_program(mem).unwrap(), 0x4433);
  315. vm.jit_compile().unwrap();
  316. unsafe {
  317. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x4433);
  318. };
  319. }
  320. #[cfg(all(not(windows), feature = "std"))]
  321. #[test]
  322. fn test_vm_jit_ldabsw() {
  323. let prog = &[
  324. 0x20, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  325. 0x00,
  326. ];
  327. let mem = &mut [
  328. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
  329. 0xff,
  330. ];
  331. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  332. assert_eq!(vm.execute_program(mem).unwrap(), 0x66554433);
  333. vm.jit_compile().unwrap();
  334. unsafe {
  335. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x66554433);
  336. };
  337. }
  338. #[cfg(all(not(windows), feature = "std"))]
  339. #[test]
  340. fn test_vm_jit_ldabsdw() {
  341. let prog = &[
  342. 0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  343. 0x00,
  344. ];
  345. let mem = &mut [
  346. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
  347. 0xff,
  348. ];
  349. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  350. assert_eq!(vm.execute_program(mem).unwrap(), 0xaa99887766554433);
  351. vm.jit_compile().unwrap();
  352. unsafe {
  353. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0xaa99887766554433);
  354. };
  355. }
  356. #[test]
  357. #[should_panic(expected = "Error: out of bounds memory load (insn #1),")]
  358. fn test_vm_err_ldabsb_oob() {
  359. let prog = &[
  360. 0x38, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  361. 0x00,
  362. ];
  363. let mem = &mut [
  364. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
  365. 0xff,
  366. ];
  367. let vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  368. vm.execute_program(mem).unwrap();
  369. // Memory check not implemented for JIT yet.
  370. }
  371. #[test]
  372. #[should_panic(expected = "Error: out of bounds memory load (insn #1),")]
  373. fn test_vm_err_ldabsb_nomem() {
  374. let prog = &[
  375. 0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  376. 0x00,
  377. ];
  378. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  379. vm.execute_program().unwrap();
  380. // Memory check not implemented for JIT yet.
  381. }
  382. #[cfg(all(not(windows), feature = "std"))]
  383. #[test]
  384. fn test_vm_jit_ldindb() {
  385. let prog = &[
  386. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x50, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00,
  387. 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  388. ];
  389. let mem = &mut [
  390. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
  391. 0xff,
  392. ];
  393. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  394. assert_eq!(vm.execute_program(mem).unwrap(), 0x88);
  395. vm.jit_compile().unwrap();
  396. unsafe {
  397. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x88);
  398. };
  399. }
  400. #[cfg(all(not(windows), feature = "std"))]
  401. #[test]
  402. fn test_vm_jit_ldindh() {
  403. let prog = &[
  404. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x48, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00,
  405. 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  406. ];
  407. let mem = &mut [
  408. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
  409. 0xff,
  410. ];
  411. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  412. assert_eq!(vm.execute_program(mem).unwrap(), 0x9988);
  413. vm.jit_compile().unwrap();
  414. unsafe {
  415. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x9988);
  416. };
  417. }
  418. #[cfg(all(not(windows), feature = "std"))]
  419. #[test]
  420. fn test_vm_jit_ldindw() {
  421. let prog = &[
  422. 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x40, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00,
  423. 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  424. ];
  425. let mem = &mut [
  426. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
  427. 0xff,
  428. ];
  429. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  430. assert_eq!(vm.execute_program(mem).unwrap(), 0x88776655);
  431. vm.jit_compile().unwrap();
  432. unsafe {
  433. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x88776655);
  434. };
  435. }
  436. #[cfg(all(not(windows), feature = "std"))]
  437. #[test]
  438. fn test_vm_jit_ldinddw() {
  439. let prog = &[
  440. 0xb7, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x58, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00,
  441. 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  442. ];
  443. let mem = &mut [
  444. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
  445. 0xff,
  446. ];
  447. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  448. assert_eq!(vm.execute_program(mem).unwrap(), 0xccbbaa9988776655);
  449. vm.jit_compile().unwrap();
  450. unsafe {
  451. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0xccbbaa9988776655);
  452. };
  453. }
  454. #[test]
  455. #[should_panic(expected = "Error: out of bounds memory load (insn #2),")]
  456. fn test_vm_err_ldindb_oob() {
  457. let prog = &[
  458. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x38, 0x10, 0x00, 0x00, 0x33, 0x00, 0x00,
  459. 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  460. ];
  461. let mem = &mut [
  462. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
  463. 0xff,
  464. ];
  465. let vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  466. vm.execute_program(mem).unwrap();
  467. // Memory check not implemented for JIT yet.
  468. }
  469. #[test]
  470. #[should_panic(expected = "Error: out of bounds memory load (insn #2),")]
  471. fn test_vm_err_ldindb_nomem() {
  472. let prog = &[
  473. 0xb7, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x38, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00,
  474. 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  475. ];
  476. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  477. vm.execute_program().unwrap();
  478. // Memory check not implemented for JIT yet.
  479. }
  480. #[test]
  481. #[should_panic(expected = "Error: No program set, call prog_set() to load one")]
  482. fn test_vm_exec_no_program() {
  483. let vm = rbpf::EbpfVmNoData::new(None).unwrap();
  484. assert_eq!(vm.execute_program().unwrap(), 0xBEE);
  485. }
  486. fn verifier_success(_prog: &[u8]) -> Result<(), Error> {
  487. Ok(())
  488. }
  489. fn verifier_fail(_prog: &[u8]) -> Result<(), Error> {
  490. Err(Error::new(ErrorKind::Other, "Gaggablaghblagh!"))
  491. }
  492. #[test]
  493. fn test_verifier_success() {
  494. let prog = assemble(
  495. "mov32 r0, 0xBEE
  496. exit",
  497. )
  498. .unwrap();
  499. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  500. vm.set_verifier(verifier_success).unwrap();
  501. vm.set_program(&prog).unwrap();
  502. assert_eq!(vm.execute_program().unwrap(), 0xBEE);
  503. }
  504. #[test]
  505. #[should_panic(expected = "Gaggablaghblagh!")]
  506. fn test_verifier_fail() {
  507. let prog = assemble(
  508. "mov32 r0, 0xBEE
  509. exit",
  510. )
  511. .unwrap();
  512. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  513. vm.set_verifier(verifier_fail).unwrap();
  514. vm.set_program(&prog).unwrap();
  515. assert_eq!(vm.execute_program().unwrap(), 0xBEE);
  516. }