Skip to content

Commit

Permalink
compile
Browse files Browse the repository at this point in the history
  • Loading branch information
junyu0312 committed Aug 8, 2024
1 parent decf272 commit 4c835de
Show file tree
Hide file tree
Showing 10 changed files with 575 additions and 308 deletions.
47 changes: 24 additions & 23 deletions crates/host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use delphinus_zkwasm::runtime::host::default_env::ExecutionArg;

use delphinus_zkwasm::runtime::host::host_env::HostEnv;
use delphinus_zkwasm::runtime::host::HostEnvBuilder;
use delphinus_zkwasm::runtime::monitor::plugins::table::FlushHint;
use delphinus_zkwasm::runtime::monitor::plugins::table::Command;
use delphinus_zkwasm::runtime::monitor::plugins::table::Event;
use delphinus_zkwasm::runtime::monitor::plugins::table::FlushStrategy;
use serde::Deserialize;
use serde::Serialize;
Expand Down Expand Up @@ -69,42 +70,42 @@ impl Default for StandardHostEnvBuilder {

struct StandardHostEnvFlushStrategy {
ops: HashMap<usize, usize>,
// avoid to scan ops for every event
hint: FlushHint,
}

impl Default for StandardHostEnvFlushStrategy {
fn default() -> Self {
Self {
ops: HashMap::new(),
hint: FlushHint::No,
}
}
}

impl FlushStrategy for StandardHostEnvFlushStrategy {
#[allow(unreachable_code)]
fn notify(&mut self, op: usize) {
let count = self.ops.entry(op).or_insert(0);
*count += 1;

if todo!("host table is full to accommodate more ops") {
self.hint = FlushHint::Demand;
} else if todo!("an ops block is met") {
self.hint = FlushHint::Suggest;
} else {
self.hint = FlushHint::No;
fn notify(&mut self, op: Event) -> Command {
match op {
Event::HostCall(op) => {
let count = self.ops.entry(op).or_insert(0);
*count += 1;
let _plugin_id = todo!("op to plugin id");

if todo!("host table is full to accommodate more ops") {
Command::Abort
} else if todo!("an ops block is met") {
Command::Start(_plugin_id)
} else if todo!("finish a block") {
Command::Commit(_plugin_id)
} else {
Command::Noop
}
}
Event::Reset => {
self.ops.clear();
todo!("return Err if including uncommitted entries, otherwise reset self");
Command::Noop
}
}
}

fn reset(&mut self) {
self.ops.clear();
self.hint = FlushHint::No;
}

fn hint(&self) -> FlushHint {
self.hint
}
}

impl HostEnvBuilder for StandardHostEnvBuilder {
Expand Down
11 changes: 4 additions & 7 deletions crates/zkwasm/src/runtime/host/default_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use crate::foreign::context::runtime::register_context_foreign;
use crate::foreign::log_helper::register_log_foreign;
use crate::foreign::require_helper::register_require_foreign;
use crate::foreign::wasm_input_helper::runtime::register_wasm_input_foreign;
use crate::runtime::monitor::plugins::table::FlushHint;
use crate::runtime::monitor::plugins::table::Command;
use crate::runtime::monitor::plugins::table::Event;
use crate::runtime::monitor::plugins::table::FlushStrategy;

use super::host_env::HostEnv;
Expand All @@ -32,12 +33,8 @@ pub struct DefaultHostEnvBuilder;
struct DefaultFlushStrategy;

impl FlushStrategy for DefaultFlushStrategy {
fn notify(&mut self, _op: usize) {}

fn reset(&mut self) {}

fn hint(&self) -> FlushHint {
FlushHint::No
fn notify(&mut self, _event: Event) -> Command {
Command::Noop
}
}

Expand Down
89 changes: 0 additions & 89 deletions crates/zkwasm/src/runtime/monitor/plugins/table/etable.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::rc::Rc;
use std::sync::Arc;

use specs::etable::EventTableEntry;
use specs::jtable::CalledFrameTable;
use specs::jtable::CalledFrameTableEntry;
use specs::jtable::FrameTableEntryInternal;
use specs::jtable::InheritedFrameTable;
use specs::jtable::InheritedFrameTableEntry;
use specs::TableBackend;
use specs::TraceBackend;
use specs::step::StepInfo;

#[derive(Clone)]
struct FrameTableEntry {
Expand Down Expand Up @@ -50,59 +49,49 @@ impl From<&FrameTableEntry> for InheritedFrameTableEntry {
}
}

pub(super) struct FrameTable {
pub(super) struct FrameTableBuilder {
initial_frame_entries: Vec<FrameTableEntry>,
slices: Vec<TableBackend<specs::jtable::FrameTable>>,

current_unreturned: Vec<FrameTableEntry>,
current_returned: Vec<FrameTableEntry>,

backend: Rc<TraceBackend>,
last_jump_eid: Vec<u32>,
}

impl FrameTable {
pub(super) fn new(backend: Rc<TraceBackend>) -> Self {
impl FrameTableBuilder {
pub(super) fn new() -> Self {
Self {
initial_frame_entries: Vec::new(),
slices: Vec::new(),

current_unreturned: Vec::new(),
current_returned: Vec::new(),

backend,
last_jump_eid: Vec::new(),
}
}

pub(super) fn push(
&mut self,
frame_id: u32,
next_frame_id: u32,
callee_fid: u32,
fid: u32,
iid: u32,
) {
pub(super) fn invoke_exported_function_pre_hook(&mut self) {
self.last_jump_eid.push(0);
}

pub(super) fn push(&mut self, frame_id: u32, callee_fid: u32, fid: u32, iid: u32) {
self.current_unreturned.push(FrameTableEntry {
frame_id,
next_frame_id,
next_frame_id: *self.last_jump_eid.last().unwrap(),
callee_fid,
fid,
iid,
inherited: false,
returned: false,
});

self.last_jump_eid.push(frame_id);
}

pub(super) fn push_static_entry(
&mut self,
frame_id: u32,
next_frame_id: u32,
callee_fid: u32,
fid: u32,
iid: u32,
) {
pub(super) fn push_static_entry(&mut self, callee_fid: u32, fid: u32, iid: u32) {
let entry = FrameTableEntry {
frame_id,
next_frame_id,
frame_id: 0,
next_frame_id: 0,
callee_fid,
fid,
iid,
Expand All @@ -115,62 +104,50 @@ impl FrameTable {
}

// Prepare for the next slice. This will remove all the entries that are returned
pub(super) fn flush(&mut self) {
pub(super) fn flush(&mut self) -> specs::jtable::FrameTable {
let frame_table = {
let frame_table = {
let inherited = self
.current_returned
.iter()
.chain(self.current_unreturned.iter())
.filter(|entry| entry.inherited)
.map(Into::into)
.collect::<Vec<InheritedFrameTableEntry>>();

let called = self
.current_returned
.iter()
.chain(self.current_unreturned.iter())
.filter(|entry| !entry.inherited)
.map(Into::into)
.collect::<Vec<CalledFrameTableEntry>>();

specs::jtable::FrameTable {
inherited: Arc::new(inherited.into()),
called: CalledFrameTable::new(called),
}
};

match self.backend.as_ref() {
TraceBackend::Memory => TableBackend::Memory(frame_table),
TraceBackend::File {
frame_table_writer, ..
} => TableBackend::Json(frame_table_writer(self.slices.len(), &frame_table)),
let inherited = self
.current_returned
.iter()
.chain(self.current_unreturned.iter())
.filter(|entry| entry.inherited)
.map(Into::into)
.collect::<Vec<InheritedFrameTableEntry>>();

let called = self
.current_returned
.iter()
.chain(self.current_unreturned.iter())
.filter(|entry| !entry.inherited)
.map(Into::into)
.collect::<Vec<CalledFrameTableEntry>>();

specs::jtable::FrameTable {
inherited: Arc::new(inherited.into()),
called: CalledFrameTable::new(called),
}
};

self.slices.push(frame_table);
// match self.backend.as_ref() {
// TraceBackend::Memory => TableBackend::Memory(frame_table),
// TraceBackend::File {
// frame_table_writer, ..
// } => TableBackend::Json(frame_table_writer(self.slices.len(), &frame_table)),
// }
};

self.current_returned.clear();
for entry in self.current_unreturned.iter_mut() {
entry.inherited = true;
}

frame_table
}

pub(super) fn pop(&mut self) {
let mut entry = self.current_unreturned.pop().unwrap();
entry.returned = true;
self.current_returned.push(entry);
}

pub(super) fn finalized(mut self) -> Vec<TableBackend<specs::jtable::FrameTable>> {
self.flush();

assert!(
self.current_unreturned.is_empty(),
"all frames should be returned"
);

self.slices
self.last_jump_eid.pop();
}

pub(super) fn build_initial_frame_table(&self) -> InheritedFrameTable {
Expand All @@ -190,4 +167,21 @@ impl FrameTable {
.try_into()
.unwrap()
}

pub(super) fn build(&mut self, entries: &[EventTableEntry]) -> specs::jtable::FrameTable {
for entry in entries {
match entry.step_info {
StepInfo::Call { index } => self.push(entry.eid, index, entry.fid, entry.iid + 1),
StepInfo::CallIndirect { func_index, .. } => {
self.push(entry.eid, func_index, entry.fid, entry.iid + 1)
}
StepInfo::Return { .. } => {
self.pop();
}
_ => (),
}
}

self.flush()
}
}
Loading

0 comments on commit 4c835de

Please sign in to comment.