Skip to content

Commit

Permalink
Merge pull request #109 from holochain/fix-memory-deallocation-for-ru…
Browse files Browse the repository at this point in the history
…st-1.78

Fix memory deallocation for Rust 1.78
  • Loading branch information
ThetaSinner authored May 21, 2024
2 parents 7ac62d3 + c5bd36c commit 48c5c49
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 138 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

## How to update holonix

This repository uses `niv` to manage Nix dependencies.
The following command will launch a transient shell with `niv` installed and update the holonix revision.
This repository uses the `rustDev` shell from [Holonix](https://github.com/holochain/holochain/blob/develop/nix/modules/devShells.nix).

```shell
nix-shell -p niv --run "niv update"
```
To update Holonix, run the following command:

```bash
nix flake update
````

## Why?

Expand Down Expand Up @@ -243,7 +243,7 @@ e.g. for nice callback handling.
It's really easy to make a mistake in the data handling (see below) and end up
with memory leaks or serialization mistakes, missing tracing or whatever else.
Use the `holochain_wasmer_host` crate to do as much heavy lifing as possible.
Use the `holochain_wasmer_host` crate to do as much heavy lifting as possible.
The `test_process_struct` shows a good minimal example of an import function:
Expand Down
2 changes: 1 addition & 1 deletion 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.93"
version = "0.0.94"
authors = ["thedavidmeister", "[email protected]"]
edition = "2021"

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.93"
version = "0.0.94"
authors = ["thedavidmeister", "[email protected]"]
edition = "2021"

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

[dependencies]
holochain_serialized_bytes = { version = "=0.0.54", features = [] }
holochain_wasmer_common = { version = "=0.0.93", path = "../common" }
holochain_wasmer_common = { version = "=0.0.94", path = "../common" }
serde = "1"
tracing = "0.1"
parking_lot = "0.12"
Expand Down
57 changes: 39 additions & 18 deletions crates/guest/src/guest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ where
O: serde::de::DeserializeOwned + std::fmt::Debug,
{
// Call the host function and receive the length of the serialized result.
let input_bytes = holochain_serialized_bytes::encode(&input).map_err(|e| wasm_error!(e))?;
let mut input_bytes = holochain_serialized_bytes::encode(&input).map_err(|e| wasm_error!(e))?;
input_bytes.shrink_to_fit();
if input_bytes.capacity() != input_bytes.len() {
tracing::warn!("Capacity should equal length, dealloc will fail");
}
debug_assert!(
input_bytes.capacity() == input_bytes.len(),
"Capacity should equal length, dealloc would fail"
);
let input_len: usize = input_bytes.len();
let input_guest_ptr = crate::allocation::write_bytes(input_bytes);

Expand Down Expand Up @@ -89,12 +97,17 @@ where
R: Serialize + std::fmt::Debug,
{
match holochain_serialized_bytes::encode::<Result<R, WasmError>>(&Ok(return_value)) {
Ok(bytes) => {
Ok(mut bytes) => {
let len: usize = bytes.len();
match merge_usize(write_bytes(bytes), len) {
Ok(v) => v,
Err(e) => return_err_ptr(e),
bytes.shrink_to_fit();
if bytes.capacity() != bytes.len() {
tracing::warn!("Capacity should equal length, dealloc will fail");
}
debug_assert!(
bytes.capacity() == bytes.len(),
"Capacity should equal length, dealloc would fail"
);
merge_usize(write_bytes(bytes), len).unwrap_or_else(|e| return_err_ptr(e))
}
Err(e) => return_err_ptr(wasm_error!(WasmErrorInner::Serialize(e))),
}
Expand All @@ -107,23 +120,31 @@ where
/// for `wasm32-unknown-unknown` target.
#[inline(always)]
pub fn return_err_ptr(wasm_error: WasmError) -> DoubleUSize {
let bytes = match holochain_serialized_bytes::encode::<Result<(), WasmError>>(&Err(wasm_error))
{
Ok(bytes) => bytes,
Err(e) => match holochain_serialized_bytes::encode::<Result<(), WasmError>>(&Err(
wasm_error!(WasmErrorInner::Serialize(e)),
)) {
let mut bytes =
match holochain_serialized_bytes::encode::<Result<(), WasmError>>(&Err(wasm_error)) {
Ok(bytes) => bytes,
// At this point we've errored while erroring
Err(_) => match holochain_serialized_bytes::encode::<Result<(), WasmError>>(&Err(
wasm_error!(WasmErrorInner::ErrorWhileError),
Err(e) => match holochain_serialized_bytes::encode::<Result<(), WasmError>>(&Err(
wasm_error!(WasmErrorInner::Serialize(e)),
)) {
Ok(bytes) => bytes,
// At this point we failed to serialize a unit variant so IDK ¯\_(ツ)_/¯
Err(_) => panic!("Failed to error"),
// At this point we've errored while erroring
Err(_) => match holochain_serialized_bytes::encode::<Result<(), WasmError>>(&Err(
wasm_error!(WasmErrorInner::ErrorWhileError),
)) {
Ok(bytes) => bytes,
// At this point we failed to serialize a unit variant so IDK ¯\_(ツ)_/¯
Err(_) => panic!("Failed to error"),
},
},
},
};
};
bytes.shrink_to_fit();
if bytes.capacity() != bytes.len() {
tracing::warn!("Capacity should equal length, dealloc will fail");
}
debug_assert!(
bytes.capacity() == bytes.len(),
"Capacity should equal length, dealloc would fail"
);
let len = bytes.len();
merge_usize(write_bytes(bytes), len).expect("Failed to build return value")
}
Expand Down
4 changes: 2 additions & 2 deletions crates/host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
name = "holochain_wasmer_host"
description = "wasm host code"
license = "Apache-2.0"
version = "0.0.93"
version = "0.0.94"
authors = ["thedavidmeister", "[email protected]"]
edition = "2021"

[dependencies]
wasmer = "=4.2.8"
wasmer-middlewares = "=4.2.8"
holochain_wasmer_common = { version = "=0.0.93", path = "../common" }
holochain_wasmer_common = { version = "=0.0.94", path = "../common" }
holochain_serialized_bytes = "=0.0.54"
serde = "1"
tracing = "0.1"
Expand Down
44 changes: 22 additions & 22 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env bash
set -euxo pipefail

export RUST_BACKTRACE=full
Expand All @@ -8,7 +9,7 @@ cargo fmt

cargo clippy
( cd test && cargo clippy )
( cd crates/guest && cargo clippy )
( cd crates/guest && cargo clippy --target wasm32-unknown-unknown )

# tests the root workspace that doesn't include any wasm code
cargo test ${1-} -- --nocapture
Expand Down
Loading

0 comments on commit 48c5c49

Please sign in to comment.