Skip to content

Commit

Permalink
🚧 Rust hello example
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Nov 11, 2023
1 parent 87fcea0 commit f44984b
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["bin", "crates/*"]
members = ["bin", "crates/*", "example/*-rs"]
resolver = "2"

[workspace.package]
Expand Down
34 changes: 34 additions & 0 deletions crates/mipsevm/src/mips/instrumented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,40 @@ mod test {
}
}

#[test]
#[ignore]
fn test_hello_rs() {
let elf_bytes = include_bytes!("../../../../example/bin/hello-rs.elf");
let mut state = load_elf(elf_bytes).unwrap();
// patch::patch_go(elf_bytes, &mut state).unwrap();
patch::patch_stack(&mut state).unwrap();

let out = BufWriter::new(Vec::default());
let err = BufWriter::new(Vec::default());
let mut ins =
InstrumentedState::new(state, StaticOracle::new(b"hello world".to_vec()), out, err);

for i in 0..400_000 {
if ins.state.exited {
break;
}
dbg!(i);
ins.step(false).unwrap();
}

assert!(ins.state.exited, "must exit");
assert_eq!(ins.state.exit_code, 0, "must exit with 0");

assert_eq!(
String::from_utf8(ins.std_out.buffer().to_vec()).unwrap(),
"hello world!\n"
);
assert_eq!(
String::from_utf8(ins.std_err.buffer().to_vec()).unwrap(),
""
);
}

#[test]
fn test_hello() {
let elf_bytes = include_bytes!("../../../../example/bin/hello.elf");
Expand Down
8 changes: 7 additions & 1 deletion example/Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
all: elf dump
all: elf dump ex-rs

.PHONY: elf
elf: $(patsubst %/go.mod,bin/%.elf,$(wildcard */go.mod))

.PHONY: dump
dump: $(patsubst %/go.mod,bin/%.dump,$(wildcard */go.mod))

.PHONY: ex-rs
ex-rs:
cd hello-rs && \
RUSTFLAGS='-C link-arg=-no-pie -C target-feature=+mips32,+crt-static,-mips32r2,-fpxx,-nooddspreg' cross build --release --target mips-unknown-linux-musl && \
cp ../../target/mips-unknown-linux-musl/release/hello-rs ../bin/hello-rs.elf

bin:
mkdir bin

Expand Down
Binary file added example/bin/hello-rs.elf
Binary file not shown.
8 changes: 8 additions & 0 deletions example/hello-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "hello-rs"
edition = "2021"
version.workspace = true
authors.workspace = true

[dependencies]
# no deps
5 changes: 5 additions & 0 deletions example/hello-rs/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use std::io::{self, Write};

fn main() {
let _ = io::stdout().write_all(b"hello world!\n");
}

0 comments on commit f44984b

Please sign in to comment.