From 9e0fd8207fc757338db6ac102399c6fec019f200 Mon Sep 17 00:00:00 2001 From: Igor Date: Fri, 8 Nov 2024 12:27:05 -0800 Subject: [PATCH] addressing comments --- .../aptos-vm-environment/src/natives.rs | 20 +++++++++++++++++++ .../move-vm/runtime/src/native_functions.rs | 20 +------------------ .../move-vm/types/src/values/values_impl.rs | 5 ++++- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/aptos-move/aptos-vm-environment/src/natives.rs b/aptos-move/aptos-vm-environment/src/natives.rs index b4a3c2d2228049..d138ca40c2d2da 100644 --- a/aptos-move/aptos-vm-environment/src/natives.rs +++ b/aptos-move/aptos-vm-environment/src/natives.rs @@ -4,15 +4,35 @@ use aptos_native_interface::SafeNativeBuilder; use move_core_types::language_storage::CORE_CODE_ADDRESS; use move_vm_runtime::native_functions::NativeFunctionTable; +use std::collections::HashSet; /// Builds and returns all Aptos native functions. pub fn aptos_natives_with_builder( builder: &mut SafeNativeBuilder, inject_create_signer_for_gov_sim: bool, ) -> NativeFunctionTable { + let vector_bytecode_instruction_methods = HashSet::from([ + "empty", + "length", + "borrow", + "borrow_mut", + "push_back", + "pop_back", + "destroy_empty", + "swap", + ]); + #[allow(unreachable_code)] aptos_move_stdlib::natives::all_natives(CORE_CODE_ADDRESS, builder) .into_iter() + .filter(|(_, name, func_name, _)| + if name.as_str() == "vector" && vector_bytecode_instruction_methods.contains(func_name.as_str()) { + println!("ERROR: Tried to register as native a vector bytecode_instruction method {}, skipping.", func_name.as_str()); + false + } else { + true + } + ) .chain(aptos_framework::natives::all_natives( CORE_CODE_ADDRESS, builder, diff --git a/third_party/move/move-vm/runtime/src/native_functions.rs b/third_party/move/move-vm/runtime/src/native_functions.rs index 2c703a1d3d179b..9a5f4ead8eaa8d 100644 --- a/third_party/move/move-vm/runtime/src/native_functions.rs +++ b/third_party/move/move-vm/runtime/src/native_functions.rs @@ -24,7 +24,7 @@ use move_vm_types::{ loaded_data::runtime_types::Type, natives::function::NativeResult, values::Value, }; use std::{ - collections::{HashMap, HashSet, VecDeque}, + collections::{HashMap, VecDeque}, fmt::Write, sync::Arc, }; @@ -81,26 +81,8 @@ impl NativeFunctions { where I: IntoIterator, { - let vector_bytecode_instruction_methods = HashSet::from([ - "empty", - "length", - "borrow", - "borrow_mut", - "push_back", - "pop_back", - "destroy_empty", - "swap", - ]); - let mut map = HashMap::new(); for (addr, module_name, func_name, func) in natives.into_iter() { - if module_name.as_str() == "string" - && vector_bytecode_instruction_methods.contains(func_name.as_str()) - { - println!("ERROR: Tried to register as native a vector bytecode_instruction method {}, skipping.", func_name.as_str()); - continue; - } - let modules = map.entry(addr).or_insert_with(HashMap::new); let funcs = modules .entry(module_name.into_string()) diff --git a/third_party/move/move-vm/types/src/values/values_impl.rs b/third_party/move/move-vm/types/src/values/values_impl.rs index ad3cead8dee187..8bbc9cd8ee70fa 100644 --- a/third_party/move/move-vm/types/src/values/values_impl.rs +++ b/third_party/move/move-vm/types/src/values/values_impl.rs @@ -2468,7 +2468,8 @@ impl VectorRef { /// In the `to` vector, elements after the `insert_position` are moved the the right to make space for new elements /// (i.e. range is inserted, while the order of the rest of the elements is kept). /// - /// Move prevents from having two mutable references to the same value, so `from` and `to` vectors are guaranted to be distinct. + /// Precondition for this function is that `from` and `to` vectors are required to be distinct + /// Move will guaranteee that invariant, because it prevents from having two mutable references to the same value. pub fn move_range( from_self: &Self, removal_position: usize, @@ -2482,6 +2483,8 @@ impl VectorRef { // potentially unnecessary as native call should've checked the types already // (unlike other vector functions that are bytecodes) + // TODO: potentially unnecessary, can be removed - as these are only required for + // bytecode instructions, as types are checked when native functions are called. check_elem_layout(type_param, from_c)?; check_elem_layout(type_param, to_c)?;