Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions ethcore/vm/src/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ pub struct WasmCosts {
/// Memory (load/store) operations multiplier.
pub mem: u32,
/// Memory copy operation, per byte.
pub mem_cmp: u32,
/// Memory copy operation, per byte.
pub mem_copy: u32,
/// Memory move operation, per byte.
pub mem_move: u32,
Expand All @@ -148,6 +150,7 @@ impl Default for WasmCosts {
div: 16,
mul: 4,
mem: 2,
mem_cmp: 1,
mem_copy: 1,
mem_move: 1,
mem_set: 1,
Expand Down
1 change: 1 addition & 0 deletions ethcore/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ byteorder = "1.0"
ethereum-types = "0.1"
log = "0.3"
parity-wasm = "0.15"
libc = "0.2"
wasm-utils = { git = "https://github.com/paritytech/wasm-utils" }
vm = { path = "../vm" }
ethcore-logger = { path = "../../logger" }
5 changes: 5 additions & 0 deletions ethcore/wasm/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ pub const SIGNATURES: &'static [UserFunctionDescriptor] = &[
&[I32; 3],
Some(I32),
),
Static(
"_ext_memcmp",
&[I32; 3],
Some(I32),
),
Static(
"_ext_memcpy",
&[I32; 3],
Expand Down
1 change: 1 addition & 0 deletions ethcore/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern crate ethereum_types;
extern crate ethcore_logger;
extern crate byteorder;
extern crate parity_wasm;
extern crate libc;
extern crate wasm_utils;

mod runtime;
Expand Down
26 changes: 26 additions & 0 deletions ethcore/wasm/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use std::sync::Arc;

use byteorder::{LittleEndian, ByteOrder};
use libc::{memcmp, c_void};

use vm;
use panic_payload;
Expand Down Expand Up @@ -567,6 +568,28 @@ impl<'a, 'b> Runtime<'a, 'b> {
&*self.memory
}

fn mem_cmp(&mut self, context: InterpreterCallerContext)
-> Result<Option<interpreter::RuntimeValue>, InterpreterError>
{
//
// method signature:
// fn memcmp(cx: *const u8, ct: *const u8, n: usize) -> i32;
//

let len = context.value_stack.pop_as::<i32>()? as u32;
let cx = context.value_stack.pop_as::<i32>()? as u32;
let ct = context.value_stack.pop_as::<i32>()? as u32;

self.charge(|schedule| schedule.wasm.mem_cmp as u64 * len as u64)?;

let cx = self.memory.get(cx, len as usize)?;
let ct = self.memory.get(ct, len as usize)?;
let result = unsafe {
memcmp(cx.as_ptr() as *const c_void, ct.as_ptr() as *const c_void, len as usize)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memcmp should fail when invoked on overlapping region, because otherwise it's undefined behaviour?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, on the second thought, it shouldn't be

};
Ok(Some(Into::into(result)))
}

fn mem_copy(&mut self, context: InterpreterCallerContext)
-> Result<Option<interpreter::RuntimeValue>, InterpreterError>
{
Expand Down Expand Up @@ -890,6 +913,9 @@ impl<'a, 'b> interpreter::UserFunctionExecutor<UserTrap> for Runtime<'a, 'b> {
"_emscripten_memcpy_big" => {
self.mem_copy(context)
},
"_ext_memcmp" => {
self.mem_cmp(context)
},
"_ext_memcpy" => {
self.mem_copy(context)
},
Expand Down