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

WASIX Preparation #3426

Merged
merged 65 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
5109a40
WASIX Preparation
theduke Dec 15, 2022
838bfe2
Make Compiler::name() optional
theduke Dec 15, 2022
2cd094b
Remove un-used Import/ExportError variant SerializationFailed
theduke Dec 15, 2022
7c67905
Rewrite some use statements
theduke Dec 15, 2022
7f63cad
Revert WasmPtr breaking changes
theduke Dec 15, 2022
7503fda
Remove module_start from ArtifactBuild::new, provide builder instead
theduke Dec 15, 2022
27627c0
Fix compiler CLI ArtifactBuild construction
theduke Dec 15, 2022
9b6c0bb
Restore C api wasi from master
theduke Dec 15, 2022
cd5dfe6
Fix various clippy lints
theduke Dec 15, 2022
0719b57
Fix From<> implementation
theduke Dec 15, 2022
77f11d2
chore: Remove unused function
theduke Dec 15, 2022
283e53f
Fix musl build
theduke Dec 15, 2022
af31383
Use byte slice for constructing JS modules
theduke Dec 15, 2022
d66fedf
Replace complicated type with type alias
theduke Dec 15, 2022
117ef3c
Document a type + add TODO
theduke Dec 15, 2022
7ac71bc
Remove redundant commented out function
theduke Dec 15, 2022
7884891
Feature gate copy_file_range_impl on unix
theduke Dec 16, 2022
01477d4
tests: Disable pointless test-js-core
theduke Dec 16, 2022
a298f58
ci: Disable make test-js-core test
theduke Dec 16, 2022
7940dd7
Re-implement copy_file_range function + add test
theduke Dec 19, 2022
11774ee
Gate copy_file_range test on unix envs
theduke Dec 19, 2022
9c12b87
Rename fork() to duplicate()
theduke Dec 19, 2022
da8e4a1
chore: Remove redunant code change
theduke Dec 19, 2022
bc78b91
Revert toolchain change from 1.63 to 1.64
theduke Dec 19, 2022
87b1f0b
Merge branch 'master' into wasix-core-changes
theduke Dec 19, 2022
b8a276a
Bumped rust toolchain to 1.64 for all CI steps
ptitSeb Dec 20, 2022
bc140d4
Bumped rust-toolchain too
ptitSeb Dec 20, 2022
6149733
Added iter_globals and set_global to StoreObjects
ptitSeb Dec 21, 2022
6eba960
Rename to and fixed assert
ptitSeb Dec 21, 2022
de443dc
Remove StoreSnapshot functionality
theduke Dec 22, 2022
9607291
Remove un-needed feature flags for DynamicFunction
theduke Dec 22, 2022
019ea5f
chore: Remove allow attribute
theduke Dec 22, 2022
cfa3bf4
Merge remote-tracking branch 'origin/master' into wasix-core-changes
theduke Dec 22, 2022
a499dea
Bump webc to 4.0.0
theduke Dec 22, 2022
af16422
Remove un-needed is_ok() methods
theduke Dec 22, 2022
422dfc5
Add cargo deny license exception for webc 4.0
theduke Dec 22, 2022
4a71dbd
Revert Mmap FD implementation
theduke Dec 27, 2022
bd3c3e1
Remove redundant verbose feature from compiler-cranelift
theduke Dec 27, 2022
fff72e4
Merge remote-tracking branch 'origin/master' into wasix-core-changes
theduke Dec 27, 2022
fdce8a6
chore: Formatting...
theduke Dec 28, 2022
87d6fb8
Fix webc Bindings usage
theduke Dec 28, 2022
4f2948c
Add iter_globals and set_global_unchecked to js Store
theduke Dec 28, 2022
0de8340
Fix webc runner exports detection
theduke Dec 28, 2022
cbce9b8
Remove unused import (dependent on feature flags)
theduke Dec 28, 2022
3f14b00
Fix Mmap creation on Windows
theduke Dec 28, 2022
b5b0d04
Merge remote-tracking branch 'origin/master' into wasix-core-changes
theduke Jan 2, 2023
b0d118d
ci: Disable test-js-core test
theduke Jan 2, 2023
d45f58d
ci: Use 1.64 toolchain
theduke Jan 2, 2023
bf2f855
Split js::Instance::from_module_and_instance externs/memory tests in …
ptitSeb Jan 2, 2023
9f7822d
Rename js::Instance::check_memory to ensure_memory_export
ptitSeb Jan 2, 2023
332ad85
Added TODO on asynctify code, linking to ticket #3451
ptitSeb Jan 3, 2023
6a42c2b
Merge branch 'master' into wasix-core-changes
ptitSeb Jan 3, 2023
a050376
[JS] Rmoved js-serializable-module feature from wasm-types-polyfill
ptitSeb Jan 4, 2023
212f836
Missed some sources to add a TODO mark about asynctify
ptitSeb Jan 4, 2023
9fbc7f4
Removed default Compiler::name() implementation
ptitSeb Jan 4, 2023
5b5d032
Fixed linter
ptitSeb Jan 4, 2023
8d7190b
Changed TODO mark about asyncify
ptitSeb Jan 5, 2023
b126849
Added one more TODO, about the imported/exported memory hack on js
ptitSeb Jan 5, 2023
60d1fcf
Fixed linter
ptitSeb Jan 5, 2023
26b43fe
Merge branch 'master' into wasix-core-changes
ptitSeb Jan 5, 2023
87eb05d
Merge branch 'master' into wasix-core-changes
ptitSeb Jan 16, 2023
36048fa
Merge branch 'master' into wasix-core-changes
ptitSeb Jan 16, 2023
e73f195
Fixed error message in test-integration-cli-ci
ptitSeb Jan 16, 2023
4c5b773
Merge branch 'master' into wasix-core-changes
ptitSeb Jan 16, 2023
e9edb7e
chore; Add some explanatory comments on allow(unused_unsafe) annotations
theduke Jan 16, 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
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: 1.63
toolchain: 1.64
- name: Configure cargo data directory
# After this point, all cargo registry and crate data is stored in
# $GITHUB_WORKSPACE/.cargo_home. This allows us to cache only the files
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: 1.63
toolchain: 1.64
target: ${{ matrix.target }}
- uses: Swatinem/rust-cache@v1
if: matrix.use_sccache != true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/cloudcompiler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: 1.63
toolchain: 1.64
target: ${{ matrix.target }}
- name: Install wasm32-wasi target
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/documentation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: 1.63
toolchain: 1.64
- name: Install LLVM
shell: bash
run: |
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,11 @@ jobs:
- name: make test-js
run: |
make test-js
- name: make test-js-core
run: |
make test-js-core

