Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 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
235 changes: 172 additions & 63 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ members = [
"marine",
"marine/tests/wasm_tests/arguments_passing",
"marine/tests/wasm_tests/arrays_passing",
"marine/tests/wasm_tests/call_parameters_v0",
"marine/tests/wasm_tests/call_parameters_v1",
"marine/tests/wasm_tests/call_parameters_v2",
"marine/tests/wasm_tests/memory_limiting",
"marine/tests/wasm_tests/records_passing",
"marine/tests/wasm_tests/wasi",
Expand Down
2 changes: 2 additions & 0 deletions core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub struct HostImportDescriptor<WB: WasmBackend> {
pub enum HostAPIVersion {
V0,
V1,
V2,
}

impl HostAPIVersion {
Expand All @@ -65,6 +66,7 @@ impl HostAPIVersion {
match self {
Self::V0 => "host",
Self::V1 => "__marine_host_api_v1",
Self::V2 => "__marine_host_api_v2",
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions marine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ path = "src/lib.rs"
marine-core = { path = "../core", version = "0.27.0", default-features = false}
marine-module-interface = { path = "../crates/module-interface", version = "0.8.1" }
marine-utils = { path = "../crates/utils", version = "0.5.1" }
marine-rs-sdk-main = { version = "0.12.0", default-features = false, features = ["logger"] }
marine-rs-sdk = { version = "0.12.0", default-features = false, features = ["logger"] }
old_sdk_call_parameters = { package = "marine-call-parameters", version = "0.10.3", default-features = false }
marine-rs-sdk-main = { version = "0.12.0", default-features = false, features = ["logger"], git = "https://github.com/fluencelabs/marine-rs-sdk/", branch = "feat/particle-parameters" }
marine-rs-sdk = { version = "0.12.0", default-features = false, features = ["logger"], git = "https://github.com/fluencelabs/marine-rs-sdk/", branch = "feat/particle-parameters" }
marine_call_parameters_v0 = { package = "marine-call-parameters", version = "0.10.3", default-features = false }
marine_call_parameters_v1 = { package = "marine-call-parameters", version = "0.12.0", default-features = false }

it-json-serde = { path = "../crates/it-json-serde", version = "0.5.1" }
marine-wasm-backend-traits = { path = "../crates/wasm-backend-traits", version = "0.5.1" }
Expand Down
33 changes: 26 additions & 7 deletions marine/src/config/to_marine_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::host_imports::logger::LoggerFilter;
use crate::host_imports::logger::WASM_LOG_ENV_NAME;
use crate::host_imports::create_call_parameters_import_v0;
use crate::host_imports::create_call_parameters_import_v1;
use crate::host_imports::create_call_parameters_import_v2;

use marine_core::generic::HostImportDescriptor;
use marine_core::generic::MModuleConfig;
Expand Down Expand Up @@ -53,8 +54,9 @@ impl<WB: WasmBackend> MModuleConfigBuilder<WB> {
self,
module_name: String,
marine_module_config: Option<MarineModuleConfig<WB>>,
call_parameters_v0: Arc<Mutex<old_sdk_call_parameters::CallParameters>>,
call_parameters_v1: Arc<Mutex<CallParameters>>,
call_parameters_v0: Arc<Mutex<marine_call_parameters_v0::CallParameters>>,
call_parameters_v1: Arc<Mutex<marine_call_parameters_v1::CallParameters>>,
call_parameters_v2: Arc<Mutex<CallParameters>>,
logger_filter: &LoggerFilter<'_>,
) -> MarineResult<MModuleConfig<WB>> {
let marine_module_config = match marine_module_config {
Expand All @@ -71,7 +73,12 @@ impl<WB: WasmBackend> MModuleConfigBuilder<WB> {

let config = self
.populate_logger(logger_enabled, logging_mask, logger_filter, module_name)
.populate_host_imports(host_imports, call_parameters_v0, call_parameters_v1)
.populate_host_imports(
host_imports,
call_parameters_v0,
call_parameters_v1,
call_parameters_v2,
)
.populate_wasi(wasi)?
.into_config();

Expand Down Expand Up @@ -124,8 +131,9 @@ impl<WB: WasmBackend> MModuleConfigBuilder<WB> {
fn populate_host_imports(
mut self,
host_imports: HashMap<HostAPIVersion, HashMap<String, HostImportDescriptor<WB>>>,
call_parameters_v0: Arc<Mutex<old_sdk_call_parameters::CallParameters>>,
call_parameters_v1: Arc<Mutex<CallParameters>>,
call_parameters_v0: Arc<Mutex<marine_call_parameters_v0::CallParameters>>,
call_parameters_v1: Arc<Mutex<marine_call_parameters_v1::CallParameters>>,
call_parameters_v2: Arc<Mutex<CallParameters>>,
) -> Self {
self.config.host_imports = host_imports;
self.config
Expand All @@ -146,6 +154,15 @@ impl<WB: WasmBackend> MModuleConfigBuilder<WB> {
create_call_parameters_import_v1(call_parameters_v1),
);

self.config
.host_imports
.entry(HostAPIVersion::V2)
.or_default()
.insert(
String::from("get_call_parameters"),
create_call_parameters_import_v2(call_parameters_v2),
);

self
}

Expand Down Expand Up @@ -205,15 +222,17 @@ impl<WB: WasmBackend> MModuleConfigBuilder<WB> {
pub(crate) fn make_marine_config<WB: WasmBackend>(
module_name: String,
marine_module_config: Option<MarineModuleConfig<WB>>,
call_parameters_v0: Arc<Mutex<old_sdk_call_parameters::CallParameters>>,
call_parameters_v1: Arc<Mutex<marine_rs_sdk::CallParameters>>,
call_parameters_v0: Arc<Mutex<marine_call_parameters_v0::CallParameters>>,
call_parameters_v1: Arc<Mutex<marine_call_parameters_v1::CallParameters>>,
call_parameters_v2: Arc<Mutex<marine_rs_sdk::CallParameters>>,
logger_filter: &LoggerFilter<'_>,
) -> MarineResult<MModuleConfig<WB>> {
MModuleConfigBuilder::new().build(
module_name,
marine_module_config,
call_parameters_v0,
call_parameters_v1,
call_parameters_v2,
logger_filter,
)
}
103 changes: 87 additions & 16 deletions marine/src/host_imports/call_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use std::sync::Arc;

/// Create the import intended for handling get_call_parameters SDK api.

pub(crate) fn create_call_parameters_import_v1<WB: WasmBackend>(
pub(crate) fn create_call_parameters_import_v2<WB: WasmBackend>(
call_parameters: Arc<Mutex<marine_rs_sdk::CallParameters>>, // TODO try to avoid using mutex
) -> HostImportDescriptor<WB> {
let call_parameters_closure = move |_ctx: &mut <WB as WasmBackend>::ImportCallContext<'_>,
Expand All @@ -51,8 +51,30 @@ pub(crate) fn create_call_parameters_import_v1<WB: WasmBackend>(
}
}

pub(crate) fn create_call_parameters_import_v1<WB: WasmBackend>(
call_parameters: Arc<Mutex<marine_call_parameters_v1::CallParameters>>, // TODO try to avoid using mutex
) -> HostImportDescriptor<WB> {
let call_parameters_closure = move |_ctx: &mut <WB as WasmBackend>::ImportCallContext<'_>,
_args: Vec<IValue>| {
let result = {
// a separate code block to unlock the mutex ASAP and to avoid double locking
crate::to_interface_value(call_parameters.lock().deref())
.unwrap_or_else(|_| panic!("CallParameters should be convertible to IValue"))
};

Some(result)
};

HostImportDescriptor {
host_exported_func: Box::new(call_parameters_closure),
argument_types: vec![],
output_type: Some(IType::Record(0)),
error_handler: None,
}
}

pub(crate) fn create_call_parameters_import_v0<WB: WasmBackend>(
call_parameters: Arc<Mutex<old_sdk_call_parameters::CallParameters>>, // TODO try to avoid using mutex
call_parameters: Arc<Mutex<marine_call_parameters_v0::CallParameters>>, // TODO try to avoid using mutex
) -> HostImportDescriptor<WB> {
let call_parameters_closure = move |_ctx: &mut <WB as WasmBackend>::ImportCallContext<'_>,
_args: Vec<IValue>| {
Expand All @@ -73,49 +95,98 @@ pub(crate) fn create_call_parameters_import_v0<WB: WasmBackend>(
}
}

pub(crate) fn call_parameters_v1_to_v0(
pub(crate) fn call_parameters_v2_to_v0(
call_parameters: marine_rs_sdk::CallParameters,
) -> old_sdk_call_parameters::CallParameters {
) -> marine_call_parameters_v0::CallParameters {
let marine_rs_sdk::CallParameters {
init_peer_id,
particle,
service_id,
service_creator_peer_id,
host_id,
particle_id,
tetraplets,
..
} = call_parameters;

old_sdk_call_parameters::CallParameters {
init_peer_id,
marine_call_parameters_v0::CallParameters {
init_peer_id: particle.init_peer_id,
service_id,
service_creator_peer_id,
host_id,
particle_id,
tetraplets: to_old_sdk_tetraplets(tetraplets),
particle_id: particle.id,
tetraplets: to_v0_sdk_tetraplets(tetraplets),
}
}

pub(crate) fn call_parameters_v2_to_v1(
call_parameters: marine_rs_sdk::CallParameters,
) -> marine_call_parameters_v1::CallParameters {
let marine_rs_sdk::CallParameters {
particle,
service_id,
service_creator_peer_id,
host_id,
tetraplets,
worker_id,
} = call_parameters;

marine_call_parameters_v1::CallParameters {
init_peer_id: particle.init_peer_id,
service_id,
service_creator_peer_id,
host_id,
worker_id,
particle_id: particle.id,
tetraplets: to_v1_sdk_tetraplets(tetraplets),
}
}

fn to_v0_sdk_tetraplets(
tetraplets: Vec<Vec<SecurityTetraplet>>,
) -> Vec<Vec<marine_call_parameters_v0::SecurityTetraplet>> {
tetraplets
.into_iter()
.map(|tetraplets| tetraplets.into_iter().map(to_v0_sdk_tetraplet).collect())
.collect()
}

fn to_v0_sdk_tetraplet(
tetraplet: SecurityTetraplet,
) -> marine_call_parameters_v0::SecurityTetraplet {
let SecurityTetraplet {
peer_pk,
service_id,
function_name,
json_path,
} = tetraplet;

marine_call_parameters_v0::SecurityTetraplet {
peer_pk,
service_id,
function_name,
json_path,
}
}

fn to_old_sdk_tetraplets(
fn to_v1_sdk_tetraplets(
tetraplets: Vec<Vec<SecurityTetraplet>>,
) -> Vec<Vec<old_sdk_call_parameters::SecurityTetraplet>> {
) -> Vec<Vec<marine_call_parameters_v1::SecurityTetraplet>> {
tetraplets
.into_iter()
.map(|tetraplets| tetraplets.into_iter().map(to_old_sdk_tetraplet).collect())
.map(|tetraplets| tetraplets.into_iter().map(to_v1_sdk_tetraplet).collect())
.collect()
}

fn to_old_sdk_tetraplet(
fn to_v1_sdk_tetraplet(
tetraplet: SecurityTetraplet,
) -> old_sdk_call_parameters::SecurityTetraplet {
) -> marine_call_parameters_v1::SecurityTetraplet {
let SecurityTetraplet {
peer_pk,
service_id,
function_name,
json_path,
} = tetraplet;

old_sdk_call_parameters::SecurityTetraplet {
marine_call_parameters_v1::SecurityTetraplet {
peer_pk,
service_id,
function_name,
Expand Down
6 changes: 4 additions & 2 deletions marine/src/host_imports/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ pub(crate) mod logger;
mod call_parameters;
mod mounted_binaries;

pub(crate) use call_parameters::create_call_parameters_import_v1;
pub(crate) use call_parameters::create_call_parameters_import_v0;
pub(crate) use call_parameters::call_parameters_v1_to_v0;
pub(crate) use call_parameters::create_call_parameters_import_v1;
pub(crate) use call_parameters::create_call_parameters_import_v2;
pub(crate) use call_parameters::call_parameters_v2_to_v0;
pub(crate) use call_parameters::call_parameters_v2_to_v1;
pub(crate) use mounted_binaries::create_mounted_binary_import;
26 changes: 19 additions & 7 deletions marine/src/marine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ use crate::MemoryStats;
use crate::module_loading::load_modules_from_fs;
use crate::host_imports::logger::LoggerFilter;
use crate::host_imports::logger::WASM_LOG_ENV_NAME;
use crate::host_imports::call_parameters_v1_to_v0;
use crate::host_imports::call_parameters_v2_to_v0;
use crate::host_imports::call_parameters_v2_to_v1;
use crate::json_to_marine_err;

use marine_wasm_backend_traits::WasmBackend;
Expand Down Expand Up @@ -61,10 +62,12 @@ pub struct Marine<WB: WasmBackend> {
core: MarineCore<WB>,

/// Parameters of call accessible by Wasm modules.
call_parameters_v0: Arc<Mutex<old_sdk_call_parameters::CallParameters>>,
call_parameters_v0: Arc<Mutex<marine_call_parameters_v0::CallParameters>>,

call_parameters_v1: Arc<Mutex<marine_call_parameters_v1::CallParameters>>,

/// Parameters of call accessible by Wasm modules.
call_parameters_v1: Arc<Mutex<CallParameters>>,
call_parameters_v2: Arc<Mutex<CallParameters>>,

/// Cached module interfaces by names.
module_interfaces_cache: HashMap<String, ModuleInterface>,
Expand Down Expand Up @@ -100,8 +103,9 @@ impl<WB: WasmBackend> Marine<WB> {
.total_memory_limit(config.total_memory_limit.unwrap_or(INFINITE_MEMORY_LIMIT))
.build();
let mut marine = MarineCore::new(core_config)?;
let call_parameters_v0 = Arc::<Mutex<old_sdk_call_parameters::CallParameters>>::default();
let call_parameters_v1 = Arc::<Mutex<CallParameters>>::default();
let call_parameters_v0 = Arc::<Mutex<marine_call_parameters_v0::CallParameters>>::default();
let call_parameters_v1 = Arc::<Mutex<marine_call_parameters_v1::CallParameters>>::default();
let call_parameters_v2 = Arc::<Mutex<CallParameters>>::default();

let modules_dir = config.modules_dir;

Expand All @@ -123,6 +127,7 @@ impl<WB: WasmBackend> Marine<WB> {
Some(module.config),
call_parameters_v0.clone(),
call_parameters_v1.clone(),
call_parameters_v2.clone(),
&logger_filter,
)?;

Expand All @@ -135,6 +140,7 @@ impl<WB: WasmBackend> Marine<WB> {
core: marine,
call_parameters_v0,
call_parameters_v1,
call_parameters_v2,
module_interfaces_cache: HashMap::new(),
})
}
Expand Down Expand Up @@ -281,14 +287,20 @@ impl<WB: WasmBackend> Marine<WB> {
fn update_call_parameters(&mut self, call_parameters: CallParameters) {
{
// a separate code block to unlock the mutex ASAP and to avoid double locking
let mut cp = self.call_parameters_v1.lock();
let mut cp = self.call_parameters_v2.lock();
*cp = call_parameters.clone();
}

{
// a separate code block to unlock the mutex ASAP and to avoid double locking
let mut cp = self.call_parameters_v0.lock();
*cp = call_parameters_v1_to_v0(call_parameters);
*cp = call_parameters_v2_to_v0(call_parameters.clone());
}

{
// a separate code block to unlock the mutex ASAP and to avoid double locking
let mut cp = self.call_parameters_v1.lock();
*cp = call_parameters_v2_to_v1(call_parameters);
}
}
}
Expand Down
Loading