Skip to content

Commit

Permalink
[vm-rewrite] Fix up sui-side tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tzakian committed Feb 11, 2025
1 parent 8704f63 commit fc9315b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 65 deletions.
143 changes: 79 additions & 64 deletions crates/sui-types/src/gas_model/tables.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,59 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::collections::BTreeMap;

use super::gas_predicates::{charge_input_as_memory, use_legacy_abstract_size};
use crate::gas_model::{
gas_predicates::native_function_threshold_exceeded,
units_types::{CostTable, Gas, GasCost},
};
use move_binary_format::errors::{PartialVMError, PartialVMResult};

use move_core_types::gas_algebra::{AbstractMemorySize, InternalGas, NumArgs, NumBytes};
use move_core_types::language_storage::ModuleId;

use move_core_types::vm_status::StatusCode;
use move_core_types::{
gas_algebra::{AbstractMemorySize, InternalGas, NumArgs, NumBytes},
language_storage::ModuleId,
vm_status::StatusCode,
};
use move_vm_profiler::GasProfiler;
use move_vm_runtime::dev_utils::tiered_gas_schedule::CostTable as TestCostTable;
use move_vm_runtime::shared::gas::{GasMeter, SimpleInstruction};
use move_vm_runtime::shared::views::{TypeView, ValueView};
use move_vm_runtime::{
dev_utils::tiered_gas_schedule::CostTable as TestCostTable,
shared::{
gas::{GasMeter, SimpleInstruction},
views::{TypeView, ValueView},
},
};
use once_cell::sync::Lazy;

use crate::gas_model::gas_predicates::native_function_threshold_exceeded;
use crate::gas_model::units_types::{CostTable, Gas, GasCost};

use super::gas_predicates::charge_input_as_memory;
use super::gas_predicates::use_legacy_abstract_size;

/// VM flat fee
pub const VM_FLAT_FEE: Gas = Gas::new(8_000);
use std::collections::BTreeMap;

pub static ZERO_COST_SCHEDULE: Lazy<CostTable> = Lazy::new(zero_cost_schedule);

pub static INITIAL_COST_SCHEDULE: Lazy<CostTable> = Lazy::new(initial_cost_schedule_v1);

macro_rules! type_size {
($name:ident, $size:expr) => {
pub const $name: AbstractMemorySize = AbstractMemorySize::new($size);
};
}

// TODO(vm-rewrite): New type sizes
// type_size!(BOOL_SIZE, 1);
// type_size!(U8_SIZE, 1);
// type_size!(U16_SIZE, 2);
// type_size!(U32_SIZE, 4);
// type_size!(U64_SIZE, 8);
// type_size!(U128_SIZE, 16);
// type_size!(U256_SIZE, 32);
// // The size of a vector (without its containing data) in bytes
// type_size!(VEC_SIZE, 8);
// // The size in bytes for a reference on the stack
// type_size!(REFERENCE_SIZE, 8);
// // The size of a struct in bytes
// type_size!(STRUCT_SIZE, 2);

// TODO(vm-rewrite): Remove these type sizes they are for the old one.
type_size!(BOOL_SIZE, 1);
type_size!(U8_SIZE, 1);
type_size!(U16_SIZE, 2);
type_size!(U32_SIZE, 4);
type_size!(U64_SIZE, 8);
type_size!(U128_SIZE, 16);
type_size!(U256_SIZE, 32);
type_size!(U16_SIZE, 1);
type_size!(U32_SIZE, 1);
type_size!(U64_SIZE, 1);
type_size!(U128_SIZE, 1);
type_size!(U256_SIZE, 1);
// The size of a vector (without its containing data) in bytes
type_size!(VEC_SIZE, 8);
// The size in bytes for a reference on the stack
Expand Down Expand Up @@ -945,25 +958,20 @@ pub fn initial_cost_schedule_for_unit_tests() -> TestCostTable {
}

