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

Refactor move runtime #4166

Merged
merged 20 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 110 additions & 93 deletions Cargo.lock

Large diffs are not rendered by default.

54 changes: 26 additions & 28 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -343,31 +343,31 @@ log4rs = { version = "1.2.0", features = ["background_rotation", "gzip"] }
lru = "0.7.8"
merkletree = { version = "0.22.1" }
mirai-annotations = "1.10.1"
move-binary-format = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-bytecode-source-map = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-bytecode-verifier = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-cli = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-command-line-common = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-compiler = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-core-types = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-coverage = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-disassembler = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-docgen = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-errmapgen = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-ir-compiler = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-ir-types = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-model = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-package = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-prover = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-prover-test-utils = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-resource-viewer = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-stdlib = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-transactional-test-runner = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-unit-test = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc", features = ["table-extension"] }
move-vm-runtime = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-vm-types = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-table-extension = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc" }
move-vm-test-utils = { git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc", features = ["table-extension"] }
move-binary-format = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-bytecode-source-map = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-bytecode-verifier = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-cli = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-command-line-common = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-compiler = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-core-types = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-coverage = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-disassembler = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-docgen = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-errmapgen = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-ir-compiler = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-ir-types = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-model = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-package = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-prover = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-prover-test-utils = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-resource-viewer = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-stdlib = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-transactional-test-runner = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-unit-test = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25", features = ["table-extension"] }
move-vm-runtime = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-vm-types = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-table-extension = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25" }
move-vm-test-utils = { git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25", features = ["table-extension"] }

names = { version = "0.14.0", default-features = false }
network-api = { path = "network/api", package = "network-api" }
Expand Down Expand Up @@ -535,9 +535,7 @@ unsigned-varint = { version = "0.6.0", features = [
"futures",
"asynchronous_codec",
] }
vm = { package = "move-binary-format", git = "https://github.com/starcoinorg/move", rev = "7b6ac7bb04515ee4ced5b1c1cdafb6ea6cb4eadc", features = [
"fuzzing",
] }
vm = { package = "move-binary-format", git = "https://github.com/starcoinorg/move", rev = "905484b15b2a2218013fdb2c034f1d7898d29a25", features = ["fuzzing"] }
vm-status-translator = { path = "vm/vm-status-translator" }
void = "1.0.2"
walkdir = "2.3.1"
Expand Down
82 changes: 79 additions & 3 deletions executor/tests/script_function_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ use starcoin_transaction_builder::{
use starcoin_types::account::Account;
use starcoin_types::account_config::association_address;
use starcoin_types::transaction::Transaction;
use starcoin_vm_types::account_config::stc_type_tag;
use starcoin_vm_types::identifier::Identifier;
use starcoin_vm_types::language_storage::ModuleId;
use starcoin_vm_types::state_view::StateReaderExt;
use starcoin_vm_types::transaction::{
Package, Script, ScriptFunction, TransactionPayload, TransactionStatus,
};
use starcoin_vm_types::vm_status::KeptVMStatus;
use std::ops::Sub;
use test_helper::executor::{
compile_ir_script, compile_modules_with_address, compile_script, execute_and_apply,
prepare_genesis,
Expand All @@ -36,10 +39,10 @@ fn prepare_module(chain_state: &ChainStateDB, net: &ChainNetwork) -> ModuleId {
public fun fn_public() {
}

public(script) fun fn_script() {
public entry fun fn_script() {
}

public(script) fun fn_script_with_args(account: signer, i: u64) {
public entry fun fn_script_with_args(account: signer, i: u64) {
let r = Self::R { i };
move_to(&account, r);
}
Expand Down Expand Up @@ -201,7 +204,7 @@ fn test_signer_cap_internal_type_error() -> Result<()> {
burn_cap:BurnCapability<Meta>,
update_cap:UpdateCapability<Meta>
}
public(script) fun init(sender: signer){
public entry fun init(sender: signer){
let meta_data = NFT::empty_meta();
NFT::register_v2<Meta>(&sender, meta_data);
let mint_cap = NFT::remove_mint_capability<Meta>(&sender);
Expand Down Expand Up @@ -383,3 +386,76 @@ fn test_struct_republish_backward_incompatible() -> Result<()> {

Ok(())
}

#[stest::test]
fn test_transaction_arg_verify() -> Result<()> {
let (initial_amount, gas_amount) = (5_000_000u128, 600u64);
let (chain_state, net) = prepare_genesis();
let account1 = Account::new();
let txn1 = Transaction::UserTransaction(create_account_txn_sent_as_association(
&account1,
0,
initial_amount,
1,
&net,
));
let output1 = execute_and_apply(&chain_state, txn1);
assert_eq!(KeptVMStatus::Executed, output1.status().status().unwrap());
let module_source = r#"
module {{sender}}::test {
use StarcoinFramework::Token::{Token};
use StarcoinFramework::Account;

public entry fun deposit_token<T: store>(account: signer, coin: Token<T>) {
Account::deposit_to_self<T>(&account, coin);
}
} "#;
let module = compile_modules_with_address(*account1.address(), module_source)
.pop()
.unwrap();

let package = Package::new_with_module(module)?;

let txn1 = Transaction::UserTransaction(account1.create_signed_txn_impl(
*account1.address(),
TransactionPayload::Package(package),
0,
gas_amount,
1,
1,
net.chain_id(),
));
let output = execute_and_apply(&chain_state, txn1);
assert_eq!(
KeptVMStatus::MiscellaneousError,
output.status().status().unwrap()
);

let balance = chain_state.get_balance(*account1.address())?;
assert_eq!(balance, Some(initial_amount.sub(u128::from(gas_amount))));

let money = 100_000;
let num: u128 = 50_000_000;
let payload = TransactionPayload::ScriptFunction(ScriptFunction::new(
ModuleId::new(*account1.address(), Identifier::new("test").unwrap()),
Identifier::new("deposit_token").unwrap(),
vec![stc_type_tag()],
vec![bcs_ext::to_bytes(&num).unwrap()],
));
let txn = Transaction::UserTransaction(account1.create_signed_txn_impl(
*account1.address(),
payload,
1,
money,
1,
1,
net.chain_id(),
));

let output = execute_and_apply(&chain_state, txn);
assert_eq!(
KeptVMStatus::MiscellaneousError,
output.status().status().unwrap()
);
Ok(())
}
5 changes: 5 additions & 0 deletions vm/move-package-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ move-core-types = { workspace = true }
move-coverage = { workspace = true }
move-disassembler = { workspace = true }
move-errmapgen = { workspace = true }
move-model = { workspace = true }
move-package = { workspace = true }
move-unit-test = { workspace = true }
move-vm-runtime = { workspace = true }
Expand All @@ -56,6 +57,10 @@ stdlib = { workspace = true }
vm-status-translator = { workspace = true }
move-vm-test-utils = { workspace = true }

# extra dependences
codespan-reporting = "0.11.1"


[dev-dependencies]

[package]
Expand Down
175 changes: 175 additions & 0 deletions vm/move-package-manager/src/extended_checks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

use move_core_types::{
account_address::AccountAddress, identifier::Identifier, language_storage::ModuleId,
};
use move_model::{
ast::Attribute,
model::{FunctionEnv, GlobalEnv, Loc, ModuleEnv, QualifiedId, StructId},
symbol::Symbol,
ty::{PrimitiveType, Type},
};
use std::rc::Rc;

pub(crate) fn run_extended_checks(env: &GlobalEnv) {
let checker = ExtendedChecker::new(env);
checker.run();
}

struct ExtendedChecker<'a> {
env: &'a GlobalEnv,
/// The id of the module defining error categories
#[allow(unused)]
error_category_module: ModuleId,
}

impl<'a> ExtendedChecker<'a> {
fn new(env: &'a GlobalEnv) -> Self {
Self {
env,
error_category_module: ModuleId::new(
AccountAddress::ONE,
Identifier::new("error").unwrap(),
),
}
}

// see implementation in `aptos-core`
// https://github.com/aptos-labs/aptos-core/blob/3af88bc872221c4958e6163660c60bc07bf53d38/aptos-move/framework/src/extended_checks.rs#L123
fn run(&self) {
for ref module in self.env.get_modules() {
if module.is_target() {
self.check_entry_functions(module);
self.check_init_module(module);
self.build_error_map(module);
}
}
}
}

// -------------------------------------------------------------------------------------------------
// Helper functions

impl<'a> ExtendedChecker<'a> {
fn has_attribute(&self, fun: &FunctionEnv, attr_name: &str) -> bool {
self.has_attribute_iter(fun.get_attributes().iter(), attr_name)
}

fn has_attribute_iter(
&self,
mut attrs: impl Iterator<Item = &'a Attribute>,
attr_name: &str,
) -> bool {
attrs.any(|attr| {
if let Attribute::Apply(_, name, _) = attr {
self.env.symbol_pool().string(*name).as_str() == attr_name
} else {
false
}
})
}

#[allow(unused)]
fn get_runtime_module_id(&self, module: &ModuleEnv<'_>) -> ModuleId {
let name = module.get_name();
let addr = AccountAddress::from_hex_literal(&format!("0x{:x}", name.addr())).unwrap();
let name = Identifier::new(self.name_string(name.name()).to_string()).unwrap();
ModuleId::new(addr, name)
}

#[allow(unused)]
fn name_string(&self, symbol: Symbol) -> Rc<String> {
self.env.symbol_pool().string(symbol)
}
}

// -------------------------------------------------------------------------------------------------
// Module Initialization

impl<'a> ExtendedChecker<'a> {
fn check_init_module(&self, _module: &ModuleEnv) {
// TODO(simon): implement me.
}
Comment on lines +90 to +93
Copy link

Choose a reason for hiding this comment

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

Implement check_init_module method.

The check_init_module method is currently not implemented. Consider implementing this method or providing more details on what needs to be done.

Do you need help implementing this method or would you like me to open a GitHub issue to track this task?

}

// -------------------------------------------------------------------------------------------------
// Entry Functions

impl<'a> ExtendedChecker<'a> {
fn check_entry_functions(&self, module: &ModuleEnv) {
for ref fun in module.get_functions() {
if !fun.is_entry() {
continue;
}

if self.has_attribute(fun, "legacy_entry_function") {
continue;
}

self.check_transaction_args(&fun.get_id_loc(), &fun.get_parameter_types());
if fun.get_return_count() > 0 {
self.env
.error(&fun.get_id_loc(), "entry function cannot return values")
}
}
}

fn check_transaction_args(&self, loc: &Loc, args: &[Type]) {
for ty in args {
self.check_transaction_input_type(loc, ty);
}
}

fn check_transaction_input_type(&self, loc: &Loc, ty: &Type) {
use Type::*;
match ty {
Primitive(_) | TypeParameter(_) => {
// Any primitive type allowed, any parameter expected to instantiate with primitive
}
//todo(simon): give it a try to change me to Reference(ReferenceKind::Immutable, bt)
Reference(false, bt) if matches!(bt.as_ref(), Primitive(PrimitiveType::Signer)) => {
// Reference to signer allowed
}
Vector(ety) => {
// Vectors are allowed if element type is allowed
self.check_transaction_input_type(loc, ety)
}
Struct(mid, sid, _) if self.is_allowed_input_struct(mid.qualified(*sid)) => {
// Specific struct types are allowed
}
_ => {
// Everything else is disallowed.
self.env.error(
loc,
&format!(
"type `{}` is not supported as a parameter type",
ty.display(&self.env.get_type_display_ctx())
),
);
}
}
}

fn is_allowed_input_struct(&self, qid: QualifiedId<StructId>) -> bool {
let name = self.env.get_struct(qid).get_full_name_with_address();
// TODO(simon) find a nice way to keep this in sync with allowed_structs in starcoin-vm
matches!(
name.as_str(),
"0x1::string::String"
| "0x1::object::Object"
| "0x1::option::Option"
| "0x1::fixed_point32::FixedPoint32"
| "0x1::fixed_point64::FixedPoint64"
)
}
}

// -------------------------------------------------------------------------------------------------
// Build errors map

impl<'a> ExtendedChecker<'a> {
fn build_error_map(&self, _module: &ModuleEnv) {
// TODO(simon): implement me.
}
Comment on lines +171 to +174
Copy link

Choose a reason for hiding this comment

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

Implement build_error_map method.

The build_error_map method is currently not implemented. Consider implementing this method or providing more details on what needs to be done.

Do you need help implementing this method or would you like me to open a GitHub issue to track this task?

}
1 change: 1 addition & 0 deletions vm/move-package-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use std::sync::Mutex;

pub mod compatibility_check_cmd;
pub mod deployment;
mod extended_checks;
pub mod package;
pub mod release;

Expand Down
Loading
Loading