ubpf_jit_x86_64.rs 70 KB

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