forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent_resource.rs
More file actions
167 lines (155 loc) · 5.81 KB
/
component_resource.rs
File metadata and controls
167 lines (155 loc) · 5.81 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#![cfg(arc_try_new)]
use wasmtime::component::{Component, Linker, Resource, ResourceAny, ResourceType};
use wasmtime::{Config, Engine, Result, Store};
use wasmtime_fuzzing::oom::OomTest;
#[tokio::test]
async fn component_resource_any_resource_drop_async() -> Result<()> {
let component_bytes = {
let mut config = Config::new();
config.concurrency_support(false);
let engine = Engine::new(&config)?;
Component::new(
&engine,
r#"
(component
(type $t' (resource (rep i32)))
(export $t "t" (type $t'))
(core func $new (canon resource.new $t))
(func (export "mk") (param "r" u32) (result (own $t))
(canon lift (core func $new))
)
)
"#,
)?
.serialize()?
};
let mut config = Config::new();
config.enable_compiler(false);
config.concurrency_support(false);
let engine = Engine::new(&config)?;
let component = unsafe { Component::deserialize(&engine, &component_bytes)? };
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&component)?;
OomTest::new()
.allow_alloc_after_oom(true)
.test_async(|| async {
let mut store = Store::try_new(&engine, ())?;
let instance = instance_pre.instantiate_async(&mut store).await?;
let mk = instance.get_typed_func::<(u32,), (ResourceAny,)>(&mut store, "mk")?;
let (resource,) = mk.call_async(&mut store, (42,)).await?;
resource.resource_drop_async(&mut store).await?;
Ok(())
})
.await
}
#[test]
fn component_resource_any_resource_drop() -> Result<()> {
let component_bytes = {
let mut config = Config::new();
config.concurrency_support(false);
let engine = Engine::new(&config)?;
Component::new(
&engine,
r#"
(component
(type $t' (resource (rep i32)))
(export $t "t" (type $t'))
(core func $new (canon resource.new $t))
(func (export "mk") (param "r" u32) (result (own $t))
(canon lift (core func $new))
)
)
"#,
)?
.serialize()?
};
let mut config = Config::new();
config.enable_compiler(false);
config.concurrency_support(false);
let engine = Engine::new(&config)?;
let component = unsafe { Component::deserialize(&engine, &component_bytes)? };
let linker = Linker::<()>::new(&engine);
let instance_pre = linker.instantiate_pre(&component)?;
// Error propagation via anyhow allocates after OOM.
OomTest::new().allow_alloc_after_oom(true).test(|| {
let mut store = Store::try_new(&engine, ())?;
let instance = instance_pre.instantiate(&mut store)?;
let mk = instance.get_typed_func::<(u32,), (ResourceAny,)>(&mut store, "mk")?;
let (resource,) = mk.call(&mut store, (42,))?;
resource.resource_drop(&mut store)?;
Ok(())
})
}
struct MyResource;
#[test]
fn component_resource_any_try_from_resource() -> Result<()> {
let component_bytes = {
let mut config = Config::new();
config.concurrency_support(false);
let engine = Engine::new(&config)?;
Component::new(
&engine,
r#"
(component
(import "t" (type $t (sub resource)))
)
"#,
)?
.serialize()?
};
let mut config = Config::new();
config.enable_compiler(false);
config.concurrency_support(false);
let engine = Engine::new(&config)?;
let component = unsafe { Component::deserialize(&engine, &component_bytes)? };
let mut linker = Linker::<()>::new(&engine);
linker
.root()
.resource("t", ResourceType::host::<MyResource>(), |_, _| Ok(()))?;
let instance_pre = linker.instantiate_pre(&component)?;
// Error propagation via anyhow allocates after OOM.
OomTest::new().allow_alloc_after_oom(true).test(|| {
let mut store = Store::try_new(&engine, ())?;
let _instance = instance_pre.instantiate(&mut store)?;
let resource = Resource::<MyResource>::new_own(42);
let any = ResourceAny::try_from_resource(resource, &mut store)?;
let _typed: Resource<MyResource> = any.try_into_resource(&mut store)?;
Ok(())
})
}
#[test]
fn component_resource_any_try_into_resource() -> Result<()> {
let component_bytes = {
let mut config = Config::new();
config.concurrency_support(false);
let engine = Engine::new(&config)?;
Component::new(
&engine,
r#"
(component
(import "t" (type $t (sub resource)))
)
"#,
)?
.serialize()?
};
let mut config = Config::new();
config.enable_compiler(false);
config.concurrency_support(false);
let engine = Engine::new(&config)?;
let component = unsafe { Component::deserialize(&engine, &component_bytes)? };
let mut linker = Linker::<()>::new(&engine);
linker
.root()
.resource("t", ResourceType::host::<MyResource>(), |_, _| Ok(()))?;
let instance_pre = linker.instantiate_pre(&component)?;
// Error propagation via anyhow allocates after OOM.
OomTest::new().allow_alloc_after_oom(true).test(|| {
let mut store = Store::try_new(&engine, ())?;
let _instance = instance_pre.instantiate(&mut store)?;
let resource = Resource::<MyResource>::new_own(100);
let any = ResourceAny::try_from_resource(resource, &mut store)?;
let _back: Resource<MyResource> = any.try_into_resource(&mut store)?;
Ok(())
})
}