Skip to content

Commit

Permalink
Merge pull request #151 from OffchainLabs/stylus-hostio-tracing
Browse files Browse the repository at this point in the history
Hostio tracer
  • Loading branch information
rachel-bousfield authored Sep 14, 2023
2 parents a9f2096 + d2bf2cd commit fc0a69d
Show file tree
Hide file tree
Showing 32 changed files with 383 additions and 179 deletions.
34 changes: 2 additions & 32 deletions arbitrator/Cargo.lock

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

9 changes: 6 additions & 3 deletions arbitrator/arbutil/src/evm/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub trait EvmApi: Send + 'static {
fn contract_call(
&mut self,
contract: Bytes20,
calldata: Vec<u8>,
calldata: &[u8],
gas: u64,
value: Bytes32,
) -> (u32, u64, UserOutcomeKind);
Expand All @@ -73,7 +73,7 @@ pub trait EvmApi: Send + 'static {
fn delegate_call(
&mut self,
contract: Bytes20,
calldata: Vec<u8>,
calldata: &[u8],
gas: u64,
) -> (u32, u64, UserOutcomeKind);

Expand All @@ -83,7 +83,7 @@ pub trait EvmApi: Send + 'static {
fn static_call(
&mut self,
contract: Bytes20,
calldata: Vec<u8>,
calldata: &[u8],
gas: u64,
) -> (u32, u64, UserOutcomeKind);

Expand Down Expand Up @@ -133,4 +133,7 @@ pub trait EvmApi: Send + 'static {
/// Note: has the side effect of updating Geth's memory usage tracker.
/// Not analogous to any EVM opcode.
fn add_pages(&mut self, pages: u16) -> u64;

/// Captures tracing information for hostio invocations during native execution.
fn capture_hostio(&self, name: &str, args: &[u8], outs: &[u8], ink: u64);
}
20 changes: 17 additions & 3 deletions arbitrator/arbutil/src/evm/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ impl From<Bytes> for ApiValue {
}
}

impl From<&[u8]> for ApiValue {
fn from(value: &[u8]) -> Self {
ApiValueKind::Bytes(value.to_vec()).into()
}
}

