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!: add memory limiting #391

Merged
merged 36 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
1f3e70d
memory limits iteration 1
ValeryAntopol Oct 11, 2023
ac930e6
correct counting
ValeryAntopol Oct 17, 2023
d698683
update to wasmtime 13.0.0
ValeryAntopol Oct 18, 2023
df6ca3b
Merge branch 'feat/update-wasmtime' into feat/memory-limits
ValeryAntopol Oct 19, 2023
d9097f4
wip stats recording
ValeryAntopol Oct 27, 2023
2b36ced
adding memory limit tests
ValeryAntopol Oct 31, 2023
88a6306
fix build
ValeryAntopol Nov 1, 2023
f934d19
fix build
ValeryAntopol Nov 1, 2023
f3a6f33
fix build
ValeryAntopol Nov 1, 2023
486babb
fix tests
ValeryAntopol Nov 1, 2023
0ce8b17
add configs
ValeryAntopol Nov 1, 2023
6ec2bb8
Merge branch 'master' into feat/memory-limits
ValeryAntopol Nov 1, 2023
96df441
update Cargo.lock
ValeryAntopol Nov 1, 2023
57fdba4
self-review fixes
ValeryAntopol Nov 1, 2023
dad082a
self-review fixes, cleanup
ValeryAntopol Nov 1, 2023
e5a6b26
fix tests build
ValeryAntopol Nov 1, 2023
0a2c57b
fix tests, remove wee_alloc form lockfile
ValeryAntopol Nov 2, 2023
5df871c
fix build
ValeryAntopol Nov 2, 2023
cbc8ad6
Merge branch 'master' into feat/memory-limits
ValeryAntopol Nov 3, 2023
3cf65a3
format tests
ValeryAntopol Nov 3, 2023
890e60d
pr fixes
ValeryAntopol Nov 3, 2023
25a8b60
Merge branch 'master' into feat/memory-limits
ValeryAntopol Nov 3, 2023
80d4a18
make tests more robust
ValeryAntopol Nov 3, 2023
fea77ca
pr fixes
ValeryAntopol Nov 3, 2023
dcf5378
Add OOM check into marine-runtime
ValeryAntopol Nov 28, 2023
aa9b234
Use new error variant in tests
ValeryAntopol Nov 28, 2023
3c1c766
add handy error message
ValeryAntopol Nov 28, 2023
909ad67
fix error msg
ValeryAntopol Nov 28, 2023
68431c2
fix tests
ValeryAntopol Nov 28, 2023
aee3964
check for OOM more precisely
ValeryAntopol Nov 28, 2023
bd8c444
improve readability
ValeryAntopol Nov 28, 2023
f158def
add "inifinity" memory limit, make memory limit mandatory, stop persi…
ValeryAntopol Dec 6, 2023
46e957a
add forgotten allocation stats clear
ValeryAntopol Dec 6, 2023
ec2e825
pr fixes
ValeryAntopol Dec 12, 2023
8594cf8
pr fixes
ValeryAntopol Dec 12, 2023
9aba97e
use bytesize in tests
ValeryAntopol Dec 13, 2023
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
68 changes: 39 additions & 29 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ members = [
"marine",
"marine/tests/wasm_tests/arguments_passing",
"marine/tests/wasm_tests/arrays_passing",
"marine/tests/wasm_tests/memory_limiting",
"marine/tests/wasm_tests/records_passing",
"marine/tests/wasm_tests/wasi",
"marine-js",
Expand Down
49 changes: 36 additions & 13 deletions core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ use std::path::PathBuf;
use std::collections::HashMap;
use std::collections::HashSet;

// 65536*1600 ~ 100 Mb (Wasm page size is 64 Kb)
const DEFAULT_HEAP_PAGES_COUNT: u32 = 1600;

pub type ErrorHandler =
Option<Box<dyn Fn(&HostImportError) -> Option<IValue> + Sync + Send + 'static>>;
pub type HostExportedFunc<WB> = Box<
Expand Down Expand Up @@ -56,10 +53,6 @@ pub struct HostImportDescriptor<WB: WasmBackend> {
}

pub struct MModuleConfig<WB: WasmBackend> {
/// Maximum number of Wasm memory pages that loaded module can use.
/// Each Wasm page is 65536 bytes long.
pub max_heap_pages_count: u32,

/// Import object that will be used in module instantiation process.
pub raw_imports: HashMap<String, RawImportCreator<WB>>,

Expand All @@ -74,7 +67,6 @@ impl<WB: WasmBackend> Default for MModuleConfig<WB> {
fn default() -> Self {
// some reasonable defaults
Self {
max_heap_pages_count: DEFAULT_HEAP_PAGES_COUNT,
raw_imports: HashMap::new(),
host_imports: HashMap::new(),
wasi_parameters: WasiParameters::default(),
Expand All @@ -86,11 +78,6 @@ impl<WB: WasmBackend> Default for MModuleConfig<WB> {

#[allow(dead_code)]
impl<WB: WasmBackend> MModuleConfig<WB> {
pub fn with_mem_pages_count(mut self, mem_pages_count: u32) -> Self {
self.max_heap_pages_count = mem_pages_count;
self
}

pub fn with_wasi_envs(mut self, envs: HashMap<String, String>) -> Self {
self.wasi_parameters.envs = envs;
self
Expand All @@ -106,3 +93,39 @@ impl<WB: WasmBackend> MModuleConfig<WB> {
self
}
}

pub struct MarineCoreConfig {
pub(crate) memory_limit: u64,
}

impl Default for MarineCoreConfig {
fn default() -> Self {
Self {
memory_limit: DEFAULT_MEMORY_LIMIT,
}
}
}

pub const DEFAULT_MEMORY_LIMIT: u64 = 0xFFFFFFFFFFFFFFFF;

#[derive(Default, Debug)]
pub struct MarineCoreConfigBuilder {
memory_limit: Option<u64>,
}

impl MarineCoreConfigBuilder {
pub fn new() -> Self {
Self::default()
}

pub fn memory_limit(mut self, memory_limit: u64) -> Self {
self.memory_limit = Some(memory_limit);
self
}

pub fn build(self) -> MarineCoreConfig {
MarineCoreConfig {
memory_limit: self.memory_limit.unwrap_or(DEFAULT_MEMORY_LIMIT),
}
}
}
3 changes: 3 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ mod module;
mod memory_statistic;

pub use crate::marine_core::MModuleInterface;
pub use config::MarineCoreConfig;
pub use config::MarineCoreConfigBuilder;
pub use config::DEFAULT_MEMORY_LIMIT;
pub use errors::MError;
pub use host_imports::HostImportError;
pub use module::IValue;
Expand Down
13 changes: 9 additions & 4 deletions core/src/marine_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

use super::generic::*;
use crate::config::MarineCoreConfig;
use crate::module::MModule;
use crate::module::MRecordTypes;
use crate::{IRecordType, IValue, MemoryStats, MError, MFunctionSignature, ModuleMemoryStat, MResult};
Expand Down Expand Up @@ -60,9 +61,10 @@ pub struct MarineCore<WB: WasmBackend> {
}

impl<WB: WasmBackend> MarineCore<WB> {
pub fn new() -> MResult<Self> {
pub fn new(config: MarineCoreConfig) -> MResult<Self> {
let wasm_backend = WB::new()?;
let store = <WB as WasmBackend>::Store::new(&wasm_backend);
let mut store = <WB as WasmBackend>::Store::new(&wasm_backend);
store.set_memory_limit(config.memory_limit);
Ok(Self {
modules: HashMap::new(),
wasm_backend,
Expand Down Expand Up @@ -184,12 +186,15 @@ impl<WB: WasmBackend> MarineCore<WB> {
ModuleMemoryStat::new(
module_name,
module.memory_size(&mut self.store.borrow_mut().as_context_mut()),
module.max_memory_size(),
)
})
.collect::<Vec<_>>();
let allocation_stats = self.store.borrow_mut().report_memory_allocation_stats();
MemoryStats::new(records, allocation_stats)
}

records.into()
pub fn clear_allocation_stats(&mut self) {
self.store.borrow_mut().clear_allocation_stats()
}

fn get_module_interface(module: &MModule<WB>) -> MModuleInterface<'_> {
Expand Down
Loading
Loading