Skip to content

Commit

Permalink
test: test suspend-resume
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Apr 3, 2024
1 parent e6d2ac4 commit 8a13f6c
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 147 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ concurrency:

jobs:
test:
name: test ${{ matrix.toolchain }}
name: test +${{ matrix.toolchain }} ${{ matrix.profile }}
runs-on: ubuntu-latest
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
toolchain: ["stable", "nightly"]
# toolchain: ["stable", "nightly"]
toolchain: ["nightly-2024-02-01"]
profile: ["dev", "release"]
steps:
- uses: actions/checkout@v4
- uses: KyleMayes/install-llvm-action@v1
Expand All @@ -35,7 +38,7 @@ jobs:
with:
cache-on-failure: true
- name: test
run: cargo test --all-features --workspace --verbose
run: cargo test --all-features --workspace --verbose --profile ${{ matrix.profile }}

feature-checks:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions crates/revm-jit-backend/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub trait Backend: BackendTypes + TypeMethods {

fn set_is_dumping(&mut self, yes: bool);
fn set_debug_assertions(&mut self, yes: bool);
fn opt_level(&self) -> OptimizationLevel;
fn set_opt_level(&mut self, level: OptimizationLevel);
fn dump_ir(&mut self, path: &Path) -> Result<()>;
fn dump_disasm(&mut self, path: &Path) -> Result<()>;
Expand Down
6 changes: 5 additions & 1 deletion crates/revm-jit-callbacks-bc/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ fn main() {
// NOTE: We need to use `CARGO_ENCODED_RUSTFLAGS` as `RUSTFLAGS` alone is ignored due to
// `--target`.
let rustflags = if let Ok(rustflags) = env::var("CARGO_ENCODED_RUSTFLAGS") {
rustflags + "\u{1f}"
if rustflags.is_empty() {
rustflags
} else {
rustflags + "\u{1f}"
}
} else {
String::new()
};
Expand Down
16 changes: 11 additions & 5 deletions crates/revm-jit-callbacks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod utils;
pub use utils::*;

#[no_mangle]
unsafe extern "C" fn __revm_jit_callback_mload(
pub unsafe extern "C" fn __revm_jit_callback_mload(
ecx: &mut EvmContext<'_>,
[offset_ptr]: &mut [EvmWord; 1],
) -> InstructionResult {
Expand All @@ -31,9 +31,9 @@ unsafe extern "C" fn __revm_jit_callback_mload(
}

#[no_mangle]
unsafe extern "C" fn __revm_jit_callback_mstore(
pub unsafe extern "C" fn __revm_jit_callback_mstore(
ecx: &mut EvmContext<'_>,
reverse_tokens![offset, value]: &mut [EvmWord; 2],
rev![offset, value]: &mut [EvmWord; 2],
) -> InstructionResult {
gas!(ecx, rgas::VERYLOW);
let offset = try_into_usize!(offset.as_u256());
Expand All @@ -43,13 +43,19 @@ unsafe extern "C" fn __revm_jit_callback_mstore(
}

#[no_mangle]
unsafe extern "C" fn __revm_jit_callback_mstore8(
pub unsafe extern "C" fn __revm_jit_callback_mstore8(
ecx: &mut EvmContext<'_>,
reverse_tokens![offset, value]: &mut [EvmWord; 2],
rev![offset, value]: &mut [EvmWord; 2],
) -> InstructionResult {
gas!(ecx, rgas::VERYLOW);
let offset = try_into_usize!(offset.as_u256());
resize_memory!(ecx, offset, 1);
ecx.memory.set_byte(offset, value.to_u256().byte(0));
InstructionResult::Continue
}

#[no_mangle]
#[inline]
pub unsafe extern "C" fn __revm_jit_callback_msize(ecx: &mut EvmContext<'_>) -> usize {
ecx.memory.len()
}
8 changes: 4 additions & 4 deletions crates/revm-jit-callbacks/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ macro_rules! resize_memory {
#[macro_export]
macro_rules! read_words {
($sp:expr, $($words:ident),+ $(,)?) => {
let reverse_tokens!($($words),+) = unsafe { read_words_rev($sp) };
let rev![$($words),+] = unsafe { read_words_rev($sp) };
};
}

Expand All @@ -85,17 +85,17 @@ macro_rules! try_into_usize {

// Credits: <https://github.com/AuroransSolis/rustconf-2023/blob/665a645d751dfe0e483261e3abca25ab4bb9e13a/reverse-tokens/src/main.rs>
#[macro_export]
macro_rules! reverse_tokens {
macro_rules! rev {
(@rev [$first:tt$(, $rest:tt)*] [$($rev:tt),*]) => {
reverse_tokens! {
rev! {
@rev [$($rest),*][$first $(, $rev)*]
}
};
(@rev [] [$($rev:tt),*]) => {
[$($rev)*] // NOTE: Extra `[]` to make this an array pattern.
};
($($tt:tt)+) => {
reverse_tokens! {
rev! {
@rev [$($tt),+] []
}
};
Expand Down
3 changes: 1 addition & 2 deletions crates/revm-jit-callbacks/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ pub fn resize_memory(ecx: &mut EvmContext<'_>, offset: usize, len: usize) -> Ins
#[inline]
pub unsafe fn copy_operation(
ecx: &mut EvmContext<'_>,
sp: *mut EvmWord,
rev![memory_offset, data_offset, len]: &mut [EvmWord; 3],
data: &[u8],
) -> InstructionResult {
read_words!(sp, memory_offset, data_offset, len);
let len = tri!(usize::try_from(len));
gas_opt!(ecx, rgas::verylowcopy_cost(len as u64));
if len == 0 {
Expand Down
4 changes: 4 additions & 0 deletions crates/revm-jit-cranelift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ impl Backend for JitEvmCraneliftBackend {
let _ = yes;
}

fn opt_level(&self) -> OptimizationLevel {
self.opt_level
}

fn set_opt_level(&mut self, level: OptimizationLevel) {
// Note that this will only affect new functions after a new module is created in
// `free_all_functions`.
Expand Down
13 changes: 13 additions & 0 deletions crates/revm-jit-llvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ impl<'ctx> Backend for JitEvmLlvmBackend<'ctx> {
self.debug_assertions = yes;
}

fn opt_level(&self) -> revm_jit_backend::OptimizationLevel {
convert_opt_level_rev(self.opt_level)
}

fn set_opt_level(&mut self, level: revm_jit_backend::OptimizationLevel) {
self.opt_level = convert_opt_level(level);
}
Expand Down Expand Up @@ -957,6 +961,15 @@ fn convert_opt_level(level: revm_jit_backend::OptimizationLevel) -> Optimization
}
}

fn convert_opt_level_rev(level: OptimizationLevel) -> revm_jit_backend::OptimizationLevel {
match level {
OptimizationLevel::None => revm_jit_backend::OptimizationLevel::None,
OptimizationLevel::Less => revm_jit_backend::OptimizationLevel::Less,
OptimizationLevel::Default => revm_jit_backend::OptimizationLevel::Default,
OptimizationLevel::Aggressive => revm_jit_backend::OptimizationLevel::Aggressive,
}
}

fn convert_attribute(
bcx: &JitEvmLlvmBuilder<'_, '_>,
attr: revm_jit_backend::Attribute,
Expand Down
11 changes: 11 additions & 0 deletions crates/revm-jit/src/bytecode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub use opcode::*;
mod stack;
pub use stack::StackIo;

/// Noop opcode used to test suspend-resume.
pub(crate) const TEST_SUSPEND: u8 = 0x25;

// TODO: Use `indexvec`.
/// An EVM instruction is a high level internal representation of an EVM opcode.
///
Expand Down Expand Up @@ -388,6 +391,10 @@ impl InstData {
/// Returns `true` if we know that this instruction will stop execution.
#[inline]
pub(crate) const fn is_diverging(&self, is_eof: bool) -> bool {
if cfg!(test) && self.opcode == TEST_SUSPEND {
return false;
}

// TODO: SELFDESTRUCT will not be diverging in the future.
self.flags.contains(InstFlags::INVALID_JUMP)
|| self.flags.contains(InstFlags::DISABLED)
Expand All @@ -399,6 +406,10 @@ impl InstData {
/// Returns `true` if this instruction will suspend execution.
#[inline]
const fn will_suspend(&self) -> bool {
if cfg!(test) && self.opcode == TEST_SUSPEND {
return true;
}

matches!(
self.opcode,
op::CALL | op::CALLCODE | op::DELEGATECALL | op::STATICCALL | op::CREATE | op::CREATE2
Expand Down
Loading

0 comments on commit 8a13f6c

Please sign in to comment.