Skip to content

Commit

Permalink
Parse and validate wasm before static analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Jul 24, 2023
1 parent 1fb845d commit ab0f885
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 241 deletions.
10 changes: 4 additions & 6 deletions packages/vm/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use crate::errors::{VmError, VmResult};
use crate::filesystem::mkdir_p;
use crate::instance::{Instance, InstanceOptions};
use crate::modules::{CachedModule, FileSystemCache, InMemoryCache, PinnedMemoryCache};
use crate::parsed_wasm::ParsedWasm;
use crate::size::Size;
use crate::static_analysis::{deserialize_exports, has_ibc_entry_points};
use crate::static_analysis::has_ibc_entry_points;
use crate::wasm_backend::{compile, make_compiling_engine, make_runtime_engine};

const STATE_DIR: &str = "state";
Expand Down Expand Up @@ -254,7 +255,7 @@ where
pub fn analyze(&self, checksum: &Checksum) -> VmResult<AnalysisReport> {
// Here we could use a streaming deserializer to slightly improve performance. However, this way it is DRYer.
let wasm = self.load_wasm(checksum)?;
let module = deserialize_exports(&wasm)?;
let module = ParsedWasm::parse(&wasm)?;
Ok(AnalysisReport {
has_ibc_entry_points: has_ibc_entry_points(&module),
required_capabilities: required_capabilities_from_module(&module),
Expand Down Expand Up @@ -571,10 +572,7 @@ mod tests {
let save_result = cache.save_wasm(&wasm);
match save_result.unwrap_err() {
VmError::StaticValidationErr { msg, .. } => {
assert_eq!(
msg,
"Wasm contract missing a required marker export: interface_version_*"
)
assert_eq!(msg, "Wasm contract must contain exactly one memory")
}
e => panic!("Unexpected error {e:?}"),
}
Expand Down
7 changes: 4 additions & 3 deletions packages/vm/src/capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ pub fn required_capabilities_from_module(module: impl ExportInfo) -> HashSet<Str

#[cfg(test)]
mod tests {
use crate::parsed_wasm::ParsedWasm;

use super::*;
use crate::static_analysis::deserialize_exports;

#[test]
fn capabilities_from_csv_works() {
Expand Down Expand Up @@ -73,7 +74,7 @@ mod tests {
)"#,
)
.unwrap();
let module = deserialize_exports(&wasm).unwrap();
let module = ParsedWasm::parse(&wasm).unwrap();

let required_capabilities = required_capabilities_from_module(&module);
assert_eq!(required_capabilities.len(), 3);
Expand All @@ -85,7 +86,7 @@ mod tests {
#[test]
fn required_capabilities_from_module_works_without_exports_section() {
let wasm = wat::parse_str(r#"(module)"#).unwrap();
let module = deserialize_exports(&wasm).unwrap();
let module = ParsedWasm::parse(&wasm).unwrap();
let required_capabilities = required_capabilities_from_module(&module);
assert_eq!(required_capabilities.len(), 0);
}
Expand Down
Loading

0 comments on commit ab0f885

Please sign in to comment.