Skip to content

Commit

Permalink
merge the v2 instuctions and binary format
Browse files Browse the repository at this point in the history
  • Loading branch information
steelgeek091 committed Nov 29, 2024
1 parent 2f612b1 commit 3fa761c
Show file tree
Hide file tree
Showing 10 changed files with 545 additions and 10 deletions.
27 changes: 27 additions & 0 deletions third_party/move/language/move-binary-format/src/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ pub trait ModuleAccess: Sync {
handle
}

fn variant_field_handle_at(&self, idx: VariantFieldHandleIndex) -> &VariantFieldHandle {
let handle = &self.as_module().variant_field_handles[idx.into_index()];
debug_assert!(handle.struct_index.into_index() < self.as_module().struct_defs.len()); // invariant

handle
}

fn struct_variant_handle_at(&self, idx: StructVariantHandleIndex) -> &StructVariantHandle {
let handle = &self.as_module().struct_variant_handles[idx.into_index()];
debug_assert!(handle.struct_index.into_index() < self.as_module().struct_defs.len()); // invariant
handle
}

fn struct_instantiation_at(&self, idx: StructDefInstantiationIndex) -> &StructDefInstantiation {
&self.as_module().struct_def_instantiations[idx.into_index()]
}
Expand All @@ -78,6 +91,20 @@ pub trait ModuleAccess: Sync {
&self.as_module().field_instantiations[idx.into_index()]
}

fn variant_field_instantiation_at(
&self,
idx: VariantFieldInstantiationIndex,
) -> &VariantFieldInstantiation {
&self.as_module().variant_field_instantiations[idx.into_index()]
}

fn struct_variant_instantiation_at(
&self,
idx: StructVariantInstantiationIndex,
) -> &StructVariantInstantiation {
&self.as_module().struct_variant_instantiations[idx.into_index()]
}

fn signature_at(&self, idx: SignatureIndex) -> &Signature {
&self.as_module().signatures[idx.into_index()]
}
Expand Down
81 changes: 81 additions & 0 deletions third_party/move/language/move-binary-format/src/binary_views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
// Copyright (c) The Move Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::file_format::{
StructVariantHandle, StructVariantHandleIndex, StructVariantInstantiation,
StructVariantInstantiationIndex, VariantFieldHandle, VariantFieldHandleIndex,
VariantFieldInstantiation, VariantFieldInstantiationIndex,
};
use crate::{
access::{ModuleAccess, ScriptAccess},
control_flow_graph::VMControlFlowGraph,
Expand Down Expand Up @@ -157,6 +162,13 @@ impl<'a> BinaryIndexedView<'a> {
}
}

pub fn variant_field_handles(&self) -> Option<&[VariantFieldHandle]> {
match self {
BinaryIndexedView::Module(module) => Some(&module.variant_field_handles),
BinaryIndexedView::Script(_) => None,
}
}

