Skip to content

Commit 2540911

Browse files
authored
Make Signals compatible with nix, implement TryFrom<&str> (#1599)
* Make our signals compatible to nix Signals * no-default nix
1 parent 20f1119 commit 2540911

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

libafl_bolts/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ serde_json = { version = "1.0", optional = true, default-features = false, featu
107107
miniz_oxide = { version = "0.7.1", optional = true}
108108
hostname = { version = "^0.3", optional = true } # Is there really no gethostname in the stdlib?
109109
rand_core = { version = "0.6", optional = true }
110-
nix = { version = "0.26", optional = true , features = ["socket", "poll"] }
110+
nix = { version = "0.26", default-features = false, optional = true, features = ["signal", "socket", "poll"] }
111111
uuid = { version = "1.4", optional = true, features = ["serde", "v4"] }
112112
clap = {version = "4.0", features = ["derive", "wrap_help"], optional = true} # CLI parsing, for libafl_bolts::cli / the `cli` feature
113113
log = "0.4.20"

libafl_bolts/src/os/unix_signals.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,38 @@ pub enum Signal {
299299
SigTrap = SIGTRAP,
300300
}
301301

302+
impl TryFrom<&str> for Signal {
303+
type Error = Error;
304+
305+
fn try_from(value: &str) -> Result<Self, Self::Error> {
306+
Ok(match value {
307+
"SIGABRT" => Signal::SigAbort,
308+
"SIGBUS" => Signal::SigBus,
309+
"SIGFPE" => Signal::SigFloatingPointException,
310+
"SIGILL" => Signal::SigIllegalInstruction,
311+
"SIGPIPE" => Signal::SigPipe,
312+
"SIGSEGV" => Signal::SigSegmentationFault,
313+
"SIGUSR2" => Signal::SigUser2,
314+
"SIGALRM" => Signal::SigAlarm,
315+
"SIGHUP" => Signal::SigHangUp,
316+
"SIGKILL" => Signal::SigKill,
317+
"SIGQUIT" => Signal::SigQuit,
318+
"SIGTERM" => Signal::SigTerm,
319+
"SIGINT" => Signal::SigInterrupt,
320+
"SIGTRAP" => Signal::SigTrap,
321+
_ => return Err(Error::illegal_argument(format!("No signal named {value}"))),
322+
})
323+
}
324+
}
325+
326+
#[cfg(feature = "std")]
327+
impl From<Signal> for nix::sys::signal::Signal {
328+
fn from(value: Signal) -> Self {
329+
// we can be semi-certain that all signals exist in nix.
330+
i32::from(value).try_into().unwrap()
331+
}
332+
}
333+
302334
/// A list of crashing signals
303335
pub static CRASH_SIGNALS: &[Signal] = &[
304336
Signal::SigAbort,

0 commit comments

Comments
 (0)