Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwiterrion committed Oct 3, 2024
2 parents cd7ae29 + 520d72e commit 54482a1
Show file tree
Hide file tree
Showing 19 changed files with 669 additions and 354 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ jobs:
static-pc-in: 'extism-static.pc.in'
- os: 'ubuntu'
target: 'x86_64-unknown-linux-musl'
artifact: ''
artifact: 'libextism.so'
static-artifact: 'libextism.a'
static-dll-artifact: ''
pc-in: ''
pc-in: 'extism.pc.in'
static-pc-in: 'extism-static.pc.in'
- os: 'windows'
target: 'x86_64-pc-windows-gnu'
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

[![Discord](https://img.shields.io/discord/1011124058408112148?color=%23404eed&label=Community%20Chat&logo=Discord&logoColor=%23404eed)](https://extism.org/discord)
![GitHub Org's stars](https://img.shields.io/github/stars/extism)
![Downloads](https://img.shields.io/crates/d/extism)
![Downloads](https://img.shields.io/crates/d/extism-manifest)
![GitHub License](https://img.shields.io/github/license/extism/extism)
![GitHub release (with filter)](https://img.shields.io/github/v/release/extism/extism)

Expand Down Expand Up @@ -61,7 +61,7 @@ started:

# Compile WebAssembly to run in Extism Hosts

Extism Hosts (running the SDK) must execute WebAssembly code that has a PDK
Extism Hosts (running the SDK) must execute WebAssembly code that has a [PDK, or Plug-in Development Kit](https://extism.org/docs/concepts/pdk),
library compiled in to the `.wasm` binary. PDKs make it easy for plug-in /
extension code authors to read input from the host and return data back, read
provided configuration, set/get variables, make outbound HTTP calls if allowed,
Expand Down
2 changes: 1 addition & 1 deletion convert/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ description = "Traits to make Rust types usable with Extism"
anyhow = "1.0.75"
base64 = "~0.22"
bytemuck = {version = "1.14.0", optional = true }
prost = { version = "0.12.0", optional = true }
prost = { version = "0.13.1", optional = true }
protobuf = { version = "3.2.0", optional = true }
rmp-serde = { version = "1.1.2", optional = true }
serde = "1.0.186"
Expand Down
4 changes: 2 additions & 2 deletions convert/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ mod tests {
c: true,
};
let raw = Raw(&x).to_bytes().unwrap();
let y = Raw::from_bytes(&raw).unwrap();
let y = Raw::from_bytes(raw).unwrap();
assert_eq!(&x, y.0);

let y: Result<Raw<[u8; std::mem::size_of::<TestRaw>()]>, Error> = Raw::from_bytes(&raw);
let y: Result<Raw<[u8; std::mem::size_of::<TestRaw>()]>, Error> = Raw::from_bytes(raw);
assert!(y.is_ok());
}
}
69 changes: 21 additions & 48 deletions kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,6 @@ impl MemoryRoot {
pub unsafe fn alloc(&mut self, length: u64) -> Option<&'static mut MemoryBlock> {
let self_position = self.position.load(Ordering::Acquire);
let self_length = self.length.load(Ordering::Acquire);
let b = self.find_free_block(length, self_position);

// If there's a free block then re-use it
if let Some(b) = b {
b.used = length as usize;
b.status
.store(MemoryStatus::Active as u8, Ordering::Release);
return Some(b);
}

// Get the current index for a new block
let curr = self.blocks.as_ptr() as u64 + self_position;
Expand All @@ -275,6 +266,21 @@ impl MemoryRoot {
// When the allocation is larger than the number of bytes available
// we will need to try to grow the memory
if length_with_block >= mem_left {
// If the current position is large enough to hold the length of the block being
// allocated then check for existing free blocks that can be re-used before
// growing memory
if length_with_block <= self_position {
let b = self.find_free_block(length, self_position);

// If there's a free block then re-use it
if let Some(b) = b {
b.used = length as usize;
b.status
.store(MemoryStatus::Active as u8, Ordering::Release);
return Some(b);
}
}

// Calculate the number of pages needed to cover the remaining bytes
let npages = num_pages(length_with_block - mem_left);
let x = core::arch::wasm32::memory_grow(0, npages);
Expand Down Expand Up @@ -489,6 +495,8 @@ pub unsafe fn store_u64(p: Pointer, x: u64) {
/// Set the range of the input data in memory
/// h must always be a handle so that length works on it
/// len must match length(handle)
/// **Note**: this function takes ownership of the handle passed in
/// the caller should not `free` this value
#[no_mangle]
pub unsafe fn input_set(h: Handle, len: u64) {
let root = MemoryRoot::new();
Expand All @@ -503,6 +511,8 @@ pub unsafe fn input_set(h: Handle, len: u64) {
}

/// Set the range of the output data in memory
/// **Note**: this function takes ownership of the handle passed in
/// the caller should not `free` this value
#[no_mangle]
pub unsafe fn output_set(p: Pointer, len: u64) {
let root = MemoryRoot::new();
Expand Down Expand Up @@ -548,6 +558,8 @@ pub unsafe fn reset() {

/// Set the error message offset, the handle passed to this
/// function should not be freed after this call
/// **Note**: this function takes ownership of the handle passed in
/// the caller should not `free` this value
#[no_mangle]
pub unsafe fn error_set(h: Handle) {
let root = MemoryRoot::new();
Expand Down Expand Up @@ -582,45 +594,6 @@ mod test {
use crate::*;
use wasm_bindgen_test::*;

// See https://github.com/extism/extism/pull/659
#[wasm_bindgen_test]
fn test_659() {
unsafe {
// Warning: These offsets will need to change if we adjust the kernel memory layout at all
reset();
assert_eq!(alloc(1065), 77);
assert_eq!(alloc(288), 1154);
assert_eq!(alloc(128), 1454);
assert_eq!(length(1154), 288);
assert_eq!(length(1454), 128);
free(1454);
assert_eq!(alloc(213), 1594);
length_unsafe(1594);
assert_eq!(alloc(511), 1819);
assert_eq!(alloc(4), 1454);
assert_eq!(length(1454), 4);
assert_eq!(length(1819), 511);
assert_eq!(alloc(13), 2342);
assert_eq!(length(2342), 13);
assert_eq!(alloc(336), 2367);
assert_eq!(alloc(1077), 2715);
assert_eq!(length(2367), 336);
assert_eq!(length(2715), 1077);
free(2715);
assert_eq!(alloc(1094), 3804);
length_unsafe(3804);

// Allocate 4 bytes, expect to receive address 3788
assert_eq!(alloc(4), 3788);

assert_eq!(alloc(4), 3772);
assert_eq!(length(3772), 4);

// Address 3788 has not been freed yet, so expect it to have 4 bytes allocated
assert_eq!(length(3788), 4);
}
}

#[wasm_bindgen_test]
fn test_oom() {
let size = 1024 * 1024 * 5;
Expand Down
8 changes: 4 additions & 4 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ repository.workspace = true
version.workspace = true

[dependencies]
wasmtime = ">= 20.0.0, < 23.0.0"
wasi-common = ">= 20.0.0, < 23.0.0"
wiggle = ">= 20.0.0, < 23.0.0"
wasmtime = ">= 20.0.0, < 24.0.0"
wasi-common = ">= 20.0.0, < 24.0.0"
wiggle = ">= 20.0.0, < 24.0.0"
anyhow = "1"
serde = {version = "1", features = ["derive"]}
serde_json = "1"
Expand All @@ -34,7 +34,7 @@ register-filesystem = [] # enables wasm to be loaded from disk
http = ["ureq"] # enables extism_http_request

[build-dependencies]
cbindgen = { version = "0.26", default-features = false }
cbindgen = { version = "0.27", default-features = false }

[dev-dependencies]
criterion = "0.5.1"
Expand Down
13 changes: 13 additions & 0 deletions runtime/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const COUNT_VOWELS: &[u8] = include_bytes!("../../wasm/code.wasm");
const REFLECT: &[u8] = include_bytes!("../../wasm/reflect.wasm");
const ECHO: &[u8] = include_bytes!("../../wasm/echo.wasm");
const CONSUME: &[u8] = include_bytes!("../../wasm/consume.wasm");
const ALLOCATIONS: &[u8] = include_bytes!("../../wasm/allocations.wasm");

host_fn!(hello_world (a: String) -> String { Ok(a) });

Expand Down Expand Up @@ -168,6 +169,17 @@ pub fn reflect(c: &mut Criterion) {
}
}

pub fn allocations(c: &mut Criterion) {
let mut g = c.benchmark_group("allocations");

let mut plugin = PluginBuilder::new(ALLOCATIONS).build().unwrap();
g.bench_function("allocations", |b| {
b.iter(|| {
plugin.call::<_, ()>("allocations", "").unwrap();
})
});
}

// This is an apples-to-apples comparison of a linked wasm "reflect" function to our host "reflect"
// function.
pub fn reflect_linked(c: &mut Criterion) {
Expand Down Expand Up @@ -260,6 +272,7 @@ pub fn reflect_linked(c: &mut Criterion) {

criterion_group!(
benches,
allocations,
consume,
echo,
reflect,
Expand Down
15 changes: 13 additions & 2 deletions runtime/extism.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,17 @@ ExtismPlugin *extism_plugin_new(const uint8_t *wasm,
bool with_wasi,
char **errmsg);

/**
* Create a new plugin and set the number of instructions a plugin is allowed to execute
*/
ExtismPlugin *extism_plugin_new_with_fuel_limit(const uint8_t *wasm,
ExtismSize wasm_size,
const ExtismFunction **functions,
ExtismSize n_functions,
bool with_wasi,
uint64_t fuel_limit,
char **errmsg);

/**
* Free the error returned by `extism_plugin_new`, errors returned from `extism_plugin_error` don't need to be freed
*/
Expand Down Expand Up @@ -373,5 +384,5 @@ bool extism_plugin_reset(ExtismPlugin *plugin);
const char *extism_version(void);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
} // extern "C"
#endif // __cplusplus
Loading

0 comments on commit 54482a1

Please sign in to comment.