Skip to content

Commit

Permalink
Merge pull request #3426 from wasmerio/wasix-core-changes
Browse files Browse the repository at this point in the history
WASIX Preparation

All changes for WASIX that affect the main crates.
  • Loading branch information
theduke authored Jan 16, 2023
2 parents 6b57568 + e9edb7e commit 0785137
Show file tree
Hide file tree
Showing 85 changed files with 1,534 additions and 388 deletions.
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
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
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
18 changes: 10 additions & 8 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: 1.63
toolchain: 1.64
components: rustfmt, clippy
- name: Install libtinfo
shell: bash
Expand Down Expand Up @@ -93,7 +93,7 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: 1.63
toolchain: 1.64
- name: Install NodeJS
uses: actions/setup-node@v2
with:
Expand All @@ -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 Expand Up @@ -239,7 +241,7 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: 1.63
toolchain: 1.64
target: ${{ matrix.metadata.target }}
- name: Install Windows-GNU linker
if: ${{ matrix.metadata.build == 'windows-gnu' }}
Expand Down Expand Up @@ -457,7 +459,7 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: 1.63
toolchain: 1.64
target: ${{ matrix.metadata.target }}
- name: Install LLVM (macOS Apple Silicon)
if: matrix.metadata.os == 'macos-11.0' && !matrix.metadata.llvm_url
Expand Down Expand Up @@ -557,7 +559,7 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: 1.63
toolchain: 1.64
target: ${{ matrix.metadata.target }}
- name: Cache
uses: whywaita/actions-cache-s3@v2
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 @@ -502,8 +502,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
1 change: 1 addition & 0 deletions 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
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
Loading

0 comments on commit 0785137

Please sign in to comment.