Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #53 from frank-emrich/upstream-dev-merge
Browse files Browse the repository at this point in the history
Upstream dev merge
  • Loading branch information
dhil authored Aug 17, 2023
2 parents 83ec1b6 + 262cf4c commit 677ac8d
Show file tree
Hide file tree
Showing 98 changed files with 5,338 additions and 2,277 deletions.
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ num_cpus = "1.13"
rand = { version = "0.8.4", features = ["small_rng"] }
rayon = "1.3"
serde = { version = "1.0.166", features = ["derive"] }
wasmtime = { version = "3.0.0", default-features = false, features = [
'cranelift',
] }
wasmtime = { version = "11.0.1", default-features = false, features = ['cranelift', 'component-model'] }
url = "2.0.0"
pretty_assertions = "1.3.0"
semver = "1.0.0"
Expand Down
14 changes: 10 additions & 4 deletions crates/fuzz-stats/src/limits.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Result;
use wasmtime::*;

#[derive(Clone)]
Expand All @@ -22,12 +23,17 @@ impl StoreLimits {
}

impl ResourceLimiter for StoreLimits {
fn memory_growing(&mut self, current: usize, desired: usize, _maximum: Option<usize>) -> bool {
self.alloc(desired - current)
fn memory_growing(
&mut self,
current: usize,
desired: usize,
_maximum: Option<usize>,
) -> Result<bool> {
Ok(self.alloc(desired - current))
}

fn table_growing(&mut self, current: u32, desired: u32, _maximum: Option<u32>) -> bool {
fn table_growing(&mut self, current: u32, desired: u32, _maximum: Option<u32>) -> Result<bool> {
let delta = (desired - current) as usize * std::mem::size_of::<usize>();
self.alloc(delta)
Ok(self.alloc(delta))
}
}
2 changes: 1 addition & 1 deletion crates/wasm-compose/example/middleware/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ publish = false

[dependencies]
flate2 = "1.0.24"
wit-bindgen = { version = "0.6.0", default_features = false }
cargo-component-bindings = { git = "https://github.com/bytecodealliance/cargo-component" }

[lib]
crate-type = ["cdylib"]
Expand Down
10 changes: 6 additions & 4 deletions crates/wasm-compose/example/middleware/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use bindings::example::service::handler as downstream;
use bindings::exports::example::service::handler::{Error, Handler, Request, Response};
cargo_component_bindings::generate!();

use bindings::{
example::service::handler as downstream,
exports::example::service::handler::{Error, Handler, Request, Response},
};
use flate2::{write::GzEncoder, Compression};
use std::io::Write;

Expand Down Expand Up @@ -44,5 +48,3 @@ impl Handler for Component {
Ok(response)
}
}

bindings::export!(Component);
12 changes: 6 additions & 6 deletions crates/wasm-compose/example/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ publish = false

[dependencies]
async-std = { version = "1.12.0", features = ["attributes"] }
clap = { version = "3.2.23", features = ["derive"] }
driftwood = "0.0.6"
clap = { version = "4.3.19", features = ["derive"] }
driftwood = "0.0.7"
tide = "0.16.0"
wasmtime = { git = "https://github.com/bytecodealliance/wasmtime", rev = "46826c62735dc22cc512fd5d23aa702a92e8a364", features = ["component-model"] }
wasmtime-wasi = { git = "https://github.com/bytecodealliance/wasmtime", rev = "46826c62735dc22cc512fd5d23aa702a92e8a364" }
wasi-cap-std-sync = { git = "https://github.com/bytecodealliance/wasmtime", rev = "46826c62735dc22cc512fd5d23aa702a92e8a364" }
anyhow = "1.0.71"
wasmtime = { git = "https://github.com/bytecodealliance/wasmtime", rev = "7b9189b", features = ["component-model"] }
wasmtime-wasi = { git = "https://github.com/bytecodealliance/wasmtime", rev = "7b9189b" }
wasi-cap-std-sync = { git = "https://github.com/bytecodealliance/wasmtime", rev = "7b9189b" }
anyhow = "1.0.72"

[workspace]
2 changes: 1 addition & 1 deletion crates/wasm-compose/example/server/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ search-paths:
instantiations:
$input:
arguments:
example:service/handler: svc
example:service/handler@0.1.0: svc
29 changes: 11 additions & 18 deletions crates/wasm-compose/example/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tide::{
};

use wasmtime::{component::*, Config, Engine, Store};
use wasmtime_wasi::preview2::{wasi, Table, WasiCtx, WasiCtxBuilder, WasiView};
use wasmtime_wasi::preview2::{command, Table, WasiCtx, WasiCtxBuilder, WasiView};

use exports::example::service::*;

Expand Down Expand Up @@ -118,33 +118,26 @@ impl ServerApp {
let body = req.body_bytes().await?;
let headers = req
.iter()
.map(|(n, v)| (n.as_str().as_bytes(), v.as_str().as_bytes()))
.map(|(n, v)| {
(
n.as_str().as_bytes().to_vec(),
v.as_str().as_bytes().to_vec(),
)
})
.collect::<Vec<_>>();

// Create a new store for the request
let state = req.state();
let mut linker = Linker::new(&state.engine);
wasi::filesystem::filesystem::add_to_linker(&mut linker, |x| x)?;
wasi::io::streams::add_to_linker(&mut linker, |x| x)?;
wasi::cli_base::environment::add_to_linker(&mut linker, |x| x)?;
wasi::cli_base::preopens::add_to_linker(&mut linker, |x| x)?;
wasi::cli_base::exit::add_to_linker(&mut linker, |x| x)?;
wasi::cli_base::stdin::add_to_linker(&mut linker, |x| x)?;
wasi::cli_base::stdout::add_to_linker(&mut linker, |x| x)?;
wasi::cli_base::stderr::add_to_linker(&mut linker, |x| x)?;
command::add_to_linker(&mut linker)?;

let wasi_view = ServerWasiView::new()?;
let mut store = Store::new(&state.engine, wasi_view);
let (service, _) = Service::instantiate_async(&mut store, &state.component, &linker).await?;
let (service, _) =
Service::instantiate_async(&mut store, &state.component, &linker).await?;
service
.example_service_handler()
.call_execute(
&mut store,
handler::Request {
headers: &headers,
body: &body,
},
)
.call_execute(&mut store, &handler::Request { headers, body })
.await?
.map(TryInto::try_into)
.map_err(handler::Error::into_tide)?
Expand Down
2 changes: 1 addition & 1 deletion crates/wasm-compose/example/service.wit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package example:service
package example:service@0.1.0

interface handler {
record request {
Expand Down
2 changes: 1 addition & 1 deletion crates/wasm-compose/example/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
publish = false

[dependencies]
wit-bindgen = { version = "0.6.0", default_features = false }
cargo-component-bindings = { git = "https://github.com/bytecodealliance/cargo-component" }

[lib]
crate-type = ["cdylib"]
Expand Down
4 changes: 2 additions & 2 deletions crates/wasm-compose/example/service/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cargo_component_bindings::generate!();

use bindings::exports::example::service::handler::{Error, Handler, Request, Response};
use std::str;

Expand Down Expand Up @@ -27,5 +29,3 @@ impl Handler for Component {
})
}
}

bindings::export!(Component);
4 changes: 2 additions & 2 deletions crates/wasm-compose/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl<'a> TypeState<'a> {
type_exports_rev: HashMap::new(),
instance_exports: HashMap::new(),
type_defs: HashMap::new(),
encodable: encodable,
encodable,
},
);
self.scopes.push(prev);
Expand Down Expand Up @@ -1324,7 +1324,7 @@ fn toposort<'a>(
}
if let Some(list) = deps.get(cur) {
for dep in list {
toposort(*dep, deps, order);
toposort(dep, deps, order);
}
}
let ok = order.insert(cur);
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmparser/src/readers/component/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl PrimitiveValType {
})
}

