Skip to content

Commit

Permalink
Reproducer for #247
Browse files Browse the repository at this point in the history
  • Loading branch information
cmichi authored and jarkkojs committed Jan 17, 2025
1 parent 7235229 commit bab8029
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ members = [

"examples/doom",
"examples/hello-world",
"examples/bug-247",
]

[workspace.package]
Expand Down
2 changes: 2 additions & 0 deletions crates/polkavm-test-data/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ fn main() -> Result<(), String> {
build("test-blob", "no-lto", &target_64)?;
build("bench-pinky", "release", &target_32)?;
build("bench-pinky", "release", &target_64)?;
build("bug-247", "release", &target_32)?;
build("bug-247", "release", &target_64)?;

Ok(())
}
2 changes: 2 additions & 0 deletions crates/polkavm-test-data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ pub const BENCH_PINKY_32: &[u8] = include_bytes!("../../../guest-programs/target
pub const BENCH_PINKY_64: &[u8] = include_bytes!("../../../guest-programs/target/riscv64emac-unknown-none-polkavm/release/bench-pinky");
pub const TEST_BLOB_32: &[u8] = include_bytes!("../../../guest-programs/target/riscv32emac-unknown-none-polkavm/no-lto/test-blob");
pub const TEST_BLOB_64: &[u8] = include_bytes!("../../../guest-programs/target/riscv64emac-unknown-none-polkavm/no-lto/test-blob");
pub const BUG_247_32: &[u8] = include_bytes!("../../../guest-programs/target/riscv32emac-unknown-none-polkavm/release/bug-247");
pub const BUG_247_64: &[u8] = include_bytes!("../../../guest-programs/target/riscv64emac-unknown-none-polkavm/release/bug-247");
10 changes: 10 additions & 0 deletions examples/bug-247/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "bug-247"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
env_logger = { version = "0.10.0", default-features = false }
polkavm = { path = "../../crates/polkavm" }
polkavm-linker = { path = "../../crates/polkavm-linker" }
48 changes: 48 additions & 0 deletions examples/bug-247/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::fs;
use polkavm::{Caller, Config, Engine, Error, InterruptKind, Linker, Module, ProgramBlob, Reg};

fn main() {
env_logger::init();

// compile
let input_path = std::env::var("INPUT").expect("no INPUT in env");

// link
let mut config = polkavm_linker::Config::default();
config.set_optimize(false);
/* Disable DWARF processing: */
config.set_strip(true);

let orig = fs::read(input_path).expect("Failed to read {input_path:?}");
let linked = polkavm_linker::program_from_elf(config, orig.as_ref())
.unwrap();

let blob = ProgramBlob::parse(linked[..].into()).unwrap();

let config = Config::from_env().unwrap();
let engine = Engine::new(&config).unwrap();
let module = Module::from_blob(&engine, &Default::default(), blob).unwrap();

// High-level API.
let mut linker: Linker = Linker::new();

linker.define_typed(
"debug_message",
|caller: Caller<()>, buffer: u32, length: u32| {
let buffer = caller.instance.read_memory(buffer, length).unwrap();
eprintln!("print: {:?}", String::from_utf8(buffer).unwrap());
},
).unwrap();

// Link the host functions with the module.
let instance_pre = linker.instantiate_pre(&module).unwrap();

// Instantiate the module.
let mut instance = instance_pre.instantiate().unwrap();

// Grab the function and call it.
println!("Calling into the guest program:");
let _result = instance
.call_typed_and_get_result::<(), ()>(&mut (), "deploy", ())
.unwrap();
}
49 changes: 49 additions & 0 deletions guest-programs/Cargo.lock

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

3 changes: 3 additions & 0 deletions guest-programs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ members = [

# Tests
"test-blob",

# https://github.com/paritytech/polkavm/issues/247
"bug-247"
]

[workspace.lints.rust]
Expand Down
19 changes: 19 additions & 0 deletions guest-programs/bug-247/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "bug-247"
version = "0.1.0"
edition = "2021"
publish = false

[[bin]]
name = "bug-247"
path = "src/main.rs"

[dependencies]
spin = "0.9.8"
talc = "4.4.2"

[target.'cfg(target_env = "polkavm")'.dependencies]
polkavm-derive = { path = "../../crates/polkavm-derive" }

[lints]
workspace = true
77 changes: 77 additions & 0 deletions guest-programs/bug-247/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#![no_std]
#![no_main]

extern crate core;
extern crate alloc;

use polkavm_derive::polkavm_export;
use core::fmt::Write;

#[repr(u32)]
pub enum Foo {
Success = 0,
CalleeTrapped = 1,
Unknown,
}

impl ::core::fmt::Debug for Foo {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(
f,
match self {
Foo::Success => "Success",
Foo::CalleeTrapped => "CalleeTrapped",
Foo::Unknown => "Unknown",
},
)
}
}

struct Writer;
impl core::fmt::Write for Writer {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
unsafe {
crate::debug_message(s.as_ptr(), s.len() as u32);
}
Ok(())
}
}

#[polkavm_derive::polkavm_import]
extern "C" {
pub fn debug_message(str_ptr: *const u8, str_len: u32);
}

#[polkavm_export(abi = polkavm_derive::default_abi)]
pub fn call() { }

#[polkavm_export(abi = polkavm_derive::default_abi)]
pub fn deploy() {
// on heap
let foo = alloc::format!("heap: {:?}", Foo::Success);
unsafe {
crate::debug_message(foo.as_ptr(), foo.len() as u32);
}

// on stack
let mut m = Writer {};
let _ = write!(&mut m, "stack: {:?}", Foo::Success);
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
unsafe {
core::arch::asm!("unimp");
core::hint::unreachable_unchecked();
}
}

use talc::*;
static mut ARENA: [u8; 10000] = [0; 10000];

#[global_allocator]
static ALLOCATOR: Talck<spin::Mutex<()>, ClaimOnOom> = Talc::new(unsafe {
ClaimOnOom::new(Span::from_array(core::ptr::addr_of!(ARENA).cast_mut()))
}).lock();

0 comments on commit bab8029

Please sign in to comment.