Skip to content

Commit

Permalink
propagate dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
rachel-bousfield committed Mar 16, 2024
1 parent 70aebed commit 72c2ab1
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 70 deletions.
7 changes: 7 additions & 0 deletions arbcompress/compress_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ const (
BrotliSuccess
)

type Dictionary uint32

const (
EmptyDictionary Dictionary = iota
StylusProgramDictionary
)

const LEVEL_FAST = 0
const LEVEL_WELL = 11
const WINDOW_SIZE = 22 // BROTLI_DEFAULT_WINDOW
Expand Down
8 changes: 6 additions & 2 deletions arbcompress/compress_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ import (
func brotliCompress(inBuf unsafe.Pointer, inBufLen uint32, outBuf unsafe.Pointer, outBufLen unsafe.Pointer, level, windowSize uint32) BrotliStatus

//go:wasmimport arbcompress brotli_decompress
func brotliDecompress(inBuf unsafe.Pointer, inBufLen uint32, outBuf unsafe.Pointer, outBufLen unsafe.Pointer) BrotliStatus
func brotliDecompress(inBuf unsafe.Pointer, inBufLen uint32, outBuf unsafe.Pointer, outBufLen unsafe.Pointer, dictionary Dictionary) BrotliStatus

func Decompress(input []byte, maxSize int) ([]byte, error) {
outBuf := make([]byte, maxSize)
outLen := uint32(len(outBuf))
status := brotliDecompress(
arbutil.SliceToUnsafePointer(input), uint32(len(input)), arbutil.SliceToUnsafePointer(outBuf), unsafe.Pointer(&outLen),
arbutil.SliceToUnsafePointer(input),
uint32(len(input)),
arbutil.SliceToUnsafePointer(outBuf),
unsafe.Pointer(&outLen),
EmptyDictionary,
)
if status != BrotliSuccess {
return nil, fmt.Errorf("failed decompression")
Expand Down
49 changes: 2 additions & 47 deletions arbitrator/caller-env/src/brotli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ type HeapItem = c_void;

// one-shot brotli API
extern "C" {
fn BrotliDecoderDecompress(
encoded_size: usize,
encoded_buffer: *const u8,
decoded_size: *mut usize,
decoded_buffer: *mut u8,
) -> BrotliStatus;

fn BrotliEncoderCompress(
quality: u32,
lgwin: u32,
Expand Down Expand Up @@ -59,8 +52,6 @@ extern "C" {
out_len: *mut usize,
) -> BrotliStatus;

fn BrotliDecoderIsFinished(state: *const DecoderState) -> BrotliBool;

fn BrotliDecoderDestroyInstance(state: *mut DecoderState);
}

Expand All @@ -72,7 +63,7 @@ const BROTLI_MODE_GENERIC: u32 = 0;
///
/// The output buffer must be sufficiently large.
/// The pointers must not be null.
pub fn brotli_decompress_with_dictionary<M: MemAccess, E: ExecEnv>(
pub fn brotli_decompress<M: MemAccess, E: ExecEnv>(
mem: &mut M,
_env: &mut E,
in_buf_ptr: GuestPtr,
Expand All @@ -98,7 +89,7 @@ pub fn brotli_decompress_with_dictionary<M: MemAccess, E: ExecEnv>(
};
}

if dictionary != Dictionary::None {
if dictionary != Dictionary::Empty {
let attatched = BrotliDecoderAttachDictionary(
state,
BrotliSharedDictionaryType::Raw,
Expand All @@ -122,7 +113,6 @@ pub fn brotli_decompress_with_dictionary<M: MemAccess, E: ExecEnv>(
&mut out_len as _,
);
require!(status == BrotliStatus::Success && out_len <= prior_out_len);
require!(BrotliDecoderIsFinished(state) == BrotliBool::True);

BrotliDecoderDestroyInstance(state);
output.set_len(out_len);
Expand All @@ -132,41 +122,6 @@ pub fn brotli_decompress_with_dictionary<M: MemAccess, E: ExecEnv>(
BrotliStatus::Success
}

/// Brotli decompresses a go slice.
///
/// # Safety
///
/// The output buffer must be sufficiently large.
/// The pointers must not be null.
pub fn brotli_decompress<M: MemAccess, E: ExecEnv>(
mem: &mut M,
_env: &mut E,
in_buf_ptr: GuestPtr,
in_buf_len: 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::with_capacity(orig_output_len);
let mut output_len = orig_output_len;
unsafe {
let res = BrotliDecoderDecompress(
in_buf_len as usize,
in_slice.as_ptr(),
&mut output_len,
output.as_mut_ptr(),
);
if (res != BrotliStatus::Success) || (output_len > orig_output_len) {
return BrotliStatus::Failure;
}
output.set_len(output_len);
}
mem.write_slice(out_buf_ptr, &output[..output_len]);
mem.write_u32(out_len_ptr, output_len as u32);
BrotliStatus::Success
}

/// Brotli compresses a go slice
///
/// The output buffer must be sufficiently large.
Expand Down
9 changes: 4 additions & 5 deletions arbitrator/caller-env/src/brotli/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#![allow(dead_code, clippy::len_without_is_empty)]

use core::ptr;
use num_enum::{IntoPrimitive, TryFromPrimitive};

#[derive(PartialEq, IntoPrimitive, TryFromPrimitive)]
Expand All @@ -28,24 +27,24 @@ pub(super) enum BrotliSharedDictionaryType {
Serialized,
}

#[derive(PartialEq)]
#[derive(PartialEq, IntoPrimitive, TryFromPrimitive)]
#[repr(u32)]
pub enum Dictionary {
None,
Empty,
StylusProgram,
}

impl Dictionary {
pub fn len(&self) -> usize {
match self {
Self::None => 0,
Self::Empty => 0,
Self::StylusProgram => todo!(),
}
}

pub fn data(&self) -> *const u8 {
match self {
Self::None => ptr::null(),
Self::Empty => [].as_ptr(),
Self::StylusProgram => todo!(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion arbitrator/caller-env/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2021-2024, Offchain Labs, Inc.
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE

#![no_std]
#![cfg_attr(target_arch = "wasm32", no_std)]

extern crate alloc;

Expand Down
14 changes: 13 additions & 1 deletion arbitrator/caller-env/src/wasmer_traits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2024, Offchain Labs, Inc.
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE

use crate::{BrotliStatus, Errno, GuestPtr};
use crate::{brotli::Dictionary, BrotliStatus, Errno, GuestPtr};
use wasmer::{FromToNativeWasmType, WasmPtr};

unsafe impl FromToNativeWasmType for GuestPtr {
Expand Down Expand Up @@ -40,6 +40,18 @@ unsafe impl FromToNativeWasmType for BrotliStatus {
}
}

unsafe impl FromToNativeWasmType for Dictionary {
type Native = i32;

fn from_native(native: i32) -> Self {
Self::try_from(u32::from_native(native)).expect("unknown brotli dictionary")
}

fn to_native(self) -> i32 {
(self as u32).to_native()
}
}

impl<T> From<GuestPtr> for WasmPtr<T> {
fn from(value: GuestPtr) -> Self {
WasmPtr::new(value.0)
Expand Down
5 changes: 3 additions & 2 deletions arbitrator/jit/src/arbcompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::caller_env::{JitEnv, JitExecEnv};
use crate::machine::Escape;
use crate::machine::WasmEnvMut;
use caller_env::brotli::BrotliStatus;
use caller_env::brotli::{BrotliStatus, Dictionary};
use caller_env::{self, GuestPtr};

macro_rules! wrap {
Expand All @@ -24,7 +24,8 @@ wrap! {
in_buf_ptr: GuestPtr,
in_buf_len: u32,
out_buf_ptr: GuestPtr,
out_len_ptr: GuestPtr
out_len_ptr: GuestPtr,
dictionary: Dictionary
) -> BrotliStatus;

fn brotli_compress(
Expand Down
4 changes: 2 additions & 2 deletions arbitrator/prover/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ impl Module {
};
ensure!(
&func.ty == have_ty,
"Import {} has different function signature than host function. Expected {} but got {}",
import_name.red(), func.ty.red(), have_ty.red(),
"Import {} for {} has different function signature than export.\nexpected {}\nbut have {}",
import_name.red(), bin.names.module.red(), func.ty.red(), have_ty.red(),
);

func_type_idxs.push(import.offset);
Expand Down
23 changes: 20 additions & 3 deletions arbitrator/wasm-libraries/brotli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

#![allow(clippy::missing_safety_doc)] // TODO: add safety docs

use caller_env::{self, brotli::BrotliStatus, GuestPtr};
use caller_env::{
self,
brotli::{BrotliStatus, Dictionary},
GuestPtr,
};
use paste::paste;

macro_rules! wrap {
Expand All @@ -24,7 +28,20 @@ macro_rules! wrap {
}

wrap! {
fn brotli_decompress(in_buf_ptr: GuestPtr, in_buf_len: u32, out_buf_ptr: GuestPtr, out_len_ptr: GuestPtr) -> BrotliStatus;
fn brotli_decompress(
in_buf_ptr: GuestPtr,
in_buf_len: u32,
out_buf_ptr: GuestPtr,
out_len_ptr: GuestPtr,
dictionary: Dictionary
) -> BrotliStatus;

fn brotli_compress(in_buf_ptr: GuestPtr, in_buf_len: u32, out_buf_ptr: GuestPtr, out_len_ptr: GuestPtr, level: u32, window_size: u32) -> BrotliStatus
fn brotli_compress(
in_buf_ptr: GuestPtr,
in_buf_len: u32,
out_buf_ptr: GuestPtr,
out_len_ptr: GuestPtr,
level: u32,
window_size: u32
) -> BrotliStatus
}
6 changes: 1 addition & 5 deletions arbitrator/wasm-libraries/user-test/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ pub unsafe extern "C" fn vm_hooks__create2(
}

#[no_mangle]
pub unsafe extern "C" fn vm_hooks__read_return_data(
dest: GuestPtr,
offset: u32,
size: u32,
) -> u32 {
pub unsafe extern "C" fn vm_hooks__read_return_data(dest: GuestPtr, offset: u32, size: u32) -> u32 {
hostio!(read_return_data(dest, offset, size))
}

Expand Down
2 changes: 1 addition & 1 deletion arbitrator/wasm-libraries/user-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#![allow(clippy::missing_safety_doc)]

use arbutil::{Bytes32, evm::EvmData};
use arbutil::{evm::EvmData, Bytes32};
use fnv::FnvHashMap as HashMap;
use lazy_static::lazy_static;
use parking_lot::Mutex;
Expand Down
2 changes: 1 addition & 1 deletion arbitrator/wasm-libraries/user-test/src/program.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2022-2024, Offchain Labs, Inc.
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE

use crate::{ARGS, EVER_PAGES, KEYS, LOGS, OPEN_PAGES, OUTS, EVM_DATA};
use crate::{ARGS, EVER_PAGES, EVM_DATA, KEYS, LOGS, OPEN_PAGES, OUTS};
use arbutil::{
evm::{
api::{EvmApi, VecReader},
Expand Down

0 comments on commit 72c2ab1

Please sign in to comment.