Skip to content

Commit

Permalink
fix(derive): hoist params
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Apr 4, 2024
2 parents e020a58 + 50a4410 commit 1d75e28
Show file tree
Hide file tree
Showing 94 changed files with 3,661 additions and 1,045 deletions.
83 changes: 83 additions & 0 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions crates/common/src/asterisc/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ pub struct AsteriscIO;
/// See https://jborza.com/post/2021-05-11-riscv-linux-syscalls/
///
/// **Note**: This is not an exhaustive list of system calls available to the `client` program,
/// only the ones necessary for the [BasicKernelInterface] trait implementation. If an extension trait for
/// the [BasicKernelInterface] trait is created for the `asterisc` kernel, this list should be extended
/// accordingly.
/// only the ones necessary for the [BasicKernelInterface] trait implementation. If an extension
/// trait for the [BasicKernelInterface] trait is created for the `asterisc` kernel, this list
/// should be extended accordingly.
#[repr(u32)]
pub(crate) enum SyscallNumber {
/// Sets the Exited and ExitCode states to true and $a0 respectively.
Expand Down
4 changes: 2 additions & 2 deletions crates/common/src/asterisc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! This module contains raw syscall bindings for the `riscv64gc` target architecture, as well as a high-level
//! implementation of the [crate::BasicKernelInterface] trait for the `asterisc` kernel.
//! This module contains raw syscall bindings for the `riscv64gc` target architecture, as well as a
//! high-level implementation of the [crate::BasicKernelInterface] trait for the `asterisc` kernel.
pub(crate) mod io;
mod syscall;
10 changes: 5 additions & 5 deletions crates/common/src/cannon/io.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{cannon::syscall, BasicKernelInterface, FileDescriptor, RegisterSize};
use anyhow::{anyhow, Result};

/// Concrete implementation of the [BasicKernelInterface] trait for the `MIPS32rel1` target architecture. Exposes a safe
/// interface for performing IO operations within the FPVM kernel.
/// Concrete implementation of the [BasicKernelInterface] trait for the `MIPS32rel1` target
/// architecture. Exposes a safe interface for performing IO operations within the FPVM kernel.
#[derive(Debug)]
pub struct CannonIO;

Expand All @@ -11,9 +11,9 @@ pub struct CannonIO;
/// See [Cannon System Call Specification](https://specs.optimism.io/experimental/fault-proof/cannon-fault-proof-vm.html#syscalls)
///
/// **Note**: This is not an exhaustive list of system calls available to the `client` program,
/// only the ones necessary for the [BasicKernelInterface] trait implementation. If an extension trait for
/// the [BasicKernelInterface] trait is created for the `Cannon` kernel, this list should be extended
/// accordingly.
/// only the ones necessary for the [BasicKernelInterface] trait implementation. If an extension
/// trait for the [BasicKernelInterface] trait is created for the `Cannon` kernel, this list should
/// be extended accordingly.
#[repr(u32)]
pub(crate) enum SyscallNumber {
/// Sets the Exited and ExitCode states to true and $a0 respectively.
Expand Down
4 changes: 2 additions & 2 deletions crates/common/src/cannon/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! This module contains raw syscall bindings for the `MIPS32r2` target architecture, as well as a high-level
//! implementation of the [crate::BasicKernelInterface] trait for the `Cannon` kernel.
//! This module contains raw syscall bindings for the `MIPS32r2` target architecture, as well as a
//! high-level implementation of the [crate::BasicKernelInterface] trait for the `Cannon` kernel.
pub(crate) mod io;
mod syscall;
22 changes: 8 additions & 14 deletions crates/common/src/cannon/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ pub(crate) unsafe fn syscall1(n: RegisterSize, arg1: RegisterSize) -> RegisterSi
options(nostack, preserves_flags)
);

(err == 0)
.then_some(ret)
.unwrap_or_else(|| ret.wrapping_neg())
(err == 0).then_some(ret).unwrap_or_else(|| ret.wrapping_neg())
}

/// Issues a raw system call with 3 arguments. (e.g. read, write)
Expand Down Expand Up @@ -97,16 +95,12 @@ pub(crate) unsafe fn syscall3(
options(nostack, preserves_flags)
);

let value = (err == 0)
.then_some(ret)
.unwrap_or_else(|| ret.wrapping_neg());
let value = (err == 0).then_some(ret).unwrap_or_else(|| ret.wrapping_neg());

(value <= -4096isize as RegisterSize)
.then_some(value)
.ok_or_else(|| {
// Truncation of the error value is guaranteed to never occur due to
// the above check. This is the same check that musl uses:
// https://git.musl-libc.org/cgit/musl/tree/src/internal/syscall_ret.c?h=v1.1.15
-(value as i32)
})
(value <= -4096isize as RegisterSize).then_some(value).ok_or_else(|| {
// Truncation of the error value is guaranteed to never occur due to
// the above check. This is the same check that musl uses:
// https://git.musl-libc.org/cgit/musl/tree/src/internal/syscall_ret.c?h=v1.1.15
-(value as i32)
})
}
8 changes: 4 additions & 4 deletions crates/common/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! This module contains utilities for handling async functions in the no_std environment. This allows for usage of
//! async/await syntax for futures in a single thread.
//! This module contains utilities for handling async functions in the no_std environment. This
//! allows for usage of async/await syntax for futures in a single thread.
use alloc::boxed::Box;
use core::{
future::Future,
task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
};

