-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ members = [ | |
|
||
"examples/doom", | ||
"examples/hello-world", | ||
"examples/bug-247", | ||
] | ||
|
||
[workspace.package] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|