mirror of
https://github.com/extism/extism.git
synced 2026-01-12 23:38:04 -05:00
Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
24622cd511 | ||
|
|
00f9155b9e | ||
|
|
7c770e2f1c | ||
|
|
33ad239c50 | ||
|
|
a62e37c21b | ||
|
|
20ee6620c6 | ||
|
|
db0b04696c | ||
|
|
6cc22bf2de | ||
|
|
52d3c4ffd6 | ||
|
|
b96b28231f | ||
|
|
c1180d576c | ||
|
|
48af68facc | ||
|
|
38e6476797 |
3
kernel/.gitignore
vendored
Normal file
3
kernel/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
owi-out
|
||||
*.wat
|
||||
proofs
|
||||
@@ -7,6 +7,7 @@ edition = "2021"
|
||||
|
||||
[dev-dependencies]
|
||||
wasm-bindgen-test = "0.3.39"
|
||||
owi = {git = "https://github.com/dylibso/owi-rs"}
|
||||
|
||||
[features]
|
||||
default = ["bounds-checking"]
|
||||
@@ -15,4 +16,4 @@ bounds-checking = []
|
||||
[workspace]
|
||||
members = [
|
||||
"."
|
||||
]
|
||||
]
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -e
|
||||
|
||||
export CARGO_FLAGS=""
|
||||
|
||||
while getopts d flag
|
||||
|
||||
21
kernel/examples/alloc_length.rs
Normal file
21
kernel/examples/alloc_length.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
#![no_main]
|
||||
#![no_std]
|
||||
use extism_runtime_kernel::*;
|
||||
use owi::*;
|
||||
|
||||
main!({
|
||||
let n = alloc(1024);
|
||||
|
||||
let x = u64();
|
||||
assume(x > 0);
|
||||
|
||||
let m = alloc(x);
|
||||
|
||||
// 1. Length should equal `x` while active
|
||||
assert(length(m) == x);
|
||||
|
||||
// 2. Length should equal `0` after free
|
||||
free(m); // Free the block
|
||||
assert(length(m) == 0);
|
||||
free(n);
|
||||
});
|
||||
24
kernel/examples/error.rs
Normal file
24
kernel/examples/error.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
#![no_main]
|
||||
#![no_std]
|
||||
use extism_runtime_kernel::*;
|
||||
use owi::*;
|
||||
|
||||
main!({
|
||||
let x = u64();
|
||||
let n = u64();
|
||||
let m = u64();
|
||||
assume(x > 0);
|
||||
assume(n > 0);
|
||||
assume(m > 0);
|
||||
|
||||
alloc(n);
|
||||
alloc(m);
|
||||
let n = alloc(x);
|
||||
assert(error_get() == 0);
|
||||
|
||||
error_set(n);
|
||||
assert(error_get() == n);
|
||||
alloc(m);
|
||||
error_set(0);
|
||||
assert(error_get() == 0);
|
||||
});
|
||||
17
kernel/examples/load_store.rs
Normal file
17
kernel/examples/load_store.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
#![no_main]
|
||||
#![no_std]
|
||||
use extism_runtime_kernel::*;
|
||||
use owi::*;
|
||||
|
||||
main!({
|
||||
let x = u64();
|
||||
assume(x > 0);
|
||||
|
||||
let m = alloc(x);
|
||||
assert(length(m) == x);
|
||||
for i in 0..x {
|
||||
store_u8(m + i, i as u8);
|
||||
assert(load_u8(m + i) == i as u8);
|
||||
}
|
||||
free(m);
|
||||
});
|
||||
39
kernel/examples/reuse.rs
Normal file
39
kernel/examples/reuse.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
#![no_main]
|
||||
#![no_std]
|
||||
use extism_runtime_kernel::*;
|
||||
use owi::*;
|
||||
|
||||
main!({
|
||||
let x = u64();
|
||||
assume(x > 0);
|
||||
|
||||
let y = u64();
|
||||
assume(y > 0);
|
||||
|
||||
let mut tmp = 0;
|
||||
|
||||
for _ in 0..y {
|
||||
let m = alloc(x);
|
||||
if tmp == 0 {
|
||||
tmp = m;
|
||||
} else {
|
||||
// Check that the existing block is being re-used
|
||||
assert(m == tmp);
|
||||
}
|
||||
|
||||
assert(length(m) == x);
|
||||
free(m); // Free the block
|
||||
}
|
||||
|
||||
let y = u64();
|
||||
assume(y == x + 1);
|
||||
let n = alloc(y);
|
||||
assert(n > tmp);
|
||||
|
||||
let z = u64();
|
||||
assume(z <= x);
|
||||
assume(x - z < 32);
|
||||
assume(z > 0);
|
||||
let p = alloc(z);
|
||||
assert(p == tmp);
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
#![no_main]
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
pub use extism_runtime_kernel::*;
|
||||
|
||||
|
||||
@@ -175,7 +175,7 @@ impl MemoryRoot {
|
||||
core::ptr::write_bytes(
|
||||
self.blocks.as_mut_ptr() as *mut u8,
|
||||
MemoryStatus::Unused as u8,
|
||||
self_position as usize,
|
||||
self_position as usize - core::mem::size_of::<MemoryRoot>(),
|
||||
);
|
||||
|
||||
// Clear extism runtime metadata
|
||||
@@ -546,8 +546,7 @@ pub unsafe fn reset() {
|
||||
MemoryRoot::new().reset()
|
||||
}
|
||||
|
||||
/// Set the error message offset, the handle passed to this
|
||||
/// function should not be freed after this call
|
||||
/// Set the error message offset
|
||||
#[no_mangle]
|
||||
pub unsafe fn error_set(h: Handle) {
|
||||
let root = MemoryRoot::new();
|
||||
@@ -639,4 +638,15 @@ mod test {
|
||||
|
||||
assert_eq!(last, 0);
|
||||
}
|
||||
|
||||
#[wasm_bindgen_test]
|
||||
fn test_sym() {
|
||||
unsafe {
|
||||
reset();
|
||||
let a = alloc(47);
|
||||
let b = alloc(20);
|
||||
assert_eq!(length(a), 47);
|
||||
assert_eq!(length(b), 20);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
23
kernel/verify.sh
Executable file
23
kernel/verify.sh
Executable file
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -e
|
||||
|
||||
OUT_DIR=./target/wasm32-unknown-unknown/release/examples/
|
||||
get_proof() {
|
||||
cp "$OUT_DIR/$1.wasm" "./proofs/$1.wasm"
|
||||
}
|
||||
|
||||
cargo build --examples --release --target wasm32-unknown-unknown --no-default-features
|
||||
|
||||
mkdir -p proofs
|
||||
get_proof alloc_length
|
||||
get_proof load_store
|
||||
get_proof reuse
|
||||
get_proof error
|
||||
|
||||
for proof in $(ls proofs/*.wasm); do
|
||||
echo "Checking $proof"
|
||||
owi conc "$proof" $@
|
||||
echo
|
||||
echo "---"
|
||||
done
|
||||
@@ -9,8 +9,8 @@ repository.workspace = true
|
||||
version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
wasmtime = ">= 20.0.0, < 23.0.0"
|
||||
wasi-common = ">= 20.0.0, < 23.0.0"
|
||||
wasmtime = ">= 20.0.0, < 22.0.0"
|
||||
wasi-common = ">= 20.0.0, < 22.0.0"
|
||||
anyhow = "1"
|
||||
serde = {version = "1", features = ["derive"]}
|
||||
serde_json = "1"
|
||||
|
||||
@@ -12,7 +12,7 @@ To use the `extism` crate, you can add it to your Cargo file:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
extism = "1.4.1"
|
||||
extism = "1.2.0"
|
||||
```
|
||||
|
||||
## Environment variables
|
||||
|
||||
@@ -35,21 +35,6 @@ pub fn create_plugin(c: &mut Criterion) {
|
||||
});
|
||||
}
|
||||
|
||||
pub fn create_plugin_no_cache(c: &mut Criterion) {
|
||||
let mut g = c.benchmark_group("create");
|
||||
g.noise_threshold(1.0);
|
||||
g.significance_level(0.2);
|
||||
g.bench_function("create_plugin_no_cache", |b| {
|
||||
b.iter(|| {
|
||||
let _plugin = PluginBuilder::new(COUNT_VOWELS)
|
||||
.with_cache_disabled()
|
||||
.with_wasi(true)
|
||||
.build()
|
||||
.unwrap();
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Deserialize, PartialEq)]
|
||||
struct Count {
|
||||
count: u32,
|
||||
@@ -266,7 +251,6 @@ criterion_group!(
|
||||
reflect_linked,
|
||||
basic,
|
||||
create_plugin,
|
||||
create_plugin_no_cache,
|
||||
count_vowels
|
||||
);
|
||||
criterion_main!(benches);
|
||||
|
||||
@@ -279,7 +279,7 @@ impl Function {
|
||||
/// For example, the following defines a host function named `add_newline` that takes a
|
||||
/// string parameter and returns a string result:
|
||||
/// ```rust
|
||||
/// extism::host_fn!(add_newline(_user_data: (); a: String) -> String { Ok(a + "\n") });
|
||||
/// extism::host_fn!(add_newline(_user_data: (), a: String) -> String { Ok(a + "\n") });
|
||||
/// ```
|
||||
/// A few things worth noting:
|
||||
/// - The function always returns a `Result` that wraps the specified return type
|
||||
|
||||
@@ -81,8 +81,6 @@ pub struct Plugin {
|
||||
pub(crate) store_needs_reset: bool,
|
||||
|
||||
pub(crate) debug_options: DebugOptions,
|
||||
|
||||
pub(crate) error_msg: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
unsafe impl Send for Plugin {}
|
||||
@@ -363,7 +361,6 @@ impl Plugin {
|
||||
store_needs_reset: false,
|
||||
debug_options,
|
||||
_functions: imports,
|
||||
error_msg: None,
|
||||
};
|
||||
|
||||
plugin.current_plugin_mut().store = &mut plugin.store;
|
||||
|
||||
@@ -505,8 +505,6 @@ pub unsafe extern "C" fn extism_plugin_call_with_host_context(
|
||||
let lock = plugin.instance.clone();
|
||||
let mut lock = lock.lock().unwrap();
|
||||
|
||||
plugin.error_msg = None;
|
||||
|
||||
// Get function name
|
||||
let name = std::ffi::CStr::from_ptr(func_name);
|
||||
let name = match name.to_str() {
|
||||
@@ -553,19 +551,10 @@ pub unsafe extern "C" fn extism_plugin_error(plugin: *mut Plugin) -> *const c_ch
|
||||
return std::ptr::null();
|
||||
}
|
||||
|
||||
let offs = plugin.output.error_offset;
|
||||
|
||||
let ptr = plugin.current_plugin_mut().memory_ptr().add(offs as usize) as *const _;
|
||||
|
||||
let len = plugin
|
||||
plugin
|
||||
.current_plugin_mut()
|
||||
.memory_length(offs)
|
||||
.unwrap_or_default();
|
||||
|
||||
let mut data = std::slice::from_raw_parts(ptr, len as usize).to_vec();
|
||||
data.push(0);
|
||||
plugin.error_msg = Some(data);
|
||||
plugin.error_msg.as_ref().unwrap().as_ptr() as *const _
|
||||
.memory_ptr()
|
||||
.add(plugin.output.error_offset as usize) as *const _
|
||||
}
|
||||
|
||||
/// Get the length of a plugin's output data
|
||||
|
||||
Reference in New Issue
Block a user