Skip to content

Commit eee4b44

Browse files
committed
[7/n][vm-rewrite][sui-execution] Update adapter
This is the big one... There is still some refactoring and changes need, but broad-strokes this plumbs in the new VM to the adapter. The main conceptual change is the idea of a `LinkedContext` which is an execution context with a specific linkage and VM instance associated with it. All commands currently require a `LinkedContext`. On the TODO list is: 1. To relax the need for a `LinkedContext` for `Upgrade` and `Publish` commands to require a context a priori (however there are some mechanics that will need to be sorted out to make this work). 2. Properly handle type versioning/defining IDs for typetags/type resolution. 3. General cleanup. NB: The code in the PR may not be working as future PRs will build on top of this.
1 parent 04ed3db commit eee4b44

File tree

17 files changed

+1885
-1415
lines changed

17 files changed

+1885
-1415
lines changed

sui-execution/latest/sui-adapter/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ move-bytecode-utils.workspace = true
2222
move-bytecode-verifier-meter.workspace = true
2323
move-core-types.workspace = true
2424
move-vm-config.workspace = true
25-
move-vm-types.workspace = true
2625
mysten-metrics.workspace = true
2726

2827
move-bytecode-verifier = { path = "../../../external-crates/move/crates/move-bytecode-verifier" }

