ubpf_vm.rs 65 KB

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