Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
4f5c5b6
Introduce first groundwork for Wasm executor.
gavofyork Dec 5, 2017
6237848
Remove old Rust-runtime code.
gavofyork Dec 6, 2017
293cf5d
Avoid commiting compled files.
gavofyork Dec 6, 2017
66b636e
Add runtime precompile.
gavofyork Dec 6, 2017
c7e7456
Rename so module makes more sense.
gavofyork Dec 6, 2017
0cfd67e
Further renaming.
gavofyork Dec 6, 2017
3011edf
Ensure tests work.
gavofyork Dec 6, 2017
a74b4fa
Allow bringing in of externalities.
gavofyork Dec 7, 2017
6dac275
Nice macros for imports.
gavofyork Dec 9, 2017
08f7b26
Allow passing in of data through allocators.
gavofyork Dec 11, 2017
1ace6b2
Can now pass in bytes to WasmExecutor.
gavofyork Dec 11, 2017
27e5fed
Additional cleanup.
gavofyork Dec 11, 2017
13dcd89
Switch usages of `OutData` to `u64`
gavofyork Dec 11, 2017
84b9f84
convert to safe but extremely verbose type conversion.
gavofyork Dec 11, 2017
7f31899
Remove StaticExternalities distinction.
gavofyork Dec 11, 2017
edf061e
Remove another unused use.
gavofyork Dec 11, 2017
edb6bea
Refactor wasm utils out
gavofyork Dec 11, 2017
9f59f48
Remove extraneous copies that weren't really testing anything.
gavofyork Dec 11, 2017
287b29d
Try to use wasm 0.15
gavofyork Dec 31, 2017
1bd55fe
Make it work!
gavofyork Dec 31, 2017
36e254a
Call-time externalities working.
gavofyork Jan 1, 2018
cd651a3
Add basic externalities.
gavofyork Jan 1, 2018
a8f9cca
Merge branch 'with-wasm-0.15' into with-wasm
gavofyork Jan 1, 2018
4404846
Fix grumbles and note unwraps to be sorted.
gavofyork Jan 1, 2018
b1d963a
Test storage externality.
gavofyork Jan 3, 2018
319d9c0
Fix nits.
gavofyork Jan 3, 2018
7ec9221
Merge branch 'master' into with-wasm
gavofyork Jan 3, 2018
2934d94
Compile collation logic.
gavofyork Jan 3, 2018
5998aa1
Move back to refs. Yey.
gavofyork Jan 3, 2018
3f4085a
Remove "object" id for storage access.
gavofyork Jan 4, 2018
4be0537
Fix test.
gavofyork Jan 4, 2018
01d7019
Fix up rest of tests.
gavofyork Jan 4, 2018
db1adee
remove unwrap.
gavofyork Jan 4, 2018
87c54f7
Expose set/get code in externalities
gavofyork Jan 5, 2018
471ea1e
Add validator set.
gavofyork Jan 5, 2018
fa35993
Introduce validator set into externalities and test.
gavofyork Jan 5, 2018
a0f64df
Add another external function.
gavofyork Jan 6, 2018
e736d46
Remove code and validators; use storage for everything.
gavofyork Jan 6, 2018
234297c
Introduce validators function.
gavofyork Jan 6, 2018
3f8a96d
Tests (and a fix) for the validators getter.
gavofyork Jan 6, 2018
6636520
Allow calls into runtime to return data.
gavofyork Jan 7, 2018
964659e
Remove unneeded trace.
gavofyork Jan 7, 2018
8ca1b7b
Make runtime printing a bit nicer.
gavofyork Jan 7, 2018
74156a2
Create separate runtimes for testing and polkadot.
gavofyork Jan 8, 2018
611a7ac
Remove commented code.
gavofyork Jan 8, 2018
c3afecc
Use new path.
gavofyork Jan 8, 2018
ea4d6c5
Refactor into shared support module.
gavofyork Jan 8, 2018
709693d
Fix warning.
gavofyork Jan 8, 2018
ec1e6b6
Remove unwraps.
gavofyork Jan 8, 2018
5c0ec3d
Make macro a little less unhygenic.
gavofyork Jan 8, 2018
79ab46f
Add wasm files.
gavofyork Jan 8, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor into shared support module.
  • Loading branch information