sui-execution/latest/sui-adapter/src/adapter.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ pub use checked::*;
66
mod checked {
77
#[cfg(feature = "tracing")]
88
use move_vm_config::runtime::VMProfilerConfig;
9+
use move_vm_runtime::natives::extensions::{NativeContextExtensions, NativeContextMut};
10+
use move_vm_runtime::natives::functions::{NativeFunctionTable, NativeFunctions};
11+
use move_vm_runtime::runtime::MoveRuntime;
912
use std::path::PathBuf;
1013
use std::{collections::BTreeMap, sync::Arc};
1114

@@ -18,10 +21,6 @@ mod checked {
1821
runtime::{VMConfig, VMRuntimeLimitsConfig},
1922
verifier::VerifierConfig,
2023
};
21-
use move_vm_runtime::{
22-
move_vm::MoveVM, native_extensions::NativeContextExtensions,
23-
native_functions::NativeFunctionTable,
24-
};
2524
use sui_move_natives::object_runtime;
2625
use sui_types::metrics::BytecodeVerifierMetrics;
2726
use sui_verifier::check_for_verifier_timeout;
@@ -39,11 +38,11 @@ mod checked {
3938
};
4039
use sui_verifier::verifier::sui_verify_module_metered_check_timeout_only;
4140

42-
pub fn new_move_vm(
41+
pub fn new_move_runtime(
4342
natives: NativeFunctionTable,
4443
protocol_config: &ProtocolConfig,
4544
_enable_profiler: Option<PathBuf>,
46-
) -> Result<MoveVM, SuiError> {
45+
) -> Result<MoveRuntime, SuiError> {
4746
#[cfg(not(feature = "tracing"))]
4847
let vm_profiler_config = None;
4948
#[cfg(feature = "tracing")]
@@ -52,8 +51,10 @@ mod checked {
5251
track_bytecode_instructions: false,
5352
use_long_function_name: false,
5453
});
55-
MoveVM::new_with_config(
56-
natives,
54+
let native_functions =
55+
NativeFunctions::new(natives).map_err(|_| SuiError::ExecutionInvariantViolation)?;
56+
Ok(MoveRuntime::new(
57+
native_functions,
5758
VMConfig {
5859
verifier: protocol_config.verifier_config(/* signing_limits */ None),
5960
max_binary_format_version: protocol_config.move_binary_format_version(),
@@ -75,8 +76,7 @@ mod checked {
7576
max_type_to_layout_nodes: protocol_config.max_type_to_layout_nodes_as_option(),
7677
optimize_bytecode: false,
7778
},
78-
)
79-
.map_err(|_| SuiError::ExecutionInvariantViolation)
79+
))
8080
}
8181

8282
pub fn new_native_extensions<'r>(
@@ -88,14 +88,14 @@ mod checked {
8888
current_epoch_id: EpochId,
8989
) -> NativeContextExtensions<'r> {
9090
let mut extensions = NativeContextExtensions::default();
91-
extensions.add(ObjectRuntime::new(
91+
extensions.add(NativeContextMut::new(ObjectRuntime::new(
9292
child_resolver,
9393
input_objects,
9494
is_metered,
9595
protocol_config,
9696
metrics,
9797
current_epoch_id,
98-
));
98+
)));
9999
extensions.add(NativesCostTable::from_protocol_config(protocol_config));
100100
extensions
101101
}

sui-execution/latest/sui-adapter/src/error.rs

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,28 @@
44
use move_binary_format::{
55
errors::{Location, VMError},
66
file_format::FunctionDefinitionIndex,
7+
CompiledModule,
78
};
89
use move_core_types::{
9-
resolver::MoveResolver,
10+
language_storage::ModuleId,
11+
resolver::ModuleResolver,
1012
vm_status::{StatusCode, StatusType},
1113
};
12-
use move_vm_runtime::move_vm::MoveVM;
13-
use sui_types::error::{ExecutionError, SuiError};
14-
use sui_types::execution_status::{ExecutionFailureStatus, MoveLocation, MoveLocationOpt};
14+
use move_vm_runtime::shared::types::PackageStorageId;
15+
use sui_protocol_config::ProtocolConfig;
16+
use sui_types::{base_types::ObjectID, error::ExecutionError, Identifier};
17+
use sui_types::{
18+
execution_config_utils::to_binary_config,
19+
execution_status::{ExecutionFailureStatus, MoveLocation, MoveLocationOpt},
20+
};
21+
22+
use crate::linkage_resolution::ResolvedLinkage;
1523

16-
pub(crate) fn convert_vm_error<S: MoveResolver<Err = SuiError>>(
24+
pub(crate) fn convert_vm_error(
1725
error: VMError,
18-
vm: &MoveVM,
19-
state_view: &S,
20-
resolve_abort_location_to_package_id: bool,
26+
linkage: &ResolvedLinkage,
27+
state_view: &impl ModuleResolver,
28+
protocol_config: &ProtocolConfig,
2129
) -> ExecutionError {
2230
let kind = match (error.major_status(), error.sub_status(), error.location()) {
2331
(StatusCode::EXECUTED, _, _) => {
@@ -31,22 +39,30 @@ pub(crate) fn convert_vm_error<S: MoveResolver<Err = SuiError>>(
3139
ExecutionFailureStatus::VMInvariantViolation
3240
}
3341
(StatusCode::ABORTED, Some(code), Location::Module(id)) => {
34-
let abort_location_id = if resolve_abort_location_to_package_id {
35-
state_view.relocate(id).unwrap_or_else(|_| id.clone())
42+
let storage_id = linkage.get(&ObjectID::from(*id.address())).map(|a| **a);
43+
44+
let abort_location_id = if protocol_config.resolve_abort_locations_to_package_id() {
45+
storage_id.unwrap_or_else(|| *id.address())
3646
} else {
37-
id.clone()
47+
*id.address()
3848
};
49+
50+
let module_id = ModuleId::new(abort_location_id, id.name().to_owned());
3951
let offset = error.offsets().first().copied().map(|(f, i)| (f.0, i));
4052
debug_assert!(offset.is_some(), "Move should set the location on aborts");
4153
let (function, instruction) = offset.unwrap_or((0, 0));
42-
let function_name = vm.load_module(id, state_view).ok().map(|module| {
43-
let fdef = module.function_def_at(FunctionDefinitionIndex(function));
44-
let fhandle = module.function_handle_at(fdef.function);
45-
module.identifier_at(fhandle.name).to_string()
54+
let function_name = storage_id.and_then(|storage_id| {
55+
load_module_function_name(
56+
storage_id,
57+
id.name().to_owned(),
58+
FunctionDefinitionIndex(function),
59+
state_view,
60+
protocol_config,
61+
)
4662
});
4763
ExecutionFailureStatus::MoveAbort(
4864
MoveLocation {
49-
module: abort_location_id,
65+
module: module_id,
5066
function,
5167
instruction,
5268
function_name,
@@ -66,10 +82,16 @@ pub(crate) fn convert_vm_error<S: MoveResolver<Err = SuiError>>(
6682
"Move should set the location on all execution errors. Error {error}"
6783
);
6884
let (function, instruction) = offset.unwrap_or((0, 0));
69-
let function_name = vm.load_module(id, state_view).ok().map(|module| {
70-
let fdef = module.function_def_at(FunctionDefinitionIndex(function));
71-
let fhandle = module.function_handle_at(fdef.function);
72-
module.identifier_at(fhandle.name).to_string()
85+
let storage_id = linkage.get(&ObjectID::from(*id.address())).map(|a| **a);
86+
87+
let function_name = storage_id.and_then(|storage_id| {
88+
load_module_function_name(
89+
storage_id,
90+
id.name().to_owned(),
91+
FunctionDefinitionIndex(function),
92+
state_view,
93+
protocol_config,
94+
)
7395
});
7496
Some(MoveLocation {
7597
module: id.clone(),
@@ -91,3 +113,24 @@ pub(crate) fn convert_vm_error<S: MoveResolver<Err = SuiError>>(
91113
};
92114
ExecutionError::new_with_source(kind, error)
93115
}
116+
117+
fn load_module_function_name(
118+
package_storage_id: PackageStorageId,
119+
module_name: Identifier,
120+
function_index: FunctionDefinitionIndex,
121+
state_view: &impl ModuleResolver,
122+
protocol_config: &ProtocolConfig,
123+
) -> Option<String> {
124+
state_view
125+
.get_module(&ModuleId::new(package_storage_id, module_name))
126+
.ok()
127+
.flatten()
128+
.and_then(|m| {
129+
CompiledModule::deserialize_with_config(&m, &to_binary_config(protocol_config)).ok()
130+
})
131+
.map(|module| {
132+
let fdef = module.function_def_at(function_index);
133+
let fhandle = module.function_handle_at(fdef.function);
134+
module.identifier_at(fhandle.name).to_string()
135+
})
136+
}

sui-execution/latest/sui-adapter/src/execution_engine.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ pub use checked::*;
66
#[sui_macros::with_checked_arithmetic]
77
mod checked {
88

9+
use crate::adapter::new_move_runtime;
910
use crate::execution_mode::{self, ExecutionMode};
1011
use move_binary_format::CompiledModule;
11-
use move_vm_runtime::move_vm::MoveVM;
12+
use move_vm_runtime::runtime::MoveRuntime;
1213
use std::{collections::HashSet, sync::Arc};
1314
use sui_types::balance::{
1415
BALANCE_CREATE_REWARDS_FUNCTION_NAME, BALANCE_DESTROY_REBATES_FUNCTION_NAME,
@@ -26,7 +27,6 @@ mod checked {
2627
use sui_types::{BRIDGE_ADDRESS, SUI_BRIDGE_OBJECT_ID, SUI_RANDOMNESS_STATE_OBJECT_ID};
2728
use tracing::{info, instrument, trace, warn};
2829

29-
use crate::adapter::new_move_vm;
3030
use crate::programmable_transactions;
3131
use crate::type_layout_resolver::TypeLayoutResolver;
3232
use crate::{gas_charger::GasCharger, temporary_store::TemporaryStore};
@@ -85,7 +85,7 @@ mod checked {
8585
transaction_kind: TransactionKind,
8686
transaction_signer: SuiAddress,
8787
transaction_digest: TransactionDigest,
88-
move_vm: &Arc<MoveVM>,
88+
move_vm: &Arc<MoveRuntime>,
8989
epoch_id: &EpochId,
9090
epoch_timestamp_ms: u64,
9191
protocol_config: &ProtocolConfig,
@@ -237,7 +237,7 @@ mod checked {
237237
store: &dyn BackingStore,
238238
protocol_config: &ProtocolConfig,
239239
metrics: Arc<LimitsMetrics>,
240-
move_vm: &Arc<MoveVM>,
240+
move_vm: &Arc<MoveRuntime>,
241241
tx_context: &mut TxContext,
242242
input_objects: CheckedInputObjects,
243243
pt: ProgrammableTransaction,
@@ -271,7 +271,7 @@ mod checked {
271271
transaction_kind: TransactionKind,
272272
gas_charger: &mut GasCharger,
273273
tx_ctx: &mut TxContext,
274-
move_vm: &Arc<MoveVM>,
274+
move_vm: &Arc<MoveRuntime>,
275275
protocol_config: &ProtocolConfig,
276276
metrics: Arc<LimitsMetrics>,
277277
enable_expensive_checks: bool,
@@ -391,7 +391,7 @@ mod checked {
391391
temporary_store: &mut TemporaryStore<'_>,
392392
gas_charger: &mut GasCharger,
393393
tx_ctx: &mut TxContext,
394-
move_vm: &Arc<MoveVM>,
394+
move_vm: &Arc<MoveRuntime>,
395395
simple_conservation_checks: bool,
396396
enable_expensive_checks: bool,
397397
cost_summary: &GasCostSummary,
@@ -546,7 +546,7 @@ mod checked {
546546
temporary_store: &mut TemporaryStore<'_>,
547547
transaction_kind: TransactionKind,
548548
tx_ctx: &mut TxContext,
549-
move_vm: &Arc<MoveVM>,
549+
move_vm: &Arc<MoveRuntime>,
550550
gas_charger: &mut GasCharger,
551551
protocol_config: &ProtocolConfig,
552552
metrics: Arc<LimitsMetrics>,
@@ -856,7 +856,7 @@ mod checked {
856856
change_epoch: ChangeEpoch,
857857
temporary_store: &mut TemporaryStore<'_>,
858858
tx_ctx: &mut TxContext,
859-
move_vm: &Arc<MoveVM>,
859+
move_vm: &Arc<MoveRuntime>,
860860
gas_charger: &mut GasCharger,
861861
protocol_config: &ProtocolConfig,
862862
metrics: Arc<LimitsMetrics>,
@@ -916,12 +916,12 @@ mod checked {
916916
}
917917

918918
if protocol_config.fresh_vm_on_framework_upgrade() {
919-
let new_vm = new_move_vm(
919+
let new_vm = new_move_runtime(
920920
all_natives(/* silent */ true, protocol_config),
921921
protocol_config,
922922
/* enable_profiler */ None,
923923
)
924-
.expect("Failed to create new MoveVM");
924+
.expect("Failed to create new MoveRuntime");
925925
process_system_packages(
926926
change_epoch,
927927
temporary_store,
@@ -949,7 +949,7 @@ mod checked {
949949
change_epoch: ChangeEpoch,
950950
temporary_store: &mut TemporaryStore<'_>,
951951
tx_ctx: &mut TxContext,
952-
move_vm: &MoveVM,
952+
move_vm: &MoveRuntime,
953953
gas_charger: &mut GasCharger,
954954
protocol_config: &ProtocolConfig,
955955
metrics: Arc<LimitsMetrics>,
@@ -1016,7 +1016,7 @@ mod checked {
10161016
consensus_commit_timestamp_ms: CheckpointTimestamp,
10171017
temporary_store: &mut TemporaryStore<'_>,
10181018
tx_ctx: &mut TxContext,
1019-
move_vm: &Arc<MoveVM>,
1019+
move_vm: &Arc<MoveRuntime>,
10201020
gas_charger: &mut GasCharger,
10211021
protocol_config: &ProtocolConfig,
10221022
metrics: Arc<LimitsMetrics>,
@@ -1153,7 +1153,7 @@ mod checked {
11531153
update: AuthenticatorStateUpdate,
11541154
temporary_store: &mut TemporaryStore<'_>,
11551155
tx_ctx: &mut TxContext,
1156-
move_vm: &Arc<MoveVM>,
1156+
move_vm: &Arc<MoveRuntime>,
11571157
gas_charger: &mut GasCharger,
11581158
protocol_config: &ProtocolConfig,
11591159
metrics: Arc<LimitsMetrics>,
@@ -1218,7 +1218,7 @@ mod checked {
12181218
update: RandomnessStateUpdate,
12191219
temporary_store: &mut TemporaryStore<'_>,
12201220
tx_ctx: &mut TxContext,
1221-
move_vm: &Arc<MoveVM>,
1221+
move_vm: &Arc<MoveRuntime>,
12221222
gas_charger: &mut GasCharger,
12231223
protocol_config: &ProtocolConfig,
12241224
metrics: Arc<LimitsMetrics>,

0 commit comments

Comments
 (0)