Skip to content

Commit 7779079

Browse files
committed
Add instantiate + migrate flags to AnalysisReport
1 parent 8a652d7 commit 7779079

File tree

2 files changed

+34
-82
lines changed

2 files changed

+34
-82
lines changed

packages/vm/src/cache.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::instance::{Instance, InstanceOptions};
1616
use crate::modules::{CachedModule, FileSystemCache, InMemoryCache, PinnedMemoryCache};
1717
use crate::parsed_wasm::ParsedWasm;
1818
use crate::size::Size;
19-
use crate::static_analysis::has_ibc_entry_points;
19+
use crate::static_analysis::{ExportInfo, REQUIRED_IBC_EXPORTS};
2020
use crate::wasm_backend::{compile, make_compiling_engine, make_runtime_engine};
2121

2222
const STATE_DIR: &str = "state";
@@ -117,7 +117,16 @@ pub struct Cache<A: BackendApi, S: Storage, Q: Querier> {
117117

118118
#[derive(PartialEq, Eq, Debug)]
119119
pub struct AnalysisReport {
120+
/// `true` if and only if all [`REQUIRED_IBC_EXPORTS`] exist as exported functions.
121+
/// This does not guarantee they are functional or even have the correct signatures.
120122
pub has_ibc_entry_points: bool,
123+
/// `true` if the module has an `instantiate` export.
124+
/// This does not guarantee it is functional or even has the correct signature.
125+
pub has_instantiate_entry_point: bool,
126+
/// `true` if the module has a `migrate` export.
127+
/// This does not guarantee it is functional or even has the correct signature.
128+
pub has_migrate_entry_point: bool,
129+
/// The set of capabilities the contract requires.
121130
pub required_capabilities: HashSet<String>,
122131
}
123132

@@ -274,8 +283,14 @@ where
274283
// Here we could use a streaming deserializer to slightly improve performance. However, this way it is DRYer.
275284
let wasm = self.load_wasm(checksum)?;
276285
let module = ParsedWasm::parse(&wasm)?;
286+
let exports = module.exported_function_names(None);
287+
277288
Ok(AnalysisReport {
278-
has_ibc_entry_points: has_ibc_entry_points(&module),
289+
has_ibc_entry_points: REQUIRED_IBC_EXPORTS
290+
.iter()
291+
.all(|required| exports.contains(*required)),
292+
has_instantiate_entry_point: exports.contains("instantiate"),
293+
has_migrate_entry_point: exports.contains("migrate"),
279294
required_capabilities: required_capabilities_from_module(&module),
280295
})
281296
}
@@ -538,6 +553,7 @@ mod tests {
538553

539554
static CONTRACT: &[u8] = include_bytes!("../testdata/hackatom.wasm");
540555
static IBC_CONTRACT: &[u8] = include_bytes!("../testdata/ibc_reflect.wasm");
556+
static EMPTY_CONTRACT: &[u8] = include_bytes!("../testdata/empty.wasm");
541557
// Invalid because it doesn't contain required memory and exports
542558
static INVALID_CONTRACT_WAT: &str = r#"(module
543559
(type $t0 (func (param i32) (result i32)))
@@ -1289,6 +1305,8 @@ mod tests {
12891305
report1,
12901306
AnalysisReport {
12911307
has_ibc_entry_points: false,
1308+
has_instantiate_entry_point: true,
1309+
has_migrate_entry_point: true,
12921310
required_capabilities: HashSet::new(),
12931311
}
12941312
);
@@ -1299,12 +1317,26 @@ mod tests {
12991317
report2,
13001318
AnalysisReport {
13011319
has_ibc_entry_points: true,
1320+
has_instantiate_entry_point: true,
1321+
has_migrate_entry_point: true,
13021322
required_capabilities: HashSet::from_iter([
13031323
"iterator".to_string(),
13041324
"stargate".to_string()
13051325
]),
13061326
}
13071327
);
1328+
1329+
let checksum3 = cache.save_wasm(EMPTY_CONTRACT).unwrap();
1330+
let report3 = cache.analyze(&checksum3).unwrap();
1331+
assert_eq!(
1332+
report3,
1333+
AnalysisReport {
1334+
has_ibc_entry_points: false,
1335+
has_instantiate_entry_point: false,
1336+
has_migrate_entry_point: false,
1337+
required_capabilities: HashSet::from(["iterator".to_string()]),
1338+
}
1339+
);
13081340
}
13091341

