Skip to content

Commit

Permalink
[7/n][vm-rewrite][sui-execution] Update adapter
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tzakian committed Feb 4, 2025
1 parent 04ed3db commit eee4b44
Show file tree
Hide file tree
Showing 17 changed files with 1,885 additions and 1,415 deletions.
1 change: 0 additions & 1 deletion sui-execution/latest/sui-adapter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ move-bytecode-utils.workspace = true
move-bytecode-verifier-meter.workspace = true
move-core-types.workspace = true
move-vm-config.workspace = true
move-vm-types.workspace = true
mysten-metrics.workspace = true

move-bytecode-verifier = { path = "../../../external-crates/move/crates/move-bytecode-verifier" }
Expand Down
24 changes: 12 additions & 12 deletions sui-execution/latest/sui-adapter/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ pub use checked::*;
mod checked {
#[cfg(feature = "tracing")]
use move_vm_config::runtime::VMProfilerConfig;
use move_vm_runtime::natives::extensions::{NativeContextExtensions, NativeContextMut};
use move_vm_runtime::natives::functions::{NativeFunctionTable, NativeFunctions};
use move_vm_runtime::runtime::MoveRuntime;
use std::path::PathBuf;
use std::{collections::BTreeMap, sync::Arc};

Expand All @@ -18,10 +21,6 @@ mod checked {
runtime::{VMConfig, VMRuntimeLimitsConfig},
verifier::VerifierConfig,
};
use move_vm_runtime::{
move_vm::MoveVM, native_extensions::NativeContextExtensions,
native_functions::NativeFunctionTable,
};
use sui_move_natives::object_runtime;
use sui_types::metrics::BytecodeVerifierMetrics;
use sui_verifier::check_for_verifier_timeout;
Expand All @@ -39,11 +38,11 @@ mod checked {
};
use sui_verifier::verifier::sui_verify_module_metered_check_timeout_only;

pub fn new_move_vm(
pub fn new_move_runtime(
natives: NativeFunctionTable,
protocol_config: &ProtocolConfig,
_enable_profiler: Option<PathBuf>,
) -> Result<MoveVM, SuiError> {
) -> Result<MoveRuntime, SuiError> {
#[cfg(not(feature = "tracing"))]
let vm_profiler_config = None;
#[cfg(feature = "tracing")]
Expand All @@ -52,8 +51,10 @@ mod checked {
track_bytecode_instructions: false,
use_long_function_name: false,
});
MoveVM::new_with_config(
natives,
let native_functions =
NativeFunctions::new(natives).map_err(|_| SuiError::ExecutionInvariantViolation)?;
Ok(MoveRuntime::new(
native_functions,
VMConfig {
verifier: protocol_config.verifier_config(/* signing_limits */ None),
max_binary_format_version: protocol_config.move_binary_format_version(),
Expand All @@ -75,8 +76,7 @@ mod checked {
max_type_to_layout_nodes: protocol_config.max_type_to_layout_nodes_as_option(),
optimize_bytecode: false,
},
)
.map_err(|_| SuiError::ExecutionInvariantViolation)
))
}

pub fn new_native_extensions<'r>(
Expand All @@ -88,14 +88,14 @@ mod checked {
current_epoch_id: EpochId,
) -> NativeContextExtensions<'r> {
let mut extensions = NativeContextExtensions::default();
extensions.add(ObjectRuntime::new(
extensions.add(NativeContextMut::new(ObjectRuntime::new(
child_resolver,
input_objects,
is_metered,
protocol_config,
metrics,
current_epoch_id,
));
)));
extensions.add(NativesCostTable::from_protocol_config(protocol_config));
extensions
}
Expand Down
83 changes: 63 additions & 20 deletions sui-execution/latest/sui-adapter/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@
use move_binary_format::{
errors::{Location, VMError},
file_format::FunctionDefinitionIndex,
CompiledModule,
};
use move_core_types::{
resolver::MoveResolver,
language_storage::ModuleId,
resolver::ModuleResolver,
vm_status::{StatusCode, StatusType},
};
use move_vm_runtime::move_vm::MoveVM;
use sui_types::error::{ExecutionError, SuiError};
use sui_types::execution_status::{ExecutionFailureStatus, MoveLocation, MoveLocationOpt};
use move_vm_runtime::shared::types::PackageStorageId;
use sui_protocol_config::ProtocolConfig;
use sui_types::{base_types::ObjectID, error::ExecutionError, Identifier};
use sui_types::{
execution_config_utils::to_binary_config,
execution_status::{ExecutionFailureStatus, MoveLocation, MoveLocationOpt},
};

use crate::linkage_resolution::ResolvedLinkage;

pub(crate) fn convert_vm_error<S: MoveResolver<Err = SuiError>>(
pub(crate) fn convert_vm_error(
error: VMError,
vm: &MoveVM,
state_view: &S,
resolve_abort_location_to_package_id: bool,
linkage: &ResolvedLinkage,
state_view: &impl ModuleResolver,
protocol_config: &ProtocolConfig,
) -> ExecutionError {
let kind = match (error.major_status(), error.sub_status(), error.location()) {
(StatusCode::EXECUTED, _, _) => {
Expand All @@ -31,22 +39,30 @@ pub(crate) fn convert_vm_error<S: MoveResolver<Err = SuiError>>(
ExecutionFailureStatus::VMInvariantViolation
}
(StatusCode::ABORTED, Some(code), Location::Module(id)) => {
let abort_location_id = if resolve_abort_location_to_package_id {
state_view.relocate(id).unwrap_or_else(|_| id.clone())
let storage_id = linkage.get(&ObjectID::from(*id.address())).map(|a| **a);

let abort_location_id = if protocol_config.resolve_abort_locations_to_package_id() {
storage_id.unwrap_or_else(|| *id.address())
} else {
id.clone()
*id.address()
};

let module_id = ModuleId::new(abort_location_id, id.name().to_owned());
let offset = error.offsets().first().copied().map(|(f, i)| (f.0, i));
debug_assert!(offset.is_some(), "Move should set the location on aborts");
let (function, instruction) = offset.unwrap_or((0, 0));
let function_name = vm.load_module(id, state_view).ok().map(|module| {
let fdef = module.function_def_at(FunctionDefinitionIndex(function));
let fhandle = module.function_handle_at(fdef.function);
module.identifier_at(fhandle.name).to_string()
let function_name = storage_id.and_then(|storage_id| {
load_module_function_name(
storage_id,
id.name().to_owned(),
FunctionDefinitionIndex(function),
state_view,
protocol_config,
)
});
ExecutionFailureStatus::MoveAbort(
MoveLocation {
module: abort_location_id,
module: module_id,
function,
instruction,
function_name,
Expand All @@ -66,10 +82,16 @@ pub(crate) fn convert_vm_error<S: MoveResolver<Err = SuiError>>(
"Move should set the location on all execution errors. Error {error}"
);
let (function, instruction) = offset.unwrap_or((0, 0));
let function_name = vm.load_module(id, state_view).ok().map(|module| {
let fdef = module.function_def_at(FunctionDefinitionIndex(function));
let fhandle = module.function_handle_at(fdef.function);
module.identifier_at(fhandle.name).to_string()
let storage_id = linkage.get(&ObjectID::from(*id.address())).map(|a| **a);

let function_name = storage_id.and_then(|storage_id| {
load_module_function_name(
storage_id,
id.name().to_owned(),
FunctionDefinitionIndex(function),
state_view,
protocol_config,
)
});
Some(MoveLocation {
module: id.clone(),
Expand All @@ -91,3 +113,24 @@ pub(crate) fn convert_vm_error<S: MoveResolver<Err = SuiError>>(
};
ExecutionError::new_with_source(kind, error)
}

fn load_module_function_name(
package_storage_id: PackageStorageId,
module_name: Identifier,
function_index: FunctionDefinitionIndex,
state_view: &impl ModuleResolver,
protocol_config: &ProtocolConfig,
) -> Option<String> {
state_view
.get_module(&ModuleId::new(package_storage_id, module_name))
.ok()
.flatten()
.and_then(|m| {
CompiledModule::deserialize_with_config(&m, &to_binary_config(protocol_config)).ok()
})
.map(|module| {
let fdef = module.function_def_at(function_index);
let fhandle = module.function_handle_at(fdef.function);
module.identifier_at(fhandle.name).to_string()
})
}
28 changes: 14 additions & 14 deletions sui-execution/latest/sui-adapter/src/execution_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ pub use checked::*;
#[sui_macros::with_checked_arithmetic]
mod checked {

use crate::adapter::new_move_runtime;
use crate::execution_mode::{self, ExecutionMode};
use move_binary_format::CompiledModule;
use move_vm_runtime::move_vm::MoveVM;
use move_vm_runtime::runtime::MoveRuntime;
use std::{collections::HashSet, sync::Arc};
use sui_types::balance::{
BALANCE_CREATE_REWARDS_FUNCTION_NAME, BALANCE_DESTROY_REBATES_FUNCTION_NAME,
Expand All @@ -26,7 +27,6 @@ mod checked {
use sui_types::{BRIDGE_ADDRESS, SUI_BRIDGE_OBJECT_ID, SUI_RANDOMNESS_STATE_OBJECT_ID};
use tracing::{info, instrument, trace, warn};

use crate::adapter::new_move_vm;
use crate::programmable_transactions;
use crate::type_layout_resolver::TypeLayoutResolver;
use crate::{gas_charger::GasCharger, temporary_store::TemporaryStore};
Expand Down Expand Up @@ -85,7 +85,7 @@ mod checked {
transaction_kind: TransactionKind,
transaction_signer: SuiAddress,
transaction_digest: TransactionDigest,
move_vm: &Arc<MoveVM>,
move_vm: &Arc<MoveRuntime>,
epoch_id: &EpochId,
epoch_timestamp_ms: u64,
protocol_config: &ProtocolConfig,
Expand Down Expand Up @@ -237,7 +237,7 @@ mod checked {
store: &dyn BackingStore,
protocol_config: &ProtocolConfig,
metrics: Arc<LimitsMetrics>,
move_vm: &Arc<MoveVM>,
move_vm: &Arc<MoveRuntime>,
tx_context: &mut TxContext,
input_objects: CheckedInputObjects,
pt: ProgrammableTransaction,
Expand Down Expand Up @@ -271,7 +271,7 @@ mod checked {
transaction_kind: TransactionKind,
gas_charger: &mut GasCharger,
tx_ctx: &mut TxContext,
move_vm: &Arc<MoveVM>,
move_vm: &Arc<MoveRuntime>,
protocol_config: &ProtocolConfig,
metrics: Arc<LimitsMetrics>,
enable_expensive_checks: bool,
Expand Down Expand Up @@ -391,7 +391,7 @@ mod checked {
temporary_store: &mut TemporaryStore<'_>,
gas_charger: &mut GasCharger,
tx_ctx: &mut TxContext,
move_vm: &Arc<MoveVM>,
move_vm: &Arc<MoveRuntime>,
simple_conservation_checks: bool,
enable_expensive_checks: bool,
cost_summary: &GasCostSummary,
Expand Down Expand Up @@ -546,7 +546,7 @@ mod checked {
temporary_store: &mut TemporaryStore<'_>,
transaction_kind: TransactionKind,
tx_ctx: &mut TxContext,
move_vm: &Arc<MoveVM>,
move_vm: &Arc<MoveRuntime>,
gas_charger: &mut GasCharger,
protocol_config: &ProtocolConfig,
metrics: Arc<LimitsMetrics>,
Expand Down Expand Up @@ -856,7 +856,7 @@ mod checked {
change_epoch: ChangeEpoch,
temporary_store: &mut TemporaryStore<'_>,
tx_ctx: &mut TxContext,
move_vm: &Arc<MoveVM>,
move_vm: &Arc<MoveRuntime>,
gas_charger: &mut GasCharger,
protocol_config: &ProtocolConfig,
metrics: Arc<LimitsMetrics>,
Expand Down Expand Up @@ -916,12 +916,12 @@ mod checked {
}

if protocol_config.fresh_vm_on_framework_upgrade() {
let new_vm = new_move_vm(
let new_vm = new_move_runtime(
all_natives(/* silent */ true, protocol_config),
protocol_config,
/* enable_profiler */ None,
)
.expect("Failed to create new MoveVM");
.expect("Failed to create new MoveRuntime");
process_system_packages(
change_epoch,
temporary_store,
Expand Down Expand Up @@ -949,7 +949,7 @@ mod checked {
change_epoch: ChangeEpoch,
temporary_store: &mut TemporaryStore<'_>,
tx_ctx: &mut TxContext,
move_vm: &MoveVM,
move_vm: &MoveRuntime,
gas_charger: &mut GasCharger,
protocol_config: &ProtocolConfig,
metrics: Arc<LimitsMetrics>,
Expand Down Expand Up @@ -1016,7 +1016,7 @@ mod checked {
consensus_commit_timestamp_ms: CheckpointTimestamp,
temporary_store: &mut TemporaryStore<'_>,
tx_ctx: &mut TxContext,
move_vm: &Arc<MoveVM>,
move_vm: &Arc<MoveRuntime>,
gas_charger: &mut GasCharger,
protocol_config: &ProtocolConfig,
metrics: Arc<LimitsMetrics>,
Expand Down Expand Up @@ -1153,7 +1153,7 @@ mod checked {
update: AuthenticatorStateUpdate,
temporary_store: &mut TemporaryStore<'_>,
tx_ctx: &mut TxContext,
move_vm: &Arc<MoveVM>,
move_vm: &Arc<MoveRuntime>,
gas_charger: &mut GasCharger,
protocol_config: &ProtocolConfig,
metrics: Arc<LimitsMetrics>,
Expand Down Expand Up @@ -1218,7 +1218,7 @@ mod checked {
update: RandomnessStateUpdate,
temporary_store: &mut TemporaryStore<'_>,
tx_ctx: &mut TxContext,
move_vm: &Arc<MoveVM>,
move_vm: &Arc<MoveRuntime>,
gas_charger: &mut GasCharger,
protocol_config: &ProtocolConfig,
metrics: Arc<LimitsMetrics>,
Expand Down
Loading

0 comments on commit eee4b44

Please sign in to comment.