impl From<Bytes20> for ApiValue {
fn from(value: Bytes20) -> Self {
ApiValueKind::Bytes20(value).into()
Expand Down Expand Up @@ -220,7 +226,7 @@ impl<T: JsCallIntoGo> EvmApi for JsEvmApi<T> {
fn contract_call(
&mut self,
contract: Bytes20,
input: Bytes,
input: &[u8],
gas: u64,
value: Bytes32,
) -> (u32, u64, UserOutcomeKind) {
Expand All @@ -231,7 +237,7 @@ impl<T: JsCallIntoGo> EvmApi for JsEvmApi<T> {
fn delegate_call(
&mut self,
contract: Bytes20,
input: Bytes,
input: &[u8],
gas: u64,
) -> (u32, u64, UserOutcomeKind) {
let [len, cost, status] = call!(self, 3, DelegateCall, contract, input, gas);
Expand All @@ -241,7 +247,7 @@ impl<T: JsCallIntoGo> EvmApi for JsEvmApi<T> {
fn static_call(
&mut self,
contract: Bytes20,
input: Bytes,
input: &[u8],
gas: u64,
) -> (u32, u64, UserOutcomeKind) {
let [len, cost, status] = call!(self, 3, StaticCall, contract, input, gas);
Expand Down Expand Up @@ -307,4 +313,12 @@ impl<T: JsCallIntoGo> EvmApi for JsEvmApi<T> {
let [cost] = call!(self, 1, AddPages, pages);
cost.assert_u64()
}

fn capture_hostio(&self, name: &str, args: &[u8], outs: &[u8], ink: u64) {
let args = hex::encode(args);
let outs = hex::encode(outs);
println!(
"Error: unexpected hostio tracing info for {name} while proving: {args}, {outs}, {ink}"
);
}
}
1 change: 1 addition & 0 deletions arbitrator/arbutil/src/evm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub struct EvmData {
pub tx_origin: Bytes20,
pub reentrant: u32,
pub return_data_len: u32,
pub tracing: bool,
}

/// Returns the minimum number of EVM words needed to store `bytes` bytes.
Expand Down
4 changes: 2 additions & 2 deletions arbitrator/arbutil/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ pub fn time(span: Duration) -> String {
let units = vec![
"ns", "μs", "ms", "s", "min", "h", "d", "w", "mo", "yr", "dec", "cent", "mill", "eon",
];
let scale = vec![
let scale = [
1000., 1000., 1000., 60., 60., 24., 7., 4.34, 12., 10., 10., 10., 1_000_000.,
];
let colors = vec![MINT, MINT, YELLOW, RED, RED, RED];
let colors = [MINT, MINT, YELLOW, RED, RED, RED];
while span >= scale[unit] && unit < scale.len() {
span /= scale[unit];
unit += 1;
Expand Down
1 change: 1 addition & 0 deletions arbitrator/jit/src/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ pub fn evm_data_impl(env: WasmEnvMut, sp: u32) {
tx_origin: sp.read_bytes20().into(),
reentrant: sp.read_u32(),
return_data_len: 0,
tracing: false,
};
sp.skip_space();
sp.write_ptr(heapify(evm_data));
Expand Down
1 change: 0 additions & 1 deletion arbitrator/stylus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ wasmer-compiler-singlepass = { path = "../tools/wasmer/lib/compiler-singlepass",
wasmer-compiler-cranelift = { path = "../tools/wasmer/lib/compiler-cranelift" }
wasmer-compiler-llvm = { path = "../tools/wasmer/lib/compiler-llvm", optional = true }
derivative = "2.2.0"
ouroboros = "0.15.5"
parking_lot = "0.12.1"
thiserror = "1.0.33"
libc = "0.2.108"
Expand Down
4 changes: 4 additions & 0 deletions arbitrator/stylus/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ impl<'a, E: EvmApi> HostioInfo<'a, E> {
self.write_slice(ptr, &src.0)?;
Ok(())
}

pub fn trace(&self, name: &str, args: &[u8], outs: &[u8], ink: u64) {
self.evm_api.capture_hostio(name, args, outs, ink);
}
}

impl<'a, E: EvmApi> MeteredMachine for HostioInfo<'a, E> {
Expand Down
38 changes: 28 additions & 10 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::RustVec;
use crate::{RustSlice, RustVec};
use arbutil::{
evm::{
api::{EvmApi, EvmApiStatus},
Expand All @@ -24,22 +24,22 @@ pub struct GoEvmApi {
pub contract_call: unsafe extern "C" fn(
id: usize,
contract: Bytes20,
calldata: *mut RustVec,
calldata: *mut RustSlice,
gas: *mut u64,
value: Bytes32,
return_data_len: *mut u32,
) -> EvmApiStatus,
pub delegate_call: unsafe extern "C" fn(
id: usize,
contract: Bytes20,
calldata: *mut RustVec,
calldata: *mut RustSlice,
gas: *mut u64,
return_data_len: *mut u32,
) -> EvmApiStatus,
pub static_call: unsafe extern "C" fn(
id: usize,
contract: Bytes20,
calldata: *mut RustVec,
calldata: *mut RustSlice,
gas: *mut u64,
return_data_len: *mut u32,
) -> EvmApiStatus,
Expand All @@ -66,6 +66,13 @@ pub struct GoEvmApi {
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,
args: *mut RustSlice,
outs: *mut RustSlice,
ink: u64,
),
pub id: usize,
}

Expand Down Expand Up @@ -111,7 +118,7 @@ impl EvmApi for GoEvmApi {
fn contract_call(
&mut self,
contract: Bytes20,
calldata: Vec<u8>,
calldata: &[u8],
gas: u64,
value: Bytes32,
) -> (u32, u64, UserOutcomeKind) {
Expand All @@ -121,7 +128,7 @@ impl EvmApi for GoEvmApi {
self,
contract_call,
contract,
ptr!(RustVec::new(calldata)),
ptr!(RustSlice::new(calldata)),
ptr!(call_gas),
value,
ptr!(return_data_len)
Expand All @@ -132,7 +139,7 @@ impl EvmApi for GoEvmApi {
fn delegate_call(
&mut self,
contract: Bytes20,
calldata: Vec<u8>,
calldata: &[u8],
gas: u64,
) -> (u32, u64, UserOutcomeKind) {
let mut call_gas = gas; // becomes the call's cost
Expand All @@ -141,7 +148,7 @@ impl EvmApi for GoEvmApi {
self,
delegate_call,
contract,
ptr!(RustVec::new(calldata)),
ptr!(RustSlice::new(calldata)),
ptr!(call_gas),
ptr!(return_data_len)
);
Expand All @@ -151,7 +158,7 @@ impl EvmApi for GoEvmApi {
fn static_call(
&mut self,
contract: Bytes20,
calldata: Vec<u8>,
calldata: &[u8],
gas: u64,
) -> (u32, u64, UserOutcomeKind) {
let mut call_gas = gas; // becomes the call's cost
Expand All @@ -160,7 +167,7 @@ impl EvmApi for GoEvmApi {
self,
static_call,
contract,
ptr!(RustVec::new(calldata)),
ptr!(RustSlice::new(calldata)),
ptr!(call_gas),
ptr!(return_data_len)
);
Expand Down Expand Up @@ -250,4 +257,15 @@ impl EvmApi for GoEvmApi {
fn add_pages(&mut self, pages: u16) -> u64 {
call!(self, add_pages, pages)
}

fn capture_hostio(&self, name: &str, args: &[u8], outs: &[u8], ink: u64) {
call!(
self,
capture_hostio,
ptr!(RustVec::new(name.as_bytes().to_vec())),
ptr!(RustSlice::new(args)),
ptr!(RustSlice::new(outs)),
ink
)
}
}
Loading

0 comments on commit fc0a69d

Please sign in to comment.