ubpf_jit_x86_64.rs 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891
  1. // SPDX-License-Identifier: (Apache-2.0 OR MIT)
  2. // Converted from the tests for uBPF <https://github.com/iovisor/ubpf>
  3. // Copyright 2015 Big Switch Networks, Inc
  4. // Copyright 2016 6WIND S.A. <[email protected]>
  5. // The tests contained in this file are extracted from the unit tests of uBPF software. Each test
  6. // in this file has a name in the form `test_jit_<name>`, and corresponds to the (human-readable)
  7. // code in `ubpf/tree/master/tests/<name>`, available at
  8. // <https://github.com/iovisor/ubpf/tree/master/tests> (hyphen had to be replaced with underscores
  9. // as Rust will not accept them in function names). It is strongly advised to refer to the uBPF
  10. // version to understand what these program do.
  11. //
  12. // Each program was assembled from the uBPF version with the assembler provided by uBPF itself, and
  13. // available at <https://github.com/iovisor/ubpf/tree/master/ubpf>.
  14. // The very few modifications that have been realized should be indicated.
  15. // These are unit tests for the eBPF JIT compiler.
  16. #![allow(clippy::unreadable_literal)]
  17. #![cfg(all(not(windows), feature = "std"))]
  18. extern crate rbpf;
  19. mod common;
  20. use common::{TCP_SACK_ASM, TCP_SACK_MATCH, TCP_SACK_NOMATCH};
  21. use rbpf::{assembler::assemble, helpers};
  22. #[test]
  23. fn test_jit_add() {
  24. let prog = assemble(
  25. "
  26. mov32 r0, 0
  27. mov32 r1, 2
  28. add32 r0, 1
  29. add32 r0, r1
  30. exit",
  31. )
  32. .unwrap();
  33. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  34. vm.jit_compile().unwrap();
  35. unsafe {
  36. assert_eq!(vm.execute_program_jit().unwrap(), 0x3);
  37. }
  38. }
  39. #[test]
  40. fn test_jit_alu64_arith() {
  41. let prog = assemble(
  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. .unwrap();
  64. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  65. vm.jit_compile().unwrap();
  66. unsafe {
  67. assert_eq!(vm.execute_program_jit().unwrap(), 0x2a);
  68. }
  69. }
  70. #[test]
  71. fn test_jit_alu64_bit() {
  72. let prog = assemble(
  73. "
  74. mov r0, 0
  75. mov r1, 1
  76. mov r2, 2
  77. mov r3, 3
  78. mov r4, 4
  79. mov r5, 5
  80. mov r6, 6
  81. mov r7, 7
  82. mov r8, 8
  83. or r0, r5
  84. or r0, 0xa0
  85. and r0, 0xa3
  86. mov r9, 0x91
  87. and r0, r9
  88. lsh r0, 32
  89. lsh r0, 22
  90. lsh r0, r8
  91. rsh r0, 32
  92. rsh r0, 19
  93. rsh r0, r7
  94. xor r0, 0x03
  95. xor r0, r2
  96. exit",
  97. )
  98. .unwrap();
  99. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  100. vm.jit_compile().unwrap();
  101. unsafe {
  102. assert_eq!(vm.execute_program_jit().unwrap(), 0x11);
  103. }
  104. }
  105. #[test]
  106. fn test_jit_alu_arith() {
  107. let prog = assemble(
  108. "
  109. mov32 r0, 0
  110. mov32 r1, 1
  111. mov32 r2, 2
  112. mov32 r3, 3
  113. mov32 r4, 4
  114. mov32 r5, 5
  115. mov32 r6, 6
  116. mov32 r7, 7
  117. mov32 r8, 8
  118. mov32 r9, 9
  119. add32 r0, 23
  120. add32 r0, r7
  121. sub32 r0, 13
  122. sub32 r0, r1
  123. mul32 r0, 7
  124. mul32 r0, r3
  125. div32 r0, 2
  126. div32 r0, r4
  127. exit",
  128. )
  129. .unwrap();
  130. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  131. vm.jit_compile().unwrap();
  132. unsafe {
  133. assert_eq!(vm.execute_program_jit().unwrap(), 0x2a);
  134. }
  135. }
  136. #[test]
  137. fn test_jit_alu_bit() {
  138. let prog = assemble(
  139. "
  140. mov32 r0, 0
  141. mov32 r1, 1
  142. mov32 r2, 2
  143. mov32 r3, 3
  144. mov32 r4, 4
  145. mov32 r5, 5
  146. mov32 r6, 6
  147. mov32 r7, 7
  148. mov32 r8, 8
  149. or32 r0, r5
  150. or32 r0, 0xa0
  151. and32 r0, 0xa3
  152. mov32 r9, 0x91
  153. and32 r0, r9
  154. lsh32 r0, 22
  155. lsh32 r0, r8
  156. rsh32 r0, 19
  157. rsh32 r0, r7
  158. xor32 r0, 0x03
  159. xor32 r0, r2
  160. exit",
  161. )
  162. .unwrap();
  163. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  164. vm.jit_compile().unwrap();
  165. unsafe {
  166. assert_eq!(vm.execute_program_jit().unwrap(), 0x11);
  167. }
  168. }
  169. #[test]
  170. fn test_jit_arsh32_high_shift() {
  171. let prog = assemble(
  172. "
  173. mov r0, 8
  174. lddw r1, 0x100000001
  175. arsh32 r0, r1
  176. exit",
  177. )
  178. .unwrap();
  179. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  180. vm.jit_compile().unwrap();
  181. unsafe {
  182. assert_eq!(vm.execute_program_jit().unwrap(), 0x4);
  183. }
  184. }
  185. #[test]
  186. fn test_jit_arsh() {
  187. let prog = assemble(
  188. "
  189. mov32 r0, 0xf8
  190. lsh32 r0, 28
  191. arsh32 r0, 16
  192. exit",
  193. )
  194. .unwrap();
  195. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  196. vm.jit_compile().unwrap();
  197. unsafe {
  198. assert_eq!(vm.execute_program_jit().unwrap(), 0xffff8000);
  199. }
  200. }
  201. #[test]
  202. fn test_jit_arsh64() {
  203. let prog = assemble(
  204. "
  205. mov32 r0, 1
  206. lsh r0, 63
  207. arsh r0, 55
  208. mov32 r1, 5
  209. arsh r0, r1
  210. exit",
  211. )
  212. .unwrap();
  213. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  214. vm.jit_compile().unwrap();
  215. unsafe {
  216. assert_eq!(vm.execute_program_jit().unwrap(), 0xfffffffffffffff8);
  217. }
  218. }
  219. #[test]
  220. fn test_jit_arsh_reg() {
  221. let prog = assemble(
  222. "
  223. mov32 r0, 0xf8
  224. mov32 r1, 16
  225. lsh32 r0, 28
  226. arsh32 r0, r1
  227. exit",
  228. )
  229. .unwrap();
  230. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  231. vm.jit_compile().unwrap();
  232. unsafe {
  233. assert_eq!(vm.execute_program_jit().unwrap(), 0xffff8000);
  234. }
  235. }
  236. #[test]
  237. fn test_jit_be16() {
  238. let prog = assemble(
  239. "
  240. ldxh r0, [r1]
  241. be16 r0
  242. exit",
  243. )
  244. .unwrap();
  245. let mem = &mut [0x11, 0x22];
  246. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  247. vm.jit_compile().unwrap();
  248. unsafe {
  249. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1122);
  250. }
  251. }
  252. #[test]
  253. fn test_jit_be16_high() {
  254. let prog = assemble(
  255. "
  256. ldxdw r0, [r1]
  257. be16 r0
  258. exit",
  259. )
  260. .unwrap();
  261. let mem = &mut [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88];
  262. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  263. vm.jit_compile().unwrap();
  264. unsafe {
  265. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1122);
  266. }
  267. }
  268. #[test]
  269. fn test_jit_be32() {
  270. let prog = assemble(
  271. "
  272. ldxw r0, [r1]
  273. be32 r0
  274. exit",
  275. )
  276. .unwrap();
  277. let mem = &mut [0x11, 0x22, 0x33, 0x44];
  278. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  279. vm.jit_compile().unwrap();
  280. unsafe {
  281. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11223344);
  282. }
  283. }
  284. #[test]
  285. fn test_jit_be32_high() {
  286. let prog = assemble(
  287. "
  288. ldxdw r0, [r1]
  289. be32 r0
  290. exit",
  291. )
  292. .unwrap();
  293. let mem = &mut [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88];
  294. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  295. vm.jit_compile().unwrap();
  296. unsafe {
  297. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11223344);
  298. }
  299. }
  300. #[test]
  301. fn test_jit_be64() {
  302. let prog = assemble(
  303. "
  304. ldxdw r0, [r1]
  305. be64 r0
  306. exit",
  307. )
  308. .unwrap();
  309. let mem = &mut [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88];
  310. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  311. vm.jit_compile().unwrap();
  312. unsafe {
  313. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1122334455667788);
  314. }
  315. }
  316. #[test]
  317. fn test_jit_call() {
  318. let prog = assemble(
  319. "
  320. mov r1, 1
  321. mov r2, 2
  322. mov r3, 3
  323. mov r4, 4
  324. mov r5, 5
  325. call 0
  326. exit",
  327. )
  328. .unwrap();
  329. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  330. vm.register_helper(0, helpers::gather_bytes).unwrap();
  331. vm.jit_compile().unwrap();
  332. unsafe {
  333. assert_eq!(vm.execute_program_jit().unwrap(), 0x0102030405);
  334. }
  335. }
  336. #[test]
  337. fn test_jit_call_memfrob() {
  338. let prog = assemble(
  339. "
  340. mov r6, r1
  341. add r1, 2
  342. mov r2, 4
  343. call 1
  344. ldxdw r0, [r6]
  345. be64 r0
  346. exit",
  347. )
  348. .unwrap();
  349. let mem = &mut [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];
  350. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  351. vm.register_helper(1, helpers::memfrob).unwrap();
  352. vm.jit_compile().unwrap();
  353. unsafe {
  354. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x102292e2f2c0708);
  355. }
  356. }
  357. // TODO: helpers::trash_registers needs asm!().
  358. // Try this again once asm!() is available in stable.
  359. //#[test]
  360. //fn test_jit_call_save() {
  361. //let prog = &[
  362. //0xb7, 0x06, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  363. //0xb7, 0x07, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  364. //0xb7, 0x08, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
  365. //0xb7, 0x09, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00,
  366. //0x85, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  367. //0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  368. //0x4f, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  369. //0x4f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  370. //0x4f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  371. //0x4f, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  372. //0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  373. //];
  374. //let mut vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  375. //vm.register_helper(2, helpers::trash_registers);
  376. //vm.jit_compile().unwrap();
  377. //unsafe { assert_eq!(vm.execute_program_jit().unwrap(), 0x4321); }
  378. //}
  379. #[test]
  380. fn test_jit_div32_high_divisor() {
  381. let prog = assemble(
  382. "
  383. mov r0, 12
  384. lddw r1, 0x100000004
  385. div32 r0, r1
  386. exit",
  387. )
  388. .unwrap();
  389. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  390. vm.jit_compile().unwrap();
  391. unsafe {
  392. assert_eq!(vm.execute_program_jit().unwrap(), 0x3);
  393. }
  394. }
  395. #[test]
  396. fn test_jit_div32_imm() {
  397. let prog = assemble(
  398. "
  399. lddw r0, 0x10000000c
  400. div32 r0, 4
  401. exit",
  402. )
  403. .unwrap();
  404. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  405. vm.jit_compile().unwrap();
  406. unsafe {
  407. assert_eq!(vm.execute_program_jit().unwrap(), 0x3);
  408. }
  409. }
  410. #[test]
  411. fn test_jit_div32_reg() {
  412. let prog = assemble(
  413. "
  414. lddw r0, 0x10000000c
  415. mov r1, 4
  416. div32 r0, r1
  417. exit",
  418. )
  419. .unwrap();
  420. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  421. vm.jit_compile().unwrap();
  422. unsafe {
  423. assert_eq!(vm.execute_program_jit().unwrap(), 0x3);
  424. }
  425. }
  426. #[test]
  427. fn test_jit_div64_imm() {
  428. let prog = assemble(
  429. "
  430. mov r0, 0xc
  431. lsh r0, 32
  432. div r0, 4
  433. exit",
  434. )
  435. .unwrap();
  436. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  437. vm.jit_compile().unwrap();
  438. unsafe {
  439. assert_eq!(vm.execute_program_jit().unwrap(), 0x300000000);
  440. }
  441. }
  442. #[test]
  443. fn test_jit_div64_reg() {
  444. let prog = assemble(
  445. "
  446. mov r0, 0xc
  447. lsh r0, 32
  448. mov r1, 4
  449. div r0, r1
  450. exit",
  451. )
  452. .unwrap();
  453. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  454. vm.jit_compile().unwrap();
  455. unsafe {
  456. assert_eq!(vm.execute_program_jit().unwrap(), 0x300000000);
  457. }
  458. }
  459. // For some register numbers, we don't emit the same instructions for handling divisions by zero,
  460. // which means we don't use the same offset to skip these instructions when the divisor is not
  461. // zero. We've had a regression because of this before, make sure we test it.
  462. #[test]
  463. fn test_jit_div32_highreg() {
  464. let prog = assemble(
  465. "
  466. mov r0, 2
  467. mov r7, 4
  468. div32 r7, r0
  469. exit",
  470. )
  471. .unwrap();
  472. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  473. vm.jit_compile().unwrap();
  474. unsafe {
  475. assert_eq!(vm.execute_program_jit().unwrap(), 0x2);
  476. }
  477. }
  478. #[test]
  479. fn test_jit_div64_highreg() {
  480. let prog = assemble(
  481. "
  482. mov r0, 2
  483. mov r7, 4
  484. div r7, r0
  485. exit",
  486. )
  487. .unwrap();
  488. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  489. vm.jit_compile().unwrap();
  490. unsafe {
  491. assert_eq!(vm.execute_program_jit().unwrap(), 0x2);
  492. }
  493. }
  494. #[test]
  495. fn test_jit_early_exit() {
  496. let prog = assemble(
  497. "
  498. mov r0, 3
  499. exit
  500. mov r0, 4
  501. exit",
  502. )
  503. .unwrap();
  504. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  505. vm.jit_compile().unwrap();
  506. unsafe {
  507. assert_eq!(vm.execute_program_jit().unwrap(), 0x3);
  508. }
  509. }
  510. // uBPF limits the number of user functions at 64. We don't.
  511. //#[test]
  512. //fn test_jit_err_call_bad_imm() {
  513. //}
  514. #[test]
  515. #[should_panic(expected = "[JIT] Error: unknown helper function (id: 0x3f)")]
  516. fn test_jit_err_call_unreg() {
  517. let prog = assemble(
  518. "
  519. mov r1, 1
  520. mov r2, 2
  521. mov r3, 3
  522. mov r4, 4
  523. mov r5, 5
  524. call 63
  525. exit",
  526. )
  527. .unwrap();
  528. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  529. vm.jit_compile().unwrap();
  530. unsafe {
  531. vm.execute_program_jit().unwrap();
  532. }
  533. }
  534. #[test]
  535. fn test_jit_div64_by_zero_imm() {
  536. let prog = assemble(
  537. "
  538. mov32 r0, 1
  539. div r0, 0
  540. exit",
  541. )
  542. .unwrap();
  543. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  544. vm.jit_compile().unwrap();
  545. unsafe {
  546. assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
  547. }
  548. }
  549. #[test]
  550. fn test_jit_div_by_zero_imm() {
  551. let prog = assemble(
  552. "
  553. mov32 r0, 1
  554. div32 r0, 0
  555. exit",
  556. )
  557. .unwrap();
  558. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  559. vm.jit_compile().unwrap();
  560. unsafe {
  561. assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
  562. }
  563. }
  564. #[test]
  565. fn test_jit_mod64_by_zero_imm() {
  566. let prog = assemble(
  567. "
  568. mov32 r0, 1
  569. mod r0, 0
  570. exit",
  571. )
  572. .unwrap();
  573. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  574. vm.jit_compile().unwrap();
  575. unsafe {
  576. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  577. }
  578. }
  579. #[test]
  580. fn test_jit_mod_by_zero_imm() {
  581. let prog = assemble(
  582. "
  583. mov32 r0, 1
  584. mod32 r0, 0
  585. exit",
  586. )
  587. .unwrap();
  588. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  589. vm.jit_compile().unwrap();
  590. unsafe {
  591. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  592. }
  593. }
  594. #[test]
  595. fn test_jit_div64_by_zero_reg() {
  596. let prog = assemble(
  597. "
  598. mov32 r0, 1
  599. mov32 r1, 0
  600. div r0, r1
  601. exit",
  602. )
  603. .unwrap();
  604. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  605. vm.jit_compile().unwrap();
  606. unsafe {
  607. assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
  608. }
  609. }
  610. #[test]
  611. fn test_jit_div_by_zero_reg() {
  612. let prog = assemble(
  613. "
  614. mov32 r0, 1
  615. mov32 r1, 0
  616. div32 r0, r1
  617. exit",
  618. )
  619. .unwrap();
  620. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  621. vm.jit_compile().unwrap();
  622. unsafe {
  623. assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
  624. }
  625. }
  626. #[test]
  627. fn test_jit_mod64_by_zero_reg() {
  628. let prog = assemble(
  629. "
  630. mov32 r0, 1
  631. mov32 r1, 0
  632. mod r0, r1
  633. exit",
  634. )
  635. .unwrap();
  636. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  637. vm.jit_compile().unwrap();
  638. unsafe {
  639. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  640. }
  641. }
  642. #[test]
  643. fn test_jit_mod_by_zero_reg() {
  644. let prog = assemble(
  645. "
  646. mov32 r0, 1
  647. mov32 r1, 0
  648. mod32 r0, r1
  649. exit",
  650. )
  651. .unwrap();
  652. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  653. vm.jit_compile().unwrap();
  654. unsafe {
  655. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  656. }
  657. }
  658. // TODO SKIP: JIT disabled for this testcase (stack oob check not implemented)
  659. // #[test]
  660. // #[should_panic(expected = "Error: out of bounds memory store (insn #1)")]
  661. // fn test_jit_err_stack_out_of_bound() {
  662. // let prog = &[
  663. // 0x72, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  664. // 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  665. // ];
  666. // let mut vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  667. // vm.jit_compile().unwrap();
  668. // unsafe { vm.execute_program_jit().unwrap(); }
  669. // }
  670. #[test]
  671. fn test_jit_exit() {
  672. let prog = assemble(
  673. "
  674. mov r0, 0
  675. exit",
  676. )
  677. .unwrap();
  678. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  679. vm.jit_compile().unwrap();
  680. unsafe {
  681. assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
  682. }
  683. }
  684. #[test]
  685. fn test_jit_ja() {
  686. let prog = assemble(
  687. "
  688. mov r0, 1
  689. ja +1
  690. mov r0, 2
  691. exit",
  692. )
  693. .unwrap();
  694. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  695. vm.jit_compile().unwrap();
  696. unsafe {
  697. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  698. }
  699. }
  700. #[test]
  701. fn test_jit_jeq_imm() {
  702. let prog = assemble(
  703. "
  704. mov32 r0, 0
  705. mov32 r1, 0xa
  706. jeq r1, 0xb, +4
  707. mov32 r0, 1
  708. mov32 r1, 0xb
  709. jeq r1, 0xb, +1
  710. mov32 r0, 2
  711. exit",
  712. )
  713. .unwrap();
  714. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  715. vm.jit_compile().unwrap();
  716. unsafe {
  717. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  718. }
  719. }
  720. #[test]
  721. fn test_jit_jeq_reg() {
  722. let prog = assemble(
  723. "
  724. mov32 r0, 0
  725. mov32 r1, 0xa
  726. mov32 r2, 0xb
  727. jeq r1, r2, +4
  728. mov32 r0, 1
  729. mov32 r1, 0xb
  730. jeq r1, r2, +1
  731. mov32 r0, 2
  732. exit",
  733. )
  734. .unwrap();
  735. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  736. vm.jit_compile().unwrap();
  737. unsafe {
  738. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  739. }
  740. }
  741. #[test]
  742. fn test_jit_jge_imm() {
  743. let prog = assemble(
  744. "
  745. mov32 r0, 0
  746. mov32 r1, 0xa
  747. jge r1, 0xb, +4
  748. mov32 r0, 1
  749. mov32 r1, 0xc
  750. jge r1, 0xb, +1
  751. mov32 r0, 2
  752. exit",
  753. )
  754. .unwrap();
  755. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  756. vm.jit_compile().unwrap();
  757. unsafe {
  758. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  759. }
  760. }
  761. #[test]
  762. fn test_jit_jle_imm() {
  763. let prog = assemble(
  764. "
  765. mov32 r0, 0
  766. mov32 r1, 5
  767. jle r1, 4, +1
  768. jle r1, 6, +1
  769. exit
  770. jle r1, 5, +1
  771. exit
  772. mov32 r0, 1
  773. exit",
  774. )
  775. .unwrap();
  776. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  777. vm.jit_compile().unwrap();
  778. unsafe {
  779. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  780. }
  781. }
  782. #[test]
  783. fn test_jit_jle_reg() {
  784. let prog = assemble(
  785. "
  786. mov r0, 0
  787. mov r1, 5
  788. mov r2, 4
  789. mov r3, 6
  790. jle r1, r2, +2
  791. jle r1, r1, +1
  792. exit
  793. jle r1, r3, +1
  794. exit
  795. mov r0, 1
  796. exit",
  797. )
  798. .unwrap();
  799. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  800. vm.jit_compile().unwrap();
  801. unsafe {
  802. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  803. }
  804. }
  805. #[test]
  806. fn test_jit_jgt_imm() {
  807. let prog = assemble(
  808. "
  809. mov32 r0, 0
  810. mov32 r1, 5
  811. jgt r1, 6, +2
  812. jgt r1, 5, +1
  813. jgt r1, 4, +1
  814. exit
  815. mov32 r0, 1
  816. exit",
  817. )
  818. .unwrap();
  819. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  820. vm.jit_compile().unwrap();
  821. unsafe {
  822. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  823. }
  824. }
  825. #[test]
  826. fn test_jit_jgt_reg() {
  827. let prog = assemble(
  828. "
  829. mov r0, 0
  830. mov r1, 5
  831. mov r2, 6
  832. mov r3, 4
  833. jgt r1, r2, +2
  834. jgt r1, r1, +1
  835. jgt r1, r3, +1
  836. exit
  837. mov r0, 1
  838. exit",
  839. )
  840. .unwrap();
  841. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  842. vm.jit_compile().unwrap();
  843. unsafe {
  844. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  845. }
  846. }
  847. #[test]
  848. fn test_jit_jlt_imm() {
  849. let prog = assemble(
  850. "
  851. mov32 r0, 0
  852. mov32 r1, 5
  853. jlt r1, 4, +2
  854. jlt r1, 5, +1
  855. jlt r1, 6, +1
  856. exit
  857. mov32 r0, 1
  858. exit",
  859. )
  860. .unwrap();
  861. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  862. vm.jit_compile().unwrap();
  863. unsafe {
  864. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  865. }
  866. }
  867. #[test]
  868. fn test_jit_jlt_reg() {
  869. let prog = assemble(
  870. "
  871. mov r0, 0
  872. mov r1, 5
  873. mov r2, 4
  874. mov r3, 6
  875. jlt r1, r2, +2
  876. jlt r1, r1, +1
  877. jlt r1, r3, +1
  878. exit
  879. mov r0, 1
  880. exit",
  881. )
  882. .unwrap();
  883. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  884. vm.jit_compile().unwrap();
  885. unsafe {
  886. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  887. }
  888. }
  889. #[test]
  890. fn test_jit_jit_bounce() {
  891. let prog = assemble(
  892. "
  893. mov r0, 1
  894. mov r6, r0
  895. mov r7, r6
  896. mov r8, r7
  897. mov r9, r8
  898. mov r0, r9
  899. exit",
  900. )
  901. .unwrap();
  902. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  903. vm.jit_compile().unwrap();
  904. unsafe {
  905. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  906. }
  907. }
  908. #[test]
  909. fn test_jit_jne_reg() {
  910. let prog = assemble(
  911. "
  912. mov32 r0, 0
  913. mov32 r1, 0xb
  914. mov32 r2, 0xb
  915. jne r1, r2, +4
  916. mov32 r0, 1
  917. mov32 r1, 0xa
  918. jne r1, r2, +1
  919. mov32 r0, 2
  920. exit",
  921. )
  922. .unwrap();
  923. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  924. vm.jit_compile().unwrap();
  925. unsafe {
  926. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  927. }
  928. }
  929. #[test]
  930. fn test_jit_jset_imm() {
  931. let prog = assemble(
  932. "
  933. mov32 r0, 0
  934. mov32 r1, 0x7
  935. jset r1, 0x8, +4
  936. mov32 r0, 1
  937. mov32 r1, 0x9
  938. jset r1, 0x8, +1
  939. mov32 r0, 2
  940. exit",
  941. )
  942. .unwrap();
  943. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  944. vm.jit_compile().unwrap();
  945. unsafe {
  946. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  947. }
  948. }
  949. #[test]
  950. fn test_jit_jset_reg() {
  951. let prog = assemble(
  952. "
  953. mov32 r0, 0
  954. mov32 r1, 0x7
  955. mov32 r2, 0x8
  956. jset r1, r2, +4
  957. mov32 r0, 1
  958. mov32 r1, 0x9
  959. jset r1, r2, +1
  960. mov32 r0, 2
  961. exit",
  962. )
  963. .unwrap();
  964. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  965. vm.jit_compile().unwrap();
  966. unsafe {
  967. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  968. }
  969. }
  970. #[test]
  971. fn test_jit_jsge_imm() {
  972. let prog = assemble(
  973. "
  974. mov32 r0, 0
  975. mov r1, -2
  976. jsge r1, -1, +5
  977. jsge r1, 0, +4
  978. mov32 r0, 1
  979. mov r1, -1
  980. jsge r1, -1, +1
  981. mov32 r0, 2
  982. exit",
  983. )
  984. .unwrap();
  985. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  986. vm.jit_compile().unwrap();
  987. unsafe {
  988. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  989. }
  990. }
  991. #[test]
  992. fn test_jit_jsge_reg() {
  993. let prog = assemble(
  994. "
  995. mov32 r0, 0
  996. mov r1, -2
  997. mov r2, -1
  998. mov32 r3, 0
  999. jsge r1, r2, +5
  1000. jsge r1, r3, +4
  1001. mov32 r0, 1
  1002. mov r1, r2
  1003. jsge r1, r2, +1
  1004. mov32 r0, 2
  1005. exit",
  1006. )
  1007. .unwrap();
  1008. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1009. vm.jit_compile().unwrap();
  1010. unsafe {
  1011. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1012. }
  1013. }
  1014. #[test]
  1015. fn test_jit_jsle_imm() {
  1016. let prog = assemble(
  1017. "
  1018. mov32 r0, 0
  1019. mov r1, -2
  1020. jsle r1, -3, +1
  1021. jsle r1, -1, +1
  1022. exit
  1023. mov32 r0, 1
  1024. jsle r1, -2, +1
  1025. mov32 r0, 2
  1026. exit",
  1027. )
  1028. .unwrap();
  1029. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1030. vm.jit_compile().unwrap();
  1031. unsafe {
  1032. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1033. }
  1034. }
  1035. #[test]
  1036. fn test_jit_jsle_reg() {
  1037. let prog = assemble(
  1038. "
  1039. mov32 r0, 0
  1040. mov r1, -1
  1041. mov r2, -2
  1042. mov32 r3, 0
  1043. jsle r1, r2, +1
  1044. jsle r1, r3, +1
  1045. exit
  1046. mov32 r0, 1
  1047. mov r1, r2
  1048. jsle r1, r2, +1
  1049. mov32 r0, 2
  1050. exit",
  1051. )
  1052. .unwrap();
  1053. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1054. vm.jit_compile().unwrap();
  1055. unsafe {
  1056. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1057. }
  1058. }
  1059. #[test]
  1060. fn test_jit_jsgt_imm() {
  1061. let prog = assemble(
  1062. "
  1063. mov32 r0, 0
  1064. mov r1, -2
  1065. jsgt r1, -1, +4
  1066. mov32 r0, 1
  1067. mov32 r1, 0
  1068. jsgt r1, -1, +1
  1069. mov32 r0, 2
  1070. exit",
  1071. )
  1072. .unwrap();
  1073. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1074. vm.jit_compile().unwrap();
  1075. unsafe {
  1076. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1077. }
  1078. }
  1079. #[test]
  1080. fn test_jit_jsgt_reg() {
  1081. let prog = assemble(
  1082. "
  1083. mov32 r0, 0
  1084. mov r1, -2
  1085. mov r2, -1
  1086. jsgt r1, r2, +4
  1087. mov32 r0, 1
  1088. mov32 r1, 0
  1089. jsgt r1, r2, +1
  1090. mov32 r0, 2
  1091. exit",
  1092. )
  1093. .unwrap();
  1094. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1095. vm.jit_compile().unwrap();
  1096. unsafe {
  1097. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1098. }
  1099. }
  1100. #[test]
  1101. fn test_jit_jslt_imm() {
  1102. let prog = assemble(
  1103. "
  1104. mov32 r0, 0
  1105. mov r1, -2
  1106. jslt r1, -3, +2
  1107. jslt r1, -2, +1
  1108. jslt r1, -1, +1
  1109. exit
  1110. mov32 r0, 1
  1111. exit",
  1112. )
  1113. .unwrap();
  1114. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1115. vm.jit_compile().unwrap();
  1116. unsafe {
  1117. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1118. }
  1119. }
  1120. #[test]
  1121. fn test_jit_jslt_reg() {
  1122. let prog = assemble(
  1123. "
  1124. mov32 r0, 0
  1125. mov r1, -2
  1126. mov r2, -3
  1127. mov r3, -1
  1128. jslt r1, r1, +2
  1129. jslt r1, r2, +1
  1130. jslt r1, r3, +1
  1131. exit
  1132. mov32 r0, 1
  1133. exit",
  1134. )
  1135. .unwrap();
  1136. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1137. vm.jit_compile().unwrap();
  1138. unsafe {
  1139. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1140. }
  1141. }
  1142. #[test]
  1143. fn test_jit_jeq32_imm() {
  1144. let prog = assemble(
  1145. "
  1146. mov r9, 1
  1147. lsh r9, 32
  1148. mov32 r0, 0x0
  1149. mov32 r1, 0xa
  1150. jeq32 r1, 0xb, +5
  1151. mov32 r0, 1
  1152. mov r1, 0xb
  1153. or r1, r9
  1154. jeq32 r1, 0xb, +1
  1155. mov32 r0, 2
  1156. exit",
  1157. )
  1158. .unwrap();
  1159. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1160. vm.jit_compile().unwrap();
  1161. unsafe {
  1162. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1163. }
  1164. }
  1165. #[test]
  1166. fn test_jit_jeq32_reg() {
  1167. let prog = assemble(
  1168. "
  1169. mov r9, 1
  1170. lsh r9, 32
  1171. mov32 r0, 0
  1172. mov32 r1, 0xa
  1173. mov32 r2, 0xb
  1174. jeq32 r1, r2, +5
  1175. mov32 r0, 1
  1176. mov32 r1, 0xb
  1177. or r1, r9
  1178. jeq32 r1, r2, +1
  1179. mov32 r0, 2
  1180. exit",
  1181. )
  1182. .unwrap();
  1183. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1184. vm.jit_compile().unwrap();
  1185. unsafe {
  1186. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1187. }
  1188. }
  1189. #[test]
  1190. fn test_jit_jge32_imm() {
  1191. let prog = assemble(
  1192. "
  1193. mov r9, 1
  1194. lsh r9, 32
  1195. mov32 r0, 0
  1196. mov32 r1, 0xa
  1197. jge32 r1, 0xb, +5
  1198. mov32 r0, 1
  1199. or r1, r9
  1200. mov32 r1, 0xc
  1201. jge32 r1, 0xb, +1
  1202. mov32 r0, 2
  1203. exit",
  1204. )
  1205. .unwrap();
  1206. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1207. vm.jit_compile().unwrap();
  1208. unsafe {
  1209. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1210. }
  1211. }
  1212. #[test]
  1213. fn test_jit_jge32_reg() {
  1214. let prog = assemble(
  1215. "
  1216. mov r9, 1
  1217. lsh r9, 32
  1218. mov32 r0, 0
  1219. mov32 r1, 0xa
  1220. mov32 r2, 0xb
  1221. jge32 r1, r2, +5
  1222. mov32 r0, 1
  1223. or r1, r9
  1224. mov32 r1, 0xc
  1225. jge32 r1, r2, +1
  1226. mov32 r0, 2
  1227. exit",
  1228. )
  1229. .unwrap();
  1230. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1231. vm.jit_compile().unwrap();
  1232. unsafe {
  1233. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1234. }
  1235. }
  1236. #[test]
  1237. fn test_jit_jgt32_imm() {
  1238. let prog = assemble(
  1239. "
  1240. mov r9, 1
  1241. lsh r9, 32
  1242. mov32 r0, 0
  1243. mov32 r1, 5
  1244. or r1, r9
  1245. jgt32 r1, 6, +4
  1246. jgt32 r1, 5, +3
  1247. jgt32 r1, 4, +1
  1248. exit
  1249. mov32 r0, 1
  1250. exit",
  1251. )
  1252. .unwrap();
  1253. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1254. vm.jit_compile().unwrap();
  1255. unsafe {
  1256. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1257. }
  1258. }
  1259. #[test]
  1260. fn test_jit_jgt32_reg() {
  1261. let prog = assemble(
  1262. "
  1263. mov r9, 1
  1264. lsh r9, 32
  1265. mov r0, 0
  1266. mov r1, 5
  1267. mov32 r1, 5
  1268. or r1, r9
  1269. mov r2, 6
  1270. mov r3, 4
  1271. jgt32 r1, r2, +4
  1272. jgt32 r1, r1, +3
  1273. jgt32 r1, r3, +1
  1274. exit
  1275. mov r0, 1
  1276. exit",
  1277. )
  1278. .unwrap();
  1279. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1280. vm.jit_compile().unwrap();
  1281. unsafe {
  1282. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1283. }
  1284. }
  1285. #[test]
  1286. fn test_jit_jle32_imm() {
  1287. let prog = assemble(
  1288. "
  1289. mov r9, 1
  1290. lsh r9, 32
  1291. mov32 r0, 0
  1292. mov32 r1, 5
  1293. or r1, r9
  1294. jle32 r1, 4, +5
  1295. jle32 r1, 6, +1
  1296. exit
  1297. jle32 r1, 5, +1
  1298. exit
  1299. mov32 r0, 1
  1300. exit",
  1301. )
  1302. .unwrap();
  1303. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1304. vm.jit_compile().unwrap();
  1305. unsafe {
  1306. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1307. }
  1308. }
  1309. #[test]
  1310. fn test_jit_jle32_reg() {
  1311. let prog = assemble(
  1312. "
  1313. mov r9, 1
  1314. lsh r9, 32
  1315. mov r0, 0
  1316. mov r1, 5
  1317. mov r2, 4
  1318. mov r3, 6
  1319. or r1, r9
  1320. jle32 r1, r2, +5
  1321. jle32 r1, r1, +1
  1322. exit
  1323. jle32 r1, r3, +1
  1324. exit
  1325. mov r0, 1
  1326. exit",
  1327. )
  1328. .unwrap();
  1329. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1330. vm.jit_compile().unwrap();
  1331. unsafe {
  1332. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1333. }
  1334. }
  1335. #[test]
  1336. fn test_jit_jlt32_imm() {
  1337. let prog = assemble(
  1338. "
  1339. mov r9, 1
  1340. lsh r9, 32
  1341. mov32 r0, 0
  1342. mov32 r1, 5
  1343. or r1, r9
  1344. jlt32 r1, 4, +4
  1345. jlt32 r1, 5, +3
  1346. jlt32 r1, 6, +1
  1347. exit
  1348. mov32 r0, 1
  1349. exit",
  1350. )
  1351. .unwrap();
  1352. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1353. vm.jit_compile().unwrap();
  1354. unsafe {
  1355. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1356. }
  1357. }
  1358. #[test]
  1359. fn test_jit_jlt32_reg() {
  1360. let prog = assemble(
  1361. "
  1362. mov r9, 1
  1363. lsh r9, 32
  1364. mov r0, 0
  1365. mov r1, 5
  1366. mov r2, 4
  1367. mov r3, 6
  1368. or r1, r9
  1369. jlt32 r1, r2, +4
  1370. jlt32 r1, r1, +3
  1371. jlt32 r1, r3, +1
  1372. exit
  1373. mov r0, 1
  1374. exit",
  1375. )
  1376. .unwrap();
  1377. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1378. vm.jit_compile().unwrap();
  1379. unsafe {
  1380. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1381. }
  1382. }
  1383. #[test]
  1384. fn test_jit_jne32_imm() {
  1385. let prog = assemble(
  1386. "
  1387. mov r9, 1
  1388. lsh r9, 32
  1389. mov32 r0, 0
  1390. mov32 r1, 0xb
  1391. or r1, r9
  1392. jne32 r1, 0xb, +4
  1393. mov32 r0, 1
  1394. mov32 r1, 0xa
  1395. or r1, r9
  1396. jne32 r1, 0xb, +1
  1397. mov32 r0, 2
  1398. exit",
  1399. )
  1400. .unwrap();
  1401. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1402. vm.jit_compile().unwrap();
  1403. unsafe {
  1404. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1405. }
  1406. }
  1407. #[test]
  1408. fn test_jit_jne32_reg() {
  1409. let prog = assemble(
  1410. "
  1411. mov r9, 1
  1412. lsh r9, 32
  1413. mov32 r0, 0
  1414. mov32 r1, 0xb
  1415. or r1, r9
  1416. mov32 r2, 0xb
  1417. jne32 r1, r2, +4
  1418. mov32 r0, 1
  1419. mov32 r1, 0xa
  1420. or r1, r9
  1421. jne32 r1, r2, +1
  1422. mov32 r0, 2
  1423. exit",
  1424. )
  1425. .unwrap();
  1426. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1427. vm.jit_compile().unwrap();
  1428. unsafe {
  1429. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1430. }
  1431. }
  1432. #[test]
  1433. fn test_jit_jset32_imm() {
  1434. let prog = assemble(
  1435. "
  1436. mov r9, 1
  1437. lsh r9, 32
  1438. mov32 r0, 0
  1439. mov32 r1, 0x7
  1440. or r1, r9
  1441. jset32 r1, 0x8, +4
  1442. mov32 r0, 1
  1443. mov32 r1, 0x9
  1444. jset32 r1, 0x8, +1
  1445. mov32 r0, 2
  1446. exit",
  1447. )
  1448. .unwrap();
  1449. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1450. vm.jit_compile().unwrap();
  1451. unsafe {
  1452. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1453. }
  1454. }
  1455. #[test]
  1456. fn test_jit_jset32_reg() {
  1457. let prog = assemble(
  1458. "
  1459. mov r9, 1
  1460. lsh r9, 32
  1461. mov32 r0, 0
  1462. mov32 r1, 0x7
  1463. or r1, r9
  1464. mov32 r2, 0x8
  1465. jset32 r1, r2, +4
  1466. mov32 r0, 1
  1467. mov32 r1, 0x9
  1468. jset32 r1, r2, +1
  1469. mov32 r0, 2
  1470. exit",
  1471. )
  1472. .unwrap();
  1473. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1474. vm.jit_compile().unwrap();
  1475. unsafe {
  1476. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1477. }
  1478. }
  1479. #[test]
  1480. fn test_jit_jsge32_imm() {
  1481. let prog = assemble(
  1482. "
  1483. mov r9, 1
  1484. lsh r9, 32
  1485. mov32 r0, 0
  1486. mov32 r1, -2
  1487. or r1, r9
  1488. jsge32 r1, -1, +5
  1489. jsge32 r1, 0, +4
  1490. mov32 r0, 1
  1491. mov r1, -1
  1492. jsge32 r1, -1, +1
  1493. mov32 r0, 2
  1494. exit",
  1495. )
  1496. .unwrap();
  1497. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1498. vm.jit_compile().unwrap();
  1499. unsafe {
  1500. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1501. }
  1502. }
  1503. #[test]
  1504. fn test_jit_jsge32_reg() {
  1505. let prog = assemble(
  1506. "
  1507. mov r9, 1
  1508. lsh r9, 32
  1509. mov32 r0, 0
  1510. mov32 r1, -2
  1511. or r1, r9
  1512. mov r2, -1
  1513. mov32 r3, 0
  1514. jsge32 r1, r2, +5
  1515. jsge32 r1, r3, +4
  1516. mov32 r0, 1
  1517. mov r1, r2
  1518. jsge32 r1, r2, +1
  1519. mov32 r0, 2
  1520. exit",
  1521. )
  1522. .unwrap();
  1523. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1524. vm.jit_compile().unwrap();
  1525. unsafe {
  1526. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1527. }
  1528. }
  1529. #[test]
  1530. fn test_jit_jsgt32_imm() {
  1531. let prog = assemble(
  1532. "
  1533. mov r9, 1
  1534. lsh r9, 32
  1535. mov32 r0, 0
  1536. mov32 r1, -2
  1537. or r1, r9
  1538. jsgt32 r1, -1, +4
  1539. mov32 r0, 1
  1540. mov32 r1, 0
  1541. jsgt32 r1, -1, +1
  1542. mov32 r0, 2
  1543. exit",
  1544. )
  1545. .unwrap();
  1546. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1547. vm.jit_compile().unwrap();
  1548. unsafe {
  1549. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1550. }
  1551. }
  1552. #[test]
  1553. fn test_jit_jsgt32_reg() {
  1554. let prog = assemble(
  1555. "
  1556. mov r9, 1
  1557. lsh r9, 32
  1558. mov32 r0, 0
  1559. mov32 r1, -2
  1560. or r1, r9
  1561. mov r2, -1
  1562. jsgt32 r1, r2, +4
  1563. mov32 r0, 1
  1564. mov32 r1, 0
  1565. jsgt32 r1, r2, +1
  1566. mov32 r0, 2
  1567. exit",
  1568. )
  1569. .unwrap();
  1570. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1571. vm.jit_compile().unwrap();
  1572. unsafe {
  1573. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1574. }
  1575. }
  1576. #[test]
  1577. fn test_jit_jsle32_imm() {
  1578. let prog = assemble(
  1579. "
  1580. mov r9, 1
  1581. lsh r9, 32
  1582. mov32 r0, 0
  1583. mov32 r1, -2
  1584. or r1, r9
  1585. jsle32 r1, -3, +5
  1586. jsle32 r1, -1, +1
  1587. exit
  1588. mov32 r0, 1
  1589. jsle32 r1, -2, +1
  1590. mov32 r0, 2
  1591. exit",
  1592. )
  1593. .unwrap();
  1594. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1595. vm.jit_compile().unwrap();
  1596. unsafe {
  1597. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1598. }
  1599. }
  1600. #[test]
  1601. fn test_jit_jsle32_reg() {
  1602. let prog = assemble(
  1603. "
  1604. mov r9, 1
  1605. lsh r9, 32
  1606. mov32 r0, 0
  1607. mov32 r1, -2
  1608. or r1, r9
  1609. mov r2, -3
  1610. mov32 r3, 0
  1611. jsle32 r1, r2, +6
  1612. jsle32 r1, r3, +1
  1613. exit
  1614. mov32 r0, 1
  1615. mov r1, r2
  1616. jsle32 r1, r2, +1
  1617. mov32 r0, 2
  1618. exit",
  1619. )
  1620. .unwrap();
  1621. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1622. vm.jit_compile().unwrap();
  1623. unsafe {
  1624. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1625. }
  1626. }
  1627. #[test]
  1628. fn test_jit_jslt32_imm() {
  1629. let prog = assemble(
  1630. "
  1631. mov r9, 1
  1632. lsh r9, 32
  1633. mov32 r0, 0
  1634. mov32 r1, -2
  1635. or r1, r9
  1636. jslt32 r1, -3, +4
  1637. jslt32 r1, -2, +3
  1638. jslt32 r1, -1, +1
  1639. exit
  1640. mov32 r0, 1
  1641. exit",
  1642. )
  1643. .unwrap();
  1644. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1645. vm.jit_compile().unwrap();
  1646. unsafe {
  1647. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1648. }
  1649. }
  1650. #[test]
  1651. fn test_jit_jslt32_reg() {
  1652. let prog = assemble(
  1653. "
  1654. mov r9, 1
  1655. lsh r9, 32
  1656. mov32 r0, 0
  1657. mov32 r1, -2
  1658. or r1, r9
  1659. mov r2, -3
  1660. mov r3, -1
  1661. jslt32 r1, r1, +4
  1662. jslt32 r1, r2, +3
  1663. jslt32 r1, r3, +1
  1664. exit
  1665. mov32 r0, 1
  1666. exit",
  1667. )
  1668. .unwrap();
  1669. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1670. vm.jit_compile().unwrap();
  1671. unsafe {
  1672. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1673. }
  1674. }
  1675. #[test]
  1676. fn test_jit_lddw() {
  1677. let prog = assemble(
  1678. "
  1679. lddw r0, 0x1122334455667788
  1680. exit",
  1681. )
  1682. .unwrap();
  1683. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1684. vm.jit_compile().unwrap();
  1685. unsafe {
  1686. assert_eq!(vm.execute_program_jit().unwrap(), 0x1122334455667788);
  1687. }
  1688. }
  1689. #[test]
  1690. fn test_jit_lddw2() {
  1691. let prog = assemble(
  1692. "
  1693. lddw r0, 0x0000000080000000
  1694. exit",
  1695. )
  1696. .unwrap();
  1697. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1698. vm.jit_compile().unwrap();
  1699. unsafe {
  1700. assert_eq!(vm.execute_program_jit().unwrap(), 0x80000000);
  1701. }
  1702. }
  1703. #[test]
  1704. fn test_jit_ldxb_all() {
  1705. let prog = assemble(
  1706. "
  1707. mov r0, r1
  1708. ldxb r9, [r0+0]
  1709. lsh r9, 0
  1710. ldxb r8, [r0+1]
  1711. lsh r8, 4
  1712. ldxb r7, [r0+2]
  1713. lsh r7, 8
  1714. ldxb r6, [r0+3]
  1715. lsh r6, 12
  1716. ldxb r5, [r0+4]
  1717. lsh r5, 16
  1718. ldxb r4, [r0+5]
  1719. lsh r4, 20
  1720. ldxb r3, [r0+6]
  1721. lsh r3, 24
  1722. ldxb r2, [r0+7]
  1723. lsh r2, 28
  1724. ldxb r1, [r0+8]
  1725. lsh r1, 32
  1726. ldxb r0, [r0+9]
  1727. lsh r0, 36
  1728. or r0, r1
  1729. or r0, r2
  1730. or r0, r3
  1731. or r0, r4
  1732. or r0, r5
  1733. or r0, r6
  1734. or r0, r7
  1735. or r0, r8
  1736. or r0, r9
  1737. exit",
  1738. )
  1739. .unwrap();
  1740. let mem = &mut [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09];
  1741. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1742. vm.jit_compile().unwrap();
  1743. unsafe {
  1744. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x9876543210);
  1745. }
  1746. }
  1747. #[test]
  1748. fn test_jit_ldxb() {
  1749. let prog = assemble(
  1750. "
  1751. ldxb r0, [r1+2]
  1752. exit",
  1753. )
  1754. .unwrap();
  1755. let mem = &mut [0xaa, 0xbb, 0x11, 0xcc, 0xdd];
  1756. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1757. vm.jit_compile().unwrap();
  1758. unsafe {
  1759. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11);
  1760. }
  1761. }
  1762. #[test]
  1763. fn test_jit_ldxdw() {
  1764. let prog = assemble(
  1765. "
  1766. ldxdw r0, [r1+2]
  1767. exit",
  1768. )
  1769. .unwrap();
  1770. let mem = &mut [
  1771. 0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0xcc, 0xdd,
  1772. ];
  1773. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1774. vm.jit_compile().unwrap();
  1775. unsafe {
  1776. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x8877665544332211);
  1777. }
  1778. }
  1779. #[test]
  1780. fn test_jit_ldxh_all() {
  1781. let prog = assemble(
  1782. "
  1783. mov r0, r1
  1784. ldxh r9, [r0+0]
  1785. be16 r9
  1786. lsh r9, 0
  1787. ldxh r8, [r0+2]
  1788. be16 r8
  1789. lsh r8, 4
  1790. ldxh r7, [r0+4]
  1791. be16 r7
  1792. lsh r7, 8
  1793. ldxh r6, [r0+6]
  1794. be16 r6
  1795. lsh r6, 12
  1796. ldxh r5, [r0+8]
  1797. be16 r5
  1798. lsh r5, 16
  1799. ldxh r4, [r0+10]
  1800. be16 r4
  1801. lsh r4, 20
  1802. ldxh r3, [r0+12]
  1803. be16 r3
  1804. lsh r3, 24
  1805. ldxh r2, [r0+14]
  1806. be16 r2
  1807. lsh r2, 28
  1808. ldxh r1, [r0+16]
  1809. be16 r1
  1810. lsh r1, 32
  1811. ldxh r0, [r0+18]
  1812. be16 r0
  1813. lsh r0, 36
  1814. or r0, r1
  1815. or r0, r2
  1816. or r0, r3
  1817. or r0, r4
  1818. or r0, r5
  1819. or r0, r6
  1820. or r0, r7
  1821. or r0, r8
  1822. or r0, r9
  1823. exit",
  1824. )
  1825. .unwrap();
  1826. let mem = &mut [
  1827. 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00,
  1828. 0x07, 0x00, 0x08, 0x00, 0x09,
  1829. ];
  1830. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1831. vm.jit_compile().unwrap();
  1832. unsafe {
  1833. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x9876543210);
  1834. }
  1835. }
  1836. #[test]
  1837. fn test_jit_ldxh_all2() {
  1838. let prog = assemble(
  1839. "
  1840. mov r0, r1
  1841. ldxh r9, [r0+0]
  1842. be16 r9
  1843. ldxh r8, [r0+2]
  1844. be16 r8
  1845. ldxh r7, [r0+4]
  1846. be16 r7
  1847. ldxh r6, [r0+6]
  1848. be16 r6
  1849. ldxh r5, [r0+8]
  1850. be16 r5
  1851. ldxh r4, [r0+10]
  1852. be16 r4
  1853. ldxh r3, [r0+12]
  1854. be16 r3
  1855. ldxh r2, [r0+14]
  1856. be16 r2
  1857. ldxh r1, [r0+16]
  1858. be16 r1
  1859. ldxh r0, [r0+18]
  1860. be16 r0
  1861. or r0, r1
  1862. or r0, r2
  1863. or r0, r3
  1864. or r0, r4
  1865. or r0, r5
  1866. or r0, r6
  1867. or r0, r7
  1868. or r0, r8
  1869. or r0, r9
  1870. exit",
  1871. )
  1872. .unwrap();
  1873. let mem = &mut [
  1874. 0x00, 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08, 0x00, 0x10, 0x00, 0x20, 0x00, 0x40, 0x00,
  1875. 0x80, 0x01, 0x00, 0x02, 0x00,
  1876. ];
  1877. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1878. vm.jit_compile().unwrap();
  1879. unsafe {
  1880. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x3ff);
  1881. }
  1882. }
  1883. #[test]
  1884. fn test_jit_ldxh() {
  1885. let prog = assemble(
  1886. "
  1887. ldxh r0, [r1+2]
  1888. exit",
  1889. )
  1890. .unwrap();
  1891. let mem = &mut [0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd];
  1892. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1893. vm.jit_compile().unwrap();
  1894. unsafe {
  1895. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x2211);
  1896. }
  1897. }
  1898. #[test]
  1899. fn test_jit_ldxh_same_reg() {
  1900. let prog = assemble(
  1901. "
  1902. mov r0, r1
  1903. sth [r0], 0x1234
  1904. ldxh r0, [r0]
  1905. exit",
  1906. )
  1907. .unwrap();
  1908. let mem = &mut [0xff, 0xff];
  1909. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1910. vm.jit_compile().unwrap();
  1911. unsafe {
  1912. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1234);
  1913. }
  1914. }
  1915. #[test]
  1916. fn test_jit_ldxw_all() {
  1917. let prog = assemble(
  1918. "
  1919. mov r0, r1
  1920. ldxw r9, [r0+0]
  1921. be32 r9
  1922. ldxw r8, [r0+4]
  1923. be32 r8
  1924. ldxw r7, [r0+8]
  1925. be32 r7
  1926. ldxw r6, [r0+12]
  1927. be32 r6
  1928. ldxw r5, [r0+16]
  1929. be32 r5
  1930. ldxw r4, [r0+20]
  1931. be32 r4
  1932. ldxw r3, [r0+24]
  1933. be32 r3
  1934. ldxw r2, [r0+28]
  1935. be32 r2
  1936. ldxw r1, [r0+32]
  1937. be32 r1
  1938. ldxw r0, [r0+36]
  1939. be32 r0
  1940. or r0, r1
  1941. or r0, r2
  1942. or r0, r3
  1943. or r0, r4
  1944. or r0, r5
  1945. or r0, r6
  1946. or r0, r7
  1947. or r0, r8
  1948. or r0, r9
  1949. exit",
  1950. )
  1951. .unwrap();
  1952. let mem = &mut [
  1953. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  1954. 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  1955. 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
  1956. ];
  1957. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1958. vm.jit_compile().unwrap();
  1959. unsafe {
  1960. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x030f0f);
  1961. }
  1962. }
  1963. #[test]
  1964. fn test_jit_ldxw() {
  1965. let prog = assemble(
  1966. "
  1967. ldxw r0, [r1+2]
  1968. exit",
  1969. )
  1970. .unwrap();
  1971. let mem = &mut [0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0xcc, 0xdd];
  1972. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1973. vm.jit_compile().unwrap();
  1974. unsafe {
  1975. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x44332211);
  1976. }
  1977. }
  1978. #[test]
  1979. fn test_jit_le16() {
  1980. let prog = assemble(
  1981. "
  1982. ldxh r0, [r1]
  1983. le16 r0
  1984. exit",
  1985. )
  1986. .unwrap();
  1987. let mem = &mut [0x22, 0x11];
  1988. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1989. vm.jit_compile().unwrap();
  1990. unsafe {
  1991. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1122);
  1992. }
  1993. }
  1994. #[test]
  1995. fn test_jit_le32() {
  1996. let prog = assemble(
  1997. "
  1998. ldxw r0, [r1]
  1999. le32 r0
  2000. exit",
  2001. )
  2002. .unwrap();
  2003. let mem = &mut [0x44, 0x33, 0x22, 0x11];
  2004. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2005. vm.jit_compile().unwrap();
  2006. unsafe {
  2007. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11223344);
  2008. }
  2009. }
  2010. #[test]
  2011. fn test_jit_le64() {
  2012. let prog = assemble(
  2013. "
  2014. ldxdw r0, [r1]
  2015. le64 r0
  2016. exit",
  2017. )
  2018. .unwrap();
  2019. let mem = &mut [0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11];
  2020. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2021. vm.jit_compile().unwrap();
  2022. unsafe {
  2023. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1122334455667788);
  2024. }
  2025. }
  2026. #[test]
  2027. fn test_jit_lsh_reg() {
  2028. let prog = assemble(
  2029. "
  2030. mov r0, 0x1
  2031. mov r7, 4
  2032. lsh r0, r7
  2033. exit",
  2034. )
  2035. .unwrap();
  2036. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2037. vm.jit_compile().unwrap();
  2038. unsafe {
  2039. assert_eq!(vm.execute_program_jit().unwrap(), 0x10);
  2040. }
  2041. }
  2042. #[test]
  2043. fn test_jit_mod() {
  2044. let prog = assemble(
  2045. "
  2046. mov32 r0, 5748
  2047. mod32 r0, 92
  2048. mov32 r1, 13
  2049. mod32 r0, r1
  2050. exit",
  2051. )
  2052. .unwrap();
  2053. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2054. vm.jit_compile().unwrap();
  2055. unsafe {
  2056. assert_eq!(vm.execute_program_jit().unwrap(), 0x5);
  2057. }
  2058. }
  2059. #[test]
  2060. fn test_jit_mod32() {
  2061. let prog = assemble(
  2062. "
  2063. lddw r0, 0x100000003
  2064. mod32 r0, 3
  2065. exit",
  2066. )
  2067. .unwrap();
  2068. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2069. vm.jit_compile().unwrap();
  2070. unsafe {
  2071. assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
  2072. }
  2073. }
  2074. #[test]
  2075. fn test_jit_mod64() {
  2076. let prog = assemble(
  2077. "
  2078. mov32 r0, -1316649930
  2079. lsh r0, 32
  2080. or r0, 0x100dc5c8
  2081. mov32 r1, 0xdde263e
  2082. lsh r1, 32
  2083. or r1, 0x3cbef7f3
  2084. mod r0, r1
  2085. mod r0, 0x658f1778
  2086. exit",
  2087. )
  2088. .unwrap();
  2089. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2090. vm.jit_compile().unwrap();
  2091. unsafe {
  2092. assert_eq!(vm.execute_program_jit().unwrap(), 0x30ba5a04);
  2093. }
  2094. }
  2095. #[test]
  2096. fn test_jit_mov() {
  2097. let prog = assemble(
  2098. "
  2099. mov32 r1, 1
  2100. mov32 r0, r1
  2101. exit",
  2102. )
  2103. .unwrap();
  2104. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2105. vm.jit_compile().unwrap();
  2106. unsafe {
  2107. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  2108. }
  2109. }
  2110. #[test]
  2111. fn test_jit_mul32_imm() {
  2112. let prog = assemble(
  2113. "
  2114. mov r0, 3
  2115. mul32 r0, 4
  2116. exit",
  2117. )
  2118. .unwrap();
  2119. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2120. vm.jit_compile().unwrap();
  2121. unsafe {
  2122. assert_eq!(vm.execute_program_jit().unwrap(), 0xc);
  2123. }
  2124. }
  2125. #[test]
  2126. fn test_jit_mul32_reg() {
  2127. let prog = assemble(
  2128. "
  2129. mov r0, 3
  2130. mov r1, 4
  2131. mul32 r0, r1
  2132. exit",
  2133. )
  2134. .unwrap();
  2135. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2136. vm.jit_compile().unwrap();
  2137. unsafe {
  2138. assert_eq!(vm.execute_program_jit().unwrap(), 0xc);
  2139. }
  2140. }
  2141. #[test]
  2142. fn test_jit_mul32_reg_overflow() {
  2143. let prog = assemble(
  2144. "
  2145. mov r0, 0x40000001
  2146. mov r1, 4
  2147. mul32 r0, r1
  2148. exit",
  2149. )
  2150. .unwrap();
  2151. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2152. vm.jit_compile().unwrap();
  2153. unsafe {
  2154. assert_eq!(vm.execute_program_jit().unwrap(), 0x4);
  2155. }
  2156. }
  2157. #[test]
  2158. fn test_jit_mul64_imm() {
  2159. let prog = assemble(
  2160. "
  2161. mov r0, 0x40000001
  2162. mul r0, 4
  2163. exit",
  2164. )
  2165. .unwrap();
  2166. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2167. vm.jit_compile().unwrap();
  2168. unsafe {
  2169. assert_eq!(vm.execute_program_jit().unwrap(), 0x100000004);
  2170. }
  2171. }
  2172. #[test]
  2173. fn test_jit_mul64_reg() {
  2174. let prog = assemble(
  2175. "
  2176. mov r0, 0x40000001
  2177. mov r1, 4
  2178. mul r0, r1
  2179. exit",
  2180. )
  2181. .unwrap();
  2182. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2183. vm.jit_compile().unwrap();
  2184. unsafe {
  2185. assert_eq!(vm.execute_program_jit().unwrap(), 0x100000004);
  2186. }
  2187. }
  2188. #[test]
  2189. fn test_jit_mul_loop() {
  2190. let prog = assemble(
  2191. "
  2192. mov r0, 0x7
  2193. add r1, 0xa
  2194. lsh r1, 0x20
  2195. rsh r1, 0x20
  2196. jeq r1, 0x0, +4
  2197. mov r0, 0x7
  2198. mul r0, 0x7
  2199. add r1, -1
  2200. jne r1, 0x0, -3
  2201. exit",
  2202. )
  2203. .unwrap();
  2204. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2205. vm.jit_compile().unwrap();
  2206. unsafe {
  2207. assert_eq!(vm.execute_program_jit().unwrap(), 0x75db9c97);
  2208. }
  2209. }
  2210. #[test]
  2211. fn test_jit_neg64() {
  2212. let prog = assemble(
  2213. "
  2214. mov32 r0, 2
  2215. neg r0
  2216. exit",
  2217. )
  2218. .unwrap();
  2219. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2220. vm.jit_compile().unwrap();
  2221. unsafe {
  2222. assert_eq!(vm.execute_program_jit().unwrap(), 0xfffffffffffffffe);
  2223. }
  2224. }
  2225. #[test]
  2226. fn test_jit_neg() {
  2227. let prog = assemble(
  2228. "
  2229. mov32 r0, 2
  2230. neg32 r0
  2231. exit",
  2232. )
  2233. .unwrap();
  2234. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2235. vm.jit_compile().unwrap();
  2236. unsafe {
  2237. assert_eq!(vm.execute_program_jit().unwrap(), 0xfffffffe);
  2238. }
  2239. }
  2240. #[test]
  2241. fn test_jit_prime() {
  2242. let prog = assemble(
  2243. "
  2244. mov r1, 67
  2245. mov r0, 0x1
  2246. mov r2, 0x2
  2247. jgt r1, 0x2, +4
  2248. ja +10
  2249. add r2, 0x1
  2250. mov r0, 0x1
  2251. jge r2, r1, +7
  2252. mov r3, r1
  2253. div r3, r2
  2254. mul r3, r2
  2255. mov r4, r1
  2256. sub r4, r3
  2257. mov r0, 0x0
  2258. jne r4, 0x0, -10
  2259. exit",
  2260. )
  2261. .unwrap();
  2262. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2263. vm.jit_compile().unwrap();
  2264. unsafe {
  2265. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  2266. }
  2267. }
  2268. #[test]
  2269. fn test_jit_rhs32() {
  2270. let prog = assemble(
  2271. "
  2272. xor r0, r0
  2273. sub r0, 1
  2274. rsh32 r0, 8
  2275. exit",
  2276. )
  2277. .unwrap();
  2278. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2279. vm.jit_compile().unwrap();
  2280. unsafe {
  2281. assert_eq!(vm.execute_program_jit().unwrap(), 0x00ffffff);
  2282. }
  2283. }
  2284. #[test]
  2285. fn test_jit_rsh_reg() {
  2286. let prog = assemble(
  2287. "
  2288. mov r0, 0x10
  2289. mov r7, 4
  2290. rsh r0, r7
  2291. exit",
  2292. )
  2293. .unwrap();
  2294. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2295. vm.jit_compile().unwrap();
  2296. unsafe {
  2297. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  2298. }
  2299. }
  2300. #[test]
  2301. fn test_jit_stack() {
  2302. let prog = assemble(
  2303. "
  2304. mov r1, 51
  2305. stdw [r10-16], 0xab
  2306. stdw [r10-8], 0xcd
  2307. and r1, 1
  2308. lsh r1, 3
  2309. mov r2, r10
  2310. add r2, r1
  2311. ldxdw r0, [r2-16]
  2312. exit",
  2313. )
  2314. .unwrap();
  2315. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2316. vm.jit_compile().unwrap();
  2317. unsafe {
  2318. assert_eq!(vm.execute_program_jit().unwrap(), 0xcd);
  2319. }
  2320. }
  2321. #[test]
  2322. fn test_jit_stack2() {
  2323. let prog = assemble(
  2324. "
  2325. stb [r10-4], 0x01
  2326. stb [r10-3], 0x02
  2327. stb [r10-2], 0x03
  2328. stb [r10-1], 0x04
  2329. mov r1, r10
  2330. mov r2, 0x4
  2331. sub r1, r2
  2332. call 1
  2333. mov r1, 0
  2334. ldxb r2, [r10-4]
  2335. ldxb r3, [r10-3]
  2336. ldxb r4, [r10-2]
  2337. ldxb r5, [r10-1]
  2338. call 0
  2339. xor r0, 0x2a2a2a2a
  2340. exit",
  2341. )
  2342. .unwrap();
  2343. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2344. vm.register_helper(0, helpers::gather_bytes).unwrap();
  2345. vm.register_helper(1, helpers::memfrob).unwrap();
  2346. vm.jit_compile().unwrap();
  2347. unsafe {
  2348. assert_eq!(vm.execute_program_jit().unwrap(), 0x01020304);
  2349. }
  2350. }
  2351. #[test]
  2352. fn test_jit_stb() {
  2353. let prog = assemble(
  2354. "
  2355. stb [r1+2], 0x11
  2356. ldxb r0, [r1+2]
  2357. exit",
  2358. )
  2359. .unwrap();
  2360. let mem = &mut [0xaa, 0xbb, 0xff, 0xcc, 0xdd];
  2361. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2362. vm.jit_compile().unwrap();
  2363. unsafe {
  2364. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11);
  2365. }
  2366. }
  2367. #[test]
  2368. fn test_jit_stdw() {
  2369. let prog = assemble(
  2370. "
  2371. stdw [r1+2], 0x44332211
  2372. ldxdw r0, [r1+2]
  2373. exit",
  2374. )
  2375. .unwrap();
  2376. let mem = &mut [
  2377. 0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd,
  2378. ];
  2379. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2380. vm.jit_compile().unwrap();
  2381. unsafe {
  2382. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x44332211);
  2383. }
  2384. }
  2385. #[test]
  2386. fn test_jit_sth() {
  2387. let prog = assemble(
  2388. "
  2389. sth [r1+2], 0x2211
  2390. ldxh r0, [r1+2]
  2391. exit",
  2392. )
  2393. .unwrap();
  2394. let mem = &mut [0xaa, 0xbb, 0xff, 0xff, 0xcc, 0xdd];
  2395. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2396. vm.jit_compile().unwrap();
  2397. unsafe {
  2398. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x2211);
  2399. }
  2400. }
  2401. #[test]
  2402. fn test_jit_string_stack() {
  2403. let prog = assemble(
  2404. "
  2405. mov r1, 0x78636261
  2406. stxw [r10-8], r1
  2407. mov r6, 0x0
  2408. stxb [r10-4], r6
  2409. stxb [r10-12], r6
  2410. mov r1, 0x79636261
  2411. stxw [r10-16], r1
  2412. mov r1, r10
  2413. add r1, -8
  2414. mov r2, r1
  2415. call 0x4
  2416. mov r1, r0
  2417. mov r0, 0x1
  2418. lsh r1, 0x20
  2419. rsh r1, 0x20
  2420. jne r1, 0x0, +11
  2421. mov r1, r10
  2422. add r1, -8
  2423. mov r2, r10
  2424. add r2, -16
  2425. call 0x4
  2426. mov r1, r0
  2427. lsh r1, 0x20
  2428. rsh r1, 0x20
  2429. mov r0, 0x1
  2430. jeq r1, r6, +1
  2431. mov r0, 0x0
  2432. exit",
  2433. )
  2434. .unwrap();
  2435. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2436. vm.register_helper(4, helpers::strcmp).unwrap();
  2437. vm.jit_compile().unwrap();
  2438. unsafe {
  2439. assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
  2440. }
  2441. }
  2442. #[test]
  2443. fn test_jit_stw() {
  2444. let prog = assemble(
  2445. "
  2446. stw [r1+2], 0x44332211
  2447. ldxw r0, [r1+2]
  2448. exit",
  2449. )
  2450. .unwrap();
  2451. let mem = &mut [0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd];
  2452. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2453. vm.jit_compile().unwrap();
  2454. unsafe {
  2455. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x44332211);
  2456. }
  2457. }
  2458. #[test]
  2459. fn test_jit_stxb() {
  2460. let prog = assemble(
  2461. "
  2462. mov32 r2, 0x11
  2463. stxb [r1+2], r2
  2464. ldxb r0, [r1+2]
  2465. exit",
  2466. )
  2467. .unwrap();
  2468. let mem = &mut [0xaa, 0xbb, 0xff, 0xcc, 0xdd];
  2469. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2470. vm.jit_compile().unwrap();
  2471. unsafe {
  2472. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11);
  2473. }
  2474. }
  2475. #[test]
  2476. fn test_jit_stxb_all() {
  2477. let prog = assemble(
  2478. "
  2479. mov r0, 0xf0
  2480. mov r2, 0xf2
  2481. mov r3, 0xf3
  2482. mov r4, 0xf4
  2483. mov r5, 0xf5
  2484. mov r6, 0xf6
  2485. mov r7, 0xf7
  2486. mov r8, 0xf8
  2487. stxb [r1], r0
  2488. stxb [r1+1], r2
  2489. stxb [r1+2], r3
  2490. stxb [r1+3], r4
  2491. stxb [r1+4], r5
  2492. stxb [r1+5], r6
  2493. stxb [r1+6], r7
  2494. stxb [r1+7], r8
  2495. ldxdw r0, [r1]
  2496. be64 r0
  2497. exit",
  2498. )
  2499. .unwrap();
  2500. let mem = &mut [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
  2501. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2502. vm.jit_compile().unwrap();
  2503. unsafe {
  2504. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0xf0f2f3f4f5f6f7f8);
  2505. }
  2506. }
  2507. #[test]
  2508. fn test_jit_stxb_all2() {
  2509. let prog = assemble(
  2510. "
  2511. mov r0, r1
  2512. mov r1, 0xf1
  2513. mov r9, 0xf9
  2514. stxb [r0], r1
  2515. stxb [r0+1], r9
  2516. ldxh r0, [r0]
  2517. be16 r0
  2518. exit",
  2519. )
  2520. .unwrap();
  2521. let mem = &mut [0xff, 0xff];
  2522. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2523. vm.jit_compile().unwrap();
  2524. unsafe {
  2525. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0xf1f9);
  2526. }
  2527. }
  2528. #[test]
  2529. fn test_jit_stxb_chain() {
  2530. let prog = assemble(
  2531. "
  2532. mov r0, r1
  2533. ldxb r9, [r0+0]
  2534. stxb [r0+1], r9
  2535. ldxb r8, [r0+1]
  2536. stxb [r0+2], r8
  2537. ldxb r7, [r0+2]
  2538. stxb [r0+3], r7
  2539. ldxb r6, [r0+3]
  2540. stxb [r0+4], r6
  2541. ldxb r5, [r0+4]
  2542. stxb [r0+5], r5
  2543. ldxb r4, [r0+5]
  2544. stxb [r0+6], r4
  2545. ldxb r3, [r0+6]
  2546. stxb [r0+7], r3
  2547. ldxb r2, [r0+7]
  2548. stxb [r0+8], r2
  2549. ldxb r1, [r0+8]
  2550. stxb [r0+9], r1
  2551. ldxb r0, [r0+9]
  2552. exit",
  2553. )
  2554. .unwrap();
  2555. let mem = &mut [0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
  2556. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2557. vm.jit_compile().unwrap();
  2558. unsafe {
  2559. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x2a);
  2560. }
  2561. }
  2562. #[test]
  2563. fn test_jit_stxdw() {
  2564. let prog = assemble(
  2565. "
  2566. mov r2, -2005440939
  2567. lsh r2, 32
  2568. or r2, 0x44332211
  2569. stxdw [r1+2], r2
  2570. ldxdw r0, [r1+2]
  2571. exit",
  2572. )
  2573. .unwrap();
  2574. let mem = &mut [
  2575. 0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd,
  2576. ];
  2577. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2578. vm.jit_compile().unwrap();
  2579. unsafe {
  2580. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x8877665544332211);
  2581. }
  2582. }
  2583. #[test]
  2584. fn test_jit_stxh() {
  2585. let prog = assemble(
  2586. "
  2587. mov32 r2, 0x2211
  2588. stxh [r1+2], r2
  2589. ldxh r0, [r1+2]
  2590. exit",
  2591. )
  2592. .unwrap();
  2593. let mem = &mut [0xaa, 0xbb, 0xff, 0xff, 0xcc, 0xdd];
  2594. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2595. vm.jit_compile().unwrap();
  2596. unsafe {
  2597. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x2211);
  2598. }
  2599. }
  2600. #[test]
  2601. fn test_jit_stxw() {
  2602. let prog = assemble(
  2603. "
  2604. mov32 r2, 0x44332211
  2605. stxw [r1+2], r2
  2606. ldxw r0, [r1+2]
  2607. exit",
  2608. )
  2609. .unwrap();
  2610. let mem = &mut [0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd];
  2611. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2612. vm.jit_compile().unwrap();
  2613. unsafe {
  2614. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x44332211);
  2615. }
  2616. }
  2617. #[test]
  2618. fn test_jit_subnet() {
  2619. let prog = assemble(
  2620. "
  2621. mov r2, 0xe
  2622. ldxh r3, [r1+12]
  2623. jne r3, 0x81, +2
  2624. mov r2, 0x12
  2625. ldxh r3, [r1+16]
  2626. and r3, 0xffff
  2627. jne r3, 0x8, +5
  2628. add r1, r2
  2629. mov r0, 0x1
  2630. ldxw r1, [r1+16]
  2631. and r1, 0xffffff
  2632. jeq r1, 0x1a8c0, +1
  2633. mov r0, 0x0
  2634. exit",
  2635. )
  2636. .unwrap();
  2637. let mem = &mut [
  2638. 0x00, 0x00, 0xc0, 0x9f, 0xa0, 0x97, 0x00, 0xa0, 0xcc, 0x3b, 0xbf, 0xfa, 0x08, 0x00, 0x45,
  2639. 0x10, 0x00, 0x3c, 0x46, 0x3c, 0x40, 0x00, 0x40, 0x06, 0x73, 0x1c, 0xc0, 0xa8, 0x01, 0x02,
  2640. 0xc0, 0xa8, 0x01, 0x01, 0x06, 0x0e, 0x00, 0x17, 0x99, 0xc5, 0xa0, 0xec, 0x00, 0x00, 0x00,
  2641. 0x00, 0xa0, 0x02, 0x7d, 0x78, 0xe0, 0xa3, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4, 0x04, 0x02,
  2642. 0x08, 0x0a, 0x00, 0x9c, 0x27, 0x24, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x00,
  2643. ];
  2644. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2645. vm.jit_compile().unwrap();
  2646. unsafe {
  2647. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1);
  2648. }
  2649. }
  2650. const PROG_TCP_PORT_80: [u8; 152] = [
  2651. 0x71, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x71, 0x13, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00,
  2652. 0x67, 0x03, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x4f, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2653. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x03, 0x0c, 0x00, 0x08, 0x00, 0x00, 0x00,
  2654. 0x71, 0x12, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x02, 0x0a, 0x00, 0x06, 0x00, 0x00, 0x00,
  2655. 0x71, 0x12, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x01, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00,
  2656. 0x57, 0x02, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x67, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  2657. 0x0f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x12, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  2658. 0x15, 0x02, 0x02, 0x00, 0x00, 0x50, 0x00, 0x00, 0x69, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2659. 0x55, 0x01, 0x01, 0x00, 0x00, 0x50, 0x00, 0x00, 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  2660. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2661. ];
  2662. #[test]
  2663. fn test_jit_tcp_port80_match() {
  2664. let mem = &mut [
  2665. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45,
  2666. 0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
  2667. 0xc0, 0xa8, 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2668. 0x00, 0x50, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2669. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2670. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2671. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2672. ];
  2673. let prog = &PROG_TCP_PORT_80;
  2674. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  2675. vm.jit_compile().unwrap();
  2676. unsafe {
  2677. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1);
  2678. }
  2679. }
  2680. #[test]
  2681. fn test_jit_tcp_port80_nomatch() {
  2682. let mem = &mut [
  2683. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45,
  2684. 0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
  2685. 0xc0, 0xa8, 0x00, 0x02, 0x00, 0x16, 0x27, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2686. 0x00, 0x51, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2687. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2688. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2689. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2690. ];
  2691. let prog = &PROG_TCP_PORT_80;
  2692. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  2693. vm.jit_compile().unwrap();
  2694. unsafe {
  2695. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x0);
  2696. }
  2697. }
  2698. #[test]
  2699. fn test_jit_tcp_port80_nomatch_ethertype() {
  2700. let mem = &mut [
  2701. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x01, 0x45,
  2702. 0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
  2703. 0xc0, 0xa8, 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2704. 0x00, 0x50, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2705. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2706. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2707. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2708. ];
  2709. let prog = &PROG_TCP_PORT_80;
  2710. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  2711. vm.jit_compile().unwrap();
  2712. unsafe {
  2713. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x0);
  2714. }
  2715. }
  2716. #[test]
  2717. fn test_jit_tcp_port80_nomatch_proto() {
  2718. let mem = &mut [
  2719. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45,
  2720. 0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
  2721. 0xc0, 0xa8, 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2722. 0x00, 0x50, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2723. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2724. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2725. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2726. ];
  2727. let prog = &PROG_TCP_PORT_80;
  2728. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  2729. vm.jit_compile().unwrap();
  2730. unsafe {
  2731. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x0);
  2732. }
  2733. }
  2734. #[test]
  2735. fn test_jit_tcp_sack_match() {
  2736. let mut mem = TCP_SACK_MATCH.to_vec();
  2737. let prog = assemble(TCP_SACK_ASM).unwrap();
  2738. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2739. vm.jit_compile().unwrap();
  2740. unsafe {
  2741. assert_eq!(vm.execute_program_jit(mem.as_mut_slice()).unwrap(), 0x1);
  2742. }
  2743. }
  2744. #[test]
  2745. fn test_jit_tcp_sack_nomatch() {
  2746. let mut mem = TCP_SACK_NOMATCH.to_vec();
  2747. let prog = assemble(TCP_SACK_ASM).unwrap();
  2748. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2749. vm.jit_compile().unwrap();
  2750. unsafe {
  2751. assert_eq!(vm.execute_program_jit(mem.as_mut_slice()).unwrap(), 0x0);
  2752. }
  2753. }