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

Export wasmer::{BaseTunables, Tunables} #1924

Merged
merged 2 commits into from
Dec 15, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [#1851](https://github.com/wasmerio/wasmer/pull/1851) Improve test suite and documentation of the Wasmer C API
- [#1874](https://github.com/wasmerio/wasmer/pull/1874) Set `CompilerConfig` to be owned (following wasm-c-api)
- [#1880](https://github.com/wasmerio/wasmer/pull/1880) Remove cmake dependency for tests
- [#1924](https://github.com/wasmerio/wasmer/pull/1924) Rename reference implementation `wasmer::Tunables` to `wasmer::BaseTunables`. Export trait `wasmer_engine::Tunables` as `wasmer::Tunables`.

### Fixed

Expand Down
9 changes: 4 additions & 5 deletions examples/tunables_limit_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ use std::sync::Arc;
use wasmer::{
imports,
vm::{self, MemoryError, MemoryStyle, TableStyle, VMMemoryDefinition, VMTableDefinition},
wat2wasm, Instance, Memory, MemoryType, Module, Pages, Store, TableType, Target,
Tunables as ReferenceTunables,
wat2wasm, BaseTunables, Instance, Memory, MemoryType, Module, Pages, Store, TableType, Target,
Tunables,
};
use wasmer_compiler_cranelift::Cranelift;
use wasmer_engine::Tunables;
use wasmer_engine_jit::JIT;

/// A custom tunables that allows you to set a memory limit.
Expand All @@ -29,7 +28,7 @@ impl<T: Tunables> LimitingTunables<T> {
Self { limit, base }
}

/// Takes in input memory type as requested by the guest and sets
/// Takes an input memory type as requested by the guest and sets
/// a maximum if missing. The resulting memory type is final if
/// valid. However, this can produce invalid types, such that
/// validate_memory must be called before creating the memory.
Expand Down Expand Up @@ -147,7 +146,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// Here is where the fun begins

let base = ReferenceTunables::for_target(&Target::default());
let base = BaseTunables::for_target(&Target::default());
let tunables = LimitingTunables::new(base, Pages(24));

// Create a store, that holds the engine and our custom tunables
Expand Down
4 changes: 2 additions & 2 deletions lib/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ pub use crate::module::Module;
pub use crate::native::NativeFunc;
pub use crate::ptr::{Array, Item, WasmPtr};
pub use crate::store::{Store, StoreObject};
pub use crate::tunables::Tunables;
pub use crate::tunables::BaseTunables;
pub use crate::types::{
ExportType, ExternRef, ExternType, FunctionType, GlobalType, HostInfo, HostRef, ImportType,
MemoryType, Mutability, TableType, Val, ValType,
Expand All @@ -311,7 +311,7 @@ pub use wasmer_compiler::{
};
pub use wasmer_engine::{
ChainableNamedResolver, DeserializeError, Engine, Export, FrameInfo, LinkError, NamedResolver,
NamedResolverChain, Resolver, RuntimeError, SerializeError,
NamedResolverChain, Resolver, RuntimeError, SerializeError, Tunables,
};
pub use wasmer_types::{
Atomically, Bytes, ExportIndex, GlobalInit, LocalFunctionIndex, MemoryView, Pages, ValueType,
Expand Down
18 changes: 7 additions & 11 deletions lib/api/src/store.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::tunables::Tunables;
use crate::tunables::BaseTunables;
use std::fmt;
use std::sync::Arc;
#[cfg(all(feature = "compiler", feature = "engine"))]
use wasmer_compiler::CompilerConfig;
use wasmer_engine::Engine;
use wasmer_engine::Tunables as BaseTunables;
use wasmer_engine::{Engine, Tunables};

/// The store represents all global state that can be manipulated by
/// WebAssembly programs. It consists of the runtime representation
Expand All @@ -19,7 +18,7 @@ use wasmer_engine::Tunables as BaseTunables;
#[derive(Clone)]
pub struct Store {
engine: Arc<dyn Engine + Send + Sync>,
tunables: Arc<dyn BaseTunables + Send + Sync>,
tunables: Arc<dyn Tunables + Send + Sync>,
}

impl Store {
Expand All @@ -30,15 +29,12 @@ impl Store {
{
Self {
engine: engine.cloned(),
tunables: Arc::new(Tunables::for_target(engine.target())),
tunables: Arc::new(BaseTunables::for_target(engine.target())),
}
}

/// Creates a new `Store` with a specific [`Engine`] and [`Tunables`].
pub fn new_with_tunables<E>(
engine: &E,
tunables: impl BaseTunables + Send + Sync + 'static,
) -> Self
pub fn new_with_tunables<E>(engine: &E, tunables: impl Tunables + Send + Sync + 'static) -> Self
where
E: Engine + ?Sized,
{
Expand All @@ -49,7 +45,7 @@ impl Store {
}

/// Returns the [`Tunables`].
pub fn tunables(&self) -> &dyn BaseTunables {
pub fn tunables(&self) -> &dyn Tunables {
self.tunables.as_ref()
}

Expand Down Expand Up @@ -111,7 +107,7 @@ impl Default for Store {

let config = get_config();
let engine = get_engine(config);
let tunables = Tunables::for_target(engine.target());
let tunables = BaseTunables::for_target(engine.target());
Store {
engine: Arc::new(engine),
tunables: Arc::new(tunables),
Expand Down
19 changes: 13 additions & 6 deletions lib/api/src/tunables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ use std::ptr::NonNull;
use std::sync::Arc;
use target_lexicon::{OperatingSystem, PointerWidth};
use wasmer_compiler::Target;
use wasmer_engine::Tunables as BaseTunables;
use wasmer_engine::Tunables;
use wasmer_vm::MemoryError;
use wasmer_vm::{
LinearMemory, LinearTable, Memory, MemoryStyle, Table, TableStyle, VMMemoryDefinition,
VMTableDefinition,
};

/// Tunable parameters for WebAssembly compilation.
/// This is the reference implementation of the `Tunables` trait,
/// used by default.
///
/// You can use this as a template for creating a custom Tunables
/// implementation or use composition to wrap your Tunables around
/// this one. The later approach is demonstrated in the
/// tunables-limit-memory example.
#[derive(Clone)]
pub struct Tunables {
pub struct BaseTunables {
/// For static heaps, the size in wasm pages of the heap protected by bounds checking.
pub static_memory_bound: Pages,

Expand All @@ -24,8 +31,8 @@ pub struct Tunables {
pub dynamic_memory_offset_guard_size: u64,
}

impl Tunables {
/// Get the `Tunables` for a specific Target
impl BaseTunables {
/// Get the `BaseTunables` for a specific Target
pub fn for_target(target: &Target) -> Self {
let triple = target.triple();
let pointer_width: PointerWidth = triple.pointer_width().unwrap();
Expand Down Expand Up @@ -61,7 +68,7 @@ impl Tunables {
}
}

impl BaseTunables for Tunables {
impl Tunables for BaseTunables {
/// Get a `MemoryStyle` for the provided `MemoryType`
fn memory_style(&self, memory: &MemoryType) -> MemoryStyle {
// A heap with a maximum that doesn't exceed the static memory bound specified by the
Expand Down Expand Up @@ -148,7 +155,7 @@ mod tests {

#[test]
fn memory_style() {
let tunables = Tunables {
let tunables = BaseTunables {
static_memory_bound: Pages(2048),
static_memory_offset_guard_size: 128,
dynamic_memory_offset_guard_size: 256,
Expand Down