Skip to content

Commit

Permalink
compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
jayz22 committed May 18, 2023
1 parent 0441376 commit 87f0aa1
Show file tree
Hide file tree
Showing 8 changed files with 375 additions and 289 deletions.
551 changes: 294 additions & 257 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ git = "https://github.com/stellar/rs-stellar-xdr"
rev = "b283ec0bcb791610013107b9eab7d7ff07a65db1"
default-features = false

[workspace.dependencies.wasmi]
package = "wasmi"
version = "0.29.0"
git = "https://github.com/paritytech/wasmi"
rev = "23d8d8c684255f2d63526baa348ab3eb3d249de0"

# [patch."https://github.com/stellar/rs-stellar-xdr"]
# stellar-xdr = { path = "../rs-stellar-xdr/" }

Expand Down
20 changes: 9 additions & 11 deletions soroban-env-common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,24 +138,22 @@ impl From<stellar_xdr::Error> for Error {
impl From<wasmi::Error> for Error {
fn from(e: wasmi::Error) -> Self {
if let wasmi::Error::Trap(trap) = e {
if let Some(code) = trap.as_code() {
if let Some(code) = trap.trap_code() {
let ec = match code {
wasmi::core::TrapCode::Unreachable => ScErrorCode::InternalError,
wasmi::core::TrapCode::UnreachableCodeReached => ScErrorCode::InternalError,

wasmi::core::TrapCode::MemoryAccessOutOfBounds
| wasmi::core::TrapCode::TableAccessOutOfBounds => ScErrorCode::IndexBounds,
wasmi::core::TrapCode::MemoryOutOfBounds
| wasmi::core::TrapCode::TableOutOfBounds => ScErrorCode::IndexBounds,

wasmi::core::TrapCode::ElemUninitialized => ScErrorCode::MissingValue,
wasmi::core::TrapCode::IndirectCallToNull => ScErrorCode::MissingValue,

wasmi::core::TrapCode::DivisionByZero
wasmi::core::TrapCode::IntegerDivisionByZero
| wasmi::core::TrapCode::IntegerOverflow
| wasmi::core::TrapCode::InvalidConversionToInt => ScErrorCode::ArithDomain,
| wasmi::core::TrapCode::BadConversionToInteger => ScErrorCode::ArithDomain,

wasmi::core::TrapCode::UnexpectedSignature => ScErrorCode::UnexpectedType,
wasmi::core::TrapCode::BadSignature => ScErrorCode::UnexpectedType,

wasmi::core::TrapCode::StackOverflow
| wasmi::core::TrapCode::MemLimitExceeded
| wasmi::core::TrapCode::CpuLimitExceeded => {
wasmi::core::TrapCode::StackOverflow | wasmi::core::TrapCode::OutOfFuel => {
return Error::from_type_and_code(
ScErrorType::Budget,
ScErrorCode::ExceededLimit,
Expand Down
2 changes: 1 addition & 1 deletion soroban-env-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub use num::{I256, U256};
pub use stellar_xdr as xdr;

// RawVal is the 64-bit transparent type.
#[cfg(feature = "vm")]
#[cfg(feature = "wasmi")]
pub use raw_val::WasmiMarshal;
pub use raw_val::{
AddressObject, ContractExecutableObject, LedgerKeyNonceObject, MapObject, VecObject,
Expand Down
51 changes: 42 additions & 9 deletions soroban-env-host/src/cost_runner/cost_types/vm_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,27 @@ impl CostRunner for VmMemReadRun {

type SampleType = VmMemRunSample;

fn run_iter(host: &crate::Host, _iter: u64, sample: Self::SampleType) {
let mut buf: Vec<u8> = vec![0; sample.1];
let vm = sample.0;
// FIXME: this whole cost should be removed, it's just MemCopy.
assert!(false);
type RecycledType = Self::SampleType;

fn run_iter(
host: &crate::Host,
_iter: u64,
mut sample: Self::SampleType,
) -> Self::RecycledType {
black_box(sample.vm.with_vmcaller(|caller| {
host.metered_vm_read_bytes_from_linear_memory(caller, &sample.vm, 0, &mut sample.buf)
.unwrap()
}));
sample
}

fn run_baseline_iter(
host: &crate::Host,
_iter: u64,
sample: Self::SampleType,
) -> Self::RecycledType {
black_box(host.charge_budget(Self::COST_TYPE, Some(0)).unwrap());
black_box(sample)
}
}

Expand All @@ -59,9 +75,26 @@ impl CostRunner for VmMemWriteRun {

type SampleType = VmMemRunSample;

fn run_iter(host: &crate::Host, _iter: u64, mut sample: Self::SampleType) {
let vm = sample.0;
// FIXME: this whole cost should be removed, it's just MemCopy.
assert!(false);
type RecycledType = Self::SampleType;

fn run_iter(
host: &crate::Host,
_iter: u64,
mut sample: Self::SampleType,
) -> Self::RecycledType {
black_box(sample.vm.with_vmcaller(|caller| {
host.metered_vm_write_bytes_to_linear_memory(caller, &sample.vm, 0, &mut sample.buf)
.unwrap()
}));
sample
}

fn run_baseline_iter(
host: &crate::Host,
_iter: u64,
sample: Self::SampleType,
) -> Self::RecycledType {
black_box(host.charge_budget(Self::COST_TYPE, Some(0)).unwrap());
black_box(sample)
}
}
2 changes: 1 addition & 1 deletion soroban-env-host/src/test/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn tuple_roundtrip() -> Result<(), HostError> {
Ok(())
}

#[cfg(feature = "vm")]
#[cfg(feature = "wasmi")]
#[test]
fn f32_does_not_work() -> Result<(), HostError> {
use soroban_env_common::xdr::Hash;
Expand Down
30 changes: 21 additions & 9 deletions soroban-env-host/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@
mod dispatch;
mod func_info;

use crate::{
err,
host::{Frame, HostImpl},
xdr::ContractCostType,
HostError, VmCaller,
};
use crate::{err, host::Frame, xdr::ContractCostType, HostError};
use std::{cell::RefCell, io::Cursor, rc::Rc};

use super::{xdr::Hash, Host, RawVal, Symbol};
Expand All @@ -27,12 +22,12 @@ use soroban_env_common::{
ConversionError, SymbolStr, TryIntoVal, WasmiMarshal,
};

use wasmi::{Caller, Engine, Instance, Linker, Memory, Module, Store, StoreContextMut, Value};
use wasmi::{Engine, Instance, Linker, Memory, Module, Store, Value};

#[cfg(any(test, feature = "testutils"))]
use soroban_env_common::{
xdr::{ScVal, ScVec},
TryFromVal,
TryFromVal, VmCaller,
};

impl wasmi::core::HostError for HostError {}
Expand Down Expand Up @@ -337,7 +332,7 @@ impl Vm {
res
}

fn module_custom_section(m: &Module, name: impl AsRef<str>) -> Option<&[u8]> {
fn module_custom_section(_m: &Module, _name: impl AsRef<str>) -> Option<&[u8]> {
todo!()
/*
m.custom_sections().iter().find_map(|s| {
Expand All @@ -355,4 +350,21 @@ impl Vm {
pub fn custom_section(&self, name: impl AsRef<str>) -> Option<&[u8]> {
Self::module_custom_section(&self.module, name)
}

// Utility function that synthesizes a `VmCaller<Host>` configured to point
// to this VM's `Store` and `Instance`, and calls the provided function
// back with it. Mainly used for testing.
#[cfg(any(test, feature = "testutils"))]
pub fn with_vmcaller<F, T>(&self, _f: F) -> T
where
F: FnOnce(&mut VmCaller<Host>) -> T,
{
// let store: &mut Store<Host> = &mut self.store.borrow_mut();
// let mut ctx: StoreContextMut<Host> = store.into();
// let caller: Caller<Host> = Caller::new(&mut ctx, Some(&self.instance));
// let mut vmcaller: VmCaller<Host> = VmCaller(Some(caller));
// f(&mut vmcaller)
// TODO: they have made the Caller::new private. Need fork.
todo!()
}
}
2 changes: 1 addition & 1 deletion soroban-test-wasms/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
//! the host here.

pub const ADD_I32: &[u8] = include_bytes!("../wasm-workspace/opt/example_add_i32.wasm").as_slice();
pub const ADD_F32: &[u8] = include_bytes!("../wasm-workspace/opt/example_add_f32.wasm").as_slice();
// pub const ADD_F32: &[u8] = include_bytes!("../wasm-workspace/opt/example_add_f32.wasm").as_slice();
pub const CREATE_CONTRACT: &[u8] =
include_bytes!("../wasm-workspace/opt/example_create_contract.wasm").as_slice();
pub const CONTRACT_STORAGE: &[u8] =
Expand Down

0 comments on commit 87f0aa1

Please sign in to comment.