4
0

misc.rs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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::assembler::assemble;
  16. #[cfg(feature = "std")]
  17. use rbpf::helpers;
  18. use rbpf::lib::{Error, ErrorKind};
  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)
  159. .unwrap();
  160. let res = vm.execute_program(packet).unwrap();
  161. println!("Program returned: {res:?} ({res:#x})");
  162. assert_eq!(res, 0xffffffff);
  163. }
  164. #[test]
  165. #[cfg(all(not(windows), feature = "std"))]
  166. fn test_jit_block_port() {
  167. // To load the bytecode from an object file instead of using the hardcoded instructions,
  168. // use the additional crates commented at the beginning of this file (and also add them to your
  169. // Cargo.toml). See comments above.
  170. //
  171. // ---
  172. // let filename = "my_ebpf_object_file.o";
  173. //
  174. // let path = PathBuf::from(filename);
  175. // let file = match elf::File::open_path(&path) {
  176. // Ok(f) => f,
  177. // Err(e) => panic!("Error: {:?}", e),
  178. // };
  179. //
  180. // let text_scn = match file.get_section(".classifier") {
  181. // Some(s) => s,
  182. // None => panic!("Failed to look up .classifier section"),
  183. // };
  184. //
  185. // let prog = &text_scn.data;
  186. // ---
  187. #[rustfmt::skip]
  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. #[rustfmt::skip]
  216. let packet = &mut [
  217. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
  218. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
  219. 0x08, 0x00, // ethertype
  220. 0x45, 0x00, 0x00, 0x3b, // start ip_hdr
  221. 0xa6, 0xab, 0x40, 0x00,
  222. 0x40, 0x06, 0x96, 0x0f,
  223. 0x7f, 0x00, 0x00, 0x01,
  224. 0x7f, 0x00, 0x00, 0x01,
  225. // Program matches the next two bytes: 0x9999 returns 0xffffffff, else return 0.
  226. 0x99, 0x99, 0xc6, 0xcc, // start tcp_hdr
  227. 0xd1, 0xe5, 0xc4, 0x9d,
  228. 0xd4, 0x30, 0xb5, 0xd2,
  229. 0x80, 0x18, 0x01, 0x56,
  230. 0xfe, 0x2f, 0x00, 0x00,
  231. 0x01, 0x01, 0x08, 0x0a, // start data
  232. 0x00, 0x23, 0x75, 0x89,
  233. 0x00, 0x23, 0x63, 0x2d,
  234. 0x71, 0x64, 0x66, 0x73,
  235. 0x64, 0x66, 0x0au8
  236. ];
  237. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x40, 0x50).unwrap();
  238. vm.register_helper(helpers::BPF_TRACE_PRINTK_IDX, helpers::bpf_trace_printf)
  239. .unwrap();
  240. vm.jit_compile().unwrap();
  241. unsafe {
  242. let res = vm.execute_program_jit(packet).unwrap();
  243. println!("Program returned: {res:?} ({res:#x})");
  244. assert_eq!(res, 0xffffffff);
  245. }
  246. }
  247. // Program and memory come from uBPF test ldxh.
  248. #[test]
  249. fn test_vm_mbuff() {
  250. #[rustfmt::skip]
  251. let prog = &[
  252. // Load mem from mbuff into R1
  253. 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  254. // ldhx r1[2], r0
  255. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  256. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  257. ];
  258. let mem = &[0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd];
  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)
  276. .set_dst(0x01)
  277. .set_src(0x01)
  278. .set_off(0x00_08)
  279. .push()
  280. .load_x(MemSize::HalfWord)
  281. .set_dst(0x00)
  282. .set_src(0x01)
  283. .set_off(0x00_02)
  284. .push()
  285. .exit()
  286. .push();
  287. let mem = &[0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd];
  288. let mbuff = [0u8; 32];
  289. unsafe {
  290. let data = mbuff.as_ptr().offset(8) as *mut u64;
  291. let data_end = mbuff.as_ptr().offset(24) as *mut u64;
  292. *data = mem.as_ptr() as u64;
  293. *data_end = mem.as_ptr() as u64 + mem.len() as u64;
  294. }
  295. let vm = rbpf::EbpfVmMbuff::new(Some(program.into_bytes())).unwrap();
  296. assert_eq!(vm.execute_program(mem, &mbuff).unwrap(), 0x2211);
  297. }
  298. // Program and memory come from uBPF test ldxh.
  299. #[test]
  300. #[cfg(all(not(windows), feature = "std"))]
  301. fn test_jit_mbuff() {
  302. #[rustfmt::skip]
  303. let prog = &[
  304. // Load mem from mbuff into R1
  305. 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  306. // ldhx r1[2], r0
  307. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  308. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  309. ];
  310. let mem = &mut [0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd];
  311. let mut mbuff = [0u8; 32];
  312. unsafe {
  313. let data = mbuff.as_ptr().offset(8) as *mut u64;
  314. let data_end = mbuff.as_ptr().offset(24) as *mut u64;
  315. *data = mem.as_ptr() as u64;
  316. *data_end = mem.as_ptr() as u64 + mem.len() as u64;
  317. }
  318. unsafe {
  319. let mut vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
  320. vm.jit_compile().unwrap();
  321. assert_eq!(vm.execute_program_jit(mem, &mut mbuff).unwrap(), 0x2211);
  322. }
  323. }
  324. #[cfg(all(not(windows), feature = "std"))]
  325. #[test]
  326. fn test_vm_jit_ldabsb() {
  327. #[rustfmt::skip]
  328. let prog = &[
  329. 0x30, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  330. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  331. ];
  332. #[rustfmt::skip]
  333. let mem = &mut [
  334. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  335. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  336. ];
  337. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  338. assert_eq!(vm.execute_program(mem).unwrap(), 0x33);
  339. vm.jit_compile().unwrap();
  340. unsafe {
  341. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x33);
  342. };
  343. }
  344. #[cfg(all(not(windows), feature = "std"))]
  345. #[test]
  346. fn test_vm_jit_ldabsh() {
  347. #[rustfmt::skip]
  348. let prog = &[
  349. 0x28, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  350. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  351. ];
  352. #[rustfmt::skip]
  353. let mem = &mut [
  354. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  355. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  356. ];
  357. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  358. assert_eq!(vm.execute_program(mem).unwrap(), 0x4433);
  359. vm.jit_compile().unwrap();
  360. unsafe {
  361. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x4433);
  362. };
  363. }
  364. #[cfg(all(not(windows), feature = "std"))]
  365. #[test]
  366. fn test_vm_jit_ldabsw() {
  367. #[rustfmt::skip]
  368. let prog = &[
  369. 0x20, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  370. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  371. ];
  372. #[rustfmt::skip]
  373. let mem = &mut [
  374. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  375. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  376. ];
  377. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  378. assert_eq!(vm.execute_program(mem).unwrap(), 0x66554433);
  379. vm.jit_compile().unwrap();
  380. unsafe {
  381. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x66554433);
  382. };
  383. }
  384. #[cfg(all(not(windows), feature = "std"))]
  385. #[test]
  386. fn test_vm_jit_ldabsdw() {
  387. #[rustfmt::skip]
  388. let prog = &[
  389. 0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  390. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  391. ];
  392. #[rustfmt::skip]
  393. let mem = &mut [
  394. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  395. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  396. ];
  397. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  398. assert_eq!(vm.execute_program(mem).unwrap(), 0xaa99887766554433);
  399. vm.jit_compile().unwrap();
  400. unsafe {
  401. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0xaa99887766554433);
  402. };
  403. }
  404. #[test]
  405. #[should_panic(expected = "Error: out of bounds memory load (insn #1),")]
  406. fn test_vm_err_ldabsb_oob() {
  407. #[rustfmt::skip]
  408. let prog = &[
  409. 0x38, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
  410. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  411. ];
  412. #[rustfmt::skip]
  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 vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  418. vm.execute_program(mem).unwrap();
  419. // Memory check not implemented for JIT yet.
  420. }
  421. #[test]
  422. #[should_panic(expected = "Error: out of bounds memory load (insn #1),")]
  423. fn test_vm_err_ldabsb_nomem() {
  424. #[rustfmt::skip]
  425. let prog = &[
  426. 0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  427. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  428. ];
  429. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  430. vm.execute_program().unwrap();
  431. // Memory check not implemented for JIT yet.
  432. }
  433. #[cfg(all(not(windows), feature = "std"))]
  434. #[test]
  435. fn test_vm_jit_ldindb() {
  436. #[rustfmt::skip]
  437. let prog = &[
  438. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  439. 0x50, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  440. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  441. ];
  442. #[rustfmt::skip]
  443. let mem = &mut [
  444. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  445. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  446. ];
  447. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  448. assert_eq!(vm.execute_program(mem).unwrap(), 0x88);
  449. vm.jit_compile().unwrap();
  450. unsafe {
  451. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x88);
  452. };
  453. }
  454. #[cfg(all(not(windows), feature = "std"))]
  455. #[test]
  456. fn test_vm_jit_ldindh() {
  457. #[rustfmt::skip]
  458. let prog = &[
  459. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  460. 0x48, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  461. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  462. ];
  463. #[rustfmt::skip]
  464. let mem = &mut [
  465. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  466. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  467. ];
  468. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  469. assert_eq!(vm.execute_program(mem).unwrap(), 0x9988);
  470. vm.jit_compile().unwrap();
  471. unsafe {
  472. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x9988);
  473. };
  474. }
  475. #[cfg(all(not(windows), feature = "std"))]
  476. #[test]
  477. fn test_vm_jit_ldindw() {
  478. #[rustfmt::skip]
  479. let prog = &[
  480. 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  481. 0x40, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  482. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  483. ];
  484. #[rustfmt::skip]
  485. let mem = &mut [
  486. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  487. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  488. ];
  489. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  490. assert_eq!(vm.execute_program(mem).unwrap(), 0x88776655);
  491. vm.jit_compile().unwrap();
  492. unsafe {
  493. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x88776655);
  494. };
  495. }
  496. #[cfg(all(not(windows), feature = "std"))]
  497. #[test]
  498. fn test_vm_jit_ldinddw() {
  499. #[rustfmt::skip]
  500. let prog = &[
  501. 0xb7, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  502. 0x58, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  503. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  504. ];
  505. #[rustfmt::skip]
  506. let mem = &mut [
  507. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  508. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  509. ];
  510. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  511. assert_eq!(vm.execute_program(mem).unwrap(), 0xccbbaa9988776655);
  512. vm.jit_compile().unwrap();
  513. unsafe {
  514. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0xccbbaa9988776655);
  515. };
  516. }
  517. #[test]
  518. #[should_panic(expected = "Error: out of bounds memory load (insn #2),")]
  519. fn test_vm_err_ldindb_oob() {
  520. #[rustfmt::skip]
  521. let prog = &[
  522. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  523. 0x38, 0x10, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
  524. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  525. ];
  526. #[rustfmt::skip]
  527. let mem = &mut [
  528. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  529. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  530. ];
  531. let vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  532. vm.execute_program(mem).unwrap();
  533. // Memory check not implemented for JIT yet.
  534. }
  535. #[test]
  536. #[should_panic(expected = "Error: out of bounds memory load (insn #2),")]
  537. fn test_vm_err_ldindb_nomem() {
  538. #[rustfmt::skip]
  539. let prog = &[
  540. 0xb7, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  541. 0x38, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  542. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  543. ];
  544. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  545. vm.execute_program().unwrap();
  546. // Memory check not implemented for JIT yet.
  547. }
  548. #[test]
  549. #[should_panic(expected = "Error: No program set, call prog_set() to load one")]
  550. fn test_vm_exec_no_program() {
  551. let vm = rbpf::EbpfVmNoData::new(None).unwrap();
  552. assert_eq!(vm.execute_program().unwrap(), 0xBEE);
  553. }
  554. fn verifier_success(_prog: &[u8]) -> Result<(), Error> {
  555. Ok(())
  556. }
  557. fn verifier_fail(_prog: &[u8]) -> Result<(), Error> {
  558. Err(Error::new(ErrorKind::Other, "Gaggablaghblagh!"))
  559. }
  560. #[test]
  561. fn test_verifier_success() {
  562. let prog = assemble(
  563. "mov32 r0, 0xBEE
  564. exit",
  565. )
  566. .unwrap();
  567. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  568. vm.set_verifier(verifier_success).unwrap();
  569. vm.set_program(&prog).unwrap();
  570. assert_eq!(vm.execute_program().unwrap(), 0xBEE);
  571. }
  572. #[test]
  573. #[should_panic(expected = "Gaggablaghblagh!")]
  574. fn test_verifier_fail() {
  575. let prog = assemble(
  576. "mov32 r0, 0xBEE
  577. exit",
  578. )
  579. .unwrap();
  580. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  581. vm.set_verifier(verifier_fail).unwrap();
  582. vm.set_program(&prog).unwrap();
  583. assert_eq!(vm.execute_program().unwrap(), 0xBEE);
  584. }
  585. #[test]
  586. fn test_vm_bpf_to_bpf_call() {
  587. let test_code = assemble(
  588. "
  589. mov64 r1, 0x10
  590. mov64 r2, 0x1
  591. callx 0x4
  592. mov64 r1, 0x1
  593. mov64 r2, r0
  594. callx 0x4
  595. exit
  596. mov64 r0, r1
  597. sub64 r0, r2
  598. exit
  599. mov64 r0, r2
  600. add64 r0, r1
  601. exit
  602. ",
  603. )
  604. .unwrap();
  605. let vm = rbpf::EbpfVmNoData::new(Some(&test_code)).unwrap();
  606. let vm_res = vm.execute_program().unwrap();
  607. assert_eq!(vm_res, 0x10);
  608. }
  609. #[cfg(all(not(windows), feature = "std"))]
  610. #[test]
  611. fn test_vm_jit_bpf_to_bpf_call() {
  612. let test_code = assemble(
  613. "
  614. mov64 r1, 0x10
  615. mov64 r2, 0x1
  616. callx 0x4
  617. mov64 r1, 0x1
  618. mov64 r2, r0
  619. callx 0x4
  620. exit
  621. mov64 r0, r1
  622. sub64 r0, r2
  623. exit
  624. mov64 r0, r2
  625. add64 r0, r1
  626. exit
  627. ",
  628. )
  629. .unwrap();
  630. let mut vm = rbpf::EbpfVmNoData::new(Some(&test_code)).unwrap();
  631. vm.jit_compile().unwrap();
  632. let vm_res = unsafe { vm.execute_program_jit().unwrap() };
  633. assert_eq!(vm_res, 0x10);
  634. }
  635. #[test]
  636. #[should_panic(expected = "[Verifier] Error: unsupported call type #2 (insn #0)")]
  637. fn test_verifier_err_other_type_call() {
  638. #[rustfmt::skip]
  639. let prog = &[
  640. 0x85, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  641. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  642. ];
  643. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  644. vm.execute_program().unwrap();
  645. }
  646. #[test]
  647. #[should_panic(expected = "Error: unsupported call type #2 (insn #0)")]
  648. fn test_vm_other_type_call() {
  649. #[rustfmt::skip]
  650. let prog = &[
  651. 0x85, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  652. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  653. ];
  654. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  655. vm.set_verifier(|_| Ok(())).unwrap();
  656. vm.set_program(prog).unwrap();
  657. vm.execute_program().unwrap();
  658. }
  659. #[cfg(all(not(windows), feature = "std"))]
  660. #[test]
  661. #[should_panic(expected = "[JIT] Error: unexpected call type #2 (insn #0)")]
  662. fn test_vm_jit_other_type_call() {
  663. #[rustfmt::skip]
  664. let prog = &[
  665. 0x85, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  666. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  667. ];
  668. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  669. vm.set_verifier(|_| Ok(())).unwrap();
  670. vm.set_program(prog).unwrap();
  671. vm.jit_compile().unwrap();
  672. unsafe { vm.execute_program_jit().unwrap() };
  673. }
  674. #[test]
  675. #[should_panic(expected = "Error: out of bounds memory store (insn #8)")]
  676. fn test_stack_overflow() {
  677. // The stdw instruction is used to test the stack overflow.
  678. let test_code = assemble(
  679. "
  680. mov64 r1, 0x10
  681. mov64 r2, 0x1
  682. callx 0x4
  683. mov64 r1, 0x1
  684. mov64 r2, r0
  685. callx 0x5
  686. exit
  687. stdw [r10-8], 0xcd
  688. mov64 r0, r1
  689. sub64 r0, r2
  690. exit
  691. mov64 r0, r2
  692. add64 r0, r1
  693. exit
  694. ",
  695. )
  696. .unwrap();
  697. let mut vm = rbpf::EbpfVmNoData::new(Some(&test_code)).unwrap();
  698. vm.set_stack_usage_calculator(|_, _, _| 512, Box::new(()))
  699. .unwrap();
  700. vm.execute_program().unwrap();
  701. }