ubpf_jit_x86_64.rs 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  1. // Converted from the tests for uBPF <https://github.com/iovisor/ubpf>
  2. // Copyright 2015 Big Switch Networks, Inc
  3. // Copyright 2016 6WIND S.A. <quentin.monnet@6wind.com>
  4. //
  5. // Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or
  6. // the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be
  7. // copied, modified, or distributed except according to those terms.
  8. // The tests contained in this file are extracted from the unit tests of uBPF software. Each test
  9. // in this file has a name in the form `test_jit_<name>`, and corresponds to the (human-readable)
  10. // code in `ubpf/tree/master/tests/<name>`, available at
  11. // <https://github.com/iovisor/ubpf/tree/master/tests> (hyphen had to be replaced with underscores
  12. // as Rust will not accept them in function names). It is strongly advised to refer to the uBPF
  13. // version to understand what these program do.
  14. //
  15. // Each program was assembled from the uBPF version with the assembler provided by uBPF itself, and
  16. // available at <https://github.com/iovisor/ubpf/tree/master/ubpf>.
  17. // The very few modifications that have been realized should be indicated.
  18. // These are unit tests for the eBPF JIT compiler.
  19. extern crate rbpf;
  20. mod common;
  21. use rbpf::helpers;
  22. use rbpf::assembler::assemble;
  23. use common::{TCP_SACK_ASM, TCP_SACK_MATCH, TCP_SACK_NOMATCH};
  24. #[test]
  25. fn test_jit_add() {
  26. let prog = &[
  27. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  28. 0xb4, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  29. 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  30. 0x0c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  32. ];
  33. let mut vm = rbpf::EbpfVmNoData::new(prog);
  34. vm.jit_compile();
  35. unsafe { assert_eq!(vm.prog_exec_jit(), 0x3); }
  36. }
  37. #[test]
  38. fn test_jit_alu64_arith() {
  39. let prog = &[
  40. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  41. 0xb7, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  42. 0xb7, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  43. 0xb7, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  44. 0xb7, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  45. 0xb7, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  46. 0xb7, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
  47. 0xb7, 0x07, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  48. 0xb7, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  49. 0xb7, 0x09, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
  50. 0x07, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  51. 0x0f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52. 0x17, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00,
  53. 0x1f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54. 0x27, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  55. 0x2f, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  56. 0x37, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  57. 0x3f, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  59. ];
  60. let mut vm = rbpf::EbpfVmNoData::new(prog);
  61. vm.jit_compile();
  62. unsafe { assert_eq!(vm.prog_exec_jit(), 0x2a); }
  63. }
  64. #[test]
  65. fn test_jit_alu64_bit() {
  66. let prog = &[
  67. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68. 0xb7, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  69. 0xb7, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  70. 0xb7, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  71. 0xb7, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  72. 0xb7, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  73. 0xb7, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
  74. 0xb7, 0x07, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  75. 0xb7, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  76. 0x4f, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  77. 0x47, 0x00, 0x00, 0x00, 0xa0, 0x00, 0x00, 0x00,
  78. 0x57, 0x00, 0x00, 0x00, 0xa3, 0x00, 0x00, 0x00,
  79. 0xb7, 0x09, 0x00, 0x00, 0x91, 0x00, 0x00, 0x00,
  80. 0x5f, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  81. 0x67, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  82. 0x67, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00,
  83. 0x6f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  84. 0x77, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  85. 0x77, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
  86. 0x7f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  87. 0xa7, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  88. 0xaf, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  89. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  90. ];
  91. let mut vm = rbpf::EbpfVmNoData::new(prog);
  92. vm.jit_compile();
  93. unsafe { assert_eq!(vm.prog_exec_jit(), 0x11); }
  94. }
  95. #[test]
  96. fn test_jit_alu_arith() {
  97. let prog = &[
  98. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  99. 0xb4, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  100. 0xb4, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  101. 0xb4, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  102. 0xb4, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  103. 0xb4, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  104. 0xb4, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
  105. 0xb4, 0x07, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  106. 0xb4, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  107. 0xb4, 0x09, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
  108. 0x04, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  109. 0x0c, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  110. 0x14, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00,
  111. 0x1c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  112. 0x24, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  113. 0x2c, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  114. 0x34, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  115. 0x3c, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  116. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  117. ];
  118. let mut vm = rbpf::EbpfVmNoData::new(prog);
  119. vm.jit_compile();
  120. unsafe { assert_eq!(vm.prog_exec_jit(), 0x2a); }
  121. }
  122. #[test]
  123. fn test_jit_alu_bit() {
  124. let prog = &[
  125. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  126. 0xb4, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  127. 0xb4, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  128. 0xb4, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  129. 0xb4, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  130. 0xb4, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  131. 0xb4, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
  132. 0xb4, 0x07, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  133. 0xb4, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  134. 0x4c, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  135. 0x44, 0x00, 0x00, 0x00, 0xa0, 0x00, 0x00, 0x00,
  136. 0x54, 0x00, 0x00, 0x00, 0xa3, 0x00, 0x00, 0x00,
  137. 0xb4, 0x09, 0x00, 0x00, 0x91, 0x00, 0x00, 0x00,
  138. 0x5c, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  139. 0x64, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00,
  140. 0x6c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  141. 0x74, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
  142. 0x7c, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  143. 0xa4, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  144. 0xac, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  145. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  146. ];
  147. let mut vm = rbpf::EbpfVmNoData::new(prog);
  148. vm.jit_compile();
  149. unsafe { assert_eq!(vm.prog_exec_jit(), 0x11); }
  150. }
  151. #[test]
  152. fn test_jit_arsh32_high_shift() {
  153. let prog = &[
  154. 0xb7, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  155. 0x18, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  156. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  157. 0xcc, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  158. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  159. ];
  160. let mut vm = rbpf::EbpfVmNoData::new(prog);
  161. vm.jit_compile();
  162. unsafe { assert_eq!(vm.prog_exec_jit(), 0x4); }
  163. }
  164. #[test]
  165. fn test_jit_arsh() {
  166. let prog = &[
  167. 0xb4, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00,
  168. 0x64, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
  169. 0xc4, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  170. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  171. ];
  172. let mut vm = rbpf::EbpfVmNoData::new(prog);
  173. vm.jit_compile();
  174. unsafe { assert_eq!(vm.prog_exec_jit(), 0xffff8000); }
  175. }
  176. #[test]
  177. fn test_jit_arsh64() {
  178. let prog = &[
  179. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  180. 0x67, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00,
  181. 0xc7, 0x00, 0x00, 0x00, 0x37, 0x00, 0x00, 0x00,
  182. 0xb4, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  183. 0xcf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  184. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  185. ];
  186. let mut vm = rbpf::EbpfVmNoData::new(prog);
  187. vm.jit_compile();
  188. unsafe { assert_eq!(vm.prog_exec_jit(), 0xfffffffffffffff8); }
  189. }
  190. #[test]
  191. fn test_jit_arsh_reg() {
  192. let prog = &[
  193. 0xb4, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00,
  194. 0xb4, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  195. 0x64, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
  196. 0xcc, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  197. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  198. ];
  199. let mut vm = rbpf::EbpfVmNoData::new(prog);
  200. vm.jit_compile();
  201. unsafe { assert_eq!(vm.prog_exec_jit(), 0xffff8000); }
  202. }
  203. #[test]
  204. fn test_jit_be16() {
  205. let prog = &[
  206. 0x69, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  207. 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  208. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  209. ];
  210. let mem = &mut [
  211. 0x11, 0x22
  212. ];
  213. let mut vm = rbpf::EbpfVmRaw::new(prog);
  214. vm.jit_compile();
  215. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x1122); }
  216. }
  217. #[test]
  218. fn test_jit_be16_high() {
  219. let prog = &[
  220. 0x79, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  221. 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  222. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  223. ];
  224. let mem = &mut [
  225. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88
  226. ];
  227. let mut vm = rbpf::EbpfVmRaw::new(prog);
  228. vm.jit_compile();
  229. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x1122); }
  230. }
  231. #[test]
  232. fn test_jit_be32() {
  233. let prog = &[
  234. 0x61, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  235. 0xdc, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  236. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  237. ];
  238. let mem = &mut [
  239. 0x11, 0x22, 0x33, 0x44
  240. ];
  241. let mut vm = rbpf::EbpfVmRaw::new(prog);
  242. vm.jit_compile();
  243. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x11223344); }
  244. }
  245. #[test]
  246. fn test_jit_be32_high() {
  247. let prog = &[
  248. 0x79, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  249. 0xdc, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  250. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  251. ];
  252. let mem = &mut [
  253. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88
  254. ];
  255. let mut vm = rbpf::EbpfVmRaw::new(prog);
  256. vm.jit_compile();
  257. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x11223344); }
  258. }
  259. #[test]
  260. fn test_jit_be64() {
  261. let prog = &[
  262. 0x79, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  263. 0xdc, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  264. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  265. ];
  266. let mem = &mut [
  267. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88
  268. ];
  269. let mut vm = rbpf::EbpfVmRaw::new(prog);
  270. vm.jit_compile();
  271. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x1122334455667788); }
  272. }
  273. #[test]
  274. fn test_jit_call() {
  275. let prog = &[
  276. 0xb7, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  277. 0xb7, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  278. 0xb7, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  279. 0xb7, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  280. 0xb7, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  281. 0x85, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  282. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  283. ];
  284. let mut vm = rbpf::EbpfVmNoData::new(prog);
  285. vm.register_helper(0, helpers::gather_bytes);
  286. vm.jit_compile();
  287. unsafe { assert_eq!(vm.prog_exec_jit(), 0x0102030405); }
  288. }
  289. #[test]
  290. fn test_jit_call_memfrob() {
  291. let prog = &[
  292. 0xbf, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  293. 0x07, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  294. 0xb7, 0x02, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  295. 0x85, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  296. 0x79, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  297. 0xdc, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  298. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  299. ];
  300. let mem = &mut [
  301. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  302. ];
  303. let mut vm = rbpf::EbpfVmRaw::new(prog);
  304. vm.register_helper(1, helpers::memfrob);
  305. vm.jit_compile();
  306. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x102292e2f2c0708); }
  307. }
  308. // TODO: helpers::trash_registers needs asm!().
  309. // Try this again once asm!() is available in stable.
  310. //#[test]
  311. //fn test_jit_call_save() {
  312. //let prog = &[
  313. //0xb7, 0x06, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  314. //0xb7, 0x07, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  315. //0xb7, 0x08, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
  316. //0xb7, 0x09, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00,
  317. //0x85, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  318. //0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  319. //0x4f, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  320. //0x4f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  321. //0x4f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  322. //0x4f, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  323. //0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  324. //];
  325. //let mut vm = rbpf::EbpfVmNoData::new(prog);
  326. //vm.register_helper(2, helpers::trash_registers);
  327. //vm.jit_compile();
  328. //unsafe { assert_eq!(vm.prog_exec_jit(), 0x4321); }
  329. //}
  330. #[test]
  331. fn test_jit_div32_high_divisor() {
  332. let prog = &[
  333. 0xb7, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
  334. 0x18, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  335. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  336. 0x3c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  337. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  338. ];
  339. let mut vm = rbpf::EbpfVmNoData::new(prog);
  340. vm.jit_compile();
  341. unsafe { assert_eq!(vm.prog_exec_jit(), 0x3); }
  342. }
  343. #[test]
  344. fn test_jit_div32_imm() {
  345. let prog = &[
  346. 0x18, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
  347. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  348. 0x34, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  349. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  350. ];
  351. let mut vm = rbpf::EbpfVmNoData::new(prog);
  352. vm.jit_compile();
  353. unsafe { assert_eq!(vm.prog_exec_jit(), 0x3); }
  354. }
  355. #[test]
  356. fn test_jit_div32_reg() {
  357. let prog = &[
  358. 0x18, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
  359. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  360. 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  361. 0x3c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  362. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  363. ];
  364. let mut vm = rbpf::EbpfVmNoData::new(prog);
  365. vm.jit_compile();
  366. unsafe { assert_eq!(vm.prog_exec_jit(), 0x3); }
  367. }
  368. #[test]
  369. fn test_jit_div64_imm() {
  370. let prog = &[
  371. 0xb7, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
  372. 0x67, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  373. 0x37, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  374. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  375. ];
  376. let mut vm = rbpf::EbpfVmNoData::new(prog);
  377. vm.jit_compile();
  378. unsafe { assert_eq!(vm.prog_exec_jit(), 0x300000000); }
  379. }
  380. #[test]
  381. fn test_jit_div64_reg() {
  382. let prog = &[
  383. 0xb7, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
  384. 0x67, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  385. 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  386. 0x3f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  387. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  388. ];
  389. let mut vm = rbpf::EbpfVmNoData::new(prog);
  390. vm.jit_compile();
  391. unsafe { assert_eq!(vm.prog_exec_jit(), 0x300000000); }
  392. }
  393. #[test]
  394. fn test_jit_early_exit() {
  395. let prog = &[
  396. 0xb7, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  397. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  398. 0xb7, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  399. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  400. ];
  401. let mut vm = rbpf::EbpfVmNoData::new(prog);
  402. vm.jit_compile();
  403. unsafe { assert_eq!(vm.prog_exec_jit(), 0x3); }
  404. }
  405. // uBPF limits the number of user functions at 64. We don't.
  406. //#[test]
  407. //fn test_jit_err_call_bad_imm() {
  408. //}
  409. #[test]
  410. #[should_panic(expected = "[JIT] Error: unknown helper function (id: 0x3f)")]
  411. fn test_jit_err_call_unreg() {
  412. let prog = &[
  413. 0xb7, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  414. 0xb7, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  415. 0xb7, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  416. 0xb7, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  417. 0xb7, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  418. 0x85, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00,
  419. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  420. ];
  421. let mut vm = rbpf::EbpfVmNoData::new(prog);
  422. vm.jit_compile();
  423. unsafe { vm.prog_exec_jit(); }
  424. }
  425. // TODO: Should panic!() instead, but I could not make it panic in JIT-compiled code, so the
  426. // program returns -1 instead. We can make it write on stderr, though.
  427. #[test]
  428. //#[should_panic(expected = "[JIT] Error: division by 0")]
  429. fn test_jit_err_div64_by_zero_reg() {
  430. let prog = &[
  431. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  432. 0xb4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  433. 0x3f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  434. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  435. ];
  436. let mut vm = rbpf::EbpfVmNoData::new(prog);
  437. vm.jit_compile();
  438. unsafe { assert_eq!(vm.prog_exec_jit(), 0xffffffffffffffff); }
  439. }
  440. // TODO: Same remark as above
  441. #[test]
  442. //#[should_panic(expected = "[JIT] Error: division by 0")]
  443. fn test_jit_err_div_by_zero_reg() {
  444. let prog = &[
  445. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  446. 0xb4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  447. 0x3c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  448. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  449. ];
  450. let mut vm = rbpf::EbpfVmNoData::new(prog);
  451. vm.jit_compile();
  452. unsafe { assert_eq!(vm.prog_exec_jit(), 0xffffffffffffffff); }
  453. }
  454. // TODO: Same remark as above
  455. #[test]
  456. //#[should_panic(expected = "[JIT] Error: division by 0")]
  457. fn test_jit_err_mod64_by_zero_reg() {
  458. let prog = &[
  459. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  460. 0xb4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  461. 0x9f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  462. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  463. ];
  464. let mut vm = rbpf::EbpfVmNoData::new(prog);
  465. vm.jit_compile();
  466. unsafe { assert_eq!(vm.prog_exec_jit(), 0xffffffffffffffff); }
  467. }
  468. // TODO: Same remark as above
  469. #[test]
  470. //#[should_panic(expected = "[JIT] Error: division by 0")]
  471. fn test_jit_err_mod_by_zero_reg() {
  472. let prog = &[
  473. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  474. 0xb4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  475. 0x9c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  476. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  477. ];
  478. let mut vm = rbpf::EbpfVmNoData::new(prog);
  479. vm.jit_compile();
  480. unsafe { assert_eq!(vm.prog_exec_jit(), 0xffffffffffffffff); }
  481. }
  482. // TODO SKIP: JIT disabled for this testcase (stack oob check not implemented)
  483. // #[test]
  484. // #[should_panic(expected = "Error: out of bounds memory store (insn #1)")]
  485. // fn test_jit_err_stack_out_of_bound() {
  486. // let prog = &[
  487. // 0x72, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  488. // 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  489. // ];
  490. // let mut vm = rbpf::EbpfVmNoData::new(prog);
  491. // vm.jit_compile();
  492. // unsafe { vm.prog_exec_jit(); }
  493. // }
  494. #[test]
  495. fn test_jit_exit() {
  496. let prog = &[
  497. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  498. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  499. ];
  500. let mut vm = rbpf::EbpfVmNoData::new(prog);
  501. vm.jit_compile();
  502. unsafe { assert_eq!(vm.prog_exec_jit(), 0x0); }
  503. }
  504. #[test]
  505. fn test_jit_ja() {
  506. let prog = &[
  507. 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  508. 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  509. 0xb7, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  510. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  511. ];
  512. let mut vm = rbpf::EbpfVmNoData::new(prog);
  513. vm.jit_compile();
  514. unsafe { assert_eq!(vm.prog_exec_jit(), 0x1); }
  515. }
  516. #[test]
  517. fn test_jit_jeq_imm() {
  518. let prog = &[
  519. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  520. 0xb4, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
  521. 0x15, 0x01, 0x04, 0x00, 0x0b, 0x00, 0x00, 0x00,
  522. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  523. 0xb4, 0x01, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
  524. 0x15, 0x01, 0x01, 0x00, 0x0b, 0x00, 0x00, 0x00,
  525. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  526. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  527. ];
  528. let mut vm = rbpf::EbpfVmNoData::new(prog);
  529. vm.jit_compile();
  530. unsafe { assert_eq!(vm.prog_exec_jit(), 0x1); }
  531. }
  532. #[test]
  533. fn test_jit_jeq_reg() {
  534. let prog = &[
  535. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  536. 0xb4, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
  537. 0xb4, 0x02, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
  538. 0x1d, 0x21, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  539. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  540. 0xb4, 0x01, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
  541. 0x1d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  542. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  543. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  544. ];
  545. let mut vm = rbpf::EbpfVmNoData::new(prog);
  546. vm.jit_compile();
  547. unsafe { assert_eq!(vm.prog_exec_jit(), 0x1); }
  548. }
  549. #[test]
  550. fn test_jit_jge_imm() {
  551. let prog = &[
  552. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  553. 0xb4, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
  554. 0x35, 0x01, 0x04, 0x00, 0x0b, 0x00, 0x00, 0x00,
  555. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  556. 0xb4, 0x01, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
  557. 0x35, 0x01, 0x01, 0x00, 0x0b, 0x00, 0x00, 0x00,
  558. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  559. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  560. ];
  561. let mut vm = rbpf::EbpfVmNoData::new(prog);
  562. vm.jit_compile();
  563. unsafe { assert_eq!(vm.prog_exec_jit(), 0x1); }
  564. }
  565. #[test]
  566. fn test_jit_jgt_imm() {
  567. let prog = &[
  568. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  569. 0xb4, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  570. 0x25, 0x01, 0x02, 0x00, 0x06, 0x00, 0x00, 0x00,
  571. 0x25, 0x01, 0x01, 0x00, 0x05, 0x00, 0x00, 0x00,
  572. 0x25, 0x01, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00,
  573. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  574. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  575. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  576. ];
  577. let mut vm = rbpf::EbpfVmNoData::new(prog);
  578. vm.jit_compile();
  579. unsafe { assert_eq!(vm.prog_exec_jit(), 0x1); }
  580. }
  581. #[test]
  582. fn test_jit_jgt_reg() {
  583. let prog = &[
  584. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  585. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  586. 0xb7, 0x02, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
  587. 0xb7, 0x03, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  588. 0x2d, 0x21, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  589. 0x2d, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  590. 0x2d, 0x31, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  591. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  592. 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  593. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  594. ];
  595. let mut vm = rbpf::EbpfVmNoData::new(prog);
  596. vm.jit_compile();
  597. unsafe { assert_eq!(vm.prog_exec_jit(), 0x1); }
  598. }
  599. #[test]
  600. fn test_jit_jit_bounce() {
  601. let prog = &[
  602. 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  603. 0xbf, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  604. 0xbf, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  605. 0xbf, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  606. 0xbf, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  607. 0xbf, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  608. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  609. ];
  610. let mut vm = rbpf::EbpfVmNoData::new(prog);
  611. vm.jit_compile();
  612. unsafe { assert_eq!(vm.prog_exec_jit(), 0x1); }
  613. }
  614. #[test]
  615. fn test_jit_jne_reg() {
  616. let prog = &[
  617. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  618. 0xb4, 0x01, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
  619. 0xb4, 0x02, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
  620. 0x5d, 0x21, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  621. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  622. 0xb4, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
  623. 0x5d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  624. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  625. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  626. ];
  627. let mut vm = rbpf::EbpfVmNoData::new(prog);
  628. vm.jit_compile();
  629. unsafe { assert_eq!(vm.prog_exec_jit(), 0x1); }
  630. }
  631. #[test]
  632. fn test_jit_jset_imm() {
  633. let prog = &[
  634. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  635. 0xb4, 0x01, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  636. 0x45, 0x01, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00,
  637. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  638. 0xb4, 0x01, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
  639. 0x45, 0x01, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00,
  640. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  641. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  642. ];
  643. let mut vm = rbpf::EbpfVmNoData::new(prog);
  644. vm.jit_compile();
  645. unsafe { assert_eq!(vm.prog_exec_jit(), 0x1); }
  646. }
  647. #[test]
  648. fn test_jit_jset_reg() {
  649. let prog = &[
  650. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  651. 0xb4, 0x01, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  652. 0xb4, 0x02, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  653. 0x4d, 0x21, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  654. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  655. 0xb4, 0x01, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
  656. 0x4d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  657. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  658. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  659. ];
  660. let mut vm = rbpf::EbpfVmNoData::new(prog);
  661. vm.jit_compile();
  662. unsafe { assert_eq!(vm.prog_exec_jit(), 0x1); }
  663. }
  664. #[test]
  665. fn test_jit_jsge_imm() {
  666. let prog = &[
  667. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  668. 0xb7, 0x01, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
  669. 0x75, 0x01, 0x05, 0x00, 0xff, 0xff, 0xff, 0xff,
  670. 0x75, 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  671. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  672. 0xb7, 0x01, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  673. 0x75, 0x01, 0x01, 0x00, 0xff, 0xff, 0xff, 0xff,
  674. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  675. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  676. ];
  677. let mut vm = rbpf::EbpfVmNoData::new(prog);
  678. vm.jit_compile();
  679. unsafe { assert_eq!(vm.prog_exec_jit(), 0x1); }
  680. }
  681. #[test]
  682. fn test_jit_jsge_reg() {
  683. let prog = &[
  684. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  685. 0xb7, 0x01, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
  686. 0xb7, 0x02, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  687. 0xb4, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  688. 0x7d, 0x21, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
  689. 0x7d, 0x31, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  690. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  691. 0xbf, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  692. 0x7d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  693. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  694. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  695. ];
  696. let mut vm = rbpf::EbpfVmNoData::new(prog);
  697. vm.jit_compile();
  698. unsafe { assert_eq!(vm.prog_exec_jit(), 0x1); }
  699. }
  700. #[test]
  701. fn test_jit_jsgt_imm() {
  702. let prog = &[
  703. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  704. 0xb7, 0x01, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
  705. 0x65, 0x01, 0x04, 0x00, 0xff, 0xff, 0xff, 0xff,
  706. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  707. 0xb4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  708. 0x65, 0x01, 0x01, 0x00, 0xff, 0xff, 0xff, 0xff,
  709. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  710. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  711. ];
  712. let mut vm = rbpf::EbpfVmNoData::new(prog);
  713. vm.jit_compile();
  714. unsafe { assert_eq!(vm.prog_exec_jit(), 0x1); }
  715. }
  716. #[test]
  717. fn test_jit_jsgt_reg() {
  718. let prog = &[
  719. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  720. 0xb7, 0x01, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
  721. 0xb7, 0x02, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  722. 0x6d, 0x21, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  723. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  724. 0xb4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  725. 0x6d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  726. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  727. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  728. ];
  729. let mut vm = rbpf::EbpfVmNoData::new(prog);
  730. vm.jit_compile();
  731. unsafe { assert_eq!(vm.prog_exec_jit(), 0x1); }
  732. }
  733. #[test]
  734. fn test_jit_lddw() {
  735. let prog = &[
  736. 0x18, 0x00, 0x00, 0x00, 0x88, 0x77, 0x66, 0x55,
  737. 0x00, 0x00, 0x00, 0x00, 0x44, 0x33, 0x22, 0x11,
  738. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  739. ];
  740. let mut vm = rbpf::EbpfVmNoData::new(prog);
  741. vm.jit_compile();
  742. unsafe { assert_eq!(vm.prog_exec_jit(), 0x1122334455667788); }
  743. }
  744. #[test]
  745. fn test_jit_lddw2() {
  746. let prog = &[
  747. 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
  748. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  749. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  750. ];
  751. let mut vm = rbpf::EbpfVmNoData::new(prog);
  752. vm.jit_compile();
  753. unsafe { assert_eq!(vm.prog_exec_jit(), 0x80000000); }
  754. }
  755. #[test]
  756. fn test_jit_ldxb_all() {
  757. let prog = &[
  758. 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  759. 0x71, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  760. 0x67, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  761. 0x71, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  762. 0x67, 0x08, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  763. 0x71, 0x07, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  764. 0x67, 0x07, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  765. 0x71, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
  766. 0x67, 0x06, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
  767. 0x71, 0x05, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  768. 0x67, 0x05, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  769. 0x71, 0x04, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
  770. 0x67, 0x04, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00,
  771. 0x71, 0x03, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
  772. 0x67, 0x03, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
  773. 0x71, 0x02, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
  774. 0x67, 0x02, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
  775. 0x71, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  776. 0x67, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  777. 0x71, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00,
  778. 0x67, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
  779. 0x4f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  780. 0x4f, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  781. 0x4f, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  782. 0x4f, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  783. 0x4f, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  784. 0x4f, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  785. 0x4f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  786. 0x4f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  787. 0x4f, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  788. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  789. ];
  790. let mem = &mut [
  791. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  792. 0x08, 0x09
  793. ];
  794. let mut vm = rbpf::EbpfVmRaw::new(prog);
  795. vm.jit_compile();
  796. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x9876543210); }
  797. }
  798. #[test]
  799. fn test_jit_ldxb() {
  800. let prog = &[
  801. 0x71, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  802. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  803. ];
  804. let mem = &mut [
  805. 0xaa, 0xbb, 0x11, 0xcc, 0xdd
  806. ];
  807. let mut vm = rbpf::EbpfVmRaw::new(prog);
  808. vm.jit_compile();
  809. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x11); }
  810. }
  811. #[test]
  812. fn test_jit_ldxdw() {
  813. let prog = &[
  814. 0x79, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  815. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  816. ];
  817. let mem = &mut [
  818. 0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
  819. 0x77, 0x88, 0xcc, 0xdd
  820. ];
  821. let mut vm = rbpf::EbpfVmRaw::new(prog);
  822. vm.jit_compile();
  823. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x8877665544332211); }
  824. }
  825. #[test]
  826. fn test_jit_ldxh_all() {
  827. let prog = &[
  828. 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  829. 0x69, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  830. 0xdc, 0x09, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  831. 0x67, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  832. 0x69, 0x08, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  833. 0xdc, 0x08, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  834. 0x67, 0x08, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  835. 0x69, 0x07, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  836. 0xdc, 0x07, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  837. 0x67, 0x07, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  838. 0x69, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
  839. 0xdc, 0x06, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  840. 0x67, 0x06, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
  841. 0x69, 0x05, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  842. 0xdc, 0x05, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  843. 0x67, 0x05, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  844. 0x69, 0x04, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,
  845. 0xdc, 0x04, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  846. 0x67, 0x04, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00,
  847. 0x69, 0x03, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
  848. 0xdc, 0x03, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  849. 0x67, 0x03, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
  850. 0x69, 0x02, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00,
  851. 0xdc, 0x02, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  852. 0x67, 0x02, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
  853. 0x69, 0x01, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
  854. 0xdc, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  855. 0x67, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  856. 0x69, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00,
  857. 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  858. 0x67, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
  859. 0x4f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  860. 0x4f, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  861. 0x4f, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  862. 0x4f, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  863. 0x4f, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  864. 0x4f, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  865. 0x4f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  866. 0x4f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  867. 0x4f, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  868. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  869. ];
  870. let mem = &mut [
  871. 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03,
  872. 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07,
  873. 0x00, 0x08, 0x00, 0x09
  874. ];
  875. let mut vm = rbpf::EbpfVmRaw::new(prog);
  876. vm.jit_compile();
  877. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x9876543210); }
  878. }
  879. #[test]
  880. fn test_jit_ldxh_all2() {
  881. let prog = &[
  882. 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  883. 0x69, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  884. 0xdc, 0x09, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  885. 0x69, 0x08, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  886. 0xdc, 0x08, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  887. 0x69, 0x07, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  888. 0xdc, 0x07, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  889. 0x69, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
  890. 0xdc, 0x06, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  891. 0x69, 0x05, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  892. 0xdc, 0x05, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  893. 0x69, 0x04, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,
  894. 0xdc, 0x04, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  895. 0x69, 0x03, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
  896. 0xdc, 0x03, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  897. 0x69, 0x02, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00,
  898. 0xdc, 0x02, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  899. 0x69, 0x01, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
  900. 0xdc, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  901. 0x69, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00,
  902. 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  903. 0x4f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  904. 0x4f, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  905. 0x4f, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  906. 0x4f, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  907. 0x4f, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  908. 0x4f, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  909. 0x4f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  910. 0x4f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  911. 0x4f, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  912. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  913. ];
  914. let mem = &mut [
  915. 0x00, 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08,
  916. 0x00, 0x10, 0x00, 0x20, 0x00, 0x40, 0x00, 0x80,
  917. 0x01, 0x00, 0x02, 0x00
  918. ];
  919. let mut vm = rbpf::EbpfVmRaw::new(prog);
  920. vm.jit_compile();
  921. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x3ff); }
  922. }
  923. #[test]
  924. fn test_jit_ldxh() {
  925. let prog = &[
  926. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  927. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  928. ];
  929. let mem = &mut [
  930. 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
  931. ];
  932. let mut vm = rbpf::EbpfVmRaw::new(prog);
  933. vm.jit_compile();
  934. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x2211); }
  935. }
  936. #[test]
  937. fn test_jit_ldxh_same_reg() {
  938. let prog = &[
  939. 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  940. 0x6a, 0x00, 0x00, 0x00, 0x34, 0x12, 0x00, 0x00,
  941. 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  942. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  943. ];
  944. let mem = &mut [
  945. 0xff, 0xff
  946. ];
  947. let mut vm = rbpf::EbpfVmRaw::new(prog);
  948. vm.jit_compile();
  949. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x1234); }
  950. }
  951. #[test]
  952. fn test_jit_ldxw_all() {
  953. let prog = &[
  954. 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  955. 0x61, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  956. 0xdc, 0x09, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  957. 0x61, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  958. 0xdc, 0x08, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  959. 0x61, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  960. 0xdc, 0x07, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  961. 0x61, 0x06, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
  962. 0xdc, 0x06, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  963. 0x61, 0x05, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
  964. 0xdc, 0x05, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  965. 0x61, 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00,
  966. 0xdc, 0x04, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  967. 0x61, 0x03, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
  968. 0xdc, 0x03, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  969. 0x61, 0x02, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00,
  970. 0xdc, 0x02, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  971. 0x61, 0x01, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
  972. 0xdc, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  973. 0x61, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00,
  974. 0xdc, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  975. 0x4f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  976. 0x4f, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  977. 0x4f, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  978. 0x4f, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  979. 0x4f, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  980. 0x4f, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  981. 0x4f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  982. 0x4f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  983. 0x4f, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  984. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  985. ];
  986. let mem = &mut [
  987. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
  988. 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x08,
  989. 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00,
  990. 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x08, 0x00,
  991. 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00
  992. ];
  993. let mut vm = rbpf::EbpfVmRaw::new(prog);
  994. vm.jit_compile();
  995. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x030f0f); }
  996. }
  997. #[test]
  998. fn test_jit_ldxw() {
  999. let prog = &[
  1000. 0x61, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1001. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1002. ];
  1003. let mem = &mut [
  1004. 0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0xcc, 0xdd
  1005. ];
  1006. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1007. vm.jit_compile();
  1008. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x44332211); }
  1009. }
  1010. #[test]
  1011. fn test_jit_le16() {
  1012. let prog = &[
  1013. 0x69, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1014. 0xd4, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  1015. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1016. ];
  1017. let mem = &mut [
  1018. 0x22, 0x11
  1019. ];
  1020. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1021. vm.jit_compile();
  1022. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x1122); }
  1023. }
  1024. #[test]
  1025. fn test_jit_le32() {
  1026. let prog = &[
  1027. 0x61, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1028. 0xd4, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  1029. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1030. ];
  1031. let mem = &mut [
  1032. 0x44, 0x33, 0x22, 0x11
  1033. ];
  1034. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1035. vm.jit_compile();
  1036. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x11223344); }
  1037. }
  1038. #[test]
  1039. fn test_jit_le64() {
  1040. let prog = &[
  1041. 0x79, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1042. 0xd4, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  1043. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1044. ];
  1045. let mem = &mut [
  1046. 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11
  1047. ];
  1048. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1049. vm.jit_compile();
  1050. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x1122334455667788); }
  1051. }
  1052. #[test]
  1053. fn test_jit_lsh_reg() {
  1054. let prog = &[
  1055. 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  1056. 0xb7, 0x07, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  1057. 0x6f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1058. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1059. ];
  1060. let mut vm = rbpf::EbpfVmNoData::new(prog);
  1061. vm.jit_compile();
  1062. unsafe { assert_eq!(vm.prog_exec_jit(), 0x10); }
  1063. }
  1064. #[test]
  1065. fn test_jit_mod() {
  1066. let prog = &[
  1067. 0xb4, 0x00, 0x00, 0x00, 0x74, 0x16, 0x00, 0x00,
  1068. 0x94, 0x00, 0x00, 0x00, 0x5c, 0x00, 0x00, 0x00,
  1069. 0xb4, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00,
  1070. 0x9c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1071. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1072. ];
  1073. let mut vm = rbpf::EbpfVmNoData::new(prog);
  1074. vm.jit_compile();
  1075. unsafe { assert_eq!(vm.prog_exec_jit(), 0x5); }
  1076. }
  1077. #[test]
  1078. fn test_jit_mod32() {
  1079. let prog = &[
  1080. 0x18, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  1081. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  1082. 0x94, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  1083. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1084. ];
  1085. let mut vm = rbpf::EbpfVmNoData::new(prog);
  1086. vm.jit_compile();
  1087. unsafe { assert_eq!(vm.prog_exec_jit(), 0x0); }
  1088. }
  1089. #[test]
  1090. fn test_jit_mod64() {
  1091. let prog = &[
  1092. 0xb4, 0x00, 0x00, 0x00, 0x36, 0x84, 0x85, 0xb1,
  1093. 0x67, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  1094. 0x47, 0x00, 0x00, 0x00, 0xc8, 0xc5, 0x0d, 0x10,
  1095. 0xb4, 0x01, 0x00, 0x00, 0x3e, 0x26, 0xde, 0x0d,
  1096. 0x67, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  1097. 0x47, 0x01, 0x00, 0x00, 0xf3, 0xf7, 0xbe, 0x3c,
  1098. 0x9f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1099. 0x97, 0x00, 0x00, 0x00, 0x78, 0x17, 0x8f, 0x65,
  1100. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1101. ];
  1102. let mut vm = rbpf::EbpfVmNoData::new(prog);
  1103. vm.jit_compile();
  1104. unsafe { assert_eq!(vm.prog_exec_jit(), 0x30ba5a04); }
  1105. }
  1106. #[test]
  1107. fn test_jit_mov() {
  1108. let prog = &[
  1109. 0xb4, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  1110. 0xbc, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1111. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1112. ];
  1113. let mut vm = rbpf::EbpfVmNoData::new(prog);
  1114. vm.jit_compile();
  1115. unsafe { assert_eq!(vm.prog_exec_jit(), 0x1); }
  1116. }
  1117. #[test]
  1118. fn test_jit_mul32_imm() {
  1119. let prog = &[
  1120. 0xb7, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  1121. 0x24, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  1122. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1123. ];
  1124. let mut vm = rbpf::EbpfVmNoData::new(prog);
  1125. vm.jit_compile();
  1126. unsafe { assert_eq!(vm.prog_exec_jit(), 0xc); }
  1127. }
  1128. #[test]
  1129. fn test_jit_mul32_reg() {
  1130. let prog = &[
  1131. 0xb7, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  1132. 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  1133. 0x2c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1134. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1135. ];
  1136. let mut vm = rbpf::EbpfVmNoData::new(prog);
  1137. vm.jit_compile();
  1138. unsafe { assert_eq!(vm.prog_exec_jit(), 0xc); }
  1139. }
  1140. #[test]
  1141. fn test_jit_mul32_reg_overflow() {
  1142. let prog = &[
  1143. 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x40,
  1144. 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  1145. 0x2c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1146. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1147. ];
  1148. let mut vm = rbpf::EbpfVmNoData::new(prog);
  1149. vm.jit_compile();
  1150. unsafe { assert_eq!(vm.prog_exec_jit(), 0x4); }
  1151. }
  1152. #[test]
  1153. fn test_jit_mul64_imm() {
  1154. let prog = &[
  1155. 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x40,
  1156. 0x27, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  1157. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1158. ];
  1159. let mut vm = rbpf::EbpfVmNoData::new(prog);
  1160. vm.jit_compile();
  1161. unsafe { assert_eq!(vm.prog_exec_jit(), 0x100000004); }
  1162. }
  1163. #[test]
  1164. fn test_jit_mul64_reg() {
  1165. let prog = &[
  1166. 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x40,
  1167. 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  1168. 0x2f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1169. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1170. ];
  1171. let mut vm = rbpf::EbpfVmNoData::new(prog);
  1172. vm.jit_compile();
  1173. unsafe { assert_eq!(vm.prog_exec_jit(), 0x100000004); }
  1174. }
  1175. #[test]
  1176. fn test_jit_mul_loop() {
  1177. let prog = &[
  1178. 0xb7, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  1179. 0x07, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
  1180. 0x67, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  1181. 0x77, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  1182. 0x15, 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  1183. 0xb7, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  1184. 0x27, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  1185. 0x07, 0x01, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  1186. 0x55, 0x01, 0xfd, 0xff, 0x00, 0x00, 0x00, 0x00,
  1187. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1188. ];
  1189. let mut vm = rbpf::EbpfVmNoData::new(prog);
  1190. vm.jit_compile();
  1191. unsafe { assert_eq!(vm.prog_exec_jit(), 0x75db9c97); }
  1192. }
  1193. #[test]
  1194. fn test_jit_neg64() {
  1195. let prog = &[
  1196. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  1197. 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1198. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1199. ];
  1200. let mut vm = rbpf::EbpfVmNoData::new(prog);
  1201. vm.jit_compile();
  1202. unsafe { assert_eq!(vm.prog_exec_jit(), 0xfffffffffffffffe); }
  1203. }
  1204. #[test]
  1205. fn test_jit_neg() {
  1206. let prog = &[
  1207. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  1208. 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1209. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1210. ];
  1211. let mut vm = rbpf::EbpfVmNoData::new(prog);
  1212. vm.jit_compile();
  1213. unsafe { assert_eq!(vm.prog_exec_jit(), 0xfffffffe); }
  1214. }
  1215. #[test]
  1216. fn test_jit_prime() {
  1217. let prog = &[
  1218. 0xb7, 0x01, 0x00, 0x00, 0x43, 0x00, 0x00, 0x00,
  1219. 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  1220. 0xb7, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  1221. 0x25, 0x01, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00,
  1222. 0x05, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,
  1223. 0x07, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  1224. 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  1225. 0x3d, 0x12, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
  1226. 0xbf, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1227. 0x3f, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1228. 0x2f, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1229. 0xbf, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1230. 0x1f, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1231. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1232. 0x55, 0x04, 0xf6, 0xff, 0x00, 0x00, 0x00, 0x00,
  1233. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1234. ];
  1235. let mut vm = rbpf::EbpfVmNoData::new(prog);
  1236. vm.jit_compile();
  1237. unsafe { assert_eq!(vm.prog_exec_jit(), 0x1); }
  1238. }
  1239. #[test]
  1240. fn test_jit_rhs32() {
  1241. let prog = &[
  1242. 0xaf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1243. 0x17, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  1244. 0x74, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  1245. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1246. ];
  1247. let mut vm = rbpf::EbpfVmNoData::new(prog);
  1248. vm.jit_compile();
  1249. unsafe { assert_eq!(vm.prog_exec_jit(), 0x00ffffff); }
  1250. }
  1251. #[test]
  1252. fn test_jit_rsh_reg() {
  1253. let prog = &[
  1254. 0xb7, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  1255. 0xb7, 0x07, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  1256. 0x7f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1257. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1258. ];
  1259. let mut vm = rbpf::EbpfVmNoData::new(prog);
  1260. vm.jit_compile();
  1261. unsafe { assert_eq!(vm.prog_exec_jit(), 0x1); }
  1262. }
  1263. #[test]
  1264. fn test_jit_stack() {
  1265. let prog = &[
  1266. 0xb7, 0x01, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
  1267. 0x7a, 0x0a, 0xf0, 0xff, 0xab, 0x00, 0x00, 0x00,
  1268. 0x7a, 0x0a, 0xf8, 0xff, 0xcd, 0x00, 0x00, 0x00,
  1269. 0x57, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  1270. 0x67, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  1271. 0xbf, 0xa2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1272. 0x0f, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1273. 0x79, 0x20, 0xf0, 0xff, 0x00, 0x00, 0x00, 0x00,
  1274. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1275. ];
  1276. let mut vm = rbpf::EbpfVmNoData::new(prog);
  1277. vm.jit_compile();
  1278. unsafe { assert_eq!(vm.prog_exec_jit(), 0xcd); }
  1279. }
  1280. #[test]
  1281. fn test_jit_stack2() {
  1282. let prog = &[
  1283. 0x72, 0x0a, 0xfc, 0xff, 0x01, 0x00, 0x00, 0x00,
  1284. 0x72, 0x0a, 0xfd, 0xff, 0x02, 0x00, 0x00, 0x00,
  1285. 0x72, 0x0a, 0xfe, 0xff, 0x03, 0x00, 0x00, 0x00,
  1286. 0x72, 0x0a, 0xff, 0xff, 0x04, 0x00, 0x00, 0x00,
  1287. 0xbf, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1288. 0xb7, 0x02, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  1289. 0x1f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1290. 0x85, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  1291. 0xb7, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1292. 0x71, 0xa2, 0xfc, 0xff, 0x00, 0x00, 0x00, 0x00,
  1293. 0x71, 0xa3, 0xfd, 0xff, 0x00, 0x00, 0x00, 0x00,
  1294. 0x71, 0xa4, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00,
  1295. 0x71, 0xa5, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  1296. 0x85, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1297. 0xa7, 0x00, 0x00, 0x00, 0x2a, 0x2a, 0x2a, 0x2a,
  1298. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1299. ];
  1300. let mut vm = rbpf::EbpfVmNoData::new(prog);
  1301. vm.register_helper(0, helpers::gather_bytes);
  1302. vm.register_helper(1, helpers::memfrob);
  1303. vm.jit_compile();
  1304. unsafe { assert_eq!(vm.prog_exec_jit(), 0x01020304); }
  1305. }
  1306. #[test]
  1307. fn test_jit_stb() {
  1308. let prog = &[
  1309. 0x72, 0x01, 0x02, 0x00, 0x11, 0x00, 0x00, 0x00,
  1310. 0x71, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1311. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1312. ];
  1313. let mem = &mut [
  1314. 0xaa, 0xbb, 0xff, 0xcc, 0xdd
  1315. ];
  1316. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1317. vm.jit_compile();
  1318. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x11); }
  1319. }
  1320. #[test]
  1321. fn test_jit_stdw() {
  1322. let prog = &[
  1323. 0x7a, 0x01, 0x02, 0x00, 0x11, 0x22, 0x33, 0x44,
  1324. 0x79, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1325. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1326. ];
  1327. let mem = &mut [
  1328. 0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1329. 0xff, 0xff, 0xcc, 0xdd
  1330. ];
  1331. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1332. vm.jit_compile();
  1333. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x44332211); }
  1334. }
  1335. #[test]
  1336. fn test_jit_sth() {
  1337. let prog = &[
  1338. 0x6a, 0x01, 0x02, 0x00, 0x11, 0x22, 0x00, 0x00,
  1339. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1340. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1341. ];
  1342. let mem = &mut [
  1343. 0xaa, 0xbb, 0xff, 0xff, 0xcc, 0xdd
  1344. ];
  1345. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1346. vm.jit_compile();
  1347. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x2211); }
  1348. }
  1349. #[test]
  1350. fn test_jit_string_stack() {
  1351. let prog = &[
  1352. 0xb7, 0x01, 0x00, 0x00, 0x61, 0x62, 0x63, 0x78,
  1353. 0x63, 0x1a, 0xf8, 0xff, 0x00, 0x00, 0x00, 0x00,
  1354. 0xb7, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1355. 0x73, 0x6a, 0xfc, 0xff, 0x00, 0x00, 0x00, 0x00,
  1356. 0x73, 0x6a, 0xf4, 0xff, 0x00, 0x00, 0x00, 0x00,
  1357. 0xb7, 0x01, 0x00, 0x00, 0x61, 0x62, 0x63, 0x79,
  1358. 0x63, 0x1a, 0xf0, 0xff, 0x00, 0x00, 0x00, 0x00,
  1359. 0xbf, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1360. 0x07, 0x01, 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff,
  1361. 0xbf, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1362. 0x85, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  1363. 0xbf, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1364. 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  1365. 0x67, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  1366. 0x77, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  1367. 0x55, 0x01, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00,
  1368. 0xbf, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1369. 0x07, 0x01, 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff,
  1370. 0xbf, 0xa2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1371. 0x07, 0x02, 0x00, 0x00, 0xf0, 0xff, 0xff, 0xff,
  1372. 0x85, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  1373. 0xbf, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1374. 0x67, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  1375. 0x77, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  1376. 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  1377. 0x1d, 0x61, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  1378. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1379. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1380. ];
  1381. let mut vm = rbpf::EbpfVmNoData::new(prog);
  1382. vm.register_helper(4, helpers::strcmp);
  1383. vm.jit_compile();
  1384. unsafe { assert_eq!(vm.prog_exec_jit(), 0x0); }
  1385. }
  1386. #[test]
  1387. fn test_jit_stw() {
  1388. let prog = &[
  1389. 0x62, 0x01, 0x02, 0x00, 0x11, 0x22, 0x33, 0x44,
  1390. 0x61, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1391. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1392. ];
  1393. let mem = &mut [
  1394. 0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd
  1395. ];
  1396. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1397. vm.jit_compile();
  1398. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x44332211); }
  1399. }
  1400. #[test]
  1401. fn test_jit_stxb() {
  1402. let prog = &[
  1403. 0xb4, 0x02, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00,
  1404. 0x73, 0x21, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1405. 0x71, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1406. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1407. ];
  1408. let mem = &mut [
  1409. 0xaa, 0xbb, 0xff, 0xcc, 0xdd
  1410. ];
  1411. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1412. vm.jit_compile();
  1413. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x11); }
  1414. }
  1415. #[test]
  1416. fn test_jit_stxb_all() {
  1417. let prog = &[
  1418. 0xb7, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00,
  1419. 0xb7, 0x02, 0x00, 0x00, 0xf2, 0x00, 0x00, 0x00,
  1420. 0xb7, 0x03, 0x00, 0x00, 0xf3, 0x00, 0x00, 0x00,
  1421. 0xb7, 0x04, 0x00, 0x00, 0xf4, 0x00, 0x00, 0x00,
  1422. 0xb7, 0x05, 0x00, 0x00, 0xf5, 0x00, 0x00, 0x00,
  1423. 0xb7, 0x06, 0x00, 0x00, 0xf6, 0x00, 0x00, 0x00,
  1424. 0xb7, 0x07, 0x00, 0x00, 0xf7, 0x00, 0x00, 0x00,
  1425. 0xb7, 0x08, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00,
  1426. 0x73, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1427. 0x73, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  1428. 0x73, 0x31, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1429. 0x73, 0x41, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
  1430. 0x73, 0x51, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  1431. 0x73, 0x61, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
  1432. 0x73, 0x71, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
  1433. 0x73, 0x81, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
  1434. 0x79, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1435. 0xdc, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  1436. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1437. ];
  1438. let mem = &mut [
  1439. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  1440. ];
  1441. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1442. vm.jit_compile();
  1443. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0xf0f2f3f4f5f6f7f8); }
  1444. }
  1445. #[test]
  1446. fn test_jit_stxb_all2() {
  1447. let prog = &[
  1448. 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1449. 0xb7, 0x01, 0x00, 0x00, 0xf1, 0x00, 0x00, 0x00,
  1450. 0xb7, 0x09, 0x00, 0x00, 0xf9, 0x00, 0x00, 0x00,
  1451. 0x73, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1452. 0x73, 0x90, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  1453. 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1454. 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  1455. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1456. ];
  1457. let mem = &mut [
  1458. 0xff, 0xff
  1459. ];
  1460. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1461. vm.jit_compile();
  1462. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0xf1f9); }
  1463. }
  1464. #[test]
  1465. fn test_jit_stxb_chain() {
  1466. let prog = &[
  1467. 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1468. 0x71, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1469. 0x73, 0x90, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  1470. 0x71, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  1471. 0x73, 0x80, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1472. 0x71, 0x07, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1473. 0x73, 0x70, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
  1474. 0x71, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
  1475. 0x73, 0x60, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  1476. 0x71, 0x05, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  1477. 0x73, 0x50, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
  1478. 0x71, 0x04, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
  1479. 0x73, 0x40, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
  1480. 0x71, 0x03, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
  1481. 0x73, 0x30, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
  1482. 0x71, 0x02, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
  1483. 0x73, 0x20, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  1484. 0x71, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  1485. 0x73, 0x10, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00,
  1486. 0x71, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00,
  1487. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1488. ];
  1489. let mem = &mut [
  1490. 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1491. 0x00, 0x00
  1492. ];
  1493. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1494. vm.jit_compile();
  1495. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x2a); }
  1496. }
  1497. #[test]
  1498. fn test_jit_stxdw() {
  1499. let prog = &[
  1500. 0xb7, 0x02, 0x00, 0x00, 0x55, 0x66, 0x77, 0x88,
  1501. 0x67, 0x02, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  1502. 0x47, 0x02, 0x00, 0x00, 0x11, 0x22, 0x33, 0x44,
  1503. 0x7b, 0x21, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1504. 0x79, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1505. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1506. ];
  1507. let mem = &mut [
  1508. 0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1509. 0xff, 0xff, 0xcc, 0xdd
  1510. ];
  1511. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1512. vm.jit_compile();
  1513. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x8877665544332211); }
  1514. }
  1515. #[test]
  1516. fn test_jit_stxh() {
  1517. let prog = &[
  1518. 0xb4, 0x02, 0x00, 0x00, 0x11, 0x22, 0x00, 0x00,
  1519. 0x6b, 0x21, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1520. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1521. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1522. ];
  1523. let mem = &mut [
  1524. 0xaa, 0xbb, 0xff, 0xff, 0xcc, 0xdd
  1525. ];
  1526. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1527. vm.jit_compile();
  1528. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x2211); }
  1529. }
  1530. #[test]
  1531. fn test_jit_stxw() {
  1532. let prog = &[
  1533. 0xb4, 0x02, 0x00, 0x00, 0x11, 0x22, 0x33, 0x44,
  1534. 0x63, 0x21, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1535. 0x61, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1536. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1537. ];
  1538. let mem = &mut [
  1539. 0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd
  1540. ];
  1541. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1542. vm.jit_compile();
  1543. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x44332211); }
  1544. }
  1545. #[test]
  1546. fn test_jit_subnet() {
  1547. let prog = &[
  1548. 0xb7, 0x02, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00,
  1549. 0x69, 0x13, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
  1550. 0x55, 0x03, 0x02, 0x00, 0x81, 0x00, 0x00, 0x00,
  1551. 0xb7, 0x02, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00,
  1552. 0x69, 0x13, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
  1553. 0x57, 0x03, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
  1554. 0x55, 0x03, 0x05, 0x00, 0x08, 0x00, 0x00, 0x00,
  1555. 0x0f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1556. 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  1557. 0x61, 0x11, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
  1558. 0x57, 0x01, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00,
  1559. 0x15, 0x01, 0x01, 0x00, 0xc0, 0xa8, 0x01, 0x00,
  1560. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1561. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1562. ];
  1563. let mem = &mut [
  1564. 0x00, 0x00, 0xc0, 0x9f, 0xa0, 0x97, 0x00, 0xa0,
  1565. 0xcc, 0x3b, 0xbf, 0xfa, 0x08, 0x00, 0x45, 0x10,
  1566. 0x00, 0x3c, 0x46, 0x3c, 0x40, 0x00, 0x40, 0x06,
  1567. 0x73, 0x1c, 0xc0, 0xa8, 0x01, 0x02, 0xc0, 0xa8,
  1568. 0x01, 0x01, 0x06, 0x0e, 0x00, 0x17, 0x99, 0xc5,
  1569. 0xa0, 0xec, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x02,
  1570. 0x7d, 0x78, 0xe0, 0xa3, 0x00, 0x00, 0x02, 0x04,
  1571. 0x05, 0xb4, 0x04, 0x02, 0x08, 0x0a, 0x00, 0x9c,
  1572. 0x27, 0x24, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03,
  1573. 0x03, 0x00
  1574. ];
  1575. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1576. vm.jit_compile();
  1577. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x1); }
  1578. }
  1579. const PROG_TCP_PORT_80: [u8;152] = [
  1580. 0x71, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
  1581. 0x71, 0x13, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00,
  1582. 0x67, 0x03, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  1583. 0x4f, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1584. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1585. 0x55, 0x03, 0x0c, 0x00, 0x08, 0x00, 0x00, 0x00,
  1586. 0x71, 0x12, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00,
  1587. 0x55, 0x02, 0x0a, 0x00, 0x06, 0x00, 0x00, 0x00,
  1588. 0x71, 0x12, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00,
  1589. 0x07, 0x01, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00,
  1590. 0x57, 0x02, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00,
  1591. 0x67, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  1592. 0x0f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1593. 0x69, 0x12, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  1594. 0x15, 0x02, 0x02, 0x00, 0x00, 0x50, 0x00, 0x00,
  1595. 0x69, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1596. 0x55, 0x01, 0x01, 0x00, 0x00, 0x50, 0x00, 0x00,
  1597. 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  1598. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1599. ];
  1600. #[test]
  1601. fn test_jit_tcp_port80_match() {
  1602. let mem = &mut [
  1603. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06,
  1604. 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45, 0x00,
  1605. 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06,
  1606. 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01, 0xc0, 0xa8,
  1607. 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00,
  1608. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02,
  1609. 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44,
  1610. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1611. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1612. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1613. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1614. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1615. 0x44, 0x44, 0x44, 0x44
  1616. ];
  1617. let prog = &PROG_TCP_PORT_80;
  1618. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1619. vm.jit_compile();
  1620. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x1); }
  1621. }
  1622. #[test]
  1623. fn test_jit_tcp_port80_nomatch() {
  1624. let mem = &mut [
  1625. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06,
  1626. 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45, 0x00,
  1627. 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06,
  1628. 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01, 0xc0, 0xa8,
  1629. 0x00, 0x02, 0x00, 0x16, 0x27, 0x10, 0x00, 0x00,
  1630. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x51, 0x02,
  1631. 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44,
  1632. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1633. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1634. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1635. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1636. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1637. 0x44, 0x44, 0x44, 0x44
  1638. ];
  1639. let prog = &PROG_TCP_PORT_80;
  1640. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1641. vm.jit_compile();
  1642. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x0); }
  1643. }
  1644. #[test]
  1645. fn test_jit_tcp_port80_nomatch_ethertype() {
  1646. let mem = &mut [
  1647. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06,
  1648. 0x07, 0x08, 0x09, 0x0a, 0x08, 0x01, 0x45, 0x00,
  1649. 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06,
  1650. 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01, 0xc0, 0xa8,
  1651. 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00,
  1652. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02,
  1653. 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44,
  1654. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1655. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1656. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1657. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1658. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1659. 0x44, 0x44, 0x44, 0x44
  1660. ];
  1661. let prog = &PROG_TCP_PORT_80;
  1662. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1663. vm.jit_compile();
  1664. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x0); }
  1665. }
  1666. #[test]
  1667. fn test_jit_tcp_port80_nomatch_proto() {
  1668. let mem = &mut [
  1669. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06,
  1670. 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45, 0x00,
  1671. 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11,
  1672. 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01, 0xc0, 0xa8,
  1673. 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00,
  1674. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02,
  1675. 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44,
  1676. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1677. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1678. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1679. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1680. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  1681. 0x44, 0x44, 0x44, 0x44
  1682. ];
  1683. let prog = &PROG_TCP_PORT_80;
  1684. let mut vm = rbpf::EbpfVmRaw::new(prog);
  1685. vm.jit_compile();
  1686. unsafe { assert_eq!(vm.prog_exec_jit(mem), 0x0); }
  1687. }
  1688. #[test]
  1689. fn test_jit_tcp_sack_match() {
  1690. let mut mem = TCP_SACK_MATCH.to_vec();
  1691. let prog = assemble(TCP_SACK_ASM).unwrap();
  1692. let mut vm = rbpf::EbpfVmRaw::new(&prog);
  1693. vm.jit_compile();
  1694. unsafe { assert_eq!(vm.prog_exec_jit(mem.as_mut_slice()), 0x1); }
  1695. }
  1696. #[test]
  1697. fn test_jit_tcp_sack_nomatch() {
  1698. let mut mem = TCP_SACK_NOMATCH.to_vec();
  1699. let prog = assemble(TCP_SACK_ASM).unwrap();
  1700. let mut vm = rbpf::EbpfVmRaw::new(&prog);
  1701. vm.jit_compile();
  1702. unsafe { assert_eq!(vm.prog_exec_jit(mem.as_mut_slice()), 0x0); }
  1703. }