# The no_std functionality doesn't work at the moment - no point in testing it.
# - name: make test-js-core
# run: |
# make test-js-core

test_wasm_build:
name: Test wasm build
Expand Down
7 changes: 5 additions & 2 deletions Cargo.lock

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

6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,10 @@ test-examples: test-stage-5-test-examples test-stage-6-test-examples-release

test-js: test-js-api test-js-wasi

test-js-core:
cd lib/api && wasm-pack test --node -- --no-default-features --features js,core,wasm-types-polyfill,wat
# TODO: disabled because the no-std / core feature doesn't actually work at the moment.
# See https://github.com/wasmerio/wasmer/issues/3429
# test-js-core:
# cd lib/api && wasm-pack test --node -- --no-default-features --features js,core,wasm-types-polyfill,wat

test-js-api:
cd lib/api && wasm-pack test --node -- --no-default-features --features js-default,wat
Expand Down
18 changes: 16 additions & 2 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ notice = "warn"
# output a note when they are encountered.
ignore = [
#"RUSTSEC-0000-0000",

]
# If this is true, then cargo deny will use the git executable to fetch advisory database.
# If this is false, then it uses a built-in git library.
# Setting this to true can be helpful if you have special authentication requirements that cargo-deny does not support.
# See Git Authentication for more information about setting up git authentication.
git-fetch-with-cli = true
# Threshold for security vulnerabilities, any vulnerability with a CVSS score
# lower than the range specified will be ignored. Note that ignored advisories
# will still output a note when they are encountered.
Expand Down Expand Up @@ -105,13 +111,21 @@ confidence-threshold = 0.8
exceptions = [
# Each entry is the crate and version constraint, and its specific allow
# list
{ allow = ["LicenseRef-LICENSE.txt"], name = "webc", version = "*" },
]



# Some crates don't have (easily) machine readable licensing information,
# adding a clarification entry for it allows you to manually specify the
# licensing information
#[[licenses.clarify]]
[[licenses.clarify]]
name = "webc"
version = "*"
expression = "BSL-1.0"
license-files = [
{ path = "LICENSE.txt", hash = 0xa2180a97 }
]

