cranelift.rs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257
  1. // SPDX-License-Identifier: (Apache-2.0 OR MIT)
  2. #![allow(clippy::unreadable_literal)]
  3. #![cfg(feature = "cranelift")]
  4. extern crate rbpf;
  5. mod common;
  6. use rbpf::{assembler::assemble, helpers};
  7. use crate::common::{TCP_SACK_ASM, TCP_SACK_MATCH, TCP_SACK_NOMATCH};
  8. macro_rules! test_cranelift {
  9. ($name:ident, $prog:expr, $expected:expr) => {
  10. #[test]
  11. fn $name() {
  12. let prog = assemble($prog).unwrap();
  13. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  14. vm.cranelift_compile().unwrap();
  15. assert_eq!(vm.execute_program_cranelift().unwrap(), $expected);
  16. }
  17. };
  18. ($name:ident, $prog:expr, $mem:expr, $expected:expr) => {
  19. #[test]
  20. fn $name() {
  21. let prog = assemble($prog).unwrap();
  22. let mem = &mut $mem;
  23. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  24. vm.cranelift_compile().unwrap();
  25. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), $expected);
  26. }
  27. };
  28. }
  29. test_cranelift!(
  30. test_cranelift_add,
  31. "
  32. mov32 r0, 0
  33. mov32 r1, 2
  34. add32 r0, 1
  35. add32 r0, r1
  36. exit
  37. ",
  38. 0x3
  39. );
  40. test_cranelift!(
  41. test_cranelift_alu64_arith,
  42. "
  43. mov r0, 0
  44. mov r1, 1
  45. mov r2, 2
  46. mov r3, 3
  47. mov r4, 4
  48. mov r5, 5
  49. mov r6, 6
  50. mov r7, 7
  51. mov r8, 8
  52. mov r9, 9
  53. add r0, 23
  54. add r0, r7
  55. sub r0, 13
  56. sub r0, r1
  57. mul r0, 7
  58. mul r0, r3
  59. div r0, 2
  60. div r0, r4
  61. exit
  62. ",
  63. 0x2a
  64. );
  65. test_cranelift!(
  66. test_cranelift_alu64_bit,
  67. "
  68. mov r0, 0
  69. mov r1, 1
  70. mov r2, 2
  71. mov r3, 3
  72. mov r4, 4
  73. mov r5, 5
  74. mov r6, 6
  75. mov r7, 7
  76. mov r8, 8
  77. or r0, r5
  78. or r0, 0xa0
  79. and r0, 0xa3
  80. mov r9, 0x91
  81. and r0, r9
  82. lsh r0, 32
  83. lsh r0, 22
  84. lsh r0, r8
  85. rsh r0, 32
  86. rsh r0, 19
  87. rsh r0, r7
  88. xor r0, 0x03
  89. xor r0, r2
  90. exit
  91. ",
  92. 0x11
  93. );
  94. test_cranelift!(
  95. test_cranelift_alu_arith,
  96. "
  97. mov32 r0, 0
  98. mov32 r1, 1
  99. mov32 r2, 2
  100. mov32 r3, 3
  101. mov32 r4, 4
  102. mov32 r5, 5
  103. mov32 r6, 6
  104. mov32 r7, 7
  105. mov32 r8, 8
  106. mov32 r9, 9
  107. add32 r0, 23
  108. add32 r0, r7
  109. sub32 r0, 13
  110. sub32 r0, r1
  111. mul32 r0, 7
  112. mul32 r0, r3
  113. div32 r0, 2
  114. div32 r0, r4
  115. exit
  116. ",
  117. 0x2a
  118. );
  119. test_cranelift!(
  120. test_cranelift_alu_bit,
  121. "
  122. mov32 r0, 0
  123. mov32 r1, 1
  124. mov32 r2, 2
  125. mov32 r3, 3
  126. mov32 r4, 4
  127. mov32 r5, 5
  128. mov32 r6, 6
  129. mov32 r7, 7
  130. mov32 r8, 8
  131. or32 r0, r5
  132. or32 r0, 0xa0
  133. and32 r0, 0xa3
  134. mov32 r9, 0x91
  135. and32 r0, r9
  136. lsh32 r0, 22
  137. lsh32 r0, r8
  138. rsh32 r0, 19
  139. rsh32 r0, r7
  140. xor32 r0, 0x03
  141. xor32 r0, r2
  142. exit
  143. ",
  144. 0x11
  145. );
  146. test_cranelift!(
  147. test_cranelift_arsh32_high_shift,
  148. "
  149. mov r0, 8
  150. lddw r1, 0x100000001
  151. arsh32 r0, r1
  152. exit
  153. ",
  154. 0x4
  155. );
  156. test_cranelift!(
  157. test_cranelift_arsh,
  158. "
  159. mov32 r0, 0xf8
  160. lsh32 r0, 28
  161. arsh32 r0, 16
  162. exit
  163. ",
  164. 0xffff8000
  165. );
  166. test_cranelift!(
  167. test_cranelift_arsh64,
  168. "
  169. mov32 r0, 1
  170. lsh r0, 63
  171. arsh r0, 55
  172. mov32 r1, 5
  173. arsh r0, r1
  174. exit
  175. ",
  176. 0xfffffffffffffff8
  177. );
  178. test_cranelift!(
  179. test_cranelift_arsh_reg,
  180. "
  181. mov32 r0, 0xf8
  182. mov32 r1, 16
  183. lsh32 r0, 28
  184. arsh32 r0, r1
  185. exit
  186. ",
  187. 0xffff8000
  188. );
  189. test_cranelift!(
  190. test_cranelift_be16,
  191. "
  192. ldxh r0, [r1]
  193. be16 r0
  194. exit
  195. ",
  196. [0x11, 0x22],
  197. 0x1122
  198. );
  199. test_cranelift!(
  200. test_cranelift_be16_high,
  201. "
  202. ldxdw r0, [r1]
  203. be16 r0
  204. exit
  205. ",
  206. [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88],
  207. 0x1122
  208. );
  209. test_cranelift!(
  210. test_cranelift_be32,
  211. "
  212. ldxw r0, [r1]
  213. be32 r0
  214. exit
  215. ",
  216. [0x11, 0x22, 0x33, 0x44],
  217. 0x11223344
  218. );
  219. test_cranelift!(
  220. test_cranelift_be32_high,
  221. "
  222. ldxdw r0, [r1]
  223. be32 r0
  224. exit
  225. ",
  226. [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88],
  227. 0x11223344
  228. );
  229. test_cranelift!(
  230. test_cranelift_be64,
  231. "
  232. ldxdw r0, [r1]
  233. be64 r0
  234. exit
  235. ",
  236. [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88],
  237. 0x1122334455667788
  238. );
  239. #[test]
  240. fn test_cranelift_call() {
  241. let prog = assemble(
  242. "
  243. mov r1, 1
  244. mov r2, 2
  245. mov r3, 3
  246. mov r4, 4
  247. mov r5, 5
  248. call 0
  249. exit",
  250. )
  251. .unwrap();
  252. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  253. vm.register_helper(0, helpers::gather_bytes).unwrap();
  254. vm.cranelift_compile().unwrap();
  255. assert_eq!(vm.execute_program_cranelift().unwrap(), 0x0102030405);
  256. }
  257. #[test]
  258. #[should_panic(expected = "[CRANELIFT] Error: unknown helper function (id: 0x3f)")]
  259. fn test_cranelift_err_call_unreg() {
  260. let prog = assemble(
  261. "
  262. mov r1, 1
  263. mov r2, 2
  264. mov r3, 3
  265. mov r4, 4
  266. mov r5, 5
  267. call 63
  268. exit
  269. ",
  270. )
  271. .unwrap();
  272. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  273. vm.cranelift_compile().unwrap();
  274. }
  275. #[test]
  276. fn test_cranelift_call_memfrob() {
  277. let prog = assemble(
  278. "
  279. mov r6, r1
  280. add r1, 2
  281. mov r2, 4
  282. call 1
  283. ldxdw r0, [r6]
  284. be64 r0
  285. exit",
  286. )
  287. .unwrap();
  288. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  289. vm.register_helper(1, helpers::memfrob).unwrap();
  290. let mem = &mut [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];
  291. vm.cranelift_compile().unwrap();
  292. assert_eq!(
  293. vm.execute_program_cranelift(mem).unwrap(),
  294. 0x102292e2f2c0708
  295. );
  296. }
  297. test_cranelift!(
  298. test_cranelift_div32_high_divisor,
  299. "
  300. mov r0, 12
  301. lddw r1, 0x100000004
  302. div32 r0, r1
  303. exit
  304. ",
  305. 0x3
  306. );
  307. test_cranelift!(
  308. test_cranelift_div32_imm,
  309. "
  310. lddw r0, 0x10000000c
  311. div32 r0, 4
  312. exit
  313. ",
  314. 0x3
  315. );
  316. test_cranelift!(
  317. test_cranelift_div32_reg,
  318. "
  319. lddw r0, 0x10000000c
  320. mov r1, 4
  321. div32 r0, r1
  322. exit
  323. ",
  324. 0x3
  325. );
  326. test_cranelift!(
  327. test_cranelift_div64_imm,
  328. "
  329. mov r0, 0xc
  330. lsh r0, 32
  331. div r0, 4
  332. exit
  333. ",
  334. 0x300000000
  335. );
  336. test_cranelift!(
  337. test_cranelift_div64_reg,
  338. "
  339. mov r0, 0xc
  340. lsh r0, 32
  341. mov r1, 4
  342. div r0, r1
  343. exit
  344. ",
  345. 0x300000000
  346. );
  347. test_cranelift!(
  348. test_cranelift_early_exit,
  349. "
  350. mov r0, 3
  351. exit
  352. mov r0, 4
  353. exit
  354. ",
  355. 0x3
  356. );
  357. test_cranelift!(
  358. test_cranelift_div64_by_zero_imm,
  359. "
  360. mov32 r0, 1
  361. div r0, 0
  362. exit
  363. ",
  364. 0x0
  365. );
  366. test_cranelift!(
  367. test_cranelift_div_by_zero_imm,
  368. "
  369. mov32 r0, 1
  370. div32 r0, 0
  371. exit
  372. ",
  373. 0x0
  374. );
  375. test_cranelift!(
  376. test_cranelift_mod64_by_zero_imm,
  377. "
  378. mov32 r0, 1
  379. mod r0, 0
  380. exit
  381. ",
  382. 0x1
  383. );
  384. test_cranelift!(
  385. test_cranelift_mod_by_zero_imm,
  386. "
  387. mov32 r0, 1
  388. mod32 r0, 0
  389. exit
  390. ",
  391. 0x1
  392. );
  393. test_cranelift!(
  394. test_cranelift_div64_by_zero_reg,
  395. "
  396. mov32 r0, 1
  397. mov32 r1, 0
  398. div r0, r1
  399. exit
  400. ",
  401. 0x0
  402. );
  403. test_cranelift!(
  404. test_cranelift_div_by_zero_reg,
  405. "
  406. mov32 r0, 1
  407. mov32 r1, 0
  408. div32 r0, r1
  409. exit
  410. ",
  411. 0x0
  412. );
  413. test_cranelift!(
  414. test_cranelift_mod64_by_zero_reg,
  415. "
  416. mov32 r0, 1
  417. mov32 r1, 0
  418. mod r0, r1
  419. exit
  420. ",
  421. 0x1
  422. );
  423. test_cranelift!(
  424. test_cranelift_mod_by_zero_reg,
  425. "
  426. mov32 r0, 1
  427. mov32 r1, 0
  428. mod32 r0, r1
  429. exit
  430. ",
  431. 0x1
  432. );
  433. #[test]
  434. // #[should_panic(expected = "Error: out of bounds memory store (insn #1)")]
  435. #[ignore = "We have stack OOB checks, but we don't yet catch the trap code and convert it into a panic"]
  436. fn test_cranelift_err_stack_out_of_bound() {
  437. let prog = [
  438. 0x72, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  439. 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. const PROG_TCP_PORT_80: [u8; 152] = [
  1889. 0x71, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x71, 0x13, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00,
  1890. 0x67, 0x03, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x4f, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1891. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x03, 0x0c, 0x00, 0x08, 0x00, 0x00, 0x00,
  1892. 0x71, 0x12, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x02, 0x0a, 0x00, 0x06, 0x00, 0x00, 0x00,
  1893. 0x71, 0x12, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x01, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00,
  1894. 0x57, 0x02, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x67, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  1895. 0x0f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x12, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1896. 0x15, 0x02, 0x02, 0x00, 0x00, 0x50, 0x00, 0x00, 0x69, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1897. 0x55, 0x01, 0x01, 0x00, 0x00, 0x50, 0x00, 0x00, 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  1898. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1899. ];
  1900. #[test]
  1901. fn test_cranelift_tcp_port80_match() {
  1902. let mem = &mut [
  1903. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45,
  1904. 0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
  1905. 0xc0, 0xa8, 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1906. 0x00, 0x50, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1907. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1908. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1909. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1910. ];
  1911. let prog = &PROG_TCP_PORT_80;
  1912. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  1913. vm.cranelift_compile().unwrap();
  1914. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x1);
  1915. }
  1916. #[test]
  1917. fn test_cranelift_tcp_port80_nomatch() {
  1918. let mem = &mut [
  1919. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45,
  1920. 0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
  1921. 0xc0, 0xa8, 0x00, 0x02, 0x00, 0x16, 0x27, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1922. 0x00, 0x51, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1923. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1924. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1925. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1926. ];
  1927. let prog = &PROG_TCP_PORT_80;
  1928. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  1929. vm.cranelift_compile().unwrap();
  1930. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x0);
  1931. }
  1932. #[test]
  1933. fn test_cranelift_tcp_port80_nomatch_ethertype() {
  1934. let mem = &mut [
  1935. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x01, 0x45,
  1936. 0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
  1937. 0xc0, 0xa8, 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1938. 0x00, 0x50, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1939. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1940. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1941. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1942. ];
  1943. let prog = &PROG_TCP_PORT_80;
  1944. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  1945. vm.cranelift_compile().unwrap();
  1946. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x0);
  1947. }
  1948. #[test]
  1949. fn test_cranelift_tcp_port80_nomatch_proto() {
  1950. let mem = &mut [
  1951. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45,
  1952. 0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
  1953. 0xc0, 0xa8, 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1954. 0x00, 0x50, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1955. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1956. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1957. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1958. ];
  1959. let prog = &PROG_TCP_PORT_80;
  1960. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  1961. vm.cranelift_compile().unwrap();
  1962. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x0);
  1963. }
  1964. #[test]
  1965. fn test_cranelift_tcp_sack_match() {
  1966. let mut mem = TCP_SACK_MATCH.to_vec();
  1967. let prog = assemble(TCP_SACK_ASM).unwrap();
  1968. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1969. vm.cranelift_compile().unwrap();
  1970. assert_eq!(
  1971. vm.execute_program_cranelift(mem.as_mut_slice()).unwrap(),
  1972. 0x1
  1973. );
  1974. }
  1975. #[test]
  1976. fn test_cranelift_tcp_sack_nomatch() {
  1977. let mut mem = TCP_SACK_NOMATCH.to_vec();
  1978. let prog = assemble(TCP_SACK_ASM).unwrap();
  1979. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1980. vm.cranelift_compile().unwrap();
  1981. assert_eq!(
  1982. vm.execute_program_cranelift(mem.as_mut_slice()).unwrap(),
  1983. 0x0
  1984. );
  1985. }
  1986. #[test]
  1987. fn test_cranelift_ldabsb() {
  1988. let prog = &[
  1989. 0x30, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1990. 0x00,
  1991. ];
  1992. let mem = &mut [
  1993. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
  1994. 0xff,
  1995. ];
  1996. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap();
  1997. vm.cranelift_compile().unwrap();
  1998. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x33);
  1999. }
  2000. #[test]
  2001. fn test_cranelift_ldabsh() {
  2002. let prog = &[
  2003. 0x28, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2004. 0x00,
  2005. ];
  2006. let mem = &mut [
  2007. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
  2008. 0xff,
  2009. ];
  2010. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap();
  2011. vm.cranelift_compile().unwrap();
  2012. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x4433);
  2013. }
  2014. #[test]
  2015. fn test_cranelift_ldabsw() {
  2016. let prog = &[
  2017. 0x20, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2018. 0x00,
  2019. ];
  2020. let mem = &mut [
  2021. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
  2022. 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(), 0x66554433);
  2027. }
  2028. #[test]
  2029. fn test_cranelift_ldabsdw() {
  2030. let prog = &[
  2031. 0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2032. 0x00,
  2033. ];
  2034. let mem = &mut [
  2035. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
  2036. 0xff,
  2037. ];
  2038. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap();
  2039. vm.cranelift_compile().unwrap();
  2040. assert_eq!(
  2041. vm.execute_program_cranelift(mem).unwrap(),
  2042. 0xaa99887766554433
  2043. );
  2044. }
  2045. #[test]
  2046. fn test_cranelift_ldindb() {
  2047. let prog = &[
  2048. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x50, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00,
  2049. 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2050. ];
  2051. let mem = &mut [
  2052. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
  2053. 0xff,
  2054. ];
  2055. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap();
  2056. vm.cranelift_compile().unwrap();
  2057. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x88);
  2058. }
  2059. #[test]
  2060. fn test_cranelift_ldindh() {
  2061. let prog = &[
  2062. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x48, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00,
  2063. 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2064. ];
  2065. let mem = &mut [
  2066. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
  2067. 0xff,
  2068. ];
  2069. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap();
  2070. vm.cranelift_compile().unwrap();
  2071. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x9988);
  2072. }
  2073. #[test]
  2074. fn test_cranelift_ldindw() {
  2075. let prog = &[
  2076. 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x40, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00,
  2077. 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2078. ];
  2079. let mem = &mut [
  2080. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
  2081. 0xff,
  2082. ];
  2083. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap();
  2084. vm.cranelift_compile().unwrap();
  2085. assert_eq!(vm.execute_program_cranelift(mem).unwrap(), 0x88776655);
  2086. }
  2087. #[test]
  2088. fn test_cranelift_ldinddw() {
  2089. let prog = &[
  2090. 0xb7, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x58, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00,
  2091. 0x00, 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2092. ];
  2093. let mem = &mut [
  2094. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
  2095. 0xff,
  2096. ];
  2097. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(prog), 0x00, 0x08).unwrap();
  2098. vm.cranelift_compile().unwrap();
  2099. assert_eq!(
  2100. vm.execute_program_cranelift(mem).unwrap(),
  2101. 0xccbbaa9988776655
  2102. );
  2103. }