pub(crate) fn requires_realloc(&self) -> bool {
pub(crate) fn contains_ptr(&self) -> bool {
matches!(self, Self::String)
}

Expand Down
2 changes: 2 additions & 0 deletions crates/wasmparser/src/readers/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod code;
mod coredumps;
mod custom;
mod data;
mod dylink0;
mod elements;
mod exports;
mod functions;
Expand All @@ -20,6 +21,7 @@ pub use self::code::*;
pub use self::coredumps::*;
pub use self::custom::*;
pub use self::data::*;
pub use self::dylink0::*;
pub use self::elements::*;
pub use self::exports::*;
pub use self::functions::*;
Expand Down
132 changes: 132 additions & 0 deletions crates/wasmparser/src/readers/core/dylink0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
use crate::{BinaryReader, Result, Subsection, Subsections};
use std::ops::Range;

/// Parser for the dynamic linking `dylink.0` custom section.
///
/// This format is currently defined upstream at
/// <https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md>.
pub type Dylink0SectionReader<'a> = Subsections<'a, Dylink0Subsection<'a>>;

const WASM_DYLINK_MEM_INFO: u8 = 1;
const WASM_DYLINK_NEEDED: u8 = 2;
const WASM_DYLINK_EXPORT_INFO: u8 = 3;
const WASM_DYLINK_IMPORT_INFO: u8 = 4;

#[allow(missing_docs)]
pub const WASM_SYM_BINDING_WEAK: u32 = 1 << 0;
#[allow(missing_docs)]
pub const WASM_SYM_BINDING_LOCAL: u32 = 1 << 1;
#[allow(missing_docs)]
pub const WASM_SYM_VISIBILITY_HIDDEN: u32 = 1 << 2;
#[allow(missing_docs)]
pub const WASM_SYM_UNDEFINED: u32 = 1 << 4;
#[allow(missing_docs)]
pub const WASM_SYM_EXPORTED: u32 = 1 << 5;
#[allow(missing_docs)]
pub const WASM_SYM_EXPLICIT_NAME: u32 = 1 << 6;
#[allow(missing_docs)]
pub const WASM_SYM_NO_STRIP: u32 = 1 << 7;

/// Represents a `WASM_DYLINK_MEM_INFO` field
#[derive(Debug, Copy, Clone)]
pub struct MemInfo {
/// Size of the memory area the loader should reserve for the module, which
/// will begin at `env.__memory_base`.
pub memory_size: u32,

/// The required alignment of the memory area, in bytes, encoded as a power
/// of 2.
pub memory_alignment: u32,

/// Size of the table area the loader should reserve for the module, which
/// will begin at `env.__table_base`.
pub table_size: u32,

/// The required alignment of the table area, in elements, encoded as a
/// power of 2.
pub table_alignment: u32,
}

#[allow(missing_docs)]
#[derive(Debug)]
pub struct ExportInfo<'a> {
pub name: &'a str,

/// Note that these flags correspond to those described in
/// <https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md>
/// with the `WASM_SYM_*` prefix.
pub flags: u32,
}

