-
Notifications
You must be signed in to change notification settings - Fork 11.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Debug module to move stdlib deps
- Loading branch information
Showing
9 changed files
with
151 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
sui_programmability/framework/deps/move-stdlib/sources/Debug.move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/// Module providing debug functionality. | ||
module Std::Debug { | ||
native public fun print<T>(x: &T); | ||
|
||
native public fun print_stack_trace(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! This verifier only runs when we are trying to publish a module onchain. | ||
//! Hence it contains publish-specific verification rules, such as no calls to Debug module. | ||
use crate::verification_failure; | ||
use move_binary_format::{ | ||
binary_views::BinaryIndexedView, | ||
file_format::{Bytecode, CompiledModule, FunctionHandleIndex}, | ||
}; | ||
use sui_types::{error::SuiResult, SUI_FRAMEWORK_ADDRESS}; | ||
|
||
pub fn verify_module(module: &CompiledModule) -> SuiResult { | ||
verify_no_debug_calls(module) | ||
} | ||
|
||
/// Checks that whether any debug code is used when publishing a module onchain. | ||
fn verify_no_debug_calls(module: &CompiledModule) -> SuiResult { | ||
let view = BinaryIndexedView::Module(module); | ||
for func_def in &module.function_defs { | ||
if func_def.code.is_none() { | ||
continue; | ||
} | ||
let code = &func_def.code.as_ref().unwrap().code; | ||
for bytecode in code { | ||
match bytecode { | ||
Bytecode::Call(idx) => check_call(&view, idx)?, | ||
Bytecode::CallGeneric(idx) => { | ||
check_call(&view, &view.function_instantiation_at(*idx).handle)? | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn check_call(view: &BinaryIndexedView, func_idx: &FunctionHandleIndex) -> SuiResult { | ||
let handle = view.function_handle_at(*func_idx); | ||
let module_idx = handle.module; | ||
let module_handle = view.module_handle_at(module_idx); | ||
if view.address_identifier_at(module_handle.address) == &SUI_FRAMEWORK_ADDRESS | ||
&& view.identifier_at(module_handle.name).as_str() == "Debug" | ||
{ | ||
Err(verification_failure( | ||
format!("Calls to Debug module not allowed when publishing code onchain. Found in function '{:?}'", view.identifier_at(handle.name)) | ||
)) | ||
} else { | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
sui_programmability/verifier/tests/publish_verification_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) 2021, Facebook, Inc. and its affiliates | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
mod common; | ||
|
||
pub use common::module_builder::ModuleBuilder; | ||
use move_binary_format::file_format::*; | ||
use sui_types::SUI_FRAMEWORK_ADDRESS; | ||
use sui_verifier::publish_verifier::verify_module; | ||
|
||
#[test] | ||
fn function_with_debug_call() { | ||
let (mut module, _) = ModuleBuilder::default(); | ||
// Add mock Debug module. | ||
let debug_module = module.add_module(SUI_FRAMEWORK_ADDRESS, "Debug"); | ||
let print_func1 = module.add_function(debug_module, "print1", vec![], vec![]); | ||
let print_func2 = module.add_generic_function(debug_module, "print2", vec![], vec![], vec![]); | ||
let func = module.add_function(module.get_self_index(), "foo", vec![], vec![]); | ||
assert!(verify_module(module.get_module()).is_ok()); | ||
|
||
// Bytecode that contains a call to Debug::print. | ||
let code = vec![Bytecode::Call(print_func1.handle)]; | ||
module.set_bytecode(func.def, code); | ||
assert!(verify_module(module.get_module()) | ||
.unwrap_err() | ||
.to_string() | ||
.contains("Calls to Debug module not allowed when publishing code onchain")); | ||
|
||
let code = vec![Bytecode::CallGeneric(print_func2.handle)]; | ||
module.set_bytecode(func.def, code); | ||
assert!(verify_module(module.get_module()) | ||
.unwrap_err() | ||
.to_string() | ||
.contains("Calls to Debug module not allowed when publishing code onchain")); | ||
} |