cranelift.rs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287
  1. // SPDX-License-Identifier: (Apache-2.0 OR MIT)
  2. #![cfg(feature = "cranelift")]
  3. extern crate rbpf;
  4. mod common;
  5. use rbpf::{assembler::assemble, helpers};
  6. use crate::common::{TCP_SACK_ASM, TCP_SACK_MATCH, TCP_SACK_NOMATCH};
  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 mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  13. vm.cranelift_compile().unwrap();
  14. assert_eq!(vm.execute_program_cranelift().unwrap(), $expected);
  15. }
  16. };
  17. ($name:ident, $prog:expr, $mem:expr, $expected:expr) => {
  18. #[test]
  19. fn $name() {
  20. let prog = assemble($prog).unwrap();
  21. let mem = &mut $mem;
  22. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  23. vm.cranelift_compile().unwrap();
  24. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), $expected);
  25. }
  26. };
  27. }
  28. test_cranelift!(
  29. test_cranelift_add,
  30. "
  31. mov32 r0, 0
  32. mov32 r1, 2
  33. add32 r0, 1
  34. add32 r0, r1
  35. exit
  36. ",
  37. 0x3
  38. );
  39. test_cranelift!(
  40. test_cranelift_alu64_arith,
  41. "
  42. mov r0, 0
  43. mov r1, 1
  44. mov r2, 2
  45. mov r3, 3
  46. mov r4, 4
  47. mov r5, 5
  48. mov r6, 6
  49. mov r7, 7
  50. mov r8, 8
  51. mov r9, 9
  52. add r0, 23
  53. add r0, r7
  54. sub r0, 13
  55. sub r0, r1
  56. mul r0, 7
  57. mul r0, r3
  58. div r0, 2
  59. div r0, r4
  60. exit
  61. ",
  62. 0x2a
  63. );
  64. test_cranelift!(
  65. test_cranelift_alu64_bit,
  66. "
  67. mov r0, 0
  68. mov r1, 1
  69. mov r2, 2
  70. mov r3, 3
  71. mov r4, 4
  72. mov r5, 5
  73. mov r6, 6
  74. mov r7, 7
  75. mov r8, 8
  76. or r0, r5
  77. or r0, 0xa0
  78. and r0, 0xa3
  79. mov r9, 0x91
  80. and r0, r9
  81. lsh r0, 32
  82. lsh r0, 22
  83. lsh r0, r8
  84. rsh r0, 32
  85. rsh r0, 19
  86. rsh r0, r7
  87. xor r0, 0x03
  88. xor r0, r2
  89. exit
  90. ",
  91. 0x11
  92. );
  93. test_cranelift!(
  94. test_cranelift_alu_arith,
  95. "
  96. mov32 r0, 0
  97. mov32 r1, 1
  98. mov32 r2, 2
  99. mov32 r3, 3
  100. mov32 r4, 4
  101. mov32 r5, 5
  102. mov32 r6, 6
  103. mov32 r7, 7
  104. mov32 r8, 8
  105. mov32 r9, 9
  106. add32 r0, 23
  107. add32 r0, r7
  108. sub32 r0, 13
  109. sub32 r0, r1
  110. mul32 r0, 7
  111. mul32 r0, r3
  112. div32 r0, 2
  113. div32 r0, r4
  114. exit
  115. ",
  116. 0x2a
  117. );
  118. test_cranelift!(
  119. test_cranelift_alu_bit,
  120. "
  121. mov32 r0, 0
  122. mov32 r1, 1
  123. mov32 r2, 2
  124. mov32 r3, 3
  125. mov32 r4, 4
  126. mov32 r5, 5
  127. mov32 r6, 6
  128. mov32 r7, 7
  129. mov32 r8, 8
  130. or32 r0, r5
  131. or32 r0, 0xa0
  132. and32 r0, 0xa3
  133. mov32 r9, 0x91
  134. and32 r0, r9
  135. lsh32 r0, 22
  136. lsh32 r0, r8
  137. rsh32 r0, 19
  138. rsh32 r0, r7
  139. xor32 r0, 0x03
  140. xor32 r0, r2
  141. exit
  142. ",
  143. 0x11
  144. );
  145. test_cranelift!(
  146. test_cranelift_arsh32_high_shift,
  147. "
  148. mov r0, 8
  149. lddw r1, 0x100000001
  150. arsh32 r0, r1
  151. exit
  152. ",
  153. 0x4
  154. );
  155. test_cranelift!(
  156. test_cranelift_arsh,
  157. "
  158. mov32 r0, 0xf8
  159. lsh32 r0, 28
  160. arsh32 r0, 16
  161. exit
  162. ",
  163. 0xffff8000
  164. );
  165. test_cranelift!(
  166. test_cranelift_arsh64,
  167. "
  168. mov32 r0, 1
  169. lsh r0, 63
  170. arsh r0, 55
  171. mov32 r1, 5
  172. arsh r0, r1
  173. exit
  174. ",
  175. 0xfffffffffffffff8
  176. );
  177. test_cranelift!(
  178. test_cranelift_arsh_reg,
  179. "
  180. mov32 r0, 0xf8
  181. mov32 r1, 16
  182. lsh32 r0, 28
  183. arsh32 r0, r1
  184. exit
  185. ",
  186. 0xffff8000
  187. );
  188. test_cranelift!(
  189. test_cranelift_be16,
  190. "
  191. ldxh r0, [r1]
  192. be16 r0
  193. exit
  194. ",
  195. [0x11, 0x22],
  196. 0x1122
  197. );
  198. test_cranelift!(
  199. test_cranelift_be16_high,
  200. "
  201. ldxdw r0, [r1]
  202. be16 r0
  203. exit
  204. ",
  205. [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88],
  206. 0x1122
  207. );
  208. test_cranelift!(
  209. test_cranelift_be32,
  210. "
  211. ldxw r0, [r1]
  212. be32 r0
  213. exit
  214. ",
  215. [0x11, 0x22, 0x33, 0x44],
  216. 0x11223344
  217. );
  218. test_cranelift!(
  219. test_cranelift_be32_high,
  220. "
  221. ldxdw r0, [r1]
  222. be32 r0
  223. exit
  224. ",
  225. [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88],
  226. 0x11223344
  227. );
  228. test_cranelift!(
  229. test_cranelift_be64,
  230. "
  231. ldxdw r0, [r1]
  232. be64 r0
  233. exit
  234. ",
  235. [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88],
  236. 0x1122334455667788
  237. );
  238. #[test]
  239. fn test_cranelift_call() {
  240. let prog = assemble(
  241. "
  242. mov r1, 1
  243. mov r2, 2
  244. mov r3, 3
  245. mov r4, 4
  246. mov r5, 5
  247. call 0
  248. exit",
  249. )
  250. .unwrap();
  251. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  252. vm.register_helper(0, helpers::gather_bytes).unwrap();
  253. vm.cranelift_compile().unwrap();
  254. assert_eq!(vm.execute_program_cranelift().unwrap(), 0x0102030405);
  255. }
  256. #[test]
  257. #[should_panic(expected = "[CRANELIFT] Error: unknown helper function (id: 0x3f)")]
  258. fn test_cranelift_err_call_unreg() {
  259. let prog = assemble(
  260. "
  261. mov r1, 1
  262. mov r2, 2
  263. mov r3, 3
  264. mov r4, 4
  265. mov r5, 5
  266. call 63
  267. exit
  268. ",
  269. )
  270. .unwrap();
  271. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  272. vm.cranelift_compile().unwrap();
  273. }
  274. #[test]
  275. fn test_cranelift_call_memfrob() {
  276. let prog = assemble(
  277. "
  278. mov r6, r1
  279. add r1, 2
  280. mov r2, 4
  281. call 1
  282. ldxdw r0, [r6]
  283. be64 r0
  284. exit",
  285. )
  286. .unwrap();
  287. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  288. vm.register_helper(1, helpers::memfrob).unwrap();
  289. let mem = &mut [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];
  290. vm.cranelift_compile().unwrap();
  291. assert_eq!(
  292. vm.execute_program_cranelift(mem).unwrap(),
  293. 0x102292e2f2c0708
  294. );
  295. }
  296. test_cranelift!(
  297. test_cranelift_div32_high_divisor,
  298. "
  299. mov r0, 12
  300. lddw r1, 0x100000004
  301. div32 r0, r1
  302. exit
  303. ",
  304. 0x3
  305. );
  306. test_cranelift!(
  307. test_cranelift_div32_imm,
  308. "
  309. lddw r0, 0x10000000c
  310. div32 r0, 4
  311. exit
  312. ",
  313. 0x3
  314. );
  315. test_cranelift!(
  316. test_cranelift_div32_reg,
  317. "
  318. lddw r0, 0x10000000c
  319. mov r1, 4
  320. div32 r0, r1
  321. exit
  322. ",
  323. 0x3
  324. );
  325. test_cranelift!(
  326. test_cranelift_div64_imm,
  327. "
  328. mov r0, 0xc
  329. lsh r0, 32
  330. div r0, 4
  331. exit
  332. ",
  333. 0x300000000
  334. );
  335. test_cranelift!(
  336. test_cranelift_div64_reg,
  337. "
  338. mov r0, 0xc
  339. lsh r0, 32
  340. mov r1, 4
  341. div r0, r1
  342. exit
  343. ",
  344. 0x300000000
  345. );
  346. test_cranelift!(
  347. test_cranelift_early_exit,
  348. "
  349. mov r0, 3
  350. exit
  351. mov r0, 4
  352. exit
  353. ",
  354. 0x3
  355. );
  356. test_cranelift!(
  357. test_cranelift_div64_by_zero_imm,
  358. "
  359. mov32 r0, 1
  360. div r0, 0
  361. exit
  362. ",
  363. 0x0
  364. );
  365. test_cranelift!(
  366. test_cranelift_div_by_zero_imm,
  367. "
  368. mov32 r0, 1
  369. div32 r0, 0
  370. exit
  371. ",
  372. 0x0
  373. );
  374. test_cranelift!(
  375. test_cranelift_mod64_by_zero_imm,
  376. "
  377. mov32 r0, 1
  378. mod r0, 0
  379. exit
  380. ",
  381. 0x1
  382. );
  383. test_cranelift!(
  384. test_cranelift_mod_by_zero_imm,
  385. "
  386. mov32 r0, 1
  387. mod32 r0, 0
  388. exit
  389. ",
  390. 0x1
  391. );
  392. test_cranelift!(
  393. test_cranelift_div64_by_zero_reg,
  394. "
  395. mov32 r0, 1
  396. mov32 r1, 0
  397. div r0, r1
  398. exit
  399. ",
  400. 0x0
  401. );
  402. test_cranelift!(
  403. test_cranelift_div_by_zero_reg,
  404. "
  405. mov32 r0, 1
  406. mov32 r1, 0
  407. div32 r0, r1
  408. exit
  409. ",
  410. 0x0
  411. );
  412. test_cranelift!(
  413. test_cranelift_mod64_by_zero_reg,
  414. "
  415. mov32 r0, 1
  416. mov32 r1, 0
  417. mod r0, r1
  418. exit
  419. ",
  420. 0x1
  421. );
  422. test_cranelift!(
  423. test_cranelift_mod_by_zero_reg,
  424. "
  425. mov32 r0, 1
  426. mov32 r1, 0
  427. mod32 r0, r1
  428. exit
  429. ",
  430. 0x1
  431. );
  432. #[test]
  433. // #[should_panic(expected = "Error: out of bounds memory store (insn #1)")]
  434. #[ignore = "We have stack OOB checks, but we don't yet catch the trap code and convert it into a panic"]
  435. fn test_cranelift_err_stack_out_of_bound() {
  436. #[rustfmt::skip]
  437. let prog = [
  438. 0x72, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  439. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  440. ];
  441. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  442. vm.cranelift_compile().unwrap();
  443. vm.execute_program_cranelift().unwrap();
  444. }
  445. test_cranelift!(
  446. test_cranelift_exit,
  447. "
  448. mov r0, 0
  449. exit
  450. ",
  451. 0x0
  452. );
  453. test_cranelift!(
  454. test_cranelift_ja,
  455. "
  456. mov r0, 1
  457. ja +1
  458. mov r0, 2
  459. exit
  460. ",
  461. 0x1
  462. );
  463. test_cranelift!(
  464. test_cranelift_jeq_imm,
  465. "
  466. mov32 r0, 0
  467. mov32 r1, 0xa
  468. jeq r1, 0xb, +4
  469. mov32 r0, 1
  470. mov32 r1, 0xb
  471. jeq r1, 0xb, +1
  472. mov32 r0, 2
  473. exit
  474. ",
  475. 0x1
  476. );
  477. test_cranelift!(
  478. test_cranelift_jeq_reg,
  479. "
  480. mov32 r0, 0
  481. mov32 r1, 0xa
  482. mov32 r2, 0xb
  483. jeq r1, r2, +4
  484. mov32 r0, 1
  485. mov32 r1, 0xb
  486. jeq r1, r2, +1
  487. mov32 r0, 2
  488. exit
  489. ",
  490. 0x1
  491. );
  492. test_cranelift!(
  493. test_cranelift_jge_imm,
  494. "
  495. mov32 r0, 0
  496. mov32 r1, 0xa
  497. jge r1, 0xb, +4
  498. mov32 r0, 1
  499. mov32 r1, 0xc
  500. jge r1, 0xb, +1
  501. mov32 r0, 2
  502. exit
  503. ",
  504. 0x1
  505. );
  506. test_cranelift!(
  507. test_cranelift_jle_imm,
  508. "
  509. mov32 r0, 0
  510. mov32 r1, 5
  511. jle r1, 4, +1
  512. jle r1, 6, +1
  513. exit
  514. jle r1, 5, +1
  515. exit
  516. mov32 r0, 1
  517. exit
  518. ",
  519. 0x1
  520. );
  521. test_cranelift!(
  522. test_cranelift_jle_reg,
  523. "
  524. mov r0, 0
  525. mov r1, 5
  526. mov r2, 4
  527. mov r3, 6
  528. jle r1, r2, +2
  529. jle r1, r1, +1
  530. exit
  531. jle r1, r3, +1
  532. exit
  533. mov r0, 1
  534. exit
  535. ",
  536. 0x1
  537. );
  538. test_cranelift!(
  539. test_cranelift_jgt_imm,
  540. "
  541. mov32 r0, 0
  542. mov32 r1, 5
  543. jgt r1, 6, +2
  544. jgt r1, 5, +1
  545. jgt r1, 4, +1
  546. exit
  547. mov32 r0, 1
  548. exit
  549. ",
  550. 0x1
  551. );
  552. test_cranelift!(
  553. test_cranelift_jgt_reg,
  554. "
  555. mov r0, 0
  556. mov r1, 5
  557. mov r2, 6
  558. mov r3, 4
  559. jgt r1, r2, +2
  560. jgt r1, r1, +1
  561. jgt r1, r3, +1
  562. exit
  563. mov r0, 1
  564. exit
  565. ",
  566. 0x1
  567. );
  568. test_cranelift!(
  569. test_cranelift_jlt_imm,
  570. "
  571. mov32 r0, 0
  572. mov32 r1, 5
  573. jlt r1, 4, +2
  574. jlt r1, 5, +1
  575. jlt r1, 6, +1
  576. exit
  577. mov32 r0, 1
  578. exit
  579. ",
  580. 0x1
  581. );
  582. test_cranelift!(
  583. test_cranelift_jlt_reg,
  584. "
  585. mov r0, 0
  586. mov r1, 5
  587. mov r2, 4
  588. mov r3, 6
  589. jlt r1, r2, +2
  590. jlt r1, r1, +1
  591. jlt r1, r3, +1
  592. exit
  593. mov r0, 1
  594. exit
  595. ",
  596. 0x1
  597. );
  598. test_cranelift!(
  599. test_cranelift_jit_bounce,
  600. "
  601. mov r0, 1
  602. mov r6, r0
  603. mov r7, r6
  604. mov r8, r7
  605. mov r9, r8
  606. mov r0, r9
  607. exit
  608. ",
  609. 0x1
  610. );
  611. test_cranelift!(
  612. test_cranelift_jne_reg,
  613. "
  614. mov32 r0, 0
  615. mov32 r1, 0xb
  616. mov32 r2, 0xb
  617. jne r1, r2, +4
  618. mov32 r0, 1
  619. mov32 r1, 0xa
  620. jne r1, r2, +1
  621. mov32 r0, 2
  622. exit
  623. ",
  624. 0x1
  625. );
  626. test_cranelift!(
  627. test_cranelift_jset_imm,
  628. "
  629. mov32 r0, 0
  630. mov32 r1, 0x7
  631. jset r1, 0x8, +4
  632. mov32 r0, 1
  633. mov32 r1, 0x9
  634. jset r1, 0x8, +1
  635. mov32 r0, 2
  636. exit
  637. ",
  638. 0x1
  639. );
  640. test_cranelift!(
  641. test_cranelift_jset_reg,
  642. "
  643. mov32 r0, 0
  644. mov32 r1, 0x7
  645. mov32 r2, 0x8
  646. jset r1, r2, +4
  647. mov32 r0, 1
  648. mov32 r1, 0x9
  649. jset r1, r2, +1
  650. mov32 r0, 2
  651. exit
  652. ",
  653. 0x1
  654. );
  655. test_cranelift!(
  656. test_cranelift_jsge_imm,
  657. "
  658. mov32 r0, 0
  659. mov r1, -2
  660. jsge r1, -1, +5
  661. jsge r1, 0, +4
  662. mov32 r0, 1
  663. mov r1, -1
  664. jsge r1, -1, +1
  665. mov32 r0, 2
  666. exit
  667. ",
  668. 0x1
  669. );
  670. test_cranelift!(
  671. test_cranelift_jsge_reg,
  672. "
  673. mov32 r0, 0
  674. mov r1, -2
  675. mov r2, -1
  676. mov32 r3, 0
  677. jsge r1, r2, +5
  678. jsge r1, r3, +4
  679. mov32 r0, 1
  680. mov r1, r2
  681. jsge r1, r2, +1
  682. mov32 r0, 2
  683. exit
  684. ",
  685. 0x1
  686. );
  687. test_cranelift!(
  688. test_cranelift_jsle_imm,
  689. "
  690. mov32 r0, 0
  691. mov r1, -2
  692. jsle r1, -3, +1
  693. jsle r1, -1, +1
  694. exit
  695. mov32 r0, 1
  696. jsle r1, -2, +1
  697. mov32 r0, 2
  698. exit
  699. ",
  700. 0x1
  701. );
  702. test_cranelift!(
  703. test_cranelift_jsle_reg,
  704. "
  705. mov32 r0, 0
  706. mov r1, -1
  707. mov r2, -2
  708. mov32 r3, 0
  709. jsle r1, r2, +1
  710. jsle r1, r3, +1
  711. exit
  712. mov32 r0, 1
  713. mov r1, r2
  714. jsle r1, r2, +1
  715. mov32 r0, 2
  716. exit
  717. ",
  718. 0x1
  719. );
  720. test_cranelift!(
  721. test_cranelift_jsgt_imm,
  722. "
  723. mov32 r0, 0
  724. mov r1, -2
  725. jsgt r1, -1, +4
  726. mov32 r0, 1
  727. mov32 r1, 0
  728. jsgt r1, -1, +1
  729. mov32 r0, 2
  730. exit
  731. ",
  732. 0x1
  733. );
  734. test_cranelift!(
  735. test_cranelift_jsgt_reg,
  736. "
  737. mov32 r0, 0
  738. mov r1, -2
  739. mov r2, -1
  740. jsgt r1, r2, +4
  741. mov32 r0, 1
  742. mov32 r1, 0
  743. jsgt r1, r2, +1
  744. mov32 r0, 2
  745. exit
  746. ",
  747. 0x1
  748. );
  749. test_cranelift!(
  750. test_cranelift_jslt_imm,
  751. "
  752. mov32 r0, 0
  753. mov r1, -2
  754. jslt r1, -3, +2
  755. jslt r1, -2, +1
  756. jslt r1, -1, +1
  757. exit
  758. mov32 r0, 1
  759. exit
  760. ",
  761. 0x1
  762. );
  763. test_cranelift!(
  764. test_cranelift_jslt_reg,
  765. "
  766. mov32 r0, 0
  767. mov r1, -2
  768. mov r2, -3
  769. mov r3, -1
  770. jslt r1, r1, +2
  771. jslt r1, r2, +1
  772. jslt r1, r3, +1
  773. exit
  774. mov32 r0, 1
  775. exit
  776. ",
  777. 0x1
  778. );
  779. test_cranelift!(
  780. test_cranelift_jeq32_imm,
  781. "
  782. mov r9, 1
  783. lsh r9, 32
  784. mov32 r0, 0x0
  785. mov32 r1, 0xa
  786. jeq32 r1, 0xb, +5
  787. mov32 r0, 1
  788. mov r1, 0xb
  789. or r1, r9
  790. jeq32 r1, 0xb, +1
  791. mov32 r0, 2
  792. exit
  793. ",
  794. 0x1
  795. );
  796. test_cranelift!(
  797. test_cranelift_jeq32_reg,
  798. "
  799. mov r9, 1
  800. lsh r9, 32
  801. mov32 r0, 0
  802. mov32 r1, 0xa
  803. mov32 r2, 0xb
  804. jeq32 r1, r2, +5
  805. mov32 r0, 1
  806. mov32 r1, 0xb
  807. or r1, r9
  808. jeq32 r1, r2, +1
  809. mov32 r0, 2
  810. exit
  811. ",
  812. 0x1
  813. );
  814. test_cranelift!(
  815. test_cranelift_jge32_imm,
  816. "
  817. mov r9, 1
  818. lsh r9, 32
  819. mov32 r0, 0
  820. mov32 r1, 0xa
  821. jge32 r1, 0xb, +5
  822. mov32 r0, 1
  823. or r1, r9
  824. mov32 r1, 0xc
  825. jge32 r1, 0xb, +1
  826. mov32 r0, 2
  827. exit
  828. ",
  829. 0x1
  830. );
  831. test_cranelift!(
  832. test_cranelift_jge32_reg,
  833. "
  834. mov r9, 1
  835. lsh r9, 32
  836. mov32 r0, 0
  837. mov32 r1, 0xa
  838. mov32 r2, 0xb
  839. jge32 r1, r2, +5
  840. mov32 r0, 1
  841. or r1, r9
  842. mov32 r1, 0xc
  843. jge32 r1, r2, +1
  844. mov32 r0, 2
  845. exit
  846. ",
  847. 0x1
  848. );
  849. test_cranelift!(
  850. test_cranelift_jgt32_imm,
  851. "
  852. mov r9, 1
  853. lsh r9, 32
  854. mov32 r0, 0
  855. mov32 r1, 5
  856. or r1, r9
  857. jgt32 r1, 6, +4
  858. jgt32 r1, 5, +3
  859. jgt32 r1, 4, +1
  860. exit
  861. mov32 r0, 1
  862. exit
  863. ",
  864. 0x1
  865. );
  866. test_cranelift!(
  867. test_cranelift_jgt32_reg,
  868. "
  869. mov r9, 1
  870. lsh r9, 32
  871. mov r0, 0
  872. mov r1, 5
  873. mov32 r1, 5
  874. or r1, r9
  875. mov r2, 6
  876. mov r3, 4
  877. jgt32 r1, r2, +4
  878. jgt32 r1, r1, +3
  879. jgt32 r1, r3, +1
  880. exit
  881. mov r0, 1
  882. exit
  883. ",
  884. 0x1
  885. );
  886. test_cranelift!(
  887. test_cranelift_jle32_imm,
  888. "
  889. mov r9, 1
  890. lsh r9, 32
  891. mov32 r0, 0
  892. mov32 r1, 5
  893. or r1, r9
  894. jle32 r1, 4, +5
  895. jle32 r1, 6, +1
  896. exit
  897. jle32 r1, 5, +1
  898. exit
  899. mov32 r0, 1
  900. exit
  901. ",
  902. 0x1
  903. );
  904. test_cranelift!(
  905. test_cranelift_jle32_reg,
  906. "
  907. mov r9, 1
  908. lsh r9, 32
  909. mov r0, 0
  910. mov r1, 5
  911. mov r2, 4
  912. mov r3, 6
  913. or r1, r9
  914. jle32 r1, r2, +5
  915. jle32 r1, r1, +1
  916. exit
  917. jle32 r1, r3, +1
  918. exit
  919. mov r0, 1
  920. exit
  921. ",
  922. 0x1
  923. );
  924. test_cranelift!(
  925. test_cranelift_jlt32_imm,
  926. "
  927. mov r9, 1
  928. lsh r9, 32
  929. mov32 r0, 0
  930. mov32 r1, 5
  931. or r1, r9
  932. jlt32 r1, 4, +4
  933. jlt32 r1, 5, +3
  934. jlt32 r1, 6, +1
  935. exit
  936. mov32 r0, 1
  937. exit
  938. ",
  939. 0x1
  940. );
  941. test_cranelift!(
  942. test_cranelift_jlt32_reg,
  943. "
  944. mov r9, 1
  945. lsh r9, 32
  946. mov r0, 0
  947. mov r1, 5
  948. mov r2, 4
  949. mov r3, 6
  950. or r1, r9
  951. jlt32 r1, r2, +4
  952. jlt32 r1, r1, +3
  953. jlt32 r1, r3, +1
  954. exit
  955. mov r0, 1
  956. exit
  957. ",
  958. 0x1
  959. );
  960. test_cranelift!(
  961. test_cranelift_jne32_imm,
  962. "
  963. mov r9, 1
  964. lsh r9, 32
  965. mov32 r0, 0
  966. mov32 r1, 0xb
  967. or r1, r9
  968. jne32 r1, 0xb, +4
  969. mov32 r0, 1
  970. mov32 r1, 0xa
  971. or r1, r9
  972. jne32 r1, 0xb, +1
  973. mov32 r0, 2
  974. exit
  975. ",
  976. 0x1
  977. );
  978. test_cranelift!(
  979. test_cranelift_jne32_reg,
  980. "
  981. mov r9, 1
  982. lsh r9, 32
  983. mov32 r0, 0
  984. mov32 r1, 0xb
  985. or r1, r9
  986. mov32 r2, 0xb
  987. jne32 r1, r2, +4
  988. mov32 r0, 1
  989. mov32 r1, 0xa
  990. or r1, r9
  991. jne32 r1, r2, +1
  992. mov32 r0, 2
  993. exit
  994. ",
  995. 0x1
  996. );
  997. test_cranelift!(
  998. test_cranelift_jset32_imm,
  999. "
  1000. mov r9, 1
  1001. lsh r9, 32
  1002. mov32 r0, 0
  1003. mov32 r1, 0x7
  1004. or r1, r9
  1005. jset32 r1, 0x8, +4
  1006. mov32 r0, 1
  1007. mov32 r1, 0x9
  1008. jset32 r1, 0x8, +1
  1009. mov32 r0, 2
  1010. exit
  1011. ",
  1012. 0x1
  1013. );
  1014. test_cranelift!(
  1015. test_cranelift_jset32_reg,
  1016. "
  1017. mov r9, 1
  1018. lsh r9, 32
  1019. mov32 r0, 0
  1020. mov32 r1, 0x7
  1021. or r1, r9
  1022. mov32 r2, 0x8
  1023. jset32 r1, r2, +4
  1024. mov32 r0, 1
  1025. mov32 r1, 0x9
  1026. jset32 r1, r2, +1
  1027. mov32 r0, 2
  1028. exit
  1029. ",
  1030. 0x1
  1031. );
  1032. test_cranelift!(
  1033. test_cranelift_jsge32_imm,
  1034. "
  1035. mov r9, 1
  1036. lsh r9, 32
  1037. mov32 r0, 0
  1038. mov32 r1, -2
  1039. or r1, r9
  1040. jsge32 r1, -1, +5
  1041. jsge32 r1, 0, +4
  1042. mov32 r0, 1
  1043. mov r1, -1
  1044. jsge32 r1, -1, +1
  1045. mov32 r0, 2
  1046. exit
  1047. ",
  1048. 0x1
  1049. );
  1050. test_cranelift!(
  1051. test_cranelift_jsge32_reg,
  1052. "
  1053. mov r9, 1
  1054. lsh r9, 32
  1055. mov32 r0, 0
  1056. mov32 r1, -2
  1057. or r1, r9
  1058. mov r2, -1
  1059. mov32 r3, 0
  1060. jsge32 r1, r2, +5
  1061. jsge32 r1, r3, +4
  1062. mov32 r0, 1
  1063. mov r1, r2
  1064. jsge32 r1, r2, +1
  1065. mov32 r0, 2
  1066. exit
  1067. ",
  1068. 0x1
  1069. );
  1070. test_cranelift!(
  1071. test_cranelift_jsgt32_imm,
  1072. "
  1073. mov r9, 1
  1074. lsh r9, 32
  1075. mov32 r0, 0
  1076. mov32 r1, -2
  1077. or r1, r9
  1078. jsgt32 r1, -1, +4
  1079. mov32 r0, 1
  1080. mov32 r1, 0
  1081. jsgt32 r1, -1, +1
  1082. mov32 r0, 2
  1083. exit
  1084. ",
  1085. 0x1
  1086. );
  1087. test_cranelift!(
  1088. test_cranelift_jsgt32_reg,
  1089. "
  1090. mov r9, 1
  1091. lsh r9, 32
  1092. mov32 r0, 0
  1093. mov32 r1, -2
  1094. or r1, r9
  1095. mov r2, -1
  1096. jsgt32 r1, r2, +4
  1097. mov32 r0, 1
  1098. mov32 r1, 0
  1099. jsgt32 r1, r2, +1
  1100. mov32 r0, 2
  1101. exit
  1102. ",
  1103. 0x1
  1104. );
  1105. test_cranelift!(
  1106. test_cranelift_jsle32_imm,
  1107. "
  1108. mov r9, 1
  1109. lsh r9, 32
  1110. mov32 r0, 0
  1111. mov32 r1, -2
  1112. or r1, r9
  1113. jsle32 r1, -3, +5
  1114. jsle32 r1, -1, +1
  1115. exit
  1116. mov32 r0, 1
  1117. jsle32 r1, -2, +1
  1118. mov32 r0, 2
  1119. exit
  1120. ",
  1121. 0x1
  1122. );
  1123. test_cranelift!(
  1124. test_cranelift_jsle32_reg,
  1125. "
  1126. mov r9, 1
  1127. lsh r9, 32
  1128. mov32 r0, 0
  1129. mov32 r1, -2
  1130. or r1, r9
  1131. mov r2, -3
  1132. mov32 r3, 0
  1133. jsle32 r1, r2, +6
  1134. jsle32 r1, r3, +1
  1135. exit
  1136. mov32 r0, 1
  1137. mov r1, r2
  1138. jsle32 r1, r2, +1
  1139. mov32 r0, 2
  1140. exit
  1141. ",
  1142. 0x1
  1143. );
  1144. test_cranelift!(
  1145. test_cranelift_jslt32_imm,
  1146. "
  1147. mov r9, 1
  1148. lsh r9, 32
  1149. mov32 r0, 0
  1150. mov32 r1, -2
  1151. or r1, r9
  1152. jslt32 r1, -3, +4
  1153. jslt32 r1, -2, +3
  1154. jslt32 r1, -1, +1
  1155. exit
  1156. mov32 r0, 1
  1157. exit
  1158. ",
  1159. 0x1
  1160. );
  1161. test_cranelift!(
  1162. test_cranelift_jslt32_reg,
  1163. "
  1164. mov r9, 1
  1165. lsh r9, 32
  1166. mov32 r0, 0
  1167. mov32 r1, -2
  1168. or r1, r9
  1169. mov r2, -3
  1170. mov r3, -1
  1171. jslt32 r1, r1, +4
  1172. jslt32 r1, r2, +3
  1173. jslt32 r1, r3, +1
  1174. exit
  1175. mov32 r0, 1
  1176. exit
  1177. ",
  1178. 0x1
  1179. );
  1180. test_cranelift!(
  1181. test_cranelift_lddw,
  1182. "
  1183. lddw r0, 0x1122334455667788
  1184. exit
  1185. ",
  1186. 0x1122334455667788
  1187. );
  1188. test_cranelift!(
  1189. test_cranelift_lddw2,
  1190. "
  1191. lddw r0, 0x0000000080000000
  1192. exit
  1193. ",
  1194. 0x80000000
  1195. );
  1196. test_cranelift!(
  1197. test_cranelift_ldxb_all,
  1198. "
  1199. mov r0, r1
  1200. ldxb r9, [r0+0]
  1201. lsh r9, 0
  1202. ldxb r8, [r0+1]
  1203. lsh r8, 4
  1204. ldxb r7, [r0+2]
  1205. lsh r7, 8
  1206. ldxb r6, [r0+3]
  1207. lsh r6, 12
  1208. ldxb r5, [r0+4]
  1209. lsh r5, 16
  1210. ldxb r4, [r0+5]
  1211. lsh r4, 20
  1212. ldxb r3, [r0+6]
  1213. lsh r3, 24
  1214. ldxb r2, [r0+7]
  1215. lsh r2, 28
  1216. ldxb r1, [r0+8]
  1217. lsh r1, 32
  1218. ldxb r0, [r0+9]
  1219. lsh r0, 36
  1220. or r0, r1
  1221. or r0, r2
  1222. or r0, r3
  1223. or r0, r4
  1224. or r0, r5
  1225. or r0, r6
  1226. or r0, r7
  1227. or r0, r8
  1228. or r0, r9
  1229. exit
  1230. ",
  1231. [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09],
  1232. 0x9876543210
  1233. );
  1234. test_cranelift!(
  1235. test_cranelift_ldxb,
  1236. "
  1237. ldxb r0, [r1+2]
  1238. exit
  1239. ",
  1240. [0xaa, 0xbb, 0x11, 0xcc, 0xdd],
  1241. 0x11
  1242. );
  1243. test_cranelift!(
  1244. test_cranelift_ldxdw,
  1245. "
  1246. ldxdw r0, [r1+2]
  1247. exit
  1248. ",
  1249. [0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0xcc, 0xdd],
  1250. 0x8877665544332211
  1251. );
  1252. test_cranelift!(
  1253. test_cranelift_ldxh_all,
  1254. "
  1255. mov r0, r1
  1256. ldxh r9, [r0+0]
  1257. be16 r9
  1258. lsh r9, 0
  1259. ldxh r8, [r0+2]
  1260. be16 r8
  1261. lsh r8, 4
  1262. ldxh r7, [r0+4]
  1263. be16 r7
  1264. lsh r7, 8
  1265. ldxh r6, [r0+6]
  1266. be16 r6
  1267. lsh r6, 12
  1268. ldxh r5, [r0+8]
  1269. be16 r5
  1270. lsh r5, 16
  1271. ldxh r4, [r0+10]
  1272. be16 r4
  1273. lsh r4, 20
  1274. ldxh r3, [r0+12]
  1275. be16 r3
  1276. lsh r3, 24
  1277. ldxh r2, [r0+14]
  1278. be16 r2
  1279. lsh r2, 28
  1280. ldxh r1, [r0+16]
  1281. be16 r1
  1282. lsh r1, 32
  1283. ldxh r0, [r0+18]
  1284. be16 r0
  1285. lsh r0, 36
  1286. or r0, r1
  1287. or r0, r2
  1288. or r0, r3
  1289. or r0, r4
  1290. or r0, r5
  1291. or r0, r6
  1292. or r0, r7
  1293. or r0, r8
  1294. or r0, r9
  1295. exit
  1296. ",
  1297. [
  1298. 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00,
  1299. 0x07, 0x00, 0x08, 0x00, 0x09
  1300. ],
  1301. 0x9876543210
  1302. );
  1303. test_cranelift!(
  1304. test_cranelift_ldxh_all2,
  1305. "
  1306. mov r0, r1
  1307. ldxh r9, [r0+0]
  1308. be16 r9
  1309. ldxh r8, [r0+2]
  1310. be16 r8
  1311. ldxh r7, [r0+4]
  1312. be16 r7
  1313. ldxh r6, [r0+6]
  1314. be16 r6
  1315. ldxh r5, [r0+8]
  1316. be16 r5
  1317. ldxh r4, [r0+10]
  1318. be16 r4
  1319. ldxh r3, [r0+12]
  1320. be16 r3
  1321. ldxh r2, [r0+14]
  1322. be16 r2
  1323. ldxh r1, [r0+16]
  1324. be16 r1
  1325. ldxh r0, [r0+18]
  1326. be16 r0
  1327. or r0, r1
  1328. or r0, r2
  1329. or r0, r3
  1330. or r0, r4
  1331. or r0, r5
  1332. or r0, r6
  1333. or r0, r7
  1334. or r0, r8
  1335. or r0, r9
  1336. exit
  1337. ",
  1338. [
  1339. 0x00, 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08, 0x00, 0x10, 0x00, 0x20, 0x00, 0x40, 0x00,
  1340. 0x80, 0x01, 0x00, 0x02, 0x00
  1341. ],
  1342. 0x3ff
  1343. );
  1344. test_cranelift!(
  1345. test_cranelift_ldxh,
  1346. "
  1347. ldxh r0, [r1+2]
  1348. exit
  1349. ",
  1350. [0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd],
  1351. 0x2211
  1352. );
  1353. test_cranelift!(
  1354. test_cranelift_ldxh_same_reg,
  1355. "
  1356. mov r0, r1
  1357. sth [r0], 0x1234
  1358. ldxh r0, [r0]
  1359. exit
  1360. ",
  1361. [0xff, 0xff],
  1362. 0x1234
  1363. );
  1364. test_cranelift!(
  1365. test_cranelift_ldxw_all,
  1366. "
  1367. mov r0, r1
  1368. ldxw r9, [r0+0]
  1369. be32 r9
  1370. ldxw r8, [r0+4]
  1371. be32 r8
  1372. ldxw r7, [r0+8]
  1373. be32 r7
  1374. ldxw r6, [r0+12]
  1375. be32 r6
  1376. ldxw r5, [r0+16]
  1377. be32 r5
  1378. ldxw r4, [r0+20]
  1379. be32 r4
  1380. ldxw r3, [r0+24]
  1381. be32 r3
  1382. ldxw r2, [r0+28]
  1383. be32 r2
  1384. ldxw r1, [r0+32]
  1385. be32 r1
  1386. ldxw r0, [r0+36]
  1387. be32 r0
  1388. or r0, r1
  1389. or r0, r2
  1390. or r0, r3
  1391. or r0, r4
  1392. or r0, r5
  1393. or r0, r6
  1394. or r0, r7
  1395. or r0, r8
  1396. or r0, r9
  1397. exit
  1398. ",
  1399. [
  1400. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  1401. 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  1402. 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00
  1403. ],
  1404. 0x030f0f
  1405. );
  1406. test_cranelift!(
  1407. test_cranelift_ldxw,
  1408. "
  1409. ldxw r0, [r1+2]
  1410. exit
  1411. ",
  1412. [0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0xcc, 0xdd],
  1413. 0x44332211
  1414. );
  1415. test_cranelift!(
  1416. test_cranelift_le16,
  1417. "
  1418. ldxh r0, [r1]
  1419. le16 r0
  1420. exit
  1421. ",
  1422. [0x22, 0x11],
  1423. 0x1122
  1424. );
  1425. test_cranelift!(
  1426. test_cranelift_le32,
  1427. "
  1428. ldxw r0, [r1]
  1429. le32 r0
  1430. exit
  1431. ",
  1432. [0x44, 0x33, 0x22, 0x11],
  1433. 0x11223344
  1434. );
  1435. test_cranelift!(
  1436. test_cranelift_le64,
  1437. "
  1438. ldxdw r0, [r1]
  1439. le64 r0
  1440. exit
  1441. ",
  1442. [0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11],
  1443. 0x1122334455667788
  1444. );
  1445. test_cranelift!(
  1446. test_cranelift_lsh_reg,
  1447. "
  1448. mov r0, 0x1
  1449. mov r7, 4
  1450. lsh r0, r7
  1451. exit
  1452. ",
  1453. 0x10
  1454. );
  1455. test_cranelift!(
  1456. test_cranelift_mod,
  1457. "
  1458. mov32 r0, 5748
  1459. mod32 r0, 92
  1460. mov32 r1, 13
  1461. mod32 r0, r1
  1462. exit
  1463. ",
  1464. 0x5
  1465. );
  1466. test_cranelift!(
  1467. test_cranelift_mod32,
  1468. "
  1469. lddw r0, 0x100000003
  1470. mod32 r0, 3
  1471. exit
  1472. ",
  1473. 0x0
  1474. );
  1475. test_cranelift!(
  1476. test_cranelift_mod64,
  1477. "
  1478. mov32 r0, -1316649930
  1479. lsh r0, 32
  1480. or r0, 0x100dc5c8
  1481. mov32 r1, 0xdde263e
  1482. lsh r1, 32
  1483. or r1, 0x3cbef7f3
  1484. mod r0, r1
  1485. mod r0, 0x658f1778
  1486. exit
  1487. ",
  1488. 0x30ba5a04
  1489. );
  1490. test_cranelift!(
  1491. test_cranelift_mov,
  1492. "
  1493. mov32 r1, 1
  1494. mov32 r0, r1
  1495. exit
  1496. ",
  1497. 0x1
  1498. );
  1499. test_cranelift!(
  1500. test_cranelift_mul32_imm,
  1501. "
  1502. mov r0, 3
  1503. mul32 r0, 4
  1504. exit
  1505. ",
  1506. 0xc
  1507. );
  1508. test_cranelift!(
  1509. test_cranelift_mul32_reg,
  1510. "
  1511. mov r0, 3
  1512. mov r1, 4
  1513. mul32 r0, r1
  1514. exit
  1515. ",
  1516. 0xc
  1517. );
  1518. test_cranelift!(
  1519. test_cranelift_mul32_reg_overflow,
  1520. "
  1521. mov r0, 0x40000001
  1522. mov r1, 4
  1523. mul32 r0, r1
  1524. exit
  1525. ",
  1526. 0x4
  1527. );
  1528. test_cranelift!(
  1529. test_cranelift_mul64_imm,
  1530. "
  1531. mov r0, 0x40000001
  1532. mul r0, 4
  1533. exit
  1534. ",
  1535. 0x100000004
  1536. );
  1537. test_cranelift!(
  1538. test_cranelift_mul64_reg,
  1539. "
  1540. mov r0, 0x40000001
  1541. mov r1, 4
  1542. mul r0, r1
  1543. exit
  1544. ",
  1545. 0x100000004
  1546. );
  1547. test_cranelift!(
  1548. test_cranelift_mul_loop,
  1549. "
  1550. mov r0, 0x7
  1551. add r1, 0xa
  1552. lsh r1, 0x20
  1553. rsh r1, 0x20
  1554. jeq r1, 0x0, +4
  1555. mov r0, 0x7
  1556. mul r0, 0x7
  1557. add r1, -1
  1558. jne r1, 0x0, -3
  1559. exit
  1560. ",
  1561. 0x75db9c97
  1562. );
  1563. test_cranelift!(
  1564. test_cranelift_neg64,
  1565. "
  1566. mov32 r0, 2
  1567. neg r0
  1568. exit
  1569. ",
  1570. 0xfffffffffffffffe
  1571. );
  1572. test_cranelift!(
  1573. test_cranelift_neg,
  1574. "
  1575. mov32 r0, 2
  1576. neg32 r0
  1577. exit
  1578. ",
  1579. 0xfffffffe
  1580. );
  1581. test_cranelift!(
  1582. test_cranelift_prime,
  1583. "
  1584. mov r1, 67
  1585. mov r0, 0x1
  1586. mov r2, 0x2
  1587. jgt r1, 0x2, +4
  1588. ja +10
  1589. add r2, 0x1
  1590. mov r0, 0x1
  1591. jge r2, r1, +7
  1592. mov r3, r1
  1593. div r3, r2
  1594. mul r3, r2
  1595. mov r4, r1
  1596. sub r4, r3
  1597. mov r0, 0x0
  1598. jne r4, 0x0, -10
  1599. exit
  1600. ",
  1601. 1
  1602. );
  1603. test_cranelift!(
  1604. test_cranelift_rhs32,
  1605. "
  1606. xor r0, r0
  1607. sub r0, 1
  1608. rsh32 r0, 8
  1609. exit
  1610. ",
  1611. 0x00ffffff
  1612. );
  1613. test_cranelift!(
  1614. test_cranelift_rsh_reg,
  1615. "
  1616. mov r0, 0x10
  1617. mov r7, 4
  1618. rsh r0, r7
  1619. exit
  1620. ",
  1621. 0x1
  1622. );
  1623. test_cranelift!(
  1624. test_cranelift_stack,
  1625. "
  1626. mov r1, 51
  1627. stdw [r10-16], 0xab
  1628. stdw [r10-8], 0xcd
  1629. and r1, 1
  1630. lsh r1, 3
  1631. mov r2, r10
  1632. add r2, r1
  1633. ldxdw r0, [r2-16]
  1634. exit
  1635. ",
  1636. 0xcd
  1637. );
  1638. #[test]
  1639. fn test_cranelift_stack2() {
  1640. let prog = assemble(
  1641. "
  1642. stb [r10-4], 0x01
  1643. stb [r10-3], 0x02
  1644. stb [r10-2], 0x03
  1645. stb [r10-1], 0x04
  1646. mov r1, r10
  1647. mov r2, 0x4
  1648. sub r1, r2
  1649. call 1
  1650. mov r1, 0
  1651. ldxb r2, [r10-4]
  1652. ldxb r3, [r10-3]
  1653. ldxb r4, [r10-2]
  1654. ldxb r5, [r10-1]
  1655. call 0
  1656. xor r0, 0x2a2a2a2a
  1657. exit",
  1658. )
  1659. .unwrap();
  1660. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1661. vm.register_helper(0, helpers::gather_bytes).unwrap();
  1662. vm.register_helper(1, helpers::memfrob).unwrap();
  1663. vm.cranelift_compile().unwrap();
  1664. assert_eq!(vm.execute_program_cranelift().unwrap(), 0x01020304);
  1665. }
  1666. test_cranelift!(
  1667. test_cranelift_stb,
  1668. "
  1669. stb [r1+2], 0x11
  1670. ldxb r0, [r1+2]
  1671. exit
  1672. ",
  1673. [0xaa, 0xbb, 0xff, 0xcc, 0xdd],
  1674. 0x11
  1675. );
  1676. test_cranelift!(
  1677. test_cranelift_stdw,
  1678. "
  1679. stdw [r1+2], 0x44332211
  1680. ldxdw r0, [r1+2]
  1681. exit
  1682. ",
  1683. [0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd],
  1684. 0x44332211
  1685. );
  1686. test_cranelift!(
  1687. test_cranelift_sth,
  1688. "
  1689. sth [r1+2], 0x2211
  1690. ldxh r0, [r1+2]
  1691. exit
  1692. ",
  1693. [0xaa, 0xbb, 0xff, 0xff, 0xcc, 0xdd],
  1694. 0x2211
  1695. );
  1696. #[test]
  1697. #[ignore]
  1698. fn test_cranelift_string_stack() {
  1699. let prog = assemble(
  1700. "
  1701. mov r1, 0x78636261
  1702. stxw [r10-8], r1
  1703. mov r6, 0x0
  1704. stxb [r10-4], r6
  1705. stxb [r10-12], r6
  1706. mov r1, 0x79636261
  1707. stxw [r10-16], r1
  1708. mov r1, r10
  1709. add r1, -8
  1710. mov r2, r1
  1711. call 0x4
  1712. mov r1, r0
  1713. mov r0, 0x1
  1714. lsh r1, 0x20
  1715. rsh r1, 0x20
  1716. jne r1, 0x0, +11
  1717. mov r1, r10
  1718. add r1, -8
  1719. mov r2, r10
  1720. add r2, -16
  1721. call 0x4
  1722. mov r1, r0
  1723. lsh r1, 0x20
  1724. rsh r1, 0x20
  1725. mov r0, 0x1
  1726. jeq r1, r6, +1
  1727. mov r0, 0x0
  1728. exit",
  1729. )
  1730. .unwrap();
  1731. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1732. vm.register_helper(4, helpers::strcmp).unwrap();
  1733. vm.cranelift_compile().unwrap();
  1734. assert_eq!(vm.execute_program_cranelift().unwrap(), 0x0);
  1735. }
  1736. test_cranelift!(
  1737. test_cranelift_stw,
  1738. "
  1739. stw [r1+2], 0x44332211
  1740. ldxw r0, [r1+2]
  1741. exit
  1742. ",
  1743. [0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd],
  1744. 0x44332211
  1745. );
  1746. test_cranelift!(
  1747. test_cranelift_stxb,
  1748. "
  1749. mov32 r2, 0x11
  1750. stxb [r1+2], r2
  1751. ldxb r0, [r1+2]
  1752. exit
  1753. ",
  1754. [0xaa, 0xbb, 0xff, 0xcc, 0xdd],
  1755. 0x11
  1756. );
  1757. test_cranelift!(
  1758. test_cranelift_stxb_all,
  1759. "
  1760. mov r0, 0xf0
  1761. mov r2, 0xf2
  1762. mov r3, 0xf3
  1763. mov r4, 0xf4
  1764. mov r5, 0xf5
  1765. mov r6, 0xf6
  1766. mov r7, 0xf7
  1767. mov r8, 0xf8
  1768. stxb [r1], r0
  1769. stxb [r1+1], r2
  1770. stxb [r1+2], r3
  1771. stxb [r1+3], r4
  1772. stxb [r1+4], r5
  1773. stxb [r1+5], r6
  1774. stxb [r1+6], r7
  1775. stxb [r1+7], r8
  1776. ldxdw r0, [r1]
  1777. be64 r0
  1778. exit
  1779. ",
  1780. [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
  1781. 0xf0f2f3f4f5f6f7f8
  1782. );
  1783. test_cranelift!(
  1784. test_cranelift_stxb_all2,
  1785. "
  1786. mov r0, r1
  1787. mov r1, 0xf1
  1788. mov r9, 0xf9
  1789. stxb [r0], r1
  1790. stxb [r0+1], r9
  1791. ldxh r0, [r0]
  1792. be16 r0
  1793. exit
  1794. ",
  1795. [0xff, 0xff],
  1796. 0xf1f9
  1797. );
  1798. test_cranelift!(
  1799. test_cranelift_stxb_chain,
  1800. "
  1801. mov r0, r1
  1802. ldxb r9, [r0+0]
  1803. stxb [r0+1], r9
  1804. ldxb r8, [r0+1]
  1805. stxb [r0+2], r8
  1806. ldxb r7, [r0+2]
  1807. stxb [r0+3], r7
  1808. ldxb r6, [r0+3]
  1809. stxb [r0+4], r6
  1810. ldxb r5, [r0+4]
  1811. stxb [r0+5], r5
  1812. ldxb r4, [r0+5]
  1813. stxb [r0+6], r4
  1814. ldxb r3, [r0+6]
  1815. stxb [r0+7], r3
  1816. ldxb r2, [r0+7]
  1817. stxb [r0+8], r2
  1818. ldxb r1, [r0+8]
  1819. stxb [r0+9], r1
  1820. ldxb r0, [r0+9]
  1821. exit
  1822. ",
  1823. [0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
  1824. 0x2a
  1825. );
  1826. test_cranelift!(
  1827. test_cranelift_stxdw,
  1828. "
  1829. mov r2, -2005440939
  1830. lsh r2, 32
  1831. or r2, 0x44332211
  1832. stxdw [r1+2], r2
  1833. ldxdw r0, [r1+2]
  1834. exit
  1835. ",
  1836. [0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd],
  1837. 0x8877665544332211
  1838. );
  1839. test_cranelift!(
  1840. test_cranelift_stxh,
  1841. "
  1842. mov32 r2, 0x2211
  1843. stxh [r1+2], r2
  1844. ldxh r0, [r1+2]
  1845. exit
  1846. ",
  1847. [0xaa, 0xbb, 0xff, 0xff, 0xcc, 0xdd],
  1848. 0x2211
  1849. );
  1850. test_cranelift!(
  1851. test_cranelift_stxw,
  1852. "
  1853. mov32 r2, 0x44332211
  1854. stxw [r1+2], r2
  1855. ldxw r0, [r1+2]
  1856. exit
  1857. ",
  1858. [0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd],
  1859. 0x44332211
  1860. );
  1861. test_cranelift!(
  1862. test_cranelift_subnet,
  1863. "
  1864. mov r2, 0xe
  1865. ldxh r3, [r1+12]
  1866. jne r3, 0x81, +2
  1867. mov r2, 0x12
  1868. ldxh r3, [r1+16]
  1869. and r3, 0xffff
  1870. jne r3, 0x8, +5
  1871. add r1, r2
  1872. mov r0, 0x1
  1873. ldxw r1, [r1+16]
  1874. and r1, 0xffffff
  1875. jeq r1, 0x1a8c0, +1
  1876. mov r0, 0x0
  1877. exit
  1878. ",
  1879. [
  1880. 0x00, 0x00, 0xc0, 0x9f, 0xa0, 0x97, 0x00, 0xa0, 0xcc, 0x3b, 0xbf, 0xfa, 0x08, 0x00, 0x45,
  1881. 0x10, 0x00, 0x3c, 0x46, 0x3c, 0x40, 0x00, 0x40, 0x06, 0x73, 0x1c, 0xc0, 0xa8, 0x01, 0x02,
  1882. 0xc0, 0xa8, 0x01, 0x01, 0x06, 0x0e, 0x00, 0x17, 0x99, 0xc5, 0xa0, 0xec, 0x00, 0x00, 0x00,
  1883. 0x00, 0xa0, 0x02, 0x7d, 0x78, 0xe0, 0xa3, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4, 0x04, 0x02,
  1884. 0x08, 0x0a, 0x00, 0x9c, 0x27, 0x24, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x00,
  1885. ],
  1886. 0x1
  1887. );
  1888. #[rustfmt::skip]
  1889. const PROG_TCP_PORT_80: [u8; 152] = [
  1890. 0x71, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
  1891. 0x71, 0x13, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00,
  1892. 0x67, 0x03, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  1893. 0x4f, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1894. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1895. 0x55, 0x03, 0x0c, 0x00, 0x08, 0x00, 0x00, 0x00,
  1896. 0x71, 0x12, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00,
  1897. 0x55, 0x02, 0x0a, 0x00, 0x06, 0x00, 0x00, 0x00,
  1898. 0x71, 0x12, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00,
  1899. 0x07, 0x01, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00,
  1900. 0x57, 0x02, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00,
  1901. 0x67, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  1902. 0x0f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1903. 0x69, 0x12, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1904. 0x15, 0x02, 0x02, 0x00, 0x00, 0x50, 0x00, 0x00,
  1905. 0x69, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1906. 0x55, 0x01, 0x01, 0x00, 0x00, 0x50, 0x00, 0x00,
  1907. 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  1908. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1909. ];
  1910. #[test]
  1911. fn test_cranelift_tcp_port80_match() {
  1912. let mem = &mut [
  1913. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45,
  1914. 0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
  1915. 0xc0, 0xa8, 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1916. 0x00, 0x50, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1917. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1918. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1919. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1920. ];
  1921. let prog = &PROG_TCP_PORT_80;
  1922. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  1923. vm.cranelift_compile().unwrap();
  1924. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x1);
  1925. }
  1926. #[test]
  1927. fn test_cranelift_tcp_port80_nomatch() {
  1928. let mem = &mut [
  1929. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45,
  1930. 0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
  1931. 0xc0, 0xa8, 0x00, 0x02, 0x00, 0x16, 0x27, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1932. 0x00, 0x51, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1933. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1934. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1935. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1936. ];
  1937. let prog = &PROG_TCP_PORT_80;
  1938. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  1939. vm.cranelift_compile().unwrap();
  1940. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x0);
  1941. }
  1942. #[test]
  1943. fn test_cranelift_tcp_port80_nomatch_ethertype() {
  1944. let mem = &mut [
  1945. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x01, 0x45,
  1946. 0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
  1947. 0xc0, 0xa8, 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1948. 0x00, 0x50, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1949. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1950. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1951. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1952. ];
  1953. let prog = &PROG_TCP_PORT_80;
  1954. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  1955. vm.cranelift_compile().unwrap();
  1956. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x0);
  1957. }
  1958. #[test]
  1959. fn test_cranelift_tcp_port80_nomatch_proto() {
  1960. let mem = &mut [
  1961. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45,
  1962. 0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
  1963. 0xc0, 0xa8, 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1964. 0x00, 0x50, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1965. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1966. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1967. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1968. ];
  1969. let prog = &PROG_TCP_PORT_80;
  1970. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  1971. vm.cranelift_compile().unwrap();
  1972. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x0);
  1973. }
  1974. #[test]
  1975. fn test_cranelift_tcp_sack_match() {
  1976. let mut mem = TCP_SACK_MATCH.to_vec();
  1977. let prog = assemble(TCP_SACK_ASM).unwrap();
  1978. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1979. vm.cranelift_compile().unwrap();
  1980. assert_eq!(
  1981. vm.execute_program_cranelift(mem.as_mut_slice()).unwrap(),
  1982. 0x1
  1983. );
  1984. }
  1985. #[test]
  1986. fn test_cranelift_tcp_sack_nomatch() {
  1987. let mut mem = TCP_SACK_NOMATCH.to_vec();
  1988. let prog = assemble(TCP_SACK_ASM).unwrap();
  1989. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1990. vm.cranelift_compile().unwrap();
  1991. assert_eq!(
  1992. vm.execute_program_cranelift(mem.as_mut_slice()).unwrap(),
  1993. 0x0
  1994. );
  1995. }
  1996. #[test]
  1997. fn test_cranelift_ldabsb() {
  1998. #[rustfmt::skip]
  1999. let prog = &[
  2000. 0x30, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  2001. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  2002. ];
  2003. #[rustfmt::skip]
  2004. let mem = &mut [
  2005. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  2006. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  2007. ];
  2008. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap();
  2009. vm.cranelift_compile().unwrap();
  2010. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x33);
  2011. }
  2012. #[test]
  2013. fn test_cranelift_ldabsh() {
  2014. #[rustfmt::skip]
  2015. let prog = &[
  2016. 0x28, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  2017. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  2018. ];
  2019. #[rustfmt::skip]
  2020. let mem = &mut [
  2021. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  2022. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  2023. ];
  2024. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap();
  2025. vm.cranelift_compile().unwrap();
  2026. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x4433);
  2027. }
  2028. #[test]
  2029. fn test_cranelift_ldabsw() {
  2030. #[rustfmt::skip]
  2031. let prog = &[
  2032. 0x20, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  2033. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  2034. ];
  2035. #[rustfmt::skip]
  2036. let mem = &mut [
  2037. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  2038. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  2039. ];
  2040. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap();
  2041. vm.cranelift_compile().unwrap();
  2042. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x66554433);
  2043. }
  2044. #[test]
  2045. fn test_cranelift_ldabsdw() {
  2046. #[rustfmt::skip]
  2047. let prog = &[
  2048. 0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  2049. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  2050. ];
  2051. #[rustfmt::skip]
  2052. let mem = &mut [
  2053. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  2054. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  2055. ];
  2056. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap();
  2057. vm.cranelift_compile().unwrap();
  2058. assert_eq!(
  2059. vm.execute_program_cranelift(mem).unwrap(),
  2060. 0xaa99887766554433
  2061. );
  2062. }
  2063. #[test]
  2064. fn test_cranelift_ldindb() {
  2065. #[rustfmt::skip]
  2066. let prog = &[
  2067. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  2068. 0x50, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  2069. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  2070. ];
  2071. #[rustfmt::skip]
  2072. let mem = &mut [
  2073. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  2074. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  2075. ];
  2076. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap();
  2077. vm.cranelift_compile().unwrap();
  2078. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x88);
  2079. }
  2080. #[test]
  2081. fn test_cranelift_ldindh() {
  2082. #[rustfmt::skip]
  2083. let prog = &[
  2084. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  2085. 0x48, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  2086. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  2087. ];
  2088. #[rustfmt::skip]
  2089. let mem = &mut [
  2090. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  2091. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  2092. ];
  2093. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap();
  2094. vm.cranelift_compile().unwrap();
  2095. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x9988);
  2096. }
  2097. #[test]
  2098. fn test_cranelift_ldindw() {
  2099. #[rustfmt::skip]
  2100. let prog = &[
  2101. 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  2102. 0x40, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  2103. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  2104. ];
  2105. #[rustfmt::skip]
  2106. let mem = &mut [
  2107. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  2108. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  2109. ];
  2110. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap();
  2111. vm.cranelift_compile().unwrap();
  2112. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x88776655);
  2113. }
  2114. #[test]
  2115. fn test_cranelift_ldinddw() {
  2116. #[rustfmt::skip]
  2117. let prog = &[
  2118. 0xb7, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  2119. 0x58, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  2120. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  2121. ];
  2122. #[rustfmt::skip]
  2123. let mem = &mut [
  2124. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  2125. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  2126. ];
  2127. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap();
  2128. vm.cranelift_compile().unwrap();
  2129. assert_eq!(
  2130. vm.execute_program_cranelift(mem).unwrap(),
  2131. 0xccbbaa9988776655
  2132. );
  2133. }