misc.rs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. // Copyright 2016 6WIND S.A. <quentin.monnet@6wind.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or
  4. // the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be
  5. // copied, modified, or distributed except according to those terms.
  6. // There are unused mut warnings due to unsafe code.
  7. #![allow(unused_mut)]
  8. #![cfg_attr(feature = "cargo-clippy", allow(unreadable_literal))]
  9. // These crates would be needed to load bytecode from a BPF-compiled object file. Since the crates
  10. // are not used anywhere else in the library, it is deactivated: we do not want to load and compile
  11. // them just for the tests. If you want to use them, do not forget to add the following
  12. // dependencies to your Cargo.toml file:
  13. //
  14. // ---
  15. // byteorder = "0.5.3"
  16. // elf = "0.0.10"
  17. // ---
  18. //
  19. // extern crate byteorder;
  20. // extern crate elf;
  21. // use std::path::PathBuf;
  22. extern crate rbpf;
  23. use rbpf::helpers;
  24. // The following two examples have been compiled from C with the following command:
  25. //
  26. // ```bash
  27. // clang -O2 -emit-llvm -c <file.c> -o - | llc -march=bpf -filetype=obj -o <file.o>
  28. // ```
  29. //
  30. // The C source code was the following:
  31. //
  32. // ```c
  33. // #include <linux/ip.h>
  34. // #include <linux/in.h>
  35. // #include <linux/tcp.h>
  36. // #include <linux/bpf.h>
  37. //
  38. // #define ETH_ALEN 6
  39. // #define ETH_P_IP 0x0008 /* htons(0x0800) */
  40. // #define TCP_HDR_LEN 20
  41. //
  42. // #define BLOCKED_TCP_PORT 0x9999
  43. //
  44. // struct eth_hdr {
  45. // unsigned char h_dest[ETH_ALEN];
  46. // unsigned char h_source[ETH_ALEN];
  47. // unsigned short h_proto;
  48. // };
  49. //
  50. // #define SEC(NAME) __attribute__((section(NAME), used))
  51. // SEC(".classifier")
  52. // int handle_ingress(struct __sk_buff *skb)
  53. // {
  54. // void *data = (void *)(long)skb->data;
  55. // void *data_end = (void *)(long)skb->data_end;
  56. // struct eth_hdr *eth = data;
  57. // struct iphdr *iph = data + sizeof(*eth);
  58. // struct tcphdr *tcp = data + sizeof(*eth) + sizeof(*iph);
  59. //
  60. // /* single length check */
  61. // if (data + sizeof(*eth) + sizeof(*iph) + sizeof(*tcp) > data_end)
  62. // return 0;
  63. // if (eth->h_proto != ETH_P_IP)
  64. // return 0;
  65. // if (iph->protocol != IPPROTO_TCP)
  66. // return 0;
  67. // if (tcp->source == BLOCKED_TCP_PORT || tcp->dest == BLOCKED_TCP_PORT)
  68. // return -1;
  69. // return 0;
  70. // }
  71. // char _license[] SEC(".license") = "GPL";
  72. // ```
  73. //
  74. // This program, once compiled, can be injected into Linux kernel, with tc for instance. Sadly, we
  75. // need to bring some modifications to the generated bytecode in order to run it: the three
  76. // instructions with opcode 0x61 load data from a packet area as 4-byte words, where we need to
  77. // load it as 8-bytes double words (0x79). The kernel does the same kind of translation before
  78. // running the program, but rbpf does not implement this.
  79. //
  80. // In addition, the offset at which the pointer to the packet data is stored must be changed: since
  81. // we use 8 bytes instead of 4 for the start and end addresses of the data packet, we cannot use
  82. // the offsets produced by clang (0x4c and 0x50), the addresses would overlap. Instead we can use,
  83. // for example, 0x40 and 0x50. See comments on the bytecode below to see the modifications.
  84. //
  85. // Once the bytecode has been (manually, in our case) edited, we can load the bytecode directly
  86. // from the ELF object file. This is easy to do, but requires the addition of two crates in the
  87. // Cargo.toml file (see comments above), so here we use just the hardcoded bytecode instructions
  88. // instead.
  89. #[test]
  90. fn test_vm_block_port() {
  91. // To load the bytecode from an object file instead of using the hardcoded instructions,
  92. // use the additional crates commented at the beginning of this file (and also add them to your
  93. // Cargo.toml). See comments above.
  94. //
  95. // ---
  96. // let filename = "my_ebpf_object_file.o";
  97. //
  98. // let path = PathBuf::from(filename);
  99. // let file = match elf::File::open_path(&path) {
  100. // Ok(f) => f,
  101. // Err(e) => panic!("Error: {:?}", e),
  102. // };
  103. //
  104. // let text_scn = match file.get_section(".classifier") {
  105. // Some(s) => s,
  106. // None => panic!("Failed to look up .classifier section"),
  107. // };
  108. //
  109. // let prog = &text_scn.data;
  110. // ---
  111. let prog = &[
  112. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  113. 0x79, 0x12, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  114. 0x79, 0x11, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61, 0x40 i.o. 0x4c
  115. 0xbf, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  116. 0x07, 0x03, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00,
  117. 0x2d, 0x23, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00,
  118. 0x69, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
  119. 0x55, 0x02, 0x10, 0x00, 0x08, 0x00, 0x00, 0x00,
  120. 0x71, 0x12, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00,
  121. 0x55, 0x02, 0x0e, 0x00, 0x06, 0x00, 0x00, 0x00,
  122. 0x18, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  123. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  124. 0x79, 0x11, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  125. 0xbf, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  126. 0x57, 0x02, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
  127. 0x15, 0x02, 0x08, 0x00, 0x99, 0x99, 0x00, 0x00,
  128. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
  129. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  130. 0x5f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  131. 0xb7, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  132. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0x99, 0x99,
  133. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  134. 0x1d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  135. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  136. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  137. ];
  138. let packet = &mut [
  139. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
  140. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
  141. 0x08, 0x00, // ethertype
  142. 0x45, 0x00, 0x00, 0x3b, // start ip_hdr
  143. 0xa6, 0xab, 0x40, 0x00,
  144. 0x40, 0x06, 0x96, 0x0f,
  145. 0x7f, 0x00, 0x00, 0x01,
  146. 0x7f, 0x00, 0x00, 0x01,
  147. // Program matches the next two bytes: 0x9999 returns 0xffffffff, else return 0.
  148. 0x99, 0x99, 0xc6, 0xcc, // start tcp_hdr
  149. 0xd1, 0xe5, 0xc4, 0x9d,
  150. 0xd4, 0x30, 0xb5, 0xd2,
  151. 0x80, 0x18, 0x01, 0x56,
  152. 0xfe, 0x2f, 0x00, 0x00,
  153. 0x01, 0x01, 0x08, 0x0a, // start data
  154. 0x00, 0x23, 0x75, 0x89,
  155. 0x00, 0x23, 0x63, 0x2d,
  156. 0x71, 0x64, 0x66, 0x73,
  157. 0x64, 0x66, 0x0au8
  158. ];
  159. let mut vm = rbpf::EbpfVmFixedMbuff::new(prog, 0x40, 0x50);
  160. vm.register_helper(helpers::BPF_TRACE_PRINTK_IDX, helpers::bpf_trace_printf);
  161. let res = vm.prog_exec(packet);
  162. println!("Program returned: {:?} ({:#x})", res, res);
  163. assert_eq!(res, 0xffffffff);
  164. }
  165. #[test]
  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(prog, 0x40, 0x50);
  236. vm.register_helper(helpers::BPF_TRACE_PRINTK_IDX, helpers::bpf_trace_printf);
  237. vm.jit_compile();
  238. unsafe {
  239. let res = vm.prog_exec_jit(packet);
  240. println!("Program returned: {:?} ({:#x})", res, res);
  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 mut data = mbuff.as_ptr().offset(8) as *mut u64;
  260. let mut data_end = mbuff.as_ptr().offset(24) as *mut u64;
  261. *data = mem.as_ptr() as u64;
  262. *data_end = mem.as_ptr() as u64 + mem.len() as u64;
  263. }
  264. let vm = rbpf::EbpfVmMbuff::new(prog);
  265. assert_eq!(vm.prog_exec(mem, &mbuff), 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 mut data = mbuff.as_ptr().offset(8) as *mut u64;
  282. let mut 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(program.into_bytes());
  287. assert_eq!(vm.prog_exec(mem, &mbuff), 0x2211);
  288. }
  289. // Program and memory come from uBPF test ldxh.
  290. #[test]
  291. fn test_jit_mbuff() {
  292. let prog = &[
  293. // Load mem from mbuff into R1
  294. 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  295. // ldhx r1[2], r0
  296. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  297. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  298. ];
  299. let mem = &mut [
  300. 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
  301. ];
  302. let mut mbuff = [0u8; 32];
  303. unsafe {
  304. let mut data = mbuff.as_ptr().offset(8) as *mut u64;
  305. let mut data_end = mbuff.as_ptr().offset(24) as *mut u64;
  306. *data = mem.as_ptr() as u64;
  307. *data_end = mem.as_ptr() as u64 + mem.len() as u64;
  308. }
  309. unsafe {
  310. let mut vm = rbpf::EbpfVmMbuff::new(prog);
  311. vm.jit_compile();
  312. assert_eq!(vm.prog_exec_jit(mem, &mut mbuff), 0x2211);
  313. }
  314. }
  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(prog);
  326. assert_eq!(vm.prog_exec(mem), 0x33);
  327. vm.jit_compile();
  328. unsafe {
  329. assert_eq!(vm.prog_exec_jit(mem), 0x33);
  330. };
  331. }
  332. #[test]
  333. fn test_vm_jit_ldabsh() {
  334. let prog = &[
  335. 0x28, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  336. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  337. ];
  338. let mem = &mut [
  339. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  340. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  341. ];
  342. let mut vm = rbpf::EbpfVmRaw::new(prog);
  343. assert_eq!(vm.prog_exec(mem), 0x4433);
  344. vm.jit_compile();
  345. unsafe {
  346. assert_eq!(vm.prog_exec_jit(mem), 0x4433);
  347. };
  348. }
  349. #[test]
  350. fn test_vm_jit_ldabsw() {
  351. let prog = &[
  352. 0x20, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  353. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  354. ];
  355. let mem = &mut [
  356. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  357. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  358. ];
  359. let mut vm = rbpf::EbpfVmRaw::new(prog);
  360. assert_eq!(vm.prog_exec(mem), 0x66554433);
  361. vm.jit_compile();
  362. unsafe {
  363. assert_eq!(vm.prog_exec_jit(mem), 0x66554433);
  364. };
  365. }
  366. #[test]
  367. fn test_vm_jit_ldabsdw() {
  368. let prog = &[
  369. 0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  370. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  371. ];
  372. let mem = &mut [
  373. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  374. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  375. ];
  376. let mut vm = rbpf::EbpfVmRaw::new(prog);
  377. assert_eq!(vm.prog_exec(mem), 0xaa99887766554433);
  378. vm.jit_compile();
  379. unsafe {
  380. assert_eq!(vm.prog_exec_jit(mem), 0xaa99887766554433);
  381. };
  382. }
  383. #[test]
  384. #[should_panic(expected = "Error: out of bounds memory load (insn #1),")]
  385. fn test_vm_err_ldabsb_oob() {
  386. let prog = &[
  387. 0x38, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
  388. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  389. ];
  390. let mem = &mut [
  391. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  392. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  393. ];
  394. let vm = rbpf::EbpfVmRaw::new(prog);
  395. vm.prog_exec(mem);
  396. // Memory check not implemented for JIT yet.
  397. }
  398. #[test]
  399. #[should_panic(expected = "Error: out of bounds memory load (insn #1),")]
  400. fn test_vm_err_ldabsb_nomem() {
  401. let prog = &[
  402. 0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  403. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  404. ];
  405. let vm = rbpf::EbpfVmNoData::new(prog);
  406. vm.prog_exec();
  407. // Memory check not implemented for JIT yet.
  408. }
  409. #[test]
  410. fn test_vm_jit_ldindb() {
  411. let prog = &[
  412. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  413. 0x50, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  414. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  415. ];
  416. let mem = &mut [
  417. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  418. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  419. ];
  420. let mut vm = rbpf::EbpfVmRaw::new(prog);
  421. assert_eq!(vm.prog_exec(mem), 0x88);
  422. vm.jit_compile();
  423. unsafe {
  424. assert_eq!(vm.prog_exec_jit(mem), 0x88);
  425. };
  426. }
  427. #[test]
  428. fn test_vm_jit_ldindh() {
  429. let prog = &[
  430. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  431. 0x48, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  432. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  433. ];
  434. let mem = &mut [
  435. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  436. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  437. ];
  438. let mut vm = rbpf::EbpfVmRaw::new(prog);
  439. assert_eq!(vm.prog_exec(mem), 0x9988);
  440. vm.jit_compile();
  441. unsafe {
  442. assert_eq!(vm.prog_exec_jit(mem), 0x9988);
  443. };
  444. }
  445. #[test]
  446. fn test_vm_jit_ldindw() {
  447. let prog = &[
  448. 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  449. 0x40, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  450. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  451. ];
  452. let mem = &mut [
  453. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  454. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  455. ];
  456. let mut vm = rbpf::EbpfVmRaw::new(prog);
  457. assert_eq!(vm.prog_exec(mem), 0x88776655);
  458. vm.jit_compile();
  459. unsafe {
  460. assert_eq!(vm.prog_exec_jit(mem), 0x88776655);
  461. };
  462. }
  463. #[test]
  464. fn test_vm_jit_ldinddw() {
  465. let prog = &[
  466. 0xb7, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  467. 0x58, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  468. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  469. ];
  470. let mem = &mut [
  471. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  472. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  473. ];
  474. let mut vm = rbpf::EbpfVmRaw::new(prog);
  475. assert_eq!(vm.prog_exec(mem), 0xccbbaa9988776655);
  476. vm.jit_compile();
  477. unsafe {
  478. assert_eq!(vm.prog_exec_jit(mem), 0xccbbaa9988776655);
  479. };
  480. }
  481. #[test]
  482. #[should_panic(expected = "Error: out of bounds memory load (insn #2),")]
  483. fn test_vm_err_ldindb_oob() {
  484. let prog = &[
  485. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  486. 0x38, 0x10, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
  487. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  488. ];
  489. let mem = &mut [
  490. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  491. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  492. ];
  493. let vm = rbpf::EbpfVmRaw::new(prog);
  494. vm.prog_exec(mem);
  495. // Memory check not implemented for JIT yet.
  496. }
  497. #[test]
  498. #[should_panic(expected = "Error: out of bounds memory load (insn #2),")]
  499. fn test_vm_err_ldindb_nomem() {
  500. let prog = &[
  501. 0xb7, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  502. 0x38, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  503. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  504. ];
  505. let vm = rbpf::EbpfVmNoData::new(prog);
  506. vm.prog_exec();
  507. // Memory check not implemented for JIT yet.
  508. }