ubpf_jit_x86_64.rs 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612
  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. <quentin.monnet@6wind.com>
  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. #![cfg(not(windows))]
  17. extern crate rbpf;
  18. mod common;
  19. use common::{TCP_SACK_ASM, TCP_SACK_MATCH, TCP_SACK_NOMATCH};
  20. use rbpf::assembler::assemble;
  21. use rbpf::helpers;
  22. #[cfg(not(feature = "std"))]
  23. fn alloc_exec_memory() -> Box<[u8]> {
  24. let size = 4096;
  25. let layout = std::alloc::Layout::from_size_align(size, 4096).unwrap();
  26. unsafe {
  27. let ptr = std::alloc::alloc(layout);
  28. assert!(!ptr.is_null(), "Failed to allocate memory");
  29. libc::mprotect(ptr.cast(), size, libc::PROT_EXEC | libc::PROT_WRITE);
  30. let slice = std::slice::from_raw_parts_mut(ptr, size);
  31. Box::from_raw(slice)
  32. }
  33. }
  34. #[test]
  35. fn test_jit_add() {
  36. let prog = assemble(
  37. "
  38. mov32 r0, 0
  39. mov32 r1, 2
  40. add32 r0, 1
  41. add32 r0, r1
  42. exit
  43. ",
  44. )
  45. .unwrap();
  46. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  47. #[cfg(not(feature = "std"))]
  48. let mut exec_mem = alloc_exec_memory();
  49. #[cfg(not(feature = "std"))]
  50. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  51. vm.jit_compile().unwrap();
  52. unsafe {
  53. assert_eq!(vm.execute_program_jit().unwrap(), 0x3);
  54. }
  55. }
  56. #[test]
  57. fn test_jit_alu64_arith() {
  58. let prog = assemble(
  59. "
  60. mov r0, 0
  61. mov r1, 1
  62. mov r2, 2
  63. mov r3, 3
  64. mov r4, 4
  65. mov r5, 5
  66. mov r6, 6
  67. mov r7, 7
  68. mov r8, 8
  69. mov r9, 9
  70. add r0, 23
  71. add r0, r7
  72. sub r0, 13
  73. sub r0, r1
  74. mul r0, 7
  75. mul r0, r3
  76. div r0, 2
  77. div r0, r4
  78. exit
  79. ",
  80. )
  81. .unwrap();
  82. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  83. #[cfg(not(feature = "std"))]
  84. let mut exec_mem = alloc_exec_memory();
  85. #[cfg(not(feature = "std"))]
  86. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  87. vm.jit_compile().unwrap();
  88. unsafe {
  89. assert_eq!(vm.execute_program_jit().unwrap(), 0x2a);
  90. }
  91. }
  92. #[test]
  93. fn test_jit_alu64_bit() {
  94. let prog = assemble(
  95. "
  96. mov r0, 0
  97. mov r1, 1
  98. mov r2, 2
  99. mov r3, 3
  100. mov r4, 4
  101. mov r5, 5
  102. mov r6, 6
  103. mov r7, 7
  104. mov r8, 8
  105. or r0, r5
  106. or r0, 0xa0
  107. and r0, 0xa3
  108. mov r9, 0x91
  109. and r0, r9
  110. lsh r0, 32
  111. lsh r0, 22
  112. lsh r0, r8
  113. rsh r0, 32
  114. rsh r0, 19
  115. rsh r0, r7
  116. xor r0, 0x03
  117. xor r0, r2
  118. exit
  119. ",
  120. )
  121. .unwrap();
  122. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  123. #[cfg(not(feature = "std"))]
  124. let mut exec_mem = alloc_exec_memory();
  125. #[cfg(not(feature = "std"))]
  126. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  127. vm.jit_compile().unwrap();
  128. unsafe {
  129. assert_eq!(vm.execute_program_jit().unwrap(), 0x11);
  130. }
  131. }
  132. #[test]
  133. fn test_jit_alu_arith() {
  134. let prog = assemble(
  135. "
  136. mov32 r0, 0
  137. mov32 r1, 1
  138. mov32 r2, 2
  139. mov32 r3, 3
  140. mov32 r4, 4
  141. mov32 r5, 5
  142. mov32 r6, 6
  143. mov32 r7, 7
  144. mov32 r8, 8
  145. mov32 r9, 9
  146. add32 r0, 23
  147. add32 r0, r7
  148. sub32 r0, 13
  149. sub32 r0, r1
  150. mul32 r0, 7
  151. mul32 r0, r3
  152. div32 r0, 2
  153. div32 r0, r4
  154. exit
  155. ",
  156. )
  157. .unwrap();
  158. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  159. #[cfg(not(feature = "std"))]
  160. let mut exec_mem = alloc_exec_memory();
  161. #[cfg(not(feature = "std"))]
  162. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  163. vm.jit_compile().unwrap();
  164. unsafe {
  165. assert_eq!(vm.execute_program_jit().unwrap(), 0x2a);
  166. }
  167. }
  168. #[test]
  169. fn test_jit_alu_bit() {
  170. let prog = assemble(
  171. "
  172. mov32 r0, 0
  173. mov32 r1, 1
  174. mov32 r2, 2
  175. mov32 r3, 3
  176. mov32 r4, 4
  177. mov32 r5, 5
  178. mov32 r6, 6
  179. mov32 r7, 7
  180. mov32 r8, 8
  181. or32 r0, r5
  182. or32 r0, 0xa0
  183. and32 r0, 0xa3
  184. mov32 r9, 0x91
  185. and32 r0, r9
  186. lsh32 r0, 22
  187. lsh32 r0, r8
  188. rsh32 r0, 19
  189. rsh32 r0, r7
  190. xor32 r0, 0x03
  191. xor32 r0, r2
  192. exit
  193. ",
  194. )
  195. .unwrap();
  196. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  197. #[cfg(not(feature = "std"))]
  198. let mut exec_mem = alloc_exec_memory();
  199. #[cfg(not(feature = "std"))]
  200. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  201. vm.jit_compile().unwrap();
  202. unsafe {
  203. assert_eq!(vm.execute_program_jit().unwrap(), 0x11);
  204. }
  205. }
  206. #[test]
  207. fn test_jit_arsh32_high_shift() {
  208. let prog = assemble(
  209. "
  210. mov r0, 8
  211. lddw r1, 0x100000001
  212. arsh32 r0, r1
  213. exit
  214. ",
  215. )
  216. .unwrap();
  217. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  218. #[cfg(not(feature = "std"))]
  219. let mut exec_mem = alloc_exec_memory();
  220. #[cfg(not(feature = "std"))]
  221. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  222. vm.jit_compile().unwrap();
  223. unsafe {
  224. assert_eq!(vm.execute_program_jit().unwrap(), 0x4);
  225. }
  226. }
  227. #[test]
  228. fn test_jit_arsh() {
  229. let prog = assemble(
  230. "
  231. mov32 r0, 0xf8
  232. lsh32 r0, 28
  233. arsh32 r0, 16
  234. exit
  235. ",
  236. )
  237. .unwrap();
  238. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  239. #[cfg(not(feature = "std"))]
  240. let mut exec_mem = alloc_exec_memory();
  241. #[cfg(not(feature = "std"))]
  242. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  243. vm.jit_compile().unwrap();
  244. unsafe {
  245. assert_eq!(vm.execute_program_jit().unwrap(), 0xffff8000);
  246. }
  247. }
  248. #[test]
  249. fn test_jit_arsh64() {
  250. let prog = assemble(
  251. "
  252. mov32 r0, 1
  253. lsh r0, 63
  254. arsh r0, 55
  255. mov32 r1, 5
  256. arsh r0, r1
  257. exit
  258. ",
  259. )
  260. .unwrap();
  261. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  262. #[cfg(not(feature = "std"))]
  263. let mut exec_mem = alloc_exec_memory();
  264. #[cfg(not(feature = "std"))]
  265. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  266. vm.jit_compile().unwrap();
  267. unsafe {
  268. assert_eq!(vm.execute_program_jit().unwrap(), 0xfffffffffffffff8);
  269. }
  270. }
  271. #[test]
  272. fn test_jit_arsh_reg() {
  273. let prog = assemble(
  274. "
  275. mov32 r0, 0xf8
  276. mov32 r1, 16
  277. lsh32 r0, 28
  278. arsh32 r0, r1
  279. exit
  280. ",
  281. )
  282. .unwrap();
  283. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  284. #[cfg(not(feature = "std"))]
  285. let mut exec_mem = alloc_exec_memory();
  286. #[cfg(not(feature = "std"))]
  287. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  288. vm.jit_compile().unwrap();
  289. unsafe {
  290. assert_eq!(vm.execute_program_jit().unwrap(), 0xffff8000);
  291. }
  292. }
  293. #[test]
  294. fn test_jit_be16() {
  295. let prog = assemble(
  296. "
  297. ldxh r0, [r1]
  298. be16 r0
  299. exit
  300. ",
  301. )
  302. .unwrap();
  303. let mem = &mut [0x11, 0x22];
  304. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  305. #[cfg(not(feature = "std"))]
  306. let mut exec_mem = alloc_exec_memory();
  307. #[cfg(not(feature = "std"))]
  308. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  309. vm.jit_compile().unwrap();
  310. unsafe {
  311. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1122);
  312. }
  313. }
  314. #[test]
  315. fn test_jit_be16_high() {
  316. let prog = assemble(
  317. "
  318. ldxdw r0, [r1]
  319. be16 r0
  320. exit
  321. ",
  322. )
  323. .unwrap();
  324. let mem = &mut [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88];
  325. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  326. #[cfg(not(feature = "std"))]
  327. let mut exec_mem = alloc_exec_memory();
  328. #[cfg(not(feature = "std"))]
  329. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  330. vm.jit_compile().unwrap();
  331. unsafe {
  332. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1122);
  333. }
  334. }
  335. #[test]
  336. fn test_jit_be32() {
  337. let prog = assemble(
  338. "
  339. ldxw r0, [r1]
  340. be32 r0
  341. exit
  342. ",
  343. )
  344. .unwrap();
  345. let mem = &mut [0x11, 0x22, 0x33, 0x44];
  346. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  347. #[cfg(not(feature = "std"))]
  348. let mut exec_mem = alloc_exec_memory();
  349. #[cfg(not(feature = "std"))]
  350. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  351. vm.jit_compile().unwrap();
  352. unsafe {
  353. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11223344);
  354. }
  355. }
  356. #[test]
  357. fn test_jit_be32_high() {
  358. let prog = assemble(
  359. "
  360. ldxdw r0, [r1]
  361. be32 r0
  362. exit
  363. ",
  364. )
  365. .unwrap();
  366. let mem = &mut [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88];
  367. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  368. #[cfg(not(feature = "std"))]
  369. let mut exec_mem = alloc_exec_memory();
  370. #[cfg(not(feature = "std"))]
  371. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  372. vm.jit_compile().unwrap();
  373. unsafe {
  374. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11223344);
  375. }
  376. }
  377. #[test]
  378. fn test_jit_be64() {
  379. let prog = assemble(
  380. "
  381. ldxdw r0, [r1]
  382. be64 r0
  383. exit
  384. ",
  385. )
  386. .unwrap();
  387. let mem = &mut [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88];
  388. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  389. #[cfg(not(feature = "std"))]
  390. let mut exec_mem = alloc_exec_memory();
  391. #[cfg(not(feature = "std"))]
  392. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  393. vm.jit_compile().unwrap();
  394. unsafe {
  395. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1122334455667788);
  396. }
  397. }
  398. #[test]
  399. fn test_jit_call() {
  400. let prog = assemble(
  401. "
  402. mov r1, 1
  403. mov r2, 2
  404. mov r3, 3
  405. mov r4, 4
  406. mov r5, 5
  407. call 0
  408. exit
  409. ",
  410. )
  411. .unwrap();
  412. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  413. vm.register_helper(0, helpers::gather_bytes).unwrap();
  414. #[cfg(not(feature = "std"))]
  415. let mut exec_mem = alloc_exec_memory();
  416. #[cfg(not(feature = "std"))]
  417. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  418. vm.jit_compile().unwrap();
  419. unsafe {
  420. assert_eq!(vm.execute_program_jit().unwrap(), 0x0102030405);
  421. }
  422. }
  423. #[test]
  424. fn test_jit_call_memfrob() {
  425. let prog = assemble(
  426. "
  427. mov r6, r1
  428. add r1, 2
  429. mov r2, 4
  430. call 1
  431. ldxdw r0, [r6]
  432. be64 r0
  433. exit
  434. ",
  435. )
  436. .unwrap();
  437. let mem = &mut [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];
  438. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  439. vm.register_helper(1, helpers::memfrob).unwrap();
  440. #[cfg(not(feature = "std"))]
  441. let mut exec_mem = alloc_exec_memory();
  442. #[cfg(not(feature = "std"))]
  443. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  444. vm.jit_compile().unwrap();
  445. unsafe {
  446. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x102292e2f2c0708);
  447. }
  448. }
  449. // TODO: helpers::trash_registers needs asm!().
  450. // Try this again once asm!() is available in stable.
  451. //#[test]
  452. //fn test_jit_call_save() {
  453. // #[rustfmt::skip]
  454. // let prog = &[
  455. // 0xb7, 0x06, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  456. // 0xb7, 0x07, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  457. // 0xb7, 0x08, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
  458. // 0xb7, 0x09, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00,
  459. // 0x85, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  460. // 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  461. // 0x4f, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  462. // 0x4f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  463. // 0x4f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  464. // 0x4f, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  465. // 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  466. // ];
  467. // let mut vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  468. // vm.register_helper(2, helpers::trash_registers);
  469. // vm.jit_compile().unwrap();
  470. // unsafe { assert_eq!(vm.execute_program_jit().unwrap(), 0x4321); }
  471. //}
  472. #[test]
  473. fn test_jit_div32_high_divisor() {
  474. let prog = assemble(
  475. "
  476. mov r0, 12
  477. lddw r1, 0x100000004
  478. div32 r0, r1
  479. exit
  480. ",
  481. )
  482. .unwrap();
  483. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  484. #[cfg(not(feature = "std"))]
  485. let mut exec_mem = alloc_exec_memory();
  486. #[cfg(not(feature = "std"))]
  487. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  488. vm.jit_compile().unwrap();
  489. unsafe {
  490. assert_eq!(vm.execute_program_jit().unwrap(), 0x3);
  491. }
  492. }
  493. #[test]
  494. fn test_jit_div32_imm() {
  495. let prog = assemble(
  496. "
  497. lddw r0, 0x10000000c
  498. div32 r0, 4
  499. exit
  500. ",
  501. )
  502. .unwrap();
  503. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  504. #[cfg(not(feature = "std"))]
  505. let mut exec_mem = alloc_exec_memory();
  506. #[cfg(not(feature = "std"))]
  507. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  508. vm.jit_compile().unwrap();
  509. unsafe {
  510. assert_eq!(vm.execute_program_jit().unwrap(), 0x3);
  511. }
  512. }
  513. #[test]
  514. fn test_jit_div32_reg() {
  515. let prog = assemble(
  516. "
  517. lddw r0, 0x10000000c
  518. mov r1, 4
  519. div32 r0, r1
  520. exit
  521. ",
  522. )
  523. .unwrap();
  524. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  525. #[cfg(not(feature = "std"))]
  526. let mut exec_mem = alloc_exec_memory();
  527. #[cfg(not(feature = "std"))]
  528. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  529. vm.jit_compile().unwrap();
  530. unsafe {
  531. assert_eq!(vm.execute_program_jit().unwrap(), 0x3);
  532. }
  533. }
  534. #[test]
  535. fn test_jit_div64_imm() {
  536. let prog = assemble(
  537. "
  538. mov r0, 0xc
  539. lsh r0, 32
  540. div r0, 4
  541. exit
  542. ",
  543. )
  544. .unwrap();
  545. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  546. #[cfg(not(feature = "std"))]
  547. let mut exec_mem = alloc_exec_memory();
  548. #[cfg(not(feature = "std"))]
  549. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  550. vm.jit_compile().unwrap();
  551. unsafe {
  552. assert_eq!(vm.execute_program_jit().unwrap(), 0x300000000);
  553. }
  554. }
  555. #[test]
  556. fn test_jit_div64_reg() {
  557. let prog = assemble(
  558. "
  559. mov r0, 0xc
  560. lsh r0, 32
  561. mov r1, 4
  562. div r0, r1
  563. exit
  564. ",
  565. )
  566. .unwrap();
  567. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  568. #[cfg(not(feature = "std"))]
  569. let mut exec_mem = alloc_exec_memory();
  570. #[cfg(not(feature = "std"))]
  571. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  572. vm.jit_compile().unwrap();
  573. unsafe {
  574. assert_eq!(vm.execute_program_jit().unwrap(), 0x300000000);
  575. }
  576. }
  577. // For some register numbers, we don't emit the same instructions for handling divisions by zero,
  578. // which means we don't use the same offset to skip these instructions when the divisor is not
  579. // zero. We've had a regression because of this before, make sure we test it.
  580. #[test]
  581. fn test_jit_div32_highreg() {
  582. let prog = assemble(
  583. "
  584. mov r0, 2
  585. mov r7, 4
  586. div32 r7, r0
  587. exit
  588. ",
  589. )
  590. .unwrap();
  591. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  592. #[cfg(not(feature = "std"))]
  593. let mut exec_mem = alloc_exec_memory();
  594. #[cfg(not(feature = "std"))]
  595. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  596. vm.jit_compile().unwrap();
  597. unsafe {
  598. assert_eq!(vm.execute_program_jit().unwrap(), 0x2);
  599. }
  600. }
  601. #[test]
  602. fn test_jit_div64_highreg() {
  603. let prog = assemble(
  604. "
  605. mov r0, 2
  606. mov r7, 4
  607. div r7, r0
  608. exit
  609. ",
  610. )
  611. .unwrap();
  612. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  613. #[cfg(not(feature = "std"))]
  614. let mut exec_mem = alloc_exec_memory();
  615. #[cfg(not(feature = "std"))]
  616. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  617. vm.jit_compile().unwrap();
  618. unsafe {
  619. assert_eq!(vm.execute_program_jit().unwrap(), 0x2);
  620. }
  621. }
  622. #[test]
  623. fn test_jit_early_exit() {
  624. let prog = assemble(
  625. "
  626. mov r0, 3
  627. exit
  628. mov r0, 4
  629. exit
  630. ",
  631. )
  632. .unwrap();
  633. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  634. #[cfg(not(feature = "std"))]
  635. let mut exec_mem = alloc_exec_memory();
  636. #[cfg(not(feature = "std"))]
  637. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  638. vm.jit_compile().unwrap();
  639. unsafe {
  640. assert_eq!(vm.execute_program_jit().unwrap(), 0x3);
  641. }
  642. }
  643. // uBPF limits the number of user functions at 64. We don't.
  644. //#[test]
  645. //fn test_jit_err_call_bad_imm() {
  646. //}
  647. #[test]
  648. #[should_panic(expected = "[JIT] Error: unknown helper function (id: 0x3f)")]
  649. fn test_jit_err_call_unreg() {
  650. let prog = assemble(
  651. "
  652. mov r1, 1
  653. mov r2, 2
  654. mov r3, 3
  655. mov r4, 4
  656. mov r5, 5
  657. call 63
  658. exit
  659. ",
  660. )
  661. .unwrap();
  662. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  663. #[cfg(not(feature = "std"))]
  664. let mut exec_mem = alloc_exec_memory();
  665. #[cfg(not(feature = "std"))]
  666. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  667. vm.jit_compile().unwrap();
  668. unsafe {
  669. vm.execute_program_jit().unwrap();
  670. }
  671. }
  672. #[test]
  673. fn test_jit_div64_by_zero_imm() {
  674. let prog = assemble(
  675. "
  676. mov32 r0, 1
  677. div r0, 0
  678. exit
  679. ",
  680. )
  681. .unwrap();
  682. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  683. #[cfg(not(feature = "std"))]
  684. let mut exec_mem = alloc_exec_memory();
  685. #[cfg(not(feature = "std"))]
  686. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  687. vm.jit_compile().unwrap();
  688. unsafe {
  689. assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
  690. }
  691. }
  692. #[test]
  693. fn test_jit_div_by_zero_imm() {
  694. let prog = assemble(
  695. "
  696. mov32 r0, 1
  697. div32 r0, 0
  698. exit
  699. ",
  700. )
  701. .unwrap();
  702. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  703. #[cfg(not(feature = "std"))]
  704. let mut exec_mem = alloc_exec_memory();
  705. #[cfg(not(feature = "std"))]
  706. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  707. vm.jit_compile().unwrap();
  708. unsafe {
  709. assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
  710. }
  711. }
  712. #[test]
  713. fn test_jit_mod64_by_zero_imm() {
  714. let prog = assemble(
  715. "
  716. mov32 r0, 1
  717. mod r0, 0
  718. exit
  719. ",
  720. )
  721. .unwrap();
  722. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  723. #[cfg(not(feature = "std"))]
  724. let mut exec_mem = alloc_exec_memory();
  725. #[cfg(not(feature = "std"))]
  726. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  727. vm.jit_compile().unwrap();
  728. unsafe {
  729. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  730. }
  731. }
  732. #[test]
  733. fn test_jit_mod_by_zero_imm() {
  734. let prog = assemble(
  735. "
  736. mov32 r0, 1
  737. mod32 r0, 0
  738. exit
  739. ",
  740. )
  741. .unwrap();
  742. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  743. #[cfg(not(feature = "std"))]
  744. let mut exec_mem = alloc_exec_memory();
  745. #[cfg(not(feature = "std"))]
  746. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  747. vm.jit_compile().unwrap();
  748. unsafe {
  749. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  750. }
  751. }
  752. #[test]
  753. fn test_jit_div64_by_zero_reg() {
  754. let prog = assemble(
  755. "
  756. mov32 r0, 1
  757. mov32 r1, 0
  758. div r0, r1
  759. exit
  760. ",
  761. )
  762. .unwrap();
  763. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  764. #[cfg(not(feature = "std"))]
  765. let mut exec_mem = alloc_exec_memory();
  766. #[cfg(not(feature = "std"))]
  767. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  768. vm.jit_compile().unwrap();
  769. unsafe {
  770. assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
  771. }
  772. }
  773. #[test]
  774. fn test_jit_div_by_zero_reg() {
  775. let prog = assemble(
  776. "
  777. mov32 r0, 1
  778. mov32 r1, 0
  779. div32 r0, r1
  780. exit
  781. ",
  782. )
  783. .unwrap();
  784. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  785. #[cfg(not(feature = "std"))]
  786. let mut exec_mem = alloc_exec_memory();
  787. #[cfg(not(feature = "std"))]
  788. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  789. vm.jit_compile().unwrap();
  790. unsafe {
  791. assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
  792. }
  793. }
  794. #[test]
  795. fn test_jit_mod64_by_zero_reg() {
  796. let prog = assemble(
  797. "
  798. mov32 r0, 1
  799. mov32 r1, 0
  800. mod r0, r1
  801. exit
  802. ",
  803. )
  804. .unwrap();
  805. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  806. #[cfg(not(feature = "std"))]
  807. let mut exec_mem = alloc_exec_memory();
  808. #[cfg(not(feature = "std"))]
  809. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  810. vm.jit_compile().unwrap();
  811. unsafe {
  812. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  813. }
  814. }
  815. #[test]
  816. fn test_jit_mod_by_zero_reg() {
  817. let prog = assemble(
  818. "
  819. mov32 r0, 1
  820. mov32 r1, 0
  821. mod32 r0, r1
  822. exit
  823. ",
  824. )
  825. .unwrap();
  826. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  827. #[cfg(not(feature = "std"))]
  828. let mut exec_mem = alloc_exec_memory();
  829. #[cfg(not(feature = "std"))]
  830. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  831. vm.jit_compile().unwrap();
  832. unsafe {
  833. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  834. }
  835. }
  836. // TODO SKIP: JIT disabled for this testcase (stack oob check not implemented)
  837. // #[test]
  838. // #[should_panic(expected = "Error: out of bounds memory store (insn #1)")]
  839. // fn test_jit_err_stack_out_of_bound() {
  840. // #[rustfmt::skip]
  841. // let prog = &[
  842. // 0x72, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  843. // 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  844. // ];
  845. // let mut vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  846. // vm.jit_compile().unwrap();
  847. // unsafe { vm.execute_program_jit().unwrap(); }
  848. // }
  849. #[test]
  850. fn test_jit_exit() {
  851. let prog = assemble(
  852. "
  853. mov r0, 0
  854. exit
  855. ",
  856. )
  857. .unwrap();
  858. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  859. #[cfg(not(feature = "std"))]
  860. let mut exec_mem = alloc_exec_memory();
  861. #[cfg(not(feature = "std"))]
  862. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  863. vm.jit_compile().unwrap();
  864. unsafe {
  865. assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
  866. }
  867. }
  868. #[test]
  869. fn test_jit_ja() {
  870. let prog = assemble(
  871. "
  872. mov r0, 1
  873. ja +1
  874. mov r0, 2
  875. exit
  876. ",
  877. )
  878. .unwrap();
  879. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  880. #[cfg(not(feature = "std"))]
  881. let mut exec_mem = alloc_exec_memory();
  882. #[cfg(not(feature = "std"))]
  883. vm.set_jit_exec_memory(&mut exec_mem).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_jeq_imm() {
  891. let prog = assemble(
  892. "
  893. mov32 r0, 0
  894. mov32 r1, 0xa
  895. jeq r1, 0xb, +4
  896. mov32 r0, 1
  897. mov32 r1, 0xb
  898. jeq r1, 0xb, +1
  899. mov32 r0, 2
  900. exit
  901. ",
  902. )
  903. .unwrap();
  904. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  905. #[cfg(not(feature = "std"))]
  906. let mut exec_mem = alloc_exec_memory();
  907. #[cfg(not(feature = "std"))]
  908. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  909. vm.jit_compile().unwrap();
  910. unsafe {
  911. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  912. }
  913. }
  914. #[test]
  915. fn test_jit_jeq_reg() {
  916. let prog = assemble(
  917. "
  918. mov32 r0, 0
  919. mov32 r1, 0xa
  920. mov32 r2, 0xb
  921. jeq r1, r2, +4
  922. mov32 r0, 1
  923. mov32 r1, 0xb
  924. jeq r1, r2, +1
  925. mov32 r0, 2
  926. exit
  927. ",
  928. )
  929. .unwrap();
  930. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  931. #[cfg(not(feature = "std"))]
  932. let mut exec_mem = alloc_exec_memory();
  933. #[cfg(not(feature = "std"))]
  934. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  935. vm.jit_compile().unwrap();
  936. unsafe {
  937. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  938. }
  939. }
  940. #[test]
  941. fn test_jit_jge_imm() {
  942. let prog = assemble(
  943. "
  944. mov32 r0, 0
  945. mov32 r1, 0xa
  946. jge r1, 0xb, +4
  947. mov32 r0, 1
  948. mov32 r1, 0xc
  949. jge r1, 0xb, +1
  950. mov32 r0, 2
  951. exit
  952. ",
  953. )
  954. .unwrap();
  955. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  956. #[cfg(not(feature = "std"))]
  957. let mut exec_mem = alloc_exec_memory();
  958. #[cfg(not(feature = "std"))]
  959. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  960. vm.jit_compile().unwrap();
  961. unsafe {
  962. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  963. }
  964. }
  965. #[test]
  966. fn test_jit_jle_imm() {
  967. let prog = assemble(
  968. "
  969. mov32 r0, 0
  970. mov32 r1, 5
  971. jle r1, 4, +1
  972. jle r1, 6, +1
  973. exit
  974. jle r1, 5, +1
  975. exit
  976. mov32 r0, 1
  977. exit
  978. ",
  979. )
  980. .unwrap();
  981. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  982. #[cfg(not(feature = "std"))]
  983. let mut exec_mem = alloc_exec_memory();
  984. #[cfg(not(feature = "std"))]
  985. vm.set_jit_exec_memory(&mut exec_mem).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_jle_reg() {
  993. let prog = assemble(
  994. "
  995. mov r0, 0
  996. mov r1, 5
  997. mov r2, 4
  998. mov r3, 6
  999. jle r1, r2, +2
  1000. jle r1, r1, +1
  1001. exit
  1002. jle r1, r3, +1
  1003. exit
  1004. mov r0, 1
  1005. exit
  1006. ",
  1007. )
  1008. .unwrap();
  1009. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1010. #[cfg(not(feature = "std"))]
  1011. let mut exec_mem = alloc_exec_memory();
  1012. #[cfg(not(feature = "std"))]
  1013. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1014. vm.jit_compile().unwrap();
  1015. unsafe {
  1016. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1017. }
  1018. }
  1019. #[test]
  1020. fn test_jit_jgt_imm() {
  1021. let prog = assemble(
  1022. "
  1023. mov32 r0, 0
  1024. mov32 r1, 5
  1025. jgt r1, 6, +2
  1026. jgt r1, 5, +1
  1027. jgt r1, 4, +1
  1028. exit
  1029. mov32 r0, 1
  1030. exit
  1031. ",
  1032. )
  1033. .unwrap();
  1034. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1035. #[cfg(not(feature = "std"))]
  1036. let mut exec_mem = alloc_exec_memory();
  1037. #[cfg(not(feature = "std"))]
  1038. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1039. vm.jit_compile().unwrap();
  1040. unsafe {
  1041. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1042. }
  1043. }
  1044. #[test]
  1045. fn test_jit_jgt_reg() {
  1046. let prog = assemble(
  1047. "
  1048. mov r0, 0
  1049. mov r1, 5
  1050. mov r2, 6
  1051. mov r3, 4
  1052. jgt r1, r2, +2
  1053. jgt r1, r1, +1
  1054. jgt r1, r3, +1
  1055. exit
  1056. mov r0, 1
  1057. exit
  1058. ",
  1059. )
  1060. .unwrap();
  1061. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1062. #[cfg(not(feature = "std"))]
  1063. let mut exec_mem = alloc_exec_memory();
  1064. #[cfg(not(feature = "std"))]
  1065. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1066. vm.jit_compile().unwrap();
  1067. unsafe {
  1068. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1069. }
  1070. }
  1071. #[test]
  1072. fn test_jit_jlt_imm() {
  1073. let prog = assemble(
  1074. "
  1075. mov32 r0, 0
  1076. mov32 r1, 5
  1077. jlt r1, 4, +2
  1078. jlt r1, 5, +1
  1079. jlt r1, 6, +1
  1080. exit
  1081. mov32 r0, 1
  1082. exit
  1083. ",
  1084. )
  1085. .unwrap();
  1086. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1087. #[cfg(not(feature = "std"))]
  1088. let mut exec_mem = alloc_exec_memory();
  1089. #[cfg(not(feature = "std"))]
  1090. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1091. vm.jit_compile().unwrap();
  1092. unsafe {
  1093. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1094. }
  1095. }
  1096. #[test]
  1097. fn test_jit_jlt_reg() {
  1098. let prog = assemble(
  1099. "
  1100. mov r0, 0
  1101. mov r1, 5
  1102. mov r2, 4
  1103. mov r3, 6
  1104. jlt r1, r2, +2
  1105. jlt r1, r1, +1
  1106. jlt r1, r3, +1
  1107. exit
  1108. mov r0, 1
  1109. exit
  1110. ",
  1111. )
  1112. .unwrap();
  1113. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1114. #[cfg(not(feature = "std"))]
  1115. let mut exec_mem = alloc_exec_memory();
  1116. #[cfg(not(feature = "std"))]
  1117. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1118. vm.jit_compile().unwrap();
  1119. unsafe {
  1120. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1121. }
  1122. }
  1123. #[test]
  1124. fn test_jit_jit_bounce() {
  1125. let prog = assemble(
  1126. "
  1127. mov r0, 1
  1128. mov r6, r0
  1129. mov r7, r6
  1130. mov r8, r7
  1131. mov r9, r8
  1132. mov r0, r9
  1133. exit
  1134. ",
  1135. )
  1136. .unwrap();
  1137. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1138. #[cfg(not(feature = "std"))]
  1139. let mut exec_mem = alloc_exec_memory();
  1140. #[cfg(not(feature = "std"))]
  1141. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1142. vm.jit_compile().unwrap();
  1143. unsafe {
  1144. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1145. }
  1146. }
  1147. #[test]
  1148. fn test_jit_jne_reg() {
  1149. let prog = assemble(
  1150. "
  1151. mov32 r0, 0
  1152. mov32 r1, 0xb
  1153. mov32 r2, 0xb
  1154. jne r1, r2, +4
  1155. mov32 r0, 1
  1156. mov32 r1, 0xa
  1157. jne r1, r2, +1
  1158. mov32 r0, 2
  1159. exit
  1160. ",
  1161. )
  1162. .unwrap();
  1163. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1164. #[cfg(not(feature = "std"))]
  1165. let mut exec_mem = alloc_exec_memory();
  1166. #[cfg(not(feature = "std"))]
  1167. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1168. vm.jit_compile().unwrap();
  1169. unsafe {
  1170. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1171. }
  1172. }
  1173. #[test]
  1174. fn test_jit_jset_imm() {
  1175. let prog = assemble(
  1176. "
  1177. mov32 r0, 0
  1178. mov32 r1, 0x7
  1179. jset r1, 0x8, +4
  1180. mov32 r0, 1
  1181. mov32 r1, 0x9
  1182. jset r1, 0x8, +1
  1183. mov32 r0, 2
  1184. exit
  1185. ",
  1186. )
  1187. .unwrap();
  1188. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1189. #[cfg(not(feature = "std"))]
  1190. let mut exec_mem = alloc_exec_memory();
  1191. #[cfg(not(feature = "std"))]
  1192. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1193. vm.jit_compile().unwrap();
  1194. unsafe {
  1195. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1196. }
  1197. }
  1198. #[test]
  1199. fn test_jit_jset_reg() {
  1200. let prog = assemble(
  1201. "
  1202. mov32 r0, 0
  1203. mov32 r1, 0x7
  1204. mov32 r2, 0x8
  1205. jset r1, r2, +4
  1206. mov32 r0, 1
  1207. mov32 r1, 0x9
  1208. jset r1, r2, +1
  1209. mov32 r0, 2
  1210. exit
  1211. ",
  1212. )
  1213. .unwrap();
  1214. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1215. #[cfg(not(feature = "std"))]
  1216. let mut exec_mem = alloc_exec_memory();
  1217. #[cfg(not(feature = "std"))]
  1218. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1219. vm.jit_compile().unwrap();
  1220. unsafe {
  1221. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1222. }
  1223. }
  1224. #[test]
  1225. fn test_jit_jsge_imm() {
  1226. let prog = assemble(
  1227. "
  1228. mov32 r0, 0
  1229. mov r1, -2
  1230. jsge r1, -1, +5
  1231. jsge r1, 0, +4
  1232. mov32 r0, 1
  1233. mov r1, -1
  1234. jsge r1, -1, +1
  1235. mov32 r0, 2
  1236. exit
  1237. ",
  1238. )
  1239. .unwrap();
  1240. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1241. #[cfg(not(feature = "std"))]
  1242. let mut exec_mem = alloc_exec_memory();
  1243. #[cfg(not(feature = "std"))]
  1244. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1245. vm.jit_compile().unwrap();
  1246. unsafe {
  1247. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1248. }
  1249. }
  1250. #[test]
  1251. fn test_jit_jsge_reg() {
  1252. let prog = assemble(
  1253. "
  1254. mov32 r0, 0
  1255. mov r1, -2
  1256. mov r2, -1
  1257. mov32 r3, 0
  1258. jsge r1, r2, +5
  1259. jsge r1, r3, +4
  1260. mov32 r0, 1
  1261. mov r1, r2
  1262. jsge r1, r2, +1
  1263. mov32 r0, 2
  1264. exit
  1265. ",
  1266. )
  1267. .unwrap();
  1268. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1269. #[cfg(not(feature = "std"))]
  1270. let mut exec_mem = alloc_exec_memory();
  1271. #[cfg(not(feature = "std"))]
  1272. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1273. vm.jit_compile().unwrap();
  1274. unsafe {
  1275. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1276. }
  1277. }
  1278. #[test]
  1279. fn test_jit_jsle_imm() {
  1280. let prog = assemble(
  1281. "
  1282. mov32 r0, 0
  1283. mov r1, -2
  1284. jsle r1, -3, +1
  1285. jsle r1, -1, +1
  1286. exit
  1287. mov32 r0, 1
  1288. jsle r1, -2, +1
  1289. mov32 r0, 2
  1290. exit
  1291. ",
  1292. )
  1293. .unwrap();
  1294. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1295. #[cfg(not(feature = "std"))]
  1296. let mut exec_mem = alloc_exec_memory();
  1297. #[cfg(not(feature = "std"))]
  1298. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1299. vm.jit_compile().unwrap();
  1300. unsafe {
  1301. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1302. }
  1303. }
  1304. #[test]
  1305. fn test_jit_jsle_reg() {
  1306. let prog = assemble(
  1307. "
  1308. mov32 r0, 0
  1309. mov r1, -1
  1310. mov r2, -2
  1311. mov32 r3, 0
  1312. jsle r1, r2, +1
  1313. jsle r1, r3, +1
  1314. exit
  1315. mov32 r0, 1
  1316. mov r1, r2
  1317. jsle r1, r2, +1
  1318. mov32 r0, 2
  1319. exit
  1320. ",
  1321. )
  1322. .unwrap();
  1323. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1324. #[cfg(not(feature = "std"))]
  1325. let mut exec_mem = alloc_exec_memory();
  1326. #[cfg(not(feature = "std"))]
  1327. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1328. vm.jit_compile().unwrap();
  1329. unsafe {
  1330. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1331. }
  1332. }
  1333. #[test]
  1334. fn test_jit_jsgt_imm() {
  1335. let prog = assemble(
  1336. "
  1337. mov32 r0, 0
  1338. mov r1, -2
  1339. jsgt r1, -1, +4
  1340. mov32 r0, 1
  1341. mov32 r1, 0
  1342. jsgt r1, -1, +1
  1343. mov32 r0, 2
  1344. exit
  1345. ",
  1346. )
  1347. .unwrap();
  1348. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1349. #[cfg(not(feature = "std"))]
  1350. let mut exec_mem = alloc_exec_memory();
  1351. #[cfg(not(feature = "std"))]
  1352. vm.set_jit_exec_memory(&mut exec_mem).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_jsgt_reg() {
  1360. let prog = assemble(
  1361. "
  1362. mov32 r0, 0
  1363. mov r1, -2
  1364. mov r2, -1
  1365. jsgt r1, r2, +4
  1366. mov32 r0, 1
  1367. mov32 r1, 0
  1368. jsgt r1, r2, +1
  1369. mov32 r0, 2
  1370. exit
  1371. ",
  1372. )
  1373. .unwrap();
  1374. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1375. #[cfg(not(feature = "std"))]
  1376. let mut exec_mem = alloc_exec_memory();
  1377. #[cfg(not(feature = "std"))]
  1378. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1379. vm.jit_compile().unwrap();
  1380. unsafe {
  1381. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1382. }
  1383. }
  1384. #[test]
  1385. fn test_jit_jslt_imm() {
  1386. let prog = assemble(
  1387. "
  1388. mov32 r0, 0
  1389. mov r1, -2
  1390. jslt r1, -3, +2
  1391. jslt r1, -2, +1
  1392. jslt r1, -1, +1
  1393. exit
  1394. mov32 r0, 1
  1395. exit
  1396. ",
  1397. )
  1398. .unwrap();
  1399. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1400. #[cfg(not(feature = "std"))]
  1401. let mut exec_mem = alloc_exec_memory();
  1402. #[cfg(not(feature = "std"))]
  1403. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1404. vm.jit_compile().unwrap();
  1405. unsafe {
  1406. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1407. }
  1408. }
  1409. #[test]
  1410. fn test_jit_jslt_reg() {
  1411. let prog = assemble(
  1412. "
  1413. mov32 r0, 0
  1414. mov r1, -2
  1415. mov r2, -3
  1416. mov r3, -1
  1417. jslt r1, r1, +2
  1418. jslt r1, r2, +1
  1419. jslt r1, r3, +1
  1420. exit
  1421. mov32 r0, 1
  1422. exit
  1423. ",
  1424. )
  1425. .unwrap();
  1426. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1427. #[cfg(not(feature = "std"))]
  1428. let mut exec_mem = alloc_exec_memory();
  1429. #[cfg(not(feature = "std"))]
  1430. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1431. vm.jit_compile().unwrap();
  1432. unsafe {
  1433. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1434. }
  1435. }
  1436. #[test]
  1437. fn test_jit_jeq32_imm() {
  1438. let prog = assemble(
  1439. "
  1440. mov r9, 1
  1441. lsh r9, 32
  1442. mov32 r0, 0x0
  1443. mov32 r1, 0xa
  1444. jeq32 r1, 0xb, +5
  1445. mov32 r0, 1
  1446. mov r1, 0xb
  1447. or r1, r9
  1448. jeq32 r1, 0xb, +1
  1449. mov32 r0, 2
  1450. exit
  1451. ",
  1452. )
  1453. .unwrap();
  1454. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1455. #[cfg(not(feature = "std"))]
  1456. let mut exec_mem = alloc_exec_memory();
  1457. #[cfg(not(feature = "std"))]
  1458. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1459. vm.jit_compile().unwrap();
  1460. unsafe {
  1461. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1462. }
  1463. }
  1464. #[test]
  1465. fn test_jit_jeq32_reg() {
  1466. let prog = assemble(
  1467. "
  1468. mov r9, 1
  1469. lsh r9, 32
  1470. mov32 r0, 0
  1471. mov32 r1, 0xa
  1472. mov32 r2, 0xb
  1473. jeq32 r1, r2, +5
  1474. mov32 r0, 1
  1475. mov32 r1, 0xb
  1476. or r1, r9
  1477. jeq32 r1, r2, +1
  1478. mov32 r0, 2
  1479. exit
  1480. ",
  1481. )
  1482. .unwrap();
  1483. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1484. #[cfg(not(feature = "std"))]
  1485. let mut exec_mem = alloc_exec_memory();
  1486. #[cfg(not(feature = "std"))]
  1487. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1488. vm.jit_compile().unwrap();
  1489. unsafe {
  1490. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1491. }
  1492. }
  1493. #[test]
  1494. fn test_jit_jge32_imm() {
  1495. let prog = assemble(
  1496. "
  1497. mov r9, 1
  1498. lsh r9, 32
  1499. mov32 r0, 0
  1500. mov32 r1, 0xa
  1501. jge32 r1, 0xb, +5
  1502. mov32 r0, 1
  1503. or r1, r9
  1504. mov32 r1, 0xc
  1505. jge32 r1, 0xb, +1
  1506. mov32 r0, 2
  1507. exit
  1508. ",
  1509. )
  1510. .unwrap();
  1511. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1512. #[cfg(not(feature = "std"))]
  1513. let mut exec_mem = alloc_exec_memory();
  1514. #[cfg(not(feature = "std"))]
  1515. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1516. vm.jit_compile().unwrap();
  1517. unsafe {
  1518. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1519. }
  1520. }
  1521. #[test]
  1522. fn test_jit_jge32_reg() {
  1523. let prog = assemble(
  1524. "
  1525. mov r9, 1
  1526. lsh r9, 32
  1527. mov32 r0, 0
  1528. mov32 r1, 0xa
  1529. mov32 r2, 0xb
  1530. jge32 r1, r2, +5
  1531. mov32 r0, 1
  1532. or r1, r9
  1533. mov32 r1, 0xc
  1534. jge32 r1, r2, +1
  1535. mov32 r0, 2
  1536. exit
  1537. ",
  1538. )
  1539. .unwrap();
  1540. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1541. #[cfg(not(feature = "std"))]
  1542. let mut exec_mem = alloc_exec_memory();
  1543. #[cfg(not(feature = "std"))]
  1544. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1545. vm.jit_compile().unwrap();
  1546. unsafe {
  1547. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1548. }
  1549. }
  1550. #[test]
  1551. fn test_jit_jgt32_imm() {
  1552. let prog = assemble(
  1553. "
  1554. mov r9, 1
  1555. lsh r9, 32
  1556. mov32 r0, 0
  1557. mov32 r1, 5
  1558. or r1, r9
  1559. jgt32 r1, 6, +4
  1560. jgt32 r1, 5, +3
  1561. jgt32 r1, 4, +1
  1562. exit
  1563. mov32 r0, 1
  1564. exit
  1565. ",
  1566. )
  1567. .unwrap();
  1568. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1569. #[cfg(not(feature = "std"))]
  1570. let mut exec_mem = alloc_exec_memory();
  1571. #[cfg(not(feature = "std"))]
  1572. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1573. vm.jit_compile().unwrap();
  1574. unsafe {
  1575. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1576. }
  1577. }
  1578. #[test]
  1579. fn test_jit_jgt32_reg() {
  1580. let prog = assemble(
  1581. "
  1582. mov r9, 1
  1583. lsh r9, 32
  1584. mov r0, 0
  1585. mov r1, 5
  1586. mov32 r1, 5
  1587. or r1, r9
  1588. mov r2, 6
  1589. mov r3, 4
  1590. jgt32 r1, r2, +4
  1591. jgt32 r1, r1, +3
  1592. jgt32 r1, r3, +1
  1593. exit
  1594. mov r0, 1
  1595. exit
  1596. ",
  1597. )
  1598. .unwrap();
  1599. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1600. #[cfg(not(feature = "std"))]
  1601. let mut exec_mem = alloc_exec_memory();
  1602. #[cfg(not(feature = "std"))]
  1603. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1604. vm.jit_compile().unwrap();
  1605. unsafe {
  1606. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1607. }
  1608. }
  1609. #[test]
  1610. fn test_jit_jle32_imm() {
  1611. let prog = assemble(
  1612. "
  1613. mov r9, 1
  1614. lsh r9, 32
  1615. mov32 r0, 0
  1616. mov32 r1, 5
  1617. or r1, r9
  1618. jle32 r1, 4, +5
  1619. jle32 r1, 6, +1
  1620. exit
  1621. jle32 r1, 5, +1
  1622. exit
  1623. mov32 r0, 1
  1624. exit
  1625. ",
  1626. )
  1627. .unwrap();
  1628. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1629. #[cfg(not(feature = "std"))]
  1630. let mut exec_mem = alloc_exec_memory();
  1631. #[cfg(not(feature = "std"))]
  1632. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1633. vm.jit_compile().unwrap();
  1634. unsafe {
  1635. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1636. }
  1637. }
  1638. #[test]
  1639. fn test_jit_jle32_reg() {
  1640. let prog = assemble(
  1641. "
  1642. mov r9, 1
  1643. lsh r9, 32
  1644. mov r0, 0
  1645. mov r1, 5
  1646. mov r2, 4
  1647. mov r3, 6
  1648. or r1, r9
  1649. jle32 r1, r2, +5
  1650. jle32 r1, r1, +1
  1651. exit
  1652. jle32 r1, r3, +1
  1653. exit
  1654. mov r0, 1
  1655. exit
  1656. ",
  1657. )
  1658. .unwrap();
  1659. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1660. #[cfg(not(feature = "std"))]
  1661. let mut exec_mem = alloc_exec_memory();
  1662. #[cfg(not(feature = "std"))]
  1663. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1664. vm.jit_compile().unwrap();
  1665. unsafe {
  1666. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1667. }
  1668. }
  1669. #[test]
  1670. fn test_jit_jlt32_imm() {
  1671. let prog = assemble(
  1672. "
  1673. mov r9, 1
  1674. lsh r9, 32
  1675. mov32 r0, 0
  1676. mov32 r1, 5
  1677. or r1, r9
  1678. jlt32 r1, 4, +4
  1679. jlt32 r1, 5, +3
  1680. jlt32 r1, 6, +1
  1681. exit
  1682. mov32 r0, 1
  1683. exit
  1684. ",
  1685. )
  1686. .unwrap();
  1687. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1688. #[cfg(not(feature = "std"))]
  1689. let mut exec_mem = alloc_exec_memory();
  1690. #[cfg(not(feature = "std"))]
  1691. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1692. vm.jit_compile().unwrap();
  1693. unsafe {
  1694. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1695. }
  1696. }
  1697. #[test]
  1698. fn test_jit_jlt32_reg() {
  1699. let prog = assemble(
  1700. "
  1701. mov r9, 1
  1702. lsh r9, 32
  1703. mov r0, 0
  1704. mov r1, 5
  1705. mov r2, 4
  1706. mov r3, 6
  1707. or r1, r9
  1708. jlt32 r1, r2, +4
  1709. jlt32 r1, r1, +3
  1710. jlt32 r1, r3, +1
  1711. exit
  1712. mov r0, 1
  1713. exit
  1714. ",
  1715. )
  1716. .unwrap();
  1717. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1718. #[cfg(not(feature = "std"))]
  1719. let mut exec_mem = alloc_exec_memory();
  1720. #[cfg(not(feature = "std"))]
  1721. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1722. vm.jit_compile().unwrap();
  1723. unsafe {
  1724. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1725. }
  1726. }
  1727. #[test]
  1728. fn test_jit_jne32_imm() {
  1729. let prog = assemble(
  1730. "
  1731. mov r9, 1
  1732. lsh r9, 32
  1733. mov32 r0, 0
  1734. mov32 r1, 0xb
  1735. or r1, r9
  1736. jne32 r1, 0xb, +4
  1737. mov32 r0, 1
  1738. mov32 r1, 0xa
  1739. or r1, r9
  1740. jne32 r1, 0xb, +1
  1741. mov32 r0, 2
  1742. exit
  1743. ",
  1744. )
  1745. .unwrap();
  1746. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1747. #[cfg(not(feature = "std"))]
  1748. let mut exec_mem = alloc_exec_memory();
  1749. #[cfg(not(feature = "std"))]
  1750. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1751. vm.jit_compile().unwrap();
  1752. unsafe {
  1753. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1754. }
  1755. }
  1756. #[test]
  1757. fn test_jit_jne32_reg() {
  1758. let prog = assemble(
  1759. "
  1760. mov r9, 1
  1761. lsh r9, 32
  1762. mov32 r0, 0
  1763. mov32 r1, 0xb
  1764. or r1, r9
  1765. mov32 r2, 0xb
  1766. jne32 r1, r2, +4
  1767. mov32 r0, 1
  1768. mov32 r1, 0xa
  1769. or r1, r9
  1770. jne32 r1, r2, +1
  1771. mov32 r0, 2
  1772. exit
  1773. ",
  1774. )
  1775. .unwrap();
  1776. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1777. #[cfg(not(feature = "std"))]
  1778. let mut exec_mem = alloc_exec_memory();
  1779. #[cfg(not(feature = "std"))]
  1780. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1781. vm.jit_compile().unwrap();
  1782. unsafe {
  1783. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1784. }
  1785. }
  1786. #[test]
  1787. fn test_jit_jset32_imm() {
  1788. let prog = assemble(
  1789. "
  1790. mov r9, 1
  1791. lsh r9, 32
  1792. mov32 r0, 0
  1793. mov32 r1, 0x7
  1794. or r1, r9
  1795. jset32 r1, 0x8, +4
  1796. mov32 r0, 1
  1797. mov32 r1, 0x9
  1798. jset32 r1, 0x8, +1
  1799. mov32 r0, 2
  1800. exit
  1801. ",
  1802. )
  1803. .unwrap();
  1804. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1805. #[cfg(not(feature = "std"))]
  1806. let mut exec_mem = alloc_exec_memory();
  1807. #[cfg(not(feature = "std"))]
  1808. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1809. vm.jit_compile().unwrap();
  1810. unsafe {
  1811. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1812. }
  1813. }
  1814. #[test]
  1815. fn test_jit_jset32_reg() {
  1816. let prog = assemble(
  1817. "
  1818. mov r9, 1
  1819. lsh r9, 32
  1820. mov32 r0, 0
  1821. mov32 r1, 0x7
  1822. or r1, r9
  1823. mov32 r2, 0x8
  1824. jset32 r1, r2, +4
  1825. mov32 r0, 1
  1826. mov32 r1, 0x9
  1827. jset32 r1, r2, +1
  1828. mov32 r0, 2
  1829. exit
  1830. ",
  1831. )
  1832. .unwrap();
  1833. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1834. #[cfg(not(feature = "std"))]
  1835. let mut exec_mem = alloc_exec_memory();
  1836. #[cfg(not(feature = "std"))]
  1837. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1838. vm.jit_compile().unwrap();
  1839. unsafe {
  1840. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1841. }
  1842. }
  1843. #[test]
  1844. fn test_jit_jsge32_imm() {
  1845. let prog = assemble(
  1846. "
  1847. mov r9, 1
  1848. lsh r9, 32
  1849. mov32 r0, 0
  1850. mov32 r1, -2
  1851. or r1, r9
  1852. jsge32 r1, -1, +5
  1853. jsge32 r1, 0, +4
  1854. mov32 r0, 1
  1855. mov r1, -1
  1856. jsge32 r1, -1, +1
  1857. mov32 r0, 2
  1858. exit
  1859. ",
  1860. )
  1861. .unwrap();
  1862. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1863. #[cfg(not(feature = "std"))]
  1864. let mut exec_mem = alloc_exec_memory();
  1865. #[cfg(not(feature = "std"))]
  1866. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1867. vm.jit_compile().unwrap();
  1868. unsafe {
  1869. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1870. }
  1871. }
  1872. #[test]
  1873. fn test_jit_jsge32_reg() {
  1874. let prog = assemble(
  1875. "
  1876. mov r9, 1
  1877. lsh r9, 32
  1878. mov32 r0, 0
  1879. mov32 r1, -2
  1880. or r1, r9
  1881. mov r2, -1
  1882. mov32 r3, 0
  1883. jsge32 r1, r2, +5
  1884. jsge32 r1, r3, +4
  1885. mov32 r0, 1
  1886. mov r1, r2
  1887. jsge32 r1, r2, +1
  1888. mov32 r0, 2
  1889. exit
  1890. ",
  1891. )
  1892. .unwrap();
  1893. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1894. #[cfg(not(feature = "std"))]
  1895. let mut exec_mem = alloc_exec_memory();
  1896. #[cfg(not(feature = "std"))]
  1897. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1898. vm.jit_compile().unwrap();
  1899. unsafe {
  1900. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1901. }
  1902. }
  1903. #[test]
  1904. fn test_jit_jsgt32_imm() {
  1905. let prog = assemble(
  1906. "
  1907. mov r9, 1
  1908. lsh r9, 32
  1909. mov32 r0, 0
  1910. mov32 r1, -2
  1911. or r1, r9
  1912. jsgt32 r1, -1, +4
  1913. mov32 r0, 1
  1914. mov32 r1, 0
  1915. jsgt32 r1, -1, +1
  1916. mov32 r0, 2
  1917. exit
  1918. ",
  1919. )
  1920. .unwrap();
  1921. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1922. #[cfg(not(feature = "std"))]
  1923. let mut exec_mem = alloc_exec_memory();
  1924. #[cfg(not(feature = "std"))]
  1925. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1926. vm.jit_compile().unwrap();
  1927. unsafe {
  1928. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1929. }
  1930. }
  1931. #[test]
  1932. fn test_jit_jsgt32_reg() {
  1933. let prog = assemble(
  1934. "
  1935. mov r9, 1
  1936. lsh r9, 32
  1937. mov32 r0, 0
  1938. mov32 r1, -2
  1939. or r1, r9
  1940. mov r2, -1
  1941. jsgt32 r1, r2, +4
  1942. mov32 r0, 1
  1943. mov32 r1, 0
  1944. jsgt32 r1, r2, +1
  1945. mov32 r0, 2
  1946. exit
  1947. ",
  1948. )
  1949. .unwrap();
  1950. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1951. #[cfg(not(feature = "std"))]
  1952. let mut exec_mem = alloc_exec_memory();
  1953. #[cfg(not(feature = "std"))]
  1954. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1955. vm.jit_compile().unwrap();
  1956. unsafe {
  1957. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1958. }
  1959. }
  1960. #[test]
  1961. fn test_jit_jsle32_imm() {
  1962. let prog = assemble(
  1963. "
  1964. mov r9, 1
  1965. lsh r9, 32
  1966. mov32 r0, 0
  1967. mov32 r1, -2
  1968. or r1, r9
  1969. jsle32 r1, -3, +5
  1970. jsle32 r1, -1, +1
  1971. exit
  1972. mov32 r0, 1
  1973. jsle32 r1, -2, +1
  1974. mov32 r0, 2
  1975. exit
  1976. ",
  1977. )
  1978. .unwrap();
  1979. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1980. #[cfg(not(feature = "std"))]
  1981. let mut exec_mem = alloc_exec_memory();
  1982. #[cfg(not(feature = "std"))]
  1983. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  1984. vm.jit_compile().unwrap();
  1985. unsafe {
  1986. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  1987. }
  1988. }
  1989. #[test]
  1990. fn test_jit_jsle32_reg() {
  1991. let prog = assemble(
  1992. "
  1993. mov r9, 1
  1994. lsh r9, 32
  1995. mov32 r0, 0
  1996. mov32 r1, -2
  1997. or r1, r9
  1998. mov r2, -3
  1999. mov32 r3, 0
  2000. jsle32 r1, r2, +6
  2001. jsle32 r1, r3, +1
  2002. exit
  2003. mov32 r0, 1
  2004. mov r1, r2
  2005. jsle32 r1, r2, +1
  2006. mov32 r0, 2
  2007. exit
  2008. ",
  2009. )
  2010. .unwrap();
  2011. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2012. #[cfg(not(feature = "std"))]
  2013. let mut exec_mem = alloc_exec_memory();
  2014. #[cfg(not(feature = "std"))]
  2015. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2016. vm.jit_compile().unwrap();
  2017. unsafe {
  2018. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  2019. }
  2020. }
  2021. #[test]
  2022. fn test_jit_jslt32_imm() {
  2023. let prog = assemble(
  2024. "
  2025. mov r9, 1
  2026. lsh r9, 32
  2027. mov32 r0, 0
  2028. mov32 r1, -2
  2029. or r1, r9
  2030. jslt32 r1, -3, +4
  2031. jslt32 r1, -2, +3
  2032. jslt32 r1, -1, +1
  2033. exit
  2034. mov32 r0, 1
  2035. exit
  2036. ",
  2037. )
  2038. .unwrap();
  2039. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2040. #[cfg(not(feature = "std"))]
  2041. let mut exec_mem = alloc_exec_memory();
  2042. #[cfg(not(feature = "std"))]
  2043. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2044. vm.jit_compile().unwrap();
  2045. unsafe {
  2046. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  2047. }
  2048. }
  2049. #[test]
  2050. fn test_jit_jslt32_reg() {
  2051. let prog = assemble(
  2052. "
  2053. mov r9, 1
  2054. lsh r9, 32
  2055. mov32 r0, 0
  2056. mov32 r1, -2
  2057. or r1, r9
  2058. mov r2, -3
  2059. mov r3, -1
  2060. jslt32 r1, r1, +4
  2061. jslt32 r1, r2, +3
  2062. jslt32 r1, r3, +1
  2063. exit
  2064. mov32 r0, 1
  2065. exit
  2066. ",
  2067. )
  2068. .unwrap();
  2069. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2070. #[cfg(not(feature = "std"))]
  2071. let mut exec_mem = alloc_exec_memory();
  2072. #[cfg(not(feature = "std"))]
  2073. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2074. vm.jit_compile().unwrap();
  2075. unsafe {
  2076. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  2077. }
  2078. }
  2079. #[test]
  2080. fn test_jit_lddw() {
  2081. let prog = assemble(
  2082. "
  2083. lddw r0, 0x1122334455667788
  2084. exit
  2085. ",
  2086. )
  2087. .unwrap();
  2088. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2089. #[cfg(not(feature = "std"))]
  2090. let mut exec_mem = alloc_exec_memory();
  2091. #[cfg(not(feature = "std"))]
  2092. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2093. vm.jit_compile().unwrap();
  2094. unsafe {
  2095. assert_eq!(vm.execute_program_jit().unwrap(), 0x1122334455667788);
  2096. }
  2097. }
  2098. #[test]
  2099. fn test_jit_lddw2() {
  2100. let prog = assemble(
  2101. "
  2102. lddw r0, 0x0000000080000000
  2103. exit
  2104. ",
  2105. )
  2106. .unwrap();
  2107. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2108. #[cfg(not(feature = "std"))]
  2109. let mut exec_mem = alloc_exec_memory();
  2110. #[cfg(not(feature = "std"))]
  2111. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2112. vm.jit_compile().unwrap();
  2113. unsafe {
  2114. assert_eq!(vm.execute_program_jit().unwrap(), 0x80000000);
  2115. }
  2116. }
  2117. #[test]
  2118. fn test_jit_ldxb_all() {
  2119. let prog = assemble(
  2120. "
  2121. mov r0, r1
  2122. ldxb r9, [r0+0]
  2123. lsh r9, 0
  2124. ldxb r8, [r0+1]
  2125. lsh r8, 4
  2126. ldxb r7, [r0+2]
  2127. lsh r7, 8
  2128. ldxb r6, [r0+3]
  2129. lsh r6, 12
  2130. ldxb r5, [r0+4]
  2131. lsh r5, 16
  2132. ldxb r4, [r0+5]
  2133. lsh r4, 20
  2134. ldxb r3, [r0+6]
  2135. lsh r3, 24
  2136. ldxb r2, [r0+7]
  2137. lsh r2, 28
  2138. ldxb r1, [r0+8]
  2139. lsh r1, 32
  2140. ldxb r0, [r0+9]
  2141. lsh r0, 36
  2142. or r0, r1
  2143. or r0, r2
  2144. or r0, r3
  2145. or r0, r4
  2146. or r0, r5
  2147. or r0, r6
  2148. or r0, r7
  2149. or r0, r8
  2150. or r0, r9
  2151. exit
  2152. ",
  2153. )
  2154. .unwrap();
  2155. #[rustfmt::skip]
  2156. let mem = &mut [
  2157. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  2158. 0x08, 0x09
  2159. ];
  2160. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2161. #[cfg(not(feature = "std"))]
  2162. let mut exec_mem = alloc_exec_memory();
  2163. #[cfg(not(feature = "std"))]
  2164. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2165. vm.jit_compile().unwrap();
  2166. unsafe {
  2167. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x9876543210);
  2168. }
  2169. }
  2170. #[test]
  2171. fn test_jit_ldxb() {
  2172. let prog = assemble(
  2173. "
  2174. ldxb r0, [r1+2]
  2175. exit
  2176. ",
  2177. )
  2178. .unwrap();
  2179. let mem = &mut [0xaa, 0xbb, 0x11, 0xcc, 0xdd];
  2180. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2181. #[cfg(not(feature = "std"))]
  2182. let mut exec_mem = alloc_exec_memory();
  2183. #[cfg(not(feature = "std"))]
  2184. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2185. vm.jit_compile().unwrap();
  2186. unsafe {
  2187. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11);
  2188. }
  2189. }
  2190. #[test]
  2191. fn test_jit_ldxdw() {
  2192. let prog = assemble(
  2193. "
  2194. ldxdw r0, [r1+2]
  2195. exit
  2196. ",
  2197. )
  2198. .unwrap();
  2199. #[rustfmt::skip]
  2200. let mem = &mut [
  2201. 0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
  2202. 0x77, 0x88, 0xcc, 0xdd
  2203. ];
  2204. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2205. #[cfg(not(feature = "std"))]
  2206. let mut exec_mem = alloc_exec_memory();
  2207. #[cfg(not(feature = "std"))]
  2208. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2209. vm.jit_compile().unwrap();
  2210. unsafe {
  2211. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x8877665544332211);
  2212. }
  2213. }
  2214. #[test]
  2215. fn test_jit_ldxh_all() {
  2216. let prog = assemble(
  2217. "
  2218. mov r0, r1
  2219. ldxh r9, [r0+0]
  2220. be16 r9
  2221. lsh r9, 0
  2222. ldxh r8, [r0+2]
  2223. be16 r8
  2224. lsh r8, 4
  2225. ldxh r7, [r0+4]
  2226. be16 r7
  2227. lsh r7, 8
  2228. ldxh r6, [r0+6]
  2229. be16 r6
  2230. lsh r6, 12
  2231. ldxh r5, [r0+8]
  2232. be16 r5
  2233. lsh r5, 16
  2234. ldxh r4, [r0+10]
  2235. be16 r4
  2236. lsh r4, 20
  2237. ldxh r3, [r0+12]
  2238. be16 r3
  2239. lsh r3, 24
  2240. ldxh r2, [r0+14]
  2241. be16 r2
  2242. lsh r2, 28
  2243. ldxh r1, [r0+16]
  2244. be16 r1
  2245. lsh r1, 32
  2246. ldxh r0, [r0+18]
  2247. be16 r0
  2248. lsh r0, 36
  2249. or r0, r1
  2250. or r0, r2
  2251. or r0, r3
  2252. or r0, r4
  2253. or r0, r5
  2254. or r0, r6
  2255. or r0, r7
  2256. or r0, r8
  2257. or r0, r9
  2258. exit
  2259. ",
  2260. )
  2261. .unwrap();
  2262. #[rustfmt::skip]
  2263. let mem = &mut [
  2264. 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03,
  2265. 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07,
  2266. 0x00, 0x08, 0x00, 0x09
  2267. ];
  2268. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2269. #[cfg(not(feature = "std"))]
  2270. let mut exec_mem = alloc_exec_memory();
  2271. #[cfg(not(feature = "std"))]
  2272. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2273. vm.jit_compile().unwrap();
  2274. unsafe {
  2275. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x9876543210);
  2276. }
  2277. }
  2278. #[test]
  2279. fn test_jit_ldxh_all2() {
  2280. let prog = assemble(
  2281. "
  2282. mov r0, r1
  2283. ldxh r9, [r0+0]
  2284. be16 r9
  2285. ldxh r8, [r0+2]
  2286. be16 r8
  2287. ldxh r7, [r0+4]
  2288. be16 r7
  2289. ldxh r6, [r0+6]
  2290. be16 r6
  2291. ldxh r5, [r0+8]
  2292. be16 r5
  2293. ldxh r4, [r0+10]
  2294. be16 r4
  2295. ldxh r3, [r0+12]
  2296. be16 r3
  2297. ldxh r2, [r0+14]
  2298. be16 r2
  2299. ldxh r1, [r0+16]
  2300. be16 r1
  2301. ldxh r0, [r0+18]
  2302. be16 r0
  2303. or r0, r1
  2304. or r0, r2
  2305. or r0, r3
  2306. or r0, r4
  2307. or r0, r5
  2308. or r0, r6
  2309. or r0, r7
  2310. or r0, r8
  2311. or r0, r9
  2312. exit
  2313. ",
  2314. )
  2315. .unwrap();
  2316. #[rustfmt::skip]
  2317. let mem = &mut [
  2318. 0x00, 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08,
  2319. 0x00, 0x10, 0x00, 0x20, 0x00, 0x40, 0x00, 0x80,
  2320. 0x01, 0x00, 0x02, 0x00
  2321. ];
  2322. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2323. #[cfg(not(feature = "std"))]
  2324. let mut exec_mem = alloc_exec_memory();
  2325. #[cfg(not(feature = "std"))]
  2326. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2327. vm.jit_compile().unwrap();
  2328. unsafe {
  2329. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x3ff);
  2330. }
  2331. }
  2332. #[test]
  2333. fn test_jit_ldxh() {
  2334. let prog = assemble(
  2335. "
  2336. ldxh r0, [r1+2]
  2337. exit
  2338. ",
  2339. )
  2340. .unwrap();
  2341. let mem = &mut [0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd];
  2342. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2343. #[cfg(not(feature = "std"))]
  2344. let mut exec_mem = alloc_exec_memory();
  2345. #[cfg(not(feature = "std"))]
  2346. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2347. vm.jit_compile().unwrap();
  2348. unsafe {
  2349. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x2211);
  2350. }
  2351. }
  2352. #[test]
  2353. fn test_jit_ldxh_same_reg() {
  2354. let prog = assemble(
  2355. "
  2356. mov r0, r1
  2357. sth [r0], 0x1234
  2358. ldxh r0, [r0]
  2359. exit
  2360. ",
  2361. )
  2362. .unwrap();
  2363. let mem = &mut [0xff, 0xff];
  2364. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2365. #[cfg(not(feature = "std"))]
  2366. let mut exec_mem = alloc_exec_memory();
  2367. #[cfg(not(feature = "std"))]
  2368. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2369. vm.jit_compile().unwrap();
  2370. unsafe {
  2371. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1234);
  2372. }
  2373. }
  2374. #[test]
  2375. fn test_jit_ldxw_all() {
  2376. let prog = assemble(
  2377. "
  2378. mov r0, r1
  2379. ldxw r9, [r0+0]
  2380. be32 r9
  2381. ldxw r8, [r0+4]
  2382. be32 r8
  2383. ldxw r7, [r0+8]
  2384. be32 r7
  2385. ldxw r6, [r0+12]
  2386. be32 r6
  2387. ldxw r5, [r0+16]
  2388. be32 r5
  2389. ldxw r4, [r0+20]
  2390. be32 r4
  2391. ldxw r3, [r0+24]
  2392. be32 r3
  2393. ldxw r2, [r0+28]
  2394. be32 r2
  2395. ldxw r1, [r0+32]
  2396. be32 r1
  2397. ldxw r0, [r0+36]
  2398. be32 r0
  2399. or r0, r1
  2400. or r0, r2
  2401. or r0, r3
  2402. or r0, r4
  2403. or r0, r5
  2404. or r0, r6
  2405. or r0, r7
  2406. or r0, r8
  2407. or r0, r9
  2408. exit
  2409. ",
  2410. )
  2411. .unwrap();
  2412. #[rustfmt::skip]
  2413. let mem = &mut [
  2414. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
  2415. 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x08,
  2416. 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00,
  2417. 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x08, 0x00,
  2418. 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00
  2419. ];
  2420. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2421. #[cfg(not(feature = "std"))]
  2422. let mut exec_mem = alloc_exec_memory();
  2423. #[cfg(not(feature = "std"))]
  2424. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2425. vm.jit_compile().unwrap();
  2426. unsafe {
  2427. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x030f0f);
  2428. }
  2429. }
  2430. #[test]
  2431. fn test_jit_ldxw() {
  2432. let prog = assemble(
  2433. "
  2434. ldxw r0, [r1+2]
  2435. exit
  2436. ",
  2437. )
  2438. .unwrap();
  2439. let mem = &mut [0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0xcc, 0xdd];
  2440. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2441. #[cfg(not(feature = "std"))]
  2442. let mut exec_mem = alloc_exec_memory();
  2443. #[cfg(not(feature = "std"))]
  2444. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2445. vm.jit_compile().unwrap();
  2446. unsafe {
  2447. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x44332211);
  2448. }
  2449. }
  2450. #[test]
  2451. fn test_jit_le16() {
  2452. let prog = assemble(
  2453. "
  2454. ldxh r0, [r1]
  2455. le16 r0
  2456. exit
  2457. ",
  2458. )
  2459. .unwrap();
  2460. let mem = &mut [0x22, 0x11];
  2461. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2462. #[cfg(not(feature = "std"))]
  2463. let mut exec_mem = alloc_exec_memory();
  2464. #[cfg(not(feature = "std"))]
  2465. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2466. vm.jit_compile().unwrap();
  2467. unsafe {
  2468. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1122);
  2469. }
  2470. }
  2471. #[test]
  2472. fn test_jit_le32() {
  2473. let prog = assemble(
  2474. "
  2475. ldxw r0, [r1]
  2476. le32 r0
  2477. exit
  2478. ",
  2479. )
  2480. .unwrap();
  2481. let mem = &mut [0x44, 0x33, 0x22, 0x11];
  2482. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2483. #[cfg(not(feature = "std"))]
  2484. let mut exec_mem = alloc_exec_memory();
  2485. #[cfg(not(feature = "std"))]
  2486. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2487. vm.jit_compile().unwrap();
  2488. unsafe {
  2489. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11223344);
  2490. }
  2491. }
  2492. #[test]
  2493. fn test_jit_le64() {
  2494. let prog = assemble(
  2495. "
  2496. ldxdw r0, [r1]
  2497. le64 r0
  2498. exit
  2499. ",
  2500. )
  2501. .unwrap();
  2502. let mem = &mut [0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11];
  2503. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2504. #[cfg(not(feature = "std"))]
  2505. let mut exec_mem = alloc_exec_memory();
  2506. #[cfg(not(feature = "std"))]
  2507. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2508. vm.jit_compile().unwrap();
  2509. unsafe {
  2510. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1122334455667788);
  2511. }
  2512. }
  2513. #[test]
  2514. fn test_jit_lsh_reg() {
  2515. let prog = assemble(
  2516. "
  2517. mov r0, 0x1
  2518. mov r7, 4
  2519. lsh r0, r7
  2520. exit
  2521. ",
  2522. )
  2523. .unwrap();
  2524. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2525. #[cfg(not(feature = "std"))]
  2526. let mut exec_mem = alloc_exec_memory();
  2527. #[cfg(not(feature = "std"))]
  2528. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2529. vm.jit_compile().unwrap();
  2530. unsafe {
  2531. assert_eq!(vm.execute_program_jit().unwrap(), 0x10);
  2532. }
  2533. }
  2534. #[test]
  2535. fn test_jit_mod() {
  2536. let prog = assemble(
  2537. "
  2538. mov32 r0, 5748
  2539. mod32 r0, 92
  2540. mov32 r1, 13
  2541. mod32 r0, r1
  2542. exit
  2543. ",
  2544. )
  2545. .unwrap();
  2546. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2547. #[cfg(not(feature = "std"))]
  2548. let mut exec_mem = alloc_exec_memory();
  2549. #[cfg(not(feature = "std"))]
  2550. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2551. vm.jit_compile().unwrap();
  2552. unsafe {
  2553. assert_eq!(vm.execute_program_jit().unwrap(), 0x5);
  2554. }
  2555. }
  2556. #[test]
  2557. fn test_jit_mod32() {
  2558. let prog = assemble(
  2559. "
  2560. lddw r0, 0x100000003
  2561. mod32 r0, 3
  2562. exit
  2563. ",
  2564. )
  2565. .unwrap();
  2566. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2567. #[cfg(not(feature = "std"))]
  2568. let mut exec_mem = alloc_exec_memory();
  2569. #[cfg(not(feature = "std"))]
  2570. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2571. vm.jit_compile().unwrap();
  2572. unsafe {
  2573. assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
  2574. }
  2575. }
  2576. #[test]
  2577. fn test_jit_mod64() {
  2578. let prog = assemble(
  2579. "
  2580. mov32 r0, -1316649930
  2581. lsh r0, 32
  2582. or r0, 0x100dc5c8
  2583. mov32 r1, 0xdde263e
  2584. lsh r1, 32
  2585. or r1, 0x3cbef7f3
  2586. mod r0, r1
  2587. mod r0, 0x658f1778
  2588. exit
  2589. ",
  2590. )
  2591. .unwrap();
  2592. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2593. #[cfg(not(feature = "std"))]
  2594. let mut exec_mem = alloc_exec_memory();
  2595. #[cfg(not(feature = "std"))]
  2596. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2597. vm.jit_compile().unwrap();
  2598. unsafe {
  2599. assert_eq!(vm.execute_program_jit().unwrap(), 0x30ba5a04);
  2600. }
  2601. }
  2602. #[test]
  2603. fn test_jit_mov() {
  2604. let prog = assemble(
  2605. "
  2606. mov32 r1, 1
  2607. mov32 r0, r1
  2608. exit
  2609. ",
  2610. )
  2611. .unwrap();
  2612. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2613. #[cfg(not(feature = "std"))]
  2614. let mut exec_mem = alloc_exec_memory();
  2615. #[cfg(not(feature = "std"))]
  2616. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2617. vm.jit_compile().unwrap();
  2618. unsafe {
  2619. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  2620. }
  2621. }
  2622. #[test]
  2623. fn test_jit_mul32_imm() {
  2624. let prog = assemble(
  2625. "
  2626. mov r0, 3
  2627. mul32 r0, 4
  2628. exit
  2629. ",
  2630. )
  2631. .unwrap();
  2632. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2633. #[cfg(not(feature = "std"))]
  2634. let mut exec_mem = alloc_exec_memory();
  2635. #[cfg(not(feature = "std"))]
  2636. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2637. vm.jit_compile().unwrap();
  2638. unsafe {
  2639. assert_eq!(vm.execute_program_jit().unwrap(), 0xc);
  2640. }
  2641. }
  2642. #[test]
  2643. fn test_jit_mul32_reg() {
  2644. let prog = assemble(
  2645. "
  2646. mov r0, 3
  2647. mov r1, 4
  2648. mul32 r0, r1
  2649. exit
  2650. ",
  2651. )
  2652. .unwrap();
  2653. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2654. #[cfg(not(feature = "std"))]
  2655. let mut exec_mem = alloc_exec_memory();
  2656. #[cfg(not(feature = "std"))]
  2657. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2658. vm.jit_compile().unwrap();
  2659. unsafe {
  2660. assert_eq!(vm.execute_program_jit().unwrap(), 0xc);
  2661. }
  2662. }
  2663. #[test]
  2664. fn test_jit_mul32_reg_overflow() {
  2665. let prog = assemble(
  2666. "
  2667. mov r0, 0x40000001
  2668. mov r1, 4
  2669. mul32 r0, r1
  2670. exit
  2671. ",
  2672. )
  2673. .unwrap();
  2674. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2675. #[cfg(not(feature = "std"))]
  2676. let mut exec_mem = alloc_exec_memory();
  2677. #[cfg(not(feature = "std"))]
  2678. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2679. vm.jit_compile().unwrap();
  2680. unsafe {
  2681. assert_eq!(vm.execute_program_jit().unwrap(), 0x4);
  2682. }
  2683. }
  2684. #[test]
  2685. fn test_jit_mul64_imm() {
  2686. let prog = assemble(
  2687. "
  2688. mov r0, 0x40000001
  2689. mul r0, 4
  2690. exit
  2691. ",
  2692. )
  2693. .unwrap();
  2694. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2695. #[cfg(not(feature = "std"))]
  2696. let mut exec_mem = alloc_exec_memory();
  2697. #[cfg(not(feature = "std"))]
  2698. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2699. vm.jit_compile().unwrap();
  2700. unsafe {
  2701. assert_eq!(vm.execute_program_jit().unwrap(), 0x100000004);
  2702. }
  2703. }
  2704. #[test]
  2705. fn test_jit_mul64_reg() {
  2706. let prog = assemble(
  2707. "
  2708. mov r0, 0x40000001
  2709. mov r1, 4
  2710. mul r0, r1
  2711. exit
  2712. ",
  2713. )
  2714. .unwrap();
  2715. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2716. #[cfg(not(feature = "std"))]
  2717. let mut exec_mem = alloc_exec_memory();
  2718. #[cfg(not(feature = "std"))]
  2719. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2720. vm.jit_compile().unwrap();
  2721. unsafe {
  2722. assert_eq!(vm.execute_program_jit().unwrap(), 0x100000004);
  2723. }
  2724. }
  2725. #[test]
  2726. fn test_jit_mul_loop() {
  2727. let prog = assemble(
  2728. "
  2729. mov r0, 0x7
  2730. add r1, 0xa
  2731. lsh r1, 0x20
  2732. rsh r1, 0x20
  2733. jeq r1, 0x0, +4
  2734. mov r0, 0x7
  2735. mul r0, 0x7
  2736. add r1, -1
  2737. jne r1, 0x0, -3
  2738. exit
  2739. ",
  2740. )
  2741. .unwrap();
  2742. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2743. #[cfg(not(feature = "std"))]
  2744. let mut exec_mem = alloc_exec_memory();
  2745. #[cfg(not(feature = "std"))]
  2746. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2747. vm.jit_compile().unwrap();
  2748. unsafe {
  2749. assert_eq!(vm.execute_program_jit().unwrap(), 0x75db9c97);
  2750. }
  2751. }
  2752. #[test]
  2753. fn test_jit_neg64() {
  2754. let prog = assemble(
  2755. "
  2756. mov32 r0, 2
  2757. neg r0
  2758. exit
  2759. ",
  2760. )
  2761. .unwrap();
  2762. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2763. #[cfg(not(feature = "std"))]
  2764. let mut exec_mem = alloc_exec_memory();
  2765. #[cfg(not(feature = "std"))]
  2766. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2767. vm.jit_compile().unwrap();
  2768. unsafe {
  2769. assert_eq!(vm.execute_program_jit().unwrap(), 0xfffffffffffffffe);
  2770. }
  2771. }
  2772. #[test]
  2773. fn test_jit_neg() {
  2774. let prog = assemble(
  2775. "
  2776. mov32 r0, 2
  2777. neg32 r0
  2778. exit
  2779. ",
  2780. )
  2781. .unwrap();
  2782. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2783. #[cfg(not(feature = "std"))]
  2784. let mut exec_mem = alloc_exec_memory();
  2785. #[cfg(not(feature = "std"))]
  2786. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2787. vm.jit_compile().unwrap();
  2788. unsafe {
  2789. assert_eq!(vm.execute_program_jit().unwrap(), 0xfffffffe);
  2790. }
  2791. }
  2792. #[test]
  2793. fn test_jit_prime() {
  2794. let prog = assemble(
  2795. "
  2796. mov r1, 67
  2797. mov r0, 0x1
  2798. mov r2, 0x2
  2799. jgt r1, 0x2, +4
  2800. ja +10
  2801. add r2, 0x1
  2802. mov r0, 0x1
  2803. jge r2, r1, +7
  2804. mov r3, r1
  2805. div r3, r2
  2806. mul r3, r2
  2807. mov r4, r1
  2808. sub r4, r3
  2809. mov r0, 0x0
  2810. jne r4, 0x0, -10
  2811. exit
  2812. ",
  2813. )
  2814. .unwrap();
  2815. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2816. #[cfg(not(feature = "std"))]
  2817. let mut exec_mem = alloc_exec_memory();
  2818. #[cfg(not(feature = "std"))]
  2819. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2820. vm.jit_compile().unwrap();
  2821. unsafe {
  2822. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  2823. }
  2824. }
  2825. #[test]
  2826. fn test_jit_rhs32() {
  2827. let prog = assemble(
  2828. "
  2829. xor r0, r0
  2830. sub r0, 1
  2831. rsh32 r0, 8
  2832. exit
  2833. ",
  2834. )
  2835. .unwrap();
  2836. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2837. #[cfg(not(feature = "std"))]
  2838. let mut exec_mem = alloc_exec_memory();
  2839. #[cfg(not(feature = "std"))]
  2840. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2841. vm.jit_compile().unwrap();
  2842. unsafe {
  2843. assert_eq!(vm.execute_program_jit().unwrap(), 0x00ffffff);
  2844. }
  2845. }
  2846. #[test]
  2847. fn test_jit_rsh_reg() {
  2848. let prog = assemble(
  2849. "
  2850. mov r0, 0x10
  2851. mov r7, 4
  2852. rsh r0, r7
  2853. exit
  2854. ",
  2855. )
  2856. .unwrap();
  2857. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2858. #[cfg(not(feature = "std"))]
  2859. let mut exec_mem = alloc_exec_memory();
  2860. #[cfg(not(feature = "std"))]
  2861. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2862. vm.jit_compile().unwrap();
  2863. unsafe {
  2864. assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
  2865. }
  2866. }
  2867. #[test]
  2868. fn test_jit_stack() {
  2869. let prog = assemble(
  2870. "
  2871. mov r1, 51
  2872. stdw [r10-16], 0xab
  2873. stdw [r10-8], 0xcd
  2874. and r1, 1
  2875. lsh r1, 3
  2876. mov r2, r10
  2877. add r2, r1
  2878. ldxdw r0, [r2-16]
  2879. exit
  2880. ",
  2881. )
  2882. .unwrap();
  2883. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2884. #[cfg(not(feature = "std"))]
  2885. let mut exec_mem = alloc_exec_memory();
  2886. #[cfg(not(feature = "std"))]
  2887. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2888. vm.jit_compile().unwrap();
  2889. unsafe {
  2890. assert_eq!(vm.execute_program_jit().unwrap(), 0xcd);
  2891. }
  2892. }
  2893. #[test]
  2894. fn test_jit_stack2() {
  2895. let prog = assemble(
  2896. "
  2897. stb [r10-4], 0x01
  2898. stb [r10-3], 0x02
  2899. stb [r10-2], 0x03
  2900. stb [r10-1], 0x04
  2901. mov r1, r10
  2902. mov r2, 0x4
  2903. sub r1, r2
  2904. call 1
  2905. mov r1, 0
  2906. ldxb r2, [r10-4]
  2907. ldxb r3, [r10-3]
  2908. ldxb r4, [r10-2]
  2909. ldxb r5, [r10-1]
  2910. call 0
  2911. xor r0, 0x2a2a2a2a
  2912. exit
  2913. ",
  2914. )
  2915. .unwrap();
  2916. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2917. vm.register_helper(0, helpers::gather_bytes).unwrap();
  2918. vm.register_helper(1, helpers::memfrob).unwrap();
  2919. #[cfg(not(feature = "std"))]
  2920. let mut exec_mem = alloc_exec_memory();
  2921. #[cfg(not(feature = "std"))]
  2922. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2923. vm.jit_compile().unwrap();
  2924. unsafe {
  2925. assert_eq!(vm.execute_program_jit().unwrap(), 0x01020304);
  2926. }
  2927. }
  2928. #[test]
  2929. fn test_jit_stb() {
  2930. let prog = assemble(
  2931. "
  2932. stb [r1+2], 0x11
  2933. ldxb r0, [r1+2]
  2934. exit
  2935. ",
  2936. )
  2937. .unwrap();
  2938. let mem = &mut [0xaa, 0xbb, 0xff, 0xcc, 0xdd];
  2939. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2940. #[cfg(not(feature = "std"))]
  2941. let mut exec_mem = alloc_exec_memory();
  2942. #[cfg(not(feature = "std"))]
  2943. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2944. vm.jit_compile().unwrap();
  2945. unsafe {
  2946. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11);
  2947. }
  2948. }
  2949. #[test]
  2950. fn test_jit_stdw() {
  2951. let prog = assemble(
  2952. "
  2953. stdw [r1+2], 0x44332211
  2954. ldxdw r0, [r1+2]
  2955. exit
  2956. ",
  2957. )
  2958. .unwrap();
  2959. #[rustfmt::skip]
  2960. let mem = &mut [
  2961. 0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  2962. 0xff, 0xff, 0xcc, 0xdd
  2963. ];
  2964. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2965. #[cfg(not(feature = "std"))]
  2966. let mut exec_mem = alloc_exec_memory();
  2967. #[cfg(not(feature = "std"))]
  2968. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2969. vm.jit_compile().unwrap();
  2970. unsafe {
  2971. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x44332211);
  2972. }
  2973. }
  2974. #[test]
  2975. fn test_jit_sth() {
  2976. let prog = assemble(
  2977. "
  2978. sth [r1+2], 0x2211
  2979. ldxh r0, [r1+2]
  2980. exit
  2981. ",
  2982. )
  2983. .unwrap();
  2984. let mem = &mut [0xaa, 0xbb, 0xff, 0xff, 0xcc, 0xdd];
  2985. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2986. #[cfg(not(feature = "std"))]
  2987. let mut exec_mem = alloc_exec_memory();
  2988. #[cfg(not(feature = "std"))]
  2989. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  2990. vm.jit_compile().unwrap();
  2991. unsafe {
  2992. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x2211);
  2993. }
  2994. }
  2995. #[test]
  2996. fn test_jit_string_stack() {
  2997. let prog = assemble(
  2998. "
  2999. mov r1, 0x78636261
  3000. stxw [r10-8], r1
  3001. mov r6, 0x0
  3002. stxb [r10-4], r6
  3003. stxb [r10-12], r6
  3004. mov r1, 0x79636261
  3005. stxw [r10-16], r1
  3006. mov r1, r10
  3007. add r1, -8
  3008. mov r2, r1
  3009. call 0x4
  3010. mov r1, r0
  3011. mov r0, 0x1
  3012. lsh r1, 0x20
  3013. rsh r1, 0x20
  3014. jne r1, 0x0, +11
  3015. mov r1, r10
  3016. add r1, -8
  3017. mov r2, r10
  3018. add r2, -16
  3019. call 0x4
  3020. mov r1, r0
  3021. lsh r1, 0x20
  3022. rsh r1, 0x20
  3023. mov r0, 0x1
  3024. jeq r1, r6, +1
  3025. mov r0, 0x0
  3026. exit
  3027. ",
  3028. )
  3029. .unwrap();
  3030. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  3031. vm.register_helper(4, helpers::strcmp).unwrap();
  3032. #[cfg(not(feature = "std"))]
  3033. let mut exec_mem = alloc_exec_memory();
  3034. #[cfg(not(feature = "std"))]
  3035. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  3036. vm.jit_compile().unwrap();
  3037. unsafe {
  3038. assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
  3039. }
  3040. }
  3041. #[test]
  3042. fn test_jit_stw() {
  3043. let prog = assemble(
  3044. "
  3045. stw [r1+2], 0x44332211
  3046. ldxw r0, [r1+2]
  3047. exit
  3048. ",
  3049. )
  3050. .unwrap();
  3051. let mem = &mut [0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd];
  3052. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  3053. #[cfg(not(feature = "std"))]
  3054. let mut exec_mem = alloc_exec_memory();
  3055. #[cfg(not(feature = "std"))]
  3056. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  3057. vm.jit_compile().unwrap();
  3058. unsafe {
  3059. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x44332211);
  3060. }
  3061. }
  3062. #[test]
  3063. fn test_jit_stxb() {
  3064. let prog = assemble(
  3065. "
  3066. mov32 r2, 0x11
  3067. stxb [r1+2], r2
  3068. ldxb r0, [r1+2]
  3069. exit
  3070. ",
  3071. )
  3072. .unwrap();
  3073. let mem = &mut [0xaa, 0xbb, 0xff, 0xcc, 0xdd];
  3074. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  3075. #[cfg(not(feature = "std"))]
  3076. let mut exec_mem = alloc_exec_memory();
  3077. #[cfg(not(feature = "std"))]
  3078. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  3079. vm.jit_compile().unwrap();
  3080. unsafe {
  3081. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11);
  3082. }
  3083. }
  3084. #[test]
  3085. fn test_jit_stxb_all() {
  3086. let prog = assemble(
  3087. "
  3088. mov r0, 0xf0
  3089. mov r2, 0xf2
  3090. mov r3, 0xf3
  3091. mov r4, 0xf4
  3092. mov r5, 0xf5
  3093. mov r6, 0xf6
  3094. mov r7, 0xf7
  3095. mov r8, 0xf8
  3096. stxb [r1], r0
  3097. stxb [r1+1], r2
  3098. stxb [r1+2], r3
  3099. stxb [r1+3], r4
  3100. stxb [r1+4], r5
  3101. stxb [r1+5], r6
  3102. stxb [r1+6], r7
  3103. stxb [r1+7], r8
  3104. ldxdw r0, [r1]
  3105. be64 r0
  3106. exit
  3107. ",
  3108. )
  3109. .unwrap();
  3110. let mem = &mut [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
  3111. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  3112. #[cfg(not(feature = "std"))]
  3113. let mut exec_mem = alloc_exec_memory();
  3114. #[cfg(not(feature = "std"))]
  3115. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  3116. vm.jit_compile().unwrap();
  3117. unsafe {
  3118. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0xf0f2f3f4f5f6f7f8);
  3119. }
  3120. }
  3121. #[test]
  3122. fn test_jit_stxb_all2() {
  3123. let prog = assemble(
  3124. "
  3125. mov r0, r1
  3126. mov r1, 0xf1
  3127. mov r9, 0xf9
  3128. stxb [r0], r1
  3129. stxb [r0+1], r9
  3130. ldxh r0, [r0]
  3131. be16 r0
  3132. exit
  3133. ",
  3134. )
  3135. .unwrap();
  3136. let mem = &mut [0xff, 0xff];
  3137. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  3138. #[cfg(not(feature = "std"))]
  3139. let mut exec_mem = alloc_exec_memory();
  3140. #[cfg(not(feature = "std"))]
  3141. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  3142. vm.jit_compile().unwrap();
  3143. unsafe {
  3144. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0xf1f9);
  3145. }
  3146. }
  3147. #[test]
  3148. fn test_jit_stxb_chain() {
  3149. let prog = assemble(
  3150. "
  3151. mov r0, r1
  3152. ldxb r9, [r0+0]
  3153. stxb [r0+1], r9
  3154. ldxb r8, [r0+1]
  3155. stxb [r0+2], r8
  3156. ldxb r7, [r0+2]
  3157. stxb [r0+3], r7
  3158. ldxb r6, [r0+3]
  3159. stxb [r0+4], r6
  3160. ldxb r5, [r0+4]
  3161. stxb [r0+5], r5
  3162. ldxb r4, [r0+5]
  3163. stxb [r0+6], r4
  3164. ldxb r3, [r0+6]
  3165. stxb [r0+7], r3
  3166. ldxb r2, [r0+7]
  3167. stxb [r0+8], r2
  3168. ldxb r1, [r0+8]
  3169. stxb [r0+9], r1
  3170. ldxb r0, [r0+9]
  3171. exit
  3172. ",
  3173. )
  3174. .unwrap();
  3175. #[rustfmt::skip]
  3176. let mem = &mut [
  3177. 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3178. 0x00, 0x00
  3179. ];
  3180. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  3181. #[cfg(not(feature = "std"))]
  3182. let mut exec_mem = alloc_exec_memory();
  3183. #[cfg(not(feature = "std"))]
  3184. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  3185. vm.jit_compile().unwrap();
  3186. unsafe {
  3187. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x2a);
  3188. }
  3189. }
  3190. #[test]
  3191. fn test_jit_stxdw() {
  3192. let prog = assemble(
  3193. "
  3194. mov r2, -2005440939
  3195. lsh r2, 32
  3196. or r2, 0x44332211
  3197. stxdw [r1+2], r2
  3198. ldxdw r0, [r1+2]
  3199. exit
  3200. ",
  3201. )
  3202. .unwrap();
  3203. #[rustfmt::skip]
  3204. let mem = &mut [
  3205. 0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  3206. 0xff, 0xff, 0xcc, 0xdd
  3207. ];
  3208. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  3209. #[cfg(not(feature = "std"))]
  3210. let mut exec_mem = alloc_exec_memory();
  3211. #[cfg(not(feature = "std"))]
  3212. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  3213. vm.jit_compile().unwrap();
  3214. unsafe {
  3215. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x8877665544332211);
  3216. }
  3217. }
  3218. #[test]
  3219. fn test_jit_stxh() {
  3220. let prog = assemble(
  3221. "
  3222. mov32 r2, 0x2211
  3223. stxh [r1+2], r2
  3224. ldxh r0, [r1+2]
  3225. exit
  3226. ",
  3227. )
  3228. .unwrap();
  3229. let mem = &mut [0xaa, 0xbb, 0xff, 0xff, 0xcc, 0xdd];
  3230. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  3231. #[cfg(not(feature = "std"))]
  3232. let mut exec_mem = alloc_exec_memory();
  3233. #[cfg(not(feature = "std"))]
  3234. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  3235. vm.jit_compile().unwrap();
  3236. unsafe {
  3237. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x2211);
  3238. }
  3239. }
  3240. #[test]
  3241. fn test_jit_stxw() {
  3242. let prog = assemble(
  3243. "
  3244. mov32 r2, 0x44332211
  3245. stxw [r1+2], r2
  3246. ldxw r0, [r1+2]
  3247. exit
  3248. ",
  3249. )
  3250. .unwrap();
  3251. let mem = &mut [0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd];
  3252. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  3253. #[cfg(not(feature = "std"))]
  3254. let mut exec_mem = alloc_exec_memory();
  3255. #[cfg(not(feature = "std"))]
  3256. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  3257. vm.jit_compile().unwrap();
  3258. unsafe {
  3259. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x44332211);
  3260. }
  3261. }
  3262. #[test]
  3263. fn test_jit_subnet() {
  3264. let prog = assemble(
  3265. "
  3266. mov r2, 0xe
  3267. ldxh r3, [r1+12]
  3268. jne r3, 0x81, +2
  3269. mov r2, 0x12
  3270. ldxh r3, [r1+16]
  3271. and r3, 0xffff
  3272. jne r3, 0x8, +5
  3273. add r1, r2
  3274. mov r0, 0x1
  3275. ldxw r1, [r1+16]
  3276. and r1, 0xffffff
  3277. jeq r1, 0x1a8c0, +1
  3278. mov r0, 0x0
  3279. exit
  3280. ",
  3281. )
  3282. .unwrap();
  3283. #[rustfmt::skip]
  3284. let mem = &mut [
  3285. 0x00, 0x00, 0xc0, 0x9f, 0xa0, 0x97, 0x00, 0xa0,
  3286. 0xcc, 0x3b, 0xbf, 0xfa, 0x08, 0x00, 0x45, 0x10,
  3287. 0x00, 0x3c, 0x46, 0x3c, 0x40, 0x00, 0x40, 0x06,
  3288. 0x73, 0x1c, 0xc0, 0xa8, 0x01, 0x02, 0xc0, 0xa8,
  3289. 0x01, 0x01, 0x06, 0x0e, 0x00, 0x17, 0x99, 0xc5,
  3290. 0xa0, 0xec, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x02,
  3291. 0x7d, 0x78, 0xe0, 0xa3, 0x00, 0x00, 0x02, 0x04,
  3292. 0x05, 0xb4, 0x04, 0x02, 0x08, 0x0a, 0x00, 0x9c,
  3293. 0x27, 0x24, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03,
  3294. 0x03, 0x00
  3295. ];
  3296. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  3297. #[cfg(not(feature = "std"))]
  3298. let mut exec_mem = alloc_exec_memory();
  3299. #[cfg(not(feature = "std"))]
  3300. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  3301. vm.jit_compile().unwrap();
  3302. unsafe {
  3303. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1);
  3304. }
  3305. }
  3306. #[rustfmt::skip]
  3307. const PROG_TCP_PORT_80: [u8;152] = [
  3308. 0x71, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
  3309. 0x71, 0x13, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00,
  3310. 0x67, 0x03, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  3311. 0x4f, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3312. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3313. 0x55, 0x03, 0x0c, 0x00, 0x08, 0x00, 0x00, 0x00,
  3314. 0x71, 0x12, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00,
  3315. 0x55, 0x02, 0x0a, 0x00, 0x06, 0x00, 0x00, 0x00,
  3316. 0x71, 0x12, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00,
  3317. 0x07, 0x01, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00,
  3318. 0x57, 0x02, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00,
  3319. 0x67, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  3320. 0x0f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3321. 0x69, 0x12, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  3322. 0x15, 0x02, 0x02, 0x00, 0x00, 0x50, 0x00, 0x00,
  3323. 0x69, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3324. 0x55, 0x01, 0x01, 0x00, 0x00, 0x50, 0x00, 0x00,
  3325. 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  3326. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  3327. ];
  3328. #[test]
  3329. fn test_jit_tcp_port80_match() {
  3330. #[rustfmt::skip]
  3331. let mem = &mut [
  3332. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06,
  3333. 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45, 0x00,
  3334. 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06,
  3335. 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01, 0xc0, 0xa8,
  3336. 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00,
  3337. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02,
  3338. 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44,
  3339. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3340. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3341. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3342. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3343. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3344. 0x44, 0x44, 0x44, 0x44
  3345. ];
  3346. let prog = &PROG_TCP_PORT_80;
  3347. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  3348. #[cfg(not(feature = "std"))]
  3349. let mut exec_mem = alloc_exec_memory();
  3350. #[cfg(not(feature = "std"))]
  3351. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  3352. vm.jit_compile().unwrap();
  3353. unsafe {
  3354. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1);
  3355. }
  3356. }
  3357. #[test]
  3358. fn test_jit_tcp_port80_nomatch() {
  3359. #[rustfmt::skip]
  3360. let mem = &mut [
  3361. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06,
  3362. 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45, 0x00,
  3363. 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06,
  3364. 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01, 0xc0, 0xa8,
  3365. 0x00, 0x02, 0x00, 0x16, 0x27, 0x10, 0x00, 0x00,
  3366. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x51, 0x02,
  3367. 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44,
  3368. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3369. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3370. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3371. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3372. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3373. 0x44, 0x44, 0x44, 0x44
  3374. ];
  3375. let prog = &PROG_TCP_PORT_80;
  3376. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  3377. #[cfg(not(feature = "std"))]
  3378. let mut exec_mem = alloc_exec_memory();
  3379. #[cfg(not(feature = "std"))]
  3380. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  3381. vm.jit_compile().unwrap();
  3382. unsafe {
  3383. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x0);
  3384. }
  3385. }
  3386. #[test]
  3387. fn test_jit_tcp_port80_nomatch_ethertype() {
  3388. #[rustfmt::skip]
  3389. let mem = &mut [
  3390. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06,
  3391. 0x07, 0x08, 0x09, 0x0a, 0x08, 0x01, 0x45, 0x00,
  3392. 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06,
  3393. 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01, 0xc0, 0xa8,
  3394. 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00,
  3395. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02,
  3396. 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44,
  3397. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3398. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3399. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3400. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3401. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3402. 0x44, 0x44, 0x44, 0x44
  3403. ];
  3404. let prog = &PROG_TCP_PORT_80;
  3405. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  3406. #[cfg(not(feature = "std"))]
  3407. let mut exec_mem = alloc_exec_memory();
  3408. #[cfg(not(feature = "std"))]
  3409. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  3410. vm.jit_compile().unwrap();
  3411. unsafe {
  3412. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x0);
  3413. }
  3414. }
  3415. #[test]
  3416. fn test_jit_tcp_port80_nomatch_proto() {
  3417. #[rustfmt::skip]
  3418. let mem = &mut [
  3419. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06,
  3420. 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45, 0x00,
  3421. 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11,
  3422. 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01, 0xc0, 0xa8,
  3423. 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00,
  3424. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02,
  3425. 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44,
  3426. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3427. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3428. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3429. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3430. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  3431. 0x44, 0x44, 0x44, 0x44
  3432. ];
  3433. let prog = &PROG_TCP_PORT_80;
  3434. let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  3435. #[cfg(not(feature = "std"))]
  3436. let mut exec_mem = alloc_exec_memory();
  3437. #[cfg(not(feature = "std"))]
  3438. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  3439. vm.jit_compile().unwrap();
  3440. unsafe {
  3441. assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x0);
  3442. }
  3443. }
  3444. #[test]
  3445. fn test_jit_tcp_sack_match() {
  3446. let mut mem = TCP_SACK_MATCH.to_vec();
  3447. let prog = assemble(TCP_SACK_ASM).unwrap();
  3448. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  3449. #[cfg(not(feature = "std"))]
  3450. let mut exec_mem = alloc_exec_memory();
  3451. #[cfg(not(feature = "std"))]
  3452. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  3453. vm.jit_compile().unwrap();
  3454. unsafe {
  3455. assert_eq!(vm.execute_program_jit(mem.as_mut_slice()).unwrap(), 0x1);
  3456. }
  3457. }
  3458. #[test]
  3459. fn test_jit_tcp_sack_nomatch() {
  3460. let mut mem = TCP_SACK_NOMATCH.to_vec();
  3461. let prog = assemble(TCP_SACK_ASM).unwrap();
  3462. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  3463. #[cfg(not(feature = "std"))]
  3464. let mut exec_mem = alloc_exec_memory();
  3465. #[cfg(not(feature = "std"))]
  3466. vm.set_jit_exec_memory(&mut exec_mem).unwrap();
  3467. vm.jit_compile().unwrap();
  3468. unsafe {
  3469. assert_eq!(vm.execute_program_jit(mem.as_mut_slice()).unwrap(), 0x0);
  3470. }
  3471. }