Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ See the "Known Issues" section for details.

| Program | w2c2<br>-O0 | w2c2<br>-O3 | wasmtime | wasmer<br>(cranelift) | wasmer<br>(llvm) | WAMR<br>-O0 | WAMR<br>-O3 | directly |
|---|---|---|---|---|---|---|---|---|
| `reva-client-eth` (Rust) | 4,543,397,058 | 1,337,696,305 | 1,074,488,397 | doesn't work | ? | didn't check | ? | 388,564,723 |
| `stateless` (Go) | 6,039,298,186 | 2,154,036,727 | 874,758,419 | 953,874,491 | ? | 5,427,433,654 | ? | 236,265,327 |
| `reva-client-eth` (Rust) | 4,543,397,058 | 1,337,696,305 | 1,074,488,397 | doesn't work | ? | 2,472,559,016 | ? | 388,564,723 |
| `stateless` (Go) | 6,039,298,186 | 2,154,036,727 | 874,758,419 | 953,874,491 | ? | 3,526,934,350 | ? | 236,265,327 |

## Analysis

Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ WORKDIR /tmp
# https://github.com/llvm/llvm-project/issues/81440
RUN git clone --branch zkvm https://github.com/psilva261/wasm-micro-runtime-zkvm.git wamr-riscv && \
cd wamr-riscv && \
git checkout e9df4f8169eaafa387b001c0352aec0ea2080ed3 && \
git checkout a93ff4cbbf99d35869fac3e03a012e58fc6cfb9c && \
cmake . \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=/opt/wamr-riscv \
Expand Down
10 changes: 8 additions & 2 deletions examples/build-wasm/go/stateless-by-wasmer/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use anyhow::Result;
use wasmer::{sys::NativeEngineExt, Engine, Instance, Module, Store};
use wasmer::{sys::NativeEngineExt, Engine, Function, Instance, Module, Store};
use wasmer_wasix::{runtime::task_manager::tokio::TokioTaskManager, PluggableRuntime, WasiEnv};

