From 9844159c4d07207ade3882718ff53935bc8ff3e5 Mon Sep 17 00:00:00 2001 From: Ashok Menon Date: Wed, 27 Mar 2024 09:29:30 +0000 Subject: [PATCH 1/3] [CLI] Add ability to run verifier on an individual module ## Description Adding a couple of features to the `verify-bytecode-meter` command: - Specify a `--module` (as bytecode) to verify. - Specify a protocol version to verify at. Also switches the `--package` path from a positional parameter to a flag, for symmetry with the new `--module` flag. ## Test Plan: ``` sui-framework/deepbook$ cargo run --bin sui -- \ client verify-bytecode-meter sui-framework/deepbook$ cargo run --bin sui -- \ client verify-bytecode-meter --package . sui-framework/deepbook$ cargo run --bin sui -- \ move build sui-framework/deepbook$ cargo run --bin sui -- \ client verify-bytecode-meter --module build/DeepBook/bytecode_modules/clob.mv sui-framework/deepbook$ cargo run --bin sui -- \ # Errors client verify-bytecode-meter --package '.' \ --module build/DeepBook/bytecode_modules/clob.mv ``` ## Release Notes - Adds a `--module`/`-m` flag to `sui client bytecode-verify-meter` to verify some module bytecode. - Replaces the positional, optional package path parameter for this command with `--package`/`-p`. --- crates/sui/src/client_commands.rs | 56 ++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/crates/sui/src/client_commands.rs b/crates/sui/src/client_commands.rs index 59eb70b5b74d5..f8d4fd6e8a9c9 100644 --- a/crates/sui/src/client_commands.rs +++ b/crates/sui/src/client_commands.rs @@ -5,6 +5,7 @@ use crate::client_ptb::ptb::PTB; use std::{ collections::{btree_map::Entry, BTreeMap}, fmt::{Debug, Display, Formatter, Write}, + fs, path::PathBuf, str::FromStr, sync::Arc, @@ -19,13 +20,14 @@ use fastcrypto::{ traits::ToFromBytes, }; +use move_binary_format::CompiledModule; use move_core_types::language_storage::TypeTag; use move_package::BuildConfig as MoveBuildConfig; use prometheus::Registry; use serde::Serialize; use serde_json::{json, Value}; use sui_move::build::resolve_lock_file_path; -use sui_protocol_config::ProtocolConfig; +use sui_protocol_config::{Chain, ProtocolConfig, ProtocolVersion}; use sui_source_validation::{BytecodeSourceVerifier, SourceMode}; use shared_crypto::intent::Intent; @@ -659,9 +661,18 @@ pub enum SuiClientCommands { /// Run the bytecode verifier on the package #[clap(name = "verify-bytecode-meter")] VerifyBytecodeMeter { - /// Path to directory containing a Move package - #[clap(name = "package_path", global = true, default_value = ".")] - package_path: PathBuf, + /// Path to directory containing a Move package, (defaults to the current directory) + #[clap(name = "package", long, short, global = true)] + package_path: Option, + + /// Protocol version to use for the bytecode verifier (defaults to the latest protocol + /// version) + #[clap(name = "protocol-version", long, short = 'P')] + protocol_version: Option, + + /// Path to specific pre-compiled module bytecode to verify (instead of an entire package) + #[clap(name = "module", long, short, global = true)] + module_path: Option, /// Package build options #[clap(flatten)] @@ -1056,20 +1067,49 @@ impl SuiClientCommands { } SuiClientCommands::VerifyBytecodeMeter { + protocol_version, + module_path, package_path, build_config, } => { - let protocol_config = ProtocolConfig::get_for_max_version_UNSAFE(); + let protocol_version = + protocol_version.map_or(ProtocolVersion::MAX, ProtocolVersion::new); + let protocol_config = + ProtocolConfig::get_for_version(protocol_version, Chain::Unknown); + let registry = &Registry::new(); let bytecode_verifier_metrics = Arc::new(BytecodeVerifierMetrics::new(registry)); - let package = compile_package_simple(build_config, package_path)?; - let modules: Vec<_> = package.get_modules().cloned().collect(); + let modules = match (module_path, package_path) { + (Some(_), Some(_)) => { + bail!("Cannot specify both a module path and a package path") + } + + (Some(module_path), None) => { + let module_bytes = + fs::read(module_path).context("Failed to read module file")?; + let module = CompiledModule::deserialize_with_defaults(&module_bytes) + .context("Failed to deserialize module")?; + vec![module] + } + + (None, package_path) => { + let package_path = package_path.unwrap_or_else(|| PathBuf::from(".")); + let package = compile_package_simple(build_config, package_path)?; + package.get_modules().cloned().collect() + } + }; let mut verifier = sui_execution::verifier(&protocol_config, true, &bytecode_verifier_metrics); let overrides = VerifierOverrides::new(None, None); - println!("Running bytecode verifier for {} modules", modules.len()); + + println!( + "Running bytecode verifier for {} module{}", + modules.len(), + if modules.len() != 1 { "s" } else { "" }, + ); + let verifier_values = verifier.meter_compiled_modules_with_overrides( &modules, &protocol_config, From ca47055465db87e2e0170607d3b986f3ea4f4c37 Mon Sep 17 00:00:00 2001 From: Ashok Menon Date: Thu, 28 Mar 2024 12:27:26 +0000 Subject: [PATCH 2/3] fixup: No short flags --- crates/sui/src/client_commands.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/sui/src/client_commands.rs b/crates/sui/src/client_commands.rs index f8d4fd6e8a9c9..98ecf4bb38758 100644 --- a/crates/sui/src/client_commands.rs +++ b/crates/sui/src/client_commands.rs @@ -662,16 +662,16 @@ pub enum SuiClientCommands { #[clap(name = "verify-bytecode-meter")] VerifyBytecodeMeter { /// Path to directory containing a Move package, (defaults to the current directory) - #[clap(name = "package", long, short, global = true)] + #[clap(name = "package", long, global = true)] package_path: Option, /// Protocol version to use for the bytecode verifier (defaults to the latest protocol /// version) - #[clap(name = "protocol-version", long, short = 'P')] + #[clap(name = "protocol-version", long)] protocol_version: Option, /// Path to specific pre-compiled module bytecode to verify (instead of an entire package) - #[clap(name = "module", long, short, global = true)] + #[clap(name = "module", long, global = true)] module_path: Option, /// Package build options From a56226878ef8db909149cd04681d0f07c03cadb8 Mon Sep 17 00:00:00 2001 From: Ashok Menon Date: Wed, 27 Mar 2024 09:59:05 +0000 Subject: [PATCH 3/3] [BUGFIX][CLI] verify-bytecode-meter returns module ticks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We were returning the ticks used for the last function as the verifier ticks, which is not correct. Returning the ticks used for the last function is also not particularly useful, but I will fix that in a follow-up PR. Test Plan: ``` sui-framework/deepbook$ cargo run --bin sui -- \ client verify-bytecode-meter Total number of linter warnings suppressed: 3 (filtered categories: 2) Running bytecode verifier for 7 modules ╭──────────────────────────────────╮ │ Module will pass metering check! │ ├────────┬────────────┬────────────┤ │ │ Module │ Function │ │ Max │ 16000000 │ 16000000 │ │ Used │ 5869980 │ 7040 │ ╰────────┴────────────┴────────────╯ ``` --- sui-execution/src/latest.rs | 2 +- sui-execution/src/v0.rs | 2 +- sui-execution/src/v1.rs | 2 +- sui-execution/src/v2.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sui-execution/src/latest.rs b/sui-execution/src/latest.rs index 1fae861aee18b..72e978810f47e 100644 --- a/sui-execution/src/latest.rs +++ b/sui-execution/src/latest.rs @@ -224,7 +224,7 @@ impl<'m> verifier::Verifier for Verifier<'m> { config.max_per_mod_meter_units = config_overrides.max_per_mod_meter_units; run_metered_move_bytecode_verifier(modules, &config, &mut self.meter, self.metrics)?; let fun_meter_units_result = self.meter.get_usage(Scope::Function); - let mod_meter_units_result = self.meter.get_usage(Scope::Function); + let mod_meter_units_result = self.meter.get_usage(Scope::Module); Ok(VerifierMeteredValues::new( max_per_fun_meter_current, max_per_mod_meter_current, diff --git a/sui-execution/src/v0.rs b/sui-execution/src/v0.rs index d2ec4b11b6d00..c5e6853c6d8dc 100644 --- a/sui-execution/src/v0.rs +++ b/sui-execution/src/v0.rs @@ -238,7 +238,7 @@ impl<'m> verifier::Verifier for Verifier<'m> { self.metrics, )?; let fun_meter_units_result = self.meter.get_usage(Scope::Function); - let mod_meter_units_result = self.meter.get_usage(Scope::Function); + let mod_meter_units_result = self.meter.get_usage(Scope::Module); Ok(VerifierMeteredValues::new( max_per_fun_meter_current, max_per_mod_meter_current, diff --git a/sui-execution/src/v1.rs b/sui-execution/src/v1.rs index 2d7cbc37c5417..870b7460eb429 100644 --- a/sui-execution/src/v1.rs +++ b/sui-execution/src/v1.rs @@ -226,7 +226,7 @@ impl<'m> verifier::Verifier for Verifier<'m> { config.max_per_mod_meter_units = config_overrides.max_per_mod_meter_units; run_metered_move_bytecode_verifier(modules, &config, &mut self.meter, self.metrics)?; let fun_meter_units_result = self.meter.get_usage(Scope::Function); - let mod_meter_units_result = self.meter.get_usage(Scope::Function); + let mod_meter_units_result = self.meter.get_usage(Scope::Module); Ok(VerifierMeteredValues::new( max_per_fun_meter_current, max_per_mod_meter_current, diff --git a/sui-execution/src/v2.rs b/sui-execution/src/v2.rs index ac258a3fc080a..d5f72b11d7484 100644 --- a/sui-execution/src/v2.rs +++ b/sui-execution/src/v2.rs @@ -224,7 +224,7 @@ impl<'m> verifier::Verifier for Verifier<'m> { config.max_per_mod_meter_units = config_overrides.max_per_mod_meter_units; run_metered_move_bytecode_verifier(modules, &config, &mut self.meter, self.metrics)?; let fun_meter_units_result = self.meter.get_usage(Scope::Function); - let mod_meter_units_result = self.meter.get_usage(Scope::Function); + let mod_meter_units_result = self.meter.get_usage(Scope::Module); Ok(VerifierMeteredValues::new( max_per_fun_meter_current, max_per_mod_meter_current,