# The name of the crate the clarification applies to
#name = "ring"
# The optional version constraint for the crate
Expand Down
3 changes: 2 additions & 1 deletion lib/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ indexmap = { version = "1.6" }
cfg-if = "1.0"
thiserror = "1.0"
more-asserts = "0.2"
derivative = { version = "^2" }
bytes = "1"
# - Optional shared dependencies.
wat = { version = "1.0", optional = true }
Expand Down Expand Up @@ -117,7 +118,7 @@ jit = ["engine"]
js = ["wasm-bindgen", "js-sys"]
js-default = ["js", "std", "wasm-types-polyfill"]

wasm-types-polyfill = ["js", "wasmparser"]
wasm-types-polyfill = ["js", "wasmparser", "js-serializable-module" ]
theduke marked this conversation as resolved.
Show resolved Hide resolved

js-serializable-module = []

Expand Down
50 changes: 50 additions & 0 deletions lib/api/src/into_bytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use bytes::Bytes;
use std::borrow::Cow;

/// Convert binary data into [`bytes::Bytes`].
pub trait IntoBytes {
/// Convert binary data into [`bytes::Bytes`].
fn into_bytes(self) -> Bytes;
}

impl IntoBytes for Bytes {
fn into_bytes(self) -> Bytes {
self
}
}

impl IntoBytes for Vec<u8> {
fn into_bytes(self) -> Bytes {
Bytes::from(self)
}
}

impl IntoBytes for &Vec<u8> {
fn into_bytes(self) -> Bytes {
Bytes::from(self.clone())
}
}

impl IntoBytes for &[u8] {
fn into_bytes(self) -> Bytes {
Bytes::from(self.to_vec())
}
}

impl<const N: usize> IntoBytes for &[u8; N] {
fn into_bytes(self) -> Bytes {
Bytes::from(self.to_vec())
}
}

impl IntoBytes for &str {
fn into_bytes(self) -> Bytes {
Bytes::from(self.as_bytes().to_vec())
}
}

impl IntoBytes for Cow<'_, [u8]> {
fn into_bytes(self) -> Bytes {
Bytes::from(self.to_vec())
}
}
56 changes: 54 additions & 2 deletions lib/api/src/js/export.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use crate::js::error::WasmError;
use crate::js::store::{AsStoreMut, AsStoreRef, InternalStoreHandle};
use crate::js::wasm_bindgen_polyfill::Global;
use crate::MemoryView;
use js_sys::Function;
use js_sys::WebAssembly::{Memory, Table};
use serde::{Deserialize, Serialize};
use std::fmt;
#[cfg(feature = "tracing")]
use tracing::trace;
use wasm_bindgen::{JsCast, JsValue};
use wasmer_types::{ExternType, FunctionType, GlobalType, MemoryType, TableType, WASM_PAGE_SIZE};
use wasmer_types::{
ExternType, FunctionType, GlobalType, MemoryError, MemoryType, Pages, TableType, WASM_PAGE_SIZE,
};

/// Represents linear memory that is managed by the javascript runtime
#[derive(Clone, Debug, PartialEq)]
Expand All @@ -25,7 +30,8 @@ struct DummyBuffer {
}

