From 8b25e3220669c5b2784ed1ed850b2c229aebe550 Mon Sep 17 00:00:00 2001 From: Evgeny Kuzyakov Date: Mon, 24 Feb 2020 10:07:40 -0800 Subject: [PATCH 1/3] fix(runtime): Fix empty method_names bug --- runtime/near-vm-logic/src/lib.rs | 3 +- runtime/near-vm-logic/src/logic.rs | 16 ++---- runtime/near-vm-logic/src/utils.rs | 80 ++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 14 deletions(-) create mode 100644 runtime/near-vm-logic/src/utils.rs diff --git a/runtime/near-vm-logic/src/lib.rs b/runtime/near-vm-logic/src/lib.rs index 959aab68448..b3058c148a6 100644 --- a/runtime/near-vm-logic/src/lib.rs +++ b/runtime/near-vm-logic/src/lib.rs @@ -6,8 +6,9 @@ mod logic; #[cfg(not(target_arch = "wasm32"))] pub mod mocks; pub mod serde_with; - pub mod types; +mod utils; + pub use config::{ExtCosts, ExtCostsConfig, VMConfig, VMLimitConfig}; pub use context::VMContext; pub use dependencies::{External, MemoryLike, ValuePtr}; diff --git a/runtime/near-vm-logic/src/logic.rs b/runtime/near-vm-logic/src/logic.rs index 16bdf9013b6..a289646938e 100644 --- a/runtime/near-vm-logic/src/logic.rs +++ b/runtime/near-vm-logic/src/logic.rs @@ -7,6 +7,7 @@ use crate::types::{ AccountId, Balance, Gas, IteratorIndex, PromiseIndex, PromiseResult, ReceiptIndex, ReturnData, StorageUsage, }; +use crate::utils::split_method_names; use crate::{ExtCosts, HostError, VMLogicError, ValuePtr}; use byteorder::ByteOrder; use near_runtime_fees::RuntimeFeesConfig; @@ -1436,20 +1437,9 @@ impl<'a> VMLogic<'a> { let allowance = self.memory_get_u128(allowance_ptr)?; let allowance = if allowance > 0 { Some(allowance) } else { None }; let receiver_id = self.read_and_parse_account_id(receiver_id_ptr, receiver_id_len)?; - let method_names = + let raw_method_names = self.get_vec_from_memory_or_register(method_names_ptr, method_names_len)?; - // Use `,` separator to split `method_names` into a vector of method names. - let method_names = - method_names - .split(|c| *c == b',') - .map(|v| { - if v.is_empty() { - Err(HostError::EmptyMethodName.into()) - } else { - Ok(v.to_vec()) - } - }) - .collect::>>()?; + let method_names = split_method_names(&raw_method_names)?; let (receipt_idx, sir) = self.promise_idx_to_receipt_idx_with_sir(promise_idx)?; diff --git a/runtime/near-vm-logic/src/utils.rs b/runtime/near-vm-logic/src/utils.rs new file mode 100644 index 00000000000..eb3360eebe7 --- /dev/null +++ b/runtime/near-vm-logic/src/utils.rs @@ -0,0 +1,80 @@ +use near_vm_errors::{HostError, VMLogicError}; + +type Result = ::std::result::Result; + +/// Uses `,` separator to split `method_names` into a vector of method names. +/// Returns an empty vec if the empty slice is given. +/// Throws `HostError::EmptyMethodName` in case there is an empty method name inside. +pub(crate) fn split_method_names(method_names: &[u8]) -> Result>> { + if method_names.is_empty() { + Ok(vec![]) + } else { + method_names + .split(|c| *c == b',') + .map( + |v| { + if v.is_empty() { + Err(HostError::EmptyMethodName.into()) + } else { + Ok(v.to_vec()) + } + }, + ) + .collect::>>() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_split_method_names_empty() { + assert_eq!(split_method_names(b""), Ok(vec![])); + } + + #[test] + fn test_split_method_one_name() { + assert_eq!(split_method_names(b"hello"), Ok(vec![b"hello".to_vec()])); + } + + #[test] + fn test_split_method_two_method_names() { + assert_eq!( + split_method_names(b"hello,world"), + Ok(vec![b"hello".to_vec(), b"world".to_vec()]) + ); + } + + #[test] + fn test_split_empty_method_name_inside() { + assert_eq!( + split_method_names(b"hello,,world"), + Err(VMLogicError::HostError(HostError::EmptyMethodName)) + ); + } + + #[test] + fn test_split_empty_method_name_front() { + assert_eq!( + split_method_names(b",world"), + Err(VMLogicError::HostError(HostError::EmptyMethodName)) + ); + } + + #[test] + fn test_split_empty_method_name_back() { + assert_eq!( + split_method_names(b"world,"), + Err(VMLogicError::HostError(HostError::EmptyMethodName)) + ); + } + + #[test] + fn test_split_empty_method_name_comma_only() { + assert_eq!( + split_method_names(b","), + Err(VMLogicError::HostError(HostError::EmptyMethodName)) + ); + } +} From e20ba49c79e9255fab4dee7cd41a0e5d2b7e8351 Mon Sep 17 00:00:00 2001 From: Evgeny Kuzyakov Date: Mon, 24 Feb 2020 10:11:47 -0800 Subject: [PATCH 2/3] Bump near-vm-logic version to 0.4.5 --- Cargo.lock | 12 ++++++------ runtime/near-vm-logic/Cargo.toml | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 412d7c81607..2d09f269bc3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2219,7 +2219,7 @@ version = "0.1.0" dependencies = [ "near-primitives 0.1.0", "near-runtime-fees 0.4.4", - "near-vm-logic 0.4.4", + "near-vm-logic 0.4.5", "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2274,7 +2274,7 @@ dependencies = [ [[package]] name = "near-vm-logic" -version = "0.4.4" +version = "0.4.5" dependencies = [ "bs58 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2295,7 +2295,7 @@ dependencies = [ "cached 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "near-runtime-fees 0.4.4", "near-vm-errors 0.4.4", - "near-vm-logic 0.4.4", + "near-vm-logic 0.4.5", "parity-wasm 0.41.0 (registry+https://github.com/rust-lang/crates.io-index)", "pwasm-utils 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "wabt 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2310,7 +2310,7 @@ version = "0.4.4" dependencies = [ "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "near-runtime-fees 0.4.4", - "near-vm-logic 0.4.4", + "near-vm-logic 0.4.5", "near-vm-runner 0.4.4", "serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2380,7 +2380,7 @@ dependencies = [ "near-runtime-fees 0.4.4", "near-store 0.1.0", "near-vm-errors 0.4.4", - "near-vm-logic 0.4.4", + "near-vm-logic 0.4.5", "near-vm-runner 0.4.4", "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3066,7 +3066,7 @@ dependencies = [ "near-primitives 0.1.0", "near-runtime-fees 0.4.4", "near-store 0.1.0", - "near-vm-logic 0.4.4", + "near-vm-logic 0.4.5", "near-vm-runner 0.4.4", "node-runtime 0.0.1", "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/runtime/near-vm-logic/Cargo.toml b/runtime/near-vm-logic/Cargo.toml index a8ab4851d7f..dccf4a1d62c 100644 --- a/runtime/near-vm-logic/Cargo.toml +++ b/runtime/near-vm-logic/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "near-vm-logic" -version = "0.4.4" +version = "0.4.5" authors = ["Near Inc "] edition = "2018" license = "Apache-2.0" From bdd46f69c3aa4a48e3a3b1d883bcb10328b31362 Mon Sep 17 00:00:00 2001 From: Evgeny Kuzyakov Date: Tue, 25 Feb 2020 10:32:56 -0800 Subject: [PATCH 3/3] Bump versions --- Cargo.lock | 38 ++++++++++---------- runtime/near-runtime-fees/Cargo.toml | 2 +- runtime/near-vm-errors/Cargo.toml | 2 +- runtime/near-vm-logic/Cargo.toml | 4 +-- runtime/near-vm-runner-standalone/Cargo.toml | 8 ++--- runtime/near-vm-runner/Cargo.toml | 8 ++--- 6 files changed, 31 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2d09f269bc3..7c0f0e920af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2180,7 +2180,7 @@ dependencies = [ "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "near-crypto 0.1.0", "near-rpc-error-macro 0.1.0", - "near-vm-errors 0.4.4", + "near-vm-errors 0.4.5", "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "reed-solomon-erasure 4.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2218,14 +2218,14 @@ name = "near-runtime-configs" version = "0.1.0" dependencies = [ "near-primitives 0.1.0", - "near-runtime-fees 0.4.4", + "near-runtime-fees 0.4.5", "near-vm-logic 0.4.5", "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "near-runtime-fees" -version = "0.4.4" +version = "0.4.5" dependencies = [ "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2265,7 +2265,7 @@ dependencies = [ [[package]] name = "near-vm-errors" -version = "0.4.4" +version = "0.4.5" dependencies = [ "borsh 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "near-rpc-error-macro 0.1.0", @@ -2278,8 +2278,8 @@ version = "0.4.5" dependencies = [ "bs58 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", - "near-runtime-fees 0.4.4", - "near-vm-errors 0.4.4", + "near-runtime-fees 0.4.5", + "near-vm-errors 0.4.5", "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)", "sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2288,13 +2288,13 @@ dependencies = [ [[package]] name = "near-vm-runner" -version = "0.4.4" +version = "0.4.5" dependencies = [ "assert_matches 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "bencher 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "cached 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", - "near-runtime-fees 0.4.4", - "near-vm-errors 0.4.4", + "near-runtime-fees 0.4.5", + "near-vm-errors 0.4.5", "near-vm-logic 0.4.5", "parity-wasm 0.41.0 (registry+https://github.com/rust-lang/crates.io-index)", "pwasm-utils 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2306,12 +2306,12 @@ dependencies = [ [[package]] name = "near-vm-runner-standalone" -version = "0.4.4" +version = "0.4.5" dependencies = [ "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", - "near-runtime-fees 0.4.4", + "near-runtime-fees 0.4.5", "near-vm-logic 0.4.5", - "near-vm-runner 0.4.4", + "near-vm-runner 0.4.5", "serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2377,11 +2377,11 @@ dependencies = [ "near-metrics 0.1.0", "near-primitives 0.1.0", "near-runtime-configs 0.1.0", - "near-runtime-fees 0.4.4", + "near-runtime-fees 0.4.5", "near-store 0.1.0", - "near-vm-errors 0.4.4", + "near-vm-errors 0.4.5", "near-vm-logic 0.4.5", - "near-vm-runner 0.4.4", + "near-vm-runner 0.4.5", "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "rocksdb 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3064,10 +3064,10 @@ dependencies = [ "near 0.4.13", "near-crypto 0.1.0", "near-primitives 0.1.0", - "near-runtime-fees 0.4.4", + "near-runtime-fees 0.4.5", "near-store 0.1.0", "near-vm-logic 0.4.5", - "near-vm-runner 0.4.4", + "near-vm-runner 0.4.5", "node-runtime 0.0.1", "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand_xorshift 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3495,9 +3495,9 @@ dependencies = [ "near-jsonrpc-client 0.1.0", "near-network 0.1.0", "near-primitives 0.1.0", - "near-runtime-fees 0.4.4", + "near-runtime-fees 0.4.5", "near-store 0.1.0", - "near-vm-errors 0.4.4", + "near-vm-errors 0.4.5", "node-runtime 0.0.1", "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/runtime/near-runtime-fees/Cargo.toml b/runtime/near-runtime-fees/Cargo.toml index cbad016123f..1e7a50533e0 100644 --- a/runtime/near-runtime-fees/Cargo.toml +++ b/runtime/near-runtime-fees/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "near-runtime-fees" -version = "0.4.4" +version = "0.4.5" authors = ["Near Inc "] edition = "2018" license = "Apache-2.0" diff --git a/runtime/near-vm-errors/Cargo.toml b/runtime/near-vm-errors/Cargo.toml index 44265fc8cf7..3920357a861 100644 --- a/runtime/near-vm-errors/Cargo.toml +++ b/runtime/near-vm-errors/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "near-vm-errors" -version = "0.4.4" +version = "0.4.5" authors = ["Near Inc "] edition = "2018" license = "Apache-2.0" diff --git a/runtime/near-vm-logic/Cargo.toml b/runtime/near-vm-logic/Cargo.toml index dccf4a1d62c..7b482be33f7 100644 --- a/runtime/near-vm-logic/Cargo.toml +++ b/runtime/near-vm-logic/Cargo.toml @@ -16,8 +16,8 @@ This crate implements the specification of the interface that Near blockchain ex byteorder = "1.2" bs58 = "0.3" serde = { version = "1.0", features = ["derive"] } -near-runtime-fees = { path = "../near-runtime-fees", version = "0.4.4" } -near-vm-errors = { path = "../near-vm-errors", version = "0.4.4" } +near-runtime-fees = { path = "../near-runtime-fees", version = "0.4.5" } +near-vm-errors = { path = "../near-vm-errors", version = "0.4.5" } [dev-dependencies] serde_json = {version= "1.0", features= ["preserve_order"]} diff --git a/runtime/near-vm-runner-standalone/Cargo.toml b/runtime/near-vm-runner-standalone/Cargo.toml index 6912097981a..5b45a968971 100644 --- a/runtime/near-vm-runner-standalone/Cargo.toml +++ b/runtime/near-vm-runner-standalone/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "near-vm-runner-standalone" -version = "0.4.4" +version = "0.4.5" authors = ["Near Inc "] edition = "2018" license = "Apache-2.0" @@ -21,6 +21,6 @@ to make sure it has expected behavior once deployed to the blockchain. [dependencies] serde_json = "1.0" clap = "2.33.0" -near-vm-logic = { path = "../near-vm-logic", version = "0.4.4"} -near-vm-runner = { path = "../near-vm-runner", version = "0.4.4" } -near-runtime-fees = { path = "../near-runtime-fees", version = "0.4.4" } +near-vm-logic = { path = "../near-vm-logic", version = "0.4.5"} +near-vm-runner = { path = "../near-vm-runner", version = "0.4.5" } +near-runtime-fees = { path = "../near-runtime-fees", version = "0.4.5" } diff --git a/runtime/near-vm-runner/Cargo.toml b/runtime/near-vm-runner/Cargo.toml index b2e97631e9d..dd8a2edcdf3 100644 --- a/runtime/near-vm-runner/Cargo.toml +++ b/runtime/near-vm-runner/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "near-vm-runner" -version = "0.4.4" +version = "0.4.5" authors = ["Near Inc "] edition = "2018" license = "Apache-2.0" @@ -16,9 +16,9 @@ This crate implements the specification of the interface that Near blockchain ex cached = "0.11.0" wasmer-runtime = { version = "0.13.1", features = ["default-backend-singlepass"], default-features = false } wasmer-runtime-core = { version = "0.13.1" } -near-runtime-fees = { path="../near-runtime-fees", version = "0.4.4" } -near-vm-logic = { path="../near-vm-logic", version = "0.4.4", default-features = false, features = []} -near-vm-errors = { path = "../near-vm-errors", version = "0.4.4" } +near-runtime-fees = { path="../near-runtime-fees", version = "0.4.5" } +near-vm-logic = { path="../near-vm-logic", version = "0.4.5", default-features = false, features = []} +near-vm-errors = { path = "../near-vm-errors", version = "0.4.5" } pwasm-utils = "0.12.0" parity-wasm = "0.41.0" wasmparser = "0.44"