Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to casper wasm #10

Merged
merged 5 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ All notable changes to this project will be documented in this file. The format

## [Unreleased]

### Changed
* Move from `parity-wasm` to `casper-wasm`.
* Don't re-export `parity-wasm` anymore to avoid further breaking changes.

## [2.0.0] - 2023-06-14

### Fixed
Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "casper-wasm-utils"
version = "2.0.0"
version = "3.0.0"
edition = "2021"
rust-version = "1.56.1"
authors = ["Nikolay Volf <nikvolf@gmail.com>", "Sergey Pepyakin <s.pepyakin@gmail.com>", "Félix Daudré-Vignier <felix@casperlabs.io>", "Michał Papierski <michal@casperlabs.io>"]
Expand All @@ -24,7 +24,7 @@ required-features = ["cli"]
[dependencies]
byteorder = { version = "1", default-features = false }
log = { version = "0.4", default-features = false }
parity-wasm = { version = "0.45", default-features = false }
casper-wasm = { version = "0.46", default-features = false }

# Dependencies only used by the binaries
clap = { version = "2", optional = true }
Expand All @@ -42,12 +42,12 @@ wabt = "0.10"

[features]
default = ["std"]
std = ["parity-wasm/std", "log/std", "byteorder/std"]
std = ["casper-wasm/std", "log/std", "byteorder/std"]
cli = [
"std",
"glob",
"clap",
"env_logger",
"lazy_static",
]
sign_ext = ["parity-wasm/sign_ext"]
sign_ext = ["casper-wasm/sign_ext"]
4 changes: 2 additions & 2 deletions cli/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ fn main() {

// Loading module
let module =
parity_wasm::deserialize_file(&args[1]).expect("Module deserialization to succeed");
casper_wasm::deserialize_file(&args[1]).expect("Module deserialization to succeed");

let result = utils::inject_gas_counter(module, &utils::rules::Set::default(), "env")
.expect("Failed to inject gas. Some forbidden opcodes?");

parity_wasm::serialize_to_file(&args[2], result).expect("Module serialization to succeed")
casper_wasm::serialize_to_file(&args[2], result).expect("Module serialization to succeed")
}
4 changes: 2 additions & 2 deletions cli/stack_height.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ fn main() {

// Loading module
let module =
parity_wasm::deserialize_file(input_file).expect("Module deserialization to succeed");
casper_wasm::deserialize_file(input_file).expect("Module deserialization to succeed");

let result =
stack_height::inject_limiter(module, 1024).expect("Failed to inject stack height counter");

parity_wasm::serialize_to_file(output_file, result).expect("Module serialization to succeed")
casper_wasm::serialize_to_file(output_file, result).expect("Module serialization to succeed")
}
4 changes: 2 additions & 2 deletions src/ext.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::std::{borrow::ToOwned, string::String, vec::Vec};

use byteorder::{ByteOrder, LittleEndian};
use parity_wasm::{builder, elements};
use casper_wasm::{builder, elements};

use crate::optimizer::{export_section, import_section};