/// This function busy waits on a future until it is ready. It uses a no-op waker to poll the future in a
/// thread-blocking loop.
/// This function busy waits on a future until it is ready. It uses a no-op waker to poll the future
/// in a thread-blocking loop.
pub fn block_on<T>(f: impl Future<Output = T>) -> T {
let mut f = Box::pin(f);

Expand Down
11 changes: 4 additions & 7 deletions crates/common/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,19 @@ mod native_io {
// forget the file descriptor so that the `Drop` impl doesn't close it.
std::mem::forget(file);

n.try_into()
.map_err(|_| anyhow!("Failed to convert usize to RegisterSize"))
n.try_into().map_err(|_| anyhow!("Failed to convert usize to RegisterSize"))
}

fn read(fd: FileDescriptor, buf: &mut [u8]) -> Result<RegisterSize> {
let raw_fd: RegisterSize = fd.into();
let mut file = unsafe { File::from_raw_fd(raw_fd as i32) };
let n = file
.read(buf)
.map_err(|e| anyhow!("Error reading from file descriptor: {e}"))?;
let n =
file.read(buf).map_err(|e| anyhow!("Error reading from file descriptor: {e}"))?;

// forget the file descriptor so that the `Drop` impl doesn't close it.
std::mem::forget(file);

n.try_into()
.map_err(|_| anyhow!("Failed to convert usize to RegisterSize"))
n.try_into().map_err(|_| anyhow!("Failed to convert usize to RegisterSize"))
}

fn exit(code: RegisterSize) -> ! {
Expand Down
7 changes: 1 addition & 6 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#![doc = include_str!("../README.md")]
#![warn(
missing_debug_implementations,
missing_docs,
unreachable_pub,
rustdoc::all
)]
#![warn(missing_debug_implementations, missing_docs, unreachable_pub, rustdoc::all)]
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![cfg_attr(target_arch = "mips", feature(asm_experimental_arch))]
Expand Down
4 changes: 2 additions & 2 deletions crates/common/src/malloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ pub mod global_allocator {
/// This function is unsafe because the caller must ensure:
/// * The allocator has not already been initialized.
/// * The provided memory region must be valid, non-null, and not used by anything else.
/// * After aligning the start and end addresses, the size of the heap must be > 0, or the function
/// will panic.
/// * After aligning the start and end addresses, the size of the heap must be > 0, or the
/// function will panic.
pub unsafe fn init_allocator(heap_start_addr: *mut u8, heap_size: usize) {
ALLOCATOR.lock().init(heap_start_addr, heap_size)
}
Expand Down
15 changes: 8 additions & 7 deletions crates/common/src/traits/basic.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
//! Defines the [BasicKernelInterface] trait, which describes the functionality of several system calls inside of
//! the FPVM kernel.
//! Defines the [BasicKernelInterface] trait, which describes the functionality of several system
//! calls inside of the FPVM kernel.
use crate::{FileDescriptor, RegisterSize};
use anyhow::Result;

/// The [BasicKernelInterface] trait describes the functionality of several core system calls inside of
/// the FPVM kernel. Commonly, FPVMs delegate IO operations to custom file descriptors in the `client` program. It is
/// a safe wrapper around the raw system calls available to the `client` program.
/// The [BasicKernelInterface] trait describes the functionality of several core system calls inside
/// of the FPVM kernel. Commonly, FPVMs delegate IO operations to custom file descriptors in the
/// `client` program. It is a safe wrapper around the raw system calls available to the `client`
/// program.
///
/// In cases where the set of system calls defined in this trait need to be extended, an additional trait should be
/// created that extends this trait.
/// In cases where the set of system calls defined in this trait need to be extended, an additional
/// trait should be created that extends this trait.
pub trait BasicKernelInterface {
/// Write the given buffer to the given file descriptor.
fn write(fd: FileDescriptor, buf: &[u8]) -> Result<RegisterSize>;
Expand Down
1 change: 1 addition & 0 deletions crates/derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ serde = { version = "1.0.197", default-features = false, features = ["derive"],

[dev-dependencies]
tokio = { version = "1.36", features = ["full"] }
proptest = "1.4.0"

[features]
serde = ["dep:serde", "alloy-primitives/serde"]
7 changes: 1 addition & 6 deletions crates/derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#![doc = include_str!("../README.md")]
#![warn(
missing_debug_implementations,
missing_docs,
unreachable_pub,
rustdoc::all
)]
#![warn(missing_debug_implementations, missing_docs, unreachable_pub, rustdoc::all)]
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![no_std]
Expand Down
4 changes: 2 additions & 2 deletions crates/derive/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ pub const CONFIG_UPDATE_TOPIC: B256 =
pub const CONFIG_UPDATE_EVENT_VERSION_0: B256 = B256::ZERO;

/// Frames cannot be larger than 1MB.
/// Data transactions that carry frames are generally not larger than 128 KB due to L1 network conditions,
/// but we leave space to grow larger anyway (gas limit allows for more data).
/// Data transactions that carry frames are generally not larger than 128 KB due to L1 network
/// conditions, but we leave space to grow larger anyway (gas limit allows for more data).
pub const MAX_FRAME_LEN: usize = 1000;
Loading

0 comments on commit 1d75e28

Please sign in to comment.