Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into removing-MockHost-fro…
Browse files Browse the repository at this point in the history
…m-athena-interface
  • Loading branch information
poszu committed Jan 3, 2025
2 parents b0083d3 + 8a10e23 commit 6a21102
Show file tree
Hide file tree
Showing 30 changed files with 477 additions and 507 deletions.
70 changes: 46 additions & 24 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ repository = { workspace = true }

[dependencies]
cargo_metadata = "0.19.1"
anyhow = { version = "1.0.94" }
anyhow = { version = "1.0.95" }
clap = { version = "4.5.21", features = ["derive", "env"] }
10 changes: 5 additions & 5 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ license.workspace = true
edition.workspace = true

[build-dependencies]
anyhow = { version = "1.0.94", features = ["backtrace"] }
anyhow = { version = "1.0.95", features = ["backtrace"] }
vergen-git2 = { version = "1.0.2", default-features = false, features = [
"build",
] }

[dependencies]
anyhow = { version = "1.0.94", features = ["backtrace"] }
anyhow = { version = "1.0.95", features = ["backtrace"] }
athena-sdk = { path = "../sdk" }
athena-builder = { path = "../builder" }
cargo_metadata = "0.19.1"
clap = { version = "4.5.21", features = ["derive", "env"] }
reqwest = { version = "0.12.9", features = [
reqwest = { version = "0.12.11", features = [
"blocking",
"json",
"rustls-tls",
], default-features = false }
indicatif = "0.17.9"
dirs = "5.0"
rand = "0.8"
serde_json = "1.0.133"
serde_json = "1.0.134"
yansi = "1.0.1"
hex = "0.4.3"
anstyle = "1.0.10"
target-lexicon = "0.13.0"
target-lexicon = "0.13.1"
tracing-subscriber = "0.3.19"
tracing = "0.1.41"
5 changes: 3 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ hex = "0.4.3"
tracing = "0.1.41"
strum_macros = "0.26"
strum = "0.26"
thiserror = "2.0.8"
anyhow = "1.0.94"
thiserror = "2.0.9"
anyhow = "1.0.95"
mockall = "0.13.1"
gdbstub = "0.7.3"
bytemuck = "1.21.0"

[dev-dependencies]
athena-core = { path = ".", features = ["unittest"] }
Expand Down
52 changes: 11 additions & 41 deletions core/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ use crate::utils::Buffer;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

/// Standard input.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone)]
pub struct AthenaStdin {
/// Input stored as a vec of vec of bytes. It's stored this way because the read syscall reads
/// a vec of bytes at a time.
pub buffer: Vec<Vec<u8>>,
pub ptr: usize,
buffer: Vec<u8>,
}

/// Public values for the runner.
Expand All @@ -17,49 +14,26 @@ pub struct AthenaPublicValues {
}

impl AthenaStdin {
/// Create a new `AthenaStdin`.
pub const fn new() -> Self {
Self {
buffer: Vec::new(),
ptr: 0,
}
}

/// Create a `AthenaStdin` from a slice of bytes.
pub fn from(data: &[u8]) -> Self {
Self {
buffer: vec![data.to_vec()],
ptr: 0,
}
}

/// Read a value from the buffer.
pub fn read<T: DeserializeOwned>(&mut self) -> T {
let result: T = bincode::deserialize(&self.buffer[self.ptr]).expect("failed to deserialize");
self.ptr += 1;
result
}

/// Read a slice of bytes from the buffer.
pub fn read_slice(&mut self, slice: &mut [u8]) {
slice.copy_from_slice(&self.buffer[self.ptr]);
self.ptr += 1;
Self { buffer: Vec::new() }
}

/// Write a value to the buffer.
pub fn write<T: Serialize>(&mut self, data: &T) {
let mut tmp = Vec::new();
bincode::serialize_into(&mut tmp, data).expect("serialization failed");
self.buffer.push(tmp);
bincode::serialize_into(&mut self.buffer, data).expect("serialization failed");
}

/// Write a slice of bytes to the buffer.
pub fn write_slice(&mut self, slice: &[u8]) {
self.buffer.push(slice.to_vec());
self.buffer.extend_from_slice(slice);
}

pub fn write_vec(&mut self, vec: Vec<u8>) {
self.buffer.push(vec);
pub fn write_vec(&mut self, mut vec: Vec<u8>) {
self.buffer.append(&mut vec);
}

pub fn to_vec(self) -> Vec<u8> {
self.buffer
}
}

Expand All @@ -71,10 +45,6 @@ impl AthenaPublicValues {
}
}

pub fn raw(&self) -> String {
format!("0x{}", hex::encode(self.buffer.data.clone()))
}

/// Create a `AthenaPublicValues` from a slice of bytes.
pub fn from(data: &[u8]) -> Self {
Self {
Expand Down
55 changes: 55 additions & 0 deletions core/src/runtime/hooks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::collections::HashMap;

use super::Runtime;

#[mockall::automock]
#[allow(clippy::needless_lifetimes)] // the lifetimes are needed by automock
pub trait Hook {
fn execute<'r, 'h>(&self, env: HookEnv<'r, 'h>, data: &[u8]) -> anyhow::Result<Vec<u8>>;
}

/// A registry of hooks to call, indexed by the file descriptors through which they are accessed.
#[derive(Default)]
pub struct HookRegistry {
/// Table of registered hooks.
table: HashMap<u32, Box<dyn Hook>>,
}

impl HookRegistry {
/// Create a registry with the default hooks.
pub fn new() -> Self {
Default::default()
}

/// Register a hook under a given FD.
/// Will fail if a hook is already registered on a given FD
/// or a FD is <= 4.
pub fn register(&mut self, fd: u32, hook: Box<dyn Hook>) -> anyhow::Result<()> {
anyhow::ensure!(fd > 4, "FDs 0-4 are reserved for internal usage");
anyhow::ensure!(
!self.table.contains_key(&fd),
"there is already a hook for FD {fd} registered"
);
self.table.insert(fd, hook);
Ok(())
}

pub(crate) fn get(&self, fd: u32) -> Option<&dyn Hook> {
self.table.get(&fd).map(AsRef::as_ref)
}
}

/// Environment that a hook may read from.
pub struct HookEnv<'r, 'h> {
pub runtime: &'r Runtime<'h>,
}

#[cfg(test)]
pub mod tests {
use super::*;

#[test]
pub fn registry_new_is_empty() {
assert!(HookRegistry::new().table.is_empty());
}
}
Loading

0 comments on commit 6a21102

Please sign in to comment.