disassembler.rs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // SPDX-License-Identifier: (Apache-2.0 OR MIT)
  2. // Copyright 2017 Jan-Erik Rediger <[email protected]>
  3. //
  4. // Adopted from tests in `tests/assembler.rs`
  5. extern crate rbpf;
  6. mod common;
  7. use rbpf::{assembler::assemble, disassembler::to_insn_vec};
  8. // Using a macro to keep actual line numbers in failure output
  9. macro_rules! disasm {
  10. ($src:expr) => {{
  11. let src = $src;
  12. let asm = assemble(src).expect("Can't assemble from string");
  13. let insn = to_insn_vec(&asm);
  14. let reasm = insn
  15. .into_iter()
  16. .map(|ins| ins.desc)
  17. .collect::<Vec<_>>()
  18. .join("\n");
  19. assert_eq!(src, reasm);
  20. }};
  21. }
  22. #[test]
  23. fn test_empty() {
  24. disasm!("");
  25. }
  26. // Example for InstructionType::NoOperand.
  27. #[test]
  28. fn test_exit() {
  29. disasm!("exit");
  30. }
  31. // Example for InstructionType::AluBinary.
  32. #[test]
  33. fn test_add64() {
  34. disasm!("add64 r1, r3");
  35. disasm!("add64 r1, 0x5");
  36. }
  37. // Example for InstructionType::AluUnary.
  38. #[test]
  39. fn test_neg64() {
  40. disasm!("neg64 r1");
  41. }
  42. // Example for InstructionType::LoadReg.
  43. #[test]
  44. fn test_ldxw() {
  45. disasm!("ldxw r1, [r2+0x5]");
  46. }
  47. // Example for InstructionType::StoreImm.
  48. #[test]
  49. fn test_stw() {
  50. disasm!("stw [r2+0x5], 0x7");
  51. }
  52. // Example for InstructionType::StoreReg.
  53. #[test]
  54. fn test_stxw() {
  55. disasm!("stxw [r2+0x5], r8");
  56. }
  57. // Example for InstructionType::JumpUnconditional.
  58. #[test]
  59. fn test_ja() {
  60. disasm!("ja +0x8");
  61. }
  62. // Example for InstructionType::JumpConditional.
  63. #[test]
  64. fn test_jeq() {
  65. disasm!("jeq r1, 0x4, +0x8");
  66. disasm!("jeq r1, r3, +0x8");
  67. }
  68. // Example for InstructionType::Call.
  69. #[test]
  70. fn test_call() {
  71. disasm!("call 0x3");
  72. }
  73. // Example for InstructionType::Endian.
  74. #[test]
  75. fn test_be32() {
  76. disasm!("be32 r1");
  77. }
  78. // Example for InstructionType::LoadImm.
  79. #[test]
  80. fn test_lddw() {
  81. disasm!("lddw r1, 0x1234abcd5678eeff");
  82. disasm!("lddw r1, 0xff11ee22dd33cc44");
  83. }
  84. // Example for InstructionType::LoadAbs.
  85. #[test]
  86. fn test_ldabsw() {
  87. disasm!("ldabsw 0x1");
  88. }
  89. // Example for InstructionType::LoadInd.
  90. #[test]
  91. fn test_ldindw() {
  92. disasm!("ldindw r1, 0x2");
  93. }
  94. // Example for InstructionType::LoadReg.
  95. #[test]
  96. fn test_ldxdw() {
  97. disasm!("ldxdw r1, [r2+0x3]");
  98. }
  99. // Example for InstructionType::StoreImm.
  100. #[test]
  101. fn test_sth() {
  102. disasm!("sth [r1+0x2], 0x3");
  103. }
  104. // Example for InstructionType::StoreReg.
  105. #[test]
  106. fn test_stxh() {
  107. disasm!("stxh [r1+0x2], r3");
  108. }
  109. // Test all supported AluBinary mnemonics.
  110. #[test]
  111. fn test_alu_binary() {
  112. disasm!(
  113. "add64 r1, r2
  114. sub64 r1, r2
  115. mul64 r1, r2
  116. div64 r1, r2
  117. or64 r1, r2
  118. and64 r1, r2
  119. lsh64 r1, r2
  120. rsh64 r1, r2
  121. mod64 r1, r2
  122. xor64 r1, r2
  123. mov64 r1, r2
  124. arsh64 r1, r2"
  125. );
  126. disasm!(
  127. "add64 r1, 0x2
  128. sub64 r1, 0x2
  129. mul64 r1, 0x2
  130. div64 r1, 0x2
  131. or64 r1, 0x2
  132. and64 r1, 0x2
  133. lsh64 r1, 0x2
  134. rsh64 r1, 0x2
  135. mod64 r1, 0x2
  136. xor64 r1, 0x2
  137. mov64 r1, 0x2
  138. arsh64 r1, 0x2"
  139. );
  140. disasm!(
  141. "add32 r1, r2
  142. sub32 r1, r2
  143. mul32 r1, r2
  144. div32 r1, r2
  145. or32 r1, r2
  146. and32 r1, r2
  147. lsh32 r1, r2
  148. rsh32 r1, r2
  149. mod32 r1, r2
  150. xor32 r1, r2
  151. mov32 r1, r2
  152. arsh32 r1, r2"
  153. );
  154. disasm!(
  155. "add32 r1, 0x2
  156. sub32 r1, 0x2
  157. mul32 r1, 0x2
  158. div32 r1, 0x2
  159. or32 r1, 0x2
  160. and32 r1, 0x2
  161. lsh32 r1, 0x2
  162. rsh32 r1, 0x2
  163. mod32 r1, 0x2
  164. xor32 r1, 0x2
  165. mov32 r1, 0x2
  166. arsh32 r1, 0x2"
  167. );
  168. }
  169. // Test all supported AluUnary mnemonics.
  170. #[test]
  171. fn test_alu_unary() {
  172. disasm!(
  173. "neg64 r1
  174. neg32 r1"
  175. );
  176. }
  177. // Test all supported LoadAbs mnemonics.
  178. #[test]
  179. fn test_load_abs() {
  180. disasm!(
  181. "ldabsw 0x1
  182. ldabsh 0x1
  183. ldabsb 0x1
  184. ldabsdw 0x1"
  185. );
  186. }
  187. // Test all supported LoadInd mnemonics.
  188. #[test]
  189. fn test_load_ind() {
  190. disasm!(
  191. "ldindw r1, 0x2
  192. ldindh r1, 0x2
  193. ldindb r1, 0x2
  194. ldinddw r1, 0x2"
  195. );
  196. }
  197. // Test all supported LoadReg mnemonics.
  198. #[test]
  199. fn test_load_reg() {
  200. disasm!(
  201. r"ldxw r1, [r2+0x3]
  202. ldxh r1, [r2+0x3]
  203. ldxb r1, [r2+0x3]
  204. ldxdw r1, [r2+0x3]"
  205. );
  206. }
  207. // Test all supported StoreImm mnemonics.
  208. #[test]
  209. fn test_store_imm() {
  210. disasm!(
  211. "stw [r1+0x2], 0x3
  212. sth [r1+0x2], 0x3
  213. stb [r1+0x2], 0x3
  214. stdw [r1+0x2], 0x3"
  215. );
  216. }
  217. // Test all supported StoreReg mnemonics.
  218. #[test]
  219. fn test_store_reg() {
  220. disasm!(
  221. "stxw [r1+0x2], r3
  222. stxh [r1+0x2], r3
  223. stxb [r1+0x2], r3
  224. stxdw [r1+0x2], r3"
  225. );
  226. }
  227. // Test all supported JumpConditional mnemonics.
  228. #[test]
  229. fn test_jump_conditional() {
  230. disasm!(
  231. "jeq r1, r2, +0x3
  232. jgt r1, r2, +0x3
  233. jge r1, r2, +0x3
  234. jlt r1, r2, +0x3
  235. jle r1, r2, +0x3
  236. jset r1, r2, +0x3
  237. jne r1, r2, +0x3
  238. jsgt r1, r2, +0x3
  239. jsge r1, r2, -0x3
  240. jslt r1, r2, +0x3
  241. jsle r1, r2, -0x3"
  242. );
  243. disasm!(
  244. "jeq r1, 0x2, +0x3
  245. jgt r1, 0x2, +0x3
  246. jge r1, 0x2, +0x3
  247. jlt r1, 0x2, +0x3
  248. jle r1, 0x2, +0x3
  249. jset r1, 0x2, +0x3
  250. jne r1, 0x2, +0x3
  251. jsgt r1, 0x2, +0x3
  252. jsge r1, 0x2, -0x3
  253. jslt r1, 0x2, +0x3
  254. jsle r1, 0x2, -0x3"
  255. );
  256. disasm!(
  257. "jeq32 r1, r2, +0x3
  258. jgt32 r1, r2, +0x3
  259. jge32 r1, r2, +0x3
  260. jlt32 r1, r2, +0x3
  261. jle32 r1, r2, +0x3
  262. jset32 r1, r2, +0x3
  263. jne32 r1, r2, +0x3
  264. jsgt32 r1, r2, +0x3
  265. jsge32 r1, r2, -0x3
  266. jslt32 r1, r2, +0x3
  267. jsle32 r1, r2, -0x3"
  268. );
  269. disasm!(
  270. "jeq32 r1, 0x2, +0x3
  271. jgt32 r1, 0x2, +0x3
  272. jge32 r1, 0x2, +0x3
  273. jlt32 r1, 0x2, +0x3
  274. jle32 r1, 0x2, +0x3
  275. jset32 r1, 0x2, +0x3
  276. jne32 r1, 0x2, +0x3
  277. jsgt32 r1, 0x2, +0x3
  278. jsge32 r1, 0x2, -0x3
  279. jslt32 r1, 0x2, +0x3
  280. jsle32 r1, 0x2, -0x3"
  281. );
  282. }
  283. // Test all supported Endian mnemonics.
  284. #[test]
  285. fn test_endian() {
  286. disasm!(
  287. "be16 r1
  288. be32 r1
  289. be64 r1
  290. le16 r1
  291. le32 r1
  292. le64 r1"
  293. );
  294. }
  295. #[test]
  296. fn test_large_immediate() {
  297. disasm!("add64 r1, 0x7fffffff");
  298. disasm!("add64 r1, 0x7fffffff");
  299. }
  300. // Non-regression tests for overflow when trying to negate offset 0x8000i16.
  301. #[test]
  302. fn test_offset_overflow() {
  303. let insns = [
  304. 0x62, 0x01, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, // stw
  305. 0x6a, 0x01, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, // sth
  306. 0x72, 0x01, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, // stb
  307. 0x7a, 0x01, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, // stdw
  308. 0x61, 0x01, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, // ldxw
  309. 0x69, 0x01, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, // ldxh
  310. 0x71, 0x01, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, // ldxb
  311. 0x79, 0x01, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, // ldxdw
  312. 0x15, 0x01, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00, // jeq (imm)
  313. 0x1d, 0x21, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, // jeq (reg)
  314. 0x16, 0x01, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00, // jeq32 (imm)
  315. 0x1e, 0x21, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, // jeq32 (reg)
  316. ];
  317. let expected_output = "stw [r1-0x8000], 0x1
  318. sth [r1-0x8000], 0x1
  319. stb [r1-0x8000], 0x1
  320. stdw [r1-0x8000], 0x1
  321. ldxw r1, [r0-0x8000]
  322. ldxh r1, [r0-0x8000]
  323. ldxb r1, [r0-0x8000]
  324. ldxdw r1, [r0-0x8000]
  325. jeq r1, 0x2, -0x8000
  326. jeq r1, r2, -0x8000
  327. jeq32 r1, 0x2, -0x8000
  328. jeq32 r1, r2, -0x8000";
  329. let prog = to_insn_vec(&insns);
  330. let asm = prog
  331. .into_iter()
  332. .map(|ins| ins.desc)
  333. .collect::<Vec<_>>()
  334. .join("\n");
  335. assert_eq!(asm, expected_output);
  336. }