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
use crate::wasm_stream::pipeline::StreamPass;
use wasm_encoder::{ExportKind, ExportSection, Module, RawSection};
pub struct StartsPreStreamPass {
pub is_vfs: bool,
pub is_library: bool,
pub start_export_name: String,
/// Additional export name for the Wasm start section function.
///
/// When set (non-VFS targets only), the Wasm start section function is
/// exported under this name in addition to `start_export_name`. This
/// allows the VFS to call the start section initializer independently
/// for reused pool worker threads.
pub thread_start_export_name: Option<String>,
/// Validate and mark a threaded VFS reactor before optimization/merging.
pub fold_reactor: bool,
}
impl StartsPreStreamPass {
pub fn new(is_vfs: bool, is_library: bool, start_export_name: String) -> Self {
Self {
is_vfs,
is_library,
start_export_name,
thread_start_export_name: None,
fold_reactor: false,
}
}
/// Sets the additional thread-start export name for target modules.
///
/// When set, the Wasm start section function is also exported under
/// this name so that `VirtualThreadPool` can call it before reusing
/// a worker thread for a new logical thread.
pub fn with_thread_start_export_name(mut self, name: String) -> Self {
self.thread_start_export_name = Some(name);
self
}
/// Enable VFS-only reactor validation and provenance markers for threaded builds.
pub fn with_reactor_folding(mut self, threads: bool) -> Self {
self.fold_reactor = threads;
self
}
}
impl StreamPass for StartsPreStreamPass {
fn run(&mut self, input_wasm: &[u8]) -> eyre::Result<Vec<u8>> {
use super::reactor_initialize::{
INITIALIZE_EXPORT, PREPARED_STATE_EXPORT, ReactorInitializer, STATE_EXPORT,
};
// Provenance must be checked on the VFS alone: target exports and
// optimizer-reordered merged function counts cannot establish ownership.
let fold = self.is_vfs
&& self.fold_reactor
&& ReactorInitializer::resolve(input_wasm, false)?.is_some();
let mut module = Module::new();
let parser = wasmparser::Parser::new(0);
let mut start_func_id = None;
let mut has_export_section = false;
let mut has_initialize = false;
let mut has_command_start = false;
// Pass 1: Find start section
for payload in wasmparser::Parser::new(0).parse_all(input_wasm) {
if let wasmparser::Payload::StartSection { func, range: _ } = payload? {
start_func_id = Some(func);
}
}
for payload in parser.parse_all(input_wasm) {
let payload = payload?;
match payload {
wasmparser::Payload::StartSection { .. } => {
// Remove start section by not emitting it
}
wasmparser::Payload::ExportSection(s) => {
has_export_section = true;
let mut exports = ExportSection::new();
for export in s {
let export = export?;
eyre::ensure!(
!matches!(export.name, INITIALIZE_EXPORT | PREPARED_STATE_EXPORT),
"reserved reactor export {} in input module",
export.name
);
// Diagnose ambiguous reactors before the merger can
// deduplicate exports or erase the command's _start name.
if self.is_vfs && export.name == "_initialize" {
eyre::ensure!(!has_initialize, "duplicate _initialize export");
eyre::ensure!(
export.kind == wasmparser::ExternalKind::Func,
"_initialize must be a function export"
);
has_initialize = true;
}
let kind = match export.kind {
wasmparser::ExternalKind::Func => ExportKind::Func,
wasmparser::ExternalKind::FuncExact => ExportKind::Func,
wasmparser::ExternalKind::Table => ExportKind::Table,
wasmparser::ExternalKind::Memory => ExportKind::Memory,
wasmparser::ExternalKind::Global => ExportKind::Global,
wasmparser::ExternalKind::Tag => ExportKind::Tag,
_ => unimplemented!(),
};
if export.name == "_start" && matches!(kind, ExportKind::Func) {
has_command_start = true;
start_func_id = Some(export.index);
} else {
let name = if fold {
match export.name {
"_initialize" => INITIALIZE_EXPORT,
STATE_EXPORT => PREPARED_STATE_EXPORT,
name => name,
}
} else {
export.name
};
exports.export(name, kind, export.index);
}
}
eyre::ensure!(
!(self.is_vfs && has_initialize && has_command_start),
"unexpected _initialize alongside command _start"
);
if let Some(func_id) = start_func_id {
exports.export(&self.start_export_name, ExportKind::Func, func_id);
// For target modules, also export the Wasm start section
// function under a separate name so that VirtualThreadPool
// can call it when reusing a worker thread.
if let Some(ref thread_start_name) = self.thread_start_export_name {
exports.export(thread_start_name, ExportKind::Func, func_id);
}
} else if !self.is_library {
// If there is no start function and it's not a library, we'd need to generate a dummy start.
// Generating a dummy start here is complex because it requires modifying Function/Code sections.
// We will defer dummy start generation to the post-combine pass where it's easier, or just let post-combine handle missing starts.
log::warn!(
"Module has no start section or _start export but is not marked as a library. Deferred dummy start generation."
);
}
module.section(&exports);
}
wasmparser::Payload::CustomSection(s) => {
module.section(&wasm_encoder::CustomSection {
name: s.name().into(),
data: std::borrow::Cow::Borrowed(s.data()),
});
}
_ => {
if let Some((id, range)) = payload.as_section() {
module.section(&RawSection {
id,
data: &input_wasm[range.clone()],
});
}
}
}
}
if !has_export_section && start_func_id.is_some() {
let mut exports = ExportSection::new();
exports.export(
&self.start_export_name,
ExportKind::Func,
start_func_id.unwrap(),
);
if let Some(ref thread_start_name) = self.thread_start_export_name {
exports.export(thread_start_name, ExportKind::Func, start_func_id.unwrap());
}
module.section(&exports);
}
Ok(module.finish())
}
}