assembler.rs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. // SPDX-License-Identifier: (Apache-2.0 OR MIT)
  2. // Copyright 2017 Rich Lane <lanerl@gmail.com>
  3. #![allow(clippy::unreadable_literal)]
  4. extern crate rbpf;
  5. mod common;
  6. use common::{TCP_SACK_ASM, TCP_SACK_BIN};
  7. use rbpf::{assembler::assemble, ebpf};
  8. fn asm(src: &str) -> Result<Vec<ebpf::Insn>, String> {
  9. Ok(ebpf::to_insn_vec(&(assemble(src))?))
  10. }
  11. fn insn(opc: u8, dst: u8, src: u8, off: i16, imm: i32) -> ebpf::Insn {
  12. ebpf::Insn {
  13. opc,
  14. dst,
  15. src,
  16. off,
  17. imm,
  18. }
  19. }
  20. #[test]
  21. fn test_empty() {
  22. assert_eq!(asm(""), Ok(vec![]));
  23. }
  24. // Example for InstructionType::NoOperand.
  25. #[test]
  26. fn test_exit() {
  27. assert_eq!(asm("exit"), Ok(vec![insn(ebpf::EXIT, 0, 0, 0, 0)]));
  28. }
  29. // Example for InstructionType::AluBinary.
  30. #[test]
  31. fn test_add64() {
  32. assert_eq!(
  33. asm("add64 r1, r3"),
  34. Ok(vec![insn(ebpf::ADD64_REG, 1, 3, 0, 0)])
  35. );
  36. assert_eq!(
  37. asm("add64 r1, 5"),
  38. Ok(vec![insn(ebpf::ADD64_IMM, 1, 0, 0, 5)])
  39. );
  40. }
  41. // Example for InstructionType::AluUnary.
  42. #[test]
  43. fn test_neg64() {
  44. assert_eq!(asm("neg64 r1"), Ok(vec![insn(ebpf::NEG64, 1, 0, 0, 0)]));
  45. }
  46. // Example for InstructionType::LoadReg.
  47. #[test]
  48. fn test_ldxw() {
  49. assert_eq!(
  50. asm("ldxw r1, [r2+5]"),
  51. Ok(vec![insn(ebpf::LD_W_REG, 1, 2, 5, 0)])
  52. );
  53. }
  54. // Example for InstructionType::StoreImm.
  55. #[test]
  56. fn test_stw() {
  57. assert_eq!(
  58. asm("stw [r2+5], 7"),
  59. Ok(vec![insn(ebpf::ST_W_IMM, 2, 0, 5, 7)])
  60. );
  61. }
  62. // Example for InstructionType::StoreReg.
  63. #[test]
  64. fn test_stxw() {
  65. assert_eq!(
  66. asm("stxw [r2+5], r8"),
  67. Ok(vec![insn(ebpf::ST_W_REG, 2, 8, 5, 0)])
  68. );
  69. }
  70. // Example for InstructionType::JumpUnconditional.
  71. #[test]
  72. fn test_ja() {
  73. assert_eq!(asm("ja +8"), Ok(vec![insn(ebpf::JA, 0, 0, 8, 0)]));
  74. assert_eq!(asm("ja -3"), Ok(vec![insn(ebpf::JA, 0, 0, -3, 0)]));
  75. }
  76. // Example for InstructionType::JumpConditional.
  77. #[test]
  78. fn test_jeq() {
  79. assert_eq!(
  80. asm("jeq r1, 4, +8"),
  81. Ok(vec![insn(ebpf::JEQ_IMM, 1, 0, 8, 4)])
  82. );
  83. assert_eq!(
  84. asm("jeq r1, r3, +8"),
  85. Ok(vec![insn(ebpf::JEQ_REG, 1, 3, 8, 0)])
  86. );
  87. }
  88. // Example for InstructionType::Call.
  89. #[test]
  90. fn test_call() {
  91. assert_eq!(asm("call 300"), Ok(vec![insn(ebpf::CALL, 0, 0, 0, 300)]));
  92. }
  93. // Example for InstructionType::Endian.
  94. #[test]
  95. fn test_be32() {
  96. assert_eq!(asm("be32 r1"), Ok(vec![insn(ebpf::BE, 1, 0, 0, 32)]));
  97. }
  98. // Example for InstructionType::LoadImm.
  99. #[test]
  100. fn test_lddw() {
  101. assert_eq!(
  102. asm("lddw r1, 0x1234abcd5678eeff"),
  103. Ok(vec![
  104. insn(ebpf::LD_DW_IMM, 1, 0, 0, 0x5678eeff),
  105. insn(0, 0, 0, 0, 0x1234abcd)
  106. ])
  107. );
  108. assert_eq!(
  109. asm("lddw r1, 0xff11ee22dd33cc44"),
  110. Ok(vec![
  111. insn(ebpf::LD_DW_IMM, 1, 0, 0, 0xdd33cc44u32 as i32),
  112. insn(0, 0, 0, 0, 0xff11ee22u32 as i32)
  113. ])
  114. );
  115. }
  116. // Example for InstructionType::LoadAbs.
  117. #[test]
  118. fn test_ldabsw() {
  119. assert_eq!(asm("ldabsw 1"), Ok(vec![insn(ebpf::LD_ABS_W, 0, 0, 0, 1)]));
  120. }
  121. // Example for InstructionType::LoadInd.
  122. #[test]
  123. fn test_ldindw() {
  124. assert_eq!(
  125. asm("ldindw r1, 2"),
  126. Ok(vec![insn(ebpf::LD_IND_W, 0, 1, 0, 2)])
  127. );
  128. }
  129. // Example for InstructionType::LoadReg.
  130. #[test]
  131. fn test_ldxdw() {
  132. assert_eq!(
  133. asm("ldxdw r1, [r2+3]"),
  134. Ok(vec![insn(ebpf::LD_DW_REG, 1, 2, 3, 0)])
  135. );
  136. }
  137. // Example for InstructionType::StoreImm.
  138. #[test]
  139. fn test_sth() {
  140. assert_eq!(
  141. asm("sth [r1+2], 3"),
  142. Ok(vec![insn(ebpf::ST_H_IMM, 1, 0, 2, 3)])
  143. );
  144. }
  145. // Example for InstructionType::StoreReg.
  146. #[test]
  147. fn test_stxh() {
  148. assert_eq!(
  149. asm("stxh [r1+2], r3"),
  150. Ok(vec![insn(ebpf::ST_H_REG, 1, 3, 2, 0)])
  151. );
  152. }
  153. // Test all supported AluBinary mnemonics.
  154. #[test]
  155. fn test_alu_binary() {
  156. assert_eq!(
  157. asm("add r1, r2
  158. sub r1, r2
  159. mul r1, r2
  160. div r1, r2
  161. or r1, r2
  162. and r1, r2
  163. lsh r1, r2
  164. rsh r1, r2
  165. mod r1, r2
  166. xor r1, r2
  167. mov r1, r2
  168. arsh r1, r2"),
  169. Ok(vec![
  170. insn(ebpf::ADD64_REG, 1, 2, 0, 0),
  171. insn(ebpf::SUB64_REG, 1, 2, 0, 0),
  172. insn(ebpf::MUL64_REG, 1, 2, 0, 0),
  173. insn(ebpf::DIV64_REG, 1, 2, 0, 0),
  174. insn(ebpf::OR64_REG, 1, 2, 0, 0),
  175. insn(ebpf::AND64_REG, 1, 2, 0, 0),
  176. insn(ebpf::LSH64_REG, 1, 2, 0, 0),
  177. insn(ebpf::RSH64_REG, 1, 2, 0, 0),
  178. insn(ebpf::MOD64_REG, 1, 2, 0, 0),
  179. insn(ebpf::XOR64_REG, 1, 2, 0, 0),
  180. insn(ebpf::MOV64_REG, 1, 2, 0, 0),
  181. insn(ebpf::ARSH64_REG, 1, 2, 0, 0)
  182. ])
  183. );
  184. assert_eq!(
  185. asm("add r1, 2
  186. sub r1, 2
  187. mul r1, 2
  188. div r1, 2
  189. or r1, 2
  190. and r1, 2
  191. lsh r1, 2
  192. rsh r1, 2
  193. mod r1, 2
  194. xor r1, 2
  195. mov r1, 2
  196. arsh r1, 2"),
  197. Ok(vec![
  198. insn(ebpf::ADD64_IMM, 1, 0, 0, 2),
  199. insn(ebpf::SUB64_IMM, 1, 0, 0, 2),
  200. insn(ebpf::MUL64_IMM, 1, 0, 0, 2),
  201. insn(ebpf::DIV64_IMM, 1, 0, 0, 2),
  202. insn(ebpf::OR64_IMM, 1, 0, 0, 2),
  203. insn(ebpf::AND64_IMM, 1, 0, 0, 2),
  204. insn(ebpf::LSH64_IMM, 1, 0, 0, 2),
  205. insn(ebpf::RSH64_IMM, 1, 0, 0, 2),
  206. insn(ebpf::MOD64_IMM, 1, 0, 0, 2),
  207. insn(ebpf::XOR64_IMM, 1, 0, 0, 2),
  208. insn(ebpf::MOV64_IMM, 1, 0, 0, 2),
  209. insn(ebpf::ARSH64_IMM, 1, 0, 0, 2)
  210. ])
  211. );
  212. assert_eq!(
  213. asm("add64 r1, r2
  214. sub64 r1, r2
  215. mul64 r1, r2
  216. div64 r1, r2
  217. or64 r1, r2
  218. and64 r1, r2
  219. lsh64 r1, r2
  220. rsh64 r1, r2
  221. mod64 r1, r2
  222. xor64 r1, r2
  223. mov64 r1, r2
  224. arsh64 r1, r2"),
  225. Ok(vec![
  226. insn(ebpf::ADD64_REG, 1, 2, 0, 0),
  227. insn(ebpf::SUB64_REG, 1, 2, 0, 0),
  228. insn(ebpf::MUL64_REG, 1, 2, 0, 0),
  229. insn(ebpf::DIV64_REG, 1, 2, 0, 0),
  230. insn(ebpf::OR64_REG, 1, 2, 0, 0),
  231. insn(ebpf::AND64_REG, 1, 2, 0, 0),
  232. insn(ebpf::LSH64_REG, 1, 2, 0, 0),
  233. insn(ebpf::RSH64_REG, 1, 2, 0, 0),
  234. insn(ebpf::MOD64_REG, 1, 2, 0, 0),
  235. insn(ebpf::XOR64_REG, 1, 2, 0, 0),
  236. insn(ebpf::MOV64_REG, 1, 2, 0, 0),
  237. insn(ebpf::ARSH64_REG, 1, 2, 0, 0)
  238. ])
  239. );
  240. assert_eq!(
  241. asm("add64 r1, 2
  242. sub64 r1, 2
  243. mul64 r1, 2
  244. div64 r1, 2
  245. or64 r1, 2
  246. and64 r1, 2
  247. lsh64 r1, 2
  248. rsh64 r1, 2
  249. mod64 r1, 2
  250. xor64 r1, 2
  251. mov64 r1, 2
  252. arsh64 r1, 2"),
  253. Ok(vec![
  254. insn(ebpf::ADD64_IMM, 1, 0, 0, 2),
  255. insn(ebpf::SUB64_IMM, 1, 0, 0, 2),
  256. insn(ebpf::MUL64_IMM, 1, 0, 0, 2),
  257. insn(ebpf::DIV64_IMM, 1, 0, 0, 2),
  258. insn(ebpf::OR64_IMM, 1, 0, 0, 2),
  259. insn(ebpf::AND64_IMM, 1, 0, 0, 2),
  260. insn(ebpf::LSH64_IMM, 1, 0, 0, 2),
  261. insn(ebpf::RSH64_IMM, 1, 0, 0, 2),
  262. insn(ebpf::MOD64_IMM, 1, 0, 0, 2),
  263. insn(ebpf::XOR64_IMM, 1, 0, 0, 2),
  264. insn(ebpf::MOV64_IMM, 1, 0, 0, 2),
  265. insn(ebpf::ARSH64_IMM, 1, 0, 0, 2)
  266. ])
  267. );
  268. assert_eq!(
  269. asm("add32 r1, r2
  270. sub32 r1, r2
  271. mul32 r1, r2
  272. div32 r1, r2
  273. or32 r1, r2
  274. and32 r1, r2
  275. lsh32 r1, r2
  276. rsh32 r1, r2
  277. mod32 r1, r2
  278. xor32 r1, r2
  279. mov32 r1, r2
  280. arsh32 r1, r2"),
  281. Ok(vec![
  282. insn(ebpf::ADD32_REG, 1, 2, 0, 0),
  283. insn(ebpf::SUB32_REG, 1, 2, 0, 0),
  284. insn(ebpf::MUL32_REG, 1, 2, 0, 0),
  285. insn(ebpf::DIV32_REG, 1, 2, 0, 0),
  286. insn(ebpf::OR32_REG, 1, 2, 0, 0),
  287. insn(ebpf::AND32_REG, 1, 2, 0, 0),
  288. insn(ebpf::LSH32_REG, 1, 2, 0, 0),
  289. insn(ebpf::RSH32_REG, 1, 2, 0, 0),
  290. insn(ebpf::MOD32_REG, 1, 2, 0, 0),
  291. insn(ebpf::XOR32_REG, 1, 2, 0, 0),
  292. insn(ebpf::MOV32_REG, 1, 2, 0, 0),
  293. insn(ebpf::ARSH32_REG, 1, 2, 0, 0)
  294. ])
  295. );
  296. assert_eq!(
  297. asm("add32 r1, 2
  298. sub32 r1, 2
  299. mul32 r1, 2
  300. div32 r1, 2
  301. or32 r1, 2
  302. and32 r1, 2
  303. lsh32 r1, 2
  304. rsh32 r1, 2
  305. mod32 r1, 2
  306. xor32 r1, 2
  307. mov32 r1, 2
  308. arsh32 r1, 2"),
  309. Ok(vec![
  310. insn(ebpf::ADD32_IMM, 1, 0, 0, 2),
  311. insn(ebpf::SUB32_IMM, 1, 0, 0, 2),
  312. insn(ebpf::MUL32_IMM, 1, 0, 0, 2),
  313. insn(ebpf::DIV32_IMM, 1, 0, 0, 2),
  314. insn(ebpf::OR32_IMM, 1, 0, 0, 2),
  315. insn(ebpf::AND32_IMM, 1, 0, 0, 2),
  316. insn(ebpf::LSH32_IMM, 1, 0, 0, 2),
  317. insn(ebpf::RSH32_IMM, 1, 0, 0, 2),
  318. insn(ebpf::MOD32_IMM, 1, 0, 0, 2),
  319. insn(ebpf::XOR32_IMM, 1, 0, 0, 2),
  320. insn(ebpf::MOV32_IMM, 1, 0, 0, 2),
  321. insn(ebpf::ARSH32_IMM, 1, 0, 0, 2)
  322. ])
  323. );
  324. }
  325. // Test all supported AluUnary mnemonics.
  326. #[test]
  327. fn test_alu_unary() {
  328. assert_eq!(
  329. asm("neg r1
  330. neg64 r1
  331. neg32 r1"),
  332. Ok(vec![
  333. insn(ebpf::NEG64, 1, 0, 0, 0),
  334. insn(ebpf::NEG64, 1, 0, 0, 0),
  335. insn(ebpf::NEG32, 1, 0, 0, 0)
  336. ])
  337. );
  338. }
  339. // Test all supported LoadAbs mnemonics.
  340. #[test]
  341. fn test_load_abs() {
  342. assert_eq!(
  343. asm("ldabsw 1
  344. ldabsh 1
  345. ldabsb 1
  346. ldabsdw 1"),
  347. Ok(vec![
  348. insn(ebpf::LD_ABS_W, 0, 0, 0, 1),
  349. insn(ebpf::LD_ABS_H, 0, 0, 0, 1),
  350. insn(ebpf::LD_ABS_B, 0, 0, 0, 1),
  351. insn(ebpf::LD_ABS_DW, 0, 0, 0, 1)
  352. ])
  353. );
  354. }
  355. // Test all supported LoadInd mnemonics.
  356. #[test]
  357. fn test_load_ind() {
  358. assert_eq!(
  359. asm("ldindw r1, 2
  360. ldindh r1, 2
  361. ldindb r1, 2
  362. ldinddw r1, 2"),
  363. Ok(vec![
  364. insn(ebpf::LD_IND_W, 0, 1, 0, 2),
  365. insn(ebpf::LD_IND_H, 0, 1, 0, 2),
  366. insn(ebpf::LD_IND_B, 0, 1, 0, 2),
  367. insn(ebpf::LD_IND_DW, 0, 1, 0, 2)
  368. ])
  369. );
  370. }
  371. // Test all supported LoadReg mnemonics.
  372. #[test]
  373. fn test_load_reg() {
  374. assert_eq!(
  375. asm("ldxw r1, [r2+3]
  376. ldxh r1, [r2+3]
  377. ldxb r1, [r2+3]
  378. ldxdw r1, [r2+3]"),
  379. Ok(vec![
  380. insn(ebpf::LD_W_REG, 1, 2, 3, 0),
  381. insn(ebpf::LD_H_REG, 1, 2, 3, 0),
  382. insn(ebpf::LD_B_REG, 1, 2, 3, 0),
  383. insn(ebpf::LD_DW_REG, 1, 2, 3, 0)
  384. ])
  385. );
  386. }
  387. // Test all supported StoreImm mnemonics.
  388. #[test]
  389. fn test_store_imm() {
  390. assert_eq!(
  391. asm("stw [r1+2], 3
  392. sth [r1+2], 3
  393. stb [r1+2], 3
  394. stdw [r1+2], 3"),
  395. Ok(vec![
  396. insn(ebpf::ST_W_IMM, 1, 0, 2, 3),
  397. insn(ebpf::ST_H_IMM, 1, 0, 2, 3),
  398. insn(ebpf::ST_B_IMM, 1, 0, 2, 3),
  399. insn(ebpf::ST_DW_IMM, 1, 0, 2, 3)
  400. ])
  401. );
  402. }
  403. // Test all supported StoreReg mnemonics.
  404. #[test]
  405. fn test_store_reg() {
  406. assert_eq!(
  407. asm("stxw [r1+2], r3
  408. stxh [r1+2], r3
  409. stxb [r1+2], r3
  410. stxdw [r1+2], r3"),
  411. Ok(vec![
  412. insn(ebpf::ST_W_REG, 1, 3, 2, 0),
  413. insn(ebpf::ST_H_REG, 1, 3, 2, 0),
  414. insn(ebpf::ST_B_REG, 1, 3, 2, 0),
  415. insn(ebpf::ST_DW_REG, 1, 3, 2, 0)
  416. ])
  417. );
  418. }
  419. // Test all supported JumpConditional mnemonics.
  420. #[test]
  421. fn test_jump_conditional() {
  422. assert_eq!(
  423. asm("jeq r1, r2, +3
  424. jgt r1, r2, +3
  425. jge r1, r2, +3
  426. jlt r1, r2, +3
  427. jle r1, r2, +3
  428. jset r1, r2, +3
  429. jne r1, r2, +3
  430. jsgt r1, r2, +3
  431. jsge r1, r2, +3
  432. jslt r1, r2, +3
  433. jsle r1, r2, +3"),
  434. Ok(vec![
  435. insn(ebpf::JEQ_REG, 1, 2, 3, 0),
  436. insn(ebpf::JGT_REG, 1, 2, 3, 0),
  437. insn(ebpf::JGE_REG, 1, 2, 3, 0),
  438. insn(ebpf::JLT_REG, 1, 2, 3, 0),
  439. insn(ebpf::JLE_REG, 1, 2, 3, 0),
  440. insn(ebpf::JSET_REG, 1, 2, 3, 0),
  441. insn(ebpf::JNE_REG, 1, 2, 3, 0),
  442. insn(ebpf::JSGT_REG, 1, 2, 3, 0),
  443. insn(ebpf::JSGE_REG, 1, 2, 3, 0),
  444. insn(ebpf::JSLT_REG, 1, 2, 3, 0),
  445. insn(ebpf::JSLE_REG, 1, 2, 3, 0)
  446. ])
  447. );
  448. assert_eq!(
  449. asm("jeq r1, 2, +3
  450. jgt r1, 2, +3
  451. jge r1, 2, +3
  452. jlt r1, 2, +3
  453. jle r1, 2, +3
  454. jset r1, 2, +3
  455. jne r1, 2, +3
  456. jsgt r1, 2, +3
  457. jsge r1, 2, +3
  458. jslt r1, 2, +3
  459. jsle r1, 2, +3"),
  460. Ok(vec![
  461. insn(ebpf::JEQ_IMM, 1, 0, 3, 2),
  462. insn(ebpf::JGT_IMM, 1, 0, 3, 2),
  463. insn(ebpf::JGE_IMM, 1, 0, 3, 2),
  464. insn(ebpf::JLT_IMM, 1, 0, 3, 2),
  465. insn(ebpf::JLE_IMM, 1, 0, 3, 2),
  466. insn(ebpf::JSET_IMM, 1, 0, 3, 2),
  467. insn(ebpf::JNE_IMM, 1, 0, 3, 2),
  468. insn(ebpf::JSGT_IMM, 1, 0, 3, 2),
  469. insn(ebpf::JSGE_IMM, 1, 0, 3, 2),
  470. insn(ebpf::JSLT_IMM, 1, 0, 3, 2),
  471. insn(ebpf::JSLE_IMM, 1, 0, 3, 2)
  472. ])
  473. );
  474. assert_eq!(
  475. asm("jeq32 r1, r2, +3
  476. jgt32 r1, r2, +3
  477. jge32 r1, r2, +3
  478. jlt32 r1, r2, +3
  479. jle32 r1, r2, +3
  480. jset32 r1, r2, +3
  481. jne32 r1, r2, +3
  482. jsgt32 r1, r2, +3
  483. jsge32 r1, r2, +3
  484. jslt32 r1, r2, +3
  485. jsle32 r1, r2, +3"),
  486. Ok(vec![
  487. insn(ebpf::JEQ_REG32, 1, 2, 3, 0),
  488. insn(ebpf::JGT_REG32, 1, 2, 3, 0),
  489. insn(ebpf::JGE_REG32, 1, 2, 3, 0),
  490. insn(ebpf::JLT_REG32, 1, 2, 3, 0),
  491. insn(ebpf::JLE_REG32, 1, 2, 3, 0),
  492. insn(ebpf::JSET_REG32, 1, 2, 3, 0),
  493. insn(ebpf::JNE_REG32, 1, 2, 3, 0),
  494. insn(ebpf::JSGT_REG32, 1, 2, 3, 0),
  495. insn(ebpf::JSGE_REG32, 1, 2, 3, 0),
  496. insn(ebpf::JSLT_REG32, 1, 2, 3, 0),
  497. insn(ebpf::JSLE_REG32, 1, 2, 3, 0)
  498. ])
  499. );
  500. assert_eq!(
  501. asm("jeq32 r1, 2, +3
  502. jgt32 r1, 2, +3
  503. jge32 r1, 2, +3
  504. jlt32 r1, 2, +3
  505. jle32 r1, 2, +3
  506. jset32 r1, 2, +3
  507. jne32 r1, 2, +3
  508. jsgt32 r1, 2, +3
  509. jsge32 r1, 2, +3
  510. jslt32 r1, 2, +3
  511. jsle32 r1, 2, +3"),
  512. Ok(vec![
  513. insn(ebpf::JEQ_IMM32, 1, 0, 3, 2),
  514. insn(ebpf::JGT_IMM32, 1, 0, 3, 2),
  515. insn(ebpf::JGE_IMM32, 1, 0, 3, 2),
  516. insn(ebpf::JLT_IMM32, 1, 0, 3, 2),
  517. insn(ebpf::JLE_IMM32, 1, 0, 3, 2),
  518. insn(ebpf::JSET_IMM32, 1, 0, 3, 2),
  519. insn(ebpf::JNE_IMM32, 1, 0, 3, 2),
  520. insn(ebpf::JSGT_IMM32, 1, 0, 3, 2),
  521. insn(ebpf::JSGE_IMM32, 1, 0, 3, 2),
  522. insn(ebpf::JSLT_IMM32, 1, 0, 3, 2),
  523. insn(ebpf::JSLE_IMM32, 1, 0, 3, 2)
  524. ])
  525. );
  526. }
  527. // Test all supported Endian mnemonics.
  528. #[test]
  529. fn test_endian() {
  530. assert_eq!(
  531. asm("be16 r1
  532. be32 r1
  533. be64 r1
  534. le16 r1
  535. le32 r1
  536. le64 r1"),
  537. Ok(vec![
  538. insn(ebpf::BE, 1, 0, 0, 16),
  539. insn(ebpf::BE, 1, 0, 0, 32),
  540. insn(ebpf::BE, 1, 0, 0, 64),
  541. insn(ebpf::LE, 1, 0, 0, 16),
  542. insn(ebpf::LE, 1, 0, 0, 32),
  543. insn(ebpf::LE, 1, 0, 0, 64)
  544. ])
  545. );
  546. }
  547. #[test]
  548. fn test_large_immediate() {
  549. assert_eq!(
  550. asm("add64 r1, 2147483647"),
  551. Ok(vec![insn(ebpf::ADD64_IMM, 1, 0, 0, 2147483647)])
  552. );
  553. assert_eq!(
  554. asm("add64 r1, -2147483648"),
  555. Ok(vec![insn(ebpf::ADD64_IMM, 1, 0, 0, -2147483648)])
  556. );
  557. }
  558. #[test]
  559. fn test_tcp_sack() {
  560. assert_eq!(assemble(TCP_SACK_ASM), Ok(TCP_SACK_BIN.to_vec()));
  561. }
  562. #[test]
  563. fn test_error_invalid_instruction() {
  564. assert_eq!(asm("abcd"), Err("Invalid instruction \"abcd\"".to_string()));
  565. }
  566. #[test]
  567. fn test_error_unexpected_operands() {
  568. assert_eq!(
  569. asm("add 1, 2"),
  570. Err("Failed to encode add: Unexpected operands: [Integer(1), Integer(2)]".to_string())
  571. );
  572. }
  573. #[test]
  574. fn test_error_too_many_operands() {
  575. assert_eq!(
  576. asm("add 1, 2, 3, 4"),
  577. Err("Failed to encode add: Too many operands".to_string())
  578. );
  579. }
  580. #[test]
  581. fn test_error_operands_out_of_range() {
  582. assert_eq!(
  583. asm("add r16, r2"),
  584. Err("Failed to encode add: Invalid destination register 16".to_string())
  585. );
  586. assert_eq!(
  587. asm("add r1, r16"),
  588. Err("Failed to encode add: Invalid source register 16".to_string())
  589. );
  590. assert_eq!(
  591. asm("ja -32769"),
  592. Err("Failed to encode ja: Invalid offset -32769".to_string())
  593. );
  594. assert_eq!(
  595. asm("ja 32768"),
  596. Err("Failed to encode ja: Invalid offset 32768".to_string())
  597. );
  598. assert_eq!(
  599. asm("add r1, 4294967296"),
  600. Err("Failed to encode add: Invalid immediate 4294967296".to_string())
  601. );
  602. assert_eq!(
  603. asm("add r1, 2147483648"),
  604. Err("Failed to encode add: Invalid immediate 2147483648".to_string())
  605. );
  606. assert_eq!(
  607. asm("add r1, -2147483649"),
  608. Err("Failed to encode add: Invalid immediate -2147483649".to_string())
  609. );
  610. }