Skip to content

Commit

Permalink
Switch to external getters library (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
novafacing authored Dec 11, 2023
1 parent 47daa65 commit 4c5c67e
Show file tree
Hide file tree
Showing 23 changed files with 196 additions and 295 deletions.
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ members = [

"modules/tsffs/src/util/command-ext",
"modules/tsffs/src/util/ffi-macro",
"modules/tsffs/src/util/getters",
"modules/tsffs/src/util/raw-cstr",
"modules/tsffs/src/util/version-tools",
]
Expand All @@ -50,7 +49,6 @@ default-members = [

"modules/tsffs/src/util/command-ext",
"modules/tsffs/src/util/ffi-macro",
"modules/tsffs/src/util/getters",
"modules/tsffs/src/util/raw-cstr",
"modules/tsffs/src/util/version-tools",
]
Expand All @@ -65,7 +63,6 @@ ispm-wrapper = { path = "modules/tsffs/src/simics/ispm-wrapper" }

command-ext = { path = "modules/tsffs/src/util/command-ext" }
ffi-macro = { path = "modules/tsffs/src/util/ffi-macro" }
getters = { path = "modules/tsffs/src/util/getters" }
raw-cstr = { path = "modules/tsffs/src/util/raw-cstr" }
version-tools = { path = "modules/tsffs/src/util/version-tools" }

Expand All @@ -77,7 +74,7 @@ libafl_targets = { git = "https://github.com/AFLplusplus/LibAFL", default-featur
] }
serde = { version = "1.0.188", features = ["derive"] }
serde_json = { version = "1.0.107" }
versions = { version = "5.0.1", features = ["serde"] }
versions = { version = "6.0.0", features = ["serde"] }


[profile.dev]
Expand Down
3 changes: 1 addition & 2 deletions modules/tsffs/src/simics/ispm-wrapper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ version.workspace = true
[dependencies]
anyhow = { workspace = true }
command-ext = { workspace = true }
getters2 = "0.1.2"
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
version-tools = { workspace = true }

