From 6995450da0505eb7aa3091903ddbe0275ff86e3d Mon Sep 17 00:00:00 2001 From: Valery Antopol Date: Mon, 5 Feb 2024 17:27:08 +0400 Subject: [PATCH 1/2] add effects extractor --- core/src/marine_core.rs | 6 ++ crates/module-info-parser/src/effects.rs | 71 +++++++++++++++++++ crates/module-info-parser/src/lib.rs | 1 + .../src/manifest/manifest_extractor.rs | 8 +++ crates/module-info-parser/src/manifest/mod.rs | 1 + 5 files changed, 87 insertions(+) create mode 100644 crates/module-info-parser/src/effects.rs diff --git a/core/src/marine_core.rs b/core/src/marine_core.rs index e7d566a02..668c7713f 100644 --- a/core/src/marine_core.rs +++ b/core/src/marine_core.rs @@ -118,6 +118,12 @@ impl MarineCore { &self.modules, )?; + println!("Module \"{}\" effects:", name); + for effect in marine_module_info_parser::effects::extract_from_bytes(wasm_bytes).unwrap() { + println!("{:?}", effect); + } + println!("{}", "-".repeat(20)); + match self.modules.entry(name) { Entry::Vacant(entry) => { entry.insert(module); diff --git a/crates/module-info-parser/src/effects.rs b/crates/module-info-parser/src/effects.rs new file mode 100644 index 000000000..66401c22d --- /dev/null +++ b/crates/module-info-parser/src/effects.rs @@ -0,0 +1,71 @@ +/* + * Copyright 2024 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use crate::ModuleInfoResult; +use crate::ModuleInfoError; + +use walrus::ModuleConfig; +use walrus::Module; + +use std::path::Path; + +const HOST_IMPORT_NAMESPACE: &str = "host"; +const LOGGER_IMPORT_NAME: &str = "log_utf8_string"; +const CALL_PARAMETERS_IMPORT_NAME: &str = "get_call_parameters"; + +#[derive(Debug)] +pub enum WasmEffect { + Logger, + MountedBinary(String), +} + +pub fn extract_from_path

(wasm_module_path: P) -> ModuleInfoResult> +where + P: AsRef, +{ + let module = ModuleConfig::new() + .parse_file(wasm_module_path) + .map_err(ModuleInfoError::CorruptedWasmFile)?; + + extract_from_module(&module) +} + +pub fn extract_from_bytes(wasm_module_bytes: &[u8]) -> ModuleInfoResult> { + let module = ModuleConfig::new() + .parse(wasm_module_bytes) + .map_err(ModuleInfoError::CorruptedWasmFile)?; + + extract_from_module(&module) +} + +pub fn extract_from_module(wasm_module: &Module) -> ModuleInfoResult> { + let effects = wasm_module + .imports + .iter() + .filter_map(|import| inspect_import(&import.module, &import.name)) + .collect(); + + Ok(effects) +} + +fn inspect_import(module: &str, name: &str) -> Option { + match (module, name) { + (HOST_IMPORT_NAMESPACE, LOGGER_IMPORT_NAME) => Some(WasmEffect::Logger), + (HOST_IMPORT_NAMESPACE, CALL_PARAMETERS_IMPORT_NAME) => None, + (HOST_IMPORT_NAMESPACE, name) => Some(WasmEffect::MountedBinary(name.to_string())), + (_, _) => None, + } +} diff --git a/crates/module-info-parser/src/lib.rs b/crates/module-info-parser/src/lib.rs index eecaa190f..fcb47626d 100644 --- a/crates/module-info-parser/src/lib.rs +++ b/crates/module-info-parser/src/lib.rs @@ -26,6 +26,7 @@ pub mod manifest; pub mod sdk_version; +pub mod effects; mod custom_section_extractor; mod errors; diff --git a/crates/module-info-parser/src/manifest/manifest_extractor.rs b/crates/module-info-parser/src/manifest/manifest_extractor.rs index f8d08bd00..97e96a02c 100644 --- a/crates/module-info-parser/src/manifest/manifest_extractor.rs +++ b/crates/module-info-parser/src/manifest/manifest_extractor.rs @@ -42,6 +42,14 @@ where extract_from_module(&module) } +pub fn extract_from_bytes(wasm_module_bytes: &[u8]) -> ModuleInfoResult { + let module = ModuleConfig::new() + .parse(wasm_module_bytes) + .map_err(ModuleInfoError::CorruptedWasmFile)?; + + extract_from_module(&module) +} + pub fn extract_from_module(wasm_module: &Module) -> ModuleInfoResult { let sections = extract_custom_sections_by_name(wasm_module, MANIFEST_SECTION_NAME)?; let section = try_as_one_section(§ions, MANIFEST_SECTION_NAME)?; diff --git a/crates/module-info-parser/src/manifest/mod.rs b/crates/module-info-parser/src/manifest/mod.rs index 14fb52e0a..a9f7cf8e5 100644 --- a/crates/module-info-parser/src/manifest/mod.rs +++ b/crates/module-info-parser/src/manifest/mod.rs @@ -24,4 +24,5 @@ pub use errors::ManifestError; pub use manifest_extractor::extract_from_path; pub use manifest_extractor::extract_from_module; pub use manifest_extractor::extract_from_compiled_module; +pub use manifest_extractor::extract_from_bytes; pub use module_manifest::ModuleManifest; From 24518742a5924699db5d881a02bed521ed9422fc Mon Sep 17 00:00:00 2001 From: Valery Antopol Date: Mon, 5 Feb 2024 17:28:54 +0400 Subject: [PATCH 2/2] revert debug changes --- core/src/marine_core.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/core/src/marine_core.rs b/core/src/marine_core.rs index 668c7713f..e7d566a02 100644 --- a/core/src/marine_core.rs +++ b/core/src/marine_core.rs @@ -118,12 +118,6 @@ impl MarineCore { &self.modules, )?; - println!("Module \"{}\" effects:", name); - for effect in marine_module_info_parser::effects::extract_from_bytes(wasm_bytes).unwrap() { - println!("{:?}", effect); - } - println!("{}", "-".repeat(20)); - match self.modules.entry(name) { Entry::Vacant(entry) => { entry.insert(module);