4
0

disassembler.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Copyright 2017 Jan-Erik Rediger <badboy@archlinux.us>
  2. //
  3. // Adopted from tests in `tests/assembler.rs`
  4. //
  5. // Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or
  6. // the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be
  7. // copied, modified, or distributed except according to those terms.
  8. extern crate rbpf;
  9. mod common;
  10. use rbpf::assembler::assemble;
  11. use rbpf::disassembler::to_insn_vec;
  12. // Using a macro to keep actual line numbers in failure output
  13. macro_rules! disasm {
  14. ($src:expr) => {
  15. {
  16. let src = $src;
  17. let asm = assemble(src).expect("Can't assemble from string");
  18. let insn = to_insn_vec(&asm);
  19. let reasm = insn.into_iter().map(|ins| ins.desc).collect::<Vec<_>>().join("\n");
  20. assert_eq!(src, reasm);
  21. }
  22. }
  23. }
  24. #[test]
  25. fn test_empty() {
  26. disasm!("");
  27. }
  28. // Example for InstructionType::NoOperand.
  29. #[test]
  30. fn test_exit() {
  31. disasm!("exit");
  32. }
  33. // Example for InstructionType::AluBinary.
  34. #[test]
  35. fn test_add64() {
  36. disasm!("add64 r1, r3");
  37. disasm!("add64 r1, 0x5");
  38. }
  39. // Example for InstructionType::AluUnary.
  40. #[test]
  41. fn test_neg64() {
  42. disasm!("neg64 r1");
  43. }
  44. // Example for InstructionType::LoadReg.
  45. #[test]
  46. fn test_ldxw() {
  47. disasm!("ldxw r1, [r2+0x5]");
  48. }
  49. // Example for InstructionType::StoreImm.
  50. #[test]
  51. fn test_stw() {
  52. disasm!("stw [r2+0x5], 0x7");
  53. }
  54. // Example for InstructionType::StoreReg.
  55. #[test]
  56. fn test_stxw() {
  57. disasm!("stxw [r2+0x5], r8");
  58. }
  59. // Example for InstructionType::JumpUnconditional.
  60. #[test]
  61. fn test_ja() {
  62. disasm!("ja +0x8");
  63. }
  64. // Example for InstructionType::JumpConditional.
  65. #[test]
  66. fn test_jeq() {
  67. disasm!("jeq r1, 0x4, +0x8");
  68. disasm!("jeq r1, r3, +0x8");
  69. }
  70. // Example for InstructionType::Call.
  71. #[test]
  72. fn test_call() {
  73. disasm!("call 0x3");
  74. }
  75. // Example for InstructionType::Endian.
  76. #[test]
  77. fn test_be32() {
  78. disasm!("be32 r1");
  79. }
  80. // Example for InstructionType::LoadImm.
  81. #[test]
  82. fn test_lddw() {
  83. disasm!("lddw r1, 0x1234abcd5678eeff");
  84. disasm!("lddw r1, 0xff11ee22dd33cc44");
  85. }
  86. // Example for InstructionType::LoadAbs.
  87. #[test]
  88. fn test_ldabsw() {
  89. disasm!("ldabsw 0x1");
  90. }
  91. // Example for InstructionType::LoadInd.
  92. #[test]
  93. fn test_ldindw() {
  94. disasm!("ldindw r1, 0x2");
  95. }
  96. // Example for InstructionType::LoadReg.
  97. #[test]
  98. fn test_ldxdw() {
  99. disasm!("ldxdw r1, [r2+0x3]");
  100. }
  101. // Example for InstructionType::StoreImm.
  102. #[test]
  103. fn test_sth() {
  104. disasm!("sth [r1+0x2], 0x3");
  105. }
  106. // Example for InstructionType::StoreReg.
  107. #[test]
  108. fn test_stxh() {
  109. disasm!("stxh [r1+0x2], r3");
  110. }
  111. // Test all supported AluBinary mnemonics.
  112. #[test]
  113. fn test_alu_binary() {
  114. disasm!("add64 r1, r2
  115. sub64 r1, r2
  116. mul64 r1, r2
  117. div64 r1, r2
  118. or64 r1, r2
  119. and64 r1, r2
  120. lsh64 r1, r2
  121. rsh64 r1, r2
  122. mod64 r1, r2
  123. xor64 r1, r2
  124. mov64 r1, r2
  125. arsh64 r1, r2");
  126. disasm!("add64 r1, 0x2
  127. sub64 r1, 0x2
  128. mul64 r1, 0x2
  129. div64 r1, 0x2
  130. or64 r1, 0x2
  131. and64 r1, 0x2
  132. lsh64 r1, 0x2
  133. rsh64 r1, 0x2
  134. mod64 r1, 0x2
  135. xor64 r1, 0x2
  136. mov64 r1, 0x2
  137. arsh64 r1, 0x2");
  138. disasm!("add32 r1, r2
  139. sub32 r1, r2
  140. mul32 r1, r2
  141. div32 r1, r2
  142. or32 r1, r2
  143. and32 r1, r2
  144. lsh32 r1, r2
  145. rsh32 r1, r2
  146. mod32 r1, r2
  147. xor32 r1, r2
  148. mov32 r1, r2
  149. arsh32 r1, r2");
  150. disasm!("add32 r1, 0x2
  151. sub32 r1, 0x2
  152. mul32 r1, 0x2
  153. div32 r1, 0x2
  154. or32 r1, 0x2
  155. and32 r1, 0x2
  156. lsh32 r1, 0x2
  157. rsh32 r1, 0x2
  158. mod32 r1, 0x2
  159. xor32 r1, 0x2
  160. mov32 r1, 0x2
  161. arsh32 r1, 0x2");
  162. }
  163. // Test all supported AluUnary mnemonics.
  164. #[test]
  165. fn test_alu_unary() {
  166. disasm!("neg64 r1
  167. neg32 r1");
  168. }
  169. // Test all supported LoadAbs mnemonics.
  170. #[test]
  171. fn test_load_abs() {
  172. disasm!("ldabsw 0x1
  173. ldabsh 0x1
  174. ldabsb 0x1
  175. ldabsdw 0x1");
  176. }
  177. // Test all supported LoadInd mnemonics.
  178. #[test]
  179. fn test_load_ind() {
  180. disasm!("ldindw r1, 0x2
  181. ldindh r1, 0x2
  182. ldindb r1, 0x2
  183. ldinddw r1, 0x2");
  184. }
  185. // Test all supported LoadReg mnemonics.
  186. #[test]
  187. fn test_load_reg() {
  188. disasm!(r"ldxw r1, [r2+0x3]
  189. ldxh r1, [r2+0x3]
  190. ldxb r1, [r2+0x3]
  191. ldxdw r1, [r2+0x3]");
  192. }
  193. // Test all supported StoreImm mnemonics.
  194. #[test]
  195. fn test_store_imm() {
  196. disasm!("stw [r1+0x2], 0x3
  197. sth [r1+0x2], 0x3
  198. stb [r1+0x2], 0x3
  199. stdw [r1+0x2], 0x3");
  200. }
  201. // Test all supported StoreReg mnemonics.
  202. #[test]
  203. fn test_store_reg() {
  204. disasm!("stxw [r1+0x2], r3
  205. stxh [r1+0x2], r3
  206. stxb [r1+0x2], r3
  207. stxdw [r1+0x2], r3");
  208. }
  209. // Test all supported JumpConditional mnemonics.
  210. #[test]
  211. fn test_jump_conditional() {
  212. disasm!("jeq r1, r2, +0x3
  213. jgt r1, r2, +0x3
  214. jge r1, r2, +0x3
  215. jset r1, r2, +0x3
  216. jne r1, r2, +0x3
  217. jsgt r1, r2, +0x3
  218. jsge r1, r2, +0x3");
  219. disasm!("jeq r1, 0x2, +0x3
  220. jgt r1, 0x2, +0x3
  221. jge r1, 0x2, +0x3
  222. jset r1, 0x2, +0x3
  223. jne r1, 0x2, +0x3
  224. jsgt r1, 0x2, +0x3
  225. jsge r1, 0x2, +0x3");
  226. }
  227. // Test all supported Endian mnemonics.
  228. #[test]
  229. fn test_endian() {
  230. disasm!("be16 r1
  231. be32 r1
  232. be64 r1
  233. le16 r1
  234. le32 r1
  235. le64 r1");
  236. }
  237. #[test]
  238. fn test_large_immediate() {
  239. disasm!("add64 r1, 0x7fffffff");
  240. disasm!("add64 r1, 0x7fffffff");
  241. }