Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ build/
*.exe
/TinyInst
*.obj
test_file.txt
test_file.txt
.vscode
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 3 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ build_configure:

build_test: build_configure
cmake --build ./test/build --config Debug

test: build_test
cargo test
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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

<sup>
Expand Down
5 changes: 4 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -45,7 +45,6 @@
overflowing_literals,
path_statements,
patterns_in_fns_without_body,
private_in_public,
unconditional_recursion,
unused,
unused_allocation,
Expand Down
42 changes: 30 additions & 12 deletions src/tinyinst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ impl litecov::Coverage {
}
}

/// The main `TinyInst` struct.
pub struct TinyInst {
tinyinst_ptr: UniquePtr<litecov::TinyInstInstrumentation>,
program_args_cstr: Vec<CString>,
Expand All @@ -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.
Expand All @@ -157,11 +158,16 @@ 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<CString> = program_args
.iter()
Expand All @@ -183,13 +189,21 @@ 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 {
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,
)
// # 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(),
self.program_args_ptr.as_mut_ptr(),
self.timeout,
self.timeout,
)
}
}

// pub unsafe fn bitmap_coverage(
Expand All @@ -204,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<u64>, clear_coverage: bool) {
// Clear coverage if there was previous coverage
afl_coverage.clear();
Expand All @@ -215,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()
Expand Down Expand Up @@ -282,6 +299,7 @@ mod tests {
assert_eq!(result, super::litecov::RunResult::OK);
}
}

#[test]
fn tinyinst_crash() {
use alloc::{string::ToString, vec::Vec};
Expand Down