4
0

misc.rs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. // These crates would be needed to load bytecode from a BPF-compiled object file. Since the crates
  7. // are not used anywhere else in the library, it is deactivated: we do not want to load and compile
  8. // them just for the tests. If you want to use them, do not forget to add the following
  9. // dependencies to your Cargo.toml file:
  10. //
  11. // ---
  12. // byteorder = "0.5.3"
  13. // elf = "0.0.10"
  14. // ---
  15. //
  16. // extern crate byteorder;
  17. // extern crate elf;
  18. // use std::path::PathBuf;
  19. extern crate rbpf;
  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(prog, 0x40, 0x50);
  157. vm.register_helper(helpers::BPF_TRACE_PRINTK_IDX, helpers::bpf_trace_printf);
  158. let res = vm.prog_exec(packet);
  159. println!("Program returned: {:?} ({:#x})", res, res);
  160. assert_eq!(res, 0xffffffff);
  161. }
  162. #[test]
  163. fn test_jit_block_port() {
  164. // To load the bytecode from an object file instead of using the hardcoded instructions,
  165. // use the additional crates commented at the beginning of this file (and also add them to your
  166. // Cargo.toml). See comments above.
  167. //
  168. // ---
  169. // let filename = "my_ebpf_object_file.o";
  170. //
  171. // let path = PathBuf::from(filename);
  172. // let file = match elf::File::open_path(&path) {
  173. // Ok(f) => f,
  174. // Err(e) => panic!("Error: {:?}", e),
  175. // };
  176. //
  177. // let text_scn = match file.get_section(".classifier") {
  178. // Some(s) => s,
  179. // None => panic!("Failed to look up .classifier section"),
  180. // };
  181. //
  182. // let prog = &text_scn.data;
  183. // ---
  184. let prog = &[
  185. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  186. 0x79, 0x12, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  187. 0x79, 0x11, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61, 0x40 i.o. 0x4c
  188. 0xbf, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  189. 0x07, 0x03, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00,
  190. 0x2d, 0x23, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00,
  191. 0x69, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
  192. 0x55, 0x02, 0x10, 0x00, 0x08, 0x00, 0x00, 0x00,
  193. 0x71, 0x12, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00,
  194. 0x55, 0x02, 0x0e, 0x00, 0x06, 0x00, 0x00, 0x00,
  195. 0x18, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  196. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  197. 0x79, 0x11, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  198. 0xbf, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  199. 0x57, 0x02, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
  200. 0x15, 0x02, 0x08, 0x00, 0x99, 0x99, 0x00, 0x00,
  201. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
  202. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  203. 0x5f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  204. 0xb7, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  205. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0x99, 0x99,
  206. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  207. 0x1d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  208. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  209. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  210. ];
  211. let packet = &mut [
  212. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
  213. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
  214. 0x08, 0x00, // ethertype
  215. 0x45, 0x00, 0x00, 0x3b, // start ip_hdr
  216. 0xa6, 0xab, 0x40, 0x00,
  217. 0x40, 0x06, 0x96, 0x0f,
  218. 0x7f, 0x00, 0x00, 0x01,
  219. 0x7f, 0x00, 0x00, 0x01,
  220. // Program matches the next two bytes: 0x9999 returns 0xffffffff, else return 0.
  221. 0x99, 0x99, 0xc6, 0xcc, // start tcp_hdr
  222. 0xd1, 0xe5, 0xc4, 0x9d,
  223. 0xd4, 0x30, 0xb5, 0xd2,
  224. 0x80, 0x18, 0x01, 0x56,
  225. 0xfe, 0x2f, 0x00, 0x00,
  226. 0x01, 0x01, 0x08, 0x0a, // start data
  227. 0x00, 0x23, 0x75, 0x89,
  228. 0x00, 0x23, 0x63, 0x2d,
  229. 0x71, 0x64, 0x66, 0x73,
  230. 0x64, 0x66, 0x0au8
  231. ];
  232. let mut vm = rbpf::EbpfVmFixedMbuff::new(prog, 0x40, 0x50);
  233. vm.register_helper(helpers::BPF_TRACE_PRINTK_IDX, helpers::bpf_trace_printf);
  234. vm.jit_compile();
  235. unsafe {
  236. let res = vm.prog_exec_jit(packet);
  237. println!("Program returned: {:?} ({:#x})", res, res);
  238. assert_eq!(res, 0xffffffff);
  239. }
  240. }
  241. // Program and memory come from uBPF test ldxh.
  242. #[test]
  243. fn test_vm_mbuff() {
  244. let prog = &[
  245. // Load mem from mbuff into R1
  246. 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  247. // ldhx r1[2], r0
  248. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  249. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  250. ];
  251. let mem = &[
  252. 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
  253. ];
  254. let mbuff = [0u8; 32];
  255. unsafe {
  256. let mut data = mbuff.as_ptr().offset(8) as *mut u64;
  257. let mut data_end = mbuff.as_ptr().offset(24) as *mut u64;
  258. *data = mem.as_ptr() as u64;
  259. *data_end = mem.as_ptr() as u64 + mem.len() as u64;
  260. }
  261. let vm = rbpf::EbpfVmMbuff::new(prog);
  262. assert_eq!(vm.prog_exec(mem, &mbuff), 0x2211);
  263. }
  264. // Program and memory come from uBPF test ldxh.
  265. #[test]
  266. fn test_vm_mbuff_with_rust_api() {
  267. use rbpf::insn_builder::*;
  268. let mut program = BpfCode::new();
  269. program
  270. .load_x(MemSize::DoubleWord).set_dst(0x01).set_src(0x01).set_off(0x00_08).push()
  271. .load_x(MemSize::HalfWord).set_dst(0x00).set_src(0x01).set_off(0x00_02).push()
  272. .exit().push();
  273. let mem = &[
  274. 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
  275. ];
  276. let mbuff = [0u8; 32];
  277. unsafe {
  278. let mut data = mbuff.as_ptr().offset(8) as *mut u64;
  279. let mut data_end = mbuff.as_ptr().offset(24) as *mut u64;
  280. *data = mem.as_ptr() as u64;
  281. *data_end = mem.as_ptr() as u64 + mem.len() as u64;
  282. }
  283. let vm = rbpf::EbpfVmMbuff::new(program.into_bytes());
  284. assert_eq!(vm.prog_exec(mem, &mbuff), 0x2211);
  285. }
  286. // Program and memory come from uBPF test ldxh.
  287. #[test]
  288. fn test_jit_mbuff() {
  289. let prog = &[
  290. // Load mem from mbuff into R1
  291. 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  292. // ldhx r1[2], r0
  293. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  294. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  295. ];
  296. let mem = &mut [
  297. 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
  298. ];
  299. let mut mbuff = [0u8; 32];
  300. unsafe {
  301. let mut data = mbuff.as_ptr().offset(8) as *mut u64;
  302. let mut data_end = mbuff.as_ptr().offset(24) as *mut u64;
  303. *data = mem.as_ptr() as u64;
  304. *data_end = mem.as_ptr() as u64 + mem.len() as u64;
  305. }
  306. unsafe {
  307. let mut vm = rbpf::EbpfVmMbuff::new(prog);
  308. vm.jit_compile();
  309. assert_eq!(vm.prog_exec_jit(mem, &mut mbuff), 0x2211);
  310. }
  311. }
  312. #[test]
  313. fn test_vm_jit_ldabsb() {
  314. let prog = &[
  315. 0x30, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  316. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  317. ];
  318. let mem = &mut [
  319. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  320. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  321. ];
  322. let mut vm = rbpf::EbpfVmRaw::new(prog);
  323. assert_eq!(vm.prog_exec(mem), 0x33);
  324. vm.jit_compile();
  325. unsafe {
  326. assert_eq!(vm.prog_exec_jit(mem), 0x33);
  327. };
  328. }
  329. #[test]
  330. fn test_vm_jit_ldabsh() {
  331. let prog = &[
  332. 0x28, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  333. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  334. ];
  335. let mem = &mut [
  336. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  337. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  338. ];
  339. let mut vm = rbpf::EbpfVmRaw::new(prog);
  340. assert_eq!(vm.prog_exec(mem), 0x4433);
  341. vm.jit_compile();
  342. unsafe {
  343. assert_eq!(vm.prog_exec_jit(mem), 0x4433);
  344. };
  345. }
  346. #[test]
  347. fn test_vm_jit_ldabsw() {
  348. let prog = &[
  349. 0x20, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  350. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  351. ];
  352. let mem = &mut [
  353. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  354. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  355. ];
  356. let mut vm = rbpf::EbpfVmRaw::new(prog);
  357. assert_eq!(vm.prog_exec(mem), 0x66554433);
  358. vm.jit_compile();
  359. unsafe {
  360. assert_eq!(vm.prog_exec_jit(mem), 0x66554433);
  361. };
  362. }
  363. #[test]
  364. fn test_vm_jit_ldabsdw() {
  365. let prog = &[
  366. 0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  367. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  368. ];
  369. let mem = &mut [
  370. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  371. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  372. ];
  373. let mut vm = rbpf::EbpfVmRaw::new(prog);
  374. assert_eq!(vm.prog_exec(mem), 0xaa99887766554433);
  375. vm.jit_compile();
  376. unsafe {
  377. assert_eq!(vm.prog_exec_jit(mem), 0xaa99887766554433);
  378. };
  379. }
  380. #[test]
  381. #[should_panic(expected = "Error: out of bounds memory load (insn #1),")]
  382. fn test_vm_err_ldabsb_oob() {
  383. let prog = &[
  384. 0x38, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
  385. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  386. ];
  387. let mem = &mut [
  388. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  389. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  390. ];
  391. let vm = rbpf::EbpfVmRaw::new(prog);
  392. vm.prog_exec(mem);
  393. // Memory check not implemented for JIT yet.
  394. }
  395. #[test]
  396. #[should_panic(expected = "Error: out of bounds memory load (insn #1),")]
  397. fn test_vm_err_ldabsb_nomem() {
  398. let prog = &[
  399. 0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  400. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  401. ];
  402. let vm = rbpf::EbpfVmNoData::new(prog);
  403. vm.prog_exec();
  404. // Memory check not implemented for JIT yet.
  405. }
  406. #[test]
  407. fn test_vm_jit_ldindb() {
  408. let prog = &[
  409. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  410. 0x50, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  411. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  412. ];
  413. let mem = &mut [
  414. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  415. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  416. ];
  417. let mut vm = rbpf::EbpfVmRaw::new(prog);
  418. assert_eq!(vm.prog_exec(mem), 0x88);
  419. vm.jit_compile();
  420. unsafe {
  421. assert_eq!(vm.prog_exec_jit(mem), 0x88);
  422. };
  423. }
  424. #[test]
  425. fn test_vm_jit_ldindh() {
  426. let prog = &[
  427. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  428. 0x48, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  429. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  430. ];
  431. let mem = &mut [
  432. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  433. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  434. ];
  435. let mut vm = rbpf::EbpfVmRaw::new(prog);
  436. assert_eq!(vm.prog_exec(mem), 0x9988);
  437. vm.jit_compile();
  438. unsafe {
  439. assert_eq!(vm.prog_exec_jit(mem), 0x9988);
  440. };
  441. }
  442. #[test]
  443. fn test_vm_jit_ldindw() {
  444. let prog = &[
  445. 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  446. 0x40, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  447. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  448. ];
  449. let mem = &mut [
  450. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  451. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  452. ];
  453. let mut vm = rbpf::EbpfVmRaw::new(prog);
  454. assert_eq!(vm.prog_exec(mem), 0x88776655);
  455. vm.jit_compile();
  456. unsafe {
  457. assert_eq!(vm.prog_exec_jit(mem), 0x88776655);
  458. };
  459. }
  460. #[test]
  461. fn test_vm_jit_ldinddw() {
  462. let prog = &[
  463. 0xb7, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  464. 0x58, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  465. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  466. ];
  467. let mem = &mut [
  468. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  469. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  470. ];
  471. let mut vm = rbpf::EbpfVmRaw::new(prog);
  472. assert_eq!(vm.prog_exec(mem), 0xccbbaa9988776655);
  473. vm.jit_compile();
  474. unsafe {
  475. assert_eq!(vm.prog_exec_jit(mem), 0xccbbaa9988776655);
  476. };
  477. }
  478. #[test]
  479. #[should_panic(expected = "Error: out of bounds memory load (insn #2),")]
  480. fn test_vm_err_ldindb_oob() {
  481. let prog = &[
  482. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  483. 0x38, 0x10, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
  484. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  485. ];
  486. let mem = &mut [
  487. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  488. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  489. ];
  490. let vm = rbpf::EbpfVmRaw::new(prog);
  491. vm.prog_exec(mem);
  492. // Memory check not implemented for JIT yet.
  493. }
  494. #[test]
  495. #[should_panic(expected = "Error: out of bounds memory load (insn #2),")]
  496. fn test_vm_err_ldindb_nomem() {
  497. let prog = &[
  498. 0xb7, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  499. 0x38, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  500. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  501. ];
  502. let vm = rbpf::EbpfVmNoData::new(prog);
  503. vm.prog_exec();
  504. // Memory check not implemented for JIT yet.
  505. }