Skip to content

Commit

Permalink
Merge pull request #306 from athenavm/cleanup-unused-Runtime-fields
Browse files Browse the repository at this point in the history
cleanup unused fields in Runtime
  • Loading branch information
poszu authored Jan 9, 2025
2 parents 911b18b + 1767f4d commit f9d92c9
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 61 deletions.
9 changes: 2 additions & 7 deletions core/src/runtime/gdbstub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,8 @@ pub fn gdb_event_loop_thread<'h>(
}
runtime.postprocess();

// Calculate remaining gas. If we spent too much gas, an error would already have been thrown and
// we would never reach this code, hence the assertion.
Ok(
runtime
.gas_left()
.map(|gas_left| u32::try_from(gas_left).expect("Gas conversion error")),
)
// Calculate remaining gas. If we spent too much gas, it will be zero.
Ok(runtime.gas_left())
}

pub fn run_under_gdb(
Expand Down
40 changes: 7 additions & 33 deletions core/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,12 @@ pub struct Runtime<'host> {
/// A buffer for writing trace events to a file.
pub trace_buf: Option<BufWriter<File>>,

/// Whether the runtime is in constrained mode or not.
///
/// In unconstrained mode, any events, clock, register, or memory changes are reset after leaving
/// the unconstrained block. The only thing preserved is writes to the input stream.
pub unconstrained: bool,

/// Max gas for the runtime.
pub max_gas: Option<u32>,

/// The mapping between syscall codes and their implementations.
pub syscall_map: HashMap<SyscallCode, Arc<dyn Syscall>>,

/// The maximum number of cycles for a syscall.
pub max_syscall_cycles: u32,

breakpoints: BTreeSet<u32>,

hook_registry: hooks::HookRegistry,
Expand Down Expand Up @@ -149,11 +140,6 @@ impl<'host> Runtime<'host> {

// Determine the maximum number of cycles for any syscall.
let syscall_map = default_syscall_map();
let max_syscall_cycles = syscall_map
.values()
.map(|syscall| syscall.num_extra_cycles())
.max()
.unwrap_or(0);

Self {
context,
Expand All @@ -162,10 +148,8 @@ impl<'host> Runtime<'host> {
host,
io_buf: HashMap::new(),
trace_buf,
unconstrained: false,
max_gas: opts.max_gas(),
syscall_map,
max_syscall_cycles,
breakpoints: BTreeSet::new(),
hook_registry: Default::default(),
}
Expand Down Expand Up @@ -623,8 +607,8 @@ impl<'host> Runtime<'host> {

// We're allowed to spend all of our gas, but no more.
// Gas checking is "lazy" here: it happens _after_ the instruction is executed.
if let Some(gas_left) = self.gas_left() {
if !self.unconstrained && gas_left < 0 {
if let Some(max) = self.max_gas {
if self.state.gas > max {
tracing::debug!("out of gas");
return Err(ExecutionError::OutOfGas());
}
Expand All @@ -641,11 +625,8 @@ impl<'host> Runtime<'host> {
Ok(None)
}

pub(crate) fn gas_left(&self) -> Option<i64> {
// gas left can be negative, if we spent too much on the last instruction
self
.max_gas
.map(|max_gas| max_gas as i64 - self.state.gas as i64)
pub(crate) fn gas_left(&self) -> Option<u32> {
self.max_gas.map(|g| g.saturating_sub(self.state.gas))
}

pub fn initialize(&mut self) {
Expand Down Expand Up @@ -713,13 +694,8 @@ impl<'host> Runtime<'host> {

self.postprocess();

// Calculate remaining gas. If we spent too much gas, an error would already have been thrown and
// we would never reach this code, hence the assertion.
Ok(
self
.gas_left()
.map(|gas_left| u32::try_from(gas_left).expect("Gas conversion error")),
)
// Calculate remaining gas. If we spent too much gas, it will be 0.
Ok(self.gas_left())
}

fn postprocess(&mut self) {
Expand Down Expand Up @@ -772,9 +748,7 @@ impl<'host> Runtime<'host> {
fn trace_execution(&mut self, instruction: Instruction) {
// Write the current program counter to the trace buffer for the cycle tracer.
if let Some(ref mut buf) = self.trace_buf {
if !self.unconstrained {
buf.write_all(&u32::to_be_bytes(self.state.pc)).unwrap();
}
buf.write_all(&u32::to_be_bytes(self.state.pc)).unwrap();
}

tracing::trace!(
Expand Down
15 changes: 0 additions & 15 deletions core/src/syscall/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ pub(crate) struct SyscallHintRead;

impl Syscall for SyscallHintRead {
fn execute(&self, ctx: &mut SyscallContext, ptr: u32, len: u32) -> SyscallResult {
if ctx.rt.unconstrained {
tracing::error!("hint read should not be used in a unconstrained block");
return Err(StatusCode::StaticModeViolation);
}
let data = ctx.rt.state.input_stream[ctx.rt.state.input_stream_ptr..].to_vec();
if len as usize > data.len() {
tracing::debug!(
Expand Down Expand Up @@ -130,17 +126,6 @@ mod tests {
);
}

#[test]
fn hint_read_cant_run_in_unconstrained() {
let mut rt = Runtime::new(Program::default(), None, AthenaCoreOpts::default(), None);
rt.unconstrained = true;
let mut ctx = SyscallContext::new(&mut rt);
let syscall = super::SyscallHintRead {};

let result = syscall.execute(&mut ctx, 0, 0);
assert_eq!(Err(StatusCode::StaticModeViolation), result);
}

#[test]
fn hint_read() {
let mut rt = Runtime::new(Program::default(), None, AthenaCoreOpts::default(), None);
Expand Down
7 changes: 1 addition & 6 deletions core/src/syscall/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ impl Syscall for SyscallHostCall {
.expect("Missing Athena runtime context");
// get remaining gas
// note: this does not factor in the cost of the current instruction
let gas_left: u32 = ctx
.rt
.gas_left()
.expect("Missing gas information")
.try_into()
.expect("gas arithmetic error");
let gas_left = ctx.rt.gas_left().unwrap_or(u32::MAX);

// note: the host is responsible for checking stack depth, not us

Expand Down

0 comments on commit f9d92c9

Please sign in to comment.