misc.rs 20 KB

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