misc.rs 20 KB

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