-
Notifications
You must be signed in to change notification settings - Fork 338
/
capabilities.rs
93 lines (82 loc) · 3.04 KB
/
capabilities.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use std::collections::HashSet;
use crate::static_analysis::ExportInfo;
const REQUIRES_PREFIX: &str = "requires_";
/// Takes a comma-separated string, splits it by commas, removes empty elements and returns a set of capabilities.
/// This can be used e.g. to initialize the cache.
pub fn capabilities_from_csv(csv: &str) -> HashSet<String> {
csv.split(',')
.map(|x| x.trim().to_string())
.filter(|f| !f.is_empty())
.collect()
}
/// Implementation for check_wasm, based on static analysis of the bytecode.
/// This is used for code upload, to perform check before compiling the Wasm.
pub fn required_capabilities_from_module(module: impl ExportInfo) -> HashSet<String> {
module
.exported_function_names(Some(REQUIRES_PREFIX))
.into_iter()
.filter_map(|name| {
if name.len() > REQUIRES_PREFIX.len() {
let (_, required_capability) = name.split_at(REQUIRES_PREFIX.len());
Some(required_capability.to_string())
} else {
None
}
})
.collect()
}
#[cfg(test)]
mod tests {
use crate::parsed_wasm::ParsedWasm;
use super::*;
#[test]
fn capabilities_from_csv_works() {
let set = capabilities_from_csv("foo, bar,baz ");
assert_eq!(set.len(), 3);
assert!(set.contains("foo"));
assert!(set.contains("bar"));
assert!(set.contains("baz"));
}
#[test]
fn capabilities_from_csv_skips_empty() {
let set = capabilities_from_csv("");
assert_eq!(set.len(), 0);
let set = capabilities_from_csv("a,,b");
assert_eq!(set.len(), 2);
assert!(set.contains("a"));
assert!(set.contains("b"));
let set = capabilities_from_csv("a,b,");
assert_eq!(set.len(), 2);
assert!(set.contains("a"));
assert!(set.contains("b"));
}
#[test]
fn required_capabilities_from_module_works() {
let wasm = wat::parse_str(
r#"(module
(type (func))
(func (type 0) nop)
(export "requires_water" (func 0))
(export "requires_" (func 0))
(export "requires_nutrients" (func 0))
(export "require_milk" (func 0))
(export "REQUIRES_air" (func 0))
(export "requires_sun" (func 0))
)"#,
)
.unwrap();
let module = ParsedWasm::parse(&wasm).unwrap();
let required_capabilities = required_capabilities_from_module(&module);
assert_eq!(required_capabilities.len(), 3);
assert!(required_capabilities.contains("nutrients"));
assert!(required_capabilities.contains("sun"));
assert!(required_capabilities.contains("water"));
}
#[test]
fn required_capabilities_from_module_works_without_exports_section() {
let wasm = wat::parse_str(r#"(module)"#).unwrap();
let module = ParsedWasm::parse(&wasm).unwrap();
let required_capabilities = required_capabilities_from_module(&module);
assert_eq!(required_capabilities.len(), 0);
}
}