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

feat(module-info-parser): add effects extractor #410

Merged
merged 2 commits into from
Feb 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
71 changes: 71 additions & 0 deletions crates/module-info-parser/src/effects.rs
Original file line number Diff line number Diff line change
@@ -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<P>(wasm_module_path: P) -> ModuleInfoResult<Vec<WasmEffect>>
where
P: AsRef<Path>,
{
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<Vec<WasmEffect>> {
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<Vec<WasmEffect>> {
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<WasmEffect> {
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,
}
}
1 change: 1 addition & 0 deletions crates/module-info-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

pub mod manifest;
pub mod sdk_version;
pub mod effects;
mod custom_section_extractor;
mod errors;

Expand Down
8 changes: 8 additions & 0 deletions crates/module-info-parser/src/manifest/manifest_extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ where
extract_from_module(&module)
}

pub fn extract_from_bytes(wasm_module_bytes: &[u8]) -> ModuleInfoResult<ModuleManifest> {
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<ModuleManifest> {
let sections = extract_custom_sections_by_name(wasm_module, MANIFEST_SECTION_NAME)?;
let section = try_as_one_section(&sections, MANIFEST_SECTION_NAME)?;
Expand Down
1 change: 1 addition & 0 deletions crates/module-info-parser/src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Loading