Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

New Module Hash Model #168

Merged
merged 20 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 5 additions & 2 deletions arbitrator/jit/src/gostack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,11 @@ impl GoStack {
data
}

pub fn write_slice<T: TryInto<u32>>(&self, ptr: T, src: &[u8]) {
let ptr: u32 = ptr.try_into().map_err(|_| "Go pointer not a u32").unwrap();
pub fn write_slice<T: TryInto<u32>>(&self, ptr: T, src: &[u8])
where
T::Error: Debug,
{
let ptr: u32 = ptr.try_into().expect("Go pointer not a u32");
self.view().write(ptr.into(), src).unwrap();
}

Expand Down
2 changes: 1 addition & 1 deletion arbitrator/jit/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn read_bytes<T: Read>(reader: &mut BufReader<T>) -> Result<Vec<u8>, io::Err
Ok(buf)
}

pub fn read_box<T: Read>(reader: &mut BufReader<T>) -> Result<Box<[u8]>, io::Error> {
pub fn read_boxed_slice<T: Read>(reader: &mut BufReader<T>) -> Result<Box<[u8]>, io::Error> {
Ok(Vec::into_boxed_slice(read_bytes(reader)?))
}

Expand Down
8 changes: 5 additions & 3 deletions arbitrator/jit/src/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ use arbutil::{
format::DebugBytes,
heapify,
};
use prover::programs::{config::PricingParams, prelude::*};
use prover::{
machine::Module,
programs::{config::PricingParams, prelude::*},
};
use std::mem;
use stylus::native;

mod evm_api;

Expand Down Expand Up @@ -54,7 +56,7 @@ pub fn stylus_activate(env: WasmEnvMut, sp: u32) {
}

let gas_left = &mut sp.read_u64_raw(gas);
let (_, module, pages) = match native::activate(&wasm, version, page_limit, debug, gas_left) {
let (module, pages) = match Module::activate(&wasm, version, page_limit, debug, gas_left) {
Ok(result) => result,
Err(error) => error!(error),
};
Expand Down
2 changes: 1 addition & 1 deletion arbitrator/jit/src/wavmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ fn ready_hostio(env: &mut WasmEnv) -> MaybeEscape {
let programs_count = socket::read_u32(stream)?;
for _ in 0..programs_count {
let module_hash = socket::read_bytes32(stream)?;
let module_asm = socket::read_box(stream)?;
let module_asm = socket::read_boxed_slice(stream)?;
env.module_asms.insert(module_hash, module_asm.into());
}

Expand Down
2 changes: 1 addition & 1 deletion arbitrator/prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub fn str_to_c_string(text: &str) -> *mut libc::c_char {
panic!("Failed to allocate memory for error string");
}
ptr::copy_nonoverlapping(text.as_ptr(), buf as *mut u8, text.len());
*(buf.add(text.len()) as *mut u8) = 0;
*(buf as *mut u8).add(text.len()) = 0;
buf as *mut libc::c_char
}
}
Expand Down
23 changes: 11 additions & 12 deletions arbitrator/prover/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,30 +286,29 @@ pub struct Module {
}

lazy_static! {
static ref USER_IMPORTS: Result<HashMap<String, AvailableImport>> = Module::calc_user_imports();
static ref USER_IMPORTS: HashMap<String, AvailableImport> = Module::calc_user_imports();
}

impl Module {
const FORWARDING_PREFIX: &str = "arbitrator_forward__";

fn calc_user_imports() -> Result<HashMap<String, AvailableImport>> {
let forward_bytes = include_bytes!("../../../target/machines/latest/forward_stub.wasm");
let forward_bin = binary::parse(forward_bytes, Path::new("forward")).unwrap();
fn calc_user_imports() -> HashMap<String, AvailableImport> {
let mut imports = HashMap::default();

let mut available_imports = HashMap::default();
let forward = include_bytes!("../../../target/machines/latest/forward_stub.wasm");
let forward = binary::parse(forward, Path::new("forward")).unwrap();

for (name, &(export, kind)) in &forward_bin.exports {
for (name, &(export, kind)) in &forward.exports {
if kind == ExportKind::Func {
let ty = match forward_bin.get_function(FunctionIndex::from_u32(export)) {
let ty = match forward.get_function(FunctionIndex::from_u32(export)) {
Ok(ty) => ty,
Err(error) => bail!("failed to read export {}: {}", name, error),
Err(error) => panic!("failed to read export {name}: {error:?}"),
};
let import = AvailableImport::new(ty, 1, export);
available_imports.insert(name.to_owned(), import);
imports.insert(name.to_owned(), import);
}
}

Ok(available_imports)
imports
}

fn from_binary(
Expand Down Expand Up @@ -573,7 +572,7 @@ impl Module {
) -> Result<Module> {
Self::from_binary(
bin,
USER_IMPORTS.as_ref().unwrap(),
&USER_IMPORTS,
&HashMap::default(),
false,
debug_funcs,
Expand Down
3 changes: 1 addition & 2 deletions arbitrator/prover/src/programs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
binary::{ExportKind, WasmBinary},
machine::Module,
memory::MemoryType,
programs::config::CompileConfig,
value::{FunctionType as ArbFunctionType, Value},
};
use arbutil::Color;
Expand All @@ -17,8 +18,6 @@ use wasmer_types::{
};
use wasmparser::{Operator, Type as WpType};

use self::config::CompileConfig;

#[cfg(feature = "native")]
use {
super::value,
Expand Down
3 changes: 3 additions & 0 deletions arbitrator/prover/test-cases/dynamic.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
(import "hostio" "program_ink_left" (func $ink_left (param i32) (result i64)))
(import "hostio" "program_ink_status" (func $ink_status (param i32) (result i32)))
(import "hostio" "program_call_main" (func $user_func (param i32 i32) (result i32)))

;; WAVM Module hash
(data (i32.const 0x0)
"\97\0c\df\6a\a9\bf\d4\3c\03\80\7f\8a\7e\67\9a\5c\12\05\94\4f\c6\5e\39\9e\00\df\5c\b3\7d\de\55\ad") ;; user

(func $start (local $user i32) (local $internals i32)
;; link in user.wat
i32.const 0
Expand Down
45 changes: 24 additions & 21 deletions arbitrator/prover/test-cases/link.wat
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,35 @@
(module
(import "hostio" "wavm_link_module" (func $link (param i32) (result i32)))
(import "hostio" "wavm_unlink_module" (func $unlink (param) (result)))
(data (i32.const 0x0)
"\6d\c0\9f\17\5f\5b\e8\73\64\bc\79\62\e8\13\fd\cb\09\2a\12\24\87\4a\af\15\f2\e1\2e\93\b0\95\30\9a")
(data (i32.const 0x20)
"\f5\6b\4c\c7\19\da\61\01\e4\e4\9a\f1\04\ca\29\97\fd\07\05\d6\c2\3b\e6\55\70\c5\54\65\a0\3f\3d\ee")
(data (i32.const 0x40)
"\57\27\40\77\40\da\77\f8\1f\fd\81\cb\00\e0\02\17\40\f0\be\e4\11\89\0a\56\ba\80\e4\b9\31\74\13\a2")
(data (i32.const 0x60)
"\53\36\71\e6\bf\90\0f\50\fd\18\5f\44\d6\18\77\2f\70\17\19\2a\1a\8d\b6\92\5a\3c\14\1a\af\86\81\d4")
(data (i32.const 0x80)
"\97\0c\df\6a\a9\bf\d4\3c\03\80\7f\8a\7e\67\9a\5c\12\05\94\4f\c6\5e\39\9e\00\df\5c\b3\7d\de\55\ad")
(data (i32.const 0xa0)
"\c7\db\9f\8e\ed\13\ac\66\72\62\76\65\93\1b\9a\64\03\c3\c8\21\44\92\5c\8d\bc\1a\d6\bd\65\f8\2b\20")
(data (i32.const 0xc0)
"\83\46\03\41\b4\5f\a6\e6\a3\0d\e9\fc\79\fc\3c\d6\c9\c3\c7\ac\97\42\bc\48\54\92\e6\84\08\37\07\a6")
(data (i32.const 0xe0)
"\42\1d\62\e9\9a\51\d4\71\ce\50\6e\b4\83\72\18\ea\f8\ab\ab\b9\29\b8\bd\6d\66\ea\52\b3\3d\50\26\34")

;; WAVM module hashes
(data (i32.const 0x000)
"\74\22\43\ad\22\2e\e5\6d\f4\bb\3f\0b\09\76\0a\bf\51\b7\17\a4\c5\50\c9\5b\45\be\ea\ed\4c\57\4d\17") ;; block
(data (i32.const 0x020)
"\53\36\71\e6\bf\90\0f\50\fd\18\5f\44\d6\18\77\2f\70\17\19\2a\1a\8d\b6\92\5a\3c\14\1a\af\86\81\d4") ;; call
(data (i32.const 0x040)
"\57\27\40\77\40\da\77\f8\1f\fd\81\cb\00\e0\02\17\40\f0\be\e4\11\89\0a\56\ba\80\e4\b9\31\74\13\a2") ;; indirect
(data (i32.const 0x060)
"\3f\c3\a1\eb\a6\62\70\2b\3b\fa\dc\5b\29\22\11\6f\58\4a\6e\e5\70\60\6f\cf\6c\66\d8\c9\77\c5\c9\23") ;; const
(data (i32.const 0x080)
"\83\46\03\41\b4\5f\a6\e6\a3\0d\e9\fc\79\fc\3c\d6\c9\c3\c7\ac\97\42\bc\48\54\92\e6\84\08\37\07\a6") ;; div
(data (i32.const 0x0a0)
"\16\90\98\f2\7f\8d\bf\73\90\b9\eb\94\9f\b9\41\cd\c3\93\2e\30\b8\12\1b\d5\87\98\18\26\f2\62\7d\2c") ;; globals
(data (i32.const 0x0c0)
"\f5\6b\4c\c7\19\da\61\01\e4\e4\9a\f1\04\ca\29\97\fd\07\05\d6\c2\3b\e6\55\70\c5\54\65\a0\3f\3d\ee") ;; if-else
(data (i32.const 0x0e0)
"\42\1d\62\e9\9a\51\d4\71\ce\50\6e\b4\83\72\18\ea\f8\ab\ab\b9\29\b8\bd\6d\66\ea\52\b3\3d\50\26\34") ;; locals
(data (i32.const 0x100)
"\74\22\43\ad\22\2e\e5\6d\f4\bb\3f\0b\09\76\0a\bf\51\b7\17\a4\c5\50\c9\5b\45\be\ea\ed\4c\57\4d\17")
"\6d\c0\9f\17\5f\5b\e8\73\64\bc\79\62\e8\13\fd\cb\09\2a\12\24\87\4a\af\15\f2\e1\2e\93\b0\95\30\9a") ;; loop
(data (i32.const 0x120)
"\16\90\98\f2\7f\8d\bf\73\90\b9\eb\94\9f\b9\41\cd\c3\93\2e\30\b8\12\1b\d5\87\98\18\26\f2\62\7d\2c")
"\a7\66\cb\0e\c4\31\ea\16\fd\c6\2f\d3\11\ca\4a\78\f8\48\6a\69\0a\4c\b9\1c\fc\47\f8\b6\63\6d\80\fa") ;; math
(data (i32.const 0x140)
"\3f\c3\a1\eb\a6\62\70\2b\3b\fa\dc\5b\29\22\11\6f\58\4a\6e\e5\70\60\6f\cf\6c\66\d8\c9\77\c5\c9\23")
"\ea\02\78\f7\a3\b3\e0\0e\55\f6\8f\13\87\d6\6f\04\38\b3\6b\4c\d5\33\e2\3d\0b\36\71\9f\57\f5\f0\59") ;; iops
(data (i32.const 0x160)
"\a7\66\cb\0e\c4\31\ea\16\fd\c6\2f\d3\11\ca\4a\78\f8\48\6a\69\0a\4c\b9\1c\fc\47\f8\b6\63\6d\80\fa")
"\97\0c\df\6a\a9\bf\d4\3c\03\80\7f\8a\7e\67\9a\5c\12\05\94\4f\c6\5e\39\9e\00\df\5c\b3\7d\de\55\ad") ;; user
(data (i32.const 0x180)
"\ea\02\78\f7\a3\b3\e0\0e\55\f6\8f\13\87\d6\6f\04\38\b3\6b\4c\d5\33\e2\3d\0b\36\71\9f\57\f5\f0\59")
"\c7\db\9f\8e\ed\13\ac\66\72\62\76\65\93\1b\9a\64\03\c3\c8\21\44\92\5c\8d\bc\1a\d6\bd\65\f8\2b\20") ;; return

(func $start (local $counter i32)

;; add modules
Expand Down
27 changes: 14 additions & 13 deletions arbitrator/stylus/src/evm_api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2022-2023, Offchain Labs, Inc.
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE

use crate::{RustSlice, RustVec};
use crate::{RustBytes, RustSlice};
use arbutil::{
evm::{
api::{EvmApi, EvmApiStatus},
Expand All @@ -19,7 +19,7 @@ pub struct GoEvmApi {
key: Bytes32,
value: Bytes32,
gas_cost: *mut u64,
error: *mut RustVec,
error: *mut RustBytes,
) -> EvmApiStatus,
pub contract_call: unsafe extern "C" fn(
id: usize,
Expand All @@ -45,30 +45,31 @@ pub struct GoEvmApi {
) -> EvmApiStatus,
pub create1: unsafe extern "C" fn(
id: usize,
code: *mut RustVec,
code: *mut RustBytes,
endowment: Bytes32,
gas: *mut u64,
return_data_len: *mut u32,
) -> EvmApiStatus,
pub create2: unsafe extern "C" fn(
id: usize,
code: *mut RustVec,
code: *mut RustBytes,
endowment: Bytes32,
salt: Bytes32,
gas: *mut u64,
return_data_len: *mut u32,
) -> EvmApiStatus,
pub get_return_data:
unsafe extern "C" fn(id: usize, output: *mut RustVec, offset: u32, size: u32),
pub emit_log: unsafe extern "C" fn(id: usize, data: *mut RustVec, topics: u32) -> EvmApiStatus,
unsafe extern "C" fn(id: usize, output: *mut RustBytes, offset: u32, size: u32),
pub emit_log:
unsafe extern "C" fn(id: usize, data: *mut RustBytes, topics: u32) -> EvmApiStatus,
pub account_balance:
unsafe extern "C" fn(id: usize, address: Bytes20, gas_cost: *mut u64) -> Bytes32, // balance
pub account_codehash:
unsafe extern "C" fn(id: usize, address: Bytes20, gas_cost: *mut u64) -> Bytes32, // codehash
pub add_pages: unsafe extern "C" fn(id: usize, pages: u16) -> u64, // gas cost
pub capture_hostio: unsafe extern "C" fn(
id: usize,
name: *mut RustVec,
name: *mut RustBytes,
args: *mut RustSlice,
outs: *mut RustSlice,
start_ink: u64,
Expand Down Expand Up @@ -106,7 +107,7 @@ impl EvmApi for GoEvmApi {
}

fn set_bytes32(&mut self, key: Bytes32, value: Bytes32) -> Result<u64> {
let mut error = RustVec::new(vec![]);
let mut error = RustBytes::new(vec![]);
let mut cost = 0;
let api_status = call!(self, set_bytes32, key, value, ptr!(cost), ptr!(error));
let error = into_vec!(error); // done here to always drop
Expand Down Expand Up @@ -183,7 +184,7 @@ impl EvmApi for GoEvmApi {
) -> (Result<Bytes20>, u32, u64) {
let mut call_gas = gas; // becomes the call's cost
let mut return_data_len = 0;
let mut code = RustVec::new(code);
let mut code = RustBytes::new(code);
let api_status = call!(
self,
create1,
Expand All @@ -209,7 +210,7 @@ impl EvmApi for GoEvmApi {
) -> (Result<Bytes20>, u32, u64) {
let mut call_gas = gas; // becomes the call's cost
let mut return_data_len = 0;
let mut code = RustVec::new(code);
let mut code = RustBytes::new(code);
let api_status = call!(
self,
create2,
Expand All @@ -228,13 +229,13 @@ impl EvmApi for GoEvmApi {
}

fn get_return_data(&mut self, offset: u32, size: u32) -> Vec<u8> {
let mut data = RustVec::new(vec![]);
let mut data = RustBytes::new(vec![]);
call!(self, get_return_data, ptr!(data), offset, size);
into_vec!(data)
}

fn emit_log(&mut self, data: Vec<u8>, topics: u32) -> Result<()> {
let mut data = RustVec::new(data);
let mut data = RustBytes::new(data);
let api_status = call!(self, emit_log, ptr!(data), topics);
let error = into_vec!(data); // done here to always drop
match api_status {
Expand Down Expand Up @@ -263,7 +264,7 @@ impl EvmApi for GoEvmApi {
call!(
self,
capture_hostio,
ptr!(RustVec::new(name.as_bytes().to_vec())),
ptr!(RustBytes::new(name.as_bytes().to_vec())),
ptr!(RustSlice::new(args)),
ptr!(RustSlice::new(outs)),
start_ink,
Expand Down
12 changes: 6 additions & 6 deletions arbitrator/stylus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ impl<'a> RustSlice<'a> {
}

#[repr(C)]
pub struct RustVec {
pub struct RustBytes {
ptr: *mut u8,
len: usize,
cap: usize,
}

impl RustVec {
impl RustBytes {
fn new(vec: Vec<u8>) -> Self {
let mut rust_vec = Self {
ptr: std::ptr::null_mut(),
Expand Down Expand Up @@ -117,7 +117,7 @@ pub unsafe extern "C" fn stylus_activate(
page_limit: u16,
version: u16,
debug: bool,
output: *mut RustVec,
output: *mut RustBytes,
asm_len: *mut usize,
module_hash: *mut Bytes32,
footprint: *mut u16,
Expand Down Expand Up @@ -156,7 +156,7 @@ pub unsafe extern "C" fn stylus_call(
go_api: GoEvmApi,
evm_data: EvmData,
debug_chain: u32,
output: *mut RustVec,
output: *mut RustBytes,
gas: *mut u64,
) -> UserOutcomeKind {
let module = module.slice();
Expand Down Expand Up @@ -191,7 +191,7 @@ pub unsafe extern "C" fn stylus_call(
///
/// Must only be called once per vec.
#[no_mangle]
pub unsafe extern "C" fn stylus_drop_vec(vec: RustVec) {
pub unsafe extern "C" fn stylus_drop_vec(vec: RustBytes) {
if !vec.ptr.is_null() {
mem::drop(vec.into_vec())
}
Expand All @@ -203,7 +203,7 @@ pub unsafe extern "C" fn stylus_drop_vec(vec: RustVec) {
///
/// `rust` must not be null.
#[no_mangle]
pub unsafe extern "C" fn stylus_vec_set_bytes(rust: *mut RustVec, data: GoSliceData) {
pub unsafe extern "C" fn stylus_vec_set_bytes(rust: *mut RustBytes, data: GoSliceData) {
let rust = &mut *rust;
let mut vec = Vec::from_raw_parts(rust.ptr, rust.len, rust.cap);
vec.clear();
Expand Down
5 changes: 3 additions & 2 deletions arbitrator/tools/module_roots/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion arbitrator/tools/module_roots/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn main() -> Result<()> {
for module in &opts.stylus_modules {
let error = || format!("failed to read module at {}", module.to_string_lossy());
let wasm = file_bytes(module).wrap_err_with(error)?;
let hash = mach.add_program(&wasm, 1, true, None).wrap_err_with(error)?;
let hash = mach.add_program(&wasm, 1, true).wrap_err_with(error)?;
let name = module.file_stem().unwrap().to_string_lossy();
stylus.push((name.to_owned(), hash));
println!("{} {}", name, hash);
Expand Down
2 changes: 1 addition & 1 deletion arbos/burn/burn.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type Burner interface {
Burn(amount uint64) error
Burned() uint64
GasLeft() *uint64
GasLeft() *uint64 // SystemBurner's panic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The apostrophe doesn't read well to me, could you do

// `SystemBurner`s panic

instead?

BurnOut() error
Restrict(err error)
HandleError(err error) error
Expand Down
Loading
Loading