Skip to content

Commit

Permalink
Merge branch 'fix-runtime-c-api-header-preprocessor-clang' of github.…
Browse files Browse the repository at this point in the history
…com:Hywan/wasmer into fix-runtime-c-api-header-preprocessor-clang
  • Loading branch information
Hywan committed Nov 14, 2019
2 parents 987b076 + c43a7c7 commit 074a8f9
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 48 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/clif-backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ description = "Wasmer runtime Cranelift compiler backend"
license = "MIT"
authors = ["The Wasmer Engineering Team <engineering@wasmer.io>"]
repository = "https://github.com/wasmerio/wasmer"
keywords = ["wasm", "webassembly", "compiler", "JIT", "AOT"]
categories = ["wasm"]
edition = "2018"
readme = "README.md"

Expand Down
2 changes: 2 additions & 0 deletions lib/emscripten/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ description = "Wasmer runtime emscripten implementation library"
license = "MIT"
authors = ["The Wasmer Engineering Team <engineering@wasmer.io>"]
repository = "https://github.com/wasmerio/wasmer"
keywords = ["wasm", "webassembly", "ABI", "emscripten", "posix"]
categories = ["wasm"]
edition = "2018"

[dependencies]
Expand Down
4 changes: 4 additions & 0 deletions lib/llvm-backend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
[package]
name = "wasmer-llvm-backend"
version = "0.10.1"
license = "MIT"
authors = ["The Wasmer Engineering Team <engineering@wasmer.io>"]
repository = "https://github.com/wasmerio/wasmer"
keywords = ["wasm", "webassembly", "compiler", "JIT", "llvm"]
categories = ["wasm"]
edition = "2018"
readme = "README.md"

Expand Down
2 changes: 2 additions & 0 deletions lib/middleware-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ repository = "https://github.com/wasmerio/wasmer"
description = "Wasmer runtime common middlewares"
license = "MIT"
authors = ["The Wasmer Engineering Team <engineering@wasmer.io>"]
keywords = ["wasm", "webassembly", "middleware", "metering"]
categories = ["wasm"]
edition = "2018"

[dependencies]
Expand Down
2 changes: 2 additions & 0 deletions lib/runtime-c-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ description = "Wasmer C API library"
license = "MIT"
authors = ["The Wasmer Engineering Team <engineering@wasmer.io>"]
repository = "https://github.com/wasmerio/wasmer"
keywords = ["wasm", "webassembly", "runtime"]
categories = ["wasm"]
edition = "2018"
readme = "README.md"

Expand Down
5 changes: 2 additions & 3 deletions lib/runtime-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ description = "Wasmer runtime core library"
license = "MIT"
authors = ["The Wasmer Engineering Team <engineering@wasmer.io>"]
repository = "https://github.com/wasmerio/wasmer"
keywords = ["wasm", "webassembly", "runtime"]
categories = ["wasm"]
edition = "2018"

[dependencies]
Expand Down Expand Up @@ -42,9 +44,6 @@ version = "0.8"
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["memoryapi"] }

[dev-dependencies]
field-offset = "0.1"

[build-dependencies]
blake2b_simd = "0.5"
rustc_version = "0.2"
Expand Down
4 changes: 0 additions & 4 deletions lib/runtime-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
#![doc(html_favicon_url = "https://wasmer.io/static/icons/favicon.ico")]
#![doc(html_logo_url = "https://avatars3.githubusercontent.com/u/44205449?s=200&v=4")]

#[cfg(test)]
#[macro_use]
extern crate field_offset;

#[macro_use]
extern crate serde_derive;

Expand Down
44 changes: 41 additions & 3 deletions lib/runtime-core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,47 @@ macro_rules! trace {
}

