Skip to content
This repository was archived by the owner on Dec 11, 2024. It is now read-only.

Commit bb06f33

Browse files
authored
Merge pull request #13 from golemcloud/vigoo/collect-memory-info
Ability to enumerate all linear memories
2 parents 440db07 + 00a60e6 commit bb06f33

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

src/analysis/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
use crate::component::*;
16+
use crate::core::Mem;
1617
use crate::AstCustomization;
1718
use mappable_rc::Mrc;
1819
use std::cell::RefCell;
@@ -233,6 +234,24 @@ impl<Ast: AstCustomization + 'static> AnalysisContext<Ast> {
233234
Ok(result)
234235
}
235236

237+
/// Gets all the memories (not just the exported ones) from all modules within the WASM component
238+
pub fn get_all_memories(&self) -> AnalysisResult<Vec<Mem>> {
239+
let mut result = Vec::new();
240+
241+
let mut component_stack = vec![self.get_component()];
242+
while let Some(component) = component_stack.pop() {
243+
for module in component.modules() {
244+
for mem in module.mems() {
245+
result.push((*mem).clone());
246+
}
247+
}
248+
for inner_component in component.components() {
249+
component_stack.push(inner_component.clone());
250+
}
251+
}
252+
Ok(result)
253+
}
254+
236255
pub fn warnings(&self) -> Vec<AnalysisWarning> {
237256
self.warnings.borrow().clone()
238257
}

src/core/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,8 @@ impl Section<CoreIndexSpace, CoreSectionType> for FuncType {
371371
///
372372
#[derive(Debug, Clone, PartialEq, Eq)]
373373
pub struct Limits {
374-
min: u64,
375-
max: Option<u64>,
374+
pub min: u64,
375+
pub max: Option<u64>,
376376
}
377377

378378
/// Memory types classify linear memories and their size range.

tests/mems.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use golem_wasm_ast::analysis::AnalysisContext;
2+
use golem_wasm_ast::component::Component;
3+
use golem_wasm_ast::core::{Limits, Mem, MemType};
4+
use golem_wasm_ast::IgnoreAllButMetadata;
5+
6+
#[test]
7+
fn mems_shopping_cart_component() {
8+
let source_bytes = include_bytes!("../wasm/shopping-cart.wasm");
9+
let component: Component<IgnoreAllButMetadata> = Component::from_bytes(source_bytes).unwrap();
10+
11+
let state = AnalysisContext::new(component);
12+
let mems = state.get_all_memories().unwrap();
13+
14+
pretty_assertions::assert_eq!(
15+
mems,
16+
vec![Mem {
17+
mem_type: MemType {
18+
limits: Limits { min: 17, max: None }
19+
}
20+
}]
21+
)
22+
}
23+
24+
#[test]
25+
fn mems_shopping_cart_resource_component() {
26+
let source_bytes = include_bytes!("../wasm/shopping-cart-resource.wasm");
27+
let component: Component<IgnoreAllButMetadata> = Component::from_bytes(source_bytes).unwrap();
28+
29+
let state = AnalysisContext::new(component);
30+
let mems = state.get_all_memories().unwrap();
31+
32+
pretty_assertions::assert_eq!(
33+
mems,
34+
vec![Mem {
35+
mem_type: MemType {
36+
limits: Limits { min: 17, max: None }
37+
}
38+
}]
39+
)
40+
}
41+
42+
#[test]
43+
fn mems_file_service_component() {
44+
let source_bytes = include_bytes!("../wasm/file-service.wasm");
45+
let component: Component<IgnoreAllButMetadata> = Component::from_bytes(source_bytes).unwrap();
46+
47+
let state = AnalysisContext::new(component);
48+
let mems = state.get_all_memories().unwrap();
49+
50+
pretty_assertions::assert_eq!(
51+
mems,
52+
vec![Mem {
53+
mem_type: MemType {
54+
limits: Limits { min: 17, max: None }
55+
}
56+
}]
57+
)
58+
}
59+
60+
#[test]
61+
fn mems_auction_registry_composed_component() {
62+
let source_bytes = include_bytes!("../wasm/auction_registry_composed.wasm");
63+
let component: Component<IgnoreAllButMetadata> = Component::from_bytes(source_bytes).unwrap();
64+
65+
let state = AnalysisContext::new(component);
66+
let mems = state.get_all_memories().unwrap();
67+
68+
pretty_assertions::assert_eq!(
69+
mems,
70+
vec![
71+
Mem {
72+
mem_type: MemType {
73+
limits: Limits { min: 17, max: None }
74+
}
75+
},
76+
Mem {
77+
mem_type: MemType {
78+
limits: Limits { min: 17, max: None }
79+
}
80+
},
81+
]
82+
)
83+
}

0 commit comments

Comments
 (0)