From e18e630bab5aab2f0b7c563584546dda57016475 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Thu, 4 May 2017 19:49:40 +0300 Subject: [PATCH] various samples and experiments --- ext/src/main.rs | 2 +- samples/import_contract.c | 43 ++++++++++++++++++++++++++++++++++++++ samples/logger_contract.rs | 2 +- samples/static_contract.rs | 37 ++++++-------------------------- 4 files changed, 51 insertions(+), 33 deletions(-) create mode 100644 samples/import_contract.c diff --git a/ext/src/main.rs b/ext/src/main.rs index 7338824..9f092b5 100644 --- a/ext/src/main.rs +++ b/ext/src/main.rs @@ -35,7 +35,7 @@ fn main() { // Loading module let module = parity_wasm::deserialize_file(&args[1]).unwrap(); - let replaced_funcs = vec!["_free", "_malloc"]; + let replaced_funcs = vec!["_free", "_malloc", "_storage_read", "_storage_write", "_storage_size"]; // Save import functions number for later let import_funcs_total = module diff --git a/samples/import_contract.c b/samples/import_contract.c new file mode 100644 index 0000000..c46c08e --- /dev/null +++ b/samples/import_contract.c @@ -0,0 +1,43 @@ +int data; + +extern void log_event(void* ptr); + +int main() { + log_event(0); +} + +void call() { + log_event(0); +} + +/* produces the following code (with gas counter and call optimization) +(module + (type (;0;) (func (param i32) (result i32))) + (type (;1;) (func)) + (type (;2;) (func (param i32))) + (import "env" "memoryBase" (global (;0;) i32)) + (import "env" "memory" (memory (;0;) 256)) + (import "env" "table" (table (;0;) 0 anyfunc)) + (import "env" "gas" (func (;0;) (type 2))) + (func (;1;) (type 0) (param i32) (result i32) + i32.const 2 + call 0 + block i32 ;; label = @1 + i32.const 13 + call 0 + get_global 0 + i32.const 5242880 + i32.add + get_global 0 + i32.const 5242880 + i32.add + i32.load + get_local 0 + i32.load offset=4 + i32.add + i32.store + i32.const 0 + end) + (export "_call" (func 1))) + +*/ diff --git a/samples/logger_contract.rs b/samples/logger_contract.rs index f454bb5..745801a 100644 --- a/samples/logger_contract.rs +++ b/samples/logger_contract.rs @@ -6,7 +6,7 @@ use std::slice; -#[link_args = "-s NO_EXIT_RUNTIME=1 -s NO_FILESYSTEM=1"] +#[link_args = "-s NO_EXIT_RUNTIME=1 -s NO_FILESYSTEM=1 -s"] extern {} /// Wrapper over storage read/write/size externs diff --git a/samples/static_contract.rs b/samples/static_contract.rs index 31cdbc9..05b6bf7 100644 --- a/samples/static_contract.rs +++ b/samples/static_contract.rs @@ -1,33 +1,8 @@ -#![feature(link_args)] -#![no_main] - -// as it is experimental preamble -#![allow(dead_code)] - -#[link_args = "-s WASM=1 -s NO_EXIT_RUNTIME=1 -s NO_FILESYSTEM=1"] -extern {} - -static DATA: u32 = 0; - -#[no_mangle] -pub fn call(_descr: *mut u8) { - let data_ptr = &DATA as *const u32 as *mut u32; - unsafe { *data_ptr += 1; } +extern { + #[link(name="env")] + fn log_event(id: *const u8); } -/* This produces the following code (after injecting gas counter & optimizing) -(module - (type (;0;) (func (param i32))) - (type (;1;) (func (param i32))) - (import "env" "memory" (memory (;0;) 256 256)) - (import "env" "table" (table (;0;) 0 0 anyfunc)) - (import "env" "gas" (func (;0;) (type 1))) - (func (;1;) (type 0) (param i32) - i32.const 4 - call 0 - i32.const 1268 - i32.const 1 - i32.store) - (export "_call" (func 1)) - (data (i32.const 1212) " \05")) -*/ \ No newline at end of file +fn main() { + unsafe { log_event(::std::ptr::null()); } +} \ No newline at end of file