Skip to content

Commit

Permalink
fixup! [7/n][vm-rewrite][sui-execution] Update adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
tzakian committed Feb 11, 2025
1 parent c6641eb commit 58f0e04
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 96 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ impl<'extensions> MoveVM<'extensions> {
}
}

/// Return the linkage context of the VM.
pub fn linkage_context(&self) -> &LinkageContext {
&self.link_context
}

// -------------------------------------------
// Execution Operations
// -------------------------------------------
Expand Down
12 changes: 9 additions & 3 deletions sui-execution/latest/sui-adapter/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::linkage_resolution::ResolvedLinkage;

pub(crate) fn convert_vm_error(
error: VMError,
linkage: &ResolvedLinkage,
resolution_linkage: &ResolvedLinkage,
state_view: &impl ModuleResolver,
protocol_config: &ProtocolConfig,
) -> ExecutionError {
Expand All @@ -39,7 +39,10 @@ pub(crate) fn convert_vm_error(
ExecutionFailureStatus::VMInvariantViolation
}
(StatusCode::ABORTED, Some(code), Location::Module(id)) => {
let storage_id = linkage.get(&ObjectID::from(*id.address())).map(|a| **a);
let storage_id = resolution_linkage
.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())
Expand Down Expand Up @@ -82,7 +85,10 @@ pub(crate) fn convert_vm_error(
"Move should set the location on all execution errors. Error {error}"
);
let (function, instruction) = offset.unwrap_or((0, 0));
let storage_id = linkage.get(&ObjectID::from(*id.address())).map(|a| **a);
let storage_id = resolution_linkage
.linkage
.get(&ObjectID::from(*id.address()))
.map(|a| **a);

let function_name = storage_id.and_then(|storage_id| {
load_module_function_name(
Expand Down
141 changes: 108 additions & 33 deletions sui-execution/latest/sui-adapter/src/linkage_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct LinkageConfig {
}

/// Unifiers. These are used to determine how to unify two packages.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum ConflictResolution {
/// An exact constraint unifies as follows:
/// 1. Exact(a) ~ Exact(b) ==> Exact(a), iff a == b
Expand All @@ -75,7 +75,12 @@ pub enum ConflictResolution {
AtLeast(SequenceNumber, ObjectID),
}

pub type ResolvedLinkage = BTreeMap<ObjectID, ObjectID>;
#[derive(Debug)]
pub struct ResolvedLinkage {
pub linkage: BTreeMap<ObjectID, ObjectID>,
pub reverse_linkage: BTreeMap<ObjectID, ObjectID>,
pub versions: BTreeMap<ObjectID, SequenceNumber>,
}

#[derive(Debug)]
pub struct PerCommandLinkage {
Expand All @@ -99,23 +104,27 @@ pub trait LinkageAnalysis {
) -> Result<ResolvedLinkage, ExecutionError>;

// Generate a linkage for the type tag
fn generate_type_linkage(
fn type_linkage(
&mut self,
ids: &[ObjectID],
store: &dyn PackageStore,
) -> Result<ResolvedLinkage, ExecutionError>;

fn publication_linkage(
&mut self,
linkage: &LinkageContext,
store: &dyn PackageStore,
) -> Result<ResolvedLinkage, ExecutionError>;
}

type ResolutionTable = BTreeMap<ObjectID, ConflictResolution>;

pub fn linkage_analysis_for_protocol_config(
protocol_config: &ProtocolConfig,
) -> Box<dyn LinkageAnalysis> {
Box::new(PerCommandLinkage::new(to_binary_config(protocol_config)))
}

pub fn into_linkage_context(linkage: ResolvedLinkage) -> LinkageContext {
LinkageContext::new(linkage.into_iter().map(|(k, v)| (*k, *v)).collect())
}

impl LinkageAnalysis for PerCommandLinkage {
fn add_command(
&mut self,
Expand All @@ -125,13 +134,21 @@ impl LinkageAnalysis for PerCommandLinkage {
self.add_command(command, store)
}

fn generate_type_linkage(
fn type_linkage(
&mut self,
ids: &[ObjectID],
store: &dyn PackageStore,
) -> Result<ResolvedLinkage, ExecutionError> {
self.internal.generate_type_tag_linkage(ids, store)
}

fn publication_linkage(
&mut self,
linkage: &LinkageContext,
store: &dyn PackageStore,
) -> Result<ResolvedLinkage, ExecutionError> {
self.internal.publication_linkage(linkage, store)
}
}

impl LinkageAnalysis for UnifiedLinkage {
Expand All @@ -143,13 +160,21 @@ impl LinkageAnalysis for UnifiedLinkage {
self.add_command(command, store)
}

fn generate_type_linkage(
fn type_linkage(
&mut self,
ids: &[ObjectID],
store: &dyn PackageStore,
) -> Result<ResolvedLinkage, ExecutionError> {
self.internal.generate_type_tag_linkage(ids, store)
}

fn publication_linkage(
&mut self,
linkage: &LinkageContext,
store: &dyn PackageStore,
) -> Result<ResolvedLinkage, ExecutionError> {
self.internal.publication_linkage(linkage, store)
}
}

impl LinkageConfig {
Expand Down Expand Up @@ -210,6 +235,33 @@ impl LinkageConfig {
}
}

impl ResolvedLinkage {
pub fn from_resolution_table(resolution_table: ResolutionTable) -> Self {
let mut linkage = BTreeMap::new();
let mut reverse_linkage = BTreeMap::new();
let mut versions = BTreeMap::new();
for (runtime_id, resolution) in resolution_table {
match resolution {
ConflictResolution::Exact(version, object_id)
| ConflictResolution::AtLeast(version, object_id) => {
linkage.insert(runtime_id, object_id);
reverse_linkage.insert(object_id, runtime_id);
versions.insert(runtime_id, version);
}
}
}
Self {
linkage,
reverse_linkage,
versions,
}
}

pub fn linkage_context(&self) -> LinkageContext {
LinkageContext::new(self.linkage.iter().map(|(k, v)| (**k, **v)).collect())
}
}

impl ConflictResolution {
pub fn exact<'a>(pkg: &MovePackage) -> ConflictResolution {
ConflictResolution::Exact(pkg.version(), pkg.id())
Expand Down Expand Up @@ -301,13 +353,17 @@ impl PerCommandLinkage {
store: &dyn PackageStore,
) -> Result<ResolvedLinkage, ExecutionError> {
let mut unification_table = BTreeMap::new();
self.internal
.add_command(command, store, &mut unification_table)
Ok(ResolvedLinkage::from_resolution_table(
self.internal
.add_command(command, store, &mut unification_table)?,
))
}
}

impl UnifiedLinkage {
pub fn new(binary_config: BinaryConfig) -> Self {
pub fn new(
binary_config: BinaryConfig,
) -> Self {
Self {
internal: PTBLinkageMetadata {
all_packages: BTreeMap::new(),
Expand All @@ -323,8 +379,10 @@ impl UnifiedLinkage {
command: &Command,
store: &dyn PackageStore,
) -> Result<ResolvedLinkage, ExecutionError> {
self.internal
.add_command(command, store, &mut self.unification_table)
Ok(ResolvedLinkage::from_resolution_table(
self.internal
.add_command(command, store, &mut self.unification_table)?,
))
}
}

Expand Down Expand Up @@ -359,13 +417,33 @@ impl PTBLinkageMetadata {
}
}

Ok(unification_table
.iter()
.map(|(k, v)| match v {
ConflictResolution::Exact(_, object_id)
| ConflictResolution::AtLeast(_, object_id) => (*k, *object_id),
})
.collect())
Ok(ResolvedLinkage::from_resolution_table(unification_table))
}

pub fn publication_linkage(
&mut self,
linkage: &LinkageContext,
store: &dyn PackageStore,
) -> Result<ResolvedLinkage, ExecutionError> {
let mut unification_table = BTreeMap::new();
for (runtime_id, package_id) in linkage.linkage_table.iter() {
let package = PTBLinkageMetadata::get_package(
&mut self.all_packages,
&ObjectID::from(*package_id),
store,
)?;

assert_eq!(*package.id(), *package_id);
assert_eq!(*package.original_package_id(), *runtime_id);

self.add_and_unify(
&ObjectID::from(*runtime_id),
store,
&mut unification_table,
ConflictResolution::exact,
)?;
}
Ok(ResolvedLinkage::from_resolution_table(unification_table))
}
}

Expand All @@ -382,8 +460,8 @@ impl PTBLinkageMetadata {
&mut self,
command: &Command,
store: &dyn PackageStore,
unification_table: &mut BTreeMap<ObjectID, ConflictResolution>,
) -> Result<ResolvedLinkage, ExecutionError> {
unification_table: &mut ResolutionTable,
) -> Result<ResolutionTable, ExecutionError> {
match command {
Command::MoveCall(programmable_move_call) => {
let pkg = Self::get_package(
Expand Down Expand Up @@ -475,7 +553,10 @@ impl PTBLinkageMetadata {
.into_iter()
.map(|id| {
let pkg = Self::get_package(&mut self.all_packages, id, store)?;
Ok((pkg.original_package_id(), pkg.id()))
Ok((
pkg.original_package_id(),
ConflictResolution::Exact(pkg.version(), pkg.id()),
))
})
.collect();
}
Expand All @@ -484,20 +565,14 @@ impl PTBLinkageMetadata {
| Command::MergeCoins(_, _) => (),
};

Ok(unification_table
.iter()
.map(|(k, v)| match v {
ConflictResolution::Exact(_, object_id)
| ConflictResolution::AtLeast(_, object_id) => (*k, *object_id),
})
.collect())
Ok(unification_table.clone())
}

fn add_type_input(
&mut self,
ty: &TypeInput,
store: &dyn PackageStore,
unification_table: &mut BTreeMap<ObjectID, ConflictResolution>,
unification_table: &mut ResolutionTable,
) -> Result<(), ExecutionError> {
let mut stack = vec![ty];
while let Some(ty) = stack.pop() {
Expand Down Expand Up @@ -582,7 +657,7 @@ impl PTBLinkageMetadata {
&mut self,
object_id: &ObjectID,
store: &dyn PackageStore,
unification_table: &mut BTreeMap<ObjectID, ConflictResolution>,
unification_table: &mut ResolutionTable,
resolution_fn: fn(&MovePackage) -> ConflictResolution,
) -> Result<(), ExecutionError> {
let package = Self::get_package(&mut self.all_packages, object_id, store)?;
Expand Down
Loading

0 comments on commit 58f0e04

Please sign in to comment.