Skip to content

Commit

Permalink
fix test-native-functions (#4317)
Browse files Browse the repository at this point in the history
* add bytecode_instrcution attr to runtime metadata
  • Loading branch information
simonjiao authored Dec 3, 2024
1 parent 86f0fbc commit 62457f8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions scripts/nextest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ cargo nextest -V >/dev/null 2>&1 || cargo install cargo-nextest --version "0.9.5
#cargo nextest run --workspace --retries 2 --build-jobs 8 --test-threads 12 --no-fail-fast --failure-output immediate-final
cargo nextest run \
-p starcoin-executor \
-p starcoin-vm-runtime \
--retries 2 --build-jobs 8 --test-threads 12 --no-fail-fast --failure-output immediate-final


Expand Down
37 changes: 33 additions & 4 deletions vm/framework/src/extended_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const RESOURCE_GROUP_MEMBER: &str = "resource_group_member";
const RESOURCE_GROUP_NAME: &str = "group";
const RESOURCE_GROUP_SCOPE: &str = "scope";
const VIEW_FUN_ATTRIBUTE: &str = "view";
const BYTECODE_INSTRUCTION: &str = "bytecode_instruction";

const RANDOMNESS_MODULE_NAME: &str = "randomness";

Expand Down Expand Up @@ -131,12 +132,40 @@ impl<'a> ExtendedChecker<'a> {
self.check_unsafe_randomness_usage(module);
self.check_and_record_events(module);
self.check_init_module(module);
self.build_error_map(module)
self.build_error_map(module);
self.check_and_record_bytecode_instruction(module)
}
}
}
}

// ----------------------------------------------------------------------------------
// Native Functions

impl<'a> ExtendedChecker<'a> {
fn check_and_record_bytecode_instruction(&mut self, module: &ModuleEnv) {
let module_id = self.get_runtime_module_id(module);
for ref fun in module.get_functions() {
if !fun.is_native() || !self.has_attribute(fun, BYTECODE_INSTRUCTION) {
continue;
}

eprintln!(
"Found bytecode instruction function {}",
fun.get_simple_name_string()
);

self.output
.entry(module_id.clone())
.or_default()
.fun_attributes
.entry(fun.get_simple_name_string().to_string())
.or_default()
.push(KnownAttribute::bytecode_instruction());
}
}
}

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

Expand Down Expand Up @@ -677,9 +706,9 @@ impl<'a> ExtendedChecker<'a> {
Type::Reference(mutability, inner) => {
if let Type::Primitive(inner) = inner.as_ref() {
if inner == &PrimitiveType::Signer
// Avoid a redundant error message for `&mut signer`, which is
// always disallowed for transaction entries, not just for
// `#[view]`.
// Avoid a redundant error message for `&mut signer`, which is
// always disallowed for transaction entries, not just for
// `#[view]`.
&& mutability == &ReferenceKind::Immutable
{
self.env.error(
Expand Down
12 changes: 12 additions & 0 deletions vm/framework/src/module_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub enum KnownAttributeKind {
ResourceGroupMember = 3,
Event = 4,
Randomness = 5,
BytecodeInstruction = 6,
}

impl KnownAttribute {
Expand Down Expand Up @@ -177,6 +178,17 @@ impl KnownAttribute {
None
}
}

pub fn bytecode_instruction() -> Self {
Self {
kind: KnownAttributeKind::BytecodeInstruction as u8,
args: vec![],
}
}

pub fn is_bytecode_instruction(&self) -> bool {
self.kind == KnownAttributeKind::BytecodeInstruction as u8
}
}

const METADATA_CACHE_SIZE: usize = 1024;
Expand Down
1 change: 1 addition & 0 deletions vm/vm-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ move-unit-test = { workspace = true, optional = true }
[dev-dependencies]
stdlib = { workspace = true }
claims = { workspace = true }
starcoin-cached-packages = { workspace = true }


[features]
Expand Down
29 changes: 23 additions & 6 deletions vm/vm-runtime/tests/test_native_functions.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
use anyhow::Result;
use starcoin_config::genesis_config::G_LATEST_GAS_PARAMS;
use starcoin_framework::get_metadata_from_compiled_module;
use starcoin_gas_schedule::LATEST_GAS_FEATURE_VERSION;
use starcoin_vm_types::access::ModuleAccess;
use starcoin_vm_types::normalized::Function;
use std::collections::HashSet;
use stdlib::load_latest_compiled_modules;
use std::collections::{HashMap, HashSet};

#[test]
pub fn test_native_function_matches() -> Result<()> {
let modules = load_latest_compiled_modules();
let modules = starcoin_cached_packages::head_release_bundle().compiled_modules();
let runtime_metadata = modules
.iter()
.filter_map(|m| {
get_metadata_from_compiled_module(m).map(|metadata| (m.self_id(), metadata))
})
.collect::<HashMap<_, _>>();
let native_functions: Vec<_> = modules
.iter()
.flat_map(|m| {
m.function_defs()
.iter()
.filter_map(|func_def| {
if func_def.is_native() {
Some(Function::new(m, func_def).0)
let func_name = Function::new(m, func_def).0;
if func_def.is_native()
&& !runtime_metadata
.get(&m.self_id())
.and_then(|metadata| {
metadata.fun_attributes.get(&func_name.to_string())
})
.map(|attr| attr.iter().any(|attr| attr.is_bytecode_instruction()))
.unwrap_or_default()
{
Some(func_name)
} else {
None
}
Expand All @@ -32,7 +48,7 @@ pub fn test_native_function_matches() -> Result<()> {
.collect();

let mut native_function_table = starcoin_vm_runtime::natives::starcoin_natives(
6,
LATEST_GAS_FEATURE_VERSION,
G_LATEST_GAS_PARAMS.clone().natives,
G_LATEST_GAS_PARAMS.clone().vm.misc,
starcoin_vm_types::on_chain_config::TimedFeaturesBuilder::enable_all().build(),
Expand All @@ -49,6 +65,7 @@ pub fn test_native_function_matches() -> Result<()> {
&f
);
}

for f in native_function_table {
println!("native {:?} is un-used in latest stdlib", f)
}
Expand Down

0 comments on commit 62457f8

Please sign in to comment.