Skip to content

Commit

Permalink
✨ Add a project example from @willemolding's Cannon-rs project
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Nov 11, 2023
1 parent 87fcea0 commit 20d62e2
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 0 deletions.
32 changes: 32 additions & 0 deletions crates/mipsevm/src/mips/instrumented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,38 @@ mod test {
}
}

#[test]
fn test_hello_rs_willem() {
let elf_bytes = include_bytes!("../../../../example/bin/hello-willem-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 _ in 0..400_000 {
if ins.state.exited {
break;
}
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
7 changes: 7 additions & 0 deletions example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ 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-cpu=mips32 -C target-feature=-mips32r2,-fpxx,-nooddspreg,+mips32,+crt-static,+soft-float' \
# cross build --release --target ../mips-unknown-linux-gnu.json -Z build-std -Z build-std-features=panic-unwind && \
# cp ../../target/mips-unknown-linux-gnu/release/hello-rs ../bin/hello-rs.elf

bin:
mkdir bin

Expand Down
Binary file added example/bin/hello-willem-rs.elf
Binary file not shown.
2 changes: 2 additions & 0 deletions example/hello-rs-willem/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Rust target dir
target
73 changes: 73 additions & 0 deletions example/hello-rs-willem/Cargo.lock

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

12 changes: 12 additions & 0 deletions example/hello-rs-willem/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "hello-rs-willem"
edition = "2021"
version = "0.0.1"
authors = ["Willem Olding"]

[dependencies]
cannon-io = { git = "https://github.com/badboilabs/Cannon-rs" }
cannon-heap = { git = "https://github.com/badboilabs/Cannon-rs" }

[profile.release]
panic = "abort"
20 changes: 20 additions & 0 deletions example/hello-rs-willem/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# [`Cannon-rs`][cannon-rs-willem] example program

This is an example Rust program for Cannon that uses Willem Olding's [program template][program-template-willem].

## Building

The program can be built using Badboilabs' provided container:

```sh
docker run \
--rm \
--platform linux/amd64 \
-v `pwd`/:/code \
-w="/code" \
ghcr.io/badboilabs/cannon-rs/builder:main cargo build --release -Zbuild-std && \
cp target/mips-unknown-none/release/hello-rs-willem ../bin/hello-willem-rs.elf
```

[cannon-rs-willem]: https://github.com/BadBoiLabs/Cannon-rs
[program-template-willem]: https://github.com/BadBoiLabs/Cannon-rs/tree/main/project-template
29 changes: 29 additions & 0 deletions example/hello-rs-willem/mips-unknown-linux-gnu.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"arch": "mips",
"cpu": "mips32",
"crt-objects-fallback": "false",
"crt-static-respected": true,
"data-layout": "E-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64",
"dynamic-linking": true,
"env": "gnu",
"features": "-mips32r2,-fpxx,-nooddspreg,+mips32,+crt-static,+soft-float",
"has-rpath": true,
"has-thread-local": true,
"linker-flavor": "gnu-cc",
"llvm-target": "mips-unknown-linux-gnu",
"max-atomic-width": 32,
"os": "linux",
"position-independent-executables": true,
"relro-level": "full",
"supported-split-debuginfo": [
"packed",
"unpacked",
"off"
],
"target-endian": "big",
"target-family": [
"unix"
],
"target-mcount": "_mcount",
"target-pointer-width": "32"
}
36 changes: 36 additions & 0 deletions example/hello-rs-willem/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! `cannon-rs` program template, provided by Willem Olding in the other [`Cannon-rs`](https://github.com/BadBoiLabs/Cannon-rs/tree/main).
#![no_std]
#![no_main]
#![feature(core_intrinsics)]
#![feature(alloc_error_handler)]
#![feature(asm_experimental_arch)]

extern crate alloc;

const HEAP_SIZE: usize = 0x400000;

use cannon_heap::init_heap;
use cannon_io::prelude::*;

#[no_mangle]
pub extern "C" fn _start() {
init_heap!(HEAP_SIZE);

print("hello world!\n").unwrap();

exit(0);
}

#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
let msg = alloc::format!("panic: {}", info);
let _ = print(&msg);
exit(2);
}

#[alloc_error_handler]
fn alloc_error_handler(_layout: alloc::alloc::Layout) -> ! {
let _ = print("alloc error!");
exit(3);
}

0 comments on commit 20d62e2

Please sign in to comment.