4
0

disassembler.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // Copyright 2017 Rich Lane <lanerl@gmail.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or
  4. // the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be
  5. // copied, modified, or distributed except according to those terms.
  6. extern crate rbpf;
  7. mod common;
  8. use rbpf::assembler::assemble;
  9. use rbpf::disassembler::to_insn_vec;
  10. // Using a macro to keep actual line numbers in failure output
  11. macro_rules! disasm {
  12. ($src:expr) => {
  13. {
  14. let src = $src;
  15. let asm = assemble(src).expect("Can't assemble from string");
  16. let insn = to_insn_vec(&asm);
  17. let reasm = insn.into_iter().map(|ins| ins.desc).collect::<Vec<_>>().join("\n");
  18. assert_eq!(src, reasm);
  19. }
  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!("add64 r1, r2
  113. sub64 r1, r2
  114. mul64 r1, r2
  115. div64 r1, r2
  116. or64 r1, r2
  117. and64 r1, r2
  118. lsh64 r1, r2
  119. rsh64 r1, r2
  120. mod64 r1, r2
  121. xor64 r1, r2
  122. mov64 r1, r2
  123. arsh64 r1, r2");
  124. disasm!("add64 r1, 0x2
  125. sub64 r1, 0x2
  126. mul64 r1, 0x2
  127. div64 r1, 0x2
  128. or64 r1, 0x2
  129. and64 r1, 0x2
  130. lsh64 r1, 0x2
  131. rsh64 r1, 0x2
  132. mod64 r1, 0x2
  133. xor64 r1, 0x2
  134. mov64 r1, 0x2
  135. arsh64 r1, 0x2");
  136. disasm!("add32 r1, r2
  137. sub32 r1, r2
  138. mul32 r1, r2
  139. div32 r1, r2
  140. or32 r1, r2
  141. and32 r1, r2
  142. lsh32 r1, r2
  143. rsh32 r1, r2
  144. mod32 r1, r2
  145. xor32 r1, r2
  146. mov32 r1, r2
  147. arsh32 r1, r2");
  148. disasm!("add32 r1, 0x2
  149. sub32 r1, 0x2
  150. mul32 r1, 0x2
  151. div32 r1, 0x2
  152. or32 r1, 0x2
  153. and32 r1, 0x2
  154. lsh32 r1, 0x2
  155. rsh32 r1, 0x2
  156. mod32 r1, 0x2
  157. xor32 r1, 0x2
  158. mov32 r1, 0x2
  159. arsh32 r1, 0x2");
  160. }
  161. // Test all supported AluUnary mnemonics.
  162. #[test]
  163. fn test_alu_unary() {
  164. disasm!("neg64 r1
  165. neg32 r1");
  166. }
  167. // Test all supported LoadAbs mnemonics.
  168. #[test]
  169. fn test_load_abs() {
  170. disasm!("ldabsw 0x1
  171. ldabsh 0x1
  172. ldabsb 0x1
  173. ldabsdw 0x1");
  174. }
  175. // Test all supported LoadInd mnemonics.
  176. #[test]
  177. fn test_load_ind() {
  178. disasm!("ldindw r1, 0x2
  179. ldindh r1, 0x2
  180. ldindb r1, 0x2
  181. ldinddw r1, 0x2");
  182. }
  183. // Test all supported LoadReg mnemonics.
  184. #[test]
  185. fn test_load_reg() {
  186. disasm!(r"ldxw r1, [r2+0x3]
  187. ldxh r1, [r2+0x3]
  188. ldxb r1, [r2+0x3]
  189. ldxdw r1, [r2+0x3]");
  190. }
  191. // Test all supported StoreImm mnemonics.
  192. #[test]
  193. fn test_store_imm() {
  194. disasm!("stw [r1+0x2], 0x3
  195. sth [r1+0x2], 0x3
  196. stb [r1+0x2], 0x3
  197. stdw [r1+0x2], 0x3");
  198. }
  199. // Test all supported StoreReg mnemonics.
  200. #[test]
  201. fn test_store_reg() {
  202. disasm!("stxw [r1+0x2], r3
  203. stxh [r1+0x2], r3
  204. stxb [r1+0x2], r3
  205. stxdw [r1+0x2], r3");
  206. }
  207. // Test all supported JumpConditional mnemonics.
  208. #[test]
  209. fn test_jump_conditional() {
  210. disasm!("jeq r1, r2, +0x3
  211. jgt r1, r2, +0x3
  212. jge r1, r2, +0x3
  213. jset r1, r2, +0x3
  214. jne r1, r2, +0x3
  215. jsgt r1, r2, +0x3
  216. jsge r1, r2, +0x3");
  217. disasm!("jeq r1, 0x2, +0x3
  218. jgt r1, 0x2, +0x3
  219. jge r1, 0x2, +0x3
  220. jset r1, 0x2, +0x3
  221. jne r1, 0x2, +0x3
  222. jsgt r1, 0x2, +0x3
  223. jsge r1, 0x2, +0x3");
  224. }
  225. // Test all supported Endian mnemonics.
  226. #[test]
  227. fn test_endian() {
  228. disasm!("be16 r1
  229. be32 r1
  230. be64 r1
  231. le16 r1
  232. le32 r1
  233. le64 r1");
  234. }
  235. #[test]
  236. fn test_large_immediate() {
  237. disasm!("add64 r1, 0x7fffffff");
  238. disasm!("add64 r1, 0x7fffffff");
  239. }