Dean 利迪恩 3 veckor sedan
förälder
incheckning
1b6262f241

+ 15 - 15
crates/assembler/src/instruction.rs

@@ -5,7 +5,7 @@ use {
         lexer::{ImmediateValue, Token},
         syscall::SYSCALLS,
     },
-    sbpf_common::{opcode::{JUMP_IMM_OPS, JUMP_OPS, JUMP_REG_OPS, Opcode}},
+    sbpf_common::opcode::{JUMP_IMM_OPS, JUMP_OPS, JUMP_REG_OPS, Opcode},
     std::ops::Range,
 };
 
@@ -61,11 +61,13 @@ impl Instruction {
         let mut operands = Vec::new();
         let span = 0..bytes.len();
 
-        let opcode: Opcode = bytes[0].try_into().map_err(|e| CompileError::BytecodeError {
-            error: format!("Invalid opcode 0x{:02x}: {}", bytes[0], e),
-            span: 0..1,
-            custom_label: None,
-        })?;
+        let opcode: Opcode = bytes[0]
+            .try_into()
+            .map_err(|e| CompileError::BytecodeError {
+                error: format!("Invalid opcode 0x{:02x}: {}", bytes[0], e),
+                span: 0..1,
+                custom_label: None,
+            })?;
         let reg = bytes[1];
         let src = reg >> 4;
         let dst = reg & 0x0f;
@@ -378,15 +380,13 @@ impl Instruction {
                     });
                 }
                 operands.push(Token::Register(dst, 1..2));
