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

[move] Rewrite verifier metering #19036

Merged
merged 23 commits into from
Aug 22, 2024
Merged
Prev Previous commit
Next Next commit
calibration
  • Loading branch information
tnowacki committed Aug 21, 2024
commit f0dee4b7a3d3dc15038bb8eba135557b4d42c9f0
6 changes: 3 additions & 3 deletions crates/sui-protocol-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2739,9 +2739,9 @@ impl ProtocolConfig {
/// MeterConfig for metering packages during signing. It is NOT safe to use during execution.
pub fn meter_config_for_signing(&self) -> MeterConfig {
MeterConfig {
max_per_fun_meter_units: Some(600_000),
max_per_mod_meter_units: Some(600_000),
max_per_pkg_meter_units: Some(600_000),
max_per_fun_meter_units: Some(1_000_000),
max_per_mod_meter_units: Some(1_000_000),
max_per_pkg_meter_units: Some(1_000_000),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ use move_binary_format::{
use move_borrow_graph::references::RefID;
use move_bytecode_verifier_meter::{Meter, Scope};
use move_core_types::vm_status::StatusCode;
use std::collections::{BTreeMap, BTreeSet};
use std::{
cmp::max,
collections::{BTreeMap, BTreeSet},
};

type BorrowGraph = move_borrow_graph::graph::BorrowGraph<(), Label>;

Expand Down Expand Up @@ -80,6 +83,10 @@ pub(crate) const JOIN_BASE_COST: u128 = 10;
pub(crate) const PER_GRAPH_ITEM_COST: u128 = 4;

pub(crate) const RELEASE_ITEM_COST: u128 = 3;
pub(crate) const RELEASE_ITEM_QUADRATIC_THRESHOLD: usize = 5;

pub(crate) const JOIN_ITEM_COST: u128 = 4;
pub(crate) const JOIN_ITEM_QUADRATIC_THRESHOLD: usize = 10;

pub(crate) const ADD_BORROW_COST: u128 = 3;

Expand Down Expand Up @@ -882,6 +889,7 @@ impl AbstractDomain for AbstractState {
assert!(joined.is_canonical());
assert!(self.locals.len() == joined.locals.len());
let max_size = std::cmp::max(std::cmp::max(self_size, state_size), joined.graph_size());
charge_join(self_size, state_size, meter)?;
charge_graph_size(max_size, meter)?;
charge_release(released, meter)?;
let locals_unchanged = self
Expand All @@ -901,17 +909,38 @@ impl AbstractDomain for AbstractState {
}

fn charge_graph_size(size: usize, meter: &mut (impl Meter + ?Sized)) -> PartialVMResult<()> {
meter.add_items(Scope::Function, PER_GRAPH_ITEM_COST, scale(size))
let size = max(size, 1);
tnowacki marked this conversation as resolved.
Show resolved Hide resolved
meter.add_items(Scope::Function, PER_GRAPH_ITEM_COST, size)
}

fn charge_release(released: usize, meter: &mut (impl Meter + ?Sized)) -> PartialVMResult<()> {
meter.add_items(Scope::Function, RELEASE_ITEM_COST, scale(released))
let size = max(released, 1);
meter.add_items(
Scope::Function,
RELEASE_ITEM_COST,
// max(x, x^2/5)
std::cmp::max(
size,
size.saturating_mul(size) / RELEASE_ITEM_QUADRATIC_THRESHOLD,
),
)
}

fn scale(x: usize) -> usize {
if x == 0 {
return 0;
}

x.saturating_add((x / 3).saturating_mul(x))
fn charge_join(
size1: usize,
size2: usize,
meter: &mut (impl Meter + ?Sized),
) -> PartialVMResult<()> {
let size1 = max(size1, 1);
let size2 = max(size2, 1);
let size = size1.saturating_add(size2);
meter.add_items(
Scope::Function,
JOIN_ITEM_COST,
// max(x, x^2/10)
std::cmp::max(
size,
size.saturating_mul(size) / JOIN_ITEM_QUADRATIC_THRESHOLD,
),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct Locals<'a> {
}

const TYPE_NODE_COST: u128 = 6;
const TYPE_NODE_QUADRATIC_THRESHOLD: usize = 10;
const TYPE_PUSH_COST: u128 = 3;

impl<'a> Locals<'a> {
Expand Down Expand Up @@ -133,10 +134,15 @@ macro_rules! charge_clone {
}

fn charge_ty(meter: &mut (impl Meter + ?Sized), ty: &SignatureToken) -> PartialVMResult<()> {
let size = ty.preorder_traversal().count();
meter.add_items(
Scope::Function,
TYPE_NODE_COST,
ty.preorder_traversal().count(),
// max(x, x^2/10)
std::cmp::max(
size,
size.saturating_mul(size) / TYPE_NODE_QUADRATIC_THRESHOLD,
),
)
}

Expand Down
6 changes: 3 additions & 3 deletions external-crates/move/crates/move-vm-config/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ impl Default for VerifierConfig {
impl Default for MeterConfig {
fn default() -> Self {
Self {
max_per_fun_meter_units: Some(600_000),
max_per_mod_meter_units: Some(600_000),
max_per_pkg_meter_units: Some(600_000),
max_per_fun_meter_units: Some(1_000_000),
max_per_mod_meter_units: Some(1_000_000),
max_per_pkg_meter_units: Some(1_000_000),
}
}
}
Expand Down