From 30e632ff29b2e2dc2a101aa5fb3e7d9fb09cab2a Mon Sep 17 00:00:00 2001 From: Poytr1 Date: Sun, 1 Oct 2023 10:52:20 +0800 Subject: [PATCH] Return move value instead of value (#5) * Return move value instead of value * update tests --- ...ace_test__test_get_call_trace_by_hash.json | 90 +++++++++---------- api/src/transactions.rs | 2 +- aptos-move/aptos-vm/src/aptos_vm.rs | 8 +- .../move/move-vm/runtime/src/interpreter.rs | 65 +++++++++++++- 4 files changed, 112 insertions(+), 53 deletions(-) diff --git a/api/goldens/aptos_api__tests__call_trace_test__test_get_call_trace_by_hash.json b/api/goldens/aptos_api__tests__call_trace_test__test_get_call_trace_by_hash.json index 18a4d0ed4d168..4f4ca6b58c3bc 100644 --- a/api/goldens/aptos_api__tests__call_trace_test__test_get_call_trace_by_hash.json +++ b/api/goldens/aptos_api__tests__call_trace_test__test_get_call_trace_by_hash.json @@ -13,10 +13,10 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::account", "func_name": "create_account", "inputs": [ - "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" + "0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf" ], "outputs": [ - "(container 600003fbec88: [Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])" + "signer(0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" ], "type_args": [], "sub_traces": [ @@ -25,10 +25,10 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::account", "func_name": "create_account_unchecked", "inputs": [ - "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" + "0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf" ], "outputs": [ - "(container 600003fbec88: [Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])" + "signer(0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" ], "type_args": [], "sub_traces": [ @@ -37,11 +37,11 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::guid", "func_name": "create", "inputs": [ - "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)", - "(&container 600003fd1bd8)[3]" + "0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf", + "0u64" ], "outputs": [ - "(container 600003fd1b78: [(container 600003fd1848: [U64(0), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + "struct[struct[0u64, 0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf]]" ], "type_args": [], "sub_traces": [] @@ -51,10 +51,10 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::event", "func_name": "new_event_handle", "inputs": [ - "(container 600003fd1a28: [(container 600003fd1ab8: [U64(0), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + "struct[struct[0u64, 0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf]]" ], "outputs": [ - "(container 600003fd1f68: [U64(0), (container 600003fd18d8: [(container 600003fd1848: [U64(0), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])])" + "struct[0u64, struct[struct[0u64, 0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf]]]" ], "type_args": [], "sub_traces": [] @@ -64,11 +64,11 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::guid", "func_name": "create", "inputs": [ - "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)", - "(&container 600003fd1bd8)[3]" + "0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf", + "1u64" ], "outputs": [ - "(container 600003fd1e48: [(container 600003fd1ea8: [U64(1), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + "struct[struct[1u64, 0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf]]" ], "type_args": [], "sub_traces": [] @@ -78,10 +78,10 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::event", "func_name": "new_event_handle", "inputs": [ - "(container 600003fd18d8: [(container 600003fd1ab8: [U64(1), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + "struct[struct[1u64, 0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf]]" ], "outputs": [ - "(container 600003fd1d88: [U64(0), (container 600003fd1de8: [(container 600003fd1ea8: [U64(1), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])])" + "struct[0u64, struct[struct[1u64, 0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf]]]" ], "type_args": [], "sub_traces": [] @@ -92,7 +92,7 @@ "func_name": "none", "inputs": [], "outputs": [ - "(container 600003fbf978: [(container 600003fbf6d8: [])])" + "" ], "type_args": [], "sub_traces": [] @@ -103,7 +103,7 @@ "func_name": "none", "inputs": [], "outputs": [ - "(container 600003fbc978: [(container 600003fbc948: [])])" + "" ], "type_args": [], "sub_traces": [] @@ -117,7 +117,7 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::coin", "func_name": "register", "inputs": [ - "(&container 600003fd1f98)" + "signer(0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" ], "outputs": [], "type_args": [], @@ -127,10 +127,10 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::signer", "func_name": "address_of", "inputs": [ - "(&container 600003fd1f98)" + "signer(0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" ], "outputs": [ - "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" + "0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf" ], "type_args": [], "sub_traces": [] @@ -140,7 +140,7 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::coin", "func_name": "is_account_registered", "inputs": [ - "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" + "0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf" ], "outputs": [ "false" @@ -153,7 +153,7 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::account", "func_name": "register_coin", "inputs": [ - "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" + "0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf" ], "outputs": [], "type_args": [], @@ -163,8 +163,8 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::event", "func_name": "emit_event", "inputs": [ - "(&container 600003fd1b78)", - "(container 600003fbfb88: [(container 600003fbf588: [Address(1), (container 600003fbee38: [97, 112, 116, 111, 115, 95, 99, 111, 105, 110]), (container 600003fbe8f8: [65, 112, 116, 111, 115, 67, 111, 105, 110])])])" + "struct[0u64, struct[struct[0u64, 0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf]]]", + "" ], "outputs": [], "type_args": [], @@ -177,10 +177,10 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::account", "func_name": "new_event_handle", "inputs": [ - "(&container 600003fd1f98)" + "signer(0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" ], "outputs": [ - "(container 600003fbe808: [U64(0), (container 600003fbeb68: [(container 600003fbeb38: [U64(2), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])])" + "struct[0u64, struct[struct[2u64, 0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf]]]" ], "type_args": [], "sub_traces": [ @@ -189,10 +189,10 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::account", "func_name": "create_guid", "inputs": [ - "(&container 600003fd1f98)" + "signer(0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" ], "outputs": [ - "(container 600003fbea48: [(container 600003fbee38: [U64(2), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + "struct[struct[2u64, 0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf]]" ], "type_args": [], "sub_traces": [ @@ -201,10 +201,10 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::signer", "func_name": "address_of", "inputs": [ - "(&container 600003fd1f98)" + "signer(0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" ], "outputs": [ - "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" + "0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf" ], "type_args": [], "sub_traces": [] @@ -214,11 +214,11 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::guid", "func_name": "create", "inputs": [ - "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)", - "(&container 600003fbc948)[2]" + "0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf", + "2u64" ], "outputs": [ - "(container 600003fbe808: [(container 600003fbeef8: [U64(2), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + "struct[struct[2u64, 0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf]]" ], "type_args": [], "sub_traces": [] @@ -230,10 +230,10 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::event", "func_name": "new_event_handle", "inputs": [ - "(container 600003fbeb68: [(container 600003fbeb38: [U64(2), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + "struct[struct[2u64, 0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf]]" ], "outputs": [ - "(container 600003fbe808: [U64(0), (container 600003fbee38: [(container 600003fbea48: [U64(2), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])])" + "struct[0u64, struct[struct[2u64, 0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf]]]" ], "type_args": [], "sub_traces": [] @@ -245,10 +245,10 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::account", "func_name": "new_event_handle", "inputs": [ - "(&container 600003fd1f98)" + "signer(0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" ], "outputs": [ - "(container 600003fbec28: [U64(0), (container 600003fbeb68: [(container 600003fbea48: [U64(3), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])])" + "struct[0u64, struct[struct[3u64, 0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf]]]" ], "type_args": [], "sub_traces": [ @@ -257,10 +257,10 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::account", "func_name": "create_guid", "inputs": [ - "(&container 600003fd1f98)" + "signer(0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" ], "outputs": [ - "(container 600003fbeef8: [(container 600003fbe508: [U64(3), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + "struct[struct[3u64, 0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf]]" ], "type_args": [], "sub_traces": [ @@ -269,10 +269,10 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::signer", "func_name": "address_of", "inputs": [ - "(&container 600003fd1f98)" + "signer(0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" ], "outputs": [ - "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)" + "0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf" ], "type_args": [], "sub_traces": [] @@ -282,11 +282,11 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::guid", "func_name": "create", "inputs": [ - "Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)", - "(&container 600003fbc948)[2]" + "0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf", + "3u64" ], "outputs": [ - "(container 600003fbec28: [(container 600003fbeb98: [U64(3), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + "struct[struct[3u64, 0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf]]" ], "type_args": [], "sub_traces": [] @@ -298,10 +298,10 @@ "module_id": "0000000000000000000000000000000000000000000000000000000000000001::event", "func_name": "new_event_handle", "inputs": [ - "(container 600003fbeb68: [(container 600003fbea48: [U64(3), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])" + "struct[struct[3u64, 0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf]]" ], "outputs": [ - "(container 600003fbec28: [U64(0), (container 600003fbe508: [(container 600003fbeef8: [U64(3), Address(34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf)])])])" + "struct[0u64, struct[struct[3u64, 0x34bf7e2d17674feb234371a7ea58efd715f0e56ba20ebf13789480d9d643afaf]]]" ], "type_args": [], "sub_traces": [] diff --git a/api/src/transactions.rs b/api/src/transactions.rs index 8dd60091e1a81..b8315a703f82d 100644 --- a/api/src/transactions.rs +++ b/api/src/transactions.rs @@ -303,7 +303,7 @@ impl TransactionsApi { ) })?; AptosVM::get_call_trace( - &state_view, + &state_view.as_executor_view(), entry_func.module().clone(), entry_func.function().to_owned(), entry_func.ty_args().to_owned(), diff --git a/aptos-move/aptos-vm/src/aptos_vm.rs b/aptos-move/aptos-vm/src/aptos_vm.rs index 62dd502657d37..c5910407a42b6 100644 --- a/aptos-move/aptos-vm/src/aptos_vm.rs +++ b/aptos-move/aptos-vm/src/aptos_vm.rs @@ -1413,15 +1413,15 @@ impl AptosVM { } pub fn get_call_trace( - state_view: &impl StateView, + executor_view: &impl ExecutorView, module_id: ModuleId, func_name: Identifier, type_args: Vec, arguments: Vec>, gas_budget: u64, ) -> Result { - let vm = AptosVM::new_from_state_view(state_view); - let log_context = AdapterLogSchema::new(state_view.id(), 0); + let vm = AptosVM::new_from_executor_view(executor_view); + let log_context = AdapterLogSchema::new(executor_view.id(), 0); let mut gas_meter = MemoryTrackedGasMeter::new(StandardGasMeter::new(StandardGasAlgebra::new( vm.0.get_gas_feature_version(), @@ -1429,7 +1429,7 @@ impl AptosVM { vm.0.get_storage_gas_parameters(&log_context)?.clone(), gas_budget, ))); - let resolver = vm.as_move_resolver(state_view); + let resolver = vm.as_move_resolver(executor_view); let mut session = vm.new_session(&resolver, SessionId::Void); let call_trace_res = session.call_trace(&module_id, &func_name, type_args, arguments, &mut gas_meter); match call_trace_res { diff --git a/third_party/move/move-vm/runtime/src/interpreter.rs b/third_party/move/move-vm/runtime/src/interpreter.rs index 9dc8610d31866..fa5cdc38cdc8a 100644 --- a/third_party/move/move-vm/runtime/src/interpreter.rs +++ b/third_party/move/move-vm/runtime/src/interpreter.rs @@ -26,6 +26,7 @@ use move_vm_types::{ }; use std::{cmp::min, collections::VecDeque, fmt::Write, sync::Arc}; use move_core_types::call_trace::{InternalCallTrace, CallTraces}; +use move_core_types::value::MoveTypeLayout; macro_rules! debug_write { ($($toks: tt)*) => { @@ -348,7 +349,27 @@ impl Interpreter { for val in self.operand_stack.last_n(current_frame.function.return_type_count()).unwrap() { outputs.push((*val).copy_value().unwrap()); } - call_traces.set_outputs(outputs.into_iter().enumerate().map(|(_, o)| {o.to_string()}).collect()); + call_traces.set_outputs( + outputs.into_iter().zip(current_frame.function.return_types()).map(|(value, ty)| { + let (ty, value) = match ty { + Type::Reference(inner) | Type::MutableReference(inner) => { + let ref_value: Reference = value.cast().map_err(|_err| { + PartialVMError::new(StatusCode::INTERNAL_TYPE_ERROR).with_message( + "non reference value given for a reference typed return value".to_string(), + ) + })?; + let inner_value = ref_value.read_ref()?; + (&**inner, inner_value) + }, + _ => (ty, value), + }; + let layout = loader.type_to_type_layout(ty).map_err(|_err| { + PartialVMError::new(StatusCode::VERIFICATION_ERROR).with_message( + "entry point functions cannot have non-serializable return types".to_string(), + ) + })?; + Ok(value.as_move_value(&layout).to_string()) + }).map(|v: Result| v.unwrap_or("".to_string())).collect()); if let Some(frame) = self.call_stack.pop() { // Note: the caller will find the callee's return values at the top of the shared operand stack @@ -407,7 +428,26 @@ impl Interpreter { pc: 0, module_id: module_id.to_string(), func_name: func.name().to_string(), - inputs: inputs.into_iter().enumerate().map(|(_, i)| i.to_string()).collect(), + inputs: inputs.into_iter().zip(func.parameter_types()).map(|(value, ty)| { + let (ty, value) = match ty { + Type::Reference(inner) | Type::MutableReference(inner) => { + let ref_value: Reference = value.cast().map_err(|_err| { + PartialVMError::new(StatusCode::INTERNAL_TYPE_ERROR).with_message( + "non reference value given for a reference typed return value".to_string(), + ) + })?; + let inner_value = ref_value.read_ref()?; + (&**inner, inner_value) + }, + _ => (ty, value), + }; + let layout = loader.type_to_type_layout(ty).map_err(|_err| { + PartialVMError::new(StatusCode::VERIFICATION_ERROR).with_message( + "entry point functions cannot have non-serializable return types".to_string(), + ) + })?; + Ok(value.as_move_value(&layout).to_string()) + }).map(|v: Result| v.unwrap_or("".to_string())).collect(), outputs: vec![], type_args: vec![], sub_traces: vec![], @@ -474,7 +514,26 @@ impl Interpreter { pc: 0, module_id: module_id.to_string(), func_name: func.name().to_string(), - inputs: inputs.into_iter().enumerate().map(|(_, i)| i.to_string()).collect(), + inputs: inputs.into_iter().zip(func.parameter_types()).map(|(value, ty)| { + let (ty, value) = match ty { + Type::Reference(inner) | Type::MutableReference(inner) => { + let ref_value: Reference = value.cast().map_err(|_err| { + PartialVMError::new(StatusCode::INTERNAL_TYPE_ERROR).with_message( + "non reference value given for a reference typed return value".to_string(), + ) + })?; + let inner_value = ref_value.read_ref()?; + (&**inner, inner_value) + }, + _ => (ty, value), + }; + let layout = loader.type_to_type_layout(ty).map_err(|_err| { + PartialVMError::new(StatusCode::VERIFICATION_ERROR).with_message( + "entry point functions cannot have non-serializable return types".to_string(), + ) + })?; + Ok(value.as_move_value(&layout).to_string()) + }).map(|v: Result| v.unwrap_or("".to_string())).collect(), outputs: vec![], type_args: vec![], sub_traces: vec![],