gavofyork committed Jan 8, 2018
commit ea4d6c5d330425a52db20957c1ab0e16d44eaa8e
4 changes: 2 additions & 2 deletions executor/src/wasm_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ impl_function_executor!(this: FunctionExecutor<'e, E>,
this.heap.deallocate(addr);
println!("free {}", addr)
},
set_storage(key_data: *const u8, key_len: i32, value_data: *const u8, value_len: i32) => {
ext_set_storage(key_data: *const u8, key_len: i32, value_data: *const u8, value_len: i32) => {
if let (Ok(key), Ok(value)) = (this.memory.get(key_data, key_len as usize), this.memory.get(value_data, value_len as usize)) {
this.ext.set_storage(key, value);
}
},
get_allocated_storage(key_data: *const u8, key_len: i32, written_out: *mut i32) -> *mut u8 => {
ext_get_allocated_storage(key_data: *const u8, key_len: i32, written_out: *mut i32) -> *mut u8 => {
let (offset, written) = if let Ok(key) = this.memory.get(key_data, key_len as usize) {
if let Ok(value) = this.ext.storage(&key) {
let offset = this.heap.allocate(value.len() as u32) as u32;
Expand Down
10 changes: 8 additions & 2 deletions runtime/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 runtime/build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh

cargo +nightly build --target=wasm32-unknown-unknown --release
dirs=`find * -maxdepth 0 -type d | grep -v pwasm-`
dirs=`find * -maxdepth 0 -type d | grep -v pwasm- | grep -v support`
for i in $dirs
do
if [[ -e $i/Cargo.toml ]]
Expand Down
3 changes: 1 addition & 2 deletions runtime/polkadot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ authors = ["Parity Technologies <[email protected]>"]
crate-type = ["cdylib"]

[dependencies]
pwasm-libc = { path = "../pwasm-libc", version = "0.1" }
pwasm-alloc = { path = "../pwasm-alloc", version = "0.1" }
runtime-support = { path = "../support", version = "0.1" }
134 changes: 9 additions & 125 deletions runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,149 +3,33 @@
#![cfg_attr(feature = "strict", deny(warnings))]

#![feature(alloc)]

extern crate alloc;
use alloc::vec::Vec;

extern crate pwasm_libc;
extern crate pwasm_alloc;

#[lang = "panic_fmt"]
#[no_mangle]
pub fn panic_fmt() -> ! {
loop {}
}

extern "C" {
fn ext_print(utf8_data: *const u8, utf8_len: i32);
fn ext_print_num(value: u64);
fn set_storage(key_data: *const u8, key_len: i32, value_data: *const u8, value_len: i32);
fn get_allocated_storage(key_data: *const u8, key_len: i32, written_out: *mut i32) -> *mut u8;
}

pub mod state {
use alloc::vec::Vec;
use super::{get_allocated_storage, set_storage as super_set_storage};

pub fn storage(key: &[u8]) -> Vec<u8> {
let mut length: i32 = 0;
unsafe {
let ptr = get_allocated_storage(&key[0], key.len() as i32, &mut length);
Vec::from_raw_parts(ptr, length as usize, length as usize)
}
}

pub fn set_storage(key: &[u8], value: &[u8]) {
unsafe {
super_set_storage(
&key[0] as *const u8, key.len() as i32,
&value[0] as *const u8, value.len() as i32
);
}
}

pub fn code() -> Vec<u8> {
storage(b"\0code")
}

pub fn set_code(new: &[u8]) {
set_storage(b"\0code", new)
}

fn value_vec(mut value: usize, initial: Vec<u8>) -> Vec<u8> {
let mut acc = initial;
while value > 0 {
acc.push(value as u8);
value /= 256;
}
acc
}

pub fn set_validator(index: usize, validator: &[u8]) {
set_storage(&value_vec(index, b"\0validator".to_vec()), validator);
}

pub fn validator(index: usize) -> Vec<u8> {
storage(&value_vec(index, b"\0validator".to_vec()))
}

pub fn set_validator_count(count: usize) {
(count..validator_count()).for_each(|i| set_validator(i, &[]));
set_storage(b"\0validator_count", &value_vec(count, Vec::new()));
}

pub fn validator_count() -> usize {
storage(b"\0validator_count").into_iter().rev().fold(0, |acc, i| (acc << 8) + (i as usize))
}

pub fn validators() -> Vec<Vec<u8>> {
(0..validator_count()).into_iter().map(validator).collect()
}

pub fn set_validators(validators: &[&[u8]]) {
set_validator_count(validators.len());
validators.iter().enumerate().for_each(|(v, i)| set_validator(v, i));
}
}

trait Printable {
fn print(self);
}

impl<'a> Printable for &'a [u8] {
fn print(self) {
unsafe {
ext_print(&self[0] as *const u8, self.len() as i32);
}
}
}

impl Printable for u64 {
fn print(self) {
unsafe { ext_print_num(self); }
}
}

fn print<T: Printable + Sized>(value: T) {
value.print();
}

macro_rules! impl_stub {
($name:ident) => {
pub mod _internal {
#[no_mangle]
pub fn $name(input_data: *mut u8, input_len: usize) -> u64 {
let input = unsafe {
super::alloc::vec::Vec::from_raw_parts(input_data, input_len, input_len)
};

let output = super::$name(input);
&output[0] as *const u8 as u64 + ((output.len() as u64) << 32)
}
}
}
}
#[macro_use]
extern crate runtime_support;
use runtime_support::{set_storage, code, set_code, storage, validators, set_validators, print};

impl_stub!(test_data_in);
fn test_data_in(input: Vec<u8>) -> Vec<u8> {
print(b"set_storage" as &[u8]);
state::set_storage(b"input", &input);
set_storage(b"input", &input);

print(b"code" as &[u8]);
state::set_storage(b"code", &state::code());
set_storage(b"code", &code());

print(b"set_code" as &[u8]);
state::set_code(&input);
set_code(&input);

print(b"storage" as &[u8]);
let copy = state::storage(b"input");
let copy = storage(b"input");

print(b"validators" as &[u8]);
let mut v = state::validators();
let mut v = validators();
v.push(copy);

print(b"set_validators" as &[u8]);
state::set_validators(&v.iter().map(Vec::as_slice).collect::<Vec<_>>());
set_validators(&v.iter().map(Vec::as_slice).collect::<Vec<_>>());

print(b"finished!" as &[u8]);
b"all ok!".to_vec()
Expand Down
11 changes: 11 additions & 0 deletions runtime/support/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "runtime-support"
version = "0.1.0"
authors = ["Parity Technologies <[email protected]>"]

[dependencies]
pwasm-libc = { path = "../pwasm-libc", version = "0.1" }
pwasm-alloc = { path = "../pwasm-alloc", version = "0.1" }

[features]
strict = []
123 changes: 123 additions & 0 deletions runtime/support/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#![no_std]
#![feature(lang_items)]
#![cfg_attr(feature = "strict", deny(warnings))]

#![feature(alloc)]

extern crate alloc;
use alloc::vec::Vec;

extern crate pwasm_libc;
extern crate pwasm_alloc;

#[lang = "panic_fmt"]
#[no_mangle]
pub fn panic_fmt() -> ! {
loop {}
}

extern "C" {
fn ext_print(utf8_data: *const u8, utf8_len: i32);
fn ext_print_num(value: u64);
fn ext_set_storage(key_data: *const u8, key_len: i32, value_data: *const u8, value_len: i32);
fn ext_get_allocated_storage(key_data: *const u8, key_len: i32, written_out: *mut i32) -> *mut u8;
}

pub fn storage(key: &[u8]) -> Vec<u8> {
let mut length: i32 = 0;
unsafe {
let ptr = ext_get_allocated_storage(&key[0], key.len() as i32, &mut length);
Vec::from_raw_parts(ptr, length as usize, length as usize)
}
}

pub fn set_storage(key: &[u8], value: &[u8]) {
unsafe {
ext_set_storage(
&key[0] as *const u8, key.len() as i32,
&value[0] as *const u8, value.len() as i32
);
}
}

pub fn code() -> Vec<u8> {
storage(b"\0code")
}

pub fn set_code(new: &[u8]) {
set_storage(b"\0code", new)
}

fn value_vec(mut value: usize, initial: Vec<u8>) -> Vec<u8> {
let mut acc = initial;
while value > 0 {
acc.push(value as u8);
value /= 256;
}
acc
}

pub fn set_validator(index: usize, validator: &[u8]) {
set_storage(&value_vec(index, b"\0validator".to_vec()), validator);
}

pub fn validator(index: usize) -> Vec<u8> {
storage(&value_vec(index, b"\0validator".to_vec()))
}

pub fn set_validator_count(count: usize) {
(count..validator_count()).for_each(|i| set_validator(i, &[]));
set_storage(b"\0validator_count", &value_vec(count, Vec::new()));
}

pub fn validator_count() -> usize {
storage(b"\0validator_count").into_iter().rev().fold(0, |acc, i| (acc << 8) + (i as usize))
}

pub fn validators() -> Vec<Vec<u8>> {
(0..validator_count()).into_iter().map(validator).collect()
}

pub fn set_validators(validators: &[&[u8]]) {
set_validator_count(validators.len());
validators.iter().enumerate().for_each(|(v, i)| set_validator(v, i));
}

pub trait Printable {
fn print(self);
}

impl<'a> Printable for &'a [u8] {
fn print(self) {
unsafe {
ext_print(&self[0] as *const u8, self.len() as i32);
}
}
}

impl Printable for u64 {
fn print(self) {
unsafe { ext_print_num(self); }
}
}

pub fn print<T: Printable + Sized>(value: T) {
value.print();
}

#[macro_export]
macro_rules! impl_stub {
($name:ident) => {
pub mod _internal {
#[no_mangle]
pub fn $name(input_data: *mut u8, input_len: usize) -> u64 {
let input = unsafe {
super::alloc::vec::Vec::from_raw_parts(input_data, input_len, input_len)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a little unhygienic. could we use $crate::alloc?

Also, is there a guarantee that input data is heap-allocated and can really be freed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes - that's given by the other side which (as might be imagined) calls the heap allocator.

};

let output = super::$name(input);
&output[0] as *const u8 as u64 + ((output.len() as u64) << 32)
}
}
}
}
3 changes: 1 addition & 2 deletions runtime/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ authors = ["Parity Technologies <[email protected]>"]
crate-type = ["cdylib"]

[dependencies]
pwasm-libc = { path = "../pwasm-libc", version = "0.1" }
pwasm-alloc = { path = "../pwasm-alloc", version = "0.1" }
runtime-support = { path = "../support", version = "0.1" }
Loading