Early exit in error callback from Wasm (#9)

* Early exit in error callback from Wasm

This avoids Wasm execution hanging due to problems such as wrong public
input.

Mimics circom_runtime behaviour with less detailed debug information.

See https://github.com/iden3/circom_runtime/blob/master/js/witness_calculator.js#L52-L64

Adds test for wrong public input. Without early exit, the test stalls.
With it, the Circom build step fails as expected.

* chore: clean up error handling

* ci: add caching

Co-authored-by: Georgios Konstantopoulos <me@gakonst.com>
This commit is contained in:
oskarth
2021-11-18 18:03:37 +08:00
committed by GitHub
parent bb0f5429fc
commit b1daefca96
3 changed files with 42 additions and 3 deletions

View File

@@ -2,7 +2,7 @@ use color_eyre::Result;
use num_bigint::BigInt;
use num_traits::Zero;
use std::cell::Cell;
use wasmer::{imports, Function, Instance, Memory, MemoryType, Module, Store};
use wasmer::{imports, Function, Instance, Memory, MemoryType, Module, RuntimeError, Store};
use super::{fnv, SafeMemory, Wasm};
@@ -13,6 +13,12 @@ pub struct WitnessCalculator {
pub n64: i32,
}
// Error type to signal end of execution.
// From https://docs.wasmer.io/integrations/examples/exit-early
#[derive(thiserror::Error, Debug, Clone, Copy)]
#[error("{0}")]
struct ExitCode(u32);
impl WitnessCalculator {
pub fn new(path: impl AsRef<std::path::Path>) -> Result<Self> {
let store = Store::default();
@@ -144,7 +150,15 @@ mod runtime {
pub fn error(store: &Store) -> Function {
#[allow(unused)]
#[allow(clippy::many_single_char_names)]
fn func(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) {}
fn func(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) {
// NOTE: We can also get more information why it is failing, see p2str etc here:
// https://github.com/iden3/circom_runtime/blob/master/js/witness_calculator.js#L52-L64
println!(
"runtime error, exiting early: {0} {1} {2} {3} {4} {5}",
a, b, c, d, e, f
);
RuntimeError::raise(Box::new(ExitCode(1)));
}
Function::new_native(store, func)
}