13101342
#[test]

packages/vm/src/static_analysis.rs

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,6 @@ impl ExportInfo for &wasmer::Module {
6060
}
6161
}
6262

63-
/// Returns true if and only if all IBC entry points ([`REQUIRED_IBC_EXPORTS`])
64-
/// exist as exported functions. This does not guarantee the entry points
65-
/// are functional and for simplicity does not even check their signatures.
66-
pub fn has_ibc_entry_points(module: impl ExportInfo) -> bool {
67-
let available_exports = module.exported_function_names(None);
68-
REQUIRED_IBC_EXPORTS
69-
.iter()
70-
.all(|required| available_exports.contains(*required))
71-
}
72-
7363
#[cfg(test)]
7464
mod tests {
7565
use crate::VmError;
@@ -225,74 +215,4 @@ mod tests {
225215
HashSet::from_iter(vec!["bar".to_string(), "baz".to_string()])
226216
);
227217
}
228-
229-
#[test]
230-
fn has_ibc_entry_points_works() {
231-
// Non-IBC contract
232-
let wasm = wat::parse_str(
233-
r#"(module
234-
(memory 3)
235-
(export "memory" (memory 0))
236-
237-
(type (func))
238-
(func (type 0) nop)
239-
(export "interface_version_8" (func 0))
240-
(export "instantiate" (func 0))
241-
(export "allocate" (func 0))
242-
(export "deallocate" (func 0))
243-
)"#,
244-
)
245-
.unwrap();
246-
let module = ParsedWasm::parse(&wasm).unwrap();
247-
assert!(!has_ibc_entry_points(&module));
248-
249-
// IBC contract
250-
let wasm = wat::parse_str(
251-
r#"(module
252-
(memory 3)
253-
(export "memory" (memory 0))
254-
255-
(type (func))
256-
(func (type 0) nop)
257-
(export "interface_version_8" (func 0))
258-
(export "instantiate" (func 0))
259-
(export "execute" (func 0))
260-
(export "allocate" (func 0))
261-
(export "deallocate" (func 0))
262-
(export "ibc_channel_open" (func 0))
263-
(export "ibc_channel_connect" (func 0))
264-
(export "ibc_channel_close" (func 0))
265-
(export "ibc_packet_receive" (func 0))
266-
(export "ibc_packet_ack" (func 0))
267-
(export "ibc_packet_timeout" (func 0))
268-
)"#,
269-
)
270-
.unwrap();
271-
let module = ParsedWasm::parse(&wasm).unwrap();
272-
assert!(has_ibc_entry_points(&module));
273-
274-
// Missing packet ack
275-
let wasm = wat::parse_str(
276-
r#"(module
277-
(memory 3)
278-
(export "memory" (memory 0))
279-
280-
(type (func))
281-
(func (type 0) nop)
282-
(export "interface_version_8" (func 0))
283-
(export "instantiate" (func 0))
284-
(export "execute" (func 0))
285-
(export "allocate" (func 0))
286-
(export "deallocate" (func 0))
287-
(export "ibc_channel_open" (func 0))
288-
(export "ibc_channel_connect" (func 0))
289-
(export "ibc_channel_close" (func 0))
290-
(export "ibc_packet_receive" (func 0))
291-
(export "ibc_packet_timeout" (func 0))
292-
)"#,
293-
)
294-
.unwrap();
295-
let module = ParsedWasm::parse(&wasm).unwrap();
296-
assert!(!has_ibc_entry_points(&module));
297-
}
298218
}

0 commit comments

Comments
 (0)