insn_builder.rs 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  1. // SPDX-License-Identifier: (Apache-2.0 OR MIT)
  2. // Copyright 2017 Alex Dukhno <alex.dukhno@icloud.com>
  3. //! Module provides API to create eBPF programs by Rust programming language
  4. use ebpf::*;
  5. /// Represents single eBPF instruction
  6. pub trait Instruction: Sized {
  7. /// returns instruction opt code
  8. fn opt_code_byte(&self) -> u8;
  9. /// returns destination register
  10. fn get_dst(&self) -> u8 {
  11. self.get_insn().dst
  12. }
  13. /// returns source register
  14. fn get_src(&self) -> u8 {
  15. self.get_insn().src
  16. }
  17. /// returns offset bytes
  18. fn get_off(&self) -> i16 {
  19. self.get_insn().off
  20. }
  21. /// returns immediate value
  22. fn get_imm(&self) -> i32 {
  23. self.get_insn().imm
  24. }
  25. /// sets destination register
  26. fn set_dst(mut self, dst: u8) -> Self {
  27. self.get_insn_mut().dst = dst;
  28. self
  29. }
  30. /// sets source register
  31. fn set_src(mut self, src: u8) -> Self {
  32. self.get_insn_mut().src = src;
  33. self
  34. }
  35. /// sets offset bytes
  36. fn set_off(mut self, offset: i16) -> Self {
  37. self.get_insn_mut().off = offset;
  38. self
  39. }
  40. /// sets immediate value
  41. fn set_imm(mut self, imm: i32) -> Self {
  42. self.get_insn_mut().imm = imm;
  43. self
  44. }
  45. /// get `ebpf::Insn` struct
  46. fn get_insn(&self) -> &Insn;
  47. /// get mutable `ebpf::Insn` struct
  48. fn get_insn_mut(&mut self) -> &mut Insn;
  49. }
  50. /// General trait for `Instruction`s and `BpfCode`.
  51. /// Provides functionality to transform `struct` into collection of bytes
  52. pub trait IntoBytes {
  53. /// type of targeted transformation
  54. type Bytes;
  55. /// consume `Self` with transformation into `Self::Bytes`
  56. fn into_bytes(self) -> Self::Bytes;
  57. }
  58. /// General implementation of `IntoBytes` for `Instruction`
  59. impl<'i, I: Instruction> IntoBytes for &'i I {
  60. type Bytes = Vec<u8>;
  61. /// transform immutable reference of `Instruction` into `Vec<u8>` with size of 8
  62. /// [ 1 byte , 1 byte , 2 bytes, 4 bytes ]
  63. /// [ OP_CODE, SRC_REG | DST_REG, OFFSET , IMMEDIATE ]
  64. fn into_bytes(self) -> Self::Bytes {
  65. let mut buffer = vec![
  66. self.opt_code_byte(),
  67. self.get_src() << 4 | self.get_dst(),
  68. self.get_off() as u8,
  69. (self.get_off() >> 8) as u8,
  70. self.get_imm() as u8,
  71. (self.get_imm() >> 8) as u8,
  72. (self.get_imm() >> 16) as u8,
  73. (self.get_imm() >> 24) as u8,
  74. ];
  75. buffer
  76. }
  77. }
  78. /// BPF instruction stack in byte representation
  79. #[derive(Default)]
  80. pub struct BpfCode {
  81. instructions: Vec<u8>
  82. }
  83. impl BpfCode {
  84. /// creates new empty BPF instruction stack
  85. pub fn new() -> Self {
  86. BpfCode { instructions: vec![] }
  87. }
  88. /// create ADD instruction
  89. pub fn add(&mut self, source: Source, arch: Arch) -> Move {
  90. self.mov_internal(source, arch, OpBits::Add)
  91. }
  92. /// create SUB instruction
  93. pub fn sub(&mut self, source: Source, arch: Arch) -> Move {
  94. self.mov_internal(source, arch, OpBits::Sub)
  95. }
  96. /// create MUL instruction
  97. pub fn mul(&mut self, source: Source, arch: Arch) -> Move {
  98. self.mov_internal(source, arch, OpBits::Mul)
  99. }
  100. /// create DIV instruction
  101. pub fn div(&mut self, source: Source, arch: Arch) -> Move {
  102. self.mov_internal(source, arch, OpBits::Div)
  103. }
  104. /// create OR instruction
  105. pub fn bit_or(&mut self, source: Source, arch: Arch) -> Move {
  106. self.mov_internal(source, arch, OpBits::BitOr)
  107. }
  108. /// create AND instruction
  109. pub fn bit_and(&mut self, source: Source, arch: Arch) -> Move {
  110. self.mov_internal(source, arch, OpBits::BitAnd)
  111. }
  112. /// create LSHIFT instruction
  113. pub fn left_shift(&mut self, source: Source, arch: Arch) -> Move {
  114. self.mov_internal(source, arch, OpBits::LShift)
  115. }
  116. /// create RSHIFT instruction
  117. pub fn right_shift(&mut self, source: Source, arch: Arch) -> Move {
  118. self.mov_internal(source, arch, OpBits::RShift)
  119. }
  120. /// create NEGATE instruction
  121. pub fn negate(&mut self, arch: Arch) -> Move {
  122. self.mov_internal(Source::Imm, arch, OpBits::Negate)
  123. }
  124. /// create MOD instruction
  125. pub fn modulo(&mut self, source: Source, arch: Arch) -> Move {
  126. self.mov_internal(source, arch, OpBits::Mod)
  127. }
  128. /// create XOR instruction
  129. pub fn bit_xor(&mut self, source: Source, arch: Arch) -> Move {
  130. self.mov_internal(source, arch, OpBits::BitXor)
  131. }
  132. /// create MOV instruction
  133. pub fn mov(&mut self, source: Source, arch: Arch) -> Move {
  134. self.mov_internal(source, arch, OpBits::Mov)
  135. }
  136. /// create SIGNED RSHIFT instruction
  137. pub fn signed_right_shift(&mut self, source: Source, arch: Arch) -> Move {
  138. self.mov_internal(source, arch, OpBits::SignRShift)
  139. }
  140. #[inline]
  141. fn mov_internal(&mut self, source: Source, arch_bits: Arch, op_bits: OpBits) -> Move {
  142. Move {
  143. bpf_code: self,
  144. src_bit: source,
  145. op_bits: op_bits,
  146. arch_bits: arch_bits,
  147. insn: Insn {
  148. opc: 0x00,
  149. dst: 0x00,
  150. src: 0x00,
  151. off: 0x00_00,
  152. imm: 0x00_00_00_00
  153. }
  154. }
  155. }
  156. /// create byte swap instruction
  157. pub fn swap_bytes(&mut self, endian: Endian) -> SwapBytes {
  158. SwapBytes {
  159. bpf_code: self,
  160. endian: endian,
  161. insn: Insn {
  162. opc: 0x00,
  163. dst: 0x00,
  164. src: 0x00,
  165. off: 0x00_00,
  166. imm: 0x00_00_00_00
  167. }
  168. }
  169. }
  170. /// create LOAD instruction, IMMEDIATE is the source
  171. pub fn load(&mut self, mem_size: MemSize) -> Load {
  172. self.load_internal(mem_size, Addressing::Imm, BPF_LD)
  173. }
  174. /// create ABSOLUTE LOAD instruction
  175. pub fn load_abs(&mut self, mem_size: MemSize) -> Load {
  176. self.load_internal(mem_size, Addressing::Abs, BPF_LD)
  177. }
  178. /// create INDIRECT LOAD instruction
  179. pub fn load_ind(&mut self, mem_size: MemSize) -> Load {
  180. self.load_internal(mem_size, Addressing::Ind, BPF_LD)
  181. }
  182. /// create LOAD instruction, MEMORY is the source
  183. pub fn load_x(&mut self, mem_size: MemSize) -> Load {
  184. self.load_internal(mem_size, Addressing::Mem, BPF_LDX)
  185. }
  186. #[inline]
  187. fn load_internal(&mut self, mem_size: MemSize, addressing: Addressing, source: u8) -> Load {
  188. Load {
  189. bpf_code: self,
  190. addressing: addressing,
  191. mem_size: mem_size,
  192. source: source,
  193. insn: Insn {
  194. opc: 0x00,
  195. dst: 0x00,
  196. src: 0x00,
  197. off: 0x00_00,
  198. imm: 0x00_00_00_00
  199. }
  200. }
  201. }
  202. /// creates STORE instruction, IMMEDIATE is the source
  203. pub fn store(&mut self, mem_size: MemSize) -> Store {
  204. self.store_internal(mem_size, BPF_IMM)
  205. }
  206. /// creates STORE instruction, MEMORY is the source
  207. pub fn store_x(&mut self, mem_size: MemSize) -> Store {
  208. self.store_internal(mem_size, BPF_MEM | BPF_STX)
  209. }
  210. #[inline]
  211. fn store_internal(&mut self, mem_size: MemSize, source: u8) -> Store {
  212. Store {
  213. bpf_code: self,
  214. mem_size: mem_size,
  215. source: source,
  216. insn: Insn {
  217. opc: 0x00,
  218. dst: 0x00,
  219. src: 0x00,
  220. off: 0x00_00,
  221. imm: 0x00_00_00_00
  222. }
  223. }
  224. }
  225. /// create unconditional JMP instruction
  226. pub fn jump_unconditional(&mut self) -> Jump {
  227. self.jump_conditional(Cond::Abs, Source::Imm)
  228. }
  229. /// create conditional JMP instruction
  230. pub fn jump_conditional(&mut self, cond: Cond, src_bit: Source) -> Jump {
  231. Jump {
  232. bpf_code: self,
  233. cond: cond,
  234. src_bit: src_bit,
  235. insn: Insn {
  236. opc: 0x00,
  237. dst: 0x00,
  238. src: 0x00,
  239. off: 0x00_00,
  240. imm: 0x00_00_00_00
  241. }
  242. }
  243. }
  244. /// create CALL instruction
  245. pub fn call(&mut self) -> FunctionCall {
  246. FunctionCall {
  247. bpf_code: self,
  248. insn: Insn {
  249. opc: 0x00,
  250. dst: 0x00,
  251. src: 0x00,
  252. off: 0x00_00,
  253. imm: 0x00_00_00_00
  254. }
  255. }
  256. }
  257. /// create EXIT instruction
  258. pub fn exit(&mut self) -> Exit {
  259. Exit {
  260. bpf_code: self,
  261. insn: Insn {
  262. opc: 0x00,
  263. dst: 0x00,
  264. src: 0x00,
  265. off: 0x00_00,
  266. imm: 0x00_00_00_00
  267. }
  268. }
  269. }
  270. }
  271. /// Transform `BpfCode` into assemble representation
  272. impl<'a> IntoBytes for &'a BpfCode {
  273. type Bytes = &'a [u8];
  274. /// returns `BpfCode` instruction stack as `&[u8]`
  275. fn into_bytes(self) -> Self::Bytes {
  276. self.instructions.as_slice()
  277. }
  278. }
  279. /// struct to represent `MOV ALU` instructions
  280. pub struct Move<'i> {
  281. bpf_code: &'i mut BpfCode,
  282. src_bit: Source,
  283. op_bits: OpBits,
  284. arch_bits: Arch,
  285. insn: Insn
  286. }
  287. impl<'i> Move<'i> {
  288. /// push MOV instruction into BpfCode instruction stack
  289. pub fn push(mut self) -> &'i mut BpfCode {
  290. let mut asm = self.into_bytes();
  291. self.bpf_code.instructions.append(&mut asm);
  292. self.bpf_code
  293. }
  294. }
  295. impl<'i> Instruction for Move<'i> {
  296. fn opt_code_byte(&self) -> u8 {
  297. let op_bits = self.op_bits as u8;
  298. let src_bit = self.src_bit as u8;
  299. let arch_bits = self.arch_bits as u8;
  300. op_bits | src_bit | arch_bits
  301. }
  302. fn get_insn_mut(&mut self) -> &mut Insn {
  303. &mut self.insn
  304. }
  305. fn get_insn(&self) -> &Insn {
  306. &self.insn
  307. }
  308. }
  309. #[derive(Copy, Clone, PartialEq, Eq)]
  310. /// The source of ALU and JMP instructions
  311. pub enum Source {
  312. /// immediate field will be used as a source
  313. Imm = BPF_IMM as isize,
  314. /// src register will be used as a source
  315. Reg = BPF_X as isize
  316. }
  317. #[derive(Copy, Clone)]
  318. enum OpBits {
  319. Add = BPF_ADD as isize,
  320. Sub = BPF_SUB as isize,
  321. Mul = BPF_MUL as isize,
  322. Div = BPF_DIV as isize,
  323. BitOr = BPF_OR as isize,
  324. BitAnd = BPF_AND as isize,
  325. LShift = BPF_LSH as isize,
  326. RShift = BPF_RSH as isize,
  327. Negate = BPF_NEG as isize,
  328. Mod = BPF_MOD as isize,
  329. BitXor = BPF_XOR as isize,
  330. Mov = BPF_MOV as isize,
  331. SignRShift = BPF_ARSH as isize
  332. }
  333. #[derive(Copy, Clone)]
  334. /// Architecture of instructions
  335. pub enum Arch {
  336. /// 64-bit instructions
  337. X64 = BPF_ALU64 as isize,
  338. /// 32-bit instructions
  339. X32 = BPF_ALU as isize
  340. }
  341. /// struct representation of byte swap operation
  342. pub struct SwapBytes<'i> {
  343. bpf_code: &'i mut BpfCode,
  344. endian: Endian,
  345. insn: Insn
  346. }
  347. impl<'i> SwapBytes<'i> {
  348. /// push bytes swap instruction into BpfCode instruction stack
  349. pub fn push(mut self) -> &'i mut BpfCode {
  350. let mut asm = self.into_bytes();
  351. self.bpf_code.instructions.append(&mut asm);
  352. self.bpf_code
  353. }
  354. }
  355. impl<'i> Instruction for SwapBytes<'i> {
  356. fn opt_code_byte(&self) -> u8 {
  357. self.endian as u8
  358. }
  359. fn get_insn_mut(&mut self) -> &mut Insn {
  360. &mut self.insn
  361. }
  362. fn get_insn(&self) -> &Insn {
  363. &self.insn
  364. }
  365. }
  366. #[derive(Copy, Clone)]
  367. /// Bytes endian
  368. pub enum Endian {
  369. /// Little endian
  370. Little = LE as isize,
  371. /// Big endian
  372. Big = BE as isize
  373. }
  374. /// struct representation of LOAD instructions
  375. pub struct Load<'i> {
  376. bpf_code: &'i mut BpfCode,
  377. addressing: Addressing,
  378. mem_size: MemSize,
  379. source: u8,
  380. insn: Insn
  381. }
  382. impl<'i> Load<'i> {
  383. /// push LOAD instruction into BpfCode instruction stack
  384. pub fn push(mut self) -> &'i mut BpfCode {
  385. let mut asm = self.into_bytes();
  386. self.bpf_code.instructions.append(&mut asm);
  387. self.bpf_code
  388. }
  389. }
  390. impl<'i> Instruction for Load<'i> {
  391. fn opt_code_byte(&self) -> u8 {
  392. let size = self.mem_size as u8;
  393. let addressing = self.addressing as u8;
  394. addressing | size | self.source
  395. }
  396. fn get_insn_mut(&mut self) -> &mut Insn {
  397. &mut self.insn
  398. }
  399. fn get_insn(&self) -> &Insn {
  400. &self.insn
  401. }
  402. }
  403. /// struct representation of STORE instructions
  404. pub struct Store<'i> {
  405. bpf_code: &'i mut BpfCode,
  406. mem_size: MemSize,
  407. source: u8,
  408. insn: Insn
  409. }
  410. impl<'i> Store<'i> {
  411. /// push STORE instruction into BpfCode instruction stack
  412. pub fn push(mut self) -> &'i mut BpfCode {
  413. let mut asm = self.into_bytes();
  414. self.bpf_code.instructions.append(&mut asm);
  415. self.bpf_code
  416. }
  417. }
  418. impl<'i> Instruction for Store<'i> {
  419. fn opt_code_byte(&self) -> u8 {
  420. let size = self.mem_size as u8;
  421. BPF_MEM | BPF_ST | size | self.source
  422. }
  423. fn get_insn_mut(&mut self) -> &mut Insn {
  424. &mut self.insn
  425. }
  426. fn get_insn(&self) -> &Insn {
  427. &self.insn
  428. }
  429. }
  430. #[derive(Copy, Clone)]
  431. /// Memory size for LOAD and STORE instructions
  432. pub enum MemSize {
  433. /// 8-bit size
  434. Byte = BPF_B as isize,
  435. /// 16-bit size
  436. HalfWord = BPF_H as isize,
  437. /// 32-bit size
  438. Word = BPF_W as isize,
  439. /// 64-bit size
  440. DoubleWord = BPF_DW as isize
  441. }
  442. #[derive(Copy, Clone)]
  443. enum Addressing {
  444. Imm = BPF_IMM as isize,
  445. Abs = BPF_ABS as isize,
  446. Ind = BPF_IND as isize,
  447. Mem = BPF_MEM as isize
  448. }
  449. /// struct representation of JMP instructions
  450. pub struct Jump<'i> {
  451. bpf_code: &'i mut BpfCode,
  452. cond: Cond,
  453. src_bit: Source,
  454. insn: Insn
  455. }
  456. impl<'i> Jump<'i> {
  457. /// push JMP instruction into BpfCode instruction stack
  458. pub fn push(mut self) -> &'i mut BpfCode {
  459. let mut asm = self.into_bytes();
  460. self.bpf_code.instructions.append(&mut asm);
  461. self.bpf_code
  462. }
  463. }
  464. impl<'i> Instruction for Jump<'i> {
  465. fn opt_code_byte(&self) -> u8 {
  466. let cmp: u8 = self.cond as u8;
  467. let src_bit = self.src_bit as u8;
  468. cmp | src_bit | BPF_JMP
  469. }
  470. fn get_insn_mut(&mut self) -> &mut Insn {
  471. &mut self.insn
  472. }
  473. fn get_insn(&self) -> &Insn {
  474. &self.insn
  475. }
  476. }
  477. #[derive(Copy, Clone, PartialEq, Eq)]
  478. /// Conditions for JMP instructions
  479. pub enum Cond {
  480. /// Absolute or unconditional
  481. Abs = BPF_JA as isize,
  482. /// Jump if `==`
  483. Equals = BPF_JEQ as isize,
  484. /// Jump if `>`
  485. Greater = BPF_JGT as isize,
  486. /// Jump if `>=`
  487. GreaterEquals = BPF_JGE as isize,
  488. /// Jump if `<`
  489. Lower = BPF_JLT as isize,
  490. /// Jump if `<=`
  491. LowerEquals = BPF_JLE as isize,
  492. /// Jump if `src` & `dst`
  493. BitAnd = BPF_JSET as isize,
  494. /// Jump if `!=`
  495. NotEquals = BPF_JNE as isize,
  496. /// Jump if `>` (signed)
  497. GreaterSigned = BPF_JSGT as isize,
  498. /// Jump if `>=` (signed)
  499. GreaterEqualsSigned = BPF_JSGE as isize,
  500. /// Jump if `<` (signed)
  501. LowerSigned = BPF_JSLT as isize,
  502. /// Jump if `<=` (signed)
  503. LowerEqualsSigned = BPF_JSLE as isize
  504. }
  505. /// struct representation of CALL instruction
  506. pub struct FunctionCall<'i> {
  507. bpf_code: &'i mut BpfCode,
  508. insn: Insn
  509. }
  510. impl<'i> FunctionCall<'i> {
  511. /// push CALL instruction into BpfCode instruction stack
  512. pub fn push(mut self) -> &'i mut BpfCode {
  513. let mut asm = self.into_bytes();
  514. self.bpf_code.instructions.append(&mut asm);
  515. self.bpf_code
  516. }
  517. }
  518. impl<'i> Instruction for FunctionCall<'i> {
  519. fn opt_code_byte(&self) -> u8 {
  520. BPF_CALL | BPF_JMP
  521. }
  522. fn get_insn_mut(&mut self) -> &mut Insn {
  523. &mut self.insn
  524. }
  525. fn get_insn(&self) -> &Insn {
  526. &self.insn
  527. }
  528. }
  529. /// struct representation of EXIT instruction
  530. pub struct Exit<'i> {
  531. bpf_code: &'i mut BpfCode,
  532. insn: Insn
  533. }
  534. impl<'i> Exit<'i> {
  535. /// push EXIT instruction into BpfCode instruction stack
  536. pub fn push(mut self) -> &'i mut BpfCode {
  537. let mut asm = self.into_bytes();
  538. self.bpf_code.instructions.append(&mut asm);
  539. self.bpf_code
  540. }
  541. }
  542. impl<'i> Instruction for Exit<'i> {
  543. fn opt_code_byte(&self) -> u8 {
  544. BPF_EXIT | BPF_JMP
  545. }
  546. fn get_insn_mut(&mut self) -> &mut Insn {
  547. &mut self.insn
  548. }
  549. fn get_insn(&self) -> &Insn {
  550. &self.insn
  551. }
  552. }
  553. #[cfg(test)]
  554. mod tests {
  555. #[cfg(test)]
  556. mod special {
  557. use super::super::*;
  558. #[test]
  559. fn call_immediate() {
  560. let mut program = BpfCode::new();
  561. program.call().set_imm(0x11_22_33_44).push();
  562. assert_eq!(program.into_bytes(), &[0x85, 0x00, 0x00, 0x00, 0x44, 0x33, 0x22, 0x11]);
  563. }
  564. #[test]
  565. fn exit_operation() {
  566. let mut program = BpfCode::new();
  567. program.exit().push();
  568. assert_eq!(program.into_bytes(), &[0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  569. }
  570. }
  571. #[cfg(test)]
  572. mod jump_instructions {
  573. #[cfg(test)]
  574. mod register {
  575. use super::super::super::*;
  576. #[test]
  577. fn jump_on_dst_equals_src() {
  578. let mut program = BpfCode::new();
  579. program.jump_conditional(Cond::Equals, Source::Reg).set_dst(0x01).set_src(0x02).push();
  580. assert_eq!(program.into_bytes(), &[0x1d, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  581. }
  582. #[test]
  583. fn jump_on_dst_greater_than_src() {
  584. let mut program = BpfCode::new();
  585. program.jump_conditional(Cond::Greater, Source::Reg).set_dst(0x03).set_src(0x02).push();
  586. assert_eq!(program.into_bytes(), &[0x2d, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  587. }
  588. #[test]
  589. fn jump_on_dst_greater_or_equals_to_src() {
  590. let mut program = BpfCode::new();
  591. program.jump_conditional(Cond::GreaterEquals, Source::Reg).set_dst(0x04).set_src(0x01).push();
  592. assert_eq!(program.into_bytes(), &[0x3d, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  593. }
  594. #[test]
  595. fn jump_on_dst_lower_than_src() {
  596. let mut program = BpfCode::new();
  597. program.jump_conditional(Cond::Lower, Source::Reg).set_dst(0x03).set_src(0x02).push();
  598. assert_eq!(program.into_bytes(), &[0xad, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  599. }
  600. #[test]
  601. fn jump_on_dst_lower_or_equals_to_src() {
  602. let mut program = BpfCode::new();
  603. program.jump_conditional(Cond::LowerEquals, Source::Reg).set_dst(0x04).set_src(0x01).push();
  604. assert_eq!(program.into_bytes(), &[0xbd, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  605. }
  606. #[test]
  607. fn jump_on_dst_bit_and_with_src_not_equal_zero() {
  608. let mut program = BpfCode::new();
  609. program.jump_conditional(Cond::BitAnd, Source::Reg).set_dst(0x05).set_src(0x02).push();
  610. assert_eq!(program.into_bytes(), &[0x4d, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  611. }
  612. #[test]
  613. fn jump_on_dst_not_equals_src() {
  614. let mut program = BpfCode::new();
  615. program.jump_conditional(Cond::NotEquals, Source::Reg).set_dst(0x03).set_src(0x05).push();
  616. assert_eq!(program.into_bytes(), &[0x5d, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  617. }
  618. #[test]
  619. fn jump_on_dst_greater_than_src_signed() {
  620. let mut program = BpfCode::new();
  621. program.jump_conditional(Cond::GreaterSigned, Source::Reg).set_dst(0x04).set_src(0x01).push();
  622. assert_eq!(program.into_bytes(), &[0x6d, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  623. }
  624. #[test]
  625. fn jump_on_dst_greater_or_equals_src_signed() {
  626. let mut program = BpfCode::new();
  627. program.jump_conditional(Cond::GreaterEqualsSigned, Source::Reg).set_dst(0x01).set_src(0x03).push();
  628. assert_eq!(program.into_bytes(), &[0x7d, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  629. }
  630. #[test]
  631. fn jump_on_dst_lower_than_src_signed() {
  632. let mut program = BpfCode::new();
  633. program.jump_conditional(Cond::LowerSigned, Source::Reg).set_dst(0x04).set_src(0x01).push();
  634. assert_eq!(program.into_bytes(), &[0xcd, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  635. }
  636. #[test]
  637. fn jump_on_dst_lower_or_equals_src_signed() {
  638. let mut program = BpfCode::new();
  639. program.jump_conditional(Cond::LowerEqualsSigned, Source::Reg).set_dst(0x01).set_src(0x03).push();
  640. assert_eq!(program.into_bytes(), &[0xdd, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  641. }
  642. }
  643. #[cfg(test)]
  644. mod immediate {
  645. use super::super::super::*;
  646. #[test]
  647. fn jump_to_label() {
  648. let mut program = BpfCode::new();
  649. program.jump_unconditional().set_off(0x00_11).push();
  650. assert_eq!(program.into_bytes(), &[0x05, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00]);
  651. }
  652. #[test]
  653. fn jump_on_dst_equals_const() {
  654. let mut program = BpfCode::new();
  655. program.jump_conditional(Cond::Equals, Source::Imm).set_dst(0x01).set_imm(0x00_11_22_33).push();
  656. assert_eq!(program.into_bytes(), &[0x15, 0x01, 0x00, 0x00, 0x33, 0x22, 0x11, 0x00]);
  657. }
  658. #[test]
  659. fn jump_on_dst_greater_than_const() {
  660. let mut program = BpfCode::new();
  661. program.jump_conditional(Cond::Greater, Source::Imm).set_dst(0x02).set_imm(0x00_11_00_11).push();
  662. assert_eq!(program.into_bytes(), &[0x25, 0x02, 0x00, 0x00, 0x11, 0x00, 0x11, 0x00]);
  663. }
  664. #[test]
  665. fn jump_on_dst_greater_or_equals_to_const() {
  666. let mut program = BpfCode::new();
  667. program.jump_conditional(Cond::GreaterEquals, Source::Imm).set_dst(0x04).set_imm(0x00_22_11_00).push();
  668. assert_eq!(program.into_bytes(), &[0x35, 0x04, 0x00, 0x00, 0x00, 0x11, 0x22, 0x00]);
  669. }
  670. #[test]
  671. fn jump_on_dst_lower_than_const() {
  672. let mut program = BpfCode::new();
  673. program.jump_conditional(Cond::Lower, Source::Imm).set_dst(0x02).set_imm(0x00_11_00_11).push();
  674. assert_eq!(program.into_bytes(), &[0xa5, 0x02, 0x00, 0x00, 0x11, 0x00, 0x11, 0x00]);
  675. }
  676. #[test]
  677. fn jump_on_dst_lower_or_equals_to_const() {
  678. let mut program = BpfCode::new();
  679. program.jump_conditional(Cond::LowerEquals, Source::Imm).set_dst(0x04).set_imm(0x00_22_11_00).push();
  680. assert_eq!(program.into_bytes(), &[0xb5, 0x04, 0x00, 0x00, 0x00, 0x11, 0x22, 0x00]);
  681. }
  682. #[test]
  683. fn jump_on_dst_bit_and_with_const_not_equal_zero() {
  684. let mut program = BpfCode::new();
  685. program.jump_conditional(Cond::BitAnd, Source::Imm).set_dst(0x05).push();
  686. assert_eq!(program.into_bytes(), &[0x45, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  687. }
  688. #[test]
  689. fn jump_on_dst_not_equals_const() {
  690. let mut program = BpfCode::new();
  691. program.jump_conditional(Cond::NotEquals, Source::Imm).set_dst(0x03).push();
  692. assert_eq!(program.into_bytes(), &[0x55, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  693. }
  694. #[test]
  695. fn jump_on_dst_greater_than_const_signed() {
  696. let mut program = BpfCode::new();
  697. program.jump_conditional(Cond::GreaterSigned, Source::Imm).set_dst(0x04).push();
  698. assert_eq!(program.into_bytes(), &[0x65, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  699. }
  700. #[test]
  701. fn jump_on_dst_greater_or_equals_src_signed() {
  702. let mut program = BpfCode::new();
  703. program.jump_conditional(Cond::GreaterEqualsSigned, Source::Imm).set_dst(0x01).push();
  704. assert_eq!(program.into_bytes(), &[0x75, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  705. }
  706. #[test]
  707. fn jump_on_dst_lower_than_const_signed() {
  708. let mut program = BpfCode::new();
  709. program.jump_conditional(Cond::LowerSigned, Source::Imm).set_dst(0x04).push();
  710. assert_eq!(program.into_bytes(), &[0xc5, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  711. }
  712. #[test]
  713. fn jump_on_dst_lower_or_equals_src_signed() {
  714. let mut program = BpfCode::new();
  715. program.jump_conditional(Cond::LowerEqualsSigned, Source::Imm).set_dst(0x01).push();
  716. assert_eq!(program.into_bytes(), &[0xd5, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  717. }
  718. }
  719. }
  720. #[cfg(test)]
  721. mod store_instructions {
  722. use super::super::*;
  723. #[test]
  724. fn store_word_from_dst_into_immediate_address() {
  725. let mut program = BpfCode::new();
  726. program.store(MemSize::Word).set_dst(0x01).set_off(0x00_11).set_imm(0x11_22_33_44).push();
  727. assert_eq!(program.into_bytes(), &[0x62, 0x01, 0x11, 0x00, 0x44, 0x33, 0x22, 0x11]);
  728. }
  729. #[test]
  730. fn store_half_word_from_dst_into_immediate_address() {
  731. let mut program = BpfCode::new();
  732. program.store(MemSize::HalfWord).set_dst(0x02).set_off(0x11_22).push();
  733. assert_eq!(program.into_bytes(), &[0x6a, 0x02, 0x22, 0x11, 0x00, 0x00, 0x00, 0x00]);
  734. }
  735. #[test]
  736. fn store_byte_from_dst_into_immediate_address() {
  737. let mut program = BpfCode::new();
  738. program.store(MemSize::Byte).push();
  739. assert_eq!(program.into_bytes(), &[0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  740. }
  741. #[test]
  742. fn store_double_word_from_dst_into_immediate_address() {
  743. let mut program = BpfCode::new();
  744. program.store(MemSize::DoubleWord).push();
  745. assert_eq!(program.into_bytes(), &[0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  746. }
  747. #[test]
  748. fn store_word_from_dst_into_src_address() {
  749. let mut program = BpfCode::new();
  750. program.store_x(MemSize::Word).set_dst(0x01).set_src(0x02).push();
  751. assert_eq!(program.into_bytes(), &[0x63, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  752. }
  753. #[test]
  754. fn store_half_word_from_dst_into_src_address() {
  755. let mut program = BpfCode::new();
  756. program.store_x(MemSize::HalfWord).push();
  757. assert_eq!(program.into_bytes(), &[0x6b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  758. }
  759. #[test]
  760. fn store_byte_from_dst_into_src_address() {
  761. let mut program = BpfCode::new();
  762. program.store_x(MemSize::Byte).push();
  763. assert_eq!(program.into_bytes(), &[0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  764. }
  765. #[test]
  766. fn store_double_word_from_dst_into_src_address() {
  767. let mut program = BpfCode::new();
  768. program.store_x(MemSize::DoubleWord).push();
  769. assert_eq!(program.into_bytes(), &[0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  770. }
  771. }
  772. #[cfg(test)]
  773. mod load_instructions {
  774. #[cfg(test)]
  775. mod register {
  776. use super::super::super::*;
  777. #[test]
  778. fn load_word_from_set_src_with_offset() {
  779. let mut program = BpfCode::new();
  780. program.load_x(MemSize::Word).set_dst(0x01).set_src(0x02).set_off(0x00_02).push();
  781. assert_eq!(program.into_bytes(), &[0x61, 0x21, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00]);
  782. }
  783. #[test]
  784. fn load_half_word_from_set_src_with_offset() {
  785. let mut program = BpfCode::new();
  786. program.load_x(MemSize::HalfWord).set_dst(0x02).set_src(0x01).set_off(0x11_22).push();
  787. assert_eq!(program.into_bytes(), &[0x69, 0x12, 0x22, 0x11, 0x00, 0x00, 0x00, 0x00]);
  788. }
  789. #[test]
  790. fn load_byte_from_set_src_with_offset() {
  791. let mut program = BpfCode::new();
  792. program.load_x(MemSize::Byte).set_dst(0x01).set_src(0x04).set_off(0x00_11).push();
  793. assert_eq!(program.into_bytes(), &[0x71, 0x41, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00]);
  794. }
  795. #[test]
  796. fn load_double_word_from_set_src_with_offset() {
  797. let mut program = BpfCode::new();
  798. program.load_x(MemSize::DoubleWord).set_dst(0x04).set_src(0x05).set_off(0x44_55).push();
  799. assert_eq!(program.into_bytes(), &[0x79, 0x54, 0x55, 0x44, 0x00, 0x00, 0x00, 0x00]);
  800. }
  801. }
  802. #[cfg(test)]
  803. mod immediate {
  804. use super::super::super::*;
  805. #[test]
  806. fn load_double_word() {
  807. let mut program = BpfCode::new();
  808. program.load(MemSize::DoubleWord).set_dst(0x01).set_imm(0x00_01_02_03).push();
  809. assert_eq!(program.into_bytes(), &[0x18, 0x01, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00]);
  810. }
  811. #[test]
  812. fn load_abs_word() {
  813. let mut program = BpfCode::new();
  814. program.load_abs(MemSize::Word).push();
  815. assert_eq!(program.into_bytes(), &[0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  816. }
  817. #[test]
  818. fn load_abs_half_word() {
  819. let mut program = BpfCode::new();
  820. program.load_abs(MemSize::HalfWord).set_dst(0x05).push();
  821. assert_eq!(program.into_bytes(), &[0x28, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  822. }
  823. #[test]
  824. fn load_abs_byte() {
  825. let mut program = BpfCode::new();
  826. program.load_abs(MemSize::Byte).set_dst(0x01).push();
  827. assert_eq!(program.into_bytes(), &[0x30, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  828. }
  829. #[test]
  830. fn load_abs_double_word() {
  831. let mut program = BpfCode::new();
  832. program.load_abs(MemSize::DoubleWord).set_dst(0x01).set_imm(0x01_02_03_04).push();
  833. assert_eq!(program.into_bytes(), &[0x38, 0x01, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01]);
  834. }
  835. #[test]
  836. fn load_indirect_word() {
  837. let mut program = BpfCode::new();
  838. program.load_ind(MemSize::Word).push();
  839. assert_eq!(program.into_bytes(), &[0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  840. }
  841. #[test]
  842. fn load_indirect_half_word() {
  843. let mut program = BpfCode::new();
  844. program.load_ind(MemSize::HalfWord).push();
  845. assert_eq!(program.into_bytes(), &[0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  846. }
  847. #[test]
  848. fn load_indirect_byte() {
  849. let mut program = BpfCode::new();
  850. program.load_ind(MemSize::Byte).push();
  851. assert_eq!(program.into_bytes(), &[0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  852. }
  853. #[test]
  854. fn load_indirect_double_word() {
  855. let mut program = BpfCode::new();
  856. program.load_ind(MemSize::DoubleWord).push();
  857. assert_eq!(program.into_bytes(), &[0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  858. }
  859. }
  860. }
  861. #[cfg(test)]
  862. mod byte_swap_instructions {
  863. use super::super::*;
  864. #[test]
  865. fn convert_host_to_little_endian_16bits() {
  866. let mut program = BpfCode::new();
  867. program.swap_bytes(Endian::Little).set_dst(0x01).set_imm(0x00_00_00_10).push();
  868. assert_eq!(program.into_bytes(), &[0xd4, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00]);
  869. }
  870. #[test]
  871. fn convert_host_to_little_endian_32bits() {
  872. let mut program = BpfCode::new();
  873. program.swap_bytes(Endian::Little).set_dst(0x02).set_imm(0x00_00_00_20).push();
  874. assert_eq!(program.into_bytes(), &[0xd4, 0x02, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00]);
  875. }
  876. #[test]
  877. fn convert_host_to_little_endian_64bit() {
  878. let mut program = BpfCode::new();
  879. program.swap_bytes(Endian::Little).set_dst(0x03).set_imm(0x00_00_00_40).push();
  880. assert_eq!(program.into_bytes(), &[0xd4, 0x03, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00]);
  881. }
  882. #[test]
  883. fn convert_host_to_big_endian_16bits() {
  884. let mut program = BpfCode::new();
  885. program.swap_bytes(Endian::Big).set_dst(0x01).set_imm(0x00_00_00_10).push();
  886. assert_eq!(program.into_bytes(), &[0xdc, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00]);
  887. }
  888. #[test]
  889. fn convert_host_to_big_endian_32bits() {
  890. let mut program = BpfCode::new();
  891. program.swap_bytes(Endian::Big).set_dst(0x02).set_imm(0x00_00_00_20).push();
  892. assert_eq!(program.into_bytes(), &[0xdc, 0x02, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00]);
  893. }
  894. #[test]
  895. fn convert_host_to_big_endian_64bit() {
  896. let mut program = BpfCode::new();
  897. program.swap_bytes(Endian::Big).set_dst(0x03).set_imm(0x00_00_00_40).push();
  898. assert_eq!(program.into_bytes(), &[0xdc, 0x03, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00]);
  899. }
  900. }
  901. #[cfg(test)]
  902. mod moves_instructions {
  903. #[cfg(test)]
  904. mod arch_x64 {
  905. #[cfg(test)]
  906. mod immediate {
  907. use super::super::super::super::*;
  908. #[test]
  909. fn move_and_add_const_to_register() {
  910. let mut program = BpfCode::new();
  911. program.add(Source::Imm, Arch::X64).set_dst(0x02).set_imm(0x01_02_03_04).push();
  912. assert_eq!(program.into_bytes(), &[0x07, 0x02, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01]);
  913. }
  914. #[test]
  915. fn move_sub_const_to_register() {
  916. let mut program = BpfCode::new();
  917. program.sub(Source::Imm, Arch::X64).set_dst(0x04).set_imm(0x00_01_02_03).push();
  918. assert_eq!(program.into_bytes(), &[0x17, 0x04, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00]);
  919. }
  920. #[test]
  921. fn move_mul_const_to_register() {
  922. let mut program = BpfCode::new();
  923. program.mul(Source::Imm, Arch::X64).set_dst(0x05).set_imm(0x04_03_02_01).push();
  924. assert_eq!(program.into_bytes(), &[0x27, 0x05, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04]);
  925. }
  926. #[test]
  927. fn move_div_constant_to_register() {
  928. let mut program = BpfCode::new();
  929. program.div(Source::Imm, Arch::X64).set_dst(0x02).set_imm(0x00_ff_00_ff).push();
  930. assert_eq!(program.into_bytes(), &[0x37, 0x02, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00]);
  931. }
  932. #[test]
  933. fn move_bit_or_const_to_register() {
  934. let mut program = BpfCode::new();
  935. program.bit_or(Source::Imm, Arch::X64).set_dst(0x02).set_imm(0x00_11_00_22).push();
  936. assert_eq!(program.into_bytes(), &[0x47, 0x02, 0x00, 0x00, 0x22, 0x00, 0x11, 0x00]);
  937. }
  938. #[test]
  939. fn move_bit_and_const_to_register() {
  940. let mut program = BpfCode::new();
  941. program.bit_and(Source::Imm, Arch::X64).set_dst(0x02).set_imm(0x11_22_33_44).push();
  942. assert_eq!(program.into_bytes(), &[0x57, 0x02, 0x00, 0x00, 0x44, 0x33, 0x22, 0x11]);
  943. }
  944. #[test]
  945. fn move_left_shift_const_to_register() {
  946. let mut program = BpfCode::new();
  947. program.left_shift(Source::Imm, Arch::X64).set_dst(0x01).push();
  948. assert_eq!(program.into_bytes(), &[0x67, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  949. }
  950. #[test]
  951. fn move_logical_right_shift_const_to_register() {
  952. let mut program = BpfCode::new();
  953. program.right_shift(Source::Imm, Arch::X64).set_dst(0x01).push();
  954. assert_eq!(program.into_bytes(), &[0x77, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  955. }
  956. #[test]
  957. fn move_negate_register() {
  958. let mut program = BpfCode::new();
  959. program.negate(Arch::X64).set_dst(0x02).push();
  960. assert_eq!(program.into_bytes(), &[0x87, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  961. }
  962. #[test]
  963. fn move_mod_const_to_register() {
  964. let mut program = BpfCode::new();
  965. program.modulo(Source::Imm, Arch::X64).set_dst(0x02).push();
  966. assert_eq!(program.into_bytes(), &[0x97, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  967. }
  968. #[test]
  969. fn move_bit_xor_const_to_register() {
  970. let mut program = BpfCode::new();
  971. program.bit_xor(Source::Imm, Arch::X64).set_dst(0x03).push();
  972. assert_eq!(program.into_bytes(), &[0xa7, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  973. }
  974. #[test]
  975. fn move_const_to_register() {
  976. let mut program = BpfCode::new();
  977. program.mov(Source::Imm, Arch::X64).set_dst(0x01).set_imm(0x00_00_00_FF).push();
  978. assert_eq!(program.into_bytes(), &[0xb7, 0x01, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00]);
  979. }
  980. #[test]
  981. fn move_signed_right_shift_const_to_register() {
  982. let mut program = BpfCode::new();
  983. program.signed_right_shift(Source::Imm, Arch::X64).set_dst(0x05).push();
  984. assert_eq!(program.into_bytes(), &[0xc7, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  985. }
  986. }
  987. #[cfg(test)]
  988. mod register {
  989. use super::super::super::super::*;
  990. #[test]
  991. fn move_and_add_from_register() {
  992. let mut program = BpfCode::new();
  993. program.add(Source::Reg, Arch::X64).set_dst(0x03).set_src(0x02).push();
  994. assert_eq!(program.into_bytes(), &[0x0f, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  995. }
  996. #[test]
  997. fn move_sub_from_register_to_register() {
  998. let mut program = BpfCode::new();
  999. program.sub(Source::Reg, Arch::X64).set_dst(0x03).set_src(0x04).push();
  1000. assert_eq!(program.into_bytes(), &[0x1f, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1001. }
  1002. #[test]
  1003. fn move_mul_from_register_to_register() {
  1004. let mut program = BpfCode::new();
  1005. program.mul(Source::Reg, Arch::X64).set_dst(0x04).set_src(0x03).push();
  1006. assert_eq!(program.into_bytes(), &[0x2f, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1007. }
  1008. #[test]
  1009. fn move_div_from_register_to_register() {
  1010. let mut program = BpfCode::new();
  1011. program.div(Source::Reg, Arch::X64).set_dst(0x01).set_src(0x00).push();
  1012. assert_eq!(program.into_bytes(), &[0x3f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1013. }
  1014. #[test]
  1015. fn move_bit_or_from_register_to_register() {
  1016. let mut program = BpfCode::new();
  1017. program.bit_or(Source::Reg, Arch::X64).set_dst(0x03).set_src(0x01).push();
  1018. assert_eq!(program.into_bytes(), &[0x4f, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1019. }
  1020. #[test]
  1021. fn move_bit_and_from_register_to_register() {
  1022. let mut program = BpfCode::new();
  1023. program.bit_and(Source::Reg, Arch::X64).set_dst(0x03).set_src(0x02).push();
  1024. assert_eq!(program.into_bytes(), &[0x5f, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1025. }
  1026. #[test]
  1027. fn move_left_shift_from_register_to_register() {
  1028. let mut program = BpfCode::new();
  1029. program.left_shift(Source::Reg, Arch::X64).set_dst(0x02).set_src(0x03).push();
  1030. assert_eq!(program.into_bytes(), &[0x6f, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1031. }
  1032. #[test]
  1033. fn move_logical_right_shift_from_register_to_register() {
  1034. let mut program = BpfCode::new();
  1035. program.right_shift(Source::Reg, Arch::X64).set_dst(0x02).set_src(0x04).push();
  1036. assert_eq!(program.into_bytes(), &[0x7f, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1037. }
  1038. #[test]
  1039. fn move_mod_from_register_to_register() {
  1040. let mut program = BpfCode::new();
  1041. program.modulo(Source::Reg, Arch::X64).set_dst(0x01).set_src(0x02).push();
  1042. assert_eq!(program.into_bytes(), &[0x9f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1043. }
  1044. #[test]
  1045. fn move_bit_xor_from_register_to_register() {
  1046. let mut program = BpfCode::new();
  1047. program.bit_xor(Source::Reg, Arch::X64).set_dst(0x02).set_src(0x04).push();
  1048. assert_eq!(program.into_bytes(), &[0xaf, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1049. }
  1050. #[test]
  1051. fn move_from_register_to_another_register() {
  1052. let mut program = BpfCode::new();
  1053. program.mov(Source::Reg, Arch::X64).set_src(0x01).push();
  1054. assert_eq!(program.into_bytes(), &[0xbf, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1055. }
  1056. #[test]
  1057. fn move_signed_right_shift_from_register_to_register() {
  1058. let mut program = BpfCode::new();
  1059. program.signed_right_shift(Source::Reg, Arch::X64).set_dst(0x02).set_src(0x03).push();
  1060. assert_eq!(program.into_bytes(), &[0xcf, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1061. }
  1062. }
  1063. }
  1064. #[cfg(test)]
  1065. mod arch_x32 {
  1066. #[cfg(test)]
  1067. mod immediate {
  1068. use super::super::super::super::*;
  1069. #[test]
  1070. fn move_and_add_const_to_register() {
  1071. let mut program = BpfCode::new();
  1072. program.add(Source::Imm, Arch::X32).set_dst(0x02).set_imm(0x01_02_03_04).push();
  1073. assert_eq!(program.into_bytes(), &[0x04, 0x02, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01]);
  1074. }
  1075. #[test]
  1076. fn move_sub_const_to_register() {
  1077. let mut program = BpfCode::new();
  1078. program.sub(Source::Imm, Arch::X32).set_dst(0x04).set_imm(0x00_01_02_03).push();
  1079. assert_eq!(program.into_bytes(), &[0x14, 0x04, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00]);
  1080. }
  1081. #[test]
  1082. fn move_mul_const_to_register() {
  1083. let mut program = BpfCode::new();
  1084. program.mul(Source::Imm, Arch::X32).set_dst(0x05).set_imm(0x04_03_02_01).push();
  1085. assert_eq!(program.into_bytes(), &[0x24, 0x05, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04]);
  1086. }
  1087. #[test]
  1088. fn move_div_constant_to_register() {
  1089. let mut program = BpfCode::new();
  1090. program.div(Source::Imm, Arch::X32).set_dst(0x02).set_imm(0x00_ff_00_ff).push();
  1091. assert_eq!(program.into_bytes(), &[0x34, 0x02, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00]);
  1092. }
  1093. #[test]
  1094. fn move_bit_or_const_to_register() {
  1095. let mut program = BpfCode::new();
  1096. program.bit_or(Source::Imm, Arch::X32).set_dst(0x02).set_imm(0x00_11_00_22).push();
  1097. assert_eq!(program.into_bytes(), &[0x44, 0x02, 0x00, 0x00, 0x22, 0x00, 0x11, 0x00]);
  1098. }
  1099. #[test]
  1100. fn move_bit_and_const_to_register() {
  1101. let mut program = BpfCode::new();
  1102. program.bit_and(Source::Imm, Arch::X32).set_dst(0x02).set_imm(0x11_22_33_44).push();
  1103. assert_eq!(program.into_bytes(), &[0x54, 0x02, 0x00, 0x00, 0x44, 0x33, 0x22, 0x11]);
  1104. }
  1105. #[test]
  1106. fn move_left_shift_const_to_register() {
  1107. let mut program = BpfCode::new();
  1108. program.left_shift(Source::Imm, Arch::X32).set_dst(0x01).push();
  1109. assert_eq!(program.into_bytes(), &[0x64, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1110. }
  1111. #[test]
  1112. fn move_logical_right_shift_const_to_register() {
  1113. let mut program = BpfCode::new();
  1114. program.right_shift(Source::Imm, Arch::X32).set_dst(0x01).push();
  1115. assert_eq!(program.into_bytes(), &[0x74, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1116. }
  1117. #[test]
  1118. fn move_negate_register() {
  1119. let mut program = BpfCode::new();
  1120. program.negate(Arch::X32).set_dst(0x02).push();
  1121. assert_eq!(program.into_bytes(), &[0x84, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1122. }
  1123. #[test]
  1124. fn move_mod_const_to_register() {
  1125. let mut program = BpfCode::new();
  1126. program.modulo(Source::Imm, Arch::X32).set_dst(0x02).push();
  1127. assert_eq!(program.into_bytes(), &[0x94, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1128. }
  1129. #[test]
  1130. fn move_bit_xor_const_to_register() {
  1131. let mut program = BpfCode::new();
  1132. program.bit_xor(Source::Imm, Arch::X32).set_dst(0x03).push();
  1133. assert_eq!(program.into_bytes(), &[0xa4, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1134. }
  1135. #[test]
  1136. fn move_const_to_register() {
  1137. let mut program = BpfCode::new();
  1138. program.mov(Source::Imm, Arch::X32).set_dst(0x01).set_imm(0x00_00_00_FF).push();
  1139. assert_eq!(program.into_bytes(), &[0xb4, 0x01, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00]);
  1140. }
  1141. #[test]
  1142. fn move_signed_right_shift_const_to_register() {
  1143. let mut program = BpfCode::new();
  1144. program.signed_right_shift(Source::Imm, Arch::X32).set_dst(0x05).push();
  1145. assert_eq!(program.into_bytes(), &[0xc4, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1146. }
  1147. }
  1148. #[cfg(test)]
  1149. mod register {
  1150. use super::super::super::super::*;
  1151. #[test]
  1152. fn move_and_add_from_register() {
  1153. let mut program = BpfCode::new();
  1154. program.add(Source::Reg, Arch::X32).set_dst(0x03).set_src(0x02).push();
  1155. assert_eq!(program.into_bytes(), &[0x0c, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1156. }
  1157. #[test]
  1158. fn move_sub_from_register_to_register() {
  1159. let mut program = BpfCode::new();
  1160. program.sub(Source::Reg, Arch::X32).set_dst(0x03).set_src(0x04).push();
  1161. assert_eq!(program.into_bytes(), &[0x1c, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1162. }
  1163. #[test]
  1164. fn move_mul_from_register_to_register() {
  1165. let mut program = BpfCode::new();
  1166. program.mul(Source::Reg, Arch::X32).set_dst(0x04).set_src(0x03).push();
  1167. assert_eq!(program.into_bytes(), &[0x2c, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1168. }
  1169. #[test]
  1170. fn move_div_from_register_to_register() {
  1171. let mut program = BpfCode::new();
  1172. program.div(Source::Reg, Arch::X32).set_dst(0x01).set_src(0x00).push();
  1173. assert_eq!(program.into_bytes(), &[0x3c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1174. }
  1175. #[test]
  1176. fn move_bit_or_from_register_to_register() {
  1177. let mut program = BpfCode::new();
  1178. program.bit_or(Source::Reg, Arch::X32).set_dst(0x03).set_src(0x01).push();
  1179. assert_eq!(program.into_bytes(), &[0x4c, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1180. }
  1181. #[test]
  1182. fn move_bit_and_from_register_to_register() {
  1183. let mut program = BpfCode::new();
  1184. program.bit_and(Source::Reg, Arch::X32).set_dst(0x03).set_src(0x02).push();
  1185. assert_eq!(program.into_bytes(), &[0x5c, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1186. }
  1187. #[test]
  1188. fn move_left_shift_from_register_to_register() {
  1189. let mut program = BpfCode::new();
  1190. program.left_shift(Source::Reg, Arch::X32).set_dst(0x02).set_src(0x03).push();
  1191. assert_eq!(program.into_bytes(), &[0x6c, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1192. }
  1193. #[test]
  1194. fn move_logical_right_shift_from_register_to_register() {
  1195. let mut program = BpfCode::new();
  1196. program.right_shift(Source::Reg, Arch::X32).set_dst(0x02).set_src(0x04).push();
  1197. assert_eq!(program.into_bytes(), &[0x7c, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1198. }
  1199. #[test]
  1200. fn move_mod_from_register_to_register() {
  1201. let mut program = BpfCode::new();
  1202. program.modulo(Source::Reg, Arch::X32).set_dst(0x01).set_src(0x02).push();
  1203. assert_eq!(program.into_bytes(), &[0x9c, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1204. }
  1205. #[test]
  1206. fn move_bit_xor_from_register_to_register() {
  1207. let mut program = BpfCode::new();
  1208. program.bit_xor(Source::Reg, Arch::X32).set_dst(0x02).set_src(0x04).push();
  1209. assert_eq!(program.into_bytes(), &[0xac, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1210. }
  1211. #[test]
  1212. fn move_from_register_to_another_register() {
  1213. let mut program = BpfCode::new();
  1214. program.mov(Source::Reg, Arch::X32).set_dst(0x00).set_src(0x01).push();
  1215. assert_eq!(program.into_bytes(), &[0xbc, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1216. }
  1217. #[test]
  1218. fn move_signed_right_shift_from_register_to_register() {
  1219. let mut program = BpfCode::new();
  1220. program.signed_right_shift(Source::Reg, Arch::X32).set_dst(0x02).set_src(0x03).push();
  1221. assert_eq!(program.into_bytes(), &[0xcc, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  1222. }
  1223. }
  1224. }
  1225. }
  1226. #[cfg(test)]
  1227. mod programs {
  1228. use super::super::*;
  1229. #[test]
  1230. fn example_from_assembler() {
  1231. let mut program = BpfCode::new();
  1232. program.add(Source::Imm, Arch::X64).set_dst(1).set_imm(0x605).push()
  1233. .mov(Source::Imm, Arch::X64).set_dst(2).set_imm(0x32).push()
  1234. .mov(Source::Reg, Arch::X64).set_src(0).set_dst(1).push()
  1235. .swap_bytes(Endian::Big).set_dst(0).set_imm(0x10).push()
  1236. .negate(Arch::X64).set_dst(2).push()
  1237. .exit().push();
  1238. let bytecode = program.into_bytes();
  1239. let ref_prog = &[
  1240. 0x07, 0x01, 0x00, 0x00, 0x05, 0x06, 0x00, 0x00,
  1241. 0xb7, 0x02, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00,
  1242. 0xbf, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1243. 0xdc, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
  1244. 0x87, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1245. 0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1246. ];
  1247. // cargo says: "`[{integer}; 48]` cannot be formatted using `{:?}`
  1248. // because it doesn't implement `std::fmt::Debug`"
  1249. // So let's check in two steps.
  1250. assert_eq!(bytecode[..32], ref_prog[..32]);
  1251. assert_eq!(bytecode[33..], ref_prog[33..]);
  1252. }
  1253. }
  1254. }