4
0

ubpf_vm.rs 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  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. mod common;
  21. use rbpf::helpers;
  22. use rbpf::assembler::assemble;
  23. use common::{TCP_SACK_ASM, TCP_SACK_MATCH, TCP_SACK_NOMATCH};
  24. #[test]
  25. fn test_vm_add() {
  26. let prog = &[
  27. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  28. 0xb4, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  29. 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  30. 0x0c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  32. ];
  33. let vm = rbpf::EbpfVmNoData::new(prog);
  34. assert_eq!(vm.prog_exec(), 0x3);
  35. }
  36. #[test]
  37. fn test_vm_alu64_arith() {
  38. let prog = &[
  39. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  40. 0xb7, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  41. 0xb7, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  42. 0xb7, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  43. 0xb7, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  44. 0xb7, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  45. 0xb7, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
  46. 0xb7, 0x07, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  47. 0xb7, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  48. 0xb7, 0x09, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
  49. 0x07, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  50. 0x0f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51. 0x17, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00,
  52. 0x1f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  53. 0x27, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  54. 0x2f, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  55. 0x37, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  56. 0x3f, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  57. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  58. ];
  59. let vm = rbpf::EbpfVmNoData::new(prog);
  60. assert_eq!(vm.prog_exec(), 0x2a);
  61. }
  62. #[test]
  63. fn test_vm_alu64_bit() {
  64. let prog = &[
  65. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0xb7, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  67. 0xb7, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  68. 0xb7, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  69. 0xb7, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  70. 0xb7, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  71. 0xb7, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
  72. 0xb7, 0x07, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  73. 0xb7, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  74. 0x4f, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  75. 0x47, 0x00, 0x00, 0x00, 0xa0, 0x00, 0x00, 0x00,
  76. 0x57, 0x00, 0x00, 0x00, 0xa3, 0x00, 0x00, 0x00,
  77. 0xb7, 0x09, 0x00, 0x00, 0x91, 0x00, 0x00, 0x00,
  78. 0x5f, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79. 0x67, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  80. 0x67, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00,
  81. 0x6f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  82. 0x77, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  83. 0x77, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
  84. 0x7f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  85. 0xa7, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  86. 0xaf, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  87. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  88. ];
  89. let vm = rbpf::EbpfVmNoData::new(prog);
  90. assert_eq!(vm.prog_exec(), 0x11);
  91. }
  92. #[test]
  93. fn test_vm_alu_arith() {
  94. let prog = &[
  95. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  96. 0xb4, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  97. 0xb4, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  98. 0xb4, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  99. 0xb4, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  100. 0xb4, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  101. 0xb4, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
  102. 0xb4, 0x07, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  103. 0xb4, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  104. 0xb4, 0x09, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
  105. 0x04, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  106. 0x0c, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  107. 0x14, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00,
  108. 0x1c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  109. 0x24, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  110. 0x2c, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  111. 0x34, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  112. 0x3c, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  113. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  114. ];
  115. let vm = rbpf::EbpfVmNoData::new(prog);
  116. assert_eq!(vm.prog_exec(), 0x2a);
  117. }
  118. #[test]
  119. fn test_vm_alu_bit() {
  120. let prog = &[
  121. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  122. 0xb4, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  123. 0xb4, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  124. 0xb4, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  125. 0xb4, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  126. 0xb4, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  127. 0xb4, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
  128. 0xb4, 0x07, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  129. 0xb4, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  130. 0x4c, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  131. 0x44, 0x00, 0x00, 0x00, 0xa0, 0x00, 0x00, 0x00,
  132. 0x54, 0x00, 0x00, 0x00, 0xa3, 0x00, 0x00, 0x00,
  133. 0xb4, 0x09, 0x00, 0x00, 0x91, 0x00, 0x00, 0x00,
  134. 0x5c, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  135. 0x64, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00,
  136. 0x6c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  137. 0x74, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
  138. 0x7c, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  139. 0xa4, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  140. 0xac, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  141. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  142. ];
  143. let vm = rbpf::EbpfVmNoData::new(prog);
  144. assert_eq!(vm.prog_exec(), 0x11);
  145. }
  146. #[test]
  147. fn test_vm_arsh32_high_shift() {
  148. let prog = &[
  149. 0xb7, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  150. 0x18, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  151. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  152. 0xcc, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  153. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  154. ];
  155. let vm = rbpf::EbpfVmNoData::new(prog);
  156. assert_eq!(vm.prog_exec(), 0x4);
  157. }
  158. #[test]
  159. fn test_vm_arsh() {
  160. let prog = &[
  161. 0xb4, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00,
  162. 0x64, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
  163. 0xc4, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  164. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  165. ];
  166. let vm = rbpf::EbpfVmNoData::new(prog);
  167. assert_eq!(vm.prog_exec(), 0xffff8000);
  168. }
  169. #[test]
  170. fn test_vm_arsh64() {
  171. let prog = &[
  172. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  173. 0x67, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00,
  174. 0xc7, 0x00, 0x00, 0x00, 0x37, 0x00, 0x00, 0x00,
  175. 0xb4, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  176. 0xcf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  177. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  178. ];
  179. let vm = rbpf::EbpfVmNoData::new(prog);
  180. assert_eq!(vm.prog_exec(), 0xfffffffffffffff8);
  181. }
  182. #[test]
  183. fn test_vm_arsh_reg() {
  184. let prog = &[
  185. 0xb4, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00,
  186. 0xb4, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  187. 0x64, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
  188. 0xcc, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  189. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  190. ];
  191. let vm = rbpf::EbpfVmNoData::new(prog);
  192. assert_eq!(vm.prog_exec(), 0xffff8000);
  193. }
  194. #[test]
  195. fn test_vm_be16() {
  196. let prog = &[
  197. 0x69, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  198. 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  199. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  200. ];
  201. let mem = &mut [
  202. 0x11, 0x22
  203. ];
  204. let vm = rbpf::EbpfVmRaw::new(prog);
  205. assert_eq!(vm.prog_exec(mem), 0x1122);
  206. }
  207. #[test]
  208. fn test_vm_be16_high() {
  209. let prog = &[
  210. 0x79, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  211. 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  212. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  213. ];
  214. let mem = &mut [
  215. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88
  216. ];
  217. let vm = rbpf::EbpfVmRaw::new(prog);
  218. assert_eq!(vm.prog_exec(mem), 0x1122);
  219. }
  220. #[test]
  221. fn test_vm_be32() {
  222. let prog = &[
  223. 0x61, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  224. 0xdc, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  225. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  226. ];
  227. let mem = &mut [
  228. 0x11, 0x22, 0x33, 0x44
  229. ];
  230. let vm = rbpf::EbpfVmRaw::new(prog);
  231. assert_eq!(vm.prog_exec(mem), 0x11223344);
  232. }
  233. #[test]
  234. fn test_vm_be32_high() {
  235. let prog = &[
  236. 0x79, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  237. 0xdc, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  238. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  239. ];
  240. let mem = &mut [
  241. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88
  242. ];
  243. let vm = rbpf::EbpfVmRaw::new(prog);
  244. assert_eq!(vm.prog_exec(mem), 0x11223344);
  245. }
  246. #[test]
  247. fn test_vm_be64() {
  248. let prog = &[
  249. 0x79, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  250. 0xdc, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  251. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  252. ];
  253. let mem = &mut [
  254. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88
  255. ];
  256. let vm = rbpf::EbpfVmRaw::new(prog);
  257. assert_eq!(vm.prog_exec(mem), 0x1122334455667788);
  258. }
  259. #[test]
  260. fn test_vm_call() {
  261. let prog = &[
  262. 0xb7, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  263. 0xb7, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  264. 0xb7, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  265. 0xb7, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  266. 0xb7, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  267. 0x85, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  268. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  269. ];
  270. let mut vm = rbpf::EbpfVmNoData::new(prog);
  271. vm.register_helper(0, helpers::gather_bytes);
  272. assert_eq!(vm.prog_exec(), 0x0102030405);
  273. }
  274. #[test]
  275. fn test_vm_call_memfrob() {
  276. let prog = &[
  277. 0xbf, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  278. 0x07, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  279. 0xb7, 0x02, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  280. 0x85, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  281. 0x79, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  282. 0xdc, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  283. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  284. ];
  285. let mem = &mut [
  286. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  287. ];
  288. let mut vm = rbpf::EbpfVmRaw::new(prog);
  289. vm.register_helper(1, helpers::memfrob);
  290. assert_eq!(vm.prog_exec(mem), 0x102292e2f2c0708);
  291. }
  292. // TODO: helpers::trash_registers needs asm!().
  293. // Try this again once asm!() is available in stable.
  294. //#[test]
  295. //fn test_vm_call_save() {
  296. //let prog = &[
  297. //0xb7, 0x06, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  298. //0xb7, 0x07, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  299. //0xb7, 0x08, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
  300. //0xb7, 0x09, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00,
  301. //0x85, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  302. //0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  303. //0x4f, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  304. //0x4f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  305. //0x4f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  306. //0x4f, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  307. //0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  308. //];
  309. //let mut vm = rbpf::EbpfVmNoData::new(prog);
  310. //vm.register_helper(2, helpers::trash_registers);
  311. //assert_eq!(vm.prog_exec(), 0x4321);
  312. //}
  313. #[test]
  314. fn test_vm_div32_high_divisor() {
  315. let prog = &[
  316. 0xb7, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
  317. 0x18, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  318. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  319. 0x3c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  320. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  321. ];
  322. let vm = rbpf::EbpfVmNoData::new(prog);
  323. assert_eq!(vm.prog_exec(), 0x3);
  324. }
  325. #[test]
  326. fn test_vm_div32_imm() {
  327. let prog = &[
  328. 0x18, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
  329. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  330. 0x34, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  331. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  332. ];
  333. let vm = rbpf::EbpfVmNoData::new(prog);
  334. assert_eq!(vm.prog_exec(), 0x3);
  335. }
  336. #[test]
  337. fn test_vm_div32_reg() {
  338. let prog = &[
  339. 0x18, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
  340. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  341. 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  342. 0x3c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  343. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  344. ];
  345. let vm = rbpf::EbpfVmNoData::new(prog);
  346. assert_eq!(vm.prog_exec(), 0x3);
  347. }
  348. #[test]
  349. fn test_vm_div64_imm() {
  350. let prog = &[
  351. 0xb7, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
  352. 0x67, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  353. 0x37, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  354. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  355. ];
  356. let vm = rbpf::EbpfVmNoData::new(prog);
  357. assert_eq!(vm.prog_exec(), 0x300000000);
  358. }
  359. #[test]
  360. fn test_vm_div64_reg() {
  361. let prog = &[
  362. 0xb7, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
  363. 0x67, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  364. 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  365. 0x3f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  366. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  367. ];
  368. let vm = rbpf::EbpfVmNoData::new(prog);
  369. assert_eq!(vm.prog_exec(), 0x300000000);
  370. }
  371. #[test]
  372. fn test_vm_early_exit() {
  373. let prog = &[
  374. 0xb7, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  375. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  376. 0xb7, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  377. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  378. ];
  379. let vm = rbpf::EbpfVmNoData::new(prog);
  380. assert_eq!(vm.prog_exec(), 0x3);
  381. }
  382. // uBPF limits the number of user functions at 64. We don't.
  383. //#[test]
  384. //fn test_vm_err_call_bad_imm() {
  385. //}
  386. #[test]
  387. #[should_panic(expected = "Error: unknown helper function (id: 0x3f)")]
  388. fn test_vm_err_call_unreg() {
  389. let prog = &[
  390. 0xb7, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  391. 0xb7, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  392. 0xb7, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  393. 0xb7, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  394. 0xb7, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  395. 0x85, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00,
  396. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  397. ];
  398. let vm = rbpf::EbpfVmNoData::new(prog);
  399. vm.prog_exec();
  400. }
  401. #[test]
  402. #[should_panic(expected = "Error: division by 0")]
  403. fn test_vm_err_div64_by_zero_reg() {
  404. let prog = &[
  405. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  406. 0xb4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  407. 0x3f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  408. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  409. ];
  410. let vm = rbpf::EbpfVmNoData::new(prog);
  411. vm.prog_exec();
  412. }
  413. #[test]
  414. #[should_panic(expected = "Error: division by 0")]
  415. fn test_vm_err_div_by_zero_reg() {
  416. let prog = &[
  417. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  418. 0xb4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  419. 0x3c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  420. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  421. ];
  422. let vm = rbpf::EbpfVmNoData::new(prog);
  423. vm.prog_exec();
  424. }
  425. #[test]
  426. #[should_panic(expected = "Error: division by 0")]
  427. fn test_vm_err_mod64_by_zero_reg() {
  428. let prog = &[
  429. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  430. 0xb4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  431. 0x9f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  432. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  433. ];
  434. let vm = rbpf::EbpfVmNoData::new(prog);
  435. vm.prog_exec();
  436. }
  437. #[test]
  438. #[should_panic(expected = "Error: division by 0")]
  439. fn test_vm_err_mod_by_zero_reg() {
  440. let prog = &[
  441. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  442. 0xb4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  443. 0x9c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  444. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  445. ];
  446. let vm = rbpf::EbpfVmNoData::new(prog);
  447. vm.prog_exec();
  448. }
  449. #[test]
  450. #[should_panic(expected = "Error: out of bounds memory store (insn #1)")]
  451. fn test_vm_err_stack_out_of_bound() {
  452. let prog = &[
  453. 0x72, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  454. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  455. ];
  456. let vm = rbpf::EbpfVmNoData::new(prog);
  457. vm.prog_exec();
  458. }
  459. #[test]
  460. fn test_vm_exit() {
  461. let prog = &[
  462. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  463. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  464. ];
  465. let vm = rbpf::EbpfVmNoData::new(prog);
  466. assert_eq!(vm.prog_exec(), 0x0);
  467. }
  468. #[test]
  469. fn test_vm_ja() {
  470. let prog = &[
  471. 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  472. 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  473. 0xb7, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  474. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  475. ];
  476. let vm = rbpf::EbpfVmNoData::new(prog);
  477. assert_eq!(vm.prog_exec(), 0x1);
  478. }
  479. #[test]
  480. fn test_vm_jeq_imm() {
  481. let prog = &[
  482. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  483. 0xb4, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
  484. 0x15, 0x01, 0x04, 0x00, 0x0b, 0x00, 0x00, 0x00,
  485. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  486. 0xb4, 0x01, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
  487. 0x15, 0x01, 0x01, 0x00, 0x0b, 0x00, 0x00, 0x00,
  488. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  489. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  490. ];
  491. let vm = rbpf::EbpfVmNoData::new(prog);
  492. assert_eq!(vm.prog_exec(), 0x1);
  493. }
  494. #[test]
  495. fn test_vm_jeq_reg() {
  496. let prog = &[
  497. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  498. 0xb4, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
  499. 0xb4, 0x02, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
  500. 0x1d, 0x21, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  501. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  502. 0xb4, 0x01, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
  503. 0x1d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  504. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  505. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  506. ];
  507. let vm = rbpf::EbpfVmNoData::new(prog);
  508. assert_eq!(vm.prog_exec(), 0x1);
  509. }
  510. #[test]
  511. fn test_vm_jge_imm() {
  512. let prog = &[
  513. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  514. 0xb4, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
  515. 0x35, 0x01, 0x04, 0x00, 0x0b, 0x00, 0x00, 0x00,
  516. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  517. 0xb4, 0x01, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
  518. 0x35, 0x01, 0x01, 0x00, 0x0b, 0x00, 0x00, 0x00,
  519. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  520. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  521. ];
  522. let vm = rbpf::EbpfVmNoData::new(prog);
  523. assert_eq!(vm.prog_exec(), 0x1);
  524. }
  525. #[test]
  526. fn test_vm_jgt_imm() {
  527. let prog = &[
  528. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  529. 0xb4, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  530. 0x25, 0x01, 0x02, 0x00, 0x06, 0x00, 0x00, 0x00,
  531. 0x25, 0x01, 0x01, 0x00, 0x05, 0x00, 0x00, 0x00,
  532. 0x25, 0x01, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00,
  533. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  534. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  535. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  536. ];
  537. let vm = rbpf::EbpfVmNoData::new(prog);
  538. assert_eq!(vm.prog_exec(), 0x1);
  539. }
  540. #[test]
  541. fn test_vm_jgt_reg() {
  542. let prog = &[
  543. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  544. 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  545. 0xb7, 0x02, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
  546. 0xb7, 0x03, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  547. 0x2d, 0x21, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  548. 0x2d, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  549. 0x2d, 0x31, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  550. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  551. 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  552. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  553. ];
  554. let vm = rbpf::EbpfVmNoData::new(prog);
  555. assert_eq!(vm.prog_exec(), 0x1);
  556. }
  557. #[test]
  558. fn test_vm_jit_bounce() {
  559. let prog = &[
  560. 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  561. 0xbf, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  562. 0xbf, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  563. 0xbf, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  564. 0xbf, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  565. 0xbf, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  566. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  567. ];
  568. let vm = rbpf::EbpfVmNoData::new(prog);
  569. assert_eq!(vm.prog_exec(), 0x1);
  570. }
  571. #[test]
  572. fn test_vm_jne_reg() {
  573. let prog = &[
  574. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  575. 0xb4, 0x01, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
  576. 0xb4, 0x02, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
  577. 0x5d, 0x21, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  578. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  579. 0xb4, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
  580. 0x5d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  581. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  582. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  583. ];
  584. let vm = rbpf::EbpfVmNoData::new(prog);
  585. assert_eq!(vm.prog_exec(), 0x1);
  586. }
  587. #[test]
  588. fn test_vm_jset_imm() {
  589. let prog = &[
  590. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  591. 0xb4, 0x01, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  592. 0x45, 0x01, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00,
  593. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  594. 0xb4, 0x01, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
  595. 0x45, 0x01, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00,
  596. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  597. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  598. ];
  599. let vm = rbpf::EbpfVmNoData::new(prog);
  600. assert_eq!(vm.prog_exec(), 0x1);
  601. }
  602. #[test]
  603. fn test_vm_jset_reg() {
  604. let prog = &[
  605. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  606. 0xb4, 0x01, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
  607. 0xb4, 0x02, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  608. 0x4d, 0x21, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  609. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  610. 0xb4, 0x01, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
  611. 0x4d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  612. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  613. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  614. ];
  615. let vm = rbpf::EbpfVmNoData::new(prog);
  616. assert_eq!(vm.prog_exec(), 0x1);
  617. }
  618. #[test]
  619. fn test_vm_jsge_imm() {
  620. let prog = &[
  621. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  622. 0xb7, 0x01, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
  623. 0x75, 0x01, 0x05, 0x00, 0xff, 0xff, 0xff, 0xff,
  624. 0x75, 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  625. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  626. 0xb7, 0x01, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  627. 0x75, 0x01, 0x01, 0x00, 0xff, 0xff, 0xff, 0xff,
  628. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  629. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  630. ];
  631. let vm = rbpf::EbpfVmNoData::new(prog);
  632. assert_eq!(vm.prog_exec(), 0x1);
  633. }
  634. #[test]
  635. fn test_vm_jsge_reg() {
  636. let prog = &[
  637. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  638. 0xb7, 0x01, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
  639. 0xb7, 0x02, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  640. 0xb4, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  641. 0x7d, 0x21, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
  642. 0x7d, 0x31, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  643. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  644. 0xbf, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  645. 0x7d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  646. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  647. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  648. ];
  649. let vm = rbpf::EbpfVmNoData::new(prog);
  650. assert_eq!(vm.prog_exec(), 0x1);
  651. }
  652. #[test]
  653. fn test_vm_jsgt_imm() {
  654. let prog = &[
  655. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  656. 0xb7, 0x01, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
  657. 0x65, 0x01, 0x04, 0x00, 0xff, 0xff, 0xff, 0xff,
  658. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  659. 0xb4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  660. 0x65, 0x01, 0x01, 0x00, 0xff, 0xff, 0xff, 0xff,
  661. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  662. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  663. ];
  664. let vm = rbpf::EbpfVmNoData::new(prog);
  665. assert_eq!(vm.prog_exec(), 0x1);
  666. }
  667. #[test]
  668. fn test_vm_jsgt_reg() {
  669. let prog = &[
  670. 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  671. 0xb7, 0x01, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
  672. 0xb7, 0x02, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  673. 0x6d, 0x21, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  674. 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  675. 0xb4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  676. 0x6d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  677. 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  678. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  679. ];
  680. let vm = rbpf::EbpfVmNoData::new(prog);
  681. assert_eq!(vm.prog_exec(), 0x1);
  682. }
  683. #[test]
  684. fn test_vm_lddw() {
  685. let prog = assemble("lddw r0, 0x1122334455667788
  686. exit").unwrap();
  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 = &[
  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 = &[
  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 mem = &mut [
  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(mem), 0x9876543210);
  741. }
  742. #[test]
  743. fn test_vm_ldxb() {
  744. let prog = &[
  745. 0x71, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  746. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  747. ];
  748. let mem = &mut [
  749. 0xaa, 0xbb, 0x11, 0xcc, 0xdd
  750. ];
  751. let vm = rbpf::EbpfVmRaw::new(prog);
  752. assert_eq!(vm.prog_exec(mem), 0x11);
  753. }
  754. #[test]
  755. fn test_vm_ldxdw() {
  756. let prog = &[
  757. 0x79, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  758. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  759. ];
  760. let mem = &mut [
  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(mem), 0x8877665544332211);
  766. }
  767. #[test]
  768. fn test_vm_ldxh_all() {
  769. let prog = &[
  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 mem = &mut [
  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(mem), 0x9876543210);
  819. }
  820. #[test]
  821. fn test_vm_ldxh_all2() {
  822. let prog = &[
  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 mem = &mut [
  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(mem), 0x3ff);
  862. }
  863. #[test]
  864. fn test_vm_ldxh() {
  865. let prog = &[
  866. 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  867. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  868. ];
  869. let mem = &mut [
  870. 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
  871. ];
  872. let vm = rbpf::EbpfVmRaw::new(prog);
  873. assert_eq!(vm.prog_exec(mem), 0x2211);
  874. }
  875. #[test]
  876. fn test_vm_ldxh_same_reg() {
  877. let prog = &[
  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 mem = &mut [
  884. 0xff, 0xff
  885. ];
  886. let vm = rbpf::EbpfVmRaw::new(prog);
  887. assert_eq!(vm.prog_exec(mem), 0x1234);
  888. }
  889. #[test]
  890. fn test_vm_ldxw_all() {
  891. let prog = &[
  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 mem = &mut [
  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(mem), 0x030f0f);
  933. }
  934. #[test]
  935. fn test_vm_ldxw() {
  936. let prog = &[
  937. 0x61, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  938. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  939. ];
  940. let mem = &mut [
  941. 0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0xcc, 0xdd
  942. ];
  943. let vm = rbpf::EbpfVmRaw::new(prog);
  944. assert_eq!(vm.prog_exec(mem), 0x44332211);
  945. }
  946. #[test]
  947. fn test_vm_le16() {
  948. let prog = &[
  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 mem = &mut [
  954. 0x22, 0x11
  955. ];
  956. let vm = rbpf::EbpfVmRaw::new(prog);
  957. assert_eq!(vm.prog_exec(mem), 0x1122);
  958. }
  959. #[test]
  960. fn test_vm_le32() {
  961. let prog = &[
  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 mem = &mut [
  967. 0x44, 0x33, 0x22, 0x11
  968. ];
  969. let vm = rbpf::EbpfVmRaw::new(prog);
  970. assert_eq!(vm.prog_exec(mem), 0x11223344);
  971. }
  972. #[test]
  973. fn test_vm_le64() {
  974. let prog = &[
  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 mem = &mut [
  980. 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11
  981. ];
  982. let vm = rbpf::EbpfVmRaw::new(prog);
  983. assert_eq!(vm.prog_exec(mem), 0x1122334455667788);
  984. }
  985. #[test]
  986. fn test_vm_lsh_reg() {
  987. let prog = &[
  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 = &[
  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 = &[
  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 = &[
  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 = &[
  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 = &[
  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 = &[
  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 = &[
  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 = &[
  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 = &[
  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 = &[
  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 = &[
  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 = &[
  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 = &[
  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 = &[
  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 = &[
  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 = &[
  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 = &[
  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 = &[
  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 mem = &mut [
  1229. 0xaa, 0xbb, 0xff, 0xcc, 0xdd
  1230. ];
  1231. let vm = rbpf::EbpfVmRaw::new(prog);
  1232. assert_eq!(vm.prog_exec(mem), 0x11);
  1233. }
  1234. #[test]
  1235. fn test_vm_stdw() {
  1236. let prog = &[
  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 mem = &mut [
  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(mem), 0x44332211);
  1247. }
  1248. #[test]
  1249. fn test_vm_sth() {
  1250. let prog = &[
  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 mem = &mut [
  1256. 0xaa, 0xbb, 0xff, 0xff, 0xcc, 0xdd
  1257. ];
  1258. let vm = rbpf::EbpfVmRaw::new(prog);
  1259. assert_eq!(vm.prog_exec(mem), 0x2211);
  1260. }
  1261. #[test]
  1262. fn test_vm_string_stack() {
  1263. let prog = &[
  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 = &[
  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 mem = &mut [
  1305. 0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd
  1306. ];
  1307. let vm = rbpf::EbpfVmRaw::new(prog);
  1308. assert_eq!(vm.prog_exec(mem), 0x44332211);
  1309. }
  1310. #[test]
  1311. fn test_vm_stxb() {
  1312. let prog = &[
  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 mem = &mut [
  1319. 0xaa, 0xbb, 0xff, 0xcc, 0xdd
  1320. ];
  1321. let vm = rbpf::EbpfVmRaw::new(prog);
  1322. assert_eq!(vm.prog_exec(mem), 0x11);
  1323. }
  1324. #[test]
  1325. fn test_vm_stxb_all() {
  1326. let prog = &[
  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 mem = &mut [
  1348. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  1349. ];
  1350. let vm = rbpf::EbpfVmRaw::new(prog);
  1351. assert_eq!(vm.prog_exec(mem), 0xf0f2f3f4f5f6f7f8);
  1352. }
  1353. #[test]
  1354. fn test_vm_stxb_all2() {
  1355. let prog = &[
  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 mem = &mut [
  1366. 0xff, 0xff
  1367. ];
  1368. let vm = rbpf::EbpfVmRaw::new(prog);
  1369. assert_eq!(vm.prog_exec(mem), 0xf1f9);
  1370. }
  1371. #[test]
  1372. fn test_vm_stxb_chain() {
  1373. let prog = &[
  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 mem = &mut [
  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(mem), 0x2a);
  1402. }
  1403. #[test]
  1404. fn test_vm_stxdw() {
  1405. let prog = &[
  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 mem = &mut [
  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(mem), 0x8877665544332211);
  1419. }
  1420. #[test]
  1421. fn test_vm_stxh() {
  1422. let prog = &[
  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 mem = &mut [
  1429. 0xaa, 0xbb, 0xff, 0xff, 0xcc, 0xdd
  1430. ];
  1431. let vm = rbpf::EbpfVmRaw::new(prog);
  1432. assert_eq!(vm.prog_exec(mem), 0x2211);
  1433. }
  1434. #[test]
  1435. fn test_vm_stxw() {
  1436. let prog = &[
  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 mem = &mut [
  1443. 0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd
  1444. ];
  1445. let vm = rbpf::EbpfVmRaw::new(prog);
  1446. assert_eq!(vm.prog_exec(mem), 0x44332211);
  1447. }
  1448. #[test]
  1449. fn test_vm_subnet() {
  1450. let prog = &[
  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 mem = &mut [
  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(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 mem = &mut [
  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;
  1520. let vm = rbpf::EbpfVmRaw::new(prog);
  1521. assert_eq!(vm.prog_exec(mem), 0x1);
  1522. }
  1523. #[test]
  1524. fn test_vm_tcp_port80_nomatch() {
  1525. let mem = &mut [
  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;
  1541. let vm = rbpf::EbpfVmRaw::new(prog);
  1542. assert_eq!(vm.prog_exec(mem), 0x0);
  1543. }
  1544. #[test]
  1545. fn test_vm_tcp_port80_nomatch_ethertype() {
  1546. let mem = &mut [
  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;
  1562. let vm = rbpf::EbpfVmRaw::new(prog);
  1563. assert_eq!(vm.prog_exec(mem), 0x0);
  1564. }
  1565. #[test]
  1566. fn test_vm_tcp_port80_nomatch_proto() {
  1567. let mem = &mut [
  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;
  1583. let vm = rbpf::EbpfVmRaw::new(prog);
  1584. assert_eq!(vm.prog_exec(mem), 0x0);
  1585. }
  1586. #[test]
  1587. fn test_vm_tcp_sack_match() {
  1588. let mut mem = TCP_SACK_MATCH.to_vec();
  1589. let prog = assemble(TCP_SACK_ASM).unwrap();
  1590. let vm = rbpf::EbpfVmRaw::new(&prog);
  1591. assert_eq!(vm.prog_exec(mem.as_mut_slice()), 0x1);
  1592. }
  1593. #[test]
  1594. fn test_vm_tcp_sack_nomatch() {
  1595. let mut mem = TCP_SACK_NOMATCH.to_vec();
  1596. let prog = assemble(TCP_SACK_ASM).unwrap();
  1597. let vm = rbpf::EbpfVmRaw::new(&prog);
  1598. assert_eq!(vm.prog_exec(mem.as_mut_slice()), 0x0);
  1599. }