-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
piecrust: change
ObjectCode
into Module
A `Module` now contains an actual `dusk_wasmtime::Module`, as opposed to just some bytes, since it allows us to check for some invariants.
- Loading branch information
Eduardo Leegwater Simões
committed
Nov 1, 2023
1 parent
78caaca
commit 71560df
Showing
6 changed files
with
156 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// | ||
// Copyright (c) DUSK NETWORK. All rights reserved. | ||
|
||
use std::io; | ||
use std::ops::Deref; | ||
use std::path::Path; | ||
|
||
use dusk_wasmtime::Engine; | ||
|
||
/// WASM object code belonging to a given contract. | ||
#[derive(Debug, Clone)] | ||
pub struct Module { | ||
module: dusk_wasmtime::Module, | ||
} | ||
|
||
fn check_single_memory(module: &dusk_wasmtime::Module) -> io::Result<()> { | ||
// Ensure the module only has one memory | ||
let n_memories = module | ||
.exports() | ||
.filter_map(|exp| exp.ty().memory().map(|_| ())) | ||
.count(); | ||
if n_memories != 1 { | ||
return Err(io::Error::new( | ||
io::ErrorKind::InvalidData, | ||
format!( | ||
"module has {} memories, but only one is allowed", | ||
n_memories | ||
), | ||
)); | ||
} | ||
Ok(()) | ||
} | ||
|
||
impl Module { | ||
pub(crate) fn new<B: AsRef<[u8]>>( | ||
engine: &Engine, | ||
bytes: B, | ||
) -> io::Result<Self> { | ||
let module = unsafe { | ||
dusk_wasmtime::Module::deserialize(engine, bytes).map_err(|e| { | ||
io::Error::new( | ||
io::ErrorKind::InvalidData, | ||
format!("failed to deserialize module: {}", e), | ||
) | ||
})? | ||
}; | ||
|
||
check_single_memory(&module)?; | ||
|
||
Ok(Self { module }) | ||
} | ||
|
||
pub(crate) fn from_file<P: AsRef<Path>>( | ||
engine: &Engine, | ||
path: P, | ||
) -> io::Result<Self> { | ||
let module = unsafe { | ||
dusk_wasmtime::Module::deserialize_file(engine, path).map_err( | ||
|e| { | ||
io::Error::new( | ||
io::ErrorKind::InvalidData, | ||
format!("failed to deserialize module: {}", e), | ||
) | ||
}, | ||
)? | ||
}; | ||
|
||
check_single_memory(&module)?; | ||
|
||
Ok(Self { module }) | ||
} | ||
|
||
pub(crate) fn serialize(&self) -> Vec<u8> { | ||
self.module | ||
.serialize() | ||
.expect("We don't use WASM components") | ||
} | ||
|
||
pub(crate) fn is_64(&self) -> bool { | ||
self.module | ||
.exports() | ||
.filter_map(|exp| exp.ty().memory().map(|mem_ty| mem_ty.is_64())) | ||
.next() | ||
.expect("We guarantee the module has one memory") | ||
} | ||
} | ||
|
||
impl Deref for Module { | ||
type Target = dusk_wasmtime::Module; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.module | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.