fn main() -> Result<()> {
Expand Down Expand Up @@ -41,7 +41,13 @@ fn main() -> Result<()> {
.finalize(&mut store)?;

// Create imports from WASI
let import_object = wasi_env.import_object(&mut store, &module)?;
let mut import_object = wasi_env.import_object(&mut store, &module)?;

let shutdown = Function::new_typed(&mut store, || {
println!("shutdown called");
});

import_object.define("testmodule", "shutdown", shutdown);

// Instantiate the module
let instance = Instance::new(&mut store, &module, &import_object)?;
Expand Down
5 changes: 5 additions & 0 deletions examples/build-wasm/go/stateless-by-wasmtime/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ fn main() -> anyhow::Result<()> {
let wasm_bytes = include_bytes!("stateless.cwasm");

let engine = Engine::default();

let module = unsafe { Module::deserialize(&engine, wasm_bytes)? };

// Create linker first
let mut linker = Linker::new(&engine);
wasmtime_wasi::p1::add_to_linker_sync(&mut linker, |s| s)?;

linker.func_wrap("testmodule", "shutdown", || {
println!("shutdown called");
})?;

// Create WASI context with build_p1()
let wasi = WasiCtx::builder().inherit_stdio().inherit_args().build_p1();

Expand Down
4 changes: 4 additions & 0 deletions examples/go/empty/example.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package main

func main() {
// During exit a fatal runtime error occurs in WAMR when
// compiled with --bounds-check=0. An early shutdown provides
// a reliable workaround.
shutdown()
}
5 changes: 5 additions & 0 deletions examples/go/empty/utils_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !wasm

package main

func shutdown() {}
5 changes: 5 additions & 0 deletions examples/go/empty/utils_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

//go:wasmimport testmodule shutdown
//go:noescape
func shutdown()
5 changes: 5 additions & 0 deletions examples/go/fibonacci/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ func main() {
n := 20
result := fibonacci(n)
fmt.Printf("Fibonacci(%d) = %d\n", n, result)

// During exit a fatal runtime error occurs in WAMR when
// compiled with --bounds-check=0. An early shutdown provides
// a reliable workaround.
shutdown()
}
5 changes: 5 additions & 0 deletions examples/go/fibonacci/utils_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !wasm

package main

func shutdown() {}
5 changes: 5 additions & 0 deletions examples/go/fibonacci/utils_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

//go:wasmimport testmodule shutdown
//go:noescape
func shutdown()
7 changes: 7 additions & 0 deletions examples/go/println/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,12 @@ func main() {
debug.SetMemoryLimit(400 * (1 << 20))

fmt.Println("Hello world from golang")

// During exit a fatal runtime error occurs in WAMR when
// compiled with --bounds-check=0. An early shutdown provides
// a reliable workaround.
shutdown()

panic("foo")

}
5 changes: 5 additions & 0 deletions examples/go/println/utils_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !wasm

package main

func shutdown() {}
5 changes: 5 additions & 0 deletions examples/go/println/utils_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

//go:wasmimport testmodule shutdown
//go:noescape
func shutdown()
5 changes: 5 additions & 0 deletions examples/go/stateless/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,9 @@ func main() {
if receiptRoot == (common.Hash{}) {
panic("Receipt root is empty")
}

// During exit a fatal runtime error occurs in WAMR when
// compiled with --bounds-check=0. An early shutdown provides
// a reliable workaround.
shutdown()
}
5 changes: 5 additions & 0 deletions examples/go/stateless/utils_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !wasm

package main

func shutdown() {}
5 changes: 5 additions & 0 deletions examples/go/stateless/utils_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

//go:wasmimport testmodule shutdown
//go:noescape
func shutdown()
47 changes: 47 additions & 0 deletions platform/riscv-qemu-user/custom_imports.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* Custom WASM imports for zkvm target
*
* This file implements custom functions for Go programs using //go:wasmimport.
*
* This will be used to implement zkvm precompiles and zkvm specific functions that the guest
* program needs.
*
*/

#include "w2c2_base.h"
#include <stdio.h>

U32 testmodule__testfunc(void* instance, U32 a, U32 b) {
return 1000*a + b;
}

U32 testmodule__testfunc2(void* instance, U32 a, U32 b) {
return a * b;
}

int testmodule__printk(void* instance, U32 val) {
char buf[12];
static const char hex[] = "0123456789abcdef";

buf[0] = '0';
buf[1] = 'x';
for (int i = 7; i >= 0; --i) {
buf[i+2] = hex[val & 0xF];
val >>= 4;
}
buf[10] = '\n';
buf[11] = '\0';

puts(buf);
}

void testmodule__shutdown(void* instance) {
exit(0);
}

U32 testmodule__input_data_len(void* instance) {
return 0;
}

U32 testmodule__input_data(void* instance, U32 index) {
return 0;
}
1 change: 1 addition & 0 deletions platform/riscv-qemu-user/scripts/c2riscv-qemu-user.sh
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ INCLUDES=(
# Source files
SOURCES=(
platform/riscv-qemu-user/main.c
platform/riscv-qemu-user/custom_imports.c
"$GUEST_DIR/guest.c"
$GUEST_DIR/s0*.c
w2c2/embedded/wasi.c
Expand Down
27 changes: 25 additions & 2 deletions platform/riscv-wamr-qemu/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

extern const char wasmModuleBuffer[];
extern int wasmModuleBuffer_length;

void shutdown();

int main(void) {
int argc = 0;
char *argv[0];
Expand All @@ -18,10 +21,30 @@ int main(void) {
wasm_exec_env_t exec_env;
uint32_t size, stack_size = 16*1024*1024;

RuntimeInitArgs init_args;
memset(&init_args, 0, sizeof(RuntimeInitArgs));

wasm_runtime_set_log_level(WASM_LOG_LEVEL_VERBOSE);

/* initialize the wasm runtime by default configurations */
if (!wasm_runtime_init()) {
/* Define an array of NativeSymbol for the APIs to be exported. */
static NativeSymbol native_symbols[] = {
{
"shutdown", // the name of WASM function name
shutdown, // the native function pointer
"()", // the function prototype signature, avoid to use i32
NULL // attachment is NULL
},
};

init_args.mem_alloc_type = Alloc_With_System_Allocator;

/* Native symbols need below registration phase */
init_args.n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol);
init_args.native_module_name = "testmodule";
init_args.native_symbols = native_symbols;

/* initialize the wasm runtime with init_args */
if (!wasm_runtime_full_init(&init_args)) {
printf("runtime init failed\n");
exit(1);
}
Expand Down
8 changes: 4 additions & 4 deletions platform/riscv-wamr-qemu/scripts/wasm2wamr-qemu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ if [ $# -lt 2 ]; then
echo " output-elf Output RISC-V ELF binary path"
echo ""
echo "Example:"
echo " $0 build/.c-packages/println build/bin/println.riscv.elf"
echo " $0 examples/build-wasm/go/println.wasm build/bin/println.wamr.elf"
echo ""
echo "To run in QEMU:"
echo " ./docker/docker-shell.sh qemu-system-riscv64 -machine virt \\"
echo " -m 1024M -d plugin -plugin /libinsn.so \\"
echo " -kernel build/bin/println.riscv.elf -nographic \\"
echo " -kernel build/bin/println.wamr.elf -nographic \\"
echo " -semihosting-config enable=on,target=native"
echo ""
exit 1
Expand Down Expand Up @@ -90,7 +90,7 @@ wamrc \
--cpu-features='+i,+m,+a' \
--opt-level=0 \
--size-level=1 \
--bounds-checks=1 \
--bounds-checks=0 \
-o $OUTPUT.riscv64.wamr $1

gcc platform/riscv-wamr-qemu/file2c/file2c.c \
Expand Down Expand Up @@ -182,7 +182,7 @@ if [ $? -eq 0 ] && [ -f "$OUTPUT" ]; then
echo "Size: $SIZE"
echo ""
echo "To run in QEMU:"
echo " ./docker/docker-shell.sh qemu-system-riscv64 -machine virt \\"
echo " ./docker-shell.sh qemu-system-riscv64 -machine virt \\"
echo " -m 1024M -d plugin -plugin /libinsn.so \\"
echo " -kernel $OUTPUT -nographic \\"
echo " -semihosting-config enable=on,target=native"
Expand Down
2 changes: 2 additions & 0 deletions platform/riscv-wamr-qemu/startup.S
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.section .text.init
.global _start
.global shutdown

_start:
la sp, _stack_top
Expand All @@ -17,6 +18,7 @@ bss_done:
/* Call main */
call main

shutdown:
/* https://github.com/xypron/riscv_test_payload (MIT-licensed) */
li a7, 0x53525354 # SBI extension: SRST (System Reset)
li a6, 0 # Function: system reset
Expand Down
5 changes: 3 additions & 2 deletions platform/riscv-wamr-qemu/syscalls.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "uart.h"

Expand Down
5 changes: 5 additions & 0 deletions rust_benchmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ echo "Transpiling WASM to WASMU with wasmer..."
# TODO: Cranelift is used for now because LLVM support is buggy. Once LLVM is fixed in wasmer use `--llvm` flag instead of `cranelift`; https://github.com/wasmerio/wasmer/issues/5951#issuecomment-3632904384
/root/.wasmer/bin/wasmer compile --cranelift --target riscv64gc-unknown-linux-gnu examples/build-wasm/rust/reva-client-eth.wasm -o examples/build-wasm/rust/reva-client-eth-by-wasmer/src/reva-client-eth.wasmu

echo "Transpiling WASM to WAMR AOT with wamrc..."

./platform/riscv-wamr-qemu/scripts/wasm2wamr-qemu.sh examples/build-wasm/rust/reva-client-eth.wasm build/bin/reva-client-eth.wamr.elf

echo "Compiling C to RISCV..."

OPT_LEVEL="-O0" ./platform/riscv-qemu/scripts/c2riscv-qemu.sh build/c-packages/reva-client-eth/ build/bin/reva-client-eth.riscv.O0.elf
Expand All @@ -38,6 +42,7 @@ run_qemu "$success_string" "false" "native" "examples/rust/reva-client-e
run_qemu "$success_string" "true" "w2c2-O0" "build/bin/reva-client-eth.riscv.O0.elf"
run_qemu "$success_string" "true" "w2c2-O3" "build/bin/reva-client-eth.riscv.O3.elf"
run_qemu "$success_string" "false" "wasmtime" "examples/build-wasm/rust/reva-client-eth-by-wasmtime/target/riscv64gc-unknown-linux-gnu/release/standalone"
run_qemu "$success_string" "true" "wamr" "build/bin/reva-client-eth.wamr.elf"

# `reva-client-eth` via `wasmer` does not work for some reason. The root cause is not yet known. The error is:
# Error: RuntimeError: out of bounds memory access
Expand Down