Skip to content

Commit

Permalink
split GoSliceData from SendGoSliceData
Browse files Browse the repository at this point in the history
  • Loading branch information
tsahee committed Feb 22, 2024
1 parent 4e935d1 commit 6ea1c61
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
14 changes: 6 additions & 8 deletions arbitrator/stylus/src/evm_api.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// Copyright 2022-2023, Offchain Labs, Inc.
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE

use crate::{GoSliceData, RustSlice};
use crate::{GoSliceData, RustSlice, SendGoSliceData};
use arbutil::evm::{
api::{
EvmApiMethod, EVM_API_METHOD_REQ_OFFSET, {DataReader, EvmApiStatus},
},
api::{EvmApiMethod, EvmApiStatus, EVM_API_METHOD_REQ_OFFSET},
req::RequestHandler,
};

Expand All @@ -17,7 +15,7 @@ pub struct NativeRequestHandler {
data: *mut RustSlice,
gas_cost: *mut u64,
result: *mut GoSliceData,
raw_data: *mut GoSliceData,
raw_data: *mut SendGoSliceData,
) -> EvmApiStatus, // value
pub id: usize,
}
Expand All @@ -28,14 +26,14 @@ macro_rules! ptr {
};
}

impl RequestHandler<GoSliceData> for NativeRequestHandler {
impl RequestHandler<SendGoSliceData> for NativeRequestHandler {
fn handle_request(
&mut self,
req_type: EvmApiMethod,
req_data: &[u8],
) -> (Vec<u8>, GoSliceData, u64) {
) -> (Vec<u8>, SendGoSliceData, u64) {
let mut result = GoSliceData::default();
let mut raw_data = GoSliceData::default();
let mut raw_data = SendGoSliceData::default();
let mut cost = 0;
let status = unsafe {
(self.handle_request_fptr)(
Expand Down
46 changes: 42 additions & 4 deletions arbitrator/stylus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use eyre::ErrReport;
use native::NativeInstance;
use prover::programs::{prelude::*, StylusData};
use run::RunProgram;
use std::{marker::PhantomData, mem};
use std::{marker::PhantomData, mem, ptr::null};

pub use prover;

Expand All @@ -31,19 +31,57 @@ mod test;
#[cfg(all(test, feature = "benchmark"))]
mod benchmarks;

#[derive(Clone, Copy, Default)]
#[derive(Clone, Copy)]
#[repr(C)]
pub struct GoSliceData {
ptr: *const u8, // stored as pointer for GO
len: usize,
}

impl Default for GoSliceData {
fn default() -> Self {
GoSliceData {
ptr: null(),
len: 0,
}
}
}

impl GoSliceData {
fn slice(&self) -> &[u8] {
if self.len == 0 {
return &[];
}
unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
}
}

// same as above, with Send semantics using dirty trickery
// GO will always use GoSliceData so these types must have
// exact same representation, see assert_go_slices_match
#[derive(Clone, Copy, Default)]
#[repr(C)]
pub struct SendGoSliceData {
ptr: usize, // not stored as pointer because rust won't let that be Send
len: usize,
}

impl DataReader for GoSliceData {
#[allow(dead_code)]
const fn assert_go_slices_match() -> () {
// TODO: this will be stabilized on rust 1.77
// assert_eq!(mem::offset_of!(GoSliceData, ptr), mem::offset_of!(SendGoSliceData, ptr));
// assert_eq!(mem::offset_of!(GoSliceData, len), mem::offset_of!(SendGoSliceData, len));
assert!(mem::size_of::<GoSliceData>() == mem::size_of::<SendGoSliceData>());
}

const _: () = assert_go_slices_match();

impl DataReader for SendGoSliceData {
fn slice(&self) -> &[u8] {
if self.len == 0 {
return &[];
}
unsafe { std::slice::from_raw_parts(self.ptr as *mut u8, self.len) }
unsafe { std::slice::from_raw_parts(self.ptr as *const u8, self.len) }
}
}

Expand Down

0 comments on commit 6ea1c61

Please sign in to comment.