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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//! Resolve and fold the official reactor initializer without copying its body.
use eyre::{Result, ensure};
use wasm_encoder::{BlockType, Function, Instruction as I, MemArg};
use wasmparser::{CompositeInnerType, ExternalKind, Operator, Parser, Payload, TypeRef, ValType};
pub const STATE_EXPORT: &str = "__wasip1_vfs_reactor_init_state";
/// Reserved provenance/optimization anchor, never a Component reactor export.
pub const INITIALIZE_EXPORT: &str = "__wvl_reactor_initialize";
pub const PREPARED_STATE_EXPORT: &str = "__wvl_reactor_init_state";
/// Validated original indices; only the function index needs post-combine rebinding.
#[derive(Debug)]
pub struct ReactorInitializer {
pub function_index: u32,
state_global: u32,
}
impl ReactorInitializer {
/// Validate a `() -> ()` reactor with WVL's shared state ABI. Call on the VFS
/// alone before merging (`prepared = false`), then on its reserved markers
/// after merging (`prepared = true`). No stale function-count ownership test
/// is used. The state word is linker-allocated, never borrowed from libc.
pub fn resolve(wasm: &[u8], prepared: bool) -> Result<Option<Self>> {
let initialize_name = if prepared {
INITIALIZE_EXPORT
} else {
"_initialize"
};
let state_name = if prepared {
PREPARED_STATE_EXPORT
} else {
STATE_EXPORT
};
let mut initialize = None;
let mut state = None;
let mut command = false;
let mut function_types = Vec::new();
let mut void_types = Vec::new();
let mut imported_functions = 0;
let mut globals = Vec::new();
let mut memory_bytes = None;
let mut other_exports = Vec::new();
for payload in Parser::new(0).parse_all(wasm) {
match payload? {
Payload::TypeSection(s) => {
for group in s {
for ty in group?.into_types() {
void_types.push(matches!(ty.composite_type.inner,
CompositeInnerType::Func(ref f) if f.params().is_empty() && f.results().is_empty()));
}
}
}
Payload::ImportSection(s) => {
for group in s {
for import in group? {
match import?.1.ty {
TypeRef::Func(ty) => {
function_types.push(ty);
imported_functions += 1;
}
TypeRef::Global(_) => globals.push(None),
TypeRef::Memory(m) => {
memory_bytes
.get_or_insert(m.initial << m.page_size_log2.unwrap_or(16));
}
_ => {}
}
}
}
}
Payload::FunctionSection(s) => {
for ty in s {
function_types.push(ty?);
}
}
Payload::MemorySection(s) => {
for m in s {
let m = m?;
memory_bytes.get_or_insert(m.initial << m.page_size_log2.unwrap_or(16));
}
}
Payload::GlobalSection(s) => {
for g in s {
let g = g?;
let mut ops = g.init_expr.get_operators_reader();
let address = match ops.read()? {
Operator::I32Const { value }
if !g.ty.mutable
&& g.ty.content_type == ValType::I32
&& matches!(ops.read()?, Operator::End) =>
{
Some(value as u32)
}
_ => None,
};
globals.push(address);
}
}
Payload::ExportSection(s) => {
for e in s {
let e = e?;
match e.name {
name if name == initialize_name => {
ensure!(initialize.is_none(), "duplicate _initialize export");
ensure!(
e.kind == ExternalKind::Func,
"_initialize must be a function export"
);
initialize = Some(e.index);
}
name if name == state_name => {
ensure!(
state.is_none(),
"duplicate reactor initialization state export"
);
ensure!(
e.kind == ExternalKind::Global,
"reactor initialization state must be a global address"
);
state = Some(e.index);
}
"_start" => command = true,
_ if e.kind == ExternalKind::Func => other_exports.push(e.index),
_ => {}
}
}
}
_ => {}
}
}
let Some(function_index) = initialize else {
return Ok(None);
};
ensure!(!command, "unexpected _initialize alongside command _start");
ensure!(
function_index >= imported_functions,
"_initialize must be a defined VFS function"
);
let ty = function_types
.get(function_index as usize)
.and_then(|ty| void_types.get(*ty as usize));
ensure!(
ty == Some(&true),
"_initialize must have signature () -> ()"
);
ensure!(
!other_exports.contains(&function_index),
"unexpected alias of _initialize"
);
let state_global = state.ok_or_else(|| eyre::eyre!(
"folding _initialize requires {STATE_EXPORT}; rebuild the VFS with the matching wasi_virt_layer library"))?;
let address = globals
.get(state_global as usize)
.copied()
.flatten()
.ok_or_else(|| {
eyre::eyre!("reactor initialization state must be a defined immutable i32 address")
})?;
ensure!(
address % 4 == 0 && memory_bytes.is_some_and(|size| u64::from(address) + 4 <= size),
"reactor initialization state must be aligned and inside VFS memory"
);
// Reject pre-existing calls rather than inserting a second start call.
// This also makes re-running the pass on an unexpected input fail closed.
for payload in Parser::new(0).parse_all(wasm) {
let payload = payload?;
if let Payload::ElementSection(s) = &payload {
for element in s.clone() {
match element?.items {
wasmparser::ElementItems::Functions(indices) => {
for index in indices {
ensure!(
index? != function_index,
"unexpected table reference to _initialize"
);
}
}
wasmparser::ElementItems::Expressions(_, expressions) => {
for expression in expressions {
for op in expression?.get_operators_reader() {
ensure!(
!matches!(op?, Operator::RefFunc { function_index: i } if i == function_index),
"unexpected table reference to _initialize"
);
}
}
}
}
}
}
if let Payload::CodeSectionEntry(body) = payload {
for op in body.get_operators_reader()? {
ensure!(
!matches!(op?, Operator::Call { function_index: i }
| Operator::ReturnCall { function_index: i }
| Operator::RefFunc { function_index: i } if i == function_index),
"unexpected existing reference to _initialize; cannot fold twice"
);
}
}
}
Ok(Some(Self {
function_index,
state_global,
}))
}
/// Emit one direct call, elected across shared instances. Publish completion
/// only after constructors return; workers must not enter target starts early.
/// A trapping initializer poisons this shared runtime (state remains 1).
pub fn emit(&self, f: &mut Function, rebind: impl Fn(u32) -> u32) {
let mem = MemArg {
offset: 0,
align: 2,
memory_index: 0,
};
for i in [
I::GlobalGet(self.state_global),
I::I32Const(0),
I::I32Const(1),
I::I32AtomicRmwCmpxchg(mem),
I::I32Eqz,
I::If(BlockType::Empty),
I::Call(rebind(self.function_index)),
I::GlobalGet(self.state_global),
I::I32Const(2),
I::I32AtomicStore(mem),
I::GlobalGet(self.state_global),
I::I32Const(-1),
I::MemoryAtomicNotify(mem),
I::Drop,
I::Else,
I::Block(BlockType::Empty),
I::Loop(BlockType::Empty),
I::GlobalGet(self.state_global),
I::I32AtomicLoad(mem),
I::I32Const(2),
I::I32Eq,
I::BrIf(1),
I::GlobalGet(self.state_global),
I::I32Const(1),
I::I64Const(-1),
I::MemoryAtomicWait32(mem),
I::Drop,
I::Br(0),
I::End,
I::End,
I::End,
] {
f.instruction(&i);
}
}
}