misc.rs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. // SPDX-License-Identifier: (Apache-2.0 OR MIT)
  2. // Copyright 2016 6WIND S.A. <quentin.monnet@6wind.com>
  3. // This crate would be needed to load bytecode from a BPF-compiled object file. Since the crate
  4. // is not used anywhere else in the library, it is deactivated: we do not want to load and compile
  5. // it just for the tests. If you want to use it, do not forget to add the following
  6. // dependency to your Cargo.toml file:
  7. //
  8. // ---
  9. // elf = "0.0.10"
  10. // ---
  11. //
  12. // extern crate elf;
  13. // use std::path::PathBuf;
  14. extern crate rbpf;
  15. use rbpf::lib::{Error, ErrorKind};
  16. use rbpf::assembler::assemble;
  17. #[cfg(feature = "std")]
  18. use rbpf::helpers;
  19. // The following two examples have been compiled from C with the following command:
  20. //
  21. // ```bash
  22. // clang -O2 -emit-llvm -c <file.c> -o - | llc -march=bpf -filetype=obj -o <file.o>
  23. // ```
  24. //
  25. // The C source code was the following:
  26. //
  27. // ```c
  28. // #include <linux/ip.h>
  29. // #include <linux/in.h>
  30. // #include <linux/tcp.h>
  31. // #include <linux/bpf.h>
  32. //
  33. // #define ETH_ALEN 6
  34. // #define ETH_P_IP 0x0008 /* htons(0x0800) */
  35. // #define TCP_HDR_LEN 20
  36. //
  37. // #define BLOCKED_TCP_PORT 0x9999
  38. //
  39. // struct eth_hdr {
  40. // unsigned char h_dest[ETH_ALEN];
  41. // unsigned char h_source[ETH_ALEN];
  42. // unsigned short h_proto;
  43. // };
  44. //
  45. // #define SEC(NAME) __attribute__((section(NAME), used))
  46. // SEC(".classifier")
  47. // int handle_ingress(struct __sk_buff *skb)
  48. // {
  49. // void *data = (void *)(long)skb->data;
  50. // void *data_end = (void *)(long)skb->data_end;
  51. // struct eth_hdr *eth = data;
  52. // struct iphdr *iph = data + sizeof(*eth);
  53. // struct tcphdr *tcp = data + sizeof(*eth) + sizeof(*iph);
  54. //
  55. // /* single length check */
  56. // if (data + sizeof(*eth) + sizeof(*iph) + sizeof(*tcp) > data_end)
  57. // return 0;
  58. // if (eth->h_proto != ETH_P_IP)
  59. // return 0;
  60. // if (iph->protocol != IPPROTO_TCP)
  61. // return 0;
  62. // if (tcp->source == BLOCKED_TCP_PORT || tcp->dest == BLOCKED_TCP_PORT)
  63. // return -1;
  64. // return 0;
  65. // }
  66. // char _license[] SEC(".license") = "GPL";
  67. // ```
  68. //
  69. // This program, once compiled, can be injected into Linux kernel, with tc for instance. Sadly, we
  70. // need to bring some modifications to the generated bytecode in order to run it: the three
  71. // instructions with opcode 0x61 load data from a packet area as 4-byte words, where we need to
  72. // load it as 8-bytes double words (0x79). The kernel does the same kind of translation before
  73. // running the program, but rbpf does not implement this.
  74. //
  75. // In addition, the offset at which the pointer to the packet data is stored must be changed: since
  76. // we use 8 bytes instead of 4 for the start and end addresses of the data packet, we cannot use
  77. // the offsets produced by clang (0x4c and 0x50), the addresses would overlap. Instead we can use,
  78. // for example, 0x40 and 0x50. See comments on the bytecode below to see the modifications.
  79. //
  80. // Once the bytecode has been (manually, in our case) edited, we can load the bytecode directly
  81. // from the ELF object file. This is easy to do, but requires the addition of two crates in the
  82. // Cargo.toml file (see comments above), so here we use just the hardcoded bytecode instructions
  83. // instead.
  84. #[test]
  85. #[cfg(feature = "std")]
  86. fn test_vm_block_port() {
  87. // To load the bytecode from an object file instead of using the hardcoded instructions,
  88. // use the additional crates commented at the beginning of this file (and also add them to your
  89. // Cargo.toml). See comments above.
  90. //
  91. // ---
  92. // let filename = "my_ebpf_object_file.o";
  93. //
  94. // let path = PathBuf::from(filename);
  95. // let file = match elf::File::open_path(&path) {
  96. // Ok(f) => f,
  97. // Err(e) => panic!("Error: {:?}", e),
  98. // };
  99. //
  100. // let text_scn = match file.get_section(".classifier") {
  101. // Some(s) => s,
  102. // None => panic!("Failed to look up .classifier section"),
  103. // };
  104. //
  105. // let prog = &text_scn.data;
  106. // ---
  107. #[rustfmt::skip]
  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. #[rustfmt::skip]
  136. let packet = &mut [
  137. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
  138. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
  139. 0x08, 0x00, // ethertype
  140. 0x45, 0x00, 0x00, 0x3b, // start ip_hdr
  141. 0xa6, 0xab, 0x40, 0x00,
  142. 0x40, 0x06, 0x96, 0x0f,
  143. 0x7f, 0x00, 0x00, 0x01,
  144. 0x7f, 0x00, 0x00, 0x01,
  145. // Program matches the next two bytes: 0x9999 returns 0xffffffff, else return 0.
  146. 0x99, 0x99, 0xc6, 0xcc, // start tcp_hdr
  147. 0xd1, 0xe5, 0xc4, 0x9d,
  148. 0xd4, 0x30, 0xb5, 0xd2,
  149. 0x80, 0x18, 0x01, 0x56,
  150. 0xfe, 0x2f, 0x00, 0x00,
  151. 0x01, 0x01, 0x08, 0x0a, // start data
  152. 0x00, 0x23, 0x75, 0x89,
  153. 0x00, 0x23, 0x63, 0x2d,
  154. 0x71, 0x64, 0x66, 0x73,
  155. 0x64, 0x66, 0x0au8
  156. ];
  157. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x40, 0x50).unwrap();
  158. vm.register_helper(helpers::BPF_TRACE_PRINTK_IDX, helpers::bpf_trace_printf).unwrap();
  159. let res = vm.execute_program(packet).unwrap();
  160. println!("Program returned: {res:?} ({res:#x})");
  161. assert_eq!(res, 0xffffffff);
  162. }
  163. #[test]
  164. #[cfg(all(not(windows), feature = "std"))]
  165. fn test_jit_block_port() {
  166. // To load the bytecode from an object file instead of using the hardcoded instructions,
  167. // use the additional crates commented at the beginning of this file (and also add them to your
  168. // Cargo.toml). See comments above.
  169. //
  170. // ---
  171. // let filename = "my_ebpf_object_file.o";
  172. //
  173. // let path = PathBuf::from(filename);
  174. // let file = match elf::File::open_path(&path) {
  175. // Ok(f) => f,
  176. // Err(e) => panic!("Error: {:?}", e),
  177. // };
  178. //
  179. // let text_scn = match file.get_section(".classifier") {
  180. // Some(s) => s,
  181. // None => panic!("Failed to look up .classifier section"),
  182. // };
  183. //
  184. // let prog = &text_scn.data;
  185. // ---
  186. #[rustfmt::skip]
  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. #[rustfmt::skip]
  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).unwrap();
  238. vm.jit_compile().unwrap();
  239. unsafe {
  240. let res = vm.execute_program_jit(packet).unwrap();
  241. println!("Program returned: {res:?} ({res:#x})");
  242. assert_eq!(res, 0xffffffff);
  243. }
  244. }
  245. // Program and memory come from uBPF test ldxh.
  246. #[test]
  247. fn test_vm_mbuff() {
  248. #[rustfmt::skip]
  249. let prog = &[
  250. // Load mem from mbuff into R1
  251. 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  252. // ldhx r1[2], r0
  253. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  254. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  255. ];
  256. let mem = &[
  257. 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
  258. ];
  259. let mbuff = [0u8; 32];
  260. unsafe {
  261. let data = mbuff.as_ptr().offset(8) as *mut u64;
  262. let data_end = mbuff.as_ptr().offset(24) as *mut u64;
  263. data.write_unaligned(mem.as_ptr() as u64);
  264. data_end.write_unaligned(mem.as_ptr() as u64 + mem.len() as u64);
  265. }
  266. let vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
  267. assert_eq!(vm.execute_program(mem, &mbuff).unwrap(), 0x2211);
  268. }
  269. // Program and memory come from uBPF test ldxh.
  270. #[test]
  271. fn test_vm_mbuff_with_rust_api() {
  272. use rbpf::insn_builder::*;
  273. let mut program = BpfCode::new();
  274. program
  275. .load_x(MemSize::DoubleWord).set_dst(0x01).set_src(0x01).set_off(0x00_08).push()
  276. .load_x(MemSize::HalfWord).set_dst(0x00).set_src(0x01).set_off(0x00_02).push()
  277. .exit().push();
  278. let mem = &[
  279. 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
  280. ];
  281. let mbuff = [0u8; 32];
  282. unsafe {
  283. let data = mbuff.as_ptr().offset(8) as *mut u64;
  284. let data_end = mbuff.as_ptr().offset(24) as *mut u64;
  285. *data = mem.as_ptr() as u64;
  286. *data_end = mem.as_ptr() as u64 + mem.len() as u64;
  287. }
  288. let vm = rbpf::EbpfVmMbuff::new(Some(program.into_bytes())).unwrap();
  289. assert_eq!(vm.execute_program(mem, &mbuff).unwrap(), 0x2211);
  290. }
  291. // Program and memory come from uBPF test ldxh.
  292. #[test]
  293. #[cfg(all(not(windows), feature = "std"))]
  294. fn test_jit_mbuff() {
  295. #[rustfmt::skip]
  296. let prog = &[
  297. // Load mem from mbuff into R1
  298. 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  299. // ldhx r1[2], r0
  300. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  301. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  302. ];
  303. let mem = &mut [
  304. 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
  305. ];
  306. let mut mbuff = [0u8; 32];
  307. unsafe {
  308. let data = mbuff.as_ptr().offset(8) as *mut u64;
  309. let data_end = mbuff.as_ptr().offset(24) as *mut u64;
  310. *data = mem.as_ptr() as u64;
  311. *data_end = mem.as_ptr() as u64 + mem.len() as u64;
  312. }
  313. unsafe {
  314. let mut vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
  315. vm.jit_compile().unwrap();
  316. assert_eq!(vm.execute_program_jit(mem, &mut mbuff).unwrap(), 0x2211);
  317. }
  318. }
  319. #[cfg(all(not(windows), feature = "std"))]
  320. #[test]
  321. fn test_vm_jit_ldabsb() {
  322. #[rustfmt::skip]
  323. let prog = &[
  324. 0x30, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  325. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  326. ];
  327. #[rustfmt::skip]
  328. let mem = &mut [
  329. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  330. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  331. ];
  332. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  333. assert_eq!(vm.execute_program(mem).unwrap(), 0x33);
  334. vm.jit_compile().unwrap();
  335. unsafe {
  336. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x33);
  337. };
  338. }
  339. #[cfg(all(not(windows), feature = "std"))]
  340. #[test]
  341. fn test_vm_jit_ldabsh() {
  342. #[rustfmt::skip]
  343. let prog = &[
  344. 0x28, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  345. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  346. ];
  347. #[rustfmt::skip]
  348. let mem = &mut [
  349. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  350. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  351. ];
  352. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  353. assert_eq!(vm.execute_program(mem).unwrap(), 0x4433);
  354. vm.jit_compile().unwrap();
  355. unsafe {
  356. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x4433);
  357. };
  358. }
  359. #[cfg(all(not(windows), feature = "std"))]
  360. #[test]
  361. fn test_vm_jit_ldabsw() {
  362. #[rustfmt::skip]
  363. let prog = &[
  364. 0x20, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  365. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  366. ];
  367. #[rustfmt::skip]
  368. let mem = &mut [
  369. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  370. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  371. ];
  372. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  373. assert_eq!(vm.execute_program(mem).unwrap(), 0x66554433);
  374. vm.jit_compile().unwrap();
  375. unsafe {
  376. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x66554433);
  377. };
  378. }
  379. #[cfg(all(not(windows), feature = "std"))]
  380. #[test]
  381. fn test_vm_jit_ldabsdw() {
  382. #[rustfmt::skip]
  383. let prog = &[
  384. 0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  385. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  386. ];
  387. #[rustfmt::skip]
  388. let mem = &mut [
  389. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  390. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  391. ];
  392. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  393. assert_eq!(vm.execute_program(mem).unwrap(), 0xaa99887766554433);
  394. vm.jit_compile().unwrap();
  395. unsafe {
  396. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0xaa99887766554433);
  397. };
  398. }
  399. #[test]
  400. #[should_panic(expected = "Error: out of bounds memory load (insn #1),")]
  401. fn test_vm_err_ldabsb_oob() {
  402. #[rustfmt::skip]
  403. let prog = &[
  404. 0x38, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
  405. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  406. ];
  407. #[rustfmt::skip]
  408. let mem = &mut [
  409. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  410. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  411. ];
  412. let vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  413. vm.execute_program(mem).unwrap();
  414. // Memory check not implemented for JIT yet.
  415. }
  416. #[test]
  417. #[should_panic(expected = "Error: out of bounds memory load (insn #1),")]
  418. fn test_vm_err_ldabsb_nomem() {
  419. #[rustfmt::skip]
  420. let prog = &[
  421. 0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  422. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  423. ];
  424. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  425. vm.execute_program().unwrap();
  426. // Memory check not implemented for JIT yet.
  427. }
  428. #[cfg(all(not(windows), feature = "std"))]
  429. #[test]
  430. fn test_vm_jit_ldindb() {
  431. #[rustfmt::skip]
  432. let prog = &[
  433. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  434. 0x50, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  435. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  436. ];
  437. #[rustfmt::skip]
  438. let mem = &mut [
  439. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  440. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  441. ];
  442. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  443. assert_eq!(vm.execute_program(mem).unwrap(), 0x88);
  444. vm.jit_compile().unwrap();
  445. unsafe {
  446. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x88);
  447. };
  448. }
  449. #[cfg(all(not(windows), feature = "std"))]
  450. #[test]
  451. fn test_vm_jit_ldindh() {
  452. #[rustfmt::skip]
  453. let prog = &[
  454. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  455. 0x48, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  456. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  457. ];
  458. #[rustfmt::skip]
  459. let mem = &mut [
  460. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  461. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  462. ];
  463. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  464. assert_eq!(vm.execute_program(mem).unwrap(), 0x9988);
  465. vm.jit_compile().unwrap();
  466. unsafe {
  467. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x9988);
  468. };
  469. }
  470. #[cfg(all(not(windows), feature = "std"))]
  471. #[test]
  472. fn test_vm_jit_ldindw() {
  473. #[rustfmt::skip]
  474. let prog = &[
  475. 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  476. 0x40, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  477. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  478. ];
  479. #[rustfmt::skip]
  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.execute_program(mem).unwrap(), 0x88776655);
  486. vm.jit_compile().unwrap();
  487. unsafe {
  488. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x88776655);
  489. };
  490. }
  491. #[cfg(all(not(windows), feature = "std"))]
  492. #[test]
  493. fn test_vm_jit_ldinddw() {
  494. #[rustfmt::skip]
  495. let prog = &[
  496. 0xb7, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  497. 0x58, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  498. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  499. ];
  500. #[rustfmt::skip]
  501. let mem = &mut [
  502. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  503. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  504. ];
  505. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  506. assert_eq!(vm.execute_program(mem).unwrap(), 0xccbbaa9988776655);
  507. vm.jit_compile().unwrap();
  508. unsafe {
  509. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0xccbbaa9988776655);
  510. };
  511. }
  512. #[test]
  513. #[should_panic(expected = "Error: out of bounds memory load (insn #2),")]
  514. fn test_vm_err_ldindb_oob() {
  515. #[rustfmt::skip]
  516. let prog = &[
  517. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  518. 0x38, 0x10, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
  519. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  520. ];
  521. #[rustfmt::skip]
  522. let mem = &mut [
  523. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  524. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  525. ];
  526. let vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  527. vm.execute_program(mem).unwrap();
  528. // Memory check not implemented for JIT yet.
  529. }
  530. #[test]
  531. #[should_panic(expected = "Error: out of bounds memory load (insn #2),")]
  532. fn test_vm_err_ldindb_nomem() {
  533. #[rustfmt::skip]
  534. let prog = &[
  535. 0xb7, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  536. 0x38, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  537. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  538. ];
  539. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  540. vm.execute_program().unwrap();
  541. // Memory check not implemented for JIT yet.
  542. }
  543. #[test]
  544. #[should_panic(expected = "Error: No program set, call prog_set() to load one")]
  545. fn test_vm_exec_no_program() {
  546. let vm = rbpf::EbpfVmNoData::new(None).unwrap();
  547. assert_eq!(vm.execute_program().unwrap(), 0xBEE);
  548. }
  549. fn verifier_success(_prog: &[u8]) -> Result<(), Error> {
  550. Ok(())
  551. }
  552. fn verifier_fail(_prog: &[u8]) -> Result<(), Error> {
  553. Err(Error::new(ErrorKind::Other,
  554. "Gaggablaghblagh!"))
  555. }
  556. #[test]
  557. fn test_verifier_success() {
  558. let prog = assemble(
  559. "mov32 r0, 0xBEE
  560. exit",
  561. ).unwrap();
  562. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  563. vm.set_verifier(verifier_success).unwrap();
  564. vm.set_program(&prog).unwrap();
  565. assert_eq!(vm.execute_program().unwrap(), 0xBEE);
  566. }
  567. #[test]
  568. #[should_panic(expected = "Gaggablaghblagh!")]
  569. fn test_verifier_fail() {
  570. let prog = assemble(
  571. "mov32 r0, 0xBEE
  572. exit",
  573. ).unwrap();
  574. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  575. vm.set_verifier(verifier_fail).unwrap();
  576. vm.set_program(&prog).unwrap();
  577. assert_eq!(vm.execute_program().unwrap(), 0xBEE);
  578. }
  579. #[test]
  580. fn test_vm_bpf_to_bpf_call(){
  581. let test_code = assemble(
  582. "
  583. mov64 r1, 0x10
  584. mov64 r2, 0x1
  585. callx 0x4
  586. mov64 r1, 0x1
  587. mov64 r2, r0
  588. callx 0x4
  589. exit
  590. mov64 r0, r1
  591. sub64 r0, r2
  592. exit
  593. mov64 r0, r2
  594. add64 r0, r1
  595. exit
  596. ").unwrap();
  597. let vm = rbpf::EbpfVmNoData::new(Some(&test_code)).unwrap();
  598. let vm_res= vm.execute_program().unwrap();
  599. assert_eq!(vm_res, 0x10);
  600. }
  601. #[cfg(all(not(windows), feature = "std"))]
  602. #[test]
  603. fn test_vm_jit_bpf_to_bpf_call(){
  604. let test_code = assemble(
  605. "
  606. mov64 r1, 0x10
  607. mov64 r2, 0x1
  608. callx 0x4
  609. mov64 r1, 0x1
  610. mov64 r2, r0
  611. callx 0x4
  612. exit
  613. mov64 r0, r1
  614. sub64 r0, r2
  615. exit
  616. mov64 r0, r2
  617. add64 r0, r1
  618. exit
  619. ").unwrap();
  620. let mut vm = rbpf::EbpfVmNoData::new(Some(&test_code)).unwrap();
  621. vm.jit_compile().unwrap();
  622. let vm_res= unsafe { vm.execute_program_jit().unwrap() };
  623. assert_eq!(vm_res, 0x10);
  624. }
  625. #[test]
  626. #[should_panic(expected = "[Verifier] Error: unsupported call type #2 (insn #0)")]
  627. fn test_verifier_err_other_type_call(){
  628. #[rustfmt::skip]
  629. let prog = &[
  630. 0x85, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  631. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  632. ];
  633. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  634. vm.execute_program().unwrap();
  635. }
  636. #[test]
  637. #[should_panic(expected = "Error: unsupported call type #2 (insn #0)")]
  638. fn test_vm_other_type_call(){
  639. #[rustfmt::skip]
  640. let prog = &[
  641. 0x85, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  642. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  643. ];
  644. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  645. vm.set_verifier(|_|{
  646. Ok(())
  647. }).unwrap();
  648. vm.set_program(prog).unwrap();
  649. vm.execute_program().unwrap();
  650. }
  651. #[cfg(all(not(windows), feature = "std"))]
  652. #[test]
  653. #[should_panic(expected = "[JIT] Error: unexpected call type #2 (insn #0)")]
  654. fn test_vm_jit_other_type_call(){
  655. #[rustfmt::skip]
  656. let prog = &[
  657. 0x85, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  658. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  659. ];
  660. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  661. vm.set_verifier(|_|{
  662. Ok(())
  663. }).unwrap();
  664. vm.set_program(prog).unwrap();
  665. vm.jit_compile().unwrap();
  666. unsafe { vm.execute_program_jit().unwrap() };
  667. }
  668. #[test]
  669. #[should_panic(expected = "Error: out of bounds memory store (insn #8)")]
  670. fn test_stack_overflow(){
  671. // The stdw instruction is used to test the stack overflow.
  672. let test_code = assemble(
  673. "
  674. mov64 r1, 0x10
  675. mov64 r2, 0x1
  676. callx 0x4
  677. mov64 r1, 0x1
  678. mov64 r2, r0
  679. callx 0x5
  680. exit
  681. stdw [r10-8], 0xcd
  682. mov64 r0, r1
  683. sub64 r0, r2
  684. exit
  685. mov64 r0, r2
  686. add64 r0, r1
  687. exit
  688. ").unwrap();
  689. let mut vm = rbpf::EbpfVmNoData::new(Some(&test_code)).unwrap();
  690. vm.set_stack_usage_calculator(|_,_,_| 512, Box::new(())).unwrap();
  691. vm.execute_program().unwrap();
  692. }