/// Helper macro to create a new `Func` object using the provided function pointer.
///
/// # Usage
///
/// Function pointers or closures are supported. Closures can capture
/// their environment (with `move). The first parameter is likely to
/// be of kind `vm::Ctx`, though it can be optional.
///
/// ```
/// # use wasmer_runtime_core::{imports, func};
/// # use wasmer_runtime_core::vm;
///
/// // A function that has access to `vm::Ctx`.
/// fn func_with_vmctx(_: &mut vm::Ctx, n: i32) -> i32 {
/// n
/// }
///
/// // A function that cannot access `vm::Ctx`.
/// fn func(n: i32) -> i32 {
/// n
/// }
///
/// let i = 7;
///
/// let import_object = imports! {
/// "env" => {
/// "foo" => func!(func_with_vmctx),
/// "bar" => func!(func),
/// // A closure with a captured environment, and an access to `vm::Ctx`.
/// "baz" => func!(move |_: &mut vm::Ctx, n: i32| -> i32 {
/// n + i
/// }),
/// // A closure without a captured environment, and no access to `vm::Ctx`.
/// "qux" => func!(|n: i32| -> i32 {
/// n
/// }),
/// },
/// };
/// ```
#[macro_export]
macro_rules! func {
($func:path) => {{
($func:expr) => {{
$crate::Func::new($func)
}};
}
Expand All @@ -56,12 +94,12 @@ macro_rules! func {
///
/// [`ImportObject`]: struct.ImportObject.html
///
/// # Note:
/// # Note
/// The `import` macro currently only supports
/// importing functions.
///
///
/// # Usage:
/// # Usage
/// ```
/// # use wasmer_runtime_core::{imports, func};
/// # use wasmer_runtime_core::vm::Ctx;
Expand Down
120 changes: 89 additions & 31 deletions lib/runtime-core/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,78 +754,136 @@ mod vm_offset_tests {
Anyfunc, Ctx, FuncCtx, ImportedFunc, InternalCtx, LocalGlobal, LocalMemory, LocalTable,
};

// Inspired by https://internals.rust-lang.org/t/discussion-on-offset-of/7440/2.
macro_rules! offset_of {
($struct:path, $field:ident) => {{
fn offset() -> usize {
use std::mem;

let structure = mem::MaybeUninit::<$struct>::uninit();

let &$struct {
$field: ref field, ..
} = unsafe { &*structure.as_ptr() };

let offset =
(field as *const _ as usize).wrapping_sub(&structure as *const _ as usize);

assert!((0..=mem::size_of_val(&structure)).contains(&offset));

offset
}

offset()
}};
}

#[test]
fn offset_of() {
use std::{mem, ptr::NonNull};

struct S0;

#[repr(C)]
struct S1 {
f1: u8,
f2: u16,
f3: u32,
f4: u64,
f5: u128,
f6: f32,
f7: f64,
f8: NonNull<S0>,
f9: Option<NonNull<S0>>,
f10: *mut S0,
z: u8,
}

assert_eq!(offset_of!(S1, f1), 0);
assert_eq!(offset_of!(S1, f2), 2);
assert_eq!(offset_of!(S1, f3), 4);
assert_eq!(offset_of!(S1, f4), 8);
assert_eq!(offset_of!(S1, f5), 16);
assert_eq!(offset_of!(S1, f6), 32);
assert_eq!(offset_of!(S1, f7), 40);
assert_eq!(offset_of!(S1, f8), 40 + mem::size_of::<usize>());
assert_eq!(offset_of!(S1, f9), 48 + mem::size_of::<usize>());
assert_eq!(offset_of!(S1, f10), 56 + mem::size_of::<usize>());
assert_eq!(offset_of!(S1, z), 64 + mem::size_of::<usize>());
}

#[test]
fn vmctx() {
assert_eq!(0usize, offset_of!(Ctx => internal).get_byte_offset(),);
assert_eq!(0usize, offset_of!(Ctx, internal));

assert_eq!(
Ctx::offset_memories() as usize,
offset_of!(InternalCtx => memories).get_byte_offset(),
offset_of!(InternalCtx, memories),
);

assert_eq!(
Ctx::offset_tables() as usize,
offset_of!(InternalCtx => tables).get_byte_offset(),
offset_of!(InternalCtx, tables),
);

assert_eq!(
Ctx::offset_globals() as usize,
offset_of!(InternalCtx => globals).get_byte_offset(),
offset_of!(InternalCtx, globals),
);

assert_eq!(
Ctx::offset_imported_memories() as usize,
offset_of!(InternalCtx => imported_memories).get_byte_offset(),
offset_of!(InternalCtx, imported_memories),
);

assert_eq!(
Ctx::offset_imported_tables() as usize,
offset_of!(InternalCtx => imported_tables).get_byte_offset(),
offset_of!(InternalCtx, imported_tables),
);

assert_eq!(
Ctx::offset_imported_globals() as usize,
offset_of!(InternalCtx => imported_globals).get_byte_offset(),
offset_of!(InternalCtx, imported_globals),
);

assert_eq!(
Ctx::offset_imported_funcs() as usize,
offset_of!(InternalCtx => imported_funcs).get_byte_offset(),
offset_of!(InternalCtx, imported_funcs),
);

assert_eq!(
Ctx::offset_intrinsics() as usize,
offset_of!(InternalCtx => intrinsics).get_byte_offset(),
offset_of!(InternalCtx, intrinsics),
);

assert_eq!(
Ctx::offset_stack_lower_bound() as usize,
offset_of!(InternalCtx => stack_lower_bound).get_byte_offset(),
offset_of!(InternalCtx, stack_lower_bound),
);

assert_eq!(
Ctx::offset_memory_base() as usize,
offset_of!(InternalCtx => memory_base).get_byte_offset(),
offset_of!(InternalCtx, memory_base),
);

assert_eq!(
Ctx::offset_memory_bound() as usize,
offset_of!(InternalCtx => memory_bound).get_byte_offset(),
offset_of!(InternalCtx, memory_bound),
);

assert_eq!(
Ctx::offset_internals() as usize,
offset_of!(InternalCtx => internals).get_byte_offset(),
offset_of!(InternalCtx, internals),
);

assert_eq!(
Ctx::offset_interrupt_signal_mem() as usize,
offset_of!(InternalCtx => interrupt_signal_mem).get_byte_offset(),
offset_of!(InternalCtx, interrupt_signal_mem),
);

assert_eq!(
Ctx::offset_local_functions() as usize,
offset_of!(Ctx => local_functions).get_byte_offset(),
offset_of!(Ctx, local_functions),
);
}

Expand All @@ -838,60 +896,60 @@ mod vm_offset_tests {

#[test]
fn imported_func() {
assert_eq!(ImportedFunc::offset_func() as usize, 0);
assert_eq!(
ImportedFunc::offset_func() as usize,
offset_of!(ImportedFunc, func),
);

assert_eq!(ImportedFunc::offset_func_ctx() as usize, 8);
assert_eq!(
ImportedFunc::offset_func_ctx() as usize,
offset_of!(ImportedFunc, func_ctx),
);
}

#[test]
fn local_table() {
assert_eq!(
LocalTable::offset_base() as usize,
offset_of!(LocalTable => base).get_byte_offset(),
offset_of!(LocalTable, base),
);

assert_eq!(
LocalTable::offset_count() as usize,
offset_of!(LocalTable => count).get_byte_offset(),
offset_of!(LocalTable, count),
);
}

#[test]
fn local_memory() {
assert_eq!(
LocalMemory::offset_base() as usize,
offset_of!(LocalMemory => base).get_byte_offset(),
offset_of!(LocalMemory, base),
);

assert_eq!(
LocalMemory::offset_bound() as usize,
offset_of!(LocalMemory => bound).get_byte_offset(),
offset_of!(LocalMemory, bound),
);
}

#[test]
fn local_global() {
assert_eq!(
LocalGlobal::offset_data() as usize,
offset_of!(LocalGlobal => data).get_byte_offset(),
offset_of!(LocalGlobal, data),
);
}

#[test]
fn cc_anyfunc() {
assert_eq!(
Anyfunc::offset_func() as usize,
offset_of!(Anyfunc => func).get_byte_offset(),
);
assert_eq!(Anyfunc::offset_func() as usize, offset_of!(Anyfunc, func),);

assert_eq!(
Anyfunc::offset_vmctx() as usize,
offset_of!(Anyfunc => ctx).get_byte_offset(),
);
assert_eq!(Anyfunc::offset_vmctx() as usize, offset_of!(Anyfunc, ctx),);

assert_eq!(
Anyfunc::offset_sig_id() as usize,
offset_of!(Anyfunc => sig_id).get_byte_offset(),
offset_of!(Anyfunc, sig_id),
);
}
}
Expand Down
Loading

0 comments on commit 074a8f9

Please sign in to comment.