Skip to content

Commit

Permalink
fix runtime exception in native function
Browse files Browse the repository at this point in the history
  • Loading branch information
steelgeek091 committed Apr 14, 2024
1 parent f4a65e8 commit f44c306
Show file tree
Hide file tree
Showing 20 changed files with 356 additions and 106 deletions.
8 changes: 8 additions & 0 deletions frameworks/moveos-stdlib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::natives::moveos_stdlib::wasm::E_INCORRECT_LENGTH_OF_ARGS;
use move_binary_format::errors::PartialVMResult;
use move_core_types::account_address::AccountAddress;
use move_core_types::gas_algebra::InternalGas;
use move_vm_types::natives::function::NativeResult;
use moveos_types::addresses::MOVEOS_NAMED_ADDRESS_MAPPING;
use std::{collections::BTreeMap, str::FromStr};

Expand All @@ -22,3 +26,7 @@ pub fn moveos_stdlib_named_addresses() -> BTreeMap<String, AccountAddress> {
);
address_mapping
}

pub fn args_count_error(base_gas: InternalGas) -> PartialVMResult<NativeResult> {
Ok(NativeResult::err(base_gas, E_INCORRECT_LENGTH_OF_ARGS))
}
10 changes: 8 additions & 2 deletions frameworks/moveos-stdlib/src/natives/moveos_stdlib/account.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::args_count_error;
use move_binary_format::errors::PartialVMResult;
use move_core_types::account_address::AccountAddress;
use move_core_types::gas_algebra::InternalGas;
Expand Down Expand Up @@ -39,8 +40,13 @@ pub fn native_create_signer(
ty_args: Vec<Type>,
mut arguments: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert!(ty_args.is_empty());
debug_assert!(arguments.len() == 1);
if !ty_args.is_empty() {
return args_count_error(gas_params.base);
}

if arguments.len() != 1 {
return args_count_error(gas_params.base);
}

let address = pop_arg!(arguments, AccountAddress);
Ok(NativeResult::ok(
Expand Down
10 changes: 8 additions & 2 deletions frameworks/moveos-stdlib/src/natives/moveos_stdlib/bcs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::args_count_error;
use crate::natives::helpers::{make_module_natives, make_native};
use move_binary_format::errors::{PartialVMError, PartialVMResult};
use move_core_types::gas_algebra::{InternalGas, InternalGasPerByte, NumBytes};
Expand Down Expand Up @@ -38,8 +39,13 @@ fn native_from_bytes(
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert_eq!(ty_args.len(), 1);
debug_assert_eq!(args.len(), 1);
if ty_args.len() != 1 {
return args_count_error(gas_params.base);
}

if args.len() != 1 {
return args_count_error(gas_params.base);
}

let mut cost = gas_params.base;

Expand Down
10 changes: 8 additions & 2 deletions frameworks/moveos-stdlib/src/natives/moveos_stdlib/event.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::args_count_error;
use crate::natives::helpers;
use better_any::{Tid, TidAble};
use move_binary_format::errors::{PartialVMError, PartialVMResult};
Expand Down Expand Up @@ -55,8 +56,13 @@ pub fn native_emit(
mut ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert!(ty_args.len() == 1);
debug_assert!(args.len() == 1);
if ty_args.len() != 1 {
return args_count_error(gas_params.base);
}

if args.len() != 1 {
return args_count_error(gas_params.base);
}

let mut cost = gas_params.base;

Expand Down
10 changes: 8 additions & 2 deletions frameworks/moveos-stdlib/src/natives/moveos_stdlib/json.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::args_count_error;
use crate::natives::helpers::{make_module_natives, make_native};
use anyhow::Result;
use log::debug;
Expand Down Expand Up @@ -235,8 +236,13 @@ fn native_from_json(
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert_eq!(ty_args.len(), 1);
debug_assert_eq!(args.len(), 1);
if ty_args.len() != 1 {
return args_count_error(gas_params.base);
}

if args.len() != 1 {
return args_count_error(gas_params.base);
}

let mut cost = gas_params.base;
let type_param = &ty_args[0];
Expand Down
60 changes: 54 additions & 6 deletions frameworks/moveos-stdlib/src/natives/moveos_stdlib/move_module.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::args_count_error;
use crate::natives::helpers::{make_module_natives, make_native};
use better_any::{Tid, TidAble};
use framework_builder::dependency_order::sort_by_dependency_order;
Expand Down Expand Up @@ -73,6 +74,14 @@ fn native_module_id_inner(
_ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
if !_ty_args.is_empty() {
return args_count_error(gas_params.base);
}

if args.len() != 1 {
return args_count_error(gas_params.base);
}

let byte_codes = pop_arg!(args, VectorRef);
let byte_codes_ref = byte_codes.as_bytes_ref();

Expand All @@ -96,9 +105,17 @@ pub struct ModuleIdFromNameInnerGasParameters {
fn native_module_id_from_name_inner(
gas_params: &ModuleIdFromNameInnerGasParameters,
_context: &mut NativeContext,
_ty_args: Vec<Type>,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
if !ty_args.is_empty() {
return args_count_error(gas_params.base);
}

if args.len() != 2 {
return args_count_error(gas_params.base);
}

let name = args.pop_back().unwrap();
let name_ident = unpack_string_to_identifier(name)?;
let account_address = pop_arg!(args, AccountAddress);
Expand Down Expand Up @@ -131,9 +148,17 @@ pub struct VerifyModulesGasParameters {
fn native_sort_and_verify_modules_inner(
gas_params: &VerifyModulesGasParameters,
context: &mut NativeContext,
_ty_args: Vec<Type>,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
if !ty_args.is_empty() {
return args_count_error(gas_params.base);
}

if args.len() < 2 {
return args_count_error(gas_params.base);
}

let mut cost = gas_params.base;
let account_address = pop_arg!(args, AccountAddress);
let mut bundle = vec![];
Expand Down Expand Up @@ -240,9 +265,17 @@ pub struct RequestInitFunctionsGasParameters {
fn request_init_functions(
gas_params: &RequestInitFunctionsGasParameters,
context: &mut NativeContext,
_ty_args: Vec<Type>,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
if !ty_args.is_empty() {
return args_count_error(gas_params.base);
}

if args.is_empty() {
return args_count_error(gas_params.base);
}

let mut cost = gas_params.base;
let module_context = context.extensions_mut().get_mut::<NativeModuleContext>();
for id_value in pop_arg!(args, Vec<Value>) {
Expand Down Expand Up @@ -276,9 +309,17 @@ pub struct CheckCompatibilityInnerGasParameters {
fn check_compatibililty_inner(
gas_params: &CheckCompatibilityInnerGasParameters,
_context: &mut NativeContext,
_ty_args: Vec<Type>,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
if !ty_args.is_empty() {
return args_count_error(gas_params.base);
}

if args.len() < 2 {
return args_count_error(gas_params.base);
}

let mut cost = gas_params.base;
// TODO: config compatibility through global configuration
let compat = Compatibility::full_check();
Expand Down Expand Up @@ -311,7 +352,7 @@ pub struct ModuleModifyGasParameters {
fn module_replace_template<C, F, TF>(
gas_params: &ModuleModifyGasParameters,
_context: &mut NativeContext,
_ty_args: Vec<Type>,
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
element_type: Type,
modify_fn: F,
Expand All @@ -322,7 +363,14 @@ where
F: Fn(&mut CompiledModule, &HashMap<C, C>) -> PartialVMResult<()>,
TF: Fn(Value) -> PartialVMResult<C>,
{
debug_assert!(args.len() == 3, "Wrong number of arguments");
if !ty_args.is_empty() {
return args_count_error(gas_params.base);
}

if args.len() != 3 {
return args_count_error(gas_params.base);
}

let mut cost = gas_params.base;
let new_vec = pop_arg!(args, Vector);
let old_vec = pop_arg!(args, Vector);
Expand Down
73 changes: 57 additions & 16 deletions frameworks/moveos-stdlib/src/natives/moveos_stdlib/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use moveos_types::{
use smallvec::smallvec;
use std::{collections::VecDeque, sync::Arc};

use crate::args_count_error;
pub use moveos_object_runtime::runtime::{
ERROR_ALREADY_EXISTS, ERROR_NOT_FOUND, ERROR_OBJECT_ALREADY_BORROWED,
ERROR_OBJECT_RUNTIME_ERROR, ERROR_TYPE_MISMATCH,
Expand Down Expand Up @@ -51,8 +52,13 @@ fn native_as_ref_inner(
ty_args: Vec<Type>,
mut arguments: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert!(ty_args.len() == 1);
debug_assert!(arguments.len() == 1);
if ty_args.len() != 1 {
return args_count_error(gas_params.base);
}

if arguments.len() != 1 {
return args_count_error(gas_params.base);
}

let object_id = arguments.pop_back().unwrap();
let object_ref = borrow_object_reference(context, object_id, &ty_args[0])?;
Expand Down Expand Up @@ -89,8 +95,13 @@ fn native_as_mut_ref_inner(
ty_args: Vec<Type>,
mut arguments: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert!(ty_args.len() == 1);
debug_assert!(arguments.len() == 1);
if ty_args.len() != 1 {
return args_count_error(gas_params.base);
}

if arguments.len() != 1 {
return args_count_error(gas_params.base);
}

let object_id = arguments.pop_back().unwrap();
let object_ref = borrow_object_reference(context, object_id, &ty_args[0])?;
Expand Down Expand Up @@ -207,8 +218,14 @@ fn native_borrow_root(
ty_args: Vec<Type>,
args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert_eq!(ty_args.len(), 0);
debug_assert_eq!(args.len(), 0);
if !ty_args.is_empty() {
return args_count_error(common_gas_params.load_base);
}

if !args.is_empty() {
return args_count_error(common_gas_params.load_base);
}

let object_context = context.extensions().get::<ObjectRuntimeContext>();
let object_runtime = object_context.object_runtime();
let value = object_runtime.read().borrow_root()?;
Expand Down Expand Up @@ -239,9 +256,13 @@ fn native_add_field(
) -> PartialVMResult<NativeResult> {
//0 K Type
//1 V Type FieldValue or ObjectEntity
if ty_args.len() != 2 {
return args_count_error(gas_params.base);
}

debug_assert_eq!(ty_args.len(), 2);
debug_assert_eq!(args.len(), 3);
if args.len() != 3 {
return args_count_error(gas_params.base);
}

let val = args.pop_back().unwrap();
let key = args.pop_back().unwrap();
Expand Down Expand Up @@ -288,8 +309,13 @@ fn native_borrow_field(
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert_eq!(ty_args.len(), 2);
debug_assert_eq!(args.len(), 2);
if ty_args.len() != 2 {
return args_count_error(gas_params.base);
}

if args.len() != 2 {
return args_count_error(gas_params.base);
}

let key = args.pop_back().unwrap();
let object_id = pop_object_id(&mut args)?;
Expand Down Expand Up @@ -334,8 +360,13 @@ fn native_contains_field(
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert_eq!(ty_args.len(), 1);
debug_assert_eq!(args.len(), 2);
if ty_args.len() != 1 {
return args_count_error(gas_params.base);
}

if args.len() != 2 {
return args_count_error(gas_params.base);
}

let key = args.pop_back().unwrap();
let object_id = pop_object_id(&mut args)?;
Expand Down Expand Up @@ -371,8 +402,13 @@ fn native_contains_field_with_value_type(
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert_eq!(ty_args.len(), 2);
debug_assert_eq!(args.len(), 2);
if ty_args.len() != 2 {
return args_count_error(gas_params.base);
}

if args.len() != 2 {
return args_count_error(gas_params.base);
}

let key = args.pop_back().unwrap();
let object_id = pop_object_id(&mut args)?;
Expand Down Expand Up @@ -423,8 +459,13 @@ fn native_remove_field(
ty_args: Vec<Type>,
mut args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
debug_assert_eq!(ty_args.len(), 2);
debug_assert_eq!(args.len(), 2);
if ty_args.len() != 2 {
return args_count_error(gas_params.base);
}

if args.len() != 2 {
return args_count_error(gas_params.base);
}

let key = args.pop_back().unwrap();
let object_id = pop_object_id(&mut args)?;
Expand Down
Loading

0 comments on commit f44c306

Please sign in to comment.