Skip to content

Commit

Permalink
GuestPtr, speed up read/write, docstrings, debug prints, wrap macro, …
Browse files Browse the repository at this point in the history
…other tweaks
  • Loading branch information
rachel-bousfield committed Mar 8, 2024
1 parent a186fc3 commit 5170ae3
Show file tree
Hide file tree
Showing 45 changed files with 1,222 additions and 942 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ WASI_SYSROOT?=/opt/wasi-sdk/wasi-sysroot

arbitrator_wasm_lib_flags=$(patsubst %, -l %, $(arbitrator_wasm_libs))

rust_arbutil_files = $(wildcard arbitrator/arbutil/src/*.* arbitrator/arbutil/src/*/*.* arbitrator/arbutil/*.toml arbitrator/callerenv/src/*.* arbitrator/callerenv/src/*/*.* arbitrator/callerenv/*.toml)
rust_arbutil_files = $(wildcard arbitrator/arbutil/src/*.* arbitrator/arbutil/src/*/*.* arbitrator/arbutil/*.toml arbitrator/caller-env/src/*.* arbitrator/caller-env/src/*/*.* arbitrator/caller-env/*.toml)

prover_direct_includes = $(patsubst %,$(output_latest)/%.wasm, forward forward_stub)
prover_src = arbitrator/prover/src
Expand Down
17 changes: 10 additions & 7 deletions arbitrator/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/arbutil/src/evm/req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<D: DataReader, H: RequestHandler<D>> EvmApiRequestor<D, H> {

let (mut res, data, cost) = self.handle_request(create_type, &request);
if res.len() != 21 || res[0] == 0 {
if res.len() > 0 {
if !res.is_empty() {
res.drain(0..=0);
}
let err_string =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
[package]
name = "callerenv"
name = "caller-env"
version = "0.1.0"
edition = "2021"

[dependencies]
num_enum = { version = "0.7.2", default-features = false }
rand_pcg = { version = "0.3.1", default-features = false }
rand = { version = "0.8.4", default-features = false }
wasmer = { path = "../tools/wasmer/lib/api", optional = true }

[features]
static_caller = []
wasmer_traits = ["dep:wasmer"]
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// Copyright 2021-2023, Offchain Labs, Inc.
// Copyright 2021-2024, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE
use crate::{ExecEnv, MemAccess, Uptr};

#![allow(clippy::too_many_arguments)]

use crate::{ExecEnv, GuestPtr, MemAccess};
use alloc::vec;
use num_enum::{IntoPrimitive, TryFromPrimitive};

#[derive(PartialEq)]
#[derive(PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u32)]
pub enum BrotliStatus {
Failure,
Expand Down Expand Up @@ -31,22 +35,22 @@ extern "C" {

const BROTLI_MODE_GENERIC: u32 = 0;

/// Brotli decompresses a go slice1
/// Brotli decompresses a go slice.
///
/// # Safety
///
/// The output buffer must be sufficiently large enough.
pub fn brotli_decompress<M: MemAccess, E: ExecEnv>(
mem: &mut M,
_env: &mut E,
in_buf_ptr: Uptr,
in_buf_ptr: GuestPtr,
in_buf_len: u32,
out_buf_ptr: Uptr,
out_len_ptr: Uptr,
) -> u32 {
out_buf_ptr: GuestPtr,
out_len_ptr: GuestPtr,
) -> BrotliStatus {
let in_slice = mem.read_slice(in_buf_ptr, in_buf_len as usize);
let orig_output_len = mem.read_u32(out_len_ptr) as usize;
let mut output = vec![0u8; orig_output_len as usize];
let mut output = vec![0; orig_output_len];
let mut output_len = orig_output_len;
unsafe {
let res = BrotliDecoderDecompress(
Expand All @@ -56,30 +60,30 @@ pub fn brotli_decompress<M: MemAccess, E: ExecEnv>(
output.as_mut_ptr(),
);
if (res != BrotliStatus::Success) || (output_len > orig_output_len) {
return 0;
return BrotliStatus::Failure;
}
}
mem.write_slice(out_buf_ptr, &output[..output_len]);
mem.write_u32(out_len_ptr, output_len as u32);
1
BrotliStatus::Success
}

/// Brotli compresses a go slice
///
/// The output buffer must be sufficiently large enough.
/// The output buffer must be large enough.
pub fn brotli_compress<M: MemAccess, E: ExecEnv>(
mem: &mut M,
_env: &mut E,
in_buf_ptr: Uptr,
in_buf_ptr: GuestPtr,
in_buf_len: u32,
out_buf_ptr: Uptr,
out_len_ptr: Uptr,
out_buf_ptr: GuestPtr,
out_len_ptr: GuestPtr,
level: u32,
window_size: u32,
) -> u32 {
) -> BrotliStatus {
let in_slice = mem.read_slice(in_buf_ptr, in_buf_len as usize);
let orig_output_len = mem.read_u32(out_len_ptr) as usize;
let mut output = vec![0u8; orig_output_len];
let mut output = vec![0; orig_output_len];
let mut output_len = orig_output_len;

unsafe {
Expand All @@ -93,10 +97,10 @@ pub fn brotli_compress<M: MemAccess, E: ExecEnv>(
output.as_mut_ptr(),
);
if (res != BrotliStatus::Success) || (output_len > orig_output_len) {
return 0;
return BrotliStatus::Failure;
}
}
mem.write_slice(out_buf_ptr, &output[..output_len]);
mem.write_u32(out_len_ptr, output_len as u32);
1
BrotliStatus::Success
}
49 changes: 49 additions & 0 deletions arbitrator/caller-env/src/guest_ptr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2024, Offchain Labs, Inc.
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE

use core::ops::{Add, AddAssign, Deref, DerefMut};

/// Represents a pointer to a Guest WASM's memory.
#[derive(Clone, Copy, Eq, PartialEq)]
#[repr(transparent)]
pub struct GuestPtr(pub u32);

impl Add<u32> for GuestPtr {
type Output = Self;

fn add(self, rhs: u32) -> Self::Output {
Self(self.0 + rhs)
}
}

impl AddAssign<u32> for GuestPtr {
fn add_assign(&mut self, rhs: u32) {
*self = *self + rhs;
}
}

impl From<GuestPtr> for u32 {
fn from(value: GuestPtr) -> Self {
value.0
}
}

impl From<GuestPtr> for u64 {
fn from(value: GuestPtr) -> Self {
value.0.into()
}
}

impl Deref for GuestPtr {
type Target = u32;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for GuestPtr {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
66 changes: 66 additions & 0 deletions arbitrator/caller-env/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2021-2024, Offchain Labs, Inc.
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE

#![no_std]

extern crate alloc;

use alloc::vec::Vec;
use rand_pcg::Pcg32;

pub use brotli::BrotliStatus;
pub use guest_ptr::GuestPtr;
pub use wasip1_stub::Errno;

#[cfg(feature = "static_caller")]
pub mod static_caller;

#[cfg(feature = "wasmer_traits")]
pub mod wasmer_traits;

pub mod brotli;
mod guest_ptr;
pub mod wasip1_stub;

/// Initializes a deterministic, psuedo-random number generator with a fixed seed.
pub fn create_pcg() -> Pcg32 {
const PCG_INIT_STATE: u64 = 0xcafef00dd15ea5e5;
const PCG_INIT_STREAM: u64 = 0xa02bdbf7bb3c0a7;
Pcg32::new(PCG_INIT_STATE, PCG_INIT_STREAM)
}

/// Access Guest memory.
pub trait MemAccess {
fn read_u8(&self, ptr: GuestPtr) -> u8;

fn read_u16(&self, ptr: GuestPtr) -> u16;

fn read_u32(&self, ptr: GuestPtr) -> u32;

fn read_u64(&self, ptr: GuestPtr) -> u64;

fn write_u8(&mut self, ptr: GuestPtr, x: u8);

fn write_u16(&mut self, ptr: GuestPtr, x: u16);

fn write_u32(&mut self, ptr: GuestPtr, x: u32);

fn write_u64(&mut self, ptr: GuestPtr, x: u64);

fn read_slice(&self, ptr: GuestPtr, len: usize) -> Vec<u8>;

fn read_fixed<const N: usize>(&self, ptr: GuestPtr) -> [u8; N];

fn write_slice(&mut self, ptr: GuestPtr, data: &[u8]);
}

/// Update the Host environment.
pub trait ExecEnv {
fn advance_time(&mut self, ns: u64);

fn get_time(&self) -> u64;

fn next_rand_u32(&mut self) -> u32;

fn print_string(&mut self, message: &[u8]);
}
Loading

0 comments on commit 5170ae3

Please sign in to comment.