mod old_versions {
use crate::gas_model::gas_predicates::native_function_threshold_exceeded;
use crate::gas_model::gas_predicates::use_legacy_abstract_size;
use crate::gas_model::tables::STRUCT_SIZE;
use crate::gas_model::tables::VEC_SIZE;

use super::{
GasStatus, BOOL_SIZE, REFERENCE_SIZE, U128_SIZE, U16_SIZE, U256_SIZE, U32_SIZE, U64_SIZE,
U8_SIZE,
use super::{GasStatus, BOOL_SIZE, REFERENCE_SIZE, STRUCT_SIZE, U64_SIZE, VEC_SIZE};
use crate::gas_model::gas_predicates::{
native_function_threshold_exceeded, use_legacy_abstract_size,
};
use legacy_move_vm_types::{
gas::{GasMeter, SimpleInstruction},
loaded_data::runtime_types::Type,
views::{TypeView, ValueView},
};
use legacy_move_vm_types::gas::GasMeter;
use legacy_move_vm_types::gas::SimpleInstruction;
use legacy_move_vm_types::views::TypeView;
use legacy_move_vm_types::views::ValueView;
use move_binary_format::errors::PartialVMResult;
use move_core_types::gas_algebra::AbstractMemorySize;
use move_core_types::gas_algebra::InternalGas;
use move_core_types::gas_algebra::NumArgs;
use move_core_types::gas_algebra::NumBytes;
use move_core_types::language_storage::ModuleId;
use move_core_types::{
gas_algebra::{AbstractMemorySize, InternalGas, NumArgs, NumBytes},
language_storage::ModuleId,
};
use move_vm_profiler::GasProfiler;

/// Returns a tuple of (<pops>, <pushes>, <stack_size_decrease>, <stack_size_increase>)
Expand All @@ -975,37 +983,44 @@ mod old_versions {
match instr {
// NB: The `Ret` pops are accounted for in `Call` instructions, so we say `Ret` has no pops.
Nop | Ret => (0, 0, 0.into(), 0.into()),
BrTrue | BrFalse => (1, 0, BOOL_SIZE, 0.into()),
BrTrue | BrFalse => (1, 0, Type::Bool.size(), 0.into()),
Branch => (0, 0, 0.into(), 0.into()),
LdU8 => (0, 1, 0.into(), U8_SIZE),
LdU16 => (0, 1, 0.into(), U16_SIZE),
LdU32 => (0, 1, 0.into(), U32_SIZE),
LdU64 => (0, 1, 0.into(), U64_SIZE),
LdU128 => (0, 1, 0.into(), U128_SIZE),
LdU256 => (0, 1, 0.into(), U256_SIZE),
LdTrue | LdFalse => (0, 1, 0.into(), BOOL_SIZE),
LdU8 => (0, 1, 0.into(), Type::U8.size()),
LdU16 => (0, 1, 0.into(), Type::U16.size()),
LdU32 => (0, 1, 0.into(), Type::U32.size()),
LdU64 => (0, 1, 0.into(), Type::U64.size()),
LdU128 => (0, 1, 0.into(), Type::U128.size()),
LdU256 => (0, 1, 0.into(), Type::U256.size()),
LdTrue | LdFalse => (0, 1, 0.into(), Type::Bool.size()),
FreezeRef => (1, 1, REFERENCE_SIZE, REFERENCE_SIZE),
ImmBorrowLoc | MutBorrowLoc => (0, 1, 0.into(), REFERENCE_SIZE),
ImmBorrowField | MutBorrowField | ImmBorrowFieldGeneric | MutBorrowFieldGeneric => {
(1, 1, REFERENCE_SIZE, REFERENCE_SIZE)
}
// Since we don't have the size of the value being cast here we take a conservative
// over-approximation: it is _always_ getting cast from the smallest integer type.
CastU8 => (1, 1, U8_SIZE, U8_SIZE),
CastU16 => (1, 1, U8_SIZE, U16_SIZE),
CastU32 => (1, 1, U8_SIZE, U32_SIZE),
CastU64 => (1, 1, U8_SIZE, U64_SIZE),
CastU128 => (1, 1, U8_SIZE, U128_SIZE),
CastU256 => (1, 1, U8_SIZE, U256_SIZE),
CastU8 => (1, 1, Type::U8.size(), Type::U8.size()),
CastU16 => (1, 1, Type::U8.size(), Type::U16.size()),
CastU32 => (1, 1, Type::U8.size(), Type::U32.size()),
CastU64 => (1, 1, Type::U8.size(), Type::U64.size()),
CastU128 => (1, 1, Type::U8.size(), Type::U128.size()),
CastU256 => (1, 1, Type::U8.size(), Type::U256.size()),
// NB: We don't know the size of what integers we're dealing with, so we conservatively
// over-approximate by popping the smallest integers, and push the largest.
Add | Sub | Mul | Mod | Div => (2, 1, U8_SIZE + U8_SIZE, U256_SIZE),
BitOr | BitAnd | Xor => (2, 1, U8_SIZE + U8_SIZE, U256_SIZE),
Shl | Shr => (2, 1, U8_SIZE + U8_SIZE, U256_SIZE),
Or | And => (2, 1, BOOL_SIZE + BOOL_SIZE, BOOL_SIZE),
Lt | Gt | Le | Ge => (2, 1, U8_SIZE + U8_SIZE, BOOL_SIZE),
Not => (1, 1, BOOL_SIZE, BOOL_SIZE),
Abort => (1, 0, U64_SIZE, 0.into()),
Add | Sub | Mul | Mod | Div => {
(2, 1, Type::U8.size() + Type::U8.size(), Type::U256.size())
}
BitOr | BitAnd | Xor => (2, 1, Type::U8.size() + Type::U8.size(), Type::U256.size()),
Shl | Shr => (2, 1, Type::U8.size() + Type::U8.size(), Type::U256.size()),
Or | And => (
2,
1,
Type::Bool.size() + Type::Bool.size(),
Type::Bool.size(),
),
Lt | Gt | Le | Ge => (2, 1, Type::U8.size() + Type::U8.size(), Type::Bool.size()),
Not => (1, 1, Type::Bool.size(), Type::Bool.size()),
Abort => (1, 0, Type::U64.size(), 0.into()),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4088,9 +4088,24 @@ pub mod prop {
.collect::<Vec<_>>()
.prop_map(move |vals| Value::struct_(Struct::pack(vals)))
.boxed(),
L::Enum(enum_layout) => pick_variant_layout((**enum_layout).clone())
.prop_flat_map(move |(tag, variant_layout)| {
let v = variant_layout
.into_iter()
.map(move |l| value_strategy_with_layout(&l))
.collect::<Vec<_>>();
v.prop_map(move |vals| Value::variant(Variant::pack(tag as u16, vals)))
})
.boxed(),
}
}

fn pick_variant_layout(
enum_layout: MoveEnumLayout,
) -> impl Strategy<Value = (u16, Vec<MoveTypeLayout>)> {
(0..enum_layout.0.len()).prop_map(move |tag| (tag as u16, enum_layout.0[tag].clone()))
}

pub fn layout_strategy() -> impl Strategy<Value = MoveTypeLayout> {
use MoveTypeLayout as L;

Expand All @@ -4110,7 +4125,7 @@ pub mod prop {
prop_oneof![
1 => inner.clone().prop_map(|layout| L::Vector(Box::new(layout))),
1 => vec(inner, 0..1).prop_map(|f_layouts| {
L::Struct(MoveStructLayout::new(f_layouts))}),
L::Struct(Box::new(MoveStructLayout::new(f_layouts)))}),
]
})
}
Expand Down

0 comments on commit fc9315b

Please sign in to comment.