Skip to content
Draft
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
3 changes: 2 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[submodule "go-ethereum"]
path = go-ethereum
url = https://github.com/OffchainLabs/go-ethereum.git
# TODO: change this back to the upstream repo, when changes are merged.
url = https://github.com/wakabat/go-ethereum.git
[submodule "arbitrator/wasm-libraries/soft-float/SoftFloat"]
path = arbitrator/wasm-libraries/soft-float/SoftFloat
url = https://github.com/OffchainLabs/SoftFloat.git
Expand Down
4 changes: 4 additions & 0 deletions arbitrator/arbutil/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ serde = { workspace = true, features = ["derive", "rc"] }
siphasher = { workspace = true }
tiny-keccak = { workspace = true, features = ["keccak"] }
wasmparser = { workspace = true }

[features]
default = []
sp1 = []
2 changes: 2 additions & 0 deletions arbitrator/arbutil/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,8 @@ impl From<&Operator<'_>> for OperatorCode {
O::I16x8RelaxedQ15mulrS { .. } => 0xfd111,
O::I16x8RelaxedDotI8x16I7x16S { .. } => 0xfd112,
O::I32x4RelaxedDotI8x16I7x16AddS { .. } => 0xfd113,
#[cfg(feature = "sp1")]
_ => todo!(),
})
}
}
Expand Down
1 change: 1 addition & 0 deletions arbitrator/prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ counters = []
native = ["dep:wasmer", "dep:wasmer-compiler-singlepass", "brotli/wasmer_traits", "dep:c-kzg"]
singlepass_rayon = ["wasmer-compiler-singlepass?/rayon"]
rayon = ["dep:rayon"]
sp1 = []
28 changes: 23 additions & 5 deletions arbitrator/prover/src/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

use crate::{
programs::{
FuncMiddleware, Middleware, ModuleMod, STYLUS_ENTRY_POINT, StylusData,
config::CompileConfig, counter::Counter, depth::DepthChecker, dynamic::DynamicMeter,
heap::HeapBound, meter::Meter, start::StartMover, FuncMiddleware, Middleware, ModuleMod,
StylusData, STYLUS_ENTRY_POINT,
heap::HeapBound, meter::Meter, start::StartMover,
},
value::{ArbValueType, FunctionType, IntegerValType, Value},
};
use arbutil::{
evm::ARBOS_VERSION_STYLUS_CHARGING_FIXES, math::SaturatingSum, Bytes32, Color, DebugColor,
Bytes32, Color, DebugColor, evm::ARBOS_VERSION_STYLUS_CHARGING_FIXES, math::SaturatingSum,
};
use eyre::{bail, ensure, eyre, Result, WrapErr};
use eyre::{Result, WrapErr, bail, ensure, eyre};
use fnv::{FnvHashMap as HashMap, FnvHashSet as HashSet};
use nom::{
branch::alt,
Expand All @@ -22,7 +22,7 @@ use nom::{
};
use serde::{Deserialize, Serialize};
use std::{convert::TryInto, fmt::Debug, hash::Hash, mem, path::Path, str::FromStr};
use wasmer_types::{entity::EntityRef, ExportIndex, FunctionIndex, LocalFunctionIndex};
use wasmer_types::{ExportIndex, FunctionIndex, LocalFunctionIndex, entity::EntityRef};
use wasmparser::{
Data, Element, ExternalKind, MemoryType, Name, NameSectionReader, Naming, Operator, Parser,
Payload, TableType, TypeRef, ValType, Validator, WasmFeatures,
Expand Down Expand Up @@ -255,6 +255,8 @@ impl From<ExportIndex> for ExportKind {
E::Table(_) => Self::Table,
E::Memory(_) => Self::Memory,
E::Global(_) => Self::Global,
#[cfg(feature = "sp1")]
E::Tag(_) => Self::Tag,
}
}
}
Expand Down Expand Up @@ -301,6 +303,7 @@ pub struct WasmBinary<'a> {
}