Expand All @@ -12,7 +12,7 @@ pub fn update_call_index(
original_imports: usize,
inserts: &[Insertion],
) {
use parity_wasm::elements::Instruction::*;
use casper_wasm::elements::Instruction::*;
for instruction in instructions.elements_mut().iter_mut() {
if let Call(call_index) = instruction {
if let Some(pos) = inserts.iter().position(|x| x.1 == *call_index) {
Expand Down
14 changes: 7 additions & 7 deletions src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ mod validation;
use crate::std::{cmp::min, mem, vec::Vec};

use crate::rules::Rules;
use parity_wasm::{builder, elements, elements::ValueType};
use casper_wasm::{builder, elements, elements::ValueType};

pub fn update_call_index(instructions: &mut elements::Instructions, inserted_index: u32) {
use parity_wasm::elements::Instruction::*;
use casper_wasm::elements::Instruction::*;
for instruction in instructions.elements_mut().iter_mut() {
if let Call(call_index) = instruction {
if *call_index >= inserted_index {
Expand Down Expand Up @@ -225,7 +225,7 @@ impl Counter {
}

fn inject_grow_counter(instructions: &mut elements::Instructions, grow_counter_func: u32) -> usize {
use parity_wasm::elements::Instruction::*;
use casper_wasm::elements::Instruction::*;
let mut counter = 0;
for instruction in instructions.elements_mut() {
if let GrowMemory(_) = *instruction {
Expand All @@ -242,7 +242,7 @@ fn add_grow_counter<R: Rules>(
gas_func: u32,
) -> elements::Module {
use crate::rules::MemoryGrowCost;
use parity_wasm::elements::Instruction::*;
use casper_wasm::elements::Instruction::*;

let cost = match rules.memory_grow_cost() {
None => return module,
Expand Down Expand Up @@ -278,7 +278,7 @@ pub(crate) fn determine_metered_blocks<R: Rules>(
instructions: &elements::Instructions,
rules: &R,
) -> Result<Vec<MeteredBlock>, ()> {
use parity_wasm::elements::Instruction::*;
use casper_wasm::elements::Instruction::*;

let mut counter = Counter::new();

Expand Down Expand Up @@ -365,7 +365,7 @@ fn insert_metering_calls(
blocks: Vec<MeteredBlock>,
gas_func: u32,
) -> Result<(), ()> {
use parity_wasm::elements::Instruction::*;
use casper_wasm::elements::Instruction::*;

// To do this in linear time, construct a new vector of instructions, copying over old
// instructions one by one and injecting new ones as required.
Expand Down Expand Up @@ -532,7 +532,7 @@ pub fn inject_gas_counter<R: Rules>(
mod tests {
use super::*;
use crate::rules;
use parity_wasm::{builder, elements, elements::Instruction::*, serialize};
use casper_wasm::{builder, elements, elements::Instruction::*, serialize};

pub fn get_function_body(
module: &elements::Module,
Expand Down
4 changes: 2 additions & 2 deletions src/gas/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
rules::{Rules, Set as RuleSet},
std::vec::Vec,
};
use parity_wasm::elements::{FuncBody, Instruction};
use casper_wasm::elements::{FuncBody, Instruction};

use crate::std::collections::BTreeMap as Map;

Expand Down Expand Up @@ -343,7 +343,7 @@ mod tests {
use super::{super::determine_metered_blocks, *};

use binaryen::tools::translate_to_fuzz_mvp;
use parity_wasm::elements;
use casper_wasm::elements;
use rand::{thread_rng, RngCore};

#[test]
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub use ext::{
};
pub use gas::inject_gas_counter;
pub use optimizer::{optimize, Error as OptimizerError};
pub use parity_wasm;

pub struct TargetSymbols {
pub create: &'static str,
Expand Down
10 changes: 5 additions & 5 deletions src/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::std::collections::BTreeSet as Set;
use crate::std::{mem, vec::Vec};

use crate::symbols::{expand_symbols, push_code_symbols, resolve_function, Symbol};
use casper_wasm::elements;
use log::trace;
use parity_wasm::elements;

#[derive(Debug)]
pub enum Error {
Expand Down Expand Up @@ -426,7 +426,7 @@ pub fn optimize(
}

pub fn update_call_index(instructions: &mut elements::Instructions, eliminated_indices: &[usize]) {
use parity_wasm::elements::Instruction::*;
use casper_wasm::elements::Instruction::*;
for instruction in instructions.elements_mut().iter_mut() {
if let Call(call_index) = instruction {
let totalle = eliminated_indices
Expand All @@ -448,7 +448,7 @@ pub fn update_global_index(
instructions: &mut [elements::Instruction],
eliminated_indices: &[usize],
) {
use parity_wasm::elements::Instruction::*;
use casper_wasm::elements::Instruction::*;
for instruction in instructions.iter_mut() {
match instruction {
GetGlobal(index) | SetGlobal(index) => {
Expand All @@ -470,7 +470,7 @@ pub fn update_global_index(

/// Updates global references considering the _ordered_ list of eliminated indices
pub fn update_type_index(instructions: &mut elements::Instructions, eliminated_indices: &[usize]) {
use parity_wasm::elements::Instruction::*;
use casper_wasm::elements::Instruction::*;
for instruction in instructions.elements_mut().iter_mut() {
if let CallIndirect(call_index, _) = instruction {
let totalle = eliminated_indices
Expand Down Expand Up @@ -545,7 +545,7 @@ pub fn type_section(module: &mut elements::Module) -> Option<&mut elements::Type
mod tests {

use super::*;
use parity_wasm::{builder, elements};
use casper_wasm::{builder, elements};

/// @spec 0
/// Optimizer presumes that export section exists and contains
Expand Down
2 changes: 1 addition & 1 deletion src/rules.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::std::collections::BTreeMap as Map;

use crate::std::{num::NonZeroU32, str::FromStr};
use parity_wasm::elements::Instruction;
use casper_wasm::elements::Instruction;

pub struct UnknownInstruction;

Expand Down
69 changes: 37 additions & 32 deletions src/stack_height.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,39 +51,44 @@

use crate::std::{mem, string::String, vec::Vec};

use parity_wasm::{
use casper_wasm::{
builder,
elements::{self, Instruction, Instructions, Type},
};

/// Macro to generate preamble and postamble.
macro_rules! instrument_call {
($callee_idx: expr, $callee_stack_cost: expr, $stack_height_global_idx: expr, $stack_limit: expr) => {{
use $crate::parity_wasm::elements::Instruction::*;
[
// stack_height += stack_cost(F)
GetGlobal($stack_height_global_idx),
I32Const($callee_stack_cost),
I32Add,
SetGlobal($stack_height_global_idx),
// if stack_counter > LIMIT: unreachable
GetGlobal($stack_height_global_idx),
I32Const($stack_limit as i32),
I32GtU,
If(elements::BlockType::NoResult),
Unreachable,
End,
// Original call
Call($callee_idx),
// stack_height -= stack_cost(F)
GetGlobal($stack_height_global_idx),
I32Const($callee_stack_cost),
I32Sub,
SetGlobal($stack_height_global_idx),
]
}};
/// Const function to generate preamble and postamble.
const fn instrument_call(
callee_idx: u32,
callee_stack_cost: u32,
stack_height_global_idx: u32,
stack_limit: u32,
) -> [Instruction; INSTRUMENT_CALL_LENGTH] {
use casper_wasm::elements::Instruction::*;
[
// stack_height += stack_cost(F)
GetGlobal(stack_height_global_idx),
I32Const(callee_stack_cost as i32),
I32Add,
SetGlobal(stack_height_global_idx),
// if stack_counter > LIMIT: unreachable
GetGlobal(stack_height_global_idx),
I32Const(stack_limit as i32),
I32GtU,
If(elements::BlockType::NoResult),
Unreachable,
End,
// Original call
Call(callee_idx),
// stack_height -= stack_cost(F)
GetGlobal(stack_height_global_idx),
I32Const(callee_stack_cost as i32),
I32Sub,
SetGlobal(stack_height_global_idx),
]
}

const INSTRUMENT_CALL_LENGTH: usize = 15;

mod max_height;
mod thunk;

Expand Down Expand Up @@ -282,7 +287,7 @@ fn instrument_function(ctx: &mut Context, func: &mut Instructions) -> Result<(),
.collect();

// The `instrumented_call!` contains the call itself. This is why we need to subtract one.
let len = func.elements().len() + calls.len() * (instrument_call!(0, 0, 0, 0).len() - 1);
let len = func.elements().len() + calls.len() * (INSTRUMENT_CALL_LENGTH - 1);
let original_instrs = mem::replace(func.elements_mut(), Vec::with_capacity(len));
let new_instrs = func.elements_mut();

Expand All @@ -291,11 +296,11 @@ fn instrument_function(ctx: &mut Context, func: &mut Instructions) -> Result<(),
// whether there is some call instruction at this position that needs to be instrumented
let did_instrument = if let Some(call) = calls.peek() {
if call.offset == original_pos {
let new_seq = instrument_call!(
let new_seq = instrument_call(
call.callee,
call.cost as i32,
call.cost,
ctx.stack_height_global_idx(),
ctx.stack_limit()
ctx.stack_limit(),
);
new_instrs.extend(new_seq);
true
Expand Down Expand Up @@ -365,7 +370,7 @@ fn resolve_func_type(
#[cfg(test)]
mod tests {
use super::*;
use parity_wasm::elements;
use casper_wasm::elements;

fn parse_wat(source: &str) -> elements::Module {
elements::deserialize_buffer(&wabt::wat2wasm(source).expect("Failed to wat2wasm"))
Expand Down
8 changes: 4 additions & 4 deletions src/stack_height/max_height.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::std::vec::Vec;

use super::{resolve_func_type, Error};
use casper_wasm::elements::{self, BlockType, Type};
use log::trace;
use parity_wasm::elements::{self, BlockType, Type};

#[cfg(feature = "sign_ext")]
use parity_wasm::elements::SignExtInstruction;
use casper_wasm::elements::SignExtInstruction;

/// Control stack frame.
#[derive(Debug)]
Expand Down Expand Up @@ -143,7 +143,7 @@ impl Stack {

/// This function expects the function to be validated.
pub(crate) fn compute(func_idx: u32, module: &elements::Module) -> Result<u32, Error> {
use parity_wasm::elements::Instruction::*;
use casper_wasm::elements::Instruction::*;

let func_section = module
.function_section()
Expand Down Expand Up @@ -443,7 +443,7 @@ pub(crate) fn compute(func_idx: u32, module: &elements::Module) -> Result<u32, E
#[cfg(test)]
mod tests {
use super::*;
use parity_wasm::elements;
use casper_wasm::elements;

fn parse_wat(source: &str) -> elements::Module {
elements::deserialize_buffer(&wabt::wat2wasm(source).expect("Failed to wat2wasm"))
Expand Down
10 changes: 5 additions & 5 deletions src/stack_height/thunk.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::std::collections::{BTreeMap as Map, BTreeSet as Set};
use crate::std::vec::Vec;

use parity_wasm::{
use casper_wasm::{
builder,
elements::{self, FunctionType, Internal},
};

use super::{resolve_func_type, Context, Error};
use super::{instrument_call, resolve_func_type, Context, Error};

struct Thunk {
signature: FunctionType,
Expand Down Expand Up @@ -77,11 +77,11 @@ pub(crate) fn generate_thunks(

let mut mbuilder = builder::from_module(module);
for (func_idx, thunk) in replacement_map.iter_mut() {
let instrumented_call = instrument_call!(
let instrumented_call = instrument_call(
*func_idx,
thunk.callee_stack_cost as i32,
thunk.callee_stack_cost,
ctx.stack_height_global_idx(),
ctx.stack_limit()
ctx.stack_limit(),
);
// Thunk body consist of:
// - argument pushing
Expand Down
Loading
Loading