4
0

misc.rs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. #[cfg(all(not(windows),not(feature="std")))]
  85. fn alloc_exec_memory() -> Box<[u8]> {
  86. let size = 4096;
  87. let layout = std::alloc::Layout::from_size_align(size, 4096).unwrap();
  88. unsafe {
  89. let ptr = std::alloc::alloc(layout);
  90. assert!(!ptr.is_null(), "Failed to allocate memory");
  91. libc::mprotect(ptr.cast(), size, libc::PROT_EXEC | libc::PROT_WRITE);
  92. let slice = std::slice::from_raw_parts_mut(ptr, size);
  93. Box::from_raw(slice)
  94. }
  95. }
  96. #[test]
  97. #[cfg(feature = "std")]
  98. fn test_vm_block_port() {
  99. // To load the bytecode from an object file instead of using the hardcoded instructions,
  100. // use the additional crates commented at the beginning of this file (and also add them to your
  101. // Cargo.toml). See comments above.
  102. //
  103. // ---
  104. // let filename = "my_ebpf_object_file.o";
  105. //
  106. // let path = PathBuf::from(filename);
  107. // let file = match elf::File::open_path(&path) {
  108. // Ok(f) => f,
  109. // Err(e) => panic!("Error: {:?}", e),
  110. // };
  111. //
  112. // let text_scn = match file.get_section(".classifier") {
  113. // Some(s) => s,
  114. // None => panic!("Failed to look up .classifier section"),
  115. // };
  116. //
  117. // let prog = &text_scn.data;
  118. // ---
  119. #[rustfmt::skip]
  120. let prog = &[
  121. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  122. 0x79, 0x12, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  123. 0x79, 0x11, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61, 0x40 i.o. 0x4c
  124. 0xbf, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  125. 0x07, 0x03, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00,
  126. 0x2d, 0x23, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00,
  127. 0x69, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
  128. 0x55, 0x02, 0x10, 0x00, 0x08, 0x00, 0x00, 0x00,
  129. 0x71, 0x12, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00,
  130. 0x55, 0x02, 0x0e, 0x00, 0x06, 0x00, 0x00, 0x00,
  131. 0x18, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  132. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  133. 0x79, 0x11, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  134. 0xbf, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  135. 0x57, 0x02, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
  136. 0x15, 0x02, 0x08, 0x00, 0x99, 0x99, 0x00, 0x00,
  137. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
  138. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  139. 0x5f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  140. 0xb7, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  141. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0x99, 0x99,
  142. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  143. 0x1d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  144. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  145. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  146. ];
  147. #[rustfmt::skip]
  148. let packet = &mut [
  149. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
  150. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
  151. 0x08, 0x00, // ethertype
  152. 0x45, 0x00, 0x00, 0x3b, // start ip_hdr
  153. 0xa6, 0xab, 0x40, 0x00,
  154. 0x40, 0x06, 0x96, 0x0f,
  155. 0x7f, 0x00, 0x00, 0x01,
  156. 0x7f, 0x00, 0x00, 0x01,
  157. // Program matches the next two bytes: 0x9999 returns 0xffffffff, else return 0.
  158. 0x99, 0x99, 0xc6, 0xcc, // start tcp_hdr
  159. 0xd1, 0xe5, 0xc4, 0x9d,
  160. 0xd4, 0x30, 0xb5, 0xd2,
  161. 0x80, 0x18, 0x01, 0x56,
  162. 0xfe, 0x2f, 0x00, 0x00,
  163. 0x01, 0x01, 0x08, 0x0a, // start data
  164. 0x00, 0x23, 0x75, 0x89,
  165. 0x00, 0x23, 0x63, 0x2d,
  166. 0x71, 0x64, 0x66, 0x73,
  167. 0x64, 0x66, 0x0au8
  168. ];
  169. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x40, 0x50).unwrap();
  170. vm.register_helper(helpers::BPF_TRACE_PRINTK_IDX, helpers::bpf_trace_printf)
  171. .unwrap();
  172. let res = vm.execute_program(packet).unwrap();
  173. println!("Program returned: {res:?} ({res:#x})");
  174. assert_eq!(res, 0xffffffff);
  175. }
  176. #[test]
  177. #[cfg(all(not(windows), feature = "std"))]
  178. fn test_jit_block_port() {
  179. // To load the bytecode from an object file instead of using the hardcoded instructions,
  180. // use the additional crates commented at the beginning of this file (and also add them to your
  181. // Cargo.toml). See comments above.
  182. //
  183. // ---
  184. // let filename = "my_ebpf_object_file.o";
  185. //
  186. // let path = PathBuf::from(filename);
  187. // let file = match elf::File::open_path(&path) {
  188. // Ok(f) => f,
  189. // Err(e) => panic!("Error: {:?}", e),
  190. // };
  191. //
  192. // let text_scn = match file.get_section(".classifier") {
  193. // Some(s) => s,
  194. // None => panic!("Failed to look up .classifier section"),
  195. // };
  196. //
  197. // let prog = &text_scn.data;
  198. // ---
  199. #[rustfmt::skip]
  200. let prog = &[
  201. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  202. 0x79, 0x12, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  203. 0x79, 0x11, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61, 0x40 i.o. 0x4c
  204. 0xbf, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  205. 0x07, 0x03, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00,
  206. 0x2d, 0x23, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00,
  207. 0x69, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
  208. 0x55, 0x02, 0x10, 0x00, 0x08, 0x00, 0x00, 0x00,
  209. 0x71, 0x12, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00,
  210. 0x55, 0x02, 0x0e, 0x00, 0x06, 0x00, 0x00, 0x00,
  211. 0x18, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  212. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  213. 0x79, 0x11, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79 instead of 0x61
  214. 0xbf, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  215. 0x57, 0x02, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
  216. 0x15, 0x02, 0x08, 0x00, 0x99, 0x99, 0x00, 0x00,
  217. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
  218. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  219. 0x5f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  220. 0xb7, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  221. 0x18, 0x02, 0x00, 0x00, 0x00, 0x00, 0x99, 0x99,
  222. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  223. 0x1d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  224. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  225. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  226. ];
  227. #[rustfmt::skip]
  228. let packet = &mut [
  229. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
  230. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
  231. 0x08, 0x00, // ethertype
  232. 0x45, 0x00, 0x00, 0x3b, // start ip_hdr
  233. 0xa6, 0xab, 0x40, 0x00,
  234. 0x40, 0x06, 0x96, 0x0f,
  235. 0x7f, 0x00, 0x00, 0x01,
  236. 0x7f, 0x00, 0x00, 0x01,
  237. // Program matches the next two bytes: 0x9999 returns 0xffffffff, else return 0.
  238. 0x99, 0x99, 0xc6, 0xcc, // start tcp_hdr
  239. 0xd1, 0xe5, 0xc4, 0x9d,
  240. 0xd4, 0x30, 0xb5, 0xd2,
  241. 0x80, 0x18, 0x01, 0x56,
  242. 0xfe, 0x2f, 0x00, 0x00,
  243. 0x01, 0x01, 0x08, 0x0a, // start data
  244. 0x00, 0x23, 0x75, 0x89,
  245. 0x00, 0x23, 0x63, 0x2d,
  246. 0x71, 0x64, 0x66, 0x73,
  247. 0x64, 0x66, 0x0au8
  248. ];
  249. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x40, 0x50).unwrap();
  250. vm.register_helper(helpers::BPF_TRACE_PRINTK_IDX, helpers::bpf_trace_printf)
  251. .unwrap();
  252. vm.jit_compile().unwrap();
  253. unsafe {
  254. let res = vm.execute_program_jit(packet).unwrap();
  255. println!("Program returned: {res:?} ({res:#x})");
  256. assert_eq!(res, 0xffffffff);
  257. }
  258. }
  259. // Program and memory come from uBPF test ldxh.
  260. #[test]
  261. fn test_vm_mbuff() {
  262. #[rustfmt::skip]
  263. let prog = &[
  264. // Load mem from mbuff into R1
  265. 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  266. // ldhx r1[2], r0
  267. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  268. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  269. ];
  270. let mem = &[0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd];
  271. let mbuff = [0u8; 32];
  272. unsafe {
  273. let data = mbuff.as_ptr().offset(8) as *mut u64;
  274. let data_end = mbuff.as_ptr().offset(24) as *mut u64;
  275. data.write_unaligned(mem.as_ptr() as u64);
  276. data_end.write_unaligned(mem.as_ptr() as u64 + mem.len() as u64);
  277. }
  278. let vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
  279. assert_eq!(vm.execute_program(mem, &mbuff).unwrap(), 0x2211);
  280. }
  281. // Program and memory come from uBPF test ldxh.
  282. #[test]
  283. fn test_vm_mbuff_with_rust_api() {
  284. use rbpf::insn_builder::*;
  285. let mut program = BpfCode::new();
  286. program
  287. .load_x(MemSize::DoubleWord)
  288. .set_dst(0x01)
  289. .set_src(0x01)
  290. .set_off(0x00_08)
  291. .push()
  292. .load_x(MemSize::HalfWord)
  293. .set_dst(0x00)
  294. .set_src(0x01)
  295. .set_off(0x00_02)
  296. .push()
  297. .exit()
  298. .push();
  299. let mem = &[0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd];
  300. let 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. let vm = rbpf::EbpfVmMbuff::new(Some(program.into_bytes())).unwrap();
  308. assert_eq!(vm.execute_program(mem, &mbuff).unwrap(), 0x2211);
  309. }
  310. // Program and memory come from uBPF test ldxh.
  311. #[test]
  312. #[cfg(not(windows))]
  313. fn test_jit_mbuff() {
  314. #[rustfmt::skip]
  315. let prog = &[
  316. // Load mem from mbuff into R1
  317. 0x79, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  318. // ldhx r1[2], r0
  319. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  320. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  321. ];
  322. let mem = &mut [0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd];
  323. let mut mbuff = [0u8; 32];
  324. unsafe {
  325. let data = mbuff.as_ptr().offset(8) as *mut u64;
  326. let data_end = mbuff.as_ptr().offset(24) as *mut u64;
  327. *data = mem.as_ptr() as u64;
  328. *data_end = mem.as_ptr() as u64 + mem.len() as u64;
  329. }
  330. unsafe {
  331. let mut vm = rbpf::EbpfVmMbuff::new(Some(prog)).unwrap();
  332. #[cfg(not(feature = "std"))]
  333. let mut exec_mem = alloc_exec_memory();
  334. #[cfg(not(feature = "std"))]
  335. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  336. vm.jit_compile().unwrap();
  337. assert_eq!(vm.execute_program_jit(mem, &mut mbuff).unwrap(), 0x2211);
  338. }
  339. }
  340. #[cfg(not(windows))]
  341. #[test]
  342. fn test_vm_jit_ldabsb() {
  343. #[rustfmt::skip]
  344. let prog = &[
  345. 0x30, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  346. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  347. ];
  348. #[rustfmt::skip]
  349. let mem = &mut [
  350. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  351. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  352. ];
  353. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  354. assert_eq!(vm.execute_program(mem).unwrap(), 0x33);
  355. #[cfg(not(feature = "std"))]
  356. let mut exec_mem = alloc_exec_memory();
  357. #[cfg(not(feature = "std"))]
  358. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  359. vm.jit_compile().unwrap();
  360. unsafe {
  361. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x33);
  362. };
  363. }
  364. #[cfg(not(windows))]
  365. #[test]
  366. fn test_vm_jit_ldabsh() {
  367. #[rustfmt::skip]
  368. let prog = &[
  369. 0x28, 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(), 0x4433);
  379. #[cfg(not(feature = "std"))]
  380. let mut exec_mem = alloc_exec_memory();
  381. #[cfg(not(feature = "std"))]
  382. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  383. vm.jit_compile().unwrap();
  384. unsafe {
  385. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x4433);
  386. };
  387. }
  388. #[cfg(not(windows))]
  389. #[test]
  390. fn test_vm_jit_ldabsw() {
  391. #[rustfmt::skip]
  392. let prog = &[
  393. 0x20, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  394. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  395. ];
  396. #[rustfmt::skip]
  397. let mem = &mut [
  398. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  399. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  400. ];
  401. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  402. assert_eq!(vm.execute_program(mem).unwrap(), 0x66554433);
  403. #[cfg(not(feature = "std"))]
  404. let mut exec_mem = alloc_exec_memory();
  405. #[cfg(not(feature = "std"))]
  406. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  407. vm.jit_compile().unwrap();
  408. unsafe {
  409. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x66554433);
  410. };
  411. }
  412. #[cfg(not(windows))]
  413. #[test]
  414. fn test_vm_jit_ldabsdw() {
  415. #[rustfmt::skip]
  416. let prog = &[
  417. 0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  418. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  419. ];
  420. #[rustfmt::skip]
  421. let mem = &mut [
  422. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  423. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  424. ];
  425. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  426. assert_eq!(vm.execute_program(mem).unwrap(), 0xaa99887766554433);
  427. #[cfg(not(feature = "std"))]
  428. let mut exec_mem = alloc_exec_memory();
  429. #[cfg(not(feature = "std"))]
  430. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  431. vm.jit_compile().unwrap();
  432. unsafe {
  433. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0xaa99887766554433);
  434. };
  435. }
  436. #[test]
  437. #[should_panic(expected = "Error: out of bounds memory load (insn #1),")]
  438. fn test_vm_err_ldabsb_oob() {
  439. #[rustfmt::skip]
  440. let prog = &[
  441. 0x38, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
  442. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  443. ];
  444. #[rustfmt::skip]
  445. let mem = &mut [
  446. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  447. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  448. ];
  449. let vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  450. vm.execute_program(mem).unwrap();
  451. // Memory check not implemented for JIT yet.
  452. }
  453. #[test]
  454. #[should_panic(expected = "Error: out of bounds memory load (insn #1),")]
  455. fn test_vm_err_ldabsb_nomem() {
  456. #[rustfmt::skip]
  457. let prog = &[
  458. 0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  459. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  460. ];
  461. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  462. vm.execute_program().unwrap();
  463. // Memory check not implemented for JIT yet.
  464. }
  465. #[cfg(not(windows))]
  466. #[test]
  467. fn test_vm_jit_ldindb() {
  468. #[rustfmt::skip]
  469. let prog = &[
  470. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  471. 0x50, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  472. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  473. ];
  474. #[rustfmt::skip]
  475. let mem = &mut [
  476. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  477. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  478. ];
  479. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  480. assert_eq!(vm.execute_program(mem).unwrap(), 0x88);
  481. #[cfg(not(feature = "std"))]
  482. let mut exec_mem = alloc_exec_memory();
  483. #[cfg(not(feature = "std"))]
  484. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  485. vm.jit_compile().unwrap();
  486. unsafe {
  487. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x88);
  488. };
  489. }
  490. #[cfg(not(windows))]
  491. #[test]
  492. fn test_vm_jit_ldindh() {
  493. #[rustfmt::skip]
  494. let prog = &[
  495. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  496. 0x48, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  497. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  498. ];
  499. #[rustfmt::skip]
  500. let mem = &mut [
  501. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  502. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  503. ];
  504. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  505. assert_eq!(vm.execute_program(mem).unwrap(), 0x9988);
  506. #[cfg(not(feature = "std"))]
  507. let mut exec_mem = alloc_exec_memory();
  508. #[cfg(not(feature = "std"))]
  509. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  510. vm.jit_compile().unwrap();
  511. unsafe {
  512. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x9988);
  513. };
  514. }
  515. #[cfg(not(windows))]
  516. #[test]
  517. fn test_vm_jit_ldindw() {
  518. #[rustfmt::skip]
  519. let prog = &[
  520. 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  521. 0x40, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  522. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  523. ];
  524. #[rustfmt::skip]
  525. let mem = &mut [
  526. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  527. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  528. ];
  529. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  530. assert_eq!(vm.execute_program(mem).unwrap(), 0x88776655);
  531. #[cfg(not(feature = "std"))]
  532. let mut exec_mem = alloc_exec_memory();
  533. #[cfg(not(feature = "std"))]
  534. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  535. vm.jit_compile().unwrap();
  536. unsafe {
  537. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x88776655);
  538. };
  539. }
  540. #[cfg(not(windows))]
  541. #[test]
  542. fn test_vm_jit_ldinddw() {
  543. #[rustfmt::skip]
  544. let prog = &[
  545. 0xb7, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  546. 0x58, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  547. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  548. ];
  549. #[rustfmt::skip]
  550. let mem = &mut [
  551. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  552. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  553. ];
  554. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  555. assert_eq!(vm.execute_program(mem).unwrap(), 0xccbbaa9988776655);
  556. #[cfg(not(feature = "std"))]
  557. let mut exec_mem = alloc_exec_memory();
  558. #[cfg(not(feature = "std"))]
  559. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  560. vm.jit_compile().unwrap();
  561. unsafe {
  562. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0xccbbaa9988776655);
  563. };
  564. }
  565. #[test]
  566. #[should_panic(expected = "Error: out of bounds memory load (insn #2),")]
  567. fn test_vm_err_ldindb_oob() {
  568. #[rustfmt::skip]
  569. let prog = &[
  570. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  571. 0x38, 0x10, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
  572. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  573. ];
  574. #[rustfmt::skip]
  575. let mem = &mut [
  576. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  577. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  578. ];
  579. let vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  580. vm.execute_program(mem).unwrap();
  581. // Memory check not implemented for JIT yet.
  582. }
  583. #[test]
  584. #[should_panic(expected = "Error: out of bounds memory load (insn #2),")]
  585. fn test_vm_err_ldindb_nomem() {
  586. #[rustfmt::skip]
  587. let prog = &[
  588. 0xb7, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  589. 0x38, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  590. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  591. ];
  592. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  593. vm.execute_program().unwrap();
  594. // Memory check not implemented for JIT yet.
  595. }
  596. #[test]
  597. #[should_panic(expected = "Error: No program set, call prog_set() to load one")]
  598. fn test_vm_exec_no_program() {
  599. let vm = rbpf::EbpfVmNoData::new(None).unwrap();
  600. assert_eq!(vm.execute_program().unwrap(), 0xBEE);
  601. }
  602. fn verifier_success(_prog: &[u8]) -> Result<(), Error> {
  603. Ok(())
  604. }
  605. fn verifier_fail(_prog: &[u8]) -> Result<(), Error> {
  606. Err(Error::new(ErrorKind::Other, "Gaggablaghblagh!"))
  607. }
  608. #[test]
  609. fn test_verifier_success() {
  610. let prog = assemble(
  611. "mov32 r0, 0xBEE
  612. exit",
  613. )
  614. .unwrap();
  615. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  616. vm.set_verifier(verifier_success).unwrap();
  617. vm.set_program(&prog).unwrap();
  618. assert_eq!(vm.execute_program().unwrap(), 0xBEE);
  619. }
  620. #[test]
  621. #[should_panic(expected = "Gaggablaghblagh!")]
  622. fn test_verifier_fail() {
  623. let prog = assemble(
  624. "mov32 r0, 0xBEE
  625. exit",
  626. )
  627. .unwrap();
  628. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  629. vm.set_verifier(verifier_fail).unwrap();
  630. vm.set_program(&prog).unwrap();
  631. assert_eq!(vm.execute_program().unwrap(), 0xBEE);
  632. }
  633. #[test]
  634. fn test_vm_bpf_to_bpf_call() {
  635. let test_code = assemble(
  636. "
  637. mov64 r1, 0x10
  638. mov64 r2, 0x1
  639. callx 0x4
  640. mov64 r1, 0x1
  641. mov64 r2, r0
  642. callx 0x4
  643. exit
  644. mov64 r0, r1
  645. sub64 r0, r2
  646. exit
  647. mov64 r0, r2
  648. add64 r0, r1
  649. exit
  650. ",
  651. )
  652. .unwrap();
  653. let vm = rbpf::EbpfVmNoData::new(Some(&test_code)).unwrap();
  654. let vm_res = vm.execute_program().unwrap();
  655. assert_eq!(vm_res, 0x10);
  656. }
  657. #[cfg(not(windows))]
  658. #[test]
  659. fn test_vm_jit_bpf_to_bpf_call() {
  660. let test_code = assemble(
  661. "
  662. mov64 r1, 0x10
  663. mov64 r2, 0x1
  664. callx 0x4
  665. mov64 r1, 0x1
  666. mov64 r2, r0
  667. callx 0x4
  668. exit
  669. mov64 r0, r1
  670. sub64 r0, r2
  671. exit
  672. mov64 r0, r2
  673. add64 r0, r1
  674. exit
  675. ",
  676. )
  677. .unwrap();
  678. let mut vm = rbpf::EbpfVmNoData::new(Some(&test_code)).unwrap();
  679. #[cfg(not(feature = "std"))]
  680. let mut exec_mem = alloc_exec_memory();
  681. #[cfg(not(feature = "std"))]
  682. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  683. vm.jit_compile().unwrap();
  684. let vm_res = unsafe { vm.execute_program_jit().unwrap() };
  685. assert_eq!(vm_res, 0x10);
  686. }
  687. #[test]
  688. #[should_panic(expected = "[Verifier] Error: unsupported call type #2 (insn #0)")]
  689. fn test_verifier_err_other_type_call() {
  690. #[rustfmt::skip]
  691. let prog = &[
  692. 0x85, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  693. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  694. ];
  695. let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  696. vm.execute_program().unwrap();
  697. }
  698. #[test]
  699. #[should_panic(expected = "Error: unsupported call type #2 (insn #0)")]
  700. fn test_vm_other_type_call() {
  701. #[rustfmt::skip]
  702. let prog = &[
  703. 0x85, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  704. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  705. ];
  706. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  707. vm.set_verifier(|_| Ok(())).unwrap();
  708. vm.set_program(prog).unwrap();
  709. vm.execute_program().unwrap();
  710. }
  711. #[cfg(not(windows))]
  712. #[test]
  713. #[should_panic(expected = "[JIT] Error: unexpected call type #2 (insn #0)")]
  714. fn test_vm_jit_other_type_call() {
  715. #[rustfmt::skip]
  716. let prog = &[
  717. 0x85, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  718. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  719. ];
  720. let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
  721. vm.set_verifier(|_| Ok(())).unwrap();
  722. vm.set_program(prog).unwrap();
  723. #[cfg(not(feature = "std"))]
  724. let mut exec_mem = alloc_exec_memory();
  725. #[cfg(not(feature = "std"))]
  726. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  727. vm.jit_compile().unwrap();
  728. unsafe { vm.execute_program_jit().unwrap() };
  729. }
  730. #[test]
  731. #[should_panic(expected = "Error: out of bounds memory store (insn #8)")]
  732. fn test_stack_overflow() {
  733. // The stdw instruction is used to test the stack overflow.
  734. let test_code = assemble(
  735. "
  736. mov64 r1, 0x10
  737. mov64 r2, 0x1
  738. callx 0x4
  739. mov64 r1, 0x1
  740. mov64 r2, r0
  741. callx 0x5
  742. exit
  743. stdw [r10-8], 0xcd
  744. mov64 r0, r1
  745. sub64 r0, r2
  746. exit
  747. mov64 r0, r2
  748. add64 r0, r1
  749. exit
  750. ",
  751. )
  752. .unwrap();
  753. let mut vm = rbpf::EbpfVmNoData::new(Some(&test_code)).unwrap();
  754. vm.set_stack_usage_calculator(|_, _, _| 512, Box::new(()))
  755. .unwrap();
  756. vm.execute_program().unwrap();
  757. }