Skip to content

Commit

Permalink
piecrust: init_traps before making any memories
Browse files Browse the repository at this point in the history
This is necessary to ensure that the handler for segfaults in `crumbles`
is properly set, instead of using the default `wasmer` handler.
  • Loading branch information
Eduardo Leegwater Simões committed Aug 28, 2023
1 parent 8475e1b commit 93bcd45
Showing 1 changed file with 5 additions and 21 deletions.
26 changes: 5 additions & 21 deletions piecrust/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::sync::Arc;
use std::thread;

use tempfile::tempdir;
use wasmer_vm::init_traps;

use crate::session::{Session, SessionData};
use crate::store::ContractStore;
Expand Down Expand Up @@ -53,6 +54,8 @@ impl VM {
/// # Errors
/// If the directory contains unparseable or inconsistent data.
pub fn new<P: AsRef<Path>>(root_dir: P) -> Result<Self, Error> {
init_traps();

let store = ContractStore::new(root_dir)
.map_err(|err| PersistenceError(Arc::new(err)))?;
Ok(Self {
Expand All @@ -69,6 +72,8 @@ impl VM {
/// # Errors
/// If creating a temporary directory fails.
pub fn ephemeral() -> Result<Self, Error> {
init_traps();

let tmp = tempdir().map_err(|err| PersistenceError(Arc::new(err)))?;
let tmp = tmp.path().to_path_buf();

Expand Down Expand Up @@ -125,27 +130,6 @@ impl VM {
self.store.commits().into_iter().map(Into::into).collect()
}

/// Remove the diff files from a commit by applying them to the base
/// memories, and writing them back to disk.
///
/// # Errors
/// If this function fails, it may be due to any number of reasons:
///
/// - [`remove_file`] may fail
/// - [`write`] may fail
///
/// Failing may result in a corrupted commit, and the user is encouraged to
/// call [`delete_commit`].
///
/// [`remove_file`]: std::fs::remove_file
/// [`write`]: std::fs::write
/// [`delete_commit`]: VM::delete_commit
pub fn squash_commit(&self, root: [u8; 32]) -> Result<(), Error> {
self.store
.squash_commit(root.into())
.map_err(|err| PersistenceError(Arc::new(err)))
}

/// Deletes the given commit from disk.
pub fn delete_commit(&self, root: [u8; 32]) -> Result<(), Error> {
self.store
Expand Down

0 comments on commit 93bcd45

Please sign in to comment.