From 0429b7982d57894a9e5f009fa5be88d87616b296 Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Wed, 3 Dec 2025 14:41:02 +0100 Subject: [PATCH 1/2] Update to Rust 2024, pin cxxbridge version, cleanup --- .github/workflows/build_and_test.yml | 9 ++++++--- .gitignore | 3 ++- Cargo.toml | 11 ++++++----- README.md | 4 +--- build.rs | 5 ++++- rustfmt.toml | 6 ++++++ src/lib.rs | 7 +++---- src/tinyinst.rs | 28 +++++++++++++++++----------- 8 files changed, 45 insertions(+), 28 deletions(-) create mode 100644 rustfmt.toml diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index d92b6b0..606709d 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -16,7 +16,8 @@ jobs: - uses: actions/checkout@v3 - uses: ilammy/msvc-dev-cmd@v1 - name: install cxxbridge - run: cargo install cxxbridge-cmd + # When changing this version, also change it in Cargo.toml + run: cargo install cxxbridge-cmd@=1.0.190 - name: install just run: cargo install just - name: Compile test @@ -30,7 +31,8 @@ jobs: steps: - uses: actions/checkout@v3 - name: install cxxbridge - run: cargo install cxxbridge-cmd + # When changing this version, also change it in Cargo.toml + run: cargo install cxxbridge-cmd@=1.0.190 - name: install just run: cargo install just - name: Compile test @@ -44,7 +46,8 @@ jobs: steps: - uses: actions/checkout@v3 - name: install cxxbridge - run: cargo install cxxbridge-cmd + # When changing this version, also change it in Cargo.toml + run: cargo install cxxbridge-cmd@=1.0.190 - name: install just run: cargo install just - name: Compile test diff --git a/.gitignore b/.gitignore index 9b193e2..a60a9e9 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ build/ *.exe /TinyInst *.obj -test_file.txt \ No newline at end of file +test_file.txt +.vscode \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index cc2d218..e23a2a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,14 +6,15 @@ repository = "https://github.com/AFLplusplus/tinyinst-rs/" readme = "./README.md" license = "MIT OR Apache-2.0" keywords = ["bindings", "testing", "security"] -version = "0.1.0" -edition = "2021" +version = "0.1.1" +edition = "2024" categories = ["development-tools::testing", "os", "no-std"] [dependencies] -cxx = { version = "1.0", default-features = false, features = ["alloc"] } +# When changing this, also change all versions in build_and_test.yml +cxx = { version = "=1.0.190", default-features = false, features = ["alloc"] } [build-dependencies] cmake = "0.1.54" -git2 = "0.20.0" -which = "7.0.2" +git2 = "0.20.2" +which = "8.0.0" diff --git a/README.md b/README.md index e8ee6d0..f92e9bb 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,10 @@ FFI to [TinyInst](https://github.com/googleprojectzero/TinyInst). Created for [L ## Dependencies * Visual Studio 2022 -* cxxbridge * cargo-make * python3 * git +* cxxbridge@=1.0.190 (or latest version from Cargo.toml) ## Running the test @@ -17,7 +17,6 @@ FFI to [TinyInst](https://github.com/googleprojectzero/TinyInst). Created for [L 3. Run `just build_test` to build the test binary 4. Run `cargo test` to run the test - ## Optional ENV Variables `CUSTOM_TINYINST_GENERATOR` = Generator used for cmake `-G` flag @@ -26,7 +25,6 @@ FFI to [TinyInst](https://github.com/googleprojectzero/TinyInst). Created for [L `CUSTOM_TINYINST_NO_BUILD` = if set, it won't build Tinyinst everytime. Useful when paired with `CUSTOM_TINYINST_DIR` - #### License diff --git a/build.rs b/build.rs index e617933..f18a3b1 100644 --- a/build.rs +++ b/build.rs @@ -19,6 +19,7 @@ fn build_dep_check(tools: &[&str]) -> bool { true } +#[allow(clippy::too_many_lines)] fn main() { if !build_dep_check(&["git", "cxxbridge", "cmake"]) { return; @@ -34,7 +35,9 @@ fn main() { let custom_tinyinst_generator = env::var_os("CUSTOM_TINYINST_GENERATOR").map(|x| x.to_string_lossy().to_string()); - env::set_var("CXXFLAGS", "-std=c++17"); + // # Safety + // the env is only accessed here, single threaded + unsafe { env::set_var("CXXFLAGS", "-std=c++17") }; let tinyinst_generator = if let Some(generator) = custom_tinyinst_generator.as_ref() { generator diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..e8a7e54 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,6 @@ +group_imports = "StdExternalCrate" +imports_granularity = "Crate" +newline_style = "Unix" +format_code_in_doc_comments = true +format_macro_bodies = true +format_macro_matchers = true diff --git a/src/lib.rs b/src/lib.rs index 495180a..7cee3f9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,8 @@ /*! * Rust bindings for [`TinyInst`](https://github.com/googleprojectzero/TinyInst) -*/ - -#![allow(incomplete_features)] + */ +#![doc = include_str!("../README.md")] +/*! */ #![no_std] #![warn(clippy::cargo)] #![deny(clippy::cargo_common_metadata)] @@ -45,7 +45,6 @@ overflowing_literals, path_statements, patterns_in_fns_without_body, - private_in_public, unconditional_recursion, unused, unused_allocation, diff --git a/src/tinyinst.rs b/src/tinyinst.rs index 9b691e1..99f0bfc 100644 --- a/src/tinyinst.rs +++ b/src/tinyinst.rs @@ -157,11 +157,15 @@ impl TinyInst { .collect(); tinyinst_args_ptr.push(core::ptr::null_mut()); - // Init TinyInst with Tinyinst arguments. - tinyinst_ptr.pin_mut().Init( - i32::try_from(tinyinst_args.len()).unwrap(), - tinyinst_args_ptr.as_mut_ptr(), - ); + // Init TinyInst with TinyInst arguments. + // # Safety + // The arguments and pointers are valid at this point + unsafe { + tinyinst_ptr.pin_mut().Init( + i32::try_from(tinyinst_args.len()).unwrap(), + tinyinst_args_ptr.as_mut_ptr(), + ); + } let program_args_cstr: Vec = program_args .iter() @@ -184,12 +188,14 @@ impl TinyInst { } pub unsafe fn run(&mut self) -> litecov::RunResult { - self.tinyinst_ptr.pin_mut().Run( - i32::try_from(self.program_args_cstr.len()).unwrap(), - self.program_args_ptr.as_mut_ptr(), - self.timeout, - self.timeout, - ) + unsafe { + self.tinyinst_ptr.pin_mut().Run( + i32::try_from(self.program_args_cstr.len()).unwrap(), + self.program_args_ptr.as_mut_ptr(), + self.timeout, + self.timeout, + ) + } } // pub unsafe fn bitmap_coverage( From dde1c32933d4d10fae6c38c67f18d2af43c09cdc Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Wed, 3 Dec 2025 14:51:04 +0100 Subject: [PATCH 2/2] just test target --- Justfile | 3 +++ src/tinyinst.rs | 14 +++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Justfile b/Justfile index d8a3b16..1f3d800 100644 --- a/Justfile +++ b/Justfile @@ -3,3 +3,6 @@ build_configure: build_test: build_configure cmake --build ./test/build --config Debug + +test: build_test + cargo test \ No newline at end of file diff --git a/src/tinyinst.rs b/src/tinyinst.rs index 99f0bfc..9a4810b 100644 --- a/src/tinyinst.rs +++ b/src/tinyinst.rs @@ -117,6 +117,7 @@ impl litecov::Coverage { } } +/// The main `TinyInst` struct. pub struct TinyInst { tinyinst_ptr: UniquePtr, program_args_cstr: Vec, @@ -136,7 +137,7 @@ impl Debug for TinyInst { impl TinyInst { #[must_use] - pub unsafe fn new(tinyinst_args: &[String], program_args: &[String], timeout: u32) -> TinyInst { + pub fn new(tinyinst_args: &[String], program_args: &[String], timeout: u32) -> TinyInst { // commented out by domenukk: // a) would require call to a libc, c++ or rust std fn // b) The program could actually be in the PATH, so, not accessible as file. @@ -158,6 +159,7 @@ impl TinyInst { tinyinst_args_ptr.push(core::ptr::null_mut()); // Init TinyInst with TinyInst arguments. + // // # Safety // The arguments and pointers are valid at this point unsafe { @@ -187,7 +189,13 @@ impl TinyInst { } } + /// Runs the target in litecov. + /// + /// # Safety + /// An insecure target can by design be unsafe to run. pub unsafe fn run(&mut self) -> litecov::RunResult { + // # Safety + // Runs the target program in litecov. Anything might happen. unsafe { self.tinyinst_ptr.pin_mut().Run( i32::try_from(self.program_args_cstr.len()).unwrap(), @@ -210,6 +218,7 @@ impl TinyInst { // litecov::get_coverage_map(bitmap, map_size, self.coverage_ptr.pin_mut()); // } + /// Gets the covered blocks as vec. pub fn vec_coverage(&mut self, afl_coverage: &mut Vec, clear_coverage: bool) { // Clear coverage if there was previous coverage afl_coverage.clear(); @@ -221,6 +230,8 @@ impl TinyInst { // This will mark coverage we have seen as already seen coverage and won't report it again. self.ignore_coverage(); } + + /// Mark coverage we have seen as already seen coverage to not report it again. fn ignore_coverage(&mut self) { self.tinyinst_ptr .pin_mut() @@ -288,6 +299,7 @@ mod tests { assert_eq!(result, super::litecov::RunResult::OK); } } + #[test] fn tinyinst_crash() { use alloc::{string::ToString, vec::Vec};