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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
fn host_op(op: &wasmparser::Operator<'_>) -> HostOp {
match op {
wasmparser::Operator::LocalGet { local_index } => HostOp::LocalGet(*local_index),
wasmparser::Operator::LocalSet { local_index } => HostOp::LocalSet(*local_index),
wasmparser::Operator::I32Const { value } => HostOp::I32Const(*value),
wasmparser::Operator::ArrayLen => HostOp::ArrayLen,
wasmparser::Operator::ArrayGetU { array_type_index } => {
HostOp::ArrayGetU(*array_type_index)
}
wasmparser::Operator::ArrayGet { array_type_index } => HostOp::ArrayGet(*array_type_index),
wasmparser::Operator::ArrayNewDefault { array_type_index } => {
HostOp::ArrayNewDefault(*array_type_index)
}
wasmparser::Operator::ArrayCopy {
array_type_index_dst,
array_type_index_src,
} => HostOp::ArrayCopy(*array_type_index_dst, *array_type_index_src),
wasmparser::Operator::I32Ne => HostOp::I32Ne,
wasmparser::Operator::I32GeU => HostOp::I32GeU,
wasmparser::Operator::I32Add => HostOp::I32Add,
wasmparser::Operator::If { .. } => HostOp::If,
wasmparser::Operator::Block { .. } => HostOp::Block,
wasmparser::Operator::Loop { .. } => HostOp::Loop,
wasmparser::Operator::Br { relative_depth } => HostOp::Br(*relative_depth),
wasmparser::Operator::BrIf { relative_depth } => HostOp::BrIf(*relative_depth),
wasmparser::Operator::Return => HostOp::Return,
wasmparser::Operator::End => HostOp::End,
_ => HostOp::Other,
}
}
fn string_host_roles(
host_roles: &std::collections::HashMap<u32, HostRole>,
) -> StringHostRoles {
let mut roles = host_roles
.iter()
.filter_map(|(idx, role)| match role {
HostRole::StringEq => Some((*idx, StringHostRole::Eq)),
HostRole::StringConcat => Some((*idx, StringHostRole::Concat)),
HostRole::Add | HostRole::Mul | HostRole::Sub => None,
})
.collect::<Vec<_>>();
roles.sort_by_key(|entry| entry.0);
roles
}
fn is_string_eq_host(
entry: &CodeEntry,
params: &[TyKind],
result: Option<TyKind>,
string_byte_array_types: &std::collections::HashSet<u32>,
) -> bool {
let [TyKind::Ref { idx: lhs, .. }, TyKind::Ref { idx: rhs, .. }] = params else {
return false;
};
if lhs != rhs || result != Some(TyKind::I32) || entry.nlocals != 2 || !entry.calls.is_empty() {
return false;
}
use HostOp::*;
let t = *lhs;
if !string_byte_array_types.contains(&t) {
return false;
}
let expected = [
LocalGet(0),
ArrayLen,
LocalGet(1),
ArrayLen,
I32Ne,
If,
I32Const(0),
Return,
End,
LocalGet(0),
ArrayLen,
LocalSet(2),
I32Const(0),
LocalSet(3),
Block,
Loop,
LocalGet(3),
LocalGet(2),
I32GeU,
BrIf(1),
LocalGet(0),
LocalGet(3),
ArrayGetU(t),
LocalGet(1),
LocalGet(3),
ArrayGetU(t),
I32Ne,
If,
I32Const(0),
Return,
End,
LocalGet(3),
I32Const(1),
I32Add,
LocalSet(3),
Br(0),
End,
End,
I32Const(1),
End,
];
entry.host_ops.as_slice() == expected
}
/// Identify the `String.concat` runtime helper by its byte-exact opcode shape.
///
/// The compiler lowers `String.concat` (and the `[s1, ..., sN].join` pattern)
/// to a fixed two-loop helper: the first loop sums the byte-lengths of every
/// element array, `array.new_default` allocates the result, and the second
/// loop `array.copy`s each element into place. The helper takes ONE argument
/// (the container array of string-arrays) and returns the byte-concatenated
/// array. As with [`is_string_eq_host`], the match is byte-exact — a helper
/// with the right signature but a different body is NOT recognised, so a
/// tampered helper fails re-derivation at verify time.
///
/// `container_ty` is the type index of the container array (array of
/// string-arrays); `byte_ty` is the type index of both the element string
/// arrays and the result byte array. Both must be `(array (mut i8))`
/// composites — pinned by membership in `string_byte_array_types`.
fn is_string_concat_host(
entry: &CodeEntry,
params: &[TyKind],
result: Option<TyKind>,
string_byte_array_types: &std::collections::HashSet<u32>,
) -> bool {
// Signature: `(ref null container_ty) -> (ref null byte_ty)`, one arg.
let [TyKind::Ref { idx: container_ty, .. }] = params else {
return false;
};
let Some(TyKind::Ref { idx: result_ty, .. }) = result else {
return false;
};
// The helper has exactly 7 locals (i32 i32 i32 ref i32 ref i32) and no
// outgoing calls — everything it does is inline loops + array ops.
if entry.nlocals != 7 || !entry.calls.is_empty() {
return false;
}
let container = *container_ty;
let byte = result_ty;
// Both the result/element type and the container type must be byte arrays.
// (The container is an array of byte-arrays, so its element type is the
// byte array type; we check the byte type membership directly, and below
// we pin the container type into the array.get/len operands.)
if !string_byte_array_types.contains(&byte) {
return false;
}
use HostOp::*;
// First loop: total length = sum of element array lengths.
// Second loop: allocate result, array.copy each element in place.
let expected = [
// --- length-accumulation loop ---
LocalGet(0),
ArrayLen,
LocalSet(3),
I32Const(0),
LocalSet(1),
I32Const(0),
LocalSet(2),
Block,
Loop,
LocalGet(2),
LocalGet(3),
I32GeU,
BrIf(1),
LocalGet(1),
LocalGet(0),
LocalGet(2),
ArrayGet(container),
ArrayLen,
I32Add,
LocalSet(1),
LocalGet(2),
I32Const(1),
I32Add,
LocalSet(2),
Br(0),
End,
End,
// --- allocation + copy loop ---
LocalGet(1),
ArrayNewDefault(byte),
LocalSet(6),
I32Const(0),
LocalSet(7),
I32Const(0),
LocalSet(2),
Block,
Loop,
LocalGet(2),
LocalGet(3),
I32GeU,
BrIf(1),
LocalGet(0),
LocalGet(2),
ArrayGet(container),
LocalSet(4),
LocalGet(4),
ArrayLen,
LocalSet(5),
LocalGet(6),
LocalGet(7),
LocalGet(4),
I32Const(0),
LocalGet(5),
ArrayCopy(byte, byte),
LocalGet(7),
LocalGet(5),
I32Add,
LocalSet(7),
LocalGet(2),
I32Const(1),
I32Add,
LocalSet(2),
Br(0),
End,
End,
LocalGet(6),
End,
];
entry.host_ops.as_slice() == expected
}
fn resolve_data_ops(ops: Vec<Op>, data_segments: &[Option<Vec<u8>>]) -> Vec<Op> {
ops.into_iter()
.map(|op| match op {
Op::ArrayNewDataUnresolved {
type_idx,
data_idx,
offset,
len,
} if offset == 0 && len >= 0 => {
let Some(Some(bytes)) = data_segments.get(data_idx as usize) else {
return Op::Other;
};
if bytes.len() == len as usize {
Op::ArrayNewData {
type_idx,
data_idx,
bytes: bytes.clone(),
}
} else {
Op::Other
}
}
Op::ArrayNewDataUnresolved { .. } => Op::Other,
other => other,
})
.collect()
}
/// The name of an instruction the opcode vocabulary does not model, for the
/// decline reason. `wasmparser`'s `Debug` prints the instruction first and its
/// operands after (`I32Sub`, `LocalTee { local_index: 3 }`), so the leading
/// identifier is exactly the instruction and nothing else. Diagnostic only: no
/// admission decision ever reads this string.
fn operator_name(op: &wasmparser::Operator<'_>) -> String {
format!("{op:?}")
.split(|c: char| !c.is_ascii_alphanumeric())
.next()
.unwrap_or_default()
.to_string()
}
fn heap_type_index(hty: wasmparser::HeapType) -> Option<u32> {
match hty {
wasmparser::HeapType::Concrete(idx) => idx.as_module_index(),
// Kernel parity: exact references are not plain 0x63 s33 refs.
wasmparser::HeapType::Exact(_) => None,
wasmparser::HeapType::Abstract { .. } => None,
}
}