#[allow(missing_docs)]
#[derive(Debug)]
pub struct ImportInfo<'a> {
pub module: &'a str,
pub field: &'a str,

/// Note that these flags correspond to those described in
/// <https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md>
/// with the `WASM_SYM_*` prefix.
pub flags: u32,
}

/// Possible subsections of the `dylink.0` custom section.
#[derive(Debug)]
#[allow(missing_docs)]
pub enum Dylink0Subsection<'a> {
MemInfo(MemInfo),
Needed(Vec<&'a str>),
ExportInfo(Vec<ExportInfo<'a>>),
ImportInfo(Vec<ImportInfo<'a>>),
Unknown {
ty: u8,
data: &'a [u8],
range: Range<usize>,
},
}

impl<'a> Subsection<'a> for Dylink0Subsection<'a> {
fn from_reader(id: u8, mut reader: BinaryReader<'a>) -> Result<Self> {
let data = reader.remaining_buffer();
let offset = reader.original_position();
Ok(match id {
WASM_DYLINK_MEM_INFO => Self::MemInfo(MemInfo {
memory_size: reader.read_var_u32()?,
memory_alignment: reader.read_var_u32()?,
table_size: reader.read_var_u32()?,
table_alignment: reader.read_var_u32()?,
}),
WASM_DYLINK_NEEDED => Self::Needed(
(0..reader.read_var_u32()?)
.map(|_| reader.read_string())
.collect::<Result<_, _>>()?,
),
WASM_DYLINK_EXPORT_INFO => Self::ExportInfo(
(0..reader.read_var_u32()?)
.map(|_| {
Ok(ExportInfo {
name: reader.read_string()?,
flags: reader.read_var_u32()?,
})
})
.collect::<Result<_, _>>()?,
),
WASM_DYLINK_IMPORT_INFO => Self::ImportInfo(
(0..reader.read_var_u32()?)
.map(|_| {
Ok(ImportInfo {
module: reader.read_string()?,
field: reader.read_string()?,
flags: reader.read_var_u32()?,
})
})
.collect::<Result<_, _>>()?,
),
ty => Self::Unknown {
ty,
data,
range: offset..offset + data.len(),
},
})
}
}
Loading

0 comments on commit 677ac8d

Please sign in to comment.