ubpf_vm.rs 64 KB

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