pub fn field_handle_at(&self, idx: FieldHandleIndex) -> PartialVMResult<&FieldHandle> {
match self {
BinaryIndexedView::Module(module) => Ok(module.field_handle_at(idx)),
Expand Down Expand Up @@ -199,6 +211,13 @@ impl<'a> BinaryIndexedView<'a> {
}
}

pub fn variant_field_instantiations(&self) -> Option<&[VariantFieldInstantiation]> {
match self {
BinaryIndexedView::Module(module) => Some(&module.variant_field_instantiations),
BinaryIndexedView::Script(_) => None,
}
}

pub fn field_instantiation_at(
&self,
idx: FieldInstantiationIndex,
Expand All @@ -211,6 +230,54 @@ impl<'a> BinaryIndexedView<'a> {
}
}

pub fn variant_field_handle_at(
&self,
idx: VariantFieldHandleIndex,
) -> PartialVMResult<&VariantFieldHandle> {
match self {
BinaryIndexedView::Module(module) => Ok(module.variant_field_handle_at(idx)),
BinaryIndexedView::Script(_) => {
Err(PartialVMError::new(StatusCode::INVALID_OPERATION_IN_SCRIPT))
}
}
}

pub fn variant_field_instantiation_at(
&self,
idx: VariantFieldInstantiationIndex,
) -> PartialVMResult<&VariantFieldInstantiation> {
match self {
BinaryIndexedView::Module(module) => Ok(module.variant_field_instantiation_at(idx)),
BinaryIndexedView::Script(_) => {
Err(PartialVMError::new(StatusCode::INVALID_OPERATION_IN_SCRIPT))
}
}
}

pub fn struct_variant_instantiation_at(
&self,
idx: StructVariantInstantiationIndex,
) -> PartialVMResult<&StructVariantInstantiation> {
match self {
BinaryIndexedView::Module(module) => Ok(module.struct_variant_instantiation_at(idx)),
BinaryIndexedView::Script(_) => {
Err(PartialVMError::new(StatusCode::INVALID_OPERATION_IN_SCRIPT))
}
}
}

pub fn struct_variant_handle_at(
&self,
idx: StructVariantHandleIndex,
) -> PartialVMResult<&StructVariantHandle> {
match self {
BinaryIndexedView::Module(module) => Ok(module.struct_variant_handle_at(idx)),
BinaryIndexedView::Script(_) => {
Err(PartialVMError::new(StatusCode::INVALID_OPERATION_IN_SCRIPT))
}
}
}

pub fn struct_defs(&self) -> Option<&[StructDefinition]> {
match self {
BinaryIndexedView::Module(module) => Some(module.struct_defs()),
Expand All @@ -227,6 +294,20 @@ impl<'a> BinaryIndexedView<'a> {
}
}

pub fn struct_variant_handles(&self) -> Option<&[StructVariantHandle]> {
match self {
BinaryIndexedView::Module(module) => Some(&module.struct_variant_handles),
BinaryIndexedView::Script(_) => None,
}
}

pub fn struct_variant_instantiations(&self) -> Option<&[StructVariantInstantiation]> {
match self {
BinaryIndexedView::Module(module) => Some(&module.struct_variant_instantiations),
BinaryIndexedView::Script(_) => None,
}
}

pub fn function_defs(&self) -> Option<&[FunctionDefinition]> {
match self {
BinaryIndexedView::Module(module) => Some(module.function_defs()),
Expand Down
49 changes: 47 additions & 2 deletions third_party/move/language/move-binary-format/src/check_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,18 @@ impl<'a> BoundsChecker<'a> {
*idx,
bytecode_offset,
)?,
MutBorrowVariantField(idx) | ImmBorrowVariantField(idx) => self
.check_code_unit_bounds_impl_opt(
&self.view.variant_field_handles(),
*idx,
bytecode_offset,
)?,
MutBorrowFieldGeneric(idx) | ImmBorrowFieldGeneric(idx) => {
self.check_code_unit_bounds_impl_opt(
&self.view.field_instantiations(),
*idx,
bytecode_offset,
)?;
// check type parameters in borrow are bound to the function type parameters
if let Some(field_inst) = self
.view
.field_instantiations()
Expand All @@ -397,6 +402,23 @@ impl<'a> BoundsChecker<'a> {
)?;
}
}
MutBorrowVariantFieldGeneric(idx) | ImmBorrowVariantFieldGeneric(idx) => {
self.check_code_unit_bounds_impl_opt(
&self.view.variant_field_instantiations(),
*idx,
bytecode_offset,
)?;
if let Some(field_inst) = self
.view
.variant_field_instantiations()
.and_then(|f| f.get(idx.into_index()))
{
self.check_type_parameters_in_signature(
field_inst.type_parameters,
type_param_count,
)?;
}
}
Call(idx) => self.check_code_unit_bounds_impl(
self.view.function_handles(),
*idx,
Expand All @@ -408,7 +430,6 @@ impl<'a> BoundsChecker<'a> {
*idx,
bytecode_offset,
)?;
// check type parameters in call are bound to the function type parameters
if let Some(func_inst) =
self.view.function_instantiations().get(idx.into_index())
{
Expand All @@ -425,6 +446,12 @@ impl<'a> BoundsChecker<'a> {
*idx,
bytecode_offset,
)?,
PackVariant(idx) | UnpackVariant(idx) | TestVariant(idx) => self
.check_code_unit_bounds_impl_opt(
&self.view.struct_variant_handles(),
*idx,
bytecode_offset,
)?,
PackGeneric(idx)
| UnpackGeneric(idx)
| ExistsGeneric(idx)
Expand All @@ -449,6 +476,24 @@ impl<'a> BoundsChecker<'a> {
)?;
}
}
PackVariantGeneric(idx) | UnpackVariantGeneric(idx) | TestVariantGeneric(idx) => {
self.check_code_unit_bounds_impl_opt(
&self.view.struct_variant_instantiations(),
*idx,
bytecode_offset,
)?;
// check type parameters
if let Some(struct_variant_inst) = self
.view
.struct_variant_instantiations()
.and_then(|s| s.get(idx.into_index()))
{
self.check_type_parameters_in_signature(
struct_variant_inst.type_parameters,
type_param_count,
)?;
}
}
// Instructions that refer to this code block.
BrTrue(offset) | BrFalse(offset) | Branch(offset) => {
let offset = *offset as usize;
Expand Down
66 changes: 66 additions & 0 deletions third_party/move/language/move-binary-format/src/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,42 @@ fn load_struct_def_inst_index(
)?))
}

fn load_variant_field_handle_index(
cursor: &mut VersionedCursor,
) -> BinaryLoaderResult<VariantFieldHandleIndex> {
Ok(VariantFieldHandleIndex(read_uleb_internal(
cursor,
TABLE_INDEX_MAX,
)?))
}

fn load_variant_field_inst_index(
cursor: &mut VersionedCursor,
) -> BinaryLoaderResult<VariantFieldInstantiationIndex> {
Ok(VariantFieldInstantiationIndex(read_uleb_internal(
cursor,
TABLE_INDEX_MAX,
)?))
}

fn load_struct_variant_handle_index(
cursor: &mut VersionedCursor,
) -> BinaryLoaderResult<StructVariantHandleIndex> {
Ok(StructVariantHandleIndex(read_uleb_internal(
cursor,
TABLE_INDEX_MAX,
)?))
}

fn load_struct_variant_inst_index(
cursor: &mut VersionedCursor,
) -> BinaryLoaderResult<StructVariantInstantiationIndex> {
Ok(StructVariantInstantiationIndex(read_uleb_internal(
cursor,
TABLE_INDEX_MAX,
)?))
}

fn load_constant_pool_index(cursor: &mut VersionedCursor) -> BinaryLoaderResult<ConstantPoolIndex> {
Ok(ConstantPoolIndex(read_uleb_internal(
cursor,
Expand Down Expand Up @@ -1520,12 +1556,42 @@ fn load_code(cursor: &mut VersionedCursor, code: &mut Vec<Bytecode>) -> BinaryLo
Opcodes::IMM_BORROW_FIELD_GENERIC => {
Bytecode::ImmBorrowFieldGeneric(load_field_inst_index(cursor)?)
}
Opcodes::MUT_BORROW_VARIANT_FIELD => {
Bytecode::MutBorrowVariantField(load_variant_field_handle_index(cursor)?)
}
Opcodes::MUT_BORROW_VARIANT_FIELD_GENERIC => {
Bytecode::MutBorrowVariantFieldGeneric(load_variant_field_inst_index(cursor)?)
}
Opcodes::IMM_BORROW_VARIANT_FIELD => {
Bytecode::ImmBorrowVariantField(load_variant_field_handle_index(cursor)?)
}
Opcodes::IMM_BORROW_VARIANT_FIELD_GENERIC => {
Bytecode::ImmBorrowVariantFieldGeneric(load_variant_field_inst_index(cursor)?)
}
Opcodes::CALL => Bytecode::Call(load_function_handle_index(cursor)?),
Opcodes::CALL_GENERIC => Bytecode::CallGeneric(load_function_inst_index(cursor)?),
Opcodes::PACK => Bytecode::Pack(load_struct_def_index(cursor)?),
Opcodes::PACK_GENERIC => Bytecode::PackGeneric(load_struct_def_inst_index(cursor)?),
Opcodes::UNPACK => Bytecode::Unpack(load_struct_def_index(cursor)?),
Opcodes::UNPACK_GENERIC => Bytecode::UnpackGeneric(load_struct_def_inst_index(cursor)?),
Opcodes::PACK_VARIANT => {
Bytecode::PackVariant(load_struct_variant_handle_index(cursor)?)
}
Opcodes::UNPACK_VARIANT => {
Bytecode::UnpackVariant(load_struct_variant_handle_index(cursor)?)
}
Opcodes::PACK_VARIANT_GENERIC => {
Bytecode::PackVariantGeneric(load_struct_variant_inst_index(cursor)?)
}
Opcodes::UNPACK_VARIANT_GENERIC => {
Bytecode::UnpackVariantGeneric(load_struct_variant_inst_index(cursor)?)
}
Opcodes::TEST_VARIANT => {
Bytecode::TestVariant(load_struct_variant_handle_index(cursor)?)
}
Opcodes::TEST_VARIANT_GENERIC => {
Bytecode::TestVariantGeneric(load_struct_variant_inst_index(cursor)?)
}
Opcodes::READ_REF => Bytecode::ReadRef,
Opcodes::WRITE_REF => Bytecode::WriteRef,
Opcodes::ADD => Bytecode::Add,
Expand Down
Loading

0 comments on commit 3fa761c

Please sign in to comment.