impl VMMemory {
pub(crate) fn new(memory: Memory, ty: MemoryType) -> Self {
/// Creates a new memory directly from a WebAssembly javascript object
pub fn new(memory: Memory, ty: MemoryType) -> Self {
Self { memory, ty }
}

Expand All @@ -45,6 +51,52 @@ impl VMMemory {
pub(crate) fn try_clone(&self) -> Option<VMMemory> {
Some(self.clone())
}

/// Copies this memory to a new memory
pub fn duplicate(&self) -> Result<VMMemory, wasmer_types::MemoryError> {
let new_memory = crate::Memory::new_internal(self.ty.clone())?;

#[cfg(feature = "tracing")]
trace!("memory copy started");

let src = MemoryView::new_raw(&self.memory);
let amount = src.data_size() as usize;
let mut dst = MemoryView::new_raw(&new_memory);
let dst_size = dst.data_size() as usize;

if amount > dst_size {
let delta = amount - dst_size;
let pages = ((delta - 1) / WASM_PAGE_SIZE) + 1;

let our_js_memory: &crate::js::externals::memory::JSMemory =
JsCast::unchecked_from_js_ref(&new_memory);
our_js_memory.grow(pages as u32).map_err(|err| {
if err.is_instance_of::<js_sys::RangeError>() {
let cur_pages = dst_size;
MemoryError::CouldNotGrow {
current: Pages(cur_pages as u32),
attempted_delta: Pages(pages as u32),
}
} else {
MemoryError::Generic(err.as_string().unwrap())
}
})?;

dst = MemoryView::new_raw(&new_memory);
}

src.copy_to_memory(amount as u64, &dst).map_err(|err| {
wasmer_types::MemoryError::Generic(format!("failed to copy the memory - {}", err))
})?;

#[cfg(feature = "tracing")]
trace!("memory copy finished (size={})", dst.size().bytes().0);

Ok(Self {
memory: new_memory,
ty: self.ty.clone(),
})
}
}

#[derive(Clone, Debug, PartialEq)]
Expand Down
62 changes: 50 additions & 12 deletions lib/api/src/js/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ pub struct Function {
pub(crate) handle: StoreHandle<VMFunction>,
}

impl From<StoreHandle<VMFunction>> for Function {
fn from(handle: StoreHandle<VMFunction>) -> Self {
Self { handle }
}
}

impl Function {
/// Creates a new host `Function` (dynamic) with the provided signature.
///
Expand Down Expand Up @@ -393,6 +399,13 @@ impl Function {
store: &mut impl AsStoreMut,
params: &[Value],
) -> Result<Box<[Value]>, RuntimeError> {
#[allow(unused_unsafe)]
let params: Vec<_> = unsafe {
params
.iter()
.map(|a| a.as_raw_value(&store.as_store_ref()))
.collect()
};
let arr = js_sys::Array::new_with_length(params.len() as u32);

// let raw_env = env.as_raw() as *mut u8;
Expand All @@ -402,11 +415,34 @@ impl Function {
let js_value = param.as_jsvalue(&store.as_store_ref());
arr.set(i as u32, js_value);
}
let result = js_sys::Reflect::apply(
&self.handle.get(store.as_store_ref().objects()).function,
&wasm_bindgen::JsValue::NULL,
&arr,
)?;

ptitSeb marked this conversation as resolved.
Show resolved Hide resolved
let result = {
let mut r;
loop {
r = js_sys::Reflect::apply(
&self.handle.get(store.as_store_ref().objects()).function,
&wasm_bindgen::JsValue::NULL,
&arr,
);
let store_mut = store.as_store_mut();
if let Some(callback) = store_mut.inner.on_called.take() {
match callback(store_mut) {
Ok(wasmer_types::OnCalledAction::InvokeAgain) => {
continue;
}
Ok(wasmer_types::OnCalledAction::Finish) => {
break;
}
Ok(wasmer_types::OnCalledAction::Trap(trap)) => {
return Err(RuntimeError::user(trap))
}
Err(trap) => return Err(RuntimeError::user(trap)),
}
}
break;
}
r?
};
theduke marked this conversation as resolved.
Show resolved Hide resolved

let result_types = self.handle.get(store.as_store_ref().objects()).ty.results();
match result_types.len() {
Expand Down Expand Up @@ -1134,16 +1170,18 @@ mod inner {
T: Send + 'static,
Func: Fn(FunctionEnvMut<'_, T>, $( $x , )*) -> RetsAsResult + 'static,
{
// let env: &Env = unsafe { &*(ptr as *const u8 as *const Env) };
let func: &Func = &*(&() as *const () as *const Func);
let mut store = StoreMut::from_raw(store_ptr as *mut _);
let mut store2 = StoreMut::from_raw(store_ptr as *mut _);

let result = panic::catch_unwind(AssertUnwindSafe(|| {
let handle: StoreHandle<VMFunctionEnvironment> = StoreHandle::from_internal(store2.objects_mut().id(), InternalStoreHandle::from_index(handle_index).unwrap());
let env: FunctionEnvMut<T> = FunctionEnv::from_handle(handle).into_mut(&mut store2);
func(env, $( FromToNativeWasmType::from_native(NativeWasmTypeInto::from_abi(&mut store, $x)) ),* ).into_result()
}));
let result = {
// let env: &Env = unsafe { &*(ptr as *const u8 as *const Env) };
let func: &Func = &*(&() as *const () as *const Func);
panic::catch_unwind(AssertUnwindSafe(|| {
let handle: StoreHandle<VMFunctionEnvironment> = StoreHandle::from_internal(store2.objects_mut().id(), InternalStoreHandle::from_index(handle_index).unwrap());
let env: FunctionEnvMut<T> = FunctionEnv::from_handle(handle).into_mut(&mut store2);
func(env, $( FromToNativeWasmType::from_native(NativeWasmTypeInto::from_abi(&mut store, $x)) ),* ).into_result()
}))
};

match result {
Ok(Ok(result)) => return result.into_c_struct(&mut store),
Expand Down
Loading