-            }
-
-            // _ => {
-            //     return Err(CompileError::BytecodeError {
-            //         error: format!("Unsupported opcode: {:?}", opcode),
-            //         span: span.clone(),
-            //         custom_label: None,
-            //     });
-            // }
+            } // _ => {
+              //     return Err(CompileError::BytecodeError {
+              //         error: format!("Unsupported opcode: {:?}", opcode),
+              //         span: span.clone(),
+              //         custom_label: None,
+              //     });
+              // }
         }
 
         Ok(Instruction {

+ 12 - 3
crates/assembler/src/parser.rs

@@ -534,7 +534,10 @@ impl ParseWithConstMap for Instruction {
                                     let new_opcode = Into::<u8>::into(opcode) | BPF_X;
                                     opcode = new_opcode.try_into().map_err(|e| {
                                         CompileError::BytecodeError {
-                                            error: format!("Invalid opcode 0x{:02x}: {}", new_opcode, e),
+                                            error: format!(
+                                                "Invalid opcode 0x{:02x}: {}",
+                                                new_opcode, e
+                                            ),
                                             span: span.clone(),
                                             custom_label: None,
                                         }
@@ -707,7 +710,10 @@ impl ParseWithConstMap for Instruction {
                                         let new_opcode = Into::<u8>::into(opcode) | BPF_X;
                                         opcode = new_opcode.try_into().map_err(|e| {
                                             CompileError::BytecodeError {
-                                                error: format!("Invalid opcode 0x{:02x}: {}", new_opcode, e),
+                                                error: format!(
+                                                    "Invalid opcode 0x{:02x}: {}",
+                                                    new_opcode, e
+                                                ),
                                                 span: span.clone(),
                                                 custom_label: None,
                                             }
@@ -742,7 +748,10 @@ impl ParseWithConstMap for Instruction {
                                         let new_opcode = Into::<u8>::into(opcode) | BPF_X;
                                         opcode = new_opcode.try_into().map_err(|e| {
                                             CompileError::BytecodeError {
-                                                error: format!("Invalid opcode 0x{:02x}: {}", new_opcode, e),
+                                                error: format!(
+                                                    "Invalid opcode 0x{:02x}: {}",
+                                                    new_opcode, e
+                                                ),
                                                 span: span.clone(),
                                                 custom_label: None,
                                             }

+ 9 - 6
crates/common/src/opcode.rs

@@ -1,5 +1,8 @@
 use {
-    crate::errors::SBPFError, core::{fmt, str::FromStr}, num_derive::FromPrimitive, serde::{Deserialize, Serialize}
+    crate::errors::SBPFError,
+    core::{fmt, str::FromStr},
+    num_derive::FromPrimitive,
+    serde::{Deserialize, Serialize},
 };
 
 #[derive(Debug, Clone, Copy)]
@@ -492,17 +495,17 @@ impl TryFrom<u8> for Opcode {
             0x8d => Ok(Opcode::Callx),
             0x95 => Ok(Opcode::Exit),
             _ => Err(SBPFError::BytecodeError {
-                error: format!("no decode handler for opcode {}", opcode),
+                error: format!("no decode handler for opcode 0x{:02x}", opcode),
                 span: 0..1,
                 custom_label: Some("Invalid opcode".to_string()),
-            })
+            }),
         }
     }
 }
 
-impl Into<u8> for Opcode {
-    fn into(self) -> u8 {
-        match self {
+impl From<Opcode> for u8 {
+    fn from(opcode: Opcode) -> u8 {
+        match opcode {
             Opcode::Lddw => 0x18,
             Opcode::Ldxb => 0x71,
             Opcode::Ldxh => 0x69,

+ 8 - 9
crates/common/src/syscalls_map.rs

@@ -97,12 +97,10 @@ impl DynamicSyscallMap {
 
         // Check if it already exists or would conflict
         match self.entries.binary_search_by_key(&hash, |(h, _)| *h) {
-            Ok(_) => {
-                return Err(format!(
-                    "Hash conflict: '{}' conflicts with existing syscall",
-                    name
-                ));
-            }
+            Ok(_) => Err(format!(
+                "Hash conflict: '{}' conflicts with existing syscall",
+                name
+            )),
             Err(pos) => {
                 self.entries.insert(pos, (hash, name));
                 Ok(())
@@ -236,9 +234,10 @@ pub const fn murmur3_32(buf: &str) -> u32 {
 
 #[cfg(test)]
 mod tests {
-    use crate::syscalls::{REGISTERED_SYSCALLS, SYSCALLS};
-
-    use super::*;
+    use {
+        super::*,
+        crate::syscalls::{REGISTERED_SYSCALLS, SYSCALLS},
+    };
 
     #[test]
     fn test_syscall_lookup() {

+ 8 - 11
crates/sbpf-syscall-map/src/dynamic_map.rs

@@ -1,4 +1,4 @@
-use crate::{murmur3_32, SyscallMap};
+use crate::{SyscallMap, murmur3_32};
 
 /// Runtime-mutable syscall map that owns its data
 /// This allows for dynamic updates at runtime
@@ -22,7 +22,8 @@ impl DynamicSyscallMap {
             if entries[i].0 == entries[i + 1].0 {
                 return Err(format!(
                     "Hash conflict detected between syscalls '{}' and '{}'",
-                    entries[i].1, entries[i + 1].1
+                    entries[i].1,
+                    entries[i + 1].1
                 ));
             }
         }
@@ -49,12 +50,10 @@ impl DynamicSyscallMap {
 
         // Check if it already exists or would conflict
         match self.entries.binary_search_by_key(&hash, |(h, _)| *h) {
-            Ok(_) => {
-                return Err(format!(
-                    "Hash conflict: '{}' conflicts with existing syscall",
-                    name
-                ));
-            }
+            Ok(_) => Err(format!(
+                "Hash conflict: '{}' conflicts with existing syscall",
+                name
+            )),
             Err(pos) => {
                 self.entries.insert(pos, (hash, name));
                 Ok(())
@@ -169,9 +168,7 @@ mod tests {
 
         // Verify we can add new syscalls to it
         let mut dynamic_mut = dynamic;
-        dynamic_mut
-            .add("my_custom_syscall".to_string())
-            .unwrap();
+        dynamic_mut.add("my_custom_syscall".to_string()).unwrap();
 
         assert_eq!(
             dynamic_mut.get(murmur3_32("my_custom_syscall")),

+ 6 - 4
crates/sbpf-syscall-map/src/lib.rs

@@ -1,7 +1,9 @@
+mod dynamic_map;
 mod hash;
 mod static_map;
-mod dynamic_map;
 
-pub use hash::murmur3_32;
-pub use static_map::{SyscallMap, compute_syscall_entries, compute_syscall_entries_const};
-pub use dynamic_map::DynamicSyscallMap;
+pub use {
+    dynamic_map::DynamicSyscallMap,
+    hash::murmur3_32,
+    static_map::{SyscallMap, compute_syscall_entries, compute_syscall_entries_const},
+};

+ 3 - 4
crates/sbpf-syscall-map/src/static_map.rs

@@ -84,9 +84,7 @@ pub const fn compute_syscall_entries_const<'a, const N: usize>(
 ///
 /// The caller must own the string data (e.g., Vec<String>) and pass references.
 /// This function returns references to those owned strings.
-pub fn compute_syscall_entries<'a, T: AsRef<str>>(
-    syscalls: &'a [T],
-) -> Vec<(u32, &'a str)> {
+pub fn compute_syscall_entries<'a, T: AsRef<str>>(syscalls: &'a [T]) -> Vec<(u32, &'a str)> {
     let mut entries: Vec<(u32, &'a str)> = syscalls
         .iter()
         .map(|name| (murmur3_32(name.as_ref()), name.as_ref()))
@@ -99,7 +97,8 @@ pub fn compute_syscall_entries<'a, T: AsRef<str>>(
         if entries[i].0 == entries[i + 1].0 {
             panic!(
                 "Hash conflict detected between syscalls '{}' and '{}'",
-                entries[i].1, entries[i + 1].1
+                entries[i].1,
+                entries[i + 1].1
             );
         }
     }