Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring interpreter and paranoid mode, introducing traits to allo… #15350

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions third_party/move/move-vm/runtime/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (c) The Move Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{interpreter::Interpreter, loader::Resolver, LoadedFunction};
use crate::{interpreter::InterpreterDebugInterface, loader::Resolver, LoadedFunction};
use move_binary_format::file_format::Bytecode;
use move_vm_types::values::{self, Locals};
use std::{
Expand Down Expand Up @@ -102,7 +102,7 @@ impl DebugContext {
pc: u16,
instr: &Bytecode,
resolver: &Resolver,
interp: &Interpreter,
interp: &dyn InterpreterDebugInterface,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: interp --> interpreter

) {
let instr_string = format!("{:?}", instr);
let function_string = function.name_as_pretty_string();
Expand Down
194 changes: 194 additions & 0 deletions third_party/move/move-vm/runtime/src/frame_type_cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

use crate::loader::Resolver;
use move_binary_format::{
errors::*,
file_format::{
FieldInstantiationIndex, SignatureIndex, StructDefInstantiationIndex,
StructVariantInstantiationIndex, VariantFieldInstantiationIndex,
},
};
use move_core_types::gas_algebra::NumTypeNodes;
use move_vm_types::loaded_data::runtime_types::Type;
use std::collections::BTreeMap;

#[derive(Default)]
pub(crate) struct FrameTypeCache {
struct_field_type_instantiation:
BTreeMap<StructDefInstantiationIndex, Vec<(Type, NumTypeNodes)>>,
struct_variant_field_type_instantiation:
BTreeMap<StructVariantInstantiationIndex, Vec<(Type, NumTypeNodes)>>,
struct_def_instantiation_type: BTreeMap<StructDefInstantiationIndex, (Type, NumTypeNodes)>,
struct_variant_instantiation_type:
BTreeMap<StructVariantInstantiationIndex, (Type, NumTypeNodes)>,
/// For a given field instantiation, the:
/// ((Type of the field, size of the field type) and (Type of its defining struct, size of its defining struct)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long, break at 100?

field_instantiation:
BTreeMap<FieldInstantiationIndex, ((Type, NumTypeNodes), (Type, NumTypeNodes))>,
/// Same as above, bot for variant field instantiations
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: bot --> but?

variant_field_instantiation:
BTreeMap<VariantFieldInstantiationIndex, ((Type, NumTypeNodes), (Type, NumTypeNodes))>,
single_sig_token_type: BTreeMap<SignatureIndex, (Type, NumTypeNodes)>,
}

impl FrameTypeCache {
#[inline(always)]
fn get_or<K: Copy + Ord + Eq, V, F>(
map: &mut BTreeMap<K, V>,
idx: K,
ty_func: F,
) -> PartialVMResult<&V>
where
F: FnOnce(K) -> PartialVMResult<V>,
{
match map.entry(idx) {
std::collections::btree_map::Entry::Occupied(entry) => Ok(entry.into_mut()),
std::collections::btree_map::Entry::Vacant(entry) => {
let v = ty_func(idx)?;
Ok(entry.insert(v))
},
}
}

#[inline(always)]
pub(crate) fn get_field_type_and_struct_type(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are just copy-paste, right?

&mut self,
idx: FieldInstantiationIndex,
resolver: &Resolver,
ty_args: &[Type],
) -> PartialVMResult<((&Type, NumTypeNodes), (&Type, NumTypeNodes))> {
let ((field_ty, field_ty_count), (struct_ty, struct_ty_count)) =
Self::get_or(&mut self.field_instantiation, idx, |idx| {
let struct_type = resolver.field_instantiation_to_struct(idx, ty_args)?;
let struct_ty_count = NumTypeNodes::new(struct_type.num_nodes() as u64);
let field_ty = resolver.get_generic_field_ty(idx, ty_args)?;
let field_ty_count = NumTypeNodes::new(field_ty.num_nodes() as u64);
Ok(((field_ty, field_ty_count), (struct_type, struct_ty_count)))
})?;
Ok(((field_ty, *field_ty_count), (struct_ty, *struct_ty_count)))
}

pub(crate) fn get_variant_field_type_and_struct_type(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, #[inline(always)]?😄

&mut self,
idx: VariantFieldInstantiationIndex,
resolver: &Resolver,
ty_args: &[Type],
) -> PartialVMResult<((&Type, NumTypeNodes), (&Type, NumTypeNodes))> {
let ((field_ty, field_ty_count), (struct_ty, struct_ty_count)) =
Self::get_or(&mut self.variant_field_instantiation, idx, |idx| {
let info = resolver.variant_field_instantiation_info_at(idx);
let struct_type = resolver.create_struct_instantiation_ty(
&info.definition_struct_type,
&info.instantiation,
ty_args,
)?;
let struct_ty_count = NumTypeNodes::new(struct_type.num_nodes() as u64);
let field_ty = resolver.instantiate_ty(
&info.uninstantiated_field_ty,
ty_args,
&info.instantiation,
)?;
let field_ty_count = NumTypeNodes::new(field_ty.num_nodes() as u64);
Ok(((field_ty, field_ty_count), (struct_type, struct_ty_count)))
})?;
Ok(((field_ty, *field_ty_count), (struct_ty, *struct_ty_count)))
}

#[inline(always)]
pub(crate) fn get_struct_type(
&mut self,
idx: StructDefInstantiationIndex,
resolver: &Resolver,
ty_args: &[Type],
) -> PartialVMResult<(&Type, NumTypeNodes)> {
let (ty, ty_count) = Self::get_or(&mut self.struct_def_instantiation_type, idx, |idx| {
let ty = resolver.get_generic_struct_ty(idx, ty_args)?;
let ty_count = NumTypeNodes::new(ty.num_nodes() as u64);
Ok((ty, ty_count))
})?;
Ok((ty, *ty_count))
}

#[inline(always)]
pub(crate) fn get_struct_variant_type(
&mut self,
idx: StructVariantInstantiationIndex,
resolver: &Resolver,
ty_args: &[Type],
) -> PartialVMResult<(&Type, NumTypeNodes)> {
let (ty, ty_count) =
Self::get_or(&mut self.struct_variant_instantiation_type, idx, |idx| {
let info = resolver.get_struct_variant_instantiation_at(idx);
let ty = resolver.create_struct_instantiation_ty(
&info.definition_struct_type,
&info.instantiation,
ty_args,
)?;
let ty_count = NumTypeNodes::new(ty.num_nodes() as u64);
Ok((ty, ty_count))
})?;
Ok((ty, *ty_count))
}

#[inline(always)]
pub(crate) fn get_struct_fields_types(
&mut self,
idx: StructDefInstantiationIndex,
resolver: &Resolver,
ty_args: &[Type],
) -> PartialVMResult<&[(Type, NumTypeNodes)]> {
Ok(Self::get_or(
&mut self.struct_field_type_instantiation,
idx,
|idx| {
Ok(resolver
.instantiate_generic_struct_fields(idx, ty_args)?
.into_iter()
.map(|ty| {
let num_nodes = NumTypeNodes::new(ty.num_nodes() as u64);
(ty, num_nodes)
})
.collect::<Vec<_>>())
},
)?)
}

#[inline(always)]
pub(crate) fn get_struct_variant_fields_types(
&mut self,
idx: StructVariantInstantiationIndex,
resolver: &Resolver,
ty_args: &[Type],
) -> PartialVMResult<&[(Type, NumTypeNodes)]> {
Ok(Self::get_or(
&mut self.struct_variant_field_type_instantiation,
idx,
|idx| {
Ok(resolver
.instantiate_generic_struct_variant_fields(idx, ty_args)?
.into_iter()
.map(|ty| {
let num_nodes = NumTypeNodes::new(ty.num_nodes() as u64);
(ty, num_nodes)
})
.collect::<Vec<_>>())
},
)?)
}

#[inline(always)]
pub(crate) fn get_signature_index_type(
&mut self,
idx: SignatureIndex,
resolver: &Resolver,
ty_args: &[Type],
) -> PartialVMResult<(&Type, NumTypeNodes)> {
let (ty, ty_count) = Self::get_or(&mut self.single_sig_token_type, idx, |idx| {
let ty = resolver.instantiate_single_type(idx, ty_args)?;
let ty_count = NumTypeNodes::new(ty.num_nodes() as u64);
Ok((ty, ty_count))
})?;
Ok((ty, *ty_count))
}
}
Loading
Loading