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
signatures
  • Loading branch information
tnowacki committed Aug 21, 2024
commit 59a80f1c4f8fc4a42c1f234fbdfe30aa33439547
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,41 @@ use move_binary_format::{
file_format_common::VERSION_6,
IndexKind,
};
use move_bytecode_verifier_meter::{Meter, Scope};
use move_core_types::vm_status::StatusCode;
use std::collections::{HashMap, HashSet};
use std::{
cell::RefCell,
collections::{HashMap, HashSet},
};

use crate::ability_cache::AbilityCache;

pub struct SignatureChecker<'a> {
module: &'a CompiledModule,
pub struct SignatureChecker<'env, 'a, 'b, M: Meter + ?Sized> {
module: &'env CompiledModule,
module_ability_cache: RefCell<&'a mut AbilityCache<'env>>,
meter: RefCell<&'b mut M>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a particular reason for these RefCells? Would be nice if they weren't needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

laziness, fixed

abilities_cache: HashMap<SignatureIndex, HashSet<Vec<AbilitySet>>>,
}

impl<'a> SignatureChecker<'a> {
pub fn verify_module(module: &'a CompiledModule) -> VMResult<()> {
Self::verify_module_impl(module).map_err(|e| e.finish(Location::Module(module.self_id())))
impl<'env, 'a, 'b, M: Meter + ?Sized> SignatureChecker<'env, 'a, 'b, M> {
pub fn verify_module(
module: &'env CompiledModule,
module_ability_cache: &'a mut AbilityCache<'env>,
meter: &'b mut M,
) -> VMResult<()> {
Self::verify_module_impl(module, module_ability_cache, meter)
.map_err(|e| e.finish(Location::Module(module.self_id())))
}

fn verify_module_impl(module: &'a CompiledModule) -> PartialVMResult<()> {
fn verify_module_impl(
module: &'env CompiledModule,
module_ability_cache: &'a mut AbilityCache<'env>,
meter: &'b mut M,
) -> PartialVMResult<()> {
let mut sig_check = Self {
module,
module_ability_cache: RefCell::new(module_ability_cache),
meter: RefCell::new(meter),
abilities_cache: HashMap::new(),
};
sig_check.verify_signature_pool(module.signatures())?;
Expand Down Expand Up @@ -489,8 +508,15 @@ impl<'a> SignatureChecker<'a> {
);
}

let mut m = self.meter.borrow_mut();
let meter: &mut M = *m;
for (constraint, ty) in constraints.into_iter().zip(type_arguments) {
let given = self.module.abilities(ty, global_abilities)?;
let given = self.module_ability_cache.borrow_mut().abilities(
Scope::Module,
meter,
global_abilities,
ty,
)?;
if !constraint.is_subset(given) {
return Err(PartialVMError::new(StatusCode::CONSTRAINT_NOT_SATISFIED)
.with_message(format!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn verify_module_with_config_metered(
})?;
LimitsVerifier::verify_module(config, module)?;
DuplicationChecker::verify_module(module)?;
SignatureChecker::verify_module(module)?;
SignatureChecker::verify_module(module, ability_cache, meter)?;
InstructionConsistency::verify_module(module)?;
constants::verify_module(module)?;
friends::verify_module(module)?;
Expand Down