Skip to content

Commit e2fa860

Browse files
authored
Update to Rust 2024, pin cxxbridge version, cleanup (#20)
* Update to Rust 2024, pin cxxbridge version, cleanup * just test target
1 parent b09409a commit e2fa860

File tree

9 files changed

+61
-29
lines changed

9 files changed

+61
-29
lines changed

.github/workflows/build_and_test.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ jobs:
1616
- uses: actions/checkout@v3
1717
- uses: ilammy/msvc-dev-cmd@v1
1818
- name: install cxxbridge
19-
run: cargo install cxxbridge-cmd
19+
# When changing this version, also change it in Cargo.toml
20+
run: cargo install cxxbridge-cmd@=1.0.190
2021
- name: install just
2122
run: cargo install just
2223
- name: Compile test
@@ -30,7 +31,8 @@ jobs:
3031
steps:
3132
- uses: actions/checkout@v3
3233
- name: install cxxbridge
33-
run: cargo install cxxbridge-cmd
34+
# When changing this version, also change it in Cargo.toml
35+
run: cargo install cxxbridge-cmd@=1.0.190
3436
- name: install just
3537
run: cargo install just
3638
- name: Compile test
@@ -44,7 +46,8 @@ jobs:
4446
steps:
4547
- uses: actions/checkout@v3
4648
- name: install cxxbridge
47-
run: cargo install cxxbridge-cmd
49+
# When changing this version, also change it in Cargo.toml
50+
run: cargo install cxxbridge-cmd@=1.0.190
4851
- name: install just
4952
run: cargo install just
5053
- name: Compile test

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ build/
44
*.exe
55
/TinyInst
66
*.obj
7-
test_file.txt
7+
test_file.txt
8+
.vscode

Cargo.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ repository = "https://github.com/AFLplusplus/tinyinst-rs/"
66
readme = "./README.md"
77
license = "MIT OR Apache-2.0"
88
keywords = ["bindings", "testing", "security"]
9-
version = "0.1.0"
10-
edition = "2021"
9+
version = "0.1.1"
10+
edition = "2024"
1111
categories = ["development-tools::testing", "os", "no-std"]
1212

1313
[dependencies]
14-
cxx = { version = "1.0", default-features = false, features = ["alloc"] }
14+
# When changing this, also change all versions in build_and_test.yml
15+
cxx = { version = "=1.0.190", default-features = false, features = ["alloc"] }
1516

1617
[build-dependencies]
1718
cmake = "0.1.54"
18-
git2 = "0.20.0"
19-
which = "7.0.2"
19+
git2 = "0.20.2"
20+
which = "8.0.0"

Justfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ build_configure:
33

44
build_test: build_configure
55
cmake --build ./test/build --config Debug
6+
7+
test: build_test
8+
cargo test

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ FFI to [TinyInst](https://github.com/googleprojectzero/TinyInst). Created for [L
55
## Dependencies
66

77
* Visual Studio 2022
8-
* cxxbridge
98
* cargo-make
109
* python3
1110
* git
11+
* cxxbridge@=1.0.190 (or latest version from Cargo.toml)
1212

1313
## Running the test
1414

@@ -17,7 +17,6 @@ FFI to [TinyInst](https://github.com/googleprojectzero/TinyInst). Created for [L
1717
3. Run `just build_test` to build the test binary
1818
4. Run `cargo test` to run the test
1919

20-
2120
## Optional ENV Variables
2221

2322
`CUSTOM_TINYINST_GENERATOR` = Generator used for cmake `-G` flag
@@ -26,7 +25,6 @@ FFI to [TinyInst](https://github.com/googleprojectzero/TinyInst). Created for [L
2625

2726
`CUSTOM_TINYINST_NO_BUILD` = if set, it won't build Tinyinst everytime. Useful when paired with `CUSTOM_TINYINST_DIR`
2827

29-
3028
#### License
3129

3230
<sup>

build.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ fn build_dep_check(tools: &[&str]) -> bool {
1919
true
2020
}
2121

22+
#[allow(clippy::too_many_lines)]
2223
fn main() {
2324
if !build_dep_check(&["git", "cxxbridge", "cmake"]) {
2425
return;
@@ -34,7 +35,9 @@ fn main() {
3435
let custom_tinyinst_generator =
3536
env::var_os("CUSTOM_TINYINST_GENERATOR").map(|x| x.to_string_lossy().to_string());
3637

37-
env::set_var("CXXFLAGS", "-std=c++17");
38+
// # Safety
39+
// the env is only accessed here, single threaded
40+
unsafe { env::set_var("CXXFLAGS", "-std=c++17") };
3841

3942
let tinyinst_generator = if let Some(generator) = custom_tinyinst_generator.as_ref() {
4043
generator

rustfmt.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
group_imports = "StdExternalCrate"
2+
imports_granularity = "Crate"
3+
newline_style = "Unix"
4+
format_code_in_doc_comments = true
5+
format_macro_bodies = true
6+
format_macro_matchers = true

src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*!
22
* Rust bindings for [`TinyInst`](https://github.com/googleprojectzero/TinyInst)
3-
*/
4-
5-
#![allow(incomplete_features)]
3+
*/
4+
#![doc = include_str!("../README.md")]
5+
/*! */
66
#![no_std]
77
#![warn(clippy::cargo)]
88
#![deny(clippy::cargo_common_metadata)]
@@ -45,7 +45,6 @@
4545
overflowing_literals,
4646
path_statements,
4747
patterns_in_fns_without_body,
48-
private_in_public,
4948
unconditional_recursion,
5049
unused,
5150
unused_allocation,

src/tinyinst.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ impl litecov::Coverage {
117117
}
118118
}
119119

120+
/// The main `TinyInst` struct.
120121
pub struct TinyInst {
121122
tinyinst_ptr: UniquePtr<litecov::TinyInstInstrumentation>,
122123
program_args_cstr: Vec<CString>,
@@ -136,7 +137,7 @@ impl Debug for TinyInst {
136137

137138
impl TinyInst {
138139
#[must_use]
139-
pub unsafe fn new(tinyinst_args: &[String], program_args: &[String], timeout: u32) -> TinyInst {
140+
pub fn new(tinyinst_args: &[String], program_args: &[String], timeout: u32) -> TinyInst {
140141
// commented out by domenukk:
141142
// a) would require call to a libc, c++ or rust std fn
142143
// b) The program could actually be in the PATH, so, not accessible as file.
@@ -157,11 +158,16 @@ impl TinyInst {
157158
.collect();
158159
tinyinst_args_ptr.push(core::ptr::null_mut());
159160

160-
// Init TinyInst with Tinyinst arguments.
161-
tinyinst_ptr.pin_mut().Init(
162-
i32::try_from(tinyinst_args.len()).unwrap(),
163-
tinyinst_args_ptr.as_mut_ptr(),
164-
);
161+
// Init TinyInst with TinyInst arguments.
162+
//
163+
// # Safety
164+
// The arguments and pointers are valid at this point
165+
unsafe {
166+
tinyinst_ptr.pin_mut().Init(
167+
i32::try_from(tinyinst_args.len()).unwrap(),
168+
tinyinst_args_ptr.as_mut_ptr(),
169+
);
170+
}
165171

166172
let program_args_cstr: Vec<CString> = program_args
167173
.iter()
@@ -183,13 +189,21 @@ impl TinyInst {
183189
}
184190
}
185191

192+
/// Runs the target in litecov.
193+
///
194+
/// # Safety
195+
/// An insecure target can by design be unsafe to run.
186196
pub unsafe fn run(&mut self) -> litecov::RunResult {
187-
self.tinyinst_ptr.pin_mut().Run(
188-
i32::try_from(self.program_args_cstr.len()).unwrap(),
189-
self.program_args_ptr.as_mut_ptr(),
190-
self.timeout,
191-
self.timeout,
192-
)
197+
// # Safety
198+
// Runs the target program in litecov. Anything might happen.
199+
unsafe {
200+
self.tinyinst_ptr.pin_mut().Run(
201+
i32::try_from(self.program_args_cstr.len()).unwrap(),
202+
self.program_args_ptr.as_mut_ptr(),
203+
self.timeout,
204+
self.timeout,
205+
)
206+
}
193207
}
194208

195209
// pub unsafe fn bitmap_coverage(
@@ -204,6 +218,7 @@ impl TinyInst {
204218
// litecov::get_coverage_map(bitmap, map_size, self.coverage_ptr.pin_mut());
205219
// }
206220

221+
/// Gets the covered blocks as vec.
207222
pub fn vec_coverage(&mut self, afl_coverage: &mut Vec<u64>, clear_coverage: bool) {
208223
// Clear coverage if there was previous coverage
209224
afl_coverage.clear();
@@ -215,6 +230,8 @@ impl TinyInst {
215230
// This will mark coverage we have seen as already seen coverage and won't report it again.
216231
self.ignore_coverage();
217232
}
233+
234+
/// Mark coverage we have seen as already seen coverage to not report it again.
218235
fn ignore_coverage(&mut self) {
219236
self.tinyinst_ptr
220237
.pin_mut()
@@ -282,6 +299,7 @@ mod tests {
282299
assert_eq!(result, super::litecov::RunResult::OK);
283300
}
284301
}
302+
285303
#[test]
286304
fn tinyinst_crash() {
287305
use alloc::{string::ToString, vec::Vec};

0 commit comments

Comments
 (0)