Skip to content

Commit

Permalink
refactor: replace lazy_static by LazyLock
Browse files Browse the repository at this point in the history
  • Loading branch information
desbma committed Oct 12, 2024
1 parent fb5c6af commit 192c8ad
Show file tree
Hide file tree
Showing 4 changed files with 545 additions and 568 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ bincode = { version = "1.3.3", default-features = false }
clap = { version = "4.3.17", default-features = false, features = ["std", "color", "help", "usage", "error-context", "suggestions", "derive"] }
function_name = { version = "0.3.0", default-features = false, optional = true }
itertools = { version = "0.11.0", default-features = false, features = ["use_std"] }
lazy_static = { version = "1.4.0", default-features = false }
log = { version = "0.4.19", default-features = false, features = ["max_level_trace", "release_max_level_info"] }
nix = { version = "0.26.2", default-features = false, features = ["fs"] }
nom = { version = "7.1.3", default-features = false, features = ["std"], optional = true }
Expand Down
59 changes: 34 additions & 25 deletions src/summarize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use std::{
ffi::OsStr,
os::unix::ffi::OsStrExt,
path::{Path, PathBuf},
sync::LazyLock,
};

use lazy_static::lazy_static;

use crate::{
strace::{
BufferExpression, BufferType, Expression, IntegerExpression, IntegerExpressionValue,
Expand Down Expand Up @@ -117,21 +116,20 @@ enum SyscallInfo {
},
}

lazy_static! {
//
// For some reference on syscalls, see:
// - https://man7.org/linux/man-pages/man2/syscalls.2.html
// - https://filippo.io/linux-syscall-table/
// - https://linasm.sourceforge.net/docs/syscalls/filesystem.php
//
static ref SYSCALL_MAP: HashMap<&'static str, SyscallInfo> = HashMap::from([
//
// For some reference on syscalls, see:
// - https://man7.org/linux/man-pages/man2/syscalls.2.html
// - https://filippo.io/linux-syscall-table/
// - https://linasm.sourceforge.net/docs/syscalls/filesystem.php
//
static SYSCALL_MAP: LazyLock<HashMap<&'static str, SyscallInfo>> = LazyLock::new(|| {
HashMap::from([
// mmap
("mmap", SyscallInfo::Mmap { prot_idx: 2 }),
("mmap2", SyscallInfo::Mmap { prot_idx: 2 }),
("shmat", SyscallInfo::Mmap { prot_idx: 2 }),
("mprotect", SyscallInfo::Mmap { prot_idx: 2 }),
("pkey_mprotect", SyscallInfo::Mmap { prot_idx: 2 }),

// network
("connect", SyscallInfo::Network { sockaddr_idx: 1 }),
("bind", SyscallInfo::Network { sockaddr_idx: 1 }),
Expand All @@ -156,7 +154,6 @@ lazy_static! {
flags_idx: 2,
},
),

// rename
(
"rename",
Expand Down Expand Up @@ -188,23 +185,37 @@ lazy_static! {
flags_idx: Some(4),
},
),

// set scheduler
("sched_setscheduler", SyscallInfo::SetScheduler),

// socket
("socket", SyscallInfo::Socket),

// stat fd
("fstat", SyscallInfo::StatFd { fd_idx: 0 }),
("getdents", SyscallInfo::StatFd { fd_idx: 0 }),

// stat path
("stat", SyscallInfo::StatPath { relfd_idx: None, path_idx: 0 }),
("lstat", SyscallInfo::StatPath { relfd_idx: None, path_idx: 0 }),
("newfstatat", SyscallInfo::StatPath { relfd_idx: Some(0), path_idx: 1 }),
]);
}
(
"stat",
SyscallInfo::StatPath {
relfd_idx: None,
path_idx: 0,
},
),
(
"lstat",
SyscallInfo::StatPath {
relfd_idx: None,
path_idx: 0,
},
),
(
"newfstatat",
SyscallInfo::StatPath {
relfd_idx: Some(0),
path_idx: 1,
},
),
])
});

/// Resolve relative path if possible, and normalize it
fn resolve_path(path: &Path, relfd_idx: Option<usize>, syscall: &Syscall) -> Option<PathBuf> {
Expand All @@ -230,10 +241,8 @@ fn resolve_path(path: &Path, relfd_idx: Option<usize>, syscall: &Syscall) -> Opt
Some(path.canonicalize().unwrap_or(path))
}

lazy_static! {
static ref FD_PSEUDO_PATH_REGEX: regex::bytes::Regex =
regex::bytes::Regex::new(r"^[a-z]+:\[[0-9a-z]+\]/?$").unwrap();
}
static FD_PSEUDO_PATH_REGEX: LazyLock<regex::bytes::Regex> =
LazyLock::new(|| regex::bytes::Regex::new(r"^[a-z]+:\[[0-9a-z]+\]/?$").unwrap());

fn is_fd_pseudo_path(path: &[u8]) -> bool {
FD_PSEUDO_PATH_REGEX.is_match(path)
Expand Down
Loading

0 comments on commit 192c8ad

Please sign in to comment.