Skip to content

Commit

Permalink
Merge branch 'tiago/read-from-buf' (#2813)
Browse files Browse the repository at this point in the history
* origin/tiago/read-from-buf:
  Changelog for #2813
  Do not reconstruct result buffer
  • Loading branch information
tzemanovic committed Apr 3, 2024
2 parents eeab108 + 075455d commit 08ab918
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/2813-read-from-buf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Avoid reconstructing wasm result buffer with unsafe code.
([\#2813](https://github.com/anoma/namada/pull/2813))
12 changes: 2 additions & 10 deletions crates/vm_env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#![deny(rustdoc::broken_intra_doc_links)]
#![deny(rustdoc::private_intra_doc_links)]

use std::mem::ManuallyDrop;

use borsh::BorshDeserialize;
use namada_core::internal::{HostEnvResult, KeyVal};

Expand Down Expand Up @@ -278,16 +276,10 @@ pub fn read_from_buffer(
if HostEnvResult::is_fail(read_result) {
None
} else {
let result: Vec<u8> = Vec::with_capacity(read_result as _);
// The `result` will be dropped from the `target`, which is
// reconstructed from the same memory
let result = ManuallyDrop::new(result);
let result = vec![0u8; read_result as _];
let offset = result.as_slice().as_ptr() as u64;
unsafe { result_buffer(offset) };
let target = unsafe {
Vec::from_raw_parts(offset as _, read_result as _, read_result as _)
};
Some(target)
Some(result)
}
}

Expand Down

0 comments on commit 08ab918

Please sign in to comment.