getters.workspace = true
typed-builder = "0.18.0"
7 changes: 6 additions & 1 deletion modules/tsffs/src/simics/ispm-wrapper/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! Data deserializable from ISPM commands and configurations
use anyhow::Result;
use getters::Getters;
use getters2::Getters;
use serde::Deserialize;
use serde_json::from_slice;
use std::{fmt::Display, fs::read, path::PathBuf};
Expand Down Expand Up @@ -159,15 +159,19 @@ impl Settings {
/// A package that is already installed
pub struct InstalledPackage {
#[serde(rename = "pkgNumber")]
#[getters(deref)]
/// The package number
package_number: isize,
#[serde(deserialize_with = "version_constraint_from_string")]
#[getters(clone)]
/// The package version
version: VersionConstraint,
#[builder(setter(into))]
#[getters(clone)]
/// The package name
name: String,
#[builder(default, setter(into))]
#[getters(clone)]
/// Paths to this installed package
paths: Vec<PathBuf>,
}
Expand Down Expand Up @@ -204,6 +208,7 @@ pub struct Packages {
/// A package which is added to a project
pub struct ProjectPackage {
#[serde(rename = "pkgNumber")]
#[getters(deref)]
/// The package number
package_number: isize,
#[serde(deserialize_with = "version_constraint_from_string")]
Expand Down
8 changes: 4 additions & 4 deletions modules/tsffs/src/simics/ispm-wrapper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub trait ToArgs {
pub mod ispm {
use std::{iter::repeat, path::PathBuf};

use getters::Getters;
use getters2::Getters;
use typed_builder::TypedBuilder;

use crate::{ToArgs, NON_INTERACTIVE_FLAG};
Expand Down Expand Up @@ -184,7 +184,7 @@ pub mod ispm {
};
use anyhow::Result;
use command_ext::CommandExtCheck;
use getters::Getters;
use getters2::Getters;
use serde_json::from_slice;
use std::{iter::repeat, path::PathBuf, process::Command};
use typed_builder::TypedBuilder;
Expand Down Expand Up @@ -288,7 +288,7 @@ pub mod ispm {
};
use anyhow::{anyhow, Result};
use command_ext::CommandExtCheck;
use getters::Getters;
use getters2::Getters;
use serde_json::from_slice;
use std::{collections::HashSet, iter::once, path::Path, process::Command};
use typed_builder::TypedBuilder;
Expand All @@ -311,7 +311,7 @@ pub mod ispm {

impl ToArgs for CreateOptions {
fn to_args(&self) -> Vec<String> {
self.packages()
self.packages_ref()
.iter()
.map(|p| Some(p.to_string()))
.chain(once(
Expand Down
10 changes: 6 additions & 4 deletions modules/tsffs/src/simics/simics-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,8 @@ impl TryIntoAttrValueTypeDictOpts {
.filter_map(|f| {
f.ident.clone().map(|i| {
let ident_name = i.to_string();
quote!((#ident_name.try_into()?, value.#i().clone().try_into()?))
let accessor = format_ident!("{}_ref", i);
quote!((#ident_name.try_into()?, value.#accessor().clone().try_into()?))
})
})
.collect::<Vec<_>>();
Expand Down Expand Up @@ -1302,9 +1303,10 @@ impl TryIntoAttrValueTypeListOpts {
.iter()
.filter(|f| !f.skip.is_present())
.filter_map(|f| {
f.ident
.clone()
.map(|i| quote!(value.#i().clone().try_into()?))
f.ident.clone().map(|i| {
let accessor = format_ident!("{}_ref", i);
quote!(value.#accessor().clone().try_into()?)
})
})
.collect::<Vec<_>>();

Expand Down
2 changes: 1 addition & 1 deletion modules/tsffs/src/tsffs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ simics-macro = { workspace = true }
yaxpeax-arch = "0.2.7"
yaxpeax-x86 = "1.2.0"
typed-builder = "0.18.0"
getters.workspace = true
getters2 = "0.1.2"
serde_json.workspace = true
goblin = "0.7.1"
yaxpeax-riscv = { git = "https://github.com/novafacing/yaxpeax-riscv", version = "0.1.0", features = [
Expand Down
2 changes: 1 addition & 1 deletion modules/tsffs/src/tsffs/src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ pub trait ArchitectureOperations {
let addr_size =
self.processor_info_v2().get_logical_address_width()? as usize / u8::BITS as usize;
let initial_size =
size.initial_size()
size.initial_size_ref()
.ok_or_else(|| anyhow!("Expected initial size for start"))? as usize;

let physical_memory = self.processor_info_v2().get_physical_memory()?;
Expand Down
2 changes: 1 addition & 1 deletion modules/tsffs/src/tsffs/src/arch/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ impl ArchitectureOperations for X86ArchitectureOperations {
let physical_memory = self.processor_info_v2().get_physical_memory()?;

let initial_size =
size.initial_size()
size.initial_size_ref()
.ok_or_else(|| anyhow!("Expected initial size for start"))? as usize;

trace!(
Expand Down
11 changes: 9 additions & 2 deletions modules/tsffs/src/tsffs/src/configuration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
path::PathBuf,
};

use getters::Getters;
use getters2::Getters;
use simics::api::{lookup_file, BreakpointId};
use simics_macro::TryIntoAttrValueTypeDict;
use typed_builder::TypedBuilder;
Expand Down Expand Up @@ -33,7 +33,7 @@ impl Configuration {
}

#[derive(TypedBuilder, Getters, Debug, Clone, TryIntoAttrValueTypeDict)]
#[getters(mutable)]
#[getters(deref, mutable)]
pub struct Configuration {
#[builder(default = false)]
/// Whether any breakpoint that occurs during fuzzing is treated as a fault
Expand All @@ -42,9 +42,11 @@ pub struct Configuration {
/// Whether any CPU exception that occurs during fuzzing is treated as a solution
all_exceptions_are_solutions: bool,
#[builder(default)]
#[getters(skip_deref, clone)]
/// The set of specific exception numbers that are treated as a solution
exceptions: BTreeSet<i64>,
#[builder(default)]
#[getters(skip_deref, clone)]
/// The set of breakpoints to treat as solutions
breakpoints: BTreeSet<BreakpointId>,
#[builder(default = Configuration::DEFAULT_TIMEOUT_SECONDS)]
Expand All @@ -66,14 +68,18 @@ pub struct Configuration {
#[builder(default, setter(strip_option))]
iterations: Option<usize>,
#[builder(default)]
#[getters(skip_deref, clone)]
tokens: Vec<Vec<u8>>,
#[builder(default = lookup_file("%simics%").expect("No simics project root found").join(Configuration::DEFAULT_CORPUS_DIRECTORY_NAME))]
#[getters(skip_deref, clone)]
corpus_directory: PathBuf,
#[builder(default = lookup_file("%simics%").expect("No simics project root found").join(Configuration::DEFAULT_SOLUTIONS_DIRECTORY_NAME))]
#[getters(skip_deref, clone)]
solutions_directory: PathBuf,
#[builder(default = false)]
generate_random_corpus: bool,
#[builder(default)]
#[getters(skip_deref, clone)]
token_files: Vec<PathBuf>,
#[builder(default = Configuration::DEFAULT_EXECUTOR_TIMEOUT)]
/// The executor timeout in seconds
Expand All @@ -83,6 +89,7 @@ pub struct Configuration {
#[builder(default = true)]
cmplog: bool,
#[builder(default)]
#[getters(skip_deref, clone)]
architecture_hints: HashMap<i32, ArchitectureHint>,
}

Expand Down
36 changes: 19 additions & 17 deletions modules/tsffs/src/tsffs/src/fuzzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::Tsffs;
use anyhow::{anyhow, Result};
use getters::Getters;
use getters2::Getters;
use libafl::{
feedback_or, feedback_or_fast,
prelude::{
Expand Down Expand Up @@ -41,7 +41,9 @@ pub mod tokenize;

#[derive(Getters, Clone, PartialEq, Eq)]
pub struct Testcase {
#[getters(clone)]
testcase: Vec<u8>,
#[getters(deref)]
cmplog: bool,
}

Expand Down Expand Up @@ -79,7 +81,7 @@ impl Tsffs {

/// Start the fuzzing thread.
pub fn start_fuzzer_thread(&mut self) -> Result<()> {
if self.fuzz_thread().is_some() {
if self.fuzz_thread_ref().is_some() {
warn!(self.as_conf_object(), "Fuzz thread already started but start_fuzzer_thread called. Returning without error.");
// We can only start the thread once
return Ok(());
Expand All @@ -98,16 +100,16 @@ impl Tsffs {
self.fuzzer_messages = Some(mrx);

let client = RefCell::new((otx, orx));
let configuration = self.configuration().clone();
let configuration = self.configuration_clone();
let coverage_map = unsafe {
from_raw_parts_mut(
self.coverage_map_mut().as_mut_slice().as_mut_ptr(),
Self::COVERAGE_MAP_SIZE,
)
};
let aflpp_cmp_map = Box::leak(unsafe { Box::from_raw(*self.aflpp_cmp_map_ptr()) });
let aflpp_cmp_map_dup = Box::leak(unsafe { Box::from_raw(*self.aflpp_cmp_map_ptr()) });
let cmplog_enabled = *self.configuration().cmplog();
let aflpp_cmp_map = Box::leak(unsafe { Box::from_raw(*self.aflpp_cmp_map_ptr_ref()) });
let aflpp_cmp_map_dup = Box::leak(unsafe { Box::from_raw(*self.aflpp_cmp_map_ptr_ref()) });
let cmplog_enabled = *self.configuration_ref().cmplog_ref();

// NOTE: We do *not* use `run_in_thread` because it causes the fuzzer to block when HAPs arrive
// which prevents forward progress.
Expand Down Expand Up @@ -174,12 +176,12 @@ impl Tsffs {
let timeout_feedback = TimeFeedback::new(Self::TIMEOUT_FEEDBACK_NAME);

let solutions = OnDiskCorpus::with_meta_format(
configuration.solutions_directory(),
configuration.solutions_directory_ref(),
OnDiskMetadataFormat::JsonPretty,
)?;

let corpus = CachedOnDiskCorpus::with_meta_format(
configuration.corpus_directory(),
configuration.corpus_directory_ref(),
Self::CORPUS_CACHE_SIZE,
Some(OnDiskMetadataFormat::Json),
)?;
Expand All @@ -203,10 +205,10 @@ impl Tsffs {

let mut tokens = Tokens::default();
configuration
.token_files()
.token_files_ref()
.iter()
.try_for_each(|f| tokens.add_from_file(f).map(|_| ()))?;
tokens.add_tokens(configuration.tokens());
tokens.add_tokens(configuration.tokens_ref());
state.add_metadata(tokens);

let scheduler =
Expand Down Expand Up @@ -270,29 +272,29 @@ impl Tsffs {
);
let tracing_stage = TracingStage::new(tracing_executor);
let synchronize_corpus_stage =
SyncFromDiskStage::with_from_file(configuration.corpus_directory().clone());
SyncFromDiskStage::with_from_file(configuration.corpus_directory_clone());
let dump_corpus_stage = DumpToDiskStage::new(
|input: &BytesInput, _state: &_| input.target_bytes().as_slice().to_vec(),
configuration.corpus_directory(),
configuration.solutions_directory(),
configuration.corpus_directory_ref(),
configuration.solutions_directory_ref(),
)?;

if state.must_load_initial_inputs() {
state.load_initial_inputs(
&mut fuzzer,
&mut executor,
&mut manager,
&[configuration.corpus_directory().clone()],
&[configuration.corpus_directory_ref().clone()],
)?;

if state.corpus().count() < 1 && *configuration.generate_random_corpus() {
if state.corpus().count() < 1 && *configuration.generate_random_corpus_ref() {
let mut generator = RandBytesGenerator::new(64);
state.generate_initial_inputs(
&mut fuzzer,
&mut executor,
&mut generator,
&mut manager,
*configuration.initial_random_corpus_size(),
configuration.initial_random_corpus_size_deref(),
)?;
}
}
Expand Down Expand Up @@ -366,7 +368,7 @@ impl Tsffs {
}

pub fn get_testcase(&mut self) -> Result<Testcase> {
let testcase = if let Some(testcase) = self.repro_testcase() {
let testcase = if let Some(testcase) = self.repro_testcase_ref() {
debug!(self.as_conf_object(), "Using repro testcase");
Testcase {
testcase: testcase.clone(),
Expand Down
Loading

0 comments on commit 4c5c67e

Please sign in to comment.