pub fn parse<'a>(input: &'a [u8], path: &'_ Path) -> Result<WasmBinary<'a>> {
#[cfg(not(feature = "sp1"))]
let features = WasmFeatures {
mutable_global: true,
saturating_float_to_int: true,
Expand All @@ -324,6 +327,17 @@ pub fn parse<'a>(input: &'a [u8], path: &'_ Path) -> Result<WasmBinary<'a>> {
component_model_values: false,
component_model_nested_names: false,
};
#[cfg(feature = "sp1")]
let features = {
let mut features = WasmFeatures::empty();
features.set(WasmFeatures::MUTABLE_GLOBAL, true);
features.set(WasmFeatures::SATURATING_FLOAT_TO_INT, true);
features.set(WasmFeatures::SIGN_EXTENSION, true);
features.set(WasmFeatures::MULTI_VALUE, true);
features.set(WasmFeatures::BULK_MEMORY, true); // not all ops supported yet
features.set(WasmFeatures::FLOATS, true);
features
};
Validator::new_with_features(features)
.validate_all(input)
.wrap_err_with(|| eyre!("failed to validate {}", path.to_string_lossy().red()))?;
Expand Down Expand Up @@ -429,7 +443,11 @@ pub fn parse<'a>(input: &'a [u8], path: &'_ Path) -> Result<WasmBinary<'a>> {
}

// CHECK: maybe reader.data_offset()
#[cfg(not(feature = "sp1"))]
let name_reader = NameSectionReader::new(reader.data(), 0);
#[cfg(feature = "sp1")]
let name_reader =
NameSectionReader::new(wasmparser::BinaryReader::new(reader.data(), 0));

for name in name_reader {
match name? {
Expand Down
48 changes: 1 addition & 47 deletions arbitrator/prover/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +8,14 @@ use crate::{
machine::{Function, InboxIdentifier},
programs::StylusData,
utils,
value::{ArbValueType, FunctionType},
value::{ArbValueType, FunctionType, InternalFunc},
wavm::{wasm_to_wavm, Instruction, Opcode},
};
use arbutil::{evm::user::UserOutcomeKind, Color, PreimageType};
use eyre::{bail, ErrReport, Result};
use lazy_static::lazy_static;
use num_derive::FromPrimitive;
use std::{collections::HashMap, path::Path, str::FromStr};

/// Represents the internal hostio functions a module may have.
#[derive(Clone, Copy, Debug, FromPrimitive)]
#[repr(u64)]
pub enum InternalFunc {
WavmCallerLoad8,
WavmCallerLoad32,
WavmCallerStore8,
WavmCallerStore32,
MemoryFill,
MemoryCopy,
UserInkLeft,
UserInkStatus,
UserSetInk,
UserStackLeft,
UserSetStack,
UserMemorySize,
CallMain,
}

impl InternalFunc {
pub fn ty(&self) -> FunctionType {
use ArbValueType::*;
use InternalFunc::*;
macro_rules! func {
([$($args:expr),*], [$($outs:expr),*]) => {
FunctionType::new(vec![$($args),*], vec![$($outs),*])
};
}
#[rustfmt::skip]
let ty = match self {
WavmCallerLoad8 | WavmCallerLoad32 => func!([I32], [I32]),
WavmCallerStore8 | WavmCallerStore32 => func!([I32, I32], []),
MemoryFill | MemoryCopy => func!([I32, I32, I32], []),
UserInkLeft => func!([], [I64]), // λ() → ink_left
UserInkStatus => func!([], [I32]), // λ() → ink_status
UserSetInk => func!([I64, I32], []), // λ(ink_left, ink_status)
UserStackLeft => func!([], [I32]), // λ() → stack_left
UserSetStack => func!([I32], []), // λ(stack_left)
UserMemorySize => func!([], [I32]), // λ() → memory_size
CallMain => func!([I32], [I32]), // λ(args_len) → status
};
ty
}
}

/// Represents the internal hostio functions a module may have.
pub enum Hostio {
WavmCallerLoad8,
Expand Down
32 changes: 1 addition & 31 deletions arbitrator/prover/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
};
use arbutil::Bytes32;
use digest::Digest;
use eyre::{bail, ErrReport, Result};
use eyre::{bail, Result};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use sha3::Keccak256;
Expand All @@ -16,8 +16,6 @@ use std::{borrow::Cow, collections::HashSet, convert::TryFrom};
#[cfg(feature = "counters")]
use std::sync::atomic::{AtomicUsize, Ordering};

use wasmer_types::Pages;

#[cfg(feature = "rayon")]
use rayon::prelude::*;

Expand All @@ -37,34 +35,6 @@ pub fn print_counters() {
);
}

pub struct MemoryType {
pub min: Pages,
pub max: Option<Pages>,
}

impl MemoryType {
pub fn new(min: Pages, max: Option<Pages>) -> Self {
Self { min, max }
}
}

impl From<&wasmer_types::MemoryType> for MemoryType {
fn from(value: &wasmer_types::MemoryType) -> Self {
Self::new(value.minimum, value.maximum)
}
}

impl TryFrom<&wasmparser::MemoryType> for MemoryType {
type Error = ErrReport;

fn try_from(value: &wasmparser::MemoryType) -> std::result::Result<Self, Self::Error> {
Ok(Self {
min: Pages(value.initial.try_into()?),
max: value.maximum.map(|x| x.try_into()).transpose()?.map(Pages),
})
}
}

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Memory {
buffer: Vec<u8>,
Expand Down
11 changes: 10 additions & 1 deletion arbitrator/prover/src/parse_input.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use arbutil::Bytes32;
use serde::Deserialize;
use serde_json;
use serde_with::base64::Base64;
use serde_with::As;
use serde_with::DisplayFromStr;
use serde_with::base64::Base64;
use std::{
collections::HashMap,
io::{self, BufRead},
Expand Down Expand Up @@ -54,13 +54,22 @@ impl AsRef<[u8]> for UserWasm {
}

/// The Vec<u8> is compressed using brotli, and must be decompressed before use.
#[cfg(not(feature = "sp1"))]
impl From<Vec<u8>> for UserWasm {
fn from(data: Vec<u8>) -> Self {
let decompressed = brotli::decompress(&data, brotli::Dictionary::Empty).unwrap();
Self(decompressed)
}
}

/// Due to alignment reason, we will do the decompression elsewhere in SP1.
#[cfg(feature = "sp1")]
impl From<Vec<u8>> for UserWasm {
fn from(data: Vec<u8>) -> Self {
Self(data)
}
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct BatchInfo {
Expand Down
3 changes: 1 addition & 2 deletions arbitrator/prover/src/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE.md

use crate::{
host::InternalFunc,
machine::Module,
value::{FunctionType, Value},
value::{FunctionType, InternalFunc, Value},
wavm::{self, Opcode},
};
use arbutil::Color;
Expand Down
18 changes: 11 additions & 7 deletions arbitrator/prover/src/programs/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ use wasmparser::Operator;
#[cfg(feature = "native")]
use {
super::{
counter::Counter, depth::DepthChecker, dynamic::DynamicMeter, heap::HeapBound,
meter::Meter, start::StartMover, MiddlewareWrapper,
MiddlewareWrapper, counter::Counter, depth::DepthChecker, dynamic::DynamicMeter,
heap::HeapBound, meter::Meter, start::StartMover,
},
std::sync::Arc,
wasmer::{Cranelift, CraneliftOptLevel, Engine, Store, Target},
wasmer_compiler_singlepass::Singlepass,
};

#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -177,8 +175,16 @@ impl CompileConfig {

config
}
}

#[cfg(feature = "native")]
#[cfg(all(feature = "native", not(feature = "sp1")))]
use {
wasmer::{Cranelift, CraneliftOptLevel, Engine, Store, Target},
wasmer_compiler_singlepass::Singlepass,
};

#[cfg(all(feature = "native", not(feature = "sp1")))]
impl CompileConfig {
fn engine_type(&self, target: Target, cranelift: bool) -> Engine {
use wasmer::sys::EngineBuilder;

Expand Down Expand Up @@ -217,13 +223,11 @@ impl CompileConfig {
.into()
}

#[cfg(feature = "native")]
// cranelift only matters for compilation and not usually needed
pub fn engine(&self, target: Target) -> Engine {
self.engine_type(target, true)
}

#[cfg(feature = "native")]
pub fn store(&self, target: Target, cranelift: bool) -> Store {
Store::new(self.engine_type(target, cranelift))
}
Expand Down
9 changes: 6 additions & 3 deletions arbitrator/prover/src/programs/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE.md

use super::{FuncMiddleware, Middleware, ModuleMod};
use crate::Machine;

#[cfg(feature = "sp1")]
use crate::operator::{OperatorCode, OperatorInfo};
#[cfg(not(feature = "sp1"))]
use arbutil::operator::{OperatorCode, OperatorInfo};
use eyre::{eyre, Result};
use eyre::{Result, eyre};
use fnv::FnvHashMap as HashMap;
use lazy_static::lazy_static;
use parking_lot::Mutex;
Expand Down Expand Up @@ -139,7 +141,8 @@ pub trait CountingMachine {
fn operator_counts(&mut self) -> Result<BTreeMap<OperatorCode, u64>>;
}

impl CountingMachine for Machine {
#[cfg(not(feature = "sp1"))]
impl CountingMachine for crate::Machine {
fn operator_counts(&mut self) -> Result<BTreeMap<OperatorCode, u64>> {
let mut counts = BTreeMap::new();

Expand Down
12 changes: 8 additions & 4 deletions arbitrator/prover/src/programs/depth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE.md

use super::{
config::{CompileMemoryParams, SigMap},
FuncMiddleware, Middleware, ModuleMod,
config::{CompileMemoryParams, SigMap},
};
use crate::{host::InternalFunc, value::FunctionType, Machine};
use crate::value::{FunctionType, InternalFunc};

use arbutil::Color;
use eyre::{bail, Result};
use eyre::{Result, bail};
use fnv::FnvHashMap as HashMap;
use parking_lot::RwLock;
use std::sync::Arc;
Expand Down Expand Up @@ -501,6 +501,9 @@ impl FuncDepthChecker<'_> {
I16x8RelaxedQ15mulrS, I16x8RelaxedDotI8x16I7x16S, I32x4RelaxedDotI8x16I7x16AddS
)
) => bail!("SIMD extension not supported {unsupported:?}"),

#[cfg(feature = "sp1")]
_ => todo!("New operator not covered yet by nitro!")
};
}

Expand Down Expand Up @@ -529,7 +532,8 @@ pub trait DepthCheckedMachine {
fn set_stack(&mut self, size: u32);
}

impl DepthCheckedMachine for Machine {
#[cfg(not(feature = "sp1"))]
impl DepthCheckedMachine for crate::Machine {
fn stack_left(&mut self) -> u32 {
let global = self.get_global(STYLUS_STACK_LEFT).unwrap();
global.try_into().expect("instrumentation type mismatch")
Expand Down
4 changes: 2 additions & 2 deletions arbitrator/prover/src/programs/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE.md

use super::{
FuncMiddleware, Middleware, ModuleMod,
config::CompilePricingParams,
meter::{STYLUS_INK_LEFT, STYLUS_INK_STATUS},
FuncMiddleware, Middleware, ModuleMod,
};
use eyre::{bail, Result};
use eyre::{Result, bail};
use parking_lot::RwLock;
use wasmer_types::{GlobalIndex, GlobalInit, LocalFunctionIndex, Type};
use wasmparser::{BlockType, Operator};
Expand Down
Loading