12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703 |
- // Converted from the tests for uBPF <https://github.com/iovisor/ubpf>
- // Copyright 2015 Big Switch Networks, Inc
- // Copyright 2016 6WIND S.A. <quentin.monnet@6wind.com>
- //
- // Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or
- // the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be
- // copied, modified, or distributed except according to those terms.
- // The tests contained in this file are extracted from the unit tests of uBPF software. Each test
- // in this file has a name in the form `test_vm_<name>`, and corresponds to the (human-readable)
- // code in `ubpf/tree/master/tests/<name>`, available at
- // <https://github.com/iovisor/ubpf/tree/master/tests> (hyphen had to be replaced with underscores
- // as Rust will not accept them in function names). It is strongly advised to refer to the uBPF
- // version to understand what these program do.
- //
- // Each program was assembled from the uBPF version with the assembler provided by uBPF itself, and
- // available at <https://github.com/iovisor/ubpf/tree/master/ubpf>.
- // The very few modifications that have been realized should be indicated.
- // These are unit tests for the eBPF interpreter.
- extern crate rbpf;
- mod common;
- use rbpf::helpers;
- use rbpf::assembler::assemble;
- use common::{TCP_SACK_ASM, TCP_SACK_MATCH, TCP_SACK_NOMATCH};
- #[test]
- fn test_vm_add() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x0c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x3);
- }
- #[test]
- fn test_vm_alu64_arith() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb7, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0xb7, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0xb7, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0xb7, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
- 0xb7, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
- 0xb7, 0x07, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
- 0xb7, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
- 0xb7, 0x09, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
- 0x07, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
- 0x0f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x17, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00,
- 0x1f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x27, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
- 0x2f, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x37, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x3f, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x2a);
- }
- #[test]
- fn test_vm_alu64_bit() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb7, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0xb7, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0xb7, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0xb7, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
- 0xb7, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
- 0xb7, 0x07, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
- 0xb7, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
- 0x4f, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x47, 0x00, 0x00, 0x00, 0xa0, 0x00, 0x00, 0x00,
- 0x57, 0x00, 0x00, 0x00, 0xa3, 0x00, 0x00, 0x00,
- 0xb7, 0x09, 0x00, 0x00, 0x91, 0x00, 0x00, 0x00,
- 0x5f, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x67, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x67, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00,
- 0x6f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x77, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x77, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
- 0x7f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xa7, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0xaf, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x11);
- }
- #[test]
- fn test_vm_alu_arith() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb4, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0xb4, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0xb4, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0xb4, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
- 0xb4, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
- 0xb4, 0x07, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
- 0xb4, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
- 0xb4, 0x09, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
- 0x04, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
- 0x0c, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x14, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00,
- 0x1c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x24, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
- 0x2c, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x34, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x3c, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x2a);
- }
- #[test]
- fn test_vm_alu_bit() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb4, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0xb4, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0xb4, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0xb4, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
- 0xb4, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
- 0xb4, 0x07, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
- 0xb4, 0x08, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
- 0x4c, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x44, 0x00, 0x00, 0x00, 0xa0, 0x00, 0x00, 0x00,
- 0x54, 0x00, 0x00, 0x00, 0xa3, 0x00, 0x00, 0x00,
- 0xb4, 0x09, 0x00, 0x00, 0x91, 0x00, 0x00, 0x00,
- 0x5c, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x64, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00,
- 0x6c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x74, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
- 0x7c, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xa4, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0xac, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x11);
- }
- #[test]
- fn test_vm_arsh32_high_shift() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
- 0x18, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xcc, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x4);
- }
- #[test]
- fn test_vm_arsh() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00,
- 0x64, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
- 0xc4, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0xffff8000);
- }
- #[test]
- fn test_vm_arsh64() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x67, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00,
- 0xc7, 0x00, 0x00, 0x00, 0x37, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
- 0xcf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0xfffffffffffffff8);
- }
- #[test]
- fn test_vm_arsh_reg() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x64, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
- 0xcc, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0xffff8000);
- }
- #[test]
- fn test_vm_be16() {
- let prog = &[
- 0x69, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0x11, 0x22
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x1122);
- }
- #[test]
- fn test_vm_be16_high() {
- let prog = &[
- 0x79, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x1122);
- }
- #[test]
- fn test_vm_be32() {
- let prog = &[
- 0x61, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0x11, 0x22, 0x33, 0x44
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x11223344);
- }
- #[test]
- fn test_vm_be32_high() {
- let prog = &[
- 0x79, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x11223344);
- }
- #[test]
- fn test_vm_be64() {
- let prog = &[
- 0x79, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x1122334455667788);
- }
- #[test]
- fn test_vm_call() {
- let prog = &[
- 0xb7, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb7, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0xb7, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0xb7, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0xb7, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
- 0x85, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mut vm = rbpf::EbpfVmNoData::new(prog);
- vm.register_helper(0, helpers::gather_bytes);
- assert_eq!(vm.prog_exec(), 0x0102030405);
- }
- #[test]
- fn test_vm_call_memfrob() {
- let prog = &[
- 0xbf, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x07, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0xb7, 0x02, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x85, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x79, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
- ];
- let mut vm = rbpf::EbpfVmRaw::new(prog);
- vm.register_helper(1, helpers::memfrob);
- assert_eq!(vm.prog_exec(mem), 0x102292e2f2c0708);
- }
- // TODO: helpers::trash_registers needs asm!().
- // Try this again once asm!() is available in stable.
- //#[test]
- //fn test_vm_call_save() {
- //let prog = &[
- //0xb7, 0x06, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- //0xb7, 0x07, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- //0xb7, 0x08, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
- //0xb7, 0x09, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00,
- //0x85, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- //0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- //0x4f, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- //0x4f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- //0x4f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- //0x4f, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- //0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- //];
- //let mut vm = rbpf::EbpfVmNoData::new(prog);
- //vm.register_helper(2, helpers::trash_registers);
- //assert_eq!(vm.prog_exec(), 0x4321);
- //}
- #[test]
- fn test_vm_div32_high_divisor() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
- 0x18, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x3c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x3);
- }
- #[test]
- fn test_vm_div32_imm() {
- let prog = &[
- 0x18, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x34, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x3);
- }
- #[test]
- fn test_vm_div32_reg() {
- let prog = &[
- 0x18, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x3c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x3);
- }
- #[test]
- fn test_vm_div64_imm() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
- 0x67, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x37, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x300000000);
- }
- #[test]
- fn test_vm_div64_reg() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
- 0x67, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x3f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x300000000);
- }
- #[test]
- fn test_vm_early_exit() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x3);
- }
- // uBPF limits the number of user functions at 64. We don't.
- //#[test]
- //fn test_vm_err_call_bad_imm() {
- //}
- #[test]
- #[should_panic(expected = "Error: unknown helper function (id: 0x3f)")]
- fn test_vm_err_call_unreg() {
- let prog = &[
- 0xb7, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb7, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0xb7, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0xb7, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0xb7, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
- 0x85, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- vm.prog_exec();
- }
- #[test]
- #[should_panic(expected = "Error: division by 0")]
- fn test_vm_err_div64_by_zero_reg() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x3f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- vm.prog_exec();
- }
- #[test]
- #[should_panic(expected = "Error: division by 0")]
- fn test_vm_err_div_by_zero_reg() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x3c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- vm.prog_exec();
- }
- #[test]
- #[should_panic(expected = "Error: division by 0")]
- fn test_vm_err_mod64_by_zero_reg() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x9f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- vm.prog_exec();
- }
- #[test]
- #[should_panic(expected = "Error: division by 0")]
- fn test_vm_err_mod_by_zero_reg() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x9c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- vm.prog_exec();
- }
- #[test]
- #[should_panic(expected = "Error: out of bounds memory store (insn #1)")]
- fn test_vm_err_stack_out_of_bound() {
- let prog = &[
- 0x72, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- vm.prog_exec();
- }
- #[test]
- fn test_vm_exit() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x0);
- }
- #[test]
- fn test_vm_ja() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x1);
- }
- #[test]
- fn test_vm_jeq_imm() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
- 0x15, 0x01, 0x04, 0x00, 0x0b, 0x00, 0x00, 0x00,
- 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
- 0x15, 0x01, 0x01, 0x00, 0x0b, 0x00, 0x00, 0x00,
- 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x1);
- }
- #[test]
- fn test_vm_jeq_reg() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
- 0xb4, 0x02, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
- 0x1d, 0x21, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
- 0x1d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x1);
- }
- #[test]
- fn test_vm_jge_imm() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
- 0x35, 0x01, 0x04, 0x00, 0x0b, 0x00, 0x00, 0x00,
- 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
- 0x35, 0x01, 0x01, 0x00, 0x0b, 0x00, 0x00, 0x00,
- 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x1);
- }
- #[test]
- fn test_vm_jgt_imm() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
- 0x25, 0x01, 0x02, 0x00, 0x06, 0x00, 0x00, 0x00,
- 0x25, 0x01, 0x01, 0x00, 0x05, 0x00, 0x00, 0x00,
- 0x25, 0x01, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x1);
- }
- #[test]
- fn test_vm_jgt_reg() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
- 0xb7, 0x02, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
- 0xb7, 0x03, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x2d, 0x21, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x2d, 0x11, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x2d, 0x31, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x1);
- }
- #[test]
- fn test_vm_jit_bounce() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xbf, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xbf, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xbf, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xbf, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xbf, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x1);
- }
- #[test]
- fn test_vm_jne_reg() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
- 0xb4, 0x02, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
- 0x5d, 0x21, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
- 0x5d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x1);
- }
- #[test]
- fn test_vm_jset_imm() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
- 0x45, 0x01, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00,
- 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
- 0x45, 0x01, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00,
- 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x1);
- }
- #[test]
- fn test_vm_jset_reg() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
- 0xb4, 0x02, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
- 0x4d, 0x21, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
- 0x4d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x1);
- }
- #[test]
- fn test_vm_jsge_imm() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x01, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
- 0x75, 0x01, 0x05, 0x00, 0xff, 0xff, 0xff, 0xff,
- 0x75, 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb7, 0x01, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
- 0x75, 0x01, 0x01, 0x00, 0xff, 0xff, 0xff, 0xff,
- 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x1);
- }
- #[test]
- fn test_vm_jsge_reg() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x01, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
- 0xb7, 0x02, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
- 0xb4, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x7d, 0x21, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x7d, 0x31, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xbf, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x7d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x1);
- }
- #[test]
- fn test_vm_jsgt_imm() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x01, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
- 0x65, 0x01, 0x04, 0x00, 0xff, 0xff, 0xff, 0xff,
- 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x65, 0x01, 0x01, 0x00, 0xff, 0xff, 0xff, 0xff,
- 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x1);
- }
- #[test]
- fn test_vm_jsgt_reg() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x01, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
- 0xb7, 0x02, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
- 0x6d, 0x21, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x6d, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x1);
- }
- #[test]
- fn test_vm_lddw() {
- let prog = assemble("lddw r0, 0x1122334455667788
- exit").unwrap();
- let vm = rbpf::EbpfVmNoData::new(&prog);
- assert_eq!(vm.prog_exec(), 0x1122334455667788);
- }
- #[test]
- fn test_vm_lddw2() {
- let prog = &[
- 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x80000000);
- }
- #[test]
- fn test_vm_ldxb_all() {
- let prog = &[
- 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x71, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x67, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x71, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x67, 0x08, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x71, 0x07, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x67, 0x07, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
- 0x71, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x67, 0x06, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
- 0x71, 0x05, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x67, 0x05, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x71, 0x04, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x67, 0x04, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00,
- 0x71, 0x03, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x67, 0x03, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
- 0x71, 0x02, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x67, 0x02, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
- 0x71, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x67, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x71, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x67, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
- 0x4f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
- 0x08, 0x09
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x9876543210);
- }
- #[test]
- fn test_vm_ldxb() {
- let prog = &[
- 0x71, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0xaa, 0xbb, 0x11, 0xcc, 0xdd
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x11);
- }
- #[test]
- fn test_vm_ldxdw() {
- let prog = &[
- 0x79, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
- 0x77, 0x88, 0xcc, 0xdd
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x8877665544332211);
- }
- #[test]
- fn test_vm_ldxh_all() {
- let prog = &[
- 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x69, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x09, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x67, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x69, 0x08, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x08, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x67, 0x08, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x69, 0x07, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x07, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x67, 0x07, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
- 0x69, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x06, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x67, 0x06, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
- 0x69, 0x05, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x05, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x67, 0x05, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x69, 0x04, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x04, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x67, 0x04, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00,
- 0x69, 0x03, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x03, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x67, 0x03, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
- 0x69, 0x02, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x02, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x67, 0x02, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
- 0x69, 0x01, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x67, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x69, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x67, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
- 0x4f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03,
- 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07,
- 0x00, 0x08, 0x00, 0x09
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x9876543210);
- }
- #[test]
- fn test_vm_ldxh_all2() {
- let prog = &[
- 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x69, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x09, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x69, 0x08, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x08, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x69, 0x07, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x07, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x69, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x06, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x69, 0x05, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x05, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x69, 0x04, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x04, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x69, 0x03, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x03, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x69, 0x02, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x02, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x69, 0x01, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x69, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x4f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0x00, 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08,
- 0x00, 0x10, 0x00, 0x20, 0x00, 0x40, 0x00, 0x80,
- 0x01, 0x00, 0x02, 0x00
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x3ff);
- }
- #[test]
- fn test_vm_ldxh() {
- let prog = &[
- 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x2211);
- }
- #[test]
- fn test_vm_ldxh_same_reg() {
- let prog = &[
- 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x6a, 0x00, 0x00, 0x00, 0x34, 0x12, 0x00, 0x00,
- 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0xff, 0xff
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x1234);
- }
- #[test]
- fn test_vm_ldxw_all() {
- let prog = &[
- 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x61, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x09, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x61, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x08, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x61, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x07, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x61, 0x06, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x06, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x61, 0x05, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x05, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x61, 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x04, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x61, 0x03, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x03, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x61, 0x02, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x02, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x61, 0x01, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x61, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x4f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x4f, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
- 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x08,
- 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00,
- 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x08, 0x00,
- 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x030f0f);
- }
- #[test]
- fn test_vm_ldxw() {
- let prog = &[
- 0x61, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0xcc, 0xdd
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x44332211);
- }
- #[test]
- fn test_vm_le16() {
- let prog = &[
- 0x69, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xd4, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0x22, 0x11
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x1122);
- }
- #[test]
- fn test_vm_le32() {
- let prog = &[
- 0x61, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xd4, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0x44, 0x33, 0x22, 0x11
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x11223344);
- }
- #[test]
- fn test_vm_le64() {
- let prog = &[
- 0x79, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xd4, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x1122334455667788);
- }
- #[test]
- fn test_vm_lsh_reg() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb7, 0x07, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x6f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x10);
- }
- #[test]
- fn test_vm_mod() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x74, 0x16, 0x00, 0x00,
- 0x94, 0x00, 0x00, 0x00, 0x5c, 0x00, 0x00, 0x00,
- 0xb4, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00,
- 0x9c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x5);
- }
- #[test]
- fn test_vm_mod32() {
- let prog = &[
- 0x18, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x94, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x0);
- }
- #[test]
- fn test_vm_mod64() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x36, 0x84, 0x85, 0xb1,
- 0x67, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x47, 0x00, 0x00, 0x00, 0xc8, 0xc5, 0x0d, 0x10,
- 0xb4, 0x01, 0x00, 0x00, 0x3e, 0x26, 0xde, 0x0d,
- 0x67, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x47, 0x01, 0x00, 0x00, 0xf3, 0xf7, 0xbe, 0x3c,
- 0x9f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x97, 0x00, 0x00, 0x00, 0x78, 0x17, 0x8f, 0x65,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x30ba5a04);
- }
- #[test]
- fn test_vm_mov() {
- let prog = &[
- 0xb4, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xbc, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x1);
- }
- #[test]
- fn test_vm_mul32_imm() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0x24, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0xc);
- }
- #[test]
- fn test_vm_mul32_reg() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x2c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0xc);
- }
- #[test]
- fn test_vm_mul32_reg_overflow() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x40,
- 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x2c, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x4);
- }
- #[test]
- fn test_vm_mul64_imm() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x40,
- 0x27, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x100000004);
- }
- #[test]
- fn test_vm_mul64_reg() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x40,
- 0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x2f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x100000004);
- }
- #[test]
- fn test_vm_mul_loop() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
- 0x07, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
- 0x67, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x77, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x15, 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
- 0x27, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
- 0x07, 0x01, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
- 0x55, 0x01, 0xfd, 0xff, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x75db9c97);
- }
- #[test]
- fn test_vm_neg64() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0xfffffffffffffffe);
- }
- #[test]
- fn test_vm_neg() {
- let prog = &[
- 0xb4, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0xfffffffe);
- }
- #[test]
- fn test_vm_prime() {
- let prog = &[
- 0xb7, 0x01, 0x00, 0x00, 0x43, 0x00, 0x00, 0x00,
- 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb7, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x25, 0x01, 0x04, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x05, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x07, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x3d, 0x12, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xbf, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x3f, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x2f, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xbf, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x1f, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x55, 0x04, 0xf6, 0xff, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x1);
- }
- #[test]
- fn test_vm_rhs32() {
- let prog = &[
- 0xaf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x17, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x74, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x00ffffff);
- }
- #[test]
- fn test_vm_rsh_reg() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0xb7, 0x07, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x7f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0x1);
- }
- #[test]
- fn test_vm_stack() {
- let prog = &[
- 0xb7, 0x01, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
- 0x7a, 0x0a, 0xf0, 0xff, 0xab, 0x00, 0x00, 0x00,
- 0x7a, 0x0a, 0xf8, 0xff, 0xcd, 0x00, 0x00, 0x00,
- 0x57, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x67, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0xbf, 0xa2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x0f, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x79, 0x20, 0xf0, 0xff, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmNoData::new(prog);
- assert_eq!(vm.prog_exec(), 0xcd);
- }
- #[test]
- fn test_vm_stack2() {
- let prog = &[
- 0x72, 0x0a, 0xfc, 0xff, 0x01, 0x00, 0x00, 0x00,
- 0x72, 0x0a, 0xfd, 0xff, 0x02, 0x00, 0x00, 0x00,
- 0x72, 0x0a, 0xfe, 0xff, 0x03, 0x00, 0x00, 0x00,
- 0x72, 0x0a, 0xff, 0xff, 0x04, 0x00, 0x00, 0x00,
- 0xbf, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x02, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x1f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x85, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0xb7, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x71, 0xa2, 0xfc, 0xff, 0x00, 0x00, 0x00, 0x00,
- 0x71, 0xa3, 0xfd, 0xff, 0x00, 0x00, 0x00, 0x00,
- 0x71, 0xa4, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00,
- 0x71, 0xa5, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
- 0x85, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xa7, 0x00, 0x00, 0x00, 0x2a, 0x2a, 0x2a, 0x2a,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mut vm = rbpf::EbpfVmNoData::new(prog);
- vm.register_helper(0, helpers::gather_bytes);
- vm.register_helper(1, helpers::memfrob);
- assert_eq!(vm.prog_exec(), 0x01020304);
- }
- #[test]
- fn test_vm_stb() {
- let prog = &[
- 0x72, 0x01, 0x02, 0x00, 0x11, 0x00, 0x00, 0x00,
- 0x71, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0xaa, 0xbb, 0xff, 0xcc, 0xdd
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x11);
- }
- #[test]
- fn test_vm_stdw() {
- let prog = &[
- 0x7a, 0x01, 0x02, 0x00, 0x11, 0x22, 0x33, 0x44,
- 0x79, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xcc, 0xdd
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x44332211);
- }
- #[test]
- fn test_vm_sth() {
- let prog = &[
- 0x6a, 0x01, 0x02, 0x00, 0x11, 0x22, 0x00, 0x00,
- 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0xaa, 0xbb, 0xff, 0xff, 0xcc, 0xdd
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x2211);
- }
- #[test]
- fn test_vm_string_stack() {
- let prog = &[
- 0xb7, 0x01, 0x00, 0x00, 0x61, 0x62, 0x63, 0x78,
- 0x63, 0x1a, 0xf8, 0xff, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x73, 0x6a, 0xfc, 0xff, 0x00, 0x00, 0x00, 0x00,
- 0x73, 0x6a, 0xf4, 0xff, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x01, 0x00, 0x00, 0x61, 0x62, 0x63, 0x79,
- 0x63, 0x1a, 0xf0, 0xff, 0x00, 0x00, 0x00, 0x00,
- 0xbf, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x07, 0x01, 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff,
- 0xbf, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x85, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0xbf, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x67, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x77, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x55, 0x01, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xbf, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x07, 0x01, 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff,
- 0xbf, 0xa2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x07, 0x02, 0x00, 0x00, 0xf0, 0xff, 0xff, 0xff,
- 0x85, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0xbf, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x67, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x77, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x1d, 0x61, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mut vm = rbpf::EbpfVmNoData::new(prog);
- vm.register_helper(4, helpers::strcmp);
- assert_eq!(vm.prog_exec(), 0x0);
- }
- #[test]
- fn test_vm_stw() {
- let prog = &[
- 0x62, 0x01, 0x02, 0x00, 0x11, 0x22, 0x33, 0x44,
- 0x61, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x44332211);
- }
- #[test]
- fn test_vm_stxb() {
- let prog = &[
- 0xb4, 0x02, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00,
- 0x73, 0x21, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x71, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0xaa, 0xbb, 0xff, 0xcc, 0xdd
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x11);
- }
- #[test]
- fn test_vm_stxb_all() {
- let prog = &[
- 0xb7, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00,
- 0xb7, 0x02, 0x00, 0x00, 0xf2, 0x00, 0x00, 0x00,
- 0xb7, 0x03, 0x00, 0x00, 0xf3, 0x00, 0x00, 0x00,
- 0xb7, 0x04, 0x00, 0x00, 0xf4, 0x00, 0x00, 0x00,
- 0xb7, 0x05, 0x00, 0x00, 0xf5, 0x00, 0x00, 0x00,
- 0xb7, 0x06, 0x00, 0x00, 0xf6, 0x00, 0x00, 0x00,
- 0xb7, 0x07, 0x00, 0x00, 0xf7, 0x00, 0x00, 0x00,
- 0xb7, 0x08, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00,
- 0x73, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x73, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x73, 0x31, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x73, 0x41, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x73, 0x51, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x73, 0x61, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x73, 0x71, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x73, 0x81, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x79, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0xf0f2f3f4f5f6f7f8);
- }
- #[test]
- fn test_vm_stxb_all2() {
- let prog = &[
- 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x01, 0x00, 0x00, 0xf1, 0x00, 0x00, 0x00,
- 0xb7, 0x09, 0x00, 0x00, 0xf9, 0x00, 0x00, 0x00,
- 0x73, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x73, 0x90, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0xff, 0xff
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0xf1f9);
- }
- #[test]
- fn test_vm_stxb_chain() {
- let prog = &[
- 0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x71, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x73, 0x90, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x71, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x73, 0x80, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x71, 0x07, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x73, 0x70, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x71, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x73, 0x60, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x71, 0x05, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x73, 0x50, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x71, 0x04, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x73, 0x40, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x71, 0x03, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x73, 0x30, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x71, 0x02, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x73, 0x20, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x71, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x73, 0x10, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x71, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x2a);
- }
- #[test]
- fn test_vm_stxdw() {
- let prog = &[
- 0xb7, 0x02, 0x00, 0x00, 0x55, 0x66, 0x77, 0x88,
- 0x67, 0x02, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
- 0x47, 0x02, 0x00, 0x00, 0x11, 0x22, 0x33, 0x44,
- 0x7b, 0x21, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x79, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xcc, 0xdd
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x8877665544332211);
- }
- #[test]
- fn test_vm_stxh() {
- let prog = &[
- 0xb4, 0x02, 0x00, 0x00, 0x11, 0x22, 0x00, 0x00,
- 0x6b, 0x21, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x69, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0xaa, 0xbb, 0xff, 0xff, 0xcc, 0xdd
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x2211);
- }
- #[test]
- fn test_vm_stxw() {
- let prog = &[
- 0xb4, 0x02, 0x00, 0x00, 0x11, 0x22, 0x33, 0x44,
- 0x63, 0x21, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x61, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x44332211);
- }
- #[test]
- fn test_vm_subnet() {
- let prog = &[
- 0xb7, 0x02, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00,
- 0x69, 0x13, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x55, 0x03, 0x02, 0x00, 0x81, 0x00, 0x00, 0x00,
- 0xb7, 0x02, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00,
- 0x69, 0x13, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x57, 0x03, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
- 0x55, 0x03, 0x05, 0x00, 0x08, 0x00, 0x00, 0x00,
- 0x0f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x61, 0x11, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x57, 0x01, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00,
- 0x15, 0x01, 0x01, 0x00, 0xc0, 0xa8, 0x01, 0x00,
- 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- let mem = &mut [
- 0x00, 0x00, 0xc0, 0x9f, 0xa0, 0x97, 0x00, 0xa0,
- 0xcc, 0x3b, 0xbf, 0xfa, 0x08, 0x00, 0x45, 0x10,
- 0x00, 0x3c, 0x46, 0x3c, 0x40, 0x00, 0x40, 0x06,
- 0x73, 0x1c, 0xc0, 0xa8, 0x01, 0x02, 0xc0, 0xa8,
- 0x01, 0x01, 0x06, 0x0e, 0x00, 0x17, 0x99, 0xc5,
- 0xa0, 0xec, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x02,
- 0x7d, 0x78, 0xe0, 0xa3, 0x00, 0x00, 0x02, 0x04,
- 0x05, 0xb4, 0x04, 0x02, 0x08, 0x0a, 0x00, 0x9c,
- 0x27, 0x24, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03,
- 0x03, 0x00
- ];
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x1);
- }
- const PROG_TCP_PORT_80: [u8;152] = [
- 0x71, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x71, 0x13, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x67, 0x03, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
- 0x4f, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x55, 0x03, 0x0c, 0x00, 0x08, 0x00, 0x00, 0x00,
- 0x71, 0x12, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x55, 0x02, 0x0a, 0x00, 0x06, 0x00, 0x00, 0x00,
- 0x71, 0x12, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x07, 0x01, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00,
- 0x57, 0x02, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00,
- 0x67, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x0f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x69, 0x12, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x15, 0x02, 0x02, 0x00, 0x00, 0x50, 0x00, 0x00,
- 0x69, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x55, 0x01, 0x01, 0x00, 0x00, 0x50, 0x00, 0x00,
- 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- ];
- #[test]
- fn test_vm_tcp_port80_match() {
- let mem = &mut [
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06,
- 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45, 0x00,
- 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06,
- 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01, 0xc0, 0xa8,
- 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02,
- 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44
- ];
- let prog = &PROG_TCP_PORT_80;
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x1);
- }
- #[test]
- fn test_vm_tcp_port80_nomatch() {
- let mem = &mut [
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06,
- 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45, 0x00,
- 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06,
- 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01, 0xc0, 0xa8,
- 0x00, 0x02, 0x00, 0x16, 0x27, 0x10, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x51, 0x02,
- 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44
- ];
- let prog = &PROG_TCP_PORT_80;
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x0);
- }
- #[test]
- fn test_vm_tcp_port80_nomatch_ethertype() {
- let mem = &mut [
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06,
- 0x07, 0x08, 0x09, 0x0a, 0x08, 0x01, 0x45, 0x00,
- 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06,
- 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01, 0xc0, 0xa8,
- 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02,
- 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44
- ];
- let prog = &PROG_TCP_PORT_80;
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x0);
- }
- #[test]
- fn test_vm_tcp_port80_nomatch_proto() {
- let mem = &mut [
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06,
- 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45, 0x00,
- 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11,
- 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01, 0xc0, 0xa8,
- 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x02,
- 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
- 0x44, 0x44, 0x44, 0x44
- ];
- let prog = &PROG_TCP_PORT_80;
- let vm = rbpf::EbpfVmRaw::new(prog);
- assert_eq!(vm.prog_exec(mem), 0x0);
- }
- #[test]
- fn test_vm_tcp_sack_match() {
- let mut mem = TCP_SACK_MATCH.to_vec();
- let prog = assemble(TCP_SACK_ASM).unwrap();
- let vm = rbpf::EbpfVmRaw::new(&prog);
- assert_eq!(vm.prog_exec(mem.as_mut_slice()), 0x1);
- }
- #[test]
- fn test_vm_tcp_sack_nomatch() {
- let mut mem = TCP_SACK_NOMATCH.to_vec();
- let prog = assemble(TCP_SACK_ASM).unwrap();
- let vm = rbpf::EbpfVmRaw::new(&prog);
- assert_eq!(vm.prog_exec(mem.as_mut_slice()), 0x0);
- }
|