4
0

cranelift.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. // SPDX-License-Identifier: (Apache-2.0 OR MIT)
  2. #![cfg_attr(feature = "cargo-clippy", allow(clippy::unreadable_literal))]
  3. #![cfg(feature = "cranelift")]
  4. extern crate rbpf;
  5. mod common;
  6. use rbpf::{assembler::assemble, helpers};
  7. macro_rules! test_cranelift {
  8. ($name:ident, $prog:expr, $expected:expr) => {
  9. #[test]
  10. fn $name() {
  11. let prog = assemble($prog).unwrap();
  12. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  13. assert_eq!(vm.execute_cranelift().unwrap(), $expected);
  14. }
  15. };
  16. ($name:ident, $prog:expr, $mem:expr, $expected:expr) => {
  17. #[test]
  18. fn $name() {
  19. let prog = assemble($prog).unwrap();
  20. let mem = &mut $mem;
  21. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  22. assert_eq!(vm.execute_cranelift(mem).unwrap(), $expected);
  23. }
  24. };
  25. }
  26. test_cranelift!(
  27. test_cranelift_add,
  28. "
  29. mov32 r0, 0
  30. mov32 r1, 2
  31. add32 r0, 1
  32. add32 r0, r1
  33. exit
  34. ",
  35. 0x3
  36. );
  37. test_cranelift!(
  38. test_cranelift_alu64_arith,
  39. "
  40. mov r0, 0
  41. mov r1, 1
  42. mov r2, 2
  43. mov r3, 3
  44. mov r4, 4
  45. mov r5, 5
  46. mov r6, 6
  47. mov r7, 7
  48. mov r8, 8
  49. mov r9, 9
  50. add r0, 23
  51. add r0, r7
  52. sub r0, 13
  53. sub r0, r1
  54. mul r0, 7
  55. mul r0, r3
  56. div r0, 2
  57. div r0, r4
  58. exit
  59. ",
  60. 0x2a
  61. );
  62. test_cranelift!(
  63. test_cranelift_alu64_bit,
  64. "
  65. mov r0, 0
  66. mov r1, 1
  67. mov r2, 2
  68. mov r3, 3
  69. mov r4, 4
  70. mov r5, 5
  71. mov r6, 6
  72. mov r7, 7
  73. mov r8, 8
  74. or r0, r5
  75. or r0, 0xa0
  76. and r0, 0xa3
  77. mov r9, 0x91
  78. and r0, r9
  79. lsh r0, 32
  80. lsh r0, 22
  81. lsh r0, r8
  82. rsh r0, 32
  83. rsh r0, 19
  84. rsh r0, r7
  85. xor r0, 0x03
  86. xor r0, r2
  87. exit
  88. ",
  89. 0x11
  90. );
  91. test_cranelift!(
  92. test_cranelift_alu_arith,
  93. "
  94. mov32 r0, 0
  95. mov32 r1, 1
  96. mov32 r2, 2
  97. mov32 r3, 3
  98. mov32 r4, 4
  99. mov32 r5, 5
  100. mov32 r6, 6
  101. mov32 r7, 7
  102. mov32 r8, 8
  103. mov32 r9, 9
  104. add32 r0, 23
  105. add32 r0, r7
  106. sub32 r0, 13
  107. sub32 r0, r1
  108. mul32 r0, 7
  109. mul32 r0, r3
  110. div32 r0, 2
  111. div32 r0, r4
  112. exit
  113. ",
  114. 0x2a
  115. );
  116. test_cranelift!(
  117. test_cranelift_alu_bit,
  118. "
  119. mov32 r0, 0
  120. mov32 r1, 1
  121. mov32 r2, 2
  122. mov32 r3, 3
  123. mov32 r4, 4
  124. mov32 r5, 5
  125. mov32 r6, 6
  126. mov32 r7, 7
  127. mov32 r8, 8
  128. or32 r0, r5
  129. or32 r0, 0xa0
  130. and32 r0, 0xa3
  131. mov32 r9, 0x91
  132. and32 r0, r9
  133. lsh32 r0, 22
  134. lsh32 r0, r8
  135. rsh32 r0, 19
  136. rsh32 r0, r7
  137. xor32 r0, 0x03
  138. xor32 r0, r2
  139. exit
  140. ",
  141. 0x11
  142. );
  143. test_cranelift!(
  144. test_cranelift_arsh32_high_shift,
  145. "
  146. mov r0, 8
  147. lddw r1, 0x100000001
  148. arsh32 r0, r1
  149. exit
  150. ",
  151. 0x4
  152. );
  153. test_cranelift!(
  154. test_cranelift_arsh,
  155. "
  156. mov32 r0, 0xf8
  157. lsh32 r0, 28
  158. arsh32 r0, 16
  159. exit
  160. ",
  161. 0xffff8000
  162. );
  163. test_cranelift!(
  164. test_cranelift_arsh64,
  165. "
  166. mov32 r0, 1
  167. lsh r0, 63
  168. arsh r0, 55
  169. mov32 r1, 5
  170. arsh r0, r1
  171. exit
  172. ",
  173. 0xfffffffffffffff8
  174. );
  175. test_cranelift!(
  176. test_cranelift_arsh_reg,
  177. "
  178. mov32 r0, 0xf8
  179. mov32 r1, 16
  180. lsh32 r0, 28
  181. arsh32 r0, r1
  182. exit
  183. ",
  184. 0xffff8000
  185. );
  186. test_cranelift!(
  187. test_cranelift_be16,
  188. "
  189. ldxh r0, [r1]
  190. be16 r0
  191. exit
  192. ",
  193. [0x11, 0x22],
  194. 0x1122
  195. );
  196. test_cranelift!(
  197. test_cranelift_be16_high,
  198. "
  199. ldxdw r0, [r1]
  200. be16 r0
  201. exit
  202. ",
  203. [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88],
  204. 0x1122
  205. );
  206. test_cranelift!(
  207. test_cranelift_be32,
  208. "
  209. ldxw r0, [r1]
  210. be32 r0
  211. exit
  212. ",
  213. [0x11, 0x22, 0x33, 0x44],
  214. 0x11223344
  215. );
  216. test_cranelift!(
  217. test_cranelift_be32_high,
  218. "
  219. ldxdw r0, [r1]
  220. be32 r0
  221. exit
  222. ",
  223. [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88],
  224. 0x11223344
  225. );
  226. test_cranelift!(
  227. test_cranelift_be64,
  228. "
  229. ldxdw r0, [r1]
  230. be64 r0
  231. exit
  232. ",
  233. [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88],
  234. 0x1122334455667788
  235. );
  236. #[test]
  237. fn test_cranelift_call() {
  238. let prog = assemble(
  239. "
  240. mov r1, 1
  241. mov r2, 2
  242. mov r3, 3
  243. mov r4, 4
  244. mov r5, 5
  245. call 0
  246. exit",
  247. )
  248. .unwrap();
  249. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  250. vm.register_helper(0, helpers::gather_bytes).unwrap();
  251. assert_eq!(vm.execute_cranelift().unwrap(), 0x0102030405);
  252. }
  253. #[test]
  254. #[should_panic(expected = "[CRANELIFT] Error: unknown helper function (id: 0x3f)")]
  255. fn test_cranelift_err_call_unreg() {
  256. let prog = assemble("
  257. mov r1, 1
  258. mov r2, 2
  259. mov r3, 3
  260. mov r4, 4
  261. mov r5, 5
  262. call 63
  263. exit
  264. ").unwrap();
  265. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  266. vm.execute_cranelift().unwrap();
  267. }
  268. #[test]
  269. fn test_cranelift_call_memfrob() {
  270. let prog = assemble(
  271. "
  272. mov r6, r1
  273. add r1, 2
  274. mov r2, 4
  275. call 1
  276. ldxdw r0, [r6]
  277. be64 r0
  278. exit",
  279. )
  280. .unwrap();
  281. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  282. vm.register_helper(1, helpers::memfrob).unwrap();
  283. let mem = &mut [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];
  284. assert_eq!(vm.execute_cranelift(mem).unwrap(), 0x102292e2f2c0708);
  285. }
  286. test_cranelift!(
  287. test_cranelift_div32_high_divisor,
  288. "
  289. mov r0, 12
  290. lddw r1, 0x100000004
  291. div32 r0, r1
  292. exit
  293. ",
  294. 0x3
  295. );
  296. test_cranelift!(
  297. test_cranelift_div32_imm,
  298. "
  299. lddw r0, 0x10000000c
  300. div32 r0, 4
  301. exit
  302. ",
  303. 0x3
  304. );
  305. test_cranelift!(
  306. test_cranelift_div32_reg,
  307. "
  308. lddw r0, 0x10000000c
  309. mov r1, 4
  310. div32 r0, r1
  311. exit
  312. ",
  313. 0x3
  314. );
  315. test_cranelift!(
  316. test_cranelift_div64_imm,
  317. "
  318. mov r0, 0xc
  319. lsh r0, 32
  320. div r0, 4
  321. exit
  322. ",
  323. 0x300000000
  324. );
  325. test_cranelift!(
  326. test_cranelift_div64_reg,
  327. "
  328. mov r0, 0xc
  329. lsh r0, 32
  330. mov r1, 4
  331. div r0, r1
  332. exit
  333. ",
  334. 0x300000000
  335. );
  336. test_cranelift!(
  337. test_cranelift_div64_by_zero_imm,
  338. "
  339. mov32 r0, 1
  340. div r0, 0
  341. exit
  342. ",
  343. 0x0
  344. );
  345. test_cranelift!(
  346. test_cranelift_div_by_zero_imm,
  347. "
  348. mov32 r0, 1
  349. div32 r0, 0
  350. exit
  351. ",
  352. 0x0
  353. );
  354. test_cranelift!(
  355. test_cranelift_mod64_by_zero_imm,
  356. "
  357. mov32 r0, 1
  358. mod r0, 0
  359. exit
  360. ",
  361. 0x1
  362. );
  363. test_cranelift!(
  364. test_cranelift_mod_by_zero_imm,
  365. "
  366. mov32 r0, 1
  367. mod32 r0, 0
  368. exit
  369. ",
  370. 0x1
  371. );
  372. test_cranelift!(
  373. test_cranelift_div64_by_zero_reg,
  374. "
  375. mov32 r0, 1
  376. mov32 r1, 0
  377. div r0, r1
  378. exit
  379. ",
  380. 0x0
  381. );
  382. test_cranelift!(
  383. test_cranelift_div_by_zero_reg,
  384. "
  385. mov32 r0, 1
  386. mov32 r1, 0
  387. div32 r0, r1
  388. exit
  389. ",
  390. 0x0
  391. );
  392. test_cranelift!(
  393. test_cranelift_mod64_by_zero_reg,
  394. "
  395. mov32 r0, 1
  396. mov32 r1, 0
  397. mod r0, r1
  398. exit
  399. ",
  400. 0x1
  401. );
  402. test_cranelift!(
  403. test_cranelift_mod_by_zero_reg,
  404. "
  405. mov32 r0, 1
  406. mov32 r1, 0
  407. mod32 r0, r1
  408. exit
  409. ",
  410. 0x1
  411. );
  412. test_cranelift!(
  413. test_cranelift_exit,
  414. "
  415. mov r0, 0
  416. exit
  417. ",
  418. 0x0
  419. );
  420. test_cranelift!(
  421. test_cranelift_lddw,
  422. "
  423. lddw r0, 0x1122334455667788
  424. exit
  425. ",
  426. 0x1122334455667788
  427. );
  428. test_cranelift!(
  429. test_cranelift_lddw2,
  430. "
  431. lddw r0, 0x0000000080000000
  432. exit
  433. ",
  434. 0x80000000
  435. );
  436. test_cranelift!(
  437. test_cranelift_ldxb_all,
  438. "
  439. mov r0, r1
  440. ldxb r9, [r0+0]
  441. lsh r9, 0
  442. ldxb r8, [r0+1]
  443. lsh r8, 4
  444. ldxb r7, [r0+2]
  445. lsh r7, 8
  446. ldxb r6, [r0+3]
  447. lsh r6, 12
  448. ldxb r5, [r0+4]
  449. lsh r5, 16
  450. ldxb r4, [r0+5]
  451. lsh r4, 20
  452. ldxb r3, [r0+6]
  453. lsh r3, 24
  454. ldxb r2, [r0+7]
  455. lsh r2, 28
  456. ldxb r1, [r0+8]
  457. lsh r1, 32
  458. ldxb r0, [r0+9]
  459. lsh r0, 36
  460. or r0, r1
  461. or r0, r2
  462. or r0, r3
  463. or r0, r4
  464. or r0, r5
  465. or r0, r6
  466. or r0, r7
  467. or r0, r8
  468. or r0, r9
  469. exit
  470. ",
  471. [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09],
  472. 0x9876543210
  473. );
  474. test_cranelift!(
  475. test_cranelift_ldxb,
  476. "
  477. ldxb r0, [r1+2]
  478. exit
  479. ",
  480. [0xaa, 0xbb, 0x11, 0xcc, 0xdd],
  481. 0x11
  482. );
  483. test_cranelift!(
  484. test_cranelift_ldxdw,
  485. "
  486. ldxdw r0, [r1+2]
  487. exit
  488. ",
  489. [0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0xcc, 0xdd],
  490. 0x8877665544332211
  491. );
  492. test_cranelift!(
  493. test_cranelift_ldxh_all,
  494. "
  495. mov r0, r1
  496. ldxh r9, [r0+0]
  497. be16 r9
  498. lsh r9, 0
  499. ldxh r8, [r0+2]
  500. be16 r8
  501. lsh r8, 4
  502. ldxh r7, [r0+4]
  503. be16 r7
  504. lsh r7, 8
  505. ldxh r6, [r0+6]
  506. be16 r6
  507. lsh r6, 12
  508. ldxh r5, [r0+8]
  509. be16 r5
  510. lsh r5, 16
  511. ldxh r4, [r0+10]
  512. be16 r4
  513. lsh r4, 20
  514. ldxh r3, [r0+12]
  515. be16 r3
  516. lsh r3, 24
  517. ldxh r2, [r0+14]
  518. be16 r2
  519. lsh r2, 28
  520. ldxh r1, [r0+16]
  521. be16 r1
  522. lsh r1, 32
  523. ldxh r0, [r0+18]
  524. be16 r0
  525. lsh r0, 36
  526. or r0, r1
  527. or r0, r2
  528. or r0, r3
  529. or r0, r4
  530. or r0, r5
  531. or r0, r6
  532. or r0, r7
  533. or r0, r8
  534. or r0, r9
  535. exit
  536. ",
  537. [
  538. 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00,
  539. 0x07, 0x00, 0x08, 0x00, 0x09
  540. ],
  541. 0x9876543210
  542. );
  543. test_cranelift!(
  544. test_cranelift_ldxh_all2,
  545. "
  546. mov r0, r1
  547. ldxh r9, [r0+0]
  548. be16 r9
  549. ldxh r8, [r0+2]
  550. be16 r8
  551. ldxh r7, [r0+4]
  552. be16 r7
  553. ldxh r6, [r0+6]
  554. be16 r6
  555. ldxh r5, [r0+8]
  556. be16 r5
  557. ldxh r4, [r0+10]
  558. be16 r4
  559. ldxh r3, [r0+12]
  560. be16 r3
  561. ldxh r2, [r0+14]
  562. be16 r2
  563. ldxh r1, [r0+16]
  564. be16 r1
  565. ldxh r0, [r0+18]
  566. be16 r0
  567. or r0, r1
  568. or r0, r2
  569. or r0, r3
  570. or r0, r4
  571. or r0, r5
  572. or r0, r6
  573. or r0, r7
  574. or r0, r8
  575. or r0, r9
  576. exit
  577. ",
  578. [
  579. 0x00, 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08, 0x00, 0x10, 0x00, 0x20, 0x00, 0x40, 0x00,
  580. 0x80, 0x01, 0x00, 0x02, 0x00
  581. ],
  582. 0x3ff
  583. );
  584. test_cranelift!(
  585. test_cranelift_ldxh,
  586. "
  587. ldxh r0, [r1+2]
  588. exit
  589. ",
  590. [0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd],
  591. 0x2211
  592. );
  593. test_cranelift!(
  594. test_cranelift_ldxw_all,
  595. "
  596. mov r0, r1
  597. ldxw r9, [r0+0]
  598. be32 r9
  599. ldxw r8, [r0+4]
  600. be32 r8
  601. ldxw r7, [r0+8]
  602. be32 r7
  603. ldxw r6, [r0+12]
  604. be32 r6
  605. ldxw r5, [r0+16]
  606. be32 r5
  607. ldxw r4, [r0+20]
  608. be32 r4
  609. ldxw r3, [r0+24]
  610. be32 r3
  611. ldxw r2, [r0+28]
  612. be32 r2
  613. ldxw r1, [r0+32]
  614. be32 r1
  615. ldxw r0, [r0+36]
  616. be32 r0
  617. or r0, r1
  618. or r0, r2
  619. or r0, r3
  620. or r0, r4
  621. or r0, r5
  622. or r0, r6
  623. or r0, r7
  624. or r0, r8
  625. or r0, r9
  626. exit
  627. ",
  628. [
  629. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  630. 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  631. 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00
  632. ],
  633. 0x030f0f
  634. );
  635. test_cranelift!(
  636. test_cranelift_ldxw,
  637. "
  638. ldxw r0, [r1+2]
  639. exit
  640. ",
  641. [0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0xcc, 0xdd],
  642. 0x44332211
  643. );
  644. test_cranelift!(
  645. test_cranelift_le16,
  646. "
  647. ldxh r0, [r1]
  648. le16 r0
  649. exit
  650. ",
  651. [0x22, 0x11],
  652. 0x1122
  653. );
  654. test_cranelift!(
  655. test_cranelift_le32,
  656. "
  657. ldxw r0, [r1]
  658. le32 r0
  659. exit
  660. ",
  661. [0x44, 0x33, 0x22, 0x11],
  662. 0x11223344
  663. );
  664. test_cranelift!(
  665. test_cranelift_le64,
  666. "
  667. ldxdw r0, [r1]
  668. le64 r0
  669. exit
  670. ",
  671. [0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11],
  672. 0x1122334455667788
  673. );
  674. test_cranelift!(
  675. test_cranelift_lsh_reg,
  676. "
  677. mov r0, 0x1
  678. mov r7, 4
  679. lsh r0, r7
  680. exit
  681. ",
  682. 0x10
  683. );
  684. test_cranelift!(
  685. test_cranelift_mod,
  686. "
  687. mov32 r0, 5748
  688. mod32 r0, 92
  689. mov32 r1, 13
  690. mod32 r0, r1
  691. exit
  692. ",
  693. 0x5
  694. );
  695. test_cranelift!(
  696. test_cranelift_mod32,
  697. "
  698. lddw r0, 0x100000003
  699. mod32 r0, 3
  700. exit
  701. ",
  702. 0x0
  703. );
  704. test_cranelift!(
  705. test_cranelift_mod64,
  706. "
  707. mov32 r0, -1316649930
  708. lsh r0, 32
  709. or r0, 0x100dc5c8
  710. mov32 r1, 0xdde263e
  711. lsh r1, 32
  712. or r1, 0x3cbef7f3
  713. mod r0, r1
  714. mod r0, 0x658f1778
  715. exit
  716. ",
  717. 0x30ba5a04
  718. );
  719. test_cranelift!(
  720. test_cranelift_mov,
  721. "
  722. mov32 r1, 1
  723. mov32 r0, r1
  724. exit
  725. ",
  726. 0x1
  727. );
  728. test_cranelift!(
  729. test_cranelift_mul32_imm,
  730. "
  731. mov r0, 3
  732. mul32 r0, 4
  733. exit
  734. ",
  735. 0xc
  736. );
  737. test_cranelift!(
  738. test_cranelift_mul32_reg,
  739. "
  740. mov r0, 3
  741. mov r1, 4
  742. mul32 r0, r1
  743. exit
  744. ",
  745. 0xc
  746. );
  747. test_cranelift!(
  748. test_cranelift_mul32_reg_overflow,
  749. "
  750. mov r0, 0x40000001
  751. mov r1, 4
  752. mul32 r0, r1
  753. exit
  754. ",
  755. 0x4
  756. );
  757. test_cranelift!(
  758. test_cranelift_mul64_imm,
  759. "
  760. mov r0, 0x40000001
  761. mul r0, 4
  762. exit
  763. ",
  764. 0x100000004
  765. );
  766. test_cranelift!(
  767. test_cranelift_mul64_reg,
  768. "
  769. mov r0, 0x40000001
  770. mov r1, 4
  771. mul r0, r1
  772. exit
  773. ",
  774. 0x100000004
  775. );