ubpf_vm.rs 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674
  1. // SPDX-License-Identifier: (Apache-2.0 OR MIT)
  2. // Converted from the tests for uBPF <https://github.com/iovisor/ubpf>
  3. // Copyright 2015 Big Switch Networks, Inc
  4. // Copyright 2016 6WIND S.A. <quentin.monnet@6wind.com>
  5. // The tests contained in this file are extracted from the unit tests of uBPF software. Each test
  6. // in this file has a name in the form `test_vm_<name>`, and corresponds to the (human-readable)
  7. // code in `ubpf/tree/master/tests/<name>`, available at
  8. // <https://github.com/iovisor/ubpf/tree/master/tests> (hyphen had to be replaced with underscores
  9. // as Rust will not accept them in function names). It is strongly advised to refer to the uBPF
  10. // version to understand what these program do.
  11. //
  12. // Each program was assembled from the uBPF version with the assembler provided by uBPF itself, and
  13. // available at <https://github.com/iovisor/ubpf/tree/master/ubpf>.
  14. // The very few modifications that have been realized should be indicated.
  15. // These are unit tests for the eBPF interpreter.
  16. #![allow(clippy::unreadable_literal)]
  17. extern crate rbpf;
  18. mod common;
  19. use common::{TCP_SACK_ASM, TCP_SACK_MATCH, TCP_SACK_NOMATCH};
  20. use rbpf::{assembler::assemble, helpers};
  21. #[test]
  22. fn test_vm_add() {
  23. let prog = assemble(
  24. "
  25. mov32 r0, 0
  26. mov32 r1, 2
  27. add32 r0, 1
  28. add32 r0, r1
  29. exit",
  30. )
  31. .unwrap();
  32. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  33. assert_eq!(vm.execute_program().unwrap(), 0x3);
  34. }
  35. #[test]
  36. fn test_vm_alu64_arith() {
  37. let prog = assemble(
  38. "
  39. mov r0, 0
  40. mov r1, 1
  41. mov r2, 2
  42. mov r3, 3
  43. mov r4, 4
  44. mov r5, 5
  45. mov r6, 6
  46. mov r7, 7
  47. mov r8, 8
  48. mov r9, 9
  49. add r0, 23
  50. add r0, r7
  51. sub r0, 13
  52. sub r0, r1
  53. mul r0, 7
  54. mul r0, r3
  55. div r0, 2
  56. div r0, r4
  57. exit",
  58. )
  59. .unwrap();
  60. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  61. assert_eq!(vm.execute_program().unwrap(), 0x2a);
  62. }
  63. #[test]
  64. fn test_vm_alu64_bit() {
  65. let prog = assemble(
  66. "
  67. mov r0, 0
  68. mov r1, 1
  69. mov r2, 2
  70. mov r3, 3
  71. mov r4, 4
  72. mov r5, 5
  73. mov r6, 6
  74. mov r7, 7
  75. mov r8, 8
  76. or r0, r5
  77. or r0, 0xa0
  78. and r0, 0xa3
  79. mov r9, 0x91
  80. and r0, r9
  81. lsh r0, 32
  82. lsh r0, 22
  83. lsh r0, r8
  84. rsh r0, 32
  85. rsh r0, 19
  86. rsh r0, r7
  87. xor r0, 0x03
  88. xor r0, r2
  89. exit",
  90. )
  91. .unwrap();
  92. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  93. assert_eq!(vm.execute_program().unwrap(), 0x11);
  94. }
  95. #[test]
  96. fn test_vm_alu_arith() {
  97. let prog = assemble(
  98. "
  99. mov32 r0, 0
  100. mov32 r1, 1
  101. mov32 r2, 2
  102. mov32 r3, 3
  103. mov32 r4, 4
  104. mov32 r5, 5
  105. mov32 r6, 6
  106. mov32 r7, 7
  107. mov32 r8, 8
  108. mov32 r9, 9
  109. add32 r0, 23
  110. add32 r0, r7
  111. sub32 r0, 13
  112. sub32 r0, r1
  113. mul32 r0, 7
  114. mul32 r0, r3
  115. div32 r0, 2
  116. div32 r0, r4
  117. exit",
  118. )
  119. .unwrap();
  120. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  121. assert_eq!(vm.execute_program().unwrap(), 0x2a);
  122. }
  123. #[test]
  124. fn test_vm_alu_bit() {
  125. let prog = assemble(
  126. "
  127. mov32 r0, 0
  128. mov32 r1, 1
  129. mov32 r2, 2
  130. mov32 r3, 3
  131. mov32 r4, 4
  132. mov32 r5, 5
  133. mov32 r6, 6
  134. mov32 r7, 7
  135. mov32 r8, 8
  136. or32 r0, r5
  137. or32 r0, 0xa0
  138. and32 r0, 0xa3
  139. mov32 r9, 0x91
  140. and32 r0, r9
  141. lsh32 r0, 22
  142. lsh32 r0, r8
  143. rsh32 r0, 19
  144. rsh32 r0, r7
  145. xor32 r0, 0x03
  146. xor32 r0, r2
  147. exit",
  148. )
  149. .unwrap();
  150. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  151. assert_eq!(vm.execute_program().unwrap(), 0x11);
  152. }
  153. #[test]
  154. fn test_vm_arsh32_high_shift() {
  155. let prog = assemble(
  156. "
  157. mov r0, 8
  158. lddw r1, 0x100000001
  159. arsh32 r0, r1
  160. exit",
  161. )
  162. .unwrap();
  163. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  164. assert_eq!(vm.execute_program().unwrap(), 0x4);
  165. }
  166. #[test]
  167. fn test_vm_arsh() {
  168. let prog = assemble(
  169. "
  170. mov32 r0, 0xf8
  171. lsh32 r0, 28
  172. arsh32 r0, 16
  173. exit",
  174. )
  175. .unwrap();
  176. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  177. assert_eq!(vm.execute_program().unwrap(), 0xffff8000);
  178. }
  179. #[test]
  180. fn test_vm_arsh64() {
  181. let prog = assemble(
  182. "
  183. mov32 r0, 1
  184. lsh r0, 63
  185. arsh r0, 55
  186. mov32 r1, 5
  187. arsh r0, r1
  188. exit",
  189. )
  190. .unwrap();
  191. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  192. assert_eq!(vm.execute_program().unwrap(), 0xfffffffffffffff8);
  193. }
  194. #[test]
  195. fn test_vm_arsh_reg() {
  196. let prog = assemble(
  197. "
  198. mov32 r0, 0xf8
  199. mov32 r1, 16
  200. lsh32 r0, 28
  201. arsh32 r0, r1
  202. exit",
  203. )
  204. .unwrap();
  205. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  206. assert_eq!(vm.execute_program().unwrap(), 0xffff8000);
  207. }
  208. #[test]
  209. fn test_vm_arsh_imm_overflow() {
  210. let prog = assemble(
  211. "
  212. mov r0, 1
  213. lsh r0, 63
  214. arsh r0, 0xff20
  215. exit",
  216. )
  217. .unwrap();
  218. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  219. assert_eq!(vm.execute_program().unwrap(), 0xffffffff80000000);
  220. }
  221. #[test]
  222. fn test_vm_arsh_reg_overflow() {
  223. let prog = assemble(
  224. "
  225. mov r0, 1
  226. lsh r0, 63
  227. mov r1, 0xff04
  228. arsh r0, r1
  229. exit",
  230. )
  231. .unwrap();
  232. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  233. assert_eq!(vm.execute_program().unwrap(), 0xf800000000000000);
  234. }
  235. #[test]
  236. fn test_vm_arsh32_imm_overflow() {
  237. let prog = assemble(
  238. "
  239. mov32 r0, 1
  240. lsh32 r0, 31
  241. arsh32 r0, 0xff10
  242. exit",
  243. )
  244. .unwrap();
  245. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  246. assert_eq!(vm.execute_program().unwrap(), 0xffff8000);
  247. }
  248. #[test]
  249. fn test_vm_arsh32_reg_overflow() {
  250. let prog = assemble(
  251. "
  252. mov32 r0, 1
  253. lsh32 r0, 31
  254. mov32 r1, 32
  255. arsh32 r0, r1
  256. exit",
  257. )
  258. .unwrap();
  259. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  260. assert_eq!(vm.execute_program().unwrap(), 0x80000000);
  261. }
  262. #[test]
  263. fn test_vm_be16() {
  264. let prog = assemble(
  265. "
  266. ldxh r0, [r1]
  267. be16 r0
  268. exit",
  269. )
  270. .unwrap();
  271. let mem = &mut [0x11, 0x22];
  272. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  273. assert_eq!(vm.execute_program(mem).unwrap(), 0x1122);
  274. }
  275. #[test]
  276. fn test_vm_be16_high() {
  277. let prog = assemble(
  278. "
  279. ldxdw r0, [r1]
  280. be16 r0
  281. exit",
  282. )
  283. .unwrap();
  284. let mem = &mut [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88];
  285. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  286. assert_eq!(vm.execute_program(mem).unwrap(), 0x1122);
  287. }
  288. #[test]
  289. fn test_vm_be32() {
  290. let prog = assemble(
  291. "
  292. ldxw r0, [r1]
  293. be32 r0
  294. exit",
  295. )
  296. .unwrap();
  297. let mem = &mut [0x11, 0x22, 0x33, 0x44];
  298. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  299. assert_eq!(vm.execute_program(mem).unwrap(), 0x11223344);
  300. }
  301. #[test]
  302. fn test_vm_be32_high() {
  303. let prog = assemble(
  304. "
  305. ldxdw r0, [r1]
  306. be32 r0
  307. exit",
  308. )
  309. .unwrap();
  310. let mem = &mut [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88];
  311. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  312. assert_eq!(vm.execute_program(mem).unwrap(), 0x11223344);
  313. }
  314. #[test]
  315. fn test_vm_be64() {
  316. let prog = assemble(
  317. "
  318. ldxdw r0, [r1]
  319. be64 r0
  320. exit",
  321. )
  322. .unwrap();
  323. let mem = &mut [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88];
  324. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  325. assert_eq!(vm.execute_program(mem).unwrap(), 0x1122334455667788);
  326. }
  327. #[test]
  328. fn test_vm_call() {
  329. let prog = assemble(
  330. "
  331. mov r1, 1
  332. mov r2, 2
  333. mov r3, 3
  334. mov r4, 4
  335. mov r5, 5
  336. call 0
  337. exit",
  338. )
  339. .unwrap();
  340. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  341. vm.register_helper(0, helpers::gather_bytes).unwrap();
  342. assert_eq!(vm.execute_program().unwrap(), 0x0102030405);
  343. }
  344. #[test]
  345. fn test_vm_call_memfrob() {
  346. let prog = assemble(
  347. "
  348. mov r6, r1
  349. add r1, 2
  350. mov r2, 4
  351. call 1
  352. ldxdw r0, [r6]
  353. be64 r0
  354. exit",
  355. )
  356. .unwrap();
  357. let mem = &mut [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];
  358. let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  359. vm.register_helper(1, helpers::memfrob).unwrap();
  360. assert_eq!(vm.execute_program(mem).unwrap(), 0x102292e2f2c0708);
  361. }
  362. // TODO: helpers::trash_registers needs asm!().
  363. // Try this again once asm!() is available in stable.
  364. //#[test]
  365. //fn test_vm_call_save() {
  366. //let prog = &[
  367. //0xb7, 0x06, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  368. //0xb7, 0x07, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
  369. //0xb7, 0x08, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
  370. //0xb7, 0x09, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00,
  371. //0x85, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  372. //0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  373. //0x4f, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  374. //0x4f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  375. //0x4f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  376. //0x4f, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  377. //0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  378. //];
  379. //let mut vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
  380. //vm.register_helper(2, helpers::trash_registers);
  381. //assert_eq!(vm.execute_program().unwrap(), 0x4321);
  382. //}
  383. #[test]
  384. fn test_vm_div32_high_divisor() {
  385. let prog = assemble(
  386. "
  387. mov r0, 12
  388. lddw r1, 0x100000004
  389. div32 r0, r1
  390. exit",
  391. )
  392. .unwrap();
  393. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  394. assert_eq!(vm.execute_program().unwrap(), 0x3);
  395. }
  396. #[test]
  397. fn test_vm_div32_imm() {
  398. let prog = assemble(
  399. "
  400. lddw r0, 0x10000000c
  401. div32 r0, 4
  402. exit",
  403. )
  404. .unwrap();
  405. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  406. assert_eq!(vm.execute_program().unwrap(), 0x3);
  407. }
  408. #[test]
  409. fn test_vm_div32_reg() {
  410. let prog = assemble(
  411. "
  412. lddw r0, 0x10000000c
  413. mov r1, 4
  414. div32 r0, r1
  415. exit",
  416. )
  417. .unwrap();
  418. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  419. assert_eq!(vm.execute_program().unwrap(), 0x3);
  420. }
  421. #[test]
  422. fn test_vm_div64_imm() {
  423. let prog = assemble(
  424. "
  425. mov r0, 0xc
  426. lsh r0, 32
  427. div r0, 4
  428. exit",
  429. )
  430. .unwrap();
  431. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  432. assert_eq!(vm.execute_program().unwrap(), 0x300000000);
  433. }
  434. #[test]
  435. fn test_vm_div64_reg() {
  436. let prog = assemble(
  437. "
  438. mov r0, 0xc
  439. lsh r0, 32
  440. mov r1, 4
  441. div r0, r1
  442. exit",
  443. )
  444. .unwrap();
  445. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  446. assert_eq!(vm.execute_program().unwrap(), 0x300000000);
  447. }
  448. #[test]
  449. fn test_vm_early_exit() {
  450. let prog = assemble(
  451. "
  452. mov r0, 3
  453. exit
  454. mov r0, 4
  455. exit",
  456. )
  457. .unwrap();
  458. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  459. assert_eq!(vm.execute_program().unwrap(), 0x3);
  460. }
  461. // uBPF limits the number of user functions at 64. We don't.
  462. //#[test]
  463. //fn test_vm_err_call_bad_imm() {
  464. //}
  465. #[test]
  466. #[should_panic(expected = "Error: unknown helper function (id: 0x3f)")]
  467. fn test_vm_err_call_unreg() {
  468. let prog = assemble(
  469. "
  470. mov r1, 1
  471. mov r2, 2
  472. mov r3, 3
  473. mov r4, 4
  474. mov r5, 5
  475. call 63
  476. exit",
  477. )
  478. .unwrap();
  479. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  480. vm.execute_program().unwrap();
  481. }
  482. #[test]
  483. fn test_vm_div64_by_zero_imm() {
  484. let prog = assemble(
  485. "
  486. mov32 r0, 1
  487. div r0, 0
  488. exit",
  489. )
  490. .unwrap();
  491. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  492. assert_eq!(vm.execute_program().unwrap(), 0x0);
  493. }
  494. #[test]
  495. fn test_vm_div_by_zero_imm() {
  496. let prog = assemble(
  497. "
  498. mov32 r0, 1
  499. div32 r0, 0
  500. exit",
  501. )
  502. .unwrap();
  503. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  504. assert_eq!(vm.execute_program().unwrap(), 0x0);
  505. }
  506. #[test]
  507. fn test_vm_mod64_by_zero_imm() {
  508. let prog = assemble(
  509. "
  510. mov32 r0, 1
  511. mod r0, 0
  512. exit",
  513. )
  514. .unwrap();
  515. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  516. assert_eq!(vm.execute_program().unwrap(), 0x1);
  517. }
  518. #[test]
  519. fn test_vm_mod_by_zero_imm() {
  520. let prog = assemble(
  521. "
  522. mov32 r0, 1
  523. mod32 r0, 0
  524. exit",
  525. )
  526. .unwrap();
  527. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  528. assert_eq!(vm.execute_program().unwrap(), 0x1);
  529. }
  530. // Make sure we only consider the last 32 bits of the divisor.
  531. #[test]
  532. fn test_vm_mod_by_zero_reg_long() {
  533. let prog = assemble(
  534. "
  535. lddw r1, 0x100000000
  536. mod32 r0, r1
  537. exit",
  538. )
  539. .unwrap();
  540. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  541. assert_eq!(vm.execute_program().unwrap(), 0x0);
  542. }
  543. #[test]
  544. fn test_vm_div64_by_zero_reg() {
  545. let prog = assemble(
  546. "
  547. mov32 r0, 1
  548. mov32 r1, 0
  549. div r0, r1
  550. exit",
  551. )
  552. .unwrap();
  553. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  554. assert_eq!(vm.execute_program().unwrap(), 0x0);
  555. }
  556. #[test]
  557. fn test_vm_div_by_zero_reg() {
  558. let prog = assemble(
  559. "
  560. mov32 r0, 1
  561. mov32 r1, 0
  562. div32 r0, r1
  563. exit",
  564. )
  565. .unwrap();
  566. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  567. assert_eq!(vm.execute_program().unwrap(), 0x0);
  568. }
  569. // Make sure we only consider the last 32 bits of the divisor.
  570. #[test]
  571. fn test_vm_div_by_zero_reg_long() {
  572. let prog = assemble(
  573. "
  574. lddw r1, 0x100000000
  575. div32 r0, r1
  576. exit",
  577. )
  578. .unwrap();
  579. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  580. assert_eq!(vm.execute_program().unwrap(), 0x0);
  581. }
  582. #[test]
  583. fn test_vm_mod64_by_zero_reg() {
  584. let prog = assemble(
  585. "
  586. mov32 r0, 1
  587. mov32 r1, 0
  588. mod r0, r1
  589. exit",
  590. )
  591. .unwrap();
  592. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  593. assert_eq!(vm.execute_program().unwrap(), 0x1);
  594. }
  595. #[test]
  596. fn test_vm_mod_by_zero_reg() {
  597. let prog = assemble(
  598. "
  599. mov32 r0, 1
  600. mov32 r1, 0
  601. mod32 r0, r1
  602. exit",
  603. )
  604. .unwrap();
  605. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  606. assert_eq!(vm.execute_program().unwrap(), 0x1);
  607. }
  608. #[test]
  609. #[should_panic(expected = "Error: out of bounds memory store (insn #1)")]
  610. fn test_vm_err_stack_out_of_bound() {
  611. let prog = assemble(
  612. "
  613. stb [r10], 0
  614. exit",
  615. )
  616. .unwrap();
  617. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  618. vm.execute_program().unwrap();
  619. }
  620. #[test]
  621. fn test_vm_exit() {
  622. let prog = assemble(
  623. "
  624. mov r0, 0
  625. exit",
  626. )
  627. .unwrap();
  628. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  629. assert_eq!(vm.execute_program().unwrap(), 0x0);
  630. }
  631. #[test]
  632. fn test_vm_ja() {
  633. let prog = assemble(
  634. "
  635. mov r0, 1
  636. ja +1
  637. mov r0, 2
  638. exit",
  639. )
  640. .unwrap();
  641. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  642. assert_eq!(vm.execute_program().unwrap(), 0x1);
  643. }
  644. #[test]
  645. fn test_vm_jeq_imm() {
  646. let prog = assemble(
  647. "
  648. mov32 r0, 0
  649. mov32 r1, 0xa
  650. jeq r1, 0xb, +4
  651. mov32 r0, 1
  652. mov32 r1, 0xb
  653. jeq r1, 0xb, +1
  654. mov32 r0, 2
  655. exit",
  656. )
  657. .unwrap();
  658. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  659. assert_eq!(vm.execute_program().unwrap(), 0x1);
  660. }
  661. #[test]
  662. fn test_vm_jeq_reg() {
  663. let prog = assemble(
  664. "
  665. mov32 r0, 0
  666. mov32 r1, 0xa
  667. mov32 r2, 0xb
  668. jeq r1, r2, +4
  669. mov32 r0, 1
  670. mov32 r1, 0xb
  671. jeq r1, r2, +1
  672. mov32 r0, 2
  673. exit",
  674. )
  675. .unwrap();
  676. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  677. assert_eq!(vm.execute_program().unwrap(), 0x1);
  678. }
  679. #[test]
  680. fn test_vm_jge_imm() {
  681. let prog = assemble(
  682. "
  683. mov32 r0, 0
  684. mov32 r1, 0xa
  685. jge r1, 0xb, +4
  686. mov32 r0, 1
  687. mov32 r1, 0xc
  688. jge r1, 0xb, +1
  689. mov32 r0, 2
  690. exit",
  691. )
  692. .unwrap();
  693. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  694. assert_eq!(vm.execute_program().unwrap(), 0x1);
  695. }
  696. #[test]
  697. fn test_vm_jle_imm() {
  698. let prog = assemble(
  699. "
  700. mov32 r0, 0
  701. mov32 r1, 5
  702. jle r1, 4, +1
  703. jle r1, 6, +1
  704. exit
  705. jle r1, 5, +1
  706. exit
  707. mov32 r0, 1
  708. exit",
  709. )
  710. .unwrap();
  711. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  712. assert_eq!(vm.execute_program().unwrap(), 0x1);
  713. }
  714. #[test]
  715. fn test_vm_jle_reg() {
  716. let prog = assemble(
  717. "
  718. mov r0, 0
  719. mov r1, 5
  720. mov r2, 4
  721. mov r3, 6
  722. jle r1, r2, +2
  723. jle r1, r1, +1
  724. exit
  725. jle r1, r3, +1
  726. exit
  727. mov r0, 1
  728. exit",
  729. )
  730. .unwrap();
  731. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  732. assert_eq!(vm.execute_program().unwrap(), 0x1);
  733. }
  734. #[test]
  735. fn test_vm_jgt_imm() {
  736. let prog = assemble(
  737. "
  738. mov32 r0, 0
  739. mov32 r1, 5
  740. jgt r1, 6, +2
  741. jgt r1, 5, +1
  742. jgt r1, 4, +1
  743. exit
  744. mov32 r0, 1
  745. exit",
  746. )
  747. .unwrap();
  748. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  749. assert_eq!(vm.execute_program().unwrap(), 0x1);
  750. }
  751. #[test]
  752. fn test_vm_jgt_reg() {
  753. let prog = assemble(
  754. "
  755. mov r0, 0
  756. mov r1, 5
  757. mov r2, 6
  758. mov r3, 4
  759. jgt r1, r2, +2
  760. jgt r1, r1, +1
  761. jgt r1, r3, +1
  762. exit
  763. mov r0, 1
  764. exit",
  765. )
  766. .unwrap();
  767. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  768. assert_eq!(vm.execute_program().unwrap(), 0x1);
  769. }
  770. #[test]
  771. fn test_vm_jlt_imm() {
  772. let prog = assemble(
  773. "
  774. mov32 r0, 0
  775. mov32 r1, 5
  776. jlt r1, 4, +2
  777. jlt r1, 5, +1
  778. jlt r1, 6, +1
  779. exit
  780. mov32 r0, 1
  781. exit",
  782. )
  783. .unwrap();
  784. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  785. assert_eq!(vm.execute_program().unwrap(), 0x1);
  786. }
  787. #[test]
  788. fn test_vm_jlt_reg() {
  789. let prog = assemble(
  790. "
  791. mov r0, 0
  792. mov r1, 5
  793. mov r2, 4
  794. mov r3, 6
  795. jlt r1, r2, +2
  796. jlt r1, r1, +1
  797. jlt r1, r3, +1
  798. exit
  799. mov r0, 1
  800. exit",
  801. )
  802. .unwrap();
  803. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  804. assert_eq!(vm.execute_program().unwrap(), 0x1);
  805. }
  806. #[test]
  807. fn test_vm_jit_bounce() {
  808. let prog = assemble(
  809. "
  810. mov r0, 1
  811. mov r6, r0
  812. mov r7, r6
  813. mov r8, r7
  814. mov r9, r8
  815. mov r0, r9
  816. exit",
  817. )
  818. .unwrap();
  819. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  820. assert_eq!(vm.execute_program().unwrap(), 0x1);
  821. }
  822. #[test]
  823. fn test_vm_jne_reg() {
  824. let prog = assemble(
  825. "
  826. mov32 r0, 0
  827. mov32 r1, 0xb
  828. mov32 r2, 0xb
  829. jne r1, r2, +4
  830. mov32 r0, 1
  831. mov32 r1, 0xa
  832. jne r1, r2, +1
  833. mov32 r0, 2
  834. exit",
  835. )
  836. .unwrap();
  837. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  838. assert_eq!(vm.execute_program().unwrap(), 0x1);
  839. }
  840. #[test]
  841. fn test_vm_jset_imm() {
  842. let prog = assemble(
  843. "
  844. mov32 r0, 0
  845. mov32 r1, 0x7
  846. jset r1, 0x8, +4
  847. mov32 r0, 1
  848. mov32 r1, 0x9
  849. jset r1, 0x8, +1
  850. mov32 r0, 2
  851. exit",
  852. )
  853. .unwrap();
  854. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  855. assert_eq!(vm.execute_program().unwrap(), 0x1);
  856. }
  857. #[test]
  858. fn test_vm_jset_reg() {
  859. let prog = assemble(
  860. "
  861. mov32 r0, 0
  862. mov32 r1, 0x7
  863. mov32 r2, 0x8
  864. jset r1, r2, +4
  865. mov32 r0, 1
  866. mov32 r1, 0x9
  867. jset r1, r2, +1
  868. mov32 r0, 2
  869. exit",
  870. )
  871. .unwrap();
  872. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  873. assert_eq!(vm.execute_program().unwrap(), 0x1);
  874. }
  875. #[test]
  876. fn test_vm_jsge_imm() {
  877. let prog = assemble(
  878. "
  879. mov32 r0, 0
  880. mov r1, -2
  881. jsge r1, -1, +5
  882. jsge r1, 0, +4
  883. mov32 r0, 1
  884. mov r1, -1
  885. jsge r1, -1, +1
  886. mov32 r0, 2
  887. exit",
  888. )
  889. .unwrap();
  890. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  891. assert_eq!(vm.execute_program().unwrap(), 0x1);
  892. }
  893. #[test]
  894. fn test_vm_jsge_reg() {
  895. let prog = assemble(
  896. "
  897. mov32 r0, 0
  898. mov r1, -2
  899. mov r2, -1
  900. mov32 r3, 0
  901. jsge r1, r2, +5
  902. jsge r1, r3, +4
  903. mov32 r0, 1
  904. mov r1, r2
  905. jsge r1, r2, +1
  906. mov32 r0, 2
  907. exit",
  908. )
  909. .unwrap();
  910. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  911. assert_eq!(vm.execute_program().unwrap(), 0x1);
  912. }
  913. #[test]
  914. fn test_vm_jsle_imm() {
  915. let prog = assemble(
  916. "
  917. mov32 r0, 0
  918. mov r1, -2
  919. jsle r1, -3, +1
  920. jsle r1, -1, +1
  921. exit
  922. mov32 r0, 1
  923. jsle r1, -2, +1
  924. mov32 r0, 2
  925. exit",
  926. )
  927. .unwrap();
  928. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  929. assert_eq!(vm.execute_program().unwrap(), 0x1);
  930. }
  931. #[test]
  932. fn test_vm_jsle_reg() {
  933. let prog = assemble(
  934. "
  935. mov32 r0, 0
  936. mov r1, -1
  937. mov r2, -2
  938. mov32 r3, 0
  939. jsle r1, r2, +1
  940. jsle r1, r3, +1
  941. exit
  942. mov32 r0, 1
  943. mov r1, r2
  944. jsle r1, r2, +1
  945. mov32 r0, 2
  946. exit",
  947. )
  948. .unwrap();
  949. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  950. assert_eq!(vm.execute_program().unwrap(), 0x1);
  951. }
  952. #[test]
  953. fn test_vm_jsgt_imm() {
  954. let prog = assemble(
  955. "
  956. mov32 r0, 0
  957. mov r1, -2
  958. jsgt r1, -1, +4
  959. mov32 r0, 1
  960. mov32 r1, 0
  961. jsgt r1, -1, +1
  962. mov32 r0, 2
  963. exit",
  964. )
  965. .unwrap();
  966. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  967. assert_eq!(vm.execute_program().unwrap(), 0x1);
  968. }
  969. #[test]
  970. fn test_vm_jsgt_reg() {
  971. let prog = assemble(
  972. "
  973. mov32 r0, 0
  974. mov r1, -2
  975. mov r2, -1
  976. jsgt r1, r2, +4
  977. mov32 r0, 1
  978. mov32 r1, 0
  979. jsgt r1, r2, +1
  980. mov32 r0, 2
  981. exit",
  982. )
  983. .unwrap();
  984. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  985. assert_eq!(vm.execute_program().unwrap(), 0x1);
  986. }
  987. #[test]
  988. fn test_vm_jslt_imm() {
  989. let prog = assemble(
  990. "
  991. mov32 r0, 0
  992. mov r1, -2
  993. jslt r1, -3, +2
  994. jslt r1, -2, +1
  995. jslt r1, -1, +1
  996. exit
  997. mov32 r0, 1
  998. exit",
  999. )
  1000. .unwrap();
  1001. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1002. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1003. }
  1004. #[test]
  1005. fn test_vm_jslt_reg() {
  1006. let prog = assemble(
  1007. "
  1008. mov32 r0, 0
  1009. mov r1, -2
  1010. mov r2, -3
  1011. mov r3, -1
  1012. jslt r1, r1, +2
  1013. jslt r1, r2, +1
  1014. jslt r1, r3, +1
  1015. exit
  1016. mov32 r0, 1
  1017. exit",
  1018. )
  1019. .unwrap();
  1020. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1021. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1022. }
  1023. #[test]
  1024. fn test_vm_jeq32_imm() {
  1025. let prog = assemble(
  1026. "
  1027. mov r9, 1
  1028. lsh r9, 32
  1029. mov32 r0, 0x0
  1030. mov32 r1, 0xa
  1031. jeq32 r1, 0xb, +5
  1032. mov32 r0, 1
  1033. mov r1, 0xb
  1034. or r1, r9
  1035. jeq32 r1, 0xb, +1
  1036. mov32 r0, 2
  1037. exit",
  1038. )
  1039. .unwrap();
  1040. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1041. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1042. }
  1043. #[test]
  1044. fn test_vm_jeq32_reg() {
  1045. let prog = assemble(
  1046. "
  1047. mov r9, 1
  1048. lsh r9, 32
  1049. mov32 r0, 0
  1050. mov32 r1, 0xa
  1051. mov32 r2, 0xb
  1052. jeq32 r1, r2, +5
  1053. mov32 r0, 1
  1054. mov32 r1, 0xb
  1055. or r1, r9
  1056. jeq32 r1, r2, +1
  1057. mov32 r0, 2
  1058. exit",
  1059. )
  1060. .unwrap();
  1061. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1062. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1063. }
  1064. #[test]
  1065. fn test_vm_jge32_imm() {
  1066. let prog = assemble(
  1067. "
  1068. mov r9, 1
  1069. lsh r9, 32
  1070. mov32 r0, 0
  1071. mov32 r1, 0xa
  1072. jge32 r1, 0xb, +5
  1073. mov32 r0, 1
  1074. or r1, r9
  1075. mov32 r1, 0xc
  1076. jge32 r1, 0xb, +1
  1077. mov32 r0, 2
  1078. exit",
  1079. )
  1080. .unwrap();
  1081. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1082. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1083. }
  1084. #[test]
  1085. fn test_vm_jge32_reg() {
  1086. let prog = assemble(
  1087. "
  1088. mov r9, 1
  1089. lsh r9, 32
  1090. mov32 r0, 0
  1091. mov32 r1, 0xa
  1092. mov32 r2, 0xb
  1093. jge32 r1, r2, +5
  1094. mov32 r0, 1
  1095. or r1, r9
  1096. mov32 r1, 0xc
  1097. jge32 r1, r2, +1
  1098. mov32 r0, 2
  1099. exit",
  1100. )
  1101. .unwrap();
  1102. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1103. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1104. }
  1105. #[test]
  1106. fn test_vm_jgt32_imm() {
  1107. let prog = assemble(
  1108. "
  1109. mov r9, 1
  1110. lsh r9, 32
  1111. mov32 r0, 0
  1112. mov32 r1, 5
  1113. or r1, r9
  1114. jgt32 r1, 6, +4
  1115. jgt32 r1, 5, +3
  1116. jgt32 r1, 4, +1
  1117. exit
  1118. mov32 r0, 1
  1119. exit",
  1120. )
  1121. .unwrap();
  1122. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1123. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1124. }
  1125. #[test]
  1126. fn test_vm_jgt32_reg() {
  1127. let prog = assemble(
  1128. "
  1129. mov r9, 1
  1130. lsh r9, 32
  1131. mov r0, 0
  1132. mov r1, 5
  1133. mov32 r1, 5
  1134. or r1, r9
  1135. mov r2, 6
  1136. mov r3, 4
  1137. jgt32 r1, r2, +4
  1138. jgt32 r1, r1, +3
  1139. jgt32 r1, r3, +1
  1140. exit
  1141. mov r0, 1
  1142. exit",
  1143. )
  1144. .unwrap();
  1145. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1146. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1147. }
  1148. #[test]
  1149. fn test_vm_jle32_imm() {
  1150. let prog = assemble(
  1151. "
  1152. mov r9, 1
  1153. lsh r9, 32
  1154. mov32 r0, 0
  1155. mov32 r1, 5
  1156. or r1, r9
  1157. jle32 r1, 4, +5
  1158. jle32 r1, 6, +1
  1159. exit
  1160. jle32 r1, 5, +1
  1161. exit
  1162. mov32 r0, 1
  1163. exit",
  1164. )
  1165. .unwrap();
  1166. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1167. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1168. }
  1169. #[test]
  1170. fn test_vm_jle32_reg() {
  1171. let prog = assemble(
  1172. "
  1173. mov r9, 1
  1174. lsh r9, 32
  1175. mov r0, 0
  1176. mov r1, 5
  1177. mov r2, 4
  1178. mov r3, 6
  1179. or r1, r9
  1180. jle32 r1, r2, +5
  1181. jle32 r1, r1, +1
  1182. exit
  1183. jle32 r1, r3, +1
  1184. exit
  1185. mov r0, 1
  1186. exit",
  1187. )
  1188. .unwrap();
  1189. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1190. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1191. }
  1192. #[test]
  1193. fn test_vm_jlt32_imm() {
  1194. let prog = assemble(
  1195. "
  1196. mov r9, 1
  1197. lsh r9, 32
  1198. mov32 r0, 0
  1199. mov32 r1, 5
  1200. or r1, r9
  1201. jlt32 r1, 4, +4
  1202. jlt32 r1, 5, +3
  1203. jlt32 r1, 6, +1
  1204. exit
  1205. mov32 r0, 1
  1206. exit",
  1207. )
  1208. .unwrap();
  1209. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1210. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1211. }
  1212. #[test]
  1213. fn test_vm_jlt32_reg() {
  1214. let prog = assemble(
  1215. "
  1216. mov r9, 1
  1217. lsh r9, 32
  1218. mov r0, 0
  1219. mov r1, 5
  1220. mov r2, 4
  1221. mov r3, 6
  1222. or r1, r9
  1223. jlt32 r1, r2, +4
  1224. jlt32 r1, r1, +3
  1225. jlt32 r1, r3, +1
  1226. exit
  1227. mov r0, 1
  1228. exit",
  1229. )
  1230. .unwrap();
  1231. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1232. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1233. }
  1234. #[test]
  1235. fn test_vm_jne32_imm() {
  1236. let prog = assemble(
  1237. "
  1238. mov r9, 1
  1239. lsh r9, 32
  1240. mov32 r0, 0
  1241. mov32 r1, 0xb
  1242. or r1, r9
  1243. jne32 r1, 0xb, +4
  1244. mov32 r0, 1
  1245. mov32 r1, 0xa
  1246. or r1, r9
  1247. jne32 r1, 0xb, +1
  1248. mov32 r0, 2
  1249. exit",
  1250. )
  1251. .unwrap();
  1252. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1253. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1254. }
  1255. #[test]
  1256. fn test_vm_jne32_reg() {
  1257. let prog = assemble(
  1258. "
  1259. mov r9, 1
  1260. lsh r9, 32
  1261. mov32 r0, 0
  1262. mov32 r1, 0xb
  1263. or r1, r9
  1264. mov32 r2, 0xb
  1265. jne32 r1, r2, +4
  1266. mov32 r0, 1
  1267. mov32 r1, 0xa
  1268. or r1, r9
  1269. jne32 r1, r2, +1
  1270. mov32 r0, 2
  1271. exit",
  1272. )
  1273. .unwrap();
  1274. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1275. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1276. }
  1277. #[test]
  1278. fn test_vm_jset32_imm() {
  1279. let prog = assemble(
  1280. "
  1281. mov r9, 1
  1282. lsh r9, 32
  1283. mov32 r0, 0
  1284. mov32 r1, 0x7
  1285. or r1, r9
  1286. jset32 r1, 0x8, +4
  1287. mov32 r0, 1
  1288. mov32 r1, 0x9
  1289. jset32 r1, 0x8, +1
  1290. mov32 r0, 2
  1291. exit",
  1292. )
  1293. .unwrap();
  1294. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1295. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1296. }
  1297. #[test]
  1298. fn test_vm_jset32_reg() {
  1299. let prog = assemble(
  1300. "
  1301. mov r9, 1
  1302. lsh r9, 32
  1303. mov32 r0, 0
  1304. mov32 r1, 0x7
  1305. or r1, r9
  1306. mov32 r2, 0x8
  1307. jset32 r1, r2, +4
  1308. mov32 r0, 1
  1309. mov32 r1, 0x9
  1310. jset32 r1, r2, +1
  1311. mov32 r0, 2
  1312. exit",
  1313. )
  1314. .unwrap();
  1315. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1316. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1317. }
  1318. #[test]
  1319. fn test_vm_jsge32_imm() {
  1320. let prog = assemble(
  1321. "
  1322. mov r9, 1
  1323. lsh r9, 32
  1324. mov32 r0, 0
  1325. mov32 r1, -2
  1326. or r1, r9
  1327. jsge32 r1, -1, +5
  1328. jsge32 r1, 0, +4
  1329. mov32 r0, 1
  1330. mov r1, -1
  1331. jsge32 r1, -1, +1
  1332. mov32 r0, 2
  1333. exit",
  1334. )
  1335. .unwrap();
  1336. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1337. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1338. }
  1339. #[test]
  1340. fn test_vm_jsge32_reg() {
  1341. let prog = assemble(
  1342. "
  1343. mov r9, 1
  1344. lsh r9, 32
  1345. mov32 r0, 0
  1346. mov32 r1, -2
  1347. or r1, r9
  1348. mov r2, -1
  1349. mov32 r3, 0
  1350. jsge32 r1, r2, +5
  1351. jsge32 r1, r3, +4
  1352. mov32 r0, 1
  1353. mov r1, r2
  1354. jsge32 r1, r2, +1
  1355. mov32 r0, 2
  1356. exit",
  1357. )
  1358. .unwrap();
  1359. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1360. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1361. }
  1362. #[test]
  1363. fn test_vm_jsgt32_imm() {
  1364. let prog = assemble(
  1365. "
  1366. mov r9, 1
  1367. lsh r9, 32
  1368. mov32 r0, 0
  1369. mov32 r1, -2
  1370. or r1, r9
  1371. jsgt32 r1, -1, +4
  1372. mov32 r0, 1
  1373. mov32 r1, 0
  1374. jsgt32 r1, -1, +1
  1375. mov32 r0, 2
  1376. exit",
  1377. )
  1378. .unwrap();
  1379. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1380. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1381. }
  1382. #[test]
  1383. fn test_vm_jsgt32_reg() {
  1384. let prog = assemble(
  1385. "
  1386. mov r9, 1
  1387. lsh r9, 32
  1388. mov32 r0, 0
  1389. mov32 r1, -2
  1390. or r1, r9
  1391. mov r2, -1
  1392. jsgt32 r1, r2, +4
  1393. mov32 r0, 1
  1394. mov32 r1, 0
  1395. jsgt32 r1, r2, +1
  1396. mov32 r0, 2
  1397. exit",
  1398. )
  1399. .unwrap();
  1400. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1401. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1402. }
  1403. #[test]
  1404. fn test_vm_jsle32_imm() {
  1405. let prog = assemble(
  1406. "
  1407. mov r9, 1
  1408. lsh r9, 32
  1409. mov32 r0, 0
  1410. mov32 r1, -2
  1411. or r1, r9
  1412. jsle32 r1, -3, +5
  1413. jsle32 r1, -1, +1
  1414. exit
  1415. mov32 r0, 1
  1416. jsle32 r1, -2, +1
  1417. mov32 r0, 2
  1418. exit",
  1419. )
  1420. .unwrap();
  1421. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1422. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1423. }
  1424. #[test]
  1425. fn test_vm_jsle32_reg() {
  1426. let prog = assemble(
  1427. "
  1428. mov r9, 1
  1429. lsh r9, 32
  1430. mov32 r0, 0
  1431. mov32 r1, -2
  1432. or r1, r9
  1433. mov r2, -3
  1434. mov32 r3, 0
  1435. jsle32 r1, r2, +6
  1436. jsle32 r1, r3, +1
  1437. exit
  1438. mov32 r0, 1
  1439. mov r1, r2
  1440. jsle32 r1, r2, +1
  1441. mov32 r0, 2
  1442. exit",
  1443. )
  1444. .unwrap();
  1445. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1446. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1447. }
  1448. #[test]
  1449. fn test_vm_jslt32_imm() {
  1450. let prog = assemble(
  1451. "
  1452. mov r9, 1
  1453. lsh r9, 32
  1454. mov32 r0, 0
  1455. mov32 r1, -2
  1456. or r1, r9
  1457. jslt32 r1, -3, +4
  1458. jslt32 r1, -2, +3
  1459. jslt32 r1, -1, +1
  1460. exit
  1461. mov32 r0, 1
  1462. exit",
  1463. )
  1464. .unwrap();
  1465. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1466. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1467. }
  1468. #[test]
  1469. fn test_vm_jslt32_reg() {
  1470. let prog = assemble(
  1471. "
  1472. mov r9, 1
  1473. lsh r9, 32
  1474. mov32 r0, 0
  1475. mov32 r1, -2
  1476. or r1, r9
  1477. mov r2, -3
  1478. mov r3, -1
  1479. jslt32 r1, r1, +4
  1480. jslt32 r1, r2, +3
  1481. jslt32 r1, r3, +1
  1482. exit
  1483. mov32 r0, 1
  1484. exit",
  1485. )
  1486. .unwrap();
  1487. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1488. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1489. }
  1490. #[test]
  1491. fn test_vm_lddw() {
  1492. let prog = assemble(
  1493. "lddw r0, 0x1122334455667788
  1494. exit",
  1495. )
  1496. .unwrap();
  1497. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1498. assert_eq!(vm.execute_program().unwrap(), 0x1122334455667788);
  1499. }
  1500. #[test]
  1501. fn test_vm_lddw2() {
  1502. let prog = assemble(
  1503. "
  1504. lddw r0, 0x0000000080000000
  1505. exit",
  1506. )
  1507. .unwrap();
  1508. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1509. assert_eq!(vm.execute_program().unwrap(), 0x80000000);
  1510. }
  1511. #[test]
  1512. fn test_vm_ldxb_all() {
  1513. let prog = assemble(
  1514. "
  1515. mov r0, r1
  1516. ldxb r9, [r0+0]
  1517. lsh r9, 0
  1518. ldxb r8, [r0+1]
  1519. lsh r8, 4
  1520. ldxb r7, [r0+2]
  1521. lsh r7, 8
  1522. ldxb r6, [r0+3]
  1523. lsh r6, 12
  1524. ldxb r5, [r0+4]
  1525. lsh r5, 16
  1526. ldxb r4, [r0+5]
  1527. lsh r4, 20
  1528. ldxb r3, [r0+6]
  1529. lsh r3, 24
  1530. ldxb r2, [r0+7]
  1531. lsh r2, 28
  1532. ldxb r1, [r0+8]
  1533. lsh r1, 32
  1534. ldxb r0, [r0+9]
  1535. lsh r0, 36
  1536. or r0, r1
  1537. or r0, r2
  1538. or r0, r3
  1539. or r0, r4
  1540. or r0, r5
  1541. or r0, r6
  1542. or r0, r7
  1543. or r0, r8
  1544. or r0, r9
  1545. exit",
  1546. )
  1547. .unwrap();
  1548. let mem = &mut [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09];
  1549. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1550. assert_eq!(vm.execute_program(mem).unwrap(), 0x9876543210);
  1551. }
  1552. #[test]
  1553. fn test_vm_ldxb() {
  1554. let prog = assemble(
  1555. "
  1556. ldxb r0, [r1+2]
  1557. exit",
  1558. )
  1559. .unwrap();
  1560. let mem = &mut [0xaa, 0xbb, 0x11, 0xcc, 0xdd];
  1561. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1562. assert_eq!(vm.execute_program(mem).unwrap(), 0x11);
  1563. }
  1564. #[test]
  1565. fn test_vm_ldxdw() {
  1566. let prog = assemble(
  1567. "
  1568. ldxdw r0, [r1+2]
  1569. exit",
  1570. )
  1571. .unwrap();
  1572. let mem = &mut [
  1573. 0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0xcc, 0xdd,
  1574. ];
  1575. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1576. assert_eq!(vm.execute_program(mem).unwrap(), 0x8877665544332211);
  1577. }
  1578. #[test]
  1579. fn test_vm_ldxh_all() {
  1580. let prog = assemble(
  1581. "
  1582. mov r0, r1
  1583. ldxh r9, [r0+0]
  1584. be16 r9
  1585. lsh r9, 0
  1586. ldxh r8, [r0+2]
  1587. be16 r8
  1588. lsh r8, 4
  1589. ldxh r7, [r0+4]
  1590. be16 r7
  1591. lsh r7, 8
  1592. ldxh r6, [r0+6]
  1593. be16 r6
  1594. lsh r6, 12
  1595. ldxh r5, [r0+8]
  1596. be16 r5
  1597. lsh r5, 16
  1598. ldxh r4, [r0+10]
  1599. be16 r4
  1600. lsh r4, 20
  1601. ldxh r3, [r0+12]
  1602. be16 r3
  1603. lsh r3, 24
  1604. ldxh r2, [r0+14]
  1605. be16 r2
  1606. lsh r2, 28
  1607. ldxh r1, [r0+16]
  1608. be16 r1
  1609. lsh r1, 32
  1610. ldxh r0, [r0+18]
  1611. be16 r0
  1612. lsh r0, 36
  1613. or r0, r1
  1614. or r0, r2
  1615. or r0, r3
  1616. or r0, r4
  1617. or r0, r5
  1618. or r0, r6
  1619. or r0, r7
  1620. or r0, r8
  1621. or r0, r9
  1622. exit",
  1623. )
  1624. .unwrap();
  1625. let mem = &mut [
  1626. 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00,
  1627. 0x07, 0x00, 0x08, 0x00, 0x09,
  1628. ];
  1629. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1630. assert_eq!(vm.execute_program(mem).unwrap(), 0x9876543210);
  1631. }
  1632. #[test]
  1633. fn test_vm_ldxh_all2() {
  1634. let prog = assemble(
  1635. "
  1636. mov r0, r1
  1637. ldxh r9, [r0+0]
  1638. be16 r9
  1639. ldxh r8, [r0+2]
  1640. be16 r8
  1641. ldxh r7, [r0+4]
  1642. be16 r7
  1643. ldxh r6, [r0+6]
  1644. be16 r6
  1645. ldxh r5, [r0+8]
  1646. be16 r5
  1647. ldxh r4, [r0+10]
  1648. be16 r4
  1649. ldxh r3, [r0+12]
  1650. be16 r3
  1651. ldxh r2, [r0+14]
  1652. be16 r2
  1653. ldxh r1, [r0+16]
  1654. be16 r1
  1655. ldxh r0, [r0+18]
  1656. be16 r0
  1657. or r0, r1
  1658. or r0, r2
  1659. or r0, r3
  1660. or r0, r4
  1661. or r0, r5
  1662. or r0, r6
  1663. or r0, r7
  1664. or r0, r8
  1665. or r0, r9
  1666. exit",
  1667. )
  1668. .unwrap();
  1669. let mem = &mut [
  1670. 0x00, 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08, 0x00, 0x10, 0x00, 0x20, 0x00, 0x40, 0x00,
  1671. 0x80, 0x01, 0x00, 0x02, 0x00,
  1672. ];
  1673. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1674. assert_eq!(vm.execute_program(mem).unwrap(), 0x3ff);
  1675. }
  1676. #[test]
  1677. fn test_vm_ldxh() {
  1678. let prog = assemble(
  1679. "
  1680. ldxh r0, [r1+2]
  1681. exit",
  1682. )
  1683. .unwrap();
  1684. let mem = &mut [0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd];
  1685. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1686. assert_eq!(vm.execute_program(mem).unwrap(), 0x2211);
  1687. }
  1688. #[test]
  1689. fn test_vm_ldxh_same_reg() {
  1690. let prog = assemble(
  1691. "
  1692. mov r0, r1
  1693. sth [r0], 0x1234
  1694. ldxh r0, [r0]
  1695. exit",
  1696. )
  1697. .unwrap();
  1698. let mem = &mut [0xff, 0xff];
  1699. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1700. assert_eq!(vm.execute_program(mem).unwrap(), 0x1234);
  1701. }
  1702. #[test]
  1703. fn test_vm_ldxw_all() {
  1704. let prog = assemble(
  1705. "
  1706. mov r0, r1
  1707. ldxw r9, [r0+0]
  1708. be32 r9
  1709. ldxw r8, [r0+4]
  1710. be32 r8
  1711. ldxw r7, [r0+8]
  1712. be32 r7
  1713. ldxw r6, [r0+12]
  1714. be32 r6
  1715. ldxw r5, [r0+16]
  1716. be32 r5
  1717. ldxw r4, [r0+20]
  1718. be32 r4
  1719. ldxw r3, [r0+24]
  1720. be32 r3
  1721. ldxw r2, [r0+28]
  1722. be32 r2
  1723. ldxw r1, [r0+32]
  1724. be32 r1
  1725. ldxw r0, [r0+36]
  1726. be32 r0
  1727. or r0, r1
  1728. or r0, r2
  1729. or r0, r3
  1730. or r0, r4
  1731. or r0, r5
  1732. or r0, r6
  1733. or r0, r7
  1734. or r0, r8
  1735. or r0, r9
  1736. exit",
  1737. )
  1738. .unwrap();
  1739. let mem = &mut [
  1740. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  1741. 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  1742. 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
  1743. ];
  1744. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1745. assert_eq!(vm.execute_program(mem).unwrap(), 0x030f0f);
  1746. }
  1747. #[test]
  1748. fn test_vm_ldxw() {
  1749. let prog = assemble(
  1750. "
  1751. ldxw r0, [r1+2]
  1752. exit",
  1753. )
  1754. .unwrap();
  1755. let mem = &mut [0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0xcc, 0xdd];
  1756. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1757. assert_eq!(vm.execute_program(mem).unwrap(), 0x44332211);
  1758. }
  1759. #[test]
  1760. fn test_vm_le16() {
  1761. let prog = assemble(
  1762. "
  1763. ldxh r0, [r1]
  1764. le16 r0
  1765. exit",
  1766. )
  1767. .unwrap();
  1768. let mem = &mut [0x22, 0x11];
  1769. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1770. assert_eq!(vm.execute_program(mem).unwrap(), 0x1122);
  1771. }
  1772. #[test]
  1773. fn test_vm_le32() {
  1774. let prog = assemble(
  1775. "
  1776. ldxw r0, [r1]
  1777. le32 r0
  1778. exit",
  1779. )
  1780. .unwrap();
  1781. let mem = &mut [0x44, 0x33, 0x22, 0x11];
  1782. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1783. assert_eq!(vm.execute_program(mem).unwrap(), 0x11223344);
  1784. }
  1785. #[test]
  1786. fn test_vm_le64() {
  1787. let prog = assemble(
  1788. "
  1789. ldxdw r0, [r1]
  1790. le64 r0
  1791. exit",
  1792. )
  1793. .unwrap();
  1794. let mem = &mut [0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11];
  1795. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  1796. assert_eq!(vm.execute_program(mem).unwrap(), 0x1122334455667788);
  1797. }
  1798. #[test]
  1799. fn test_vm_lsh_imm() {
  1800. let prog = assemble(
  1801. "
  1802. mov r0, 1
  1803. lsh r0, 4
  1804. exit",
  1805. )
  1806. .unwrap();
  1807. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1808. assert_eq!(vm.execute_program().unwrap(), 0x10);
  1809. }
  1810. #[test]
  1811. fn test_vm_lsh_reg() {
  1812. let prog = assemble(
  1813. "
  1814. mov r0, 1
  1815. mov r7, 4
  1816. lsh r0, r7
  1817. exit",
  1818. )
  1819. .unwrap();
  1820. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1821. assert_eq!(vm.execute_program().unwrap(), 0x10);
  1822. }
  1823. #[test]
  1824. fn test_vm_lsh32_imm() {
  1825. let prog = assemble(
  1826. "
  1827. mov32 r0, 1
  1828. lsh32 r0, 4
  1829. exit",
  1830. )
  1831. .unwrap();
  1832. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1833. assert_eq!(vm.execute_program().unwrap(), 0x10);
  1834. }
  1835. #[test]
  1836. fn test_vm_lsh32_reg() {
  1837. let prog = assemble(
  1838. "
  1839. mov32 r0, 1
  1840. mov32 r7, 4
  1841. lsh32 r0, r7
  1842. exit",
  1843. )
  1844. .unwrap();
  1845. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1846. assert_eq!(vm.execute_program().unwrap(), 0x10);
  1847. }
  1848. #[test]
  1849. fn test_vm_lsh_imm_overflow() {
  1850. let prog = assemble(
  1851. "
  1852. mov r0, 1
  1853. lsh r0, 64
  1854. exit",
  1855. )
  1856. .unwrap();
  1857. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1858. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1859. }
  1860. #[test]
  1861. fn test_vm_lsh_reg_overflow() {
  1862. let prog = assemble(
  1863. "
  1864. mov r0, 1
  1865. mov r7, 64
  1866. lsh r0, r7
  1867. exit",
  1868. )
  1869. .unwrap();
  1870. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1871. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1872. }
  1873. #[test]
  1874. fn test_vm_lsh32_imm_overflow() {
  1875. let prog = assemble(
  1876. "
  1877. mov32 r0, 1
  1878. lsh32 r0, 32
  1879. exit",
  1880. )
  1881. .unwrap();
  1882. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1883. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1884. }
  1885. #[test]
  1886. fn test_vm_lsh32_reg_overflow() {
  1887. let prog = assemble(
  1888. "
  1889. mov32 r0, 1
  1890. mov32 r7, 32
  1891. lsh32 r0, r7
  1892. exit",
  1893. )
  1894. .unwrap();
  1895. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1896. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1897. }
  1898. #[test]
  1899. fn test_vm_mod() {
  1900. let prog = assemble(
  1901. "
  1902. mov32 r0, 5748
  1903. mod32 r0, 92
  1904. mov32 r1, 13
  1905. mod32 r0, r1
  1906. exit",
  1907. )
  1908. .unwrap();
  1909. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1910. assert_eq!(vm.execute_program().unwrap(), 0x5);
  1911. }
  1912. #[test]
  1913. fn test_vm_mod32() {
  1914. let prog = assemble(
  1915. "
  1916. lddw r0, 0x100000003
  1917. mod32 r0, 3
  1918. exit",
  1919. )
  1920. .unwrap();
  1921. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1922. assert_eq!(vm.execute_program().unwrap(), 0x0);
  1923. }
  1924. #[test]
  1925. fn test_vm_mod64() {
  1926. let prog = assemble(
  1927. "
  1928. mov32 r0, -1316649930
  1929. lsh r0, 32
  1930. or r0, 0x100dc5c8
  1931. mov32 r1, 0xdde263e
  1932. lsh r1, 32
  1933. or r1, 0x3cbef7f3
  1934. mod r0, r1
  1935. mod r0, 0x658f1778
  1936. exit",
  1937. )
  1938. .unwrap();
  1939. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1940. assert_eq!(vm.execute_program().unwrap(), 0x30ba5a04);
  1941. }
  1942. #[test]
  1943. fn test_vm_mov() {
  1944. let prog = assemble(
  1945. "
  1946. mov32 r1, 1
  1947. mov32 r0, r1
  1948. exit",
  1949. )
  1950. .unwrap();
  1951. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1952. assert_eq!(vm.execute_program().unwrap(), 0x1);
  1953. }
  1954. #[test]
  1955. fn test_vm_mul32_imm() {
  1956. let prog = assemble(
  1957. "
  1958. mov r0, 3
  1959. mul32 r0, 4
  1960. exit",
  1961. )
  1962. .unwrap();
  1963. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1964. assert_eq!(vm.execute_program().unwrap(), 0xc);
  1965. }
  1966. #[test]
  1967. fn test_vm_mul32_reg() {
  1968. let prog = assemble(
  1969. "
  1970. mov r0, 3
  1971. mov r1, 4
  1972. mul32 r0, r1
  1973. exit",
  1974. )
  1975. .unwrap();
  1976. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1977. assert_eq!(vm.execute_program().unwrap(), 0xc);
  1978. }
  1979. #[test]
  1980. fn test_vm_mul32_reg_overflow() {
  1981. let prog = assemble(
  1982. "
  1983. mov r0, 0x40000001
  1984. mov r1, 4
  1985. mul32 r0, r1
  1986. exit",
  1987. )
  1988. .unwrap();
  1989. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  1990. assert_eq!(vm.execute_program().unwrap(), 0x4);
  1991. }
  1992. #[test]
  1993. fn test_vm_mul64_imm() {
  1994. let prog = assemble(
  1995. "
  1996. mov r0, 0x40000001
  1997. mul r0, 4
  1998. exit",
  1999. )
  2000. .unwrap();
  2001. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2002. assert_eq!(vm.execute_program().unwrap(), 0x100000004);
  2003. }
  2004. #[test]
  2005. fn test_vm_mul64_reg() {
  2006. let prog = assemble(
  2007. "
  2008. mov r0, 0x40000001
  2009. mov r1, 4
  2010. mul r0, r1
  2011. exit",
  2012. )
  2013. .unwrap();
  2014. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2015. assert_eq!(vm.execute_program().unwrap(), 0x100000004);
  2016. }
  2017. #[test]
  2018. fn test_vm_mul_loop() {
  2019. let prog = assemble(
  2020. "
  2021. mov r0, 0x7
  2022. add r1, 0xa
  2023. lsh r1, 0x20
  2024. rsh r1, 0x20
  2025. jeq r1, 0x0, +4
  2026. mov r0, 0x7
  2027. mul r0, 0x7
  2028. add r1, -1
  2029. jne r1, 0x0, -3
  2030. exit",
  2031. )
  2032. .unwrap();
  2033. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2034. assert_eq!(vm.execute_program().unwrap(), 0x75db9c97);
  2035. }
  2036. #[test]
  2037. fn test_vm_neg64() {
  2038. let prog = assemble(
  2039. "
  2040. mov32 r0, 2
  2041. neg r0
  2042. exit",
  2043. )
  2044. .unwrap();
  2045. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2046. assert_eq!(vm.execute_program().unwrap(), 0xfffffffffffffffe);
  2047. }
  2048. #[test]
  2049. fn test_vm_neg() {
  2050. let prog = assemble(
  2051. "
  2052. mov32 r0, 2
  2053. neg32 r0
  2054. exit",
  2055. )
  2056. .unwrap();
  2057. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2058. assert_eq!(vm.execute_program().unwrap(), 0xfffffffe);
  2059. }
  2060. #[test]
  2061. fn test_vm_prime() {
  2062. let prog = assemble(
  2063. "
  2064. mov r1, 67
  2065. mov r0, 0x1
  2066. mov r2, 0x2
  2067. jgt r1, 0x2, +4
  2068. ja +10
  2069. add r2, 0x1
  2070. mov r0, 0x1
  2071. jge r2, r1, +7
  2072. mov r3, r1
  2073. div r3, r2
  2074. mul r3, r2
  2075. mov r4, r1
  2076. sub r4, r3
  2077. mov r0, 0x0
  2078. jne r4, 0x0, -10
  2079. exit",
  2080. )
  2081. .unwrap();
  2082. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2083. assert_eq!(vm.execute_program().unwrap(), 0x1);
  2084. }
  2085. #[test]
  2086. fn test_vm_rhs32() {
  2087. let prog = assemble(
  2088. "
  2089. xor r0, r0
  2090. sub r0, 1
  2091. rsh32 r0, 8
  2092. exit",
  2093. )
  2094. .unwrap();
  2095. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2096. assert_eq!(vm.execute_program().unwrap(), 0x00ffffff);
  2097. }
  2098. #[test]
  2099. fn test_vm_rsh_reg() {
  2100. let prog = assemble(
  2101. "
  2102. mov r0, 0x10
  2103. mov r7, 4
  2104. rsh r0, r7
  2105. exit",
  2106. )
  2107. .unwrap();
  2108. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2109. assert_eq!(vm.execute_program().unwrap(), 0x1);
  2110. }
  2111. #[test]
  2112. fn test_vm_stack() {
  2113. let prog = assemble(
  2114. "
  2115. mov r1, 51
  2116. stdw [r10-16], 0xab
  2117. stdw [r10-8], 0xcd
  2118. and r1, 1
  2119. lsh r1, 3
  2120. mov r2, r10
  2121. add r2, r1
  2122. ldxdw r0, [r2-16]
  2123. exit",
  2124. )
  2125. .unwrap();
  2126. let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2127. assert_eq!(vm.execute_program().unwrap(), 0xcd);
  2128. }
  2129. #[test]
  2130. fn test_vm_stack2() {
  2131. let prog = assemble(
  2132. "
  2133. stb [r10-4], 0x01
  2134. stb [r10-3], 0x02
  2135. stb [r10-2], 0x03
  2136. stb [r10-1], 0x04
  2137. mov r1, r10
  2138. mov r2, 0x4
  2139. sub r1, r2
  2140. call 1
  2141. mov r1, 0
  2142. ldxb r2, [r10-4]
  2143. ldxb r3, [r10-3]
  2144. ldxb r4, [r10-2]
  2145. ldxb r5, [r10-1]
  2146. call 0
  2147. xor r0, 0x2a2a2a2a
  2148. exit",
  2149. )
  2150. .unwrap();
  2151. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2152. vm.register_helper(0, helpers::gather_bytes).unwrap();
  2153. vm.register_helper(1, helpers::memfrob).unwrap();
  2154. assert_eq!(vm.execute_program().unwrap(), 0x01020304);
  2155. }
  2156. #[test]
  2157. fn test_vm_stb() {
  2158. let prog = assemble(
  2159. "
  2160. stb [r1+2], 0x11
  2161. ldxb r0, [r1+2]
  2162. exit",
  2163. )
  2164. .unwrap();
  2165. let mem = &mut [0xaa, 0xbb, 0xff, 0xcc, 0xdd];
  2166. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2167. assert_eq!(vm.execute_program(mem).unwrap(), 0x11);
  2168. }
  2169. #[test]
  2170. fn test_vm_stdw() {
  2171. let prog = assemble(
  2172. "
  2173. stdw [r1+2], 0x44332211
  2174. ldxdw r0, [r1+2]
  2175. exit",
  2176. )
  2177. .unwrap();
  2178. let mem = &mut [
  2179. 0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd,
  2180. ];
  2181. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2182. assert_eq!(vm.execute_program(mem).unwrap(), 0x44332211);
  2183. }
  2184. // If this case is not handled properly in check_mem(), then we may overflow when adding the
  2185. // context address and the offset, and make the thread panic with "attempt to add with overflow".
  2186. // Check that we panic with the expected out-of-bounds error.
  2187. //
  2188. // The new toolchain introduced `assert_unsafe_precondition` which panics with a different message and can't be
  2189. // caught by `#[should_panic]`. This is why we use `#[ignore]` here.
  2190. #[test]
  2191. #[should_panic(expected = "Error: out of bounds memory store (insn #1)")]
  2192. #[ignore]
  2193. fn test_vm_stdw_add_overflow() {
  2194. let prog = assemble(
  2195. "
  2196. stdw [r2-0x1], 0x44332211
  2197. ldxw r0, [r1+2]
  2198. exit",
  2199. )
  2200. .unwrap();
  2201. let mem = &mut [
  2202. 0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd,
  2203. ];
  2204. let mut vm = rbpf::EbpfVmFixedMbuff::new(Some(&prog), 0x00, 0x10).unwrap();
  2205. _ = vm.execute_program(mem).unwrap();
  2206. }
  2207. #[test]
  2208. fn test_vm_sth() {
  2209. let prog = assemble(
  2210. "
  2211. sth [r1+2], 0x2211
  2212. ldxh r0, [r1+2]
  2213. exit",
  2214. )
  2215. .unwrap();
  2216. let mem = &mut [0xaa, 0xbb, 0xff, 0xff, 0xcc, 0xdd];
  2217. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2218. assert_eq!(vm.execute_program(mem).unwrap(), 0x2211);
  2219. }
  2220. #[test]
  2221. fn test_vm_string_stack() {
  2222. let prog = assemble(
  2223. "
  2224. mov r1, 0x78636261
  2225. stxw [r10-8], r1
  2226. mov r6, 0x0
  2227. stxb [r10-4], r6
  2228. stxb [r10-12], r6
  2229. mov r1, 0x79636261
  2230. stxw [r10-16], r1
  2231. mov r1, r10
  2232. add r1, -8
  2233. mov r2, r1
  2234. call 0x4
  2235. mov r1, r0
  2236. mov r0, 0x1
  2237. lsh r1, 0x20
  2238. rsh r1, 0x20
  2239. jne r1, 0x0, +11
  2240. mov r1, r10
  2241. add r1, -8
  2242. mov r2, r10
  2243. add r2, -16
  2244. call 0x4
  2245. mov r1, r0
  2246. lsh r1, 0x20
  2247. rsh r1, 0x20
  2248. mov r0, 0x1
  2249. jeq r1, r6, +1
  2250. mov r0, 0x0
  2251. exit",
  2252. )
  2253. .unwrap();
  2254. let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
  2255. vm.register_helper(4, helpers::strcmp).unwrap();
  2256. assert_eq!(vm.execute_program().unwrap(), 0x0);
  2257. }
  2258. #[test]
  2259. fn test_vm_stw() {
  2260. let prog = assemble(
  2261. "
  2262. stw [r1+2], 0x44332211
  2263. ldxw r0, [r1+2]
  2264. exit",
  2265. )
  2266. .unwrap();
  2267. let mem = &mut [0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd];
  2268. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2269. assert_eq!(vm.execute_program(mem).unwrap(), 0x44332211);
  2270. }
  2271. #[test]
  2272. fn test_vm_stxb() {
  2273. let prog = assemble(
  2274. "
  2275. mov32 r2, 0x11
  2276. stxb [r1+2], r2
  2277. ldxb r0, [r1+2]
  2278. exit",
  2279. )
  2280. .unwrap();
  2281. let mem = &mut [0xaa, 0xbb, 0xff, 0xcc, 0xdd];
  2282. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2283. assert_eq!(vm.execute_program(mem).unwrap(), 0x11);
  2284. }
  2285. #[test]
  2286. fn test_vm_stxb_all() {
  2287. let prog = assemble(
  2288. "
  2289. mov r0, 0xf0
  2290. mov r2, 0xf2
  2291. mov r3, 0xf3
  2292. mov r4, 0xf4
  2293. mov r5, 0xf5
  2294. mov r6, 0xf6
  2295. mov r7, 0xf7
  2296. mov r8, 0xf8
  2297. stxb [r1], r0
  2298. stxb [r1+1], r2
  2299. stxb [r1+2], r3
  2300. stxb [r1+3], r4
  2301. stxb [r1+4], r5
  2302. stxb [r1+5], r6
  2303. stxb [r1+6], r7
  2304. stxb [r1+7], r8
  2305. ldxdw r0, [r1]
  2306. be64 r0
  2307. exit",
  2308. )
  2309. .unwrap();
  2310. let mem = &mut [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
  2311. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2312. assert_eq!(vm.execute_program(mem).unwrap(), 0xf0f2f3f4f5f6f7f8);
  2313. }
  2314. #[test]
  2315. fn test_vm_stxb_all2() {
  2316. let prog = assemble(
  2317. "
  2318. mov r0, r1
  2319. mov r1, 0xf1
  2320. mov r9, 0xf9
  2321. stxb [r0], r1
  2322. stxb [r0+1], r9
  2323. ldxh r0, [r0]
  2324. be16 r0
  2325. exit",
  2326. )
  2327. .unwrap();
  2328. let mem = &mut [0xff, 0xff];
  2329. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2330. assert_eq!(vm.execute_program(mem).unwrap(), 0xf1f9);
  2331. }
  2332. #[test]
  2333. fn test_vm_stxb_chain() {
  2334. let prog = assemble(
  2335. "
  2336. mov r0, r1
  2337. ldxb r9, [r0+0]
  2338. stxb [r0+1], r9
  2339. ldxb r8, [r0+1]
  2340. stxb [r0+2], r8
  2341. ldxb r7, [r0+2]
  2342. stxb [r0+3], r7
  2343. ldxb r6, [r0+3]
  2344. stxb [r0+4], r6
  2345. ldxb r5, [r0+4]
  2346. stxb [r0+5], r5
  2347. ldxb r4, [r0+5]
  2348. stxb [r0+6], r4
  2349. ldxb r3, [r0+6]
  2350. stxb [r0+7], r3
  2351. ldxb r2, [r0+7]
  2352. stxb [r0+8], r2
  2353. ldxb r1, [r0+8]
  2354. stxb [r0+9], r1
  2355. ldxb r0, [r0+9]
  2356. exit",
  2357. )
  2358. .unwrap();
  2359. let mem = &mut [0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
  2360. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2361. assert_eq!(vm.execute_program(mem).unwrap(), 0x2a);
  2362. }
  2363. #[test]
  2364. fn test_vm_stxdw() {
  2365. let prog = assemble(
  2366. "
  2367. mov r2, -2005440939
  2368. lsh r2, 32
  2369. or r2, 0x44332211
  2370. stxdw [r1+2], r2
  2371. ldxdw r0, [r1+2]
  2372. exit",
  2373. )
  2374. .unwrap();
  2375. let mem = &mut [
  2376. 0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd,
  2377. ];
  2378. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2379. assert_eq!(vm.execute_program(mem).unwrap(), 0x8877665544332211);
  2380. }
  2381. #[test]
  2382. fn test_vm_stxh() {
  2383. let prog = assemble(
  2384. "
  2385. mov32 r2, 0x2211
  2386. stxh [r1+2], r2
  2387. ldxh r0, [r1+2]
  2388. exit",
  2389. )
  2390. .unwrap();
  2391. let mem = &mut [0xaa, 0xbb, 0xff, 0xff, 0xcc, 0xdd];
  2392. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2393. assert_eq!(vm.execute_program(mem).unwrap(), 0x2211);
  2394. }
  2395. #[test]
  2396. fn test_vm_stxw() {
  2397. let prog = assemble(
  2398. "
  2399. mov32 r2, 0x44332211
  2400. stxw [r1+2], r2
  2401. ldxw r0, [r1+2]
  2402. exit",
  2403. )
  2404. .unwrap();
  2405. let mem = &mut [0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd];
  2406. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2407. assert_eq!(vm.execute_program(mem).unwrap(), 0x44332211);
  2408. }
  2409. #[test]
  2410. fn test_vm_subnet() {
  2411. let prog = assemble(
  2412. "
  2413. mov r2, 0xe
  2414. ldxh r3, [r1+12]
  2415. jne r3, 0x81, +2
  2416. mov r2, 0x12
  2417. ldxh r3, [r1+16]
  2418. and r3, 0xffff
  2419. jne r3, 0x8, +5
  2420. add r1, r2
  2421. mov r0, 0x1
  2422. ldxw r1, [r1+16]
  2423. and r1, 0xffffff
  2424. jeq r1, 0x1a8c0, +1
  2425. mov r0, 0x0
  2426. exit",
  2427. )
  2428. .unwrap();
  2429. let mem = &mut [
  2430. 0x00, 0x00, 0xc0, 0x9f, 0xa0, 0x97, 0x00, 0xa0, 0xcc, 0x3b, 0xbf, 0xfa, 0x08, 0x00, 0x45,
  2431. 0x10, 0x00, 0x3c, 0x46, 0x3c, 0x40, 0x00, 0x40, 0x06, 0x73, 0x1c, 0xc0, 0xa8, 0x01, 0x02,
  2432. 0xc0, 0xa8, 0x01, 0x01, 0x06, 0x0e, 0x00, 0x17, 0x99, 0xc5, 0xa0, 0xec, 0x00, 0x00, 0x00,
  2433. 0x00, 0xa0, 0x02, 0x7d, 0x78, 0xe0, 0xa3, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4, 0x04, 0x02,
  2434. 0x08, 0x0a, 0x00, 0x9c, 0x27, 0x24, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x00,
  2435. ];
  2436. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2437. assert_eq!(vm.execute_program(mem).unwrap(), 0x1);
  2438. }
  2439. const PROG_TCP_PORT_80: [u8; 152] = [
  2440. 0x71, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x71, 0x13, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00,
  2441. 0x67, 0x03, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x4f, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2442. 0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x03, 0x0c, 0x00, 0x08, 0x00, 0x00, 0x00,
  2443. 0x71, 0x12, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x02, 0x0a, 0x00, 0x06, 0x00, 0x00, 0x00,
  2444. 0x71, 0x12, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x01, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00,
  2445. 0x57, 0x02, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x67, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  2446. 0x0f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x12, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  2447. 0x15, 0x02, 0x02, 0x00, 0x00, 0x50, 0x00, 0x00, 0x69, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2448. 0x55, 0x01, 0x01, 0x00, 0x00, 0x50, 0x00, 0x00, 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  2449. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2450. ];
  2451. #[test]
  2452. fn test_vm_tcp_port80_match() {
  2453. let mem = &mut [
  2454. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45,
  2455. 0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
  2456. 0xc0, 0xa8, 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2457. 0x00, 0x50, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2458. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2459. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2460. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2461. ];
  2462. let prog = &PROG_TCP_PORT_80;
  2463. let vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  2464. assert_eq!(vm.execute_program(mem).unwrap(), 0x1);
  2465. }
  2466. #[test]
  2467. fn test_vm_tcp_port80_nomatch() {
  2468. let mem = &mut [
  2469. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45,
  2470. 0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
  2471. 0xc0, 0xa8, 0x00, 0x02, 0x00, 0x16, 0x27, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2472. 0x00, 0x51, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2473. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2474. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2475. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2476. ];
  2477. let prog = &PROG_TCP_PORT_80;
  2478. let vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  2479. assert_eq!(vm.execute_program(mem).unwrap(), 0x0);
  2480. }
  2481. #[test]
  2482. fn test_vm_tcp_port80_nomatch_ethertype() {
  2483. let mem = &mut [
  2484. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x01, 0x45,
  2485. 0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
  2486. 0xc0, 0xa8, 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2487. 0x00, 0x50, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2488. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2489. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2490. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2491. ];
  2492. let prog = &PROG_TCP_PORT_80;
  2493. let vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  2494. assert_eq!(vm.execute_program(mem).unwrap(), 0x0);
  2495. }
  2496. #[test]
  2497. fn test_vm_tcp_port80_nomatch_proto() {
  2498. let mem = &mut [
  2499. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45,
  2500. 0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
  2501. 0xc0, 0xa8, 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2502. 0x00, 0x50, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2503. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2504. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2505. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  2506. ];
  2507. let prog = &PROG_TCP_PORT_80;
  2508. let vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
  2509. assert_eq!(vm.execute_program(mem).unwrap(), 0x0);
  2510. }
  2511. #[test]
  2512. fn test_vm_tcp_sack_match() {
  2513. let mut mem = TCP_SACK_MATCH.to_vec();
  2514. let prog = assemble(TCP_SACK_ASM).unwrap();
  2515. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2516. assert_eq!(vm.execute_program(mem.as_mut_slice()).unwrap(), 0x1);
  2517. }
  2518. #[test]
  2519. fn test_vm_tcp_sack_nomatch() {
  2520. let mut mem = TCP_SACK_NOMATCH.to_vec();
  2521. let prog = assemble(TCP_SACK_ASM).unwrap();
  2522. let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
  2523. assert_eq!(vm.execute_program(mem.as_mut_slice()).unwrap(), 0x0);
  2524. }