Skip to content

Commit

Permalink
Merge branch 'develop' into build/upgrade-holochain-serialized-bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
jost-s authored Oct 31, 2023
2 parents eb6e009 + 4cb068d commit e516341
Show file tree
Hide file tree
Showing 23 changed files with 1,730 additions and 1,464 deletions.
16 changes: 2 additions & 14 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,9 @@
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [0.0.86]

### Added

### Changed

- Bumped holochain_serialized_bytes to v0.0.52

### Deprecated

### Removed

### Fixed

### Security
- Support wasmer 4.x

## [0.0.73] - 2021-07-20

Expand Down
5 changes: 2 additions & 3 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "holochain_wasmer_common"
description = "commons for both host and guest"
license = "Apache-2.0"
version = "0.0.84"
version = "0.0.86"
authors = [ "thedavidmeister", "[email protected]" ]
edition = "2021"

Expand All @@ -18,5 +18,4 @@ default = []
error_as_host = []

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
wasmer = "=2.3.0"
wasmer-engine = "=2.3.0"
wasmer = "=4.2.2"
2 changes: 1 addition & 1 deletion crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub fn split_usize(u: DoubleUSize) -> Result<(usize, usize), WasmError> {
#[cfg(target_pointer_width = "64")]
return split_u128(u).map(|(a, b)| (a as usize, b as usize));
#[cfg(target_pointer_width = "32")]
return split_u64(u as u64).map(|(a, b)| (a as usize, b as usize));
return split_u64(u).map(|(a, b)| (a as usize, b as usize));
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions crates/guest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "holochain_wasmer_guest"
description = "wasm guest code"
license = "Apache-2.0"
version = "0.0.84"
version = "0.0.86"
authors = [ "thedavidmeister", "[email protected]" ]
edition = "2021"

Expand All @@ -15,7 +15,7 @@ path = "src/guest.rs"

[dependencies]
holochain_serialized_bytes = { version = "=0.0.53", features = [] }
holochain_wasmer_common = { version = "=0.0.84", path = "../common" }
holochain_wasmer_common = { version = "=0.0.86", path = "../common" }
serde = "1"
tracing = "0.1"
parking_lot = "0.12"
Expand Down
11 changes: 6 additions & 5 deletions crates/host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@
name = "holochain_wasmer_host"
description = "wasm host code"
license = "Apache-2.0"
version = "0.0.84"
version = "0.0.86"
authors = [ "thedavidmeister", "[email protected]" ]
edition = "2021"

[dependencies]
wasmer = "=2.3.0"
holochain_wasmer_common = { version = "=0.0.84", path = "../common" }
wasmer = "=4.2.2"
wasmer-middlewares = "=4.2.2"
holochain_wasmer_common = { version = "=0.0.86", path = "../common" }
holochain_serialized_bytes = "=0.0.53"
serde = "1"
tracing = "0.1"
parking_lot = "0.12"
once_cell = "1"
rand = "0.8"
bimap = "0.6"
bytes = "1"

[lib]
name = "holochain_wasmer_host"
Expand All @@ -25,5 +27,4 @@ path = "src/host.rs"
[features]
default = ["error_as_host"]
debug_memory = []
error_as_host = ["holochain_wasmer_common/error_as_host"]
dylib = ["wasmer/dylib"]
error_as_host = ["holochain_wasmer_common/error_as_host"]
75 changes: 38 additions & 37 deletions crates/host/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,51 @@ use std::num::TryFromIntError;

use crate::guest::read_bytes;
use crate::prelude::*;
use wasmer::Function;
use wasmer::LazyInit;
use wasmer::Memory;
use wasmer::WasmerEnv;
use wasmer::StoreMut;
use wasmer::TypedFunction;

#[derive(Clone, Default, WasmerEnv)]
#[derive(Clone, Default)]
pub struct Env {
#[wasmer(export)]
memory: LazyInit<Memory>,
#[wasmer(export(name = "__hc__allocate_1"))]
allocate: LazyInit<Function>,
#[wasmer(export(name = "__hc__deallocate_1"))]
deallocate: LazyInit<Function>,
pub memory: Option<Memory>,
pub allocate: Option<TypedFunction<i32, i32>>,
pub deallocate: Option<TypedFunction<(i32, i32), ()>>,
}

impl Env {
/// Given some input I that can be serialized, request an allocation from the
/// guest and copy the serialized bytes to the allocated pointer. The guest
/// MUST subsequently take ownership of these bytes or it will leak memory.
pub fn move_data_to_guest<I>(&self, input: I) -> Result<GuestPtrLen, wasmer::RuntimeError>
pub fn move_data_to_guest<I>(
&self,
store_mut: &mut StoreMut,
input: I,
) -> Result<GuestPtrLen, wasmer::RuntimeError>
where
I: serde::Serialize + std::fmt::Debug,
{
let data = holochain_serialized_bytes::encode(&input).map_err(|e| wasm_error!(e))?;
let guest_ptr: GuestPtr = match self
.allocate_ref()
let guest_ptr: GuestPtr = self
.allocate
.as_ref()
.ok_or(wasm_error!(WasmErrorInner::Memory))?
.call(&[Value::I32(
.call(
store_mut,
data.len()
.try_into()
.map_err(|_| wasm_error!(WasmErrorInner::PointerMap))?,
)])
)
.map_err(|e| wasm_error!(e.to_string()))?
.get(0)
{
Some(Value::I32(guest_ptr)) => (*guest_ptr)
.try_into()
.map_err(|e: TryFromIntError| wasm_error!(e))?,
_ => return Err(wasm_error!(WasmErrorInner::PointerMap).into()),
};
.try_into()
.map_err(|e: TryFromIntError| wasm_error!(e))?;
let len: Len = match data.len().try_into() {
Ok(len) => len,
Err(e) => return Err(wasm_error!(e).into()),
};
crate::guest::write_bytes(
self.memory_ref()
store_mut,
self.memory
.as_ref()
.ok_or(wasm_error!(WasmErrorInner::Memory))?,
guest_ptr,
&data,
Expand All @@ -61,32 +60,34 @@ impl Env {
/// deserialization is successful.
pub fn consume_bytes_from_guest<O>(
&self,
store_mut: &mut StoreMut,
guest_ptr: GuestPtr,
len: Len,
) -> Result<O, wasmer::RuntimeError>
where
O: serde::de::DeserializeOwned + std::fmt::Debug,
{
let bytes = read_bytes(
self.memory_ref()
.ok_or(wasm_error!(WasmErrorInner::Memory))?,
&self
.memory
.as_ref()
.ok_or(wasm_error!(WasmErrorInner::Memory))?
.view(&store_mut),
guest_ptr,
len,
)
.map_err(|_| wasm_error!(WasmErrorInner::Memory))?;
self.deallocate_ref()
self.deallocate
.as_ref()
.ok_or(wasm_error!(WasmErrorInner::Memory))?
.call(&[
Value::I32(
guest_ptr
.try_into()
.map_err(|_| wasm_error!(WasmErrorInner::PointerMap))?,
),
Value::I32(
len.try_into()
.map_err(|_| wasm_error!(WasmErrorInner::PointerMap))?,
),
])
.call(
store_mut,
guest_ptr
.try_into()
.map_err(|_| wasm_error!(WasmErrorInner::PointerMap))?,
len.try_into()
.map_err(|_| wasm_error!(WasmErrorInner::PointerMap))?,
)
.map_err(|e| wasm_error!(e.to_string()))?;
match holochain_serialized_bytes::decode(&bytes) {
Ok(v) => Ok(v),
Expand Down
Loading

0 comments on commit e516341

Please sign in to comment.