misc.rs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. let prog = &[
  108. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  109. 0x79, 0x12, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  110. 0x79, 0x11, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61, 0x40 i.o. 0x4c
  111. 0xbf, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  112. 0x07, 0x03, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00,
  113. 0x2d, 0x23, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00,
  114. 0x69, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
  115. 0x55, 0x02, 0x10, 0x00, 0x08, 0x00, 0x00, 0x00,
  116. 0x71, 0x12, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00,
  117. 0x55, 0x02, 0x0e, 0x00, 0x06, 0x00, 0x00, 0x00,
  118. 0x18, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  119. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  120. 0x79, 0x11, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  121. 0xbf, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  122. 0x57, 0x02, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
  123. 0x15, 0x02, 0x08, 0x00, 0x99, 0x99, 0x00, 0x00,
  124. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
  125. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  126. 0x5f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  127. 0xb7, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  128. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0x99, 0x99,
  129. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  130. 0x1d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  131. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  132. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  133. ];
  134. let packet = &mut [
  135. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
  136. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
  137. 0x08, 0x00, // ethertype
  138. 0x45, 0x00, 0x00, 0x3b, // start ip_hdr
  139. 0xa6, 0xab, 0x40, 0x00,
  140. 0x40, 0x06, 0x96, 0x0f,
  141. 0x7f, 0x00, 0x00, 0x01,
  142. 0x7f, 0x00, 0x00, 0x01,
  143. // Program matches the next two bytes: 0x9999 returns 0xffffffff, else return 0.
  144. 0x99, 0x99, 0xc6, 0xcc, // start tcp_hdr
  145. 0xd1, 0xe5, 0xc4, 0x9d,
  146. 0xd4, 0x30, 0xb5, 0xd2,
  147. 0x80, 0x18, 0x01, 0x56,
  148. 0xfe, 0x2f, 0x00, 0x00,
  149. 0x01, 0x01, 0x08, 0x0a, // start data
  150. 0x00, 0x23, 0x75, 0x89,
  151. 0x00, 0x23, 0x63, 0x2d,
  152. 0x71, 0x64, 0x66, 0x73,
  153. 0x64, 0x66, 0x0au8
  154. ];
  155. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x40, 0x50).unwrap();
  156. vm.register_helper(helpers::BPF_TRACE_PRINTK_IDX, helpers::bpf_trace_printf).unwrap();
  157. let res = vm.execute_program(packet).unwrap();
  158. println!("Program returned: {res:?} ({res:#x})");
  159. assert_eq!(res, 0xffffffff);
  160. }
  161. #[test]
  162. #[cfg(all(not(windows), feature = "std"))]
  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(Some(prog), 0x40, 0x50).unwrap();
  233. vm.register_helper(helpers::BPF_TRACE_PRINTK_IDX, helpers::bpf_trace_printf).unwrap();
  234. vm.jit_compile().unwrap();
  235. unsafe {
  236. let res = vm.execute_program_jit(packet).unwrap();
  237. println!("Program returned: {res:?} ({res:#x})");
  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 data = mbuff.as_ptr().offset(8) as *mut u64;
  257. let data_end = mbuff.as_ptr().offset(24) as *mut u64;
  258. data.write_unaligned(mem.as_ptr() as u64);
  259. data_end.write_unaligned(mem.as_ptr() as u64 + mem.len() as u64);
  260. }
  261. let vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
  262. assert_eq!(vm.execute_program(mem, &mbuff).unwrap(), 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 data = mbuff.as_ptr().offset(8) as *mut u64;
  279. let 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(Some(program.into_bytes())).unwrap();
  284. assert_eq!(vm.execute_program(mem, &mbuff).unwrap(), 0x2211);
  285. }
  286. // Program and memory come from uBPF test ldxh.
  287. #[test]
  288. #[cfg(all(not(windows), feature = "std"))]
  289. fn test_jit_mbuff() {
  290. let prog = &[
  291. // Load mem from mbuff into R1
  292. 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  293. // ldhx r1[2], r0
  294. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  295. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  296. ];
  297. let mem = &mut [
  298. 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
  299. ];
  300. let mut mbuff = [0u8; 32];
  301. unsafe {
  302. let data = mbuff.as_ptr().offset(8) as *mut u64;
  303. let data_end = mbuff.as_ptr().offset(24) as *mut u64;
  304. *data = mem.as_ptr() as u64;
  305. *data_end = mem.as_ptr() as u64 + mem.len() as u64;
  306. }
  307. unsafe {
  308. let mut vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
  309. vm.jit_compile().unwrap();
  310. assert_eq!(vm.execute_program_jit(mem, &mut mbuff).unwrap(), 0x2211);
  311. }
  312. }
  313. #[cfg(all(not(windows), feature = "std"))]
  314. #[test]
  315. fn test_vm_jit_ldabsb() {
  316. let prog = &[
  317. 0x30, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  318. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  319. ];
  320. let mem = &mut [
  321. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  322. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  323. ];
  324. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  325. assert_eq!(vm.execute_program(mem).unwrap(), 0x33);
  326. vm.jit_compile().unwrap();
  327. unsafe {
  328. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x33);
  329. };
  330. }
  331. #[cfg(all(not(windows), feature = "std"))]
  332. #[test]
  333. fn test_vm_jit_ldabsh() {
  334. let prog = &[
  335. 0x28, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  336. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  337. ];
  338. let mem = &mut [
  339. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  340. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  341. ];
  342. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  343. assert_eq!(vm.execute_program(mem).unwrap(), 0x4433);
  344. vm.jit_compile().unwrap();
  345. unsafe {
  346. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x4433);
  347. };
  348. }
  349. #[cfg(all(not(windows), feature = "std"))]
  350. #[test]
  351. fn test_vm_jit_ldabsw() {
  352. let prog = &[
  353. 0x20, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  354. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  355. ];
  356. let mem = &mut [
  357. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  358. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  359. ];
  360. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  361. assert_eq!(vm.execute_program(mem).unwrap(), 0x66554433);
  362. vm.jit_compile().unwrap();
  363. unsafe {
  364. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x66554433);
  365. };
  366. }
  367. #[cfg(all(not(windows), feature = "std"))]
  368. #[test]
  369. fn test_vm_jit_ldabsdw() {
  370. let prog = &[
  371. 0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  372. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  373. ];
  374. let mem = &mut [
  375. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  376. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  377. ];
  378. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  379. assert_eq!(vm.execute_program(mem).unwrap(), 0xaa99887766554433);
  380. vm.jit_compile().unwrap();
  381. unsafe {
  382. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0xaa99887766554433);
  383. };
  384. }
  385. #[test]
  386. #[should_panic(expected = "Error: out of bounds memory load (insn #1),")]
  387. fn test_vm_err_ldabsb_oob() {
  388. let prog = &[
  389. 0x38, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
  390. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  391. ];
  392. let mem = &mut [
  393. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  394. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  395. ];
  396. let vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  397. vm.execute_program(mem).unwrap();
  398. // Memory check not implemented for JIT yet.
  399. }
  400. #[test]
  401. #[should_panic(expected = "Error: out of bounds memory load (insn #1),")]
  402. fn test_vm_err_ldabsb_nomem() {
  403. let prog = &[
  404. 0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  405. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  406. ];
  407. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  408. vm.execute_program().unwrap();
  409. // Memory check not implemented for JIT yet.
  410. }
  411. #[cfg(all(not(windows), feature = "std"))]
  412. #[test]
  413. fn test_vm_jit_ldindb() {
  414. let prog = &[
  415. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  416. 0x50, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  417. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  418. ];
  419. let mem = &mut [
  420. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  421. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  422. ];
  423. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  424. assert_eq!(vm.execute_program(mem).unwrap(), 0x88);
  425. vm.jit_compile().unwrap();
  426. unsafe {
  427. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x88);
  428. };
  429. }
  430. #[cfg(all(not(windows), feature = "std"))]
  431. #[test]
  432. fn test_vm_jit_ldindh() {
  433. let prog = &[
  434. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  435. 0x48, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  436. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  437. ];
  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(), 0x9988);
  444. vm.jit_compile().unwrap();
  445. unsafe {
  446. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x9988);
  447. };
  448. }
  449. #[cfg(all(not(windows), feature = "std"))]
  450. #[test]
  451. fn test_vm_jit_ldindw() {
  452. let prog = &[
  453. 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  454. 0x40, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  455. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  456. ];
  457. let mem = &mut [
  458. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  459. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  460. ];
  461. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  462. assert_eq!(vm.execute_program(mem).unwrap(), 0x88776655);
  463. vm.jit_compile().unwrap();
  464. unsafe {
  465. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x88776655);
  466. };
  467. }
  468. #[cfg(all(not(windows), feature = "std"))]
  469. #[test]
  470. fn test_vm_jit_ldinddw() {
  471. let prog = &[
  472. 0xb7, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  473. 0x58, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  474. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  475. ];
  476. let mem = &mut [
  477. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  478. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  479. ];
  480. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  481. assert_eq!(vm.execute_program(mem).unwrap(), 0xccbbaa9988776655);
  482. vm.jit_compile().unwrap();
  483. unsafe {
  484. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0xccbbaa9988776655);
  485. };
  486. }
  487. #[test]
  488. #[should_panic(expected = "Error: out of bounds memory load (insn #2),")]
  489. fn test_vm_err_ldindb_oob() {
  490. let prog = &[
  491. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  492. 0x38, 0x10, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
  493. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  494. ];
  495. let mem = &mut [
  496. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  497. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  498. ];
  499. let vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  500. vm.execute_program(mem).unwrap();
  501. // Memory check not implemented for JIT yet.
  502. }
  503. #[test]
  504. #[should_panic(expected = "Error: out of bounds memory load (insn #2),")]
  505. fn test_vm_err_ldindb_nomem() {
  506. let prog = &[
  507. 0xb7, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  508. 0x38, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  509. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  510. ];
  511. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  512. vm.execute_program().unwrap();
  513. // Memory check not implemented for JIT yet.
  514. }
  515. #[test]
  516. #[should_panic(expected = "Error: No program set, call prog_set() to load one")]
  517. fn test_vm_exec_no_program() {
  518. let vm = rbpf::EbpfVmNoData::new(None).unwrap();
  519. assert_eq!(vm.execute_program().unwrap(), 0xBEE);
  520. }
  521. fn verifier_success(_prog: &[u8]) -> Result<(), Error> {
  522. Ok(())
  523. }
  524. fn verifier_fail(_prog: &[u8]) -> Result<(), Error> {
  525. Err(Error::new(ErrorKind::Other,
  526. "Gaggablaghblagh!"))
  527. }
  528. #[test]
  529. fn test_verifier_success() {
  530. let prog = assemble(
  531. "mov32 r0, 0xBEE
  532. exit",
  533. ).unwrap();
  534. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  535. vm.set_verifier(verifier_success).unwrap();
  536. vm.set_program(&prog).unwrap();
  537. assert_eq!(vm.execute_program().unwrap(), 0xBEE);
  538. }
  539. #[test]
  540. #[should_panic(expected = "Gaggablaghblagh!")]
  541. fn test_verifier_fail() {
  542. let prog = assemble(
  543. "mov32 r0, 0xBEE
  544. exit",
  545. ).unwrap();
  546. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  547. vm.set_verifier(verifier_fail).unwrap();
  548. vm.set_program(&prog).unwrap();
  549. assert_eq!(vm.execute_program().unwrap(), 0xBEE);
  550. }
  551. #[test]
  552. fn test_vm_bpf_to_bpf_call(){
  553. let test_code = assemble(
  554. "
  555. mov64 r1, 0x10
  556. mov64 r2, 0x1
  557. callx 0x4
  558. mov64 r1, 0x1
  559. mov64 r2, r0
  560. callx 0x4
  561. exit
  562. mov64 r0, r1
  563. sub64 r0, r2
  564. exit
  565. mov64 r0, r2
  566. add64 r0, r1
  567. exit
  568. ").unwrap();
  569. let vm = rbpf::EbpfVmNoData::new(Some(&test_code)).unwrap();
  570. let vm_res= vm.execute_program().unwrap();
  571. assert_eq!(vm_res, 0x10);
  572. }
  573. #[cfg(all(not(windows), feature = "std"))]
  574. #[test]
  575. fn test_vm_jit_bpf_to_bpf_call(){
  576. let test_code = assemble(
  577. "
  578. mov64 r1, 0x10
  579. mov64 r2, 0x1
  580. callx 0x4
  581. mov64 r1, 0x1
  582. mov64 r2, r0
  583. callx 0x4
  584. exit
  585. mov64 r0, r1
  586. sub64 r0, r2
  587. exit
  588. mov64 r0, r2
  589. add64 r0, r1
  590. exit").unwrap();
  591. let mut vm = rbpf::EbpfVmNoData::new(Some(&test_code)).unwrap();
  592. vm.jit_compile().unwrap();
  593. let vm_res= unsafe { vm.execute_program_jit().unwrap() };
  594. assert_eq!(vm_res, 0x10);
  595. }
  596. #[test]
  597. #[should_panic(expected = "[Verifier] Error: unsupported call type #2 (insn #0)")]
  598. fn test_verifier_err_other_type_call(){
  599. let prog = &[
  600. 0x85, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  601. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  602. ];
  603. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  604. vm.execute_program().unwrap();
  605. }
  606. #[test]
  607. #[should_panic(expected = "Error: unsupported call type #2 (insn #0)")]
  608. fn test_vm_other_type_call(){
  609. let prog = &[
  610. 0x85, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  611. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  612. ];
  613. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  614. vm.set_verifier(|_|{
  615. Ok(())
  616. }).unwrap();
  617. vm.set_program(prog).unwrap();
  618. vm.execute_program().unwrap();
  619. }
  620. #[cfg(all(not(windows), feature = "std"))]
  621. #[test]
  622. #[should_panic(expected = "[JIT] Error: unexpected call type #2 (insn #0)")]
  623. fn test_vm_jit_other_type_call(){
  624. let prog = &[
  625. 0x85, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  626. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  627. ];
  628. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  629. vm.set_verifier(|_|{
  630. Ok(())
  631. }).unwrap();
  632. vm.set_program(prog).unwrap();
  633. vm.jit_compile().unwrap();
  634. unsafe { vm.execute_program_jit().unwrap() };
  635. }
  636. #[test]
  637. #[should_panic(expected = "Error: out of bounds memory store (insn #8)")]
  638. fn test_stack_overflow(){
  639. // The stdw instruction is used to test the stack overflow.
  640. let test_code = assemble(
  641. "
  642. mov64 r1, 0x10
  643. mov64 r2, 0x1
  644. callx 0x4
  645. mov64 r1, 0x1
  646. mov64 r2, r0
  647. callx 0x5
  648. exit
  649. stdw [r10-8], 0xcd
  650. mov64 r0, r1
  651. sub64 r0, r2
  652. exit
  653. mov64 r0, r2
  654. add64 r0, r1
  655. exit").unwrap();
  656. let mut vm = rbpf::EbpfVmNoData::new(Some(&test_code)).unwrap();
  657. vm.set_stack_usage_calculator(|_,_,_| 512, Box::new(())).unwrap();
  658. vm.execute_program().unwrap();
  659. }