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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use crate::ir::DataType;
use crate::serial::wire::decode::reject_reserved_extension_id;
use crate::serial::wire::tags::{
atomic_op_from_tag, bin_op_from_tag, data_type_from_tag, un_op_from_tag,
};
use crate::serial::wire::{Expr, Node, Reader, MAX_ARGS, MAX_DECODE_DEPTH, MAX_NODES};
impl Reader<'_> {
/// Decode a statement-node vector from the wire format.
///
/// # Wire-format layout
///
/// A little-endian `u32` count (checked against `MAX_NODES`), followed by
/// that many `Node` records decoded via `Reader::node`.
///
/// # Bounds checks
///
/// * Count > `MAX_NODES` → rejected with a `Fix:` error (I10).
/// * Underlying bytes truncated before the last node → rejected by the
/// sub-decode calls.
///
/// # Return semantics
///
/// * `Ok(Vec<Node>)` – owned vector of decoded statements.
/// * `Err(String)` – actionable `Fix:`-prefixed error (truncation, unknown
/// tag, depth limit, etc.).
#[inline]
pub(crate) fn nodes(&mut self) -> Result<Vec<Node>, String> {
let count = self.bounded_len(MAX_NODES, "node count")?;
let mut nodes = Vec::with_capacity(count);
for _ in 0..count {
nodes.push(self.node()?);
}
Ok(nodes)
}
/// Decode a single `Node` from the wire format.
///
/// # Decode-time invariants
///
/// Reads a one-byte tag and dispatches:
/// * `0` – `Let` (name string, value expression)
/// * `1` – `Assign` (name string, value expression)
/// * `2` – `Store` (buffer name string, index expression, value expression)
/// * `3` – `If` (condition expression, then-body node list, else-body node list)
/// * `4` – `Loop` (variable name string, from expression, to expression,
/// body node list)
/// * `5` – `Return`
/// * `6` – `Block` (nested node list)
/// * `7` – `Barrier`
/// * `8` – `IndirectDispatch` (count buffer name string, count offset `u64`)
/// * `9` – `AsyncLoad` (tag string)
/// * `10` – `AsyncWait` (tag string)
/// * `13` – `Trap` (address expression, tag string)
/// * `14` – `Resume` (tag string)
/// * any other tag – rejected as unknown.
///
/// # Recursion guard (L.1.35)
///
/// Increments `Reader::depth` on entry and decrements on exit.
/// If the depth already equals `MAX_DECODE_DEPTH`, the blob is rejected
/// **before** any further decode to prevent stack-overflow DoS from
/// deeply nested `Block`, `If`, or `Loop` bodies.
///
/// # Return semantics
///
/// * `Ok(Node)` – successfully decoded statement.
/// * `Err(String)` – actionable `Fix:`-prefixed error describing the
/// failure (unknown tag, depth exceeded, truncation, etc.).
#[inline]
pub(crate) fn node(&mut self) -> Result<Node, String> {
// Recursion guard: every `node()` enter increments depth, every
// exit decrements. Nested decode stops at `MAX_DECODE_DEPTH`.
if self.depth >= MAX_DECODE_DEPTH {
return Err(format!(
"Fix: IR wire format exceeds maximum decode depth {MAX_DECODE_DEPTH}; flatten deeply nested Block/If/Loop structures or reject this untrusted blob."
));
}
self.depth += 1;
let result = self.node_inner();
self.depth -= 1;
result
}
fn node_inner(&mut self) -> Result<Node, String> {
match self.u8()? {
0 => Ok(Node::Let {
name: self.string()?.into(),
value: self.expr()?,
}),
1 => Ok(Node::Assign {
name: self.string()?.into(),
value: self.expr()?,
}),
2 => Ok(Node::Store {
buffer: self.string()?.into(),
index: self.expr()?,
value: self.expr()?,
}),
3 => Ok(Node::If {
cond: self.expr()?,
then: self.nodes()?,
otherwise: self.nodes()?,
}),
4 => Ok(Node::Loop {
var: self.string()?.into(),
from: self.expr()?,
to: self.expr()?,
body: self.nodes()?,
}),
5 => Ok(Node::Return),
6 => Ok(Node::Block(self.nodes()?)),
7 => Ok(Node::Barrier {
ordering: crate::memory_model::MemoryOrdering::from_wire_tag(self.u8()?)?,
}),
8 => Ok(Node::IndirectDispatch {
count_buffer: self.string()?.into(),
count_offset: self.u64()?,
}),
9 => {
let source: crate::ir::Ident = self.string()?.into();
let destination: crate::ir::Ident = self.string()?.into();
let offset = self.expr()?;
let size = self.expr()?;
let tag: crate::ir::Ident = self.string()?.into();
Ok(Node::async_load_ext(source, destination, offset, size, tag))
}
10 => Ok(Node::AsyncWait {
tag: self.string()?.into(),
}),
12 => {
let source: crate::ir::Ident = self.string()?.into();
let destination: crate::ir::Ident = self.string()?.into();
let offset = self.expr()?;
let size = self.expr()?;
let tag: crate::ir::Ident = self.string()?.into();
Ok(Node::async_store(source, destination, offset, size, tag))
}
13 => Ok(Node::trap(self.expr()?, self.string()?)),
14 => Ok(Node::resume(self.string()?)),
11 => {
let generator: crate::ir::Ident = self.string()?.into();
let presence = self.u8()?;
let source_region = match presence {
0 => None,
1 => Some(crate::ir::model::expr::GeneratorRef {
name: self.string()?,
}),
other => {
return Err(format!(
"Fix: Region source_region presence byte must be 0 or 1, got {other}"
));
}
};
let body = self.nodes()?;
Ok(Node::Region {
generator,
source_region,
body: std::sync::Arc::new(body),
})
}
0x80 => {
let kind = self.string()?;
let payload_len = self.bounded_len(MAX_ARGS * 1024, "opaque node payload")?;
let payload = self.bytes(payload_len)?;
crate::extension::decode_opaque_node(&kind, &payload)
}
tag => Err(format!(
"Fix: unknown IR node tag {tag}; use a Program serializer compatible with this vyre version."
)),
}
}
/// Decode a single `Expr` from the wire format.
///
/// # Decode-time invariants
///
/// Reads a one-byte tag and dispatches:
/// * `0` – `LitU32` (little-endian `u32`)
/// * `1` – `LitI32` (little-endian `i32` reinterpreted from `u32` bits)
/// * `2` – `LitBool` (`0` = false, non-zero = true)
/// * `3` – `Var` (string name)
/// * `4` – `Load` (buffer name string, index expression)
/// * `5` – `BufLen` (buffer name string)
/// * `6` – `InvocationId` (axis `u8`)
/// * `7` – `WorkgroupId` (axis `u8`)
/// * `8` – `LocalId` (axis `u8`)
/// * `9` – `BinOp` (operator tag, left expr, right expr)
/// * `10` – `UnOp` (operator tag, operand expr)
/// * `11` – `Call` (op id string, argument count ≤ `MAX_ARGS`, arguments)
/// * `12` – `Select` (cond expr, true expr, false expr)
/// * `13` – `Cast` (target `DataType`, value expr)
/// * `14` – `Atomic` (operator tag, buffer name string, index expr,
/// expected-expr flag, value expr)
/// * `15` – `LitF32` (`f32` reinterpreted from `u32` bits)
/// * `16` – `Fma` (a expr, b expr, c expr)
/// * any other tag – rejected as unknown.
///
/// # Recursion guard (L.1.35)
///
/// Increments the shared `Reader::depth` counter on entry and decrements
/// on exit. If the depth already equals `MAX_DECODE_DEPTH`, the blob is
/// rejected **before** any nested expression is decoded. This prevents
/// stack-overflow DoS from arbitrarily nested `BinOp`, `UnOp`, `Select`,
/// `Cast`, or `Call` argument trees.
///
/// # Return semantics
///
/// * `Ok(Expr)` – successfully decoded expression.
/// * `Err(String)` – actionable `Fix:`-prefixed error (unknown tag, depth
/// exceeded, truncation, invalid UTF-8, etc.).
#[inline]
pub(crate) fn expr(&mut self) -> Result<Expr, String> {
// Recursion guard for arbitrarily nested Expr trees (BinOp, UnOp,
// Select, Cast, Call arg lists, etc). Shares the same depth
// counter and budget as `node()` so a hostile blob can't evade
// the limit by alternating statement and expression levels.
if self.depth >= MAX_DECODE_DEPTH {
return Err(format!(
"Fix: IR wire format exceeds maximum decode depth {MAX_DECODE_DEPTH}; flatten deeply nested Expr trees or reject this untrusted blob."
));
}
self.depth += 1;
let result = self.expr_inner();
self.depth -= 1;
result
}
/// Decode a `DataType` from the wire format.
///
/// # Wire-format tag semantics
///
/// * `12` – dynamic-length `Array` type: a little-endian `u32` element size
/// follows, which must fit in `usize` on the target platform.
/// * any other value – forwarded to `data_type_from_tag`, which maps
/// fixed scalar tags (`u8`/`i8`/`u32`/etc.) to their `DataType`
/// variants. Unknown scalar tags are rejected there.
///
/// # Bounds checks
///
/// * `element_size` > `usize::MAX` on the current target → rejected with a
/// `Fix:` error advising decode on a supported target or rejection of the
/// blob.
///
/// # Return semantics
///
/// * `Ok(DataType)` – valid scalar or array type.
/// * `Err(String)` – actionable `Fix:`-prefixed error (overflow or unknown
/// scalar tag).
#[inline]
pub(crate) fn data_type(&mut self) -> Result<DataType, String> {
let tag = self.u8()?;
if tag == 0x08 {
let element_size = usize::try_from(self.u32()?).map_err(|err| {
format!(
"Fix: array element_size cannot fit usize on this target ({err}); decode this VIR0 blob on a supported target or reject it."
)
})?;
return Ok(DataType::Array { element_size });
}
if tag == 0x13 {
return Ok(DataType::Handle(vyre_spec::data_type::TypeId(self.u32()?)));
}
if tag == 0x14 {
let element = Box::new(self.data_type()?);
let count = self.u8()?;
return Ok(DataType::Vec { element, count });
}
if tag == 0x15 {
let element = Box::new(self.data_type()?);
let len = usize::try_from(self.u32()?).map_err(|err| {
format!("Fix: tensor rank cannot fit usize on this target ({err}); reject this VIR0 blob.")
})?;
let mut shape = smallvec::SmallVec::<[u32; 4]>::new();
for _ in 0..len {
shape.push(self.u32()?);
}
return Ok(DataType::TensorShaped { element, shape });
}
if tag == 0x80 {
// Opaque: u32 extension id follows.
let id = reject_reserved_extension_id(self.u32()?, "DataType")?;
return Ok(DataType::Opaque(vyre_spec::extension::ExtensionDataTypeId(
id,
)));
}
data_type_from_tag(tag)
}
fn expr_inner(&mut self) -> Result<Expr, String> {
match self.u8()? {
0 => Ok(Expr::LitU32(self.u32()?)),
1 => Ok(Expr::LitI32(self.i32()?)),
2 => Ok(Expr::LitBool(self.u8()? != 0)),
15 => Ok(Expr::LitF32(f32::from_bits(self.u32()?))),
3 => Ok(Expr::Var(self.string()?.into())),
4 => Ok(Expr::Load {
buffer: self.string()?.into(),
index: Box::new(self.expr()?),
}),
5 => Ok(Expr::BufLen {
buffer: self.string()?.into(),
}),
6 => Ok(Expr::InvocationId { axis: self.u8()? }),
7 => Ok(Expr::WorkgroupId { axis: self.u8()? }),
8 => Ok(Expr::LocalId { axis: self.u8()? }),
9 => {
let tag = self.u8()?;
let op = if tag == 0x80 {
// Opaque BinOp: u32 extension id follows.
let id = reject_reserved_extension_id(self.u32()?, "BinOp")?;
crate::ir::BinOp::Opaque(vyre_spec::extension::ExtensionBinOpId(id))
} else {
bin_op_from_tag(tag)?
};
Ok(Expr::BinOp {
op,
left: Box::new(self.expr()?),
right: Box::new(self.expr()?),
})
}
10 => {
let tag = self.u8()?;
let op = if tag == 0x80 {
let id = reject_reserved_extension_id(self.u32()?, "UnOp")?;
crate::ir::UnOp::Opaque(vyre_spec::extension::ExtensionUnOpId(id))
} else {
un_op_from_tag(tag)?
};
Ok(Expr::UnOp {
op,
operand: Box::new(self.expr()?),
})
}
11 => {
let op_id = self.string()?.into();
let count = self.bounded_len(MAX_ARGS, "call argument count")?;
let mut args = Vec::with_capacity(count);
for _ in 0..count {
args.push(self.expr()?);
}
Ok(Expr::Call { op_id, args })
}
12 => Ok(Expr::Select {
cond: Box::new(self.expr()?),
true_val: Box::new(self.expr()?),
false_val: Box::new(self.expr()?),
}),
13 => Ok(Expr::Cast {
target: self.data_type()?,
value: Box::new(self.expr()?),
}),
14 => {
let tag = self.u8()?;
let op = if tag == 0x80 {
let id = reject_reserved_extension_id(self.u32()?, "AtomicOp")?;
crate::ir::AtomicOp::Opaque(vyre_spec::extension::ExtensionAtomicOpId(id))
} else {
atomic_op_from_tag(tag)?
};
let ordering = crate::memory_model::MemoryOrdering::from_wire_tag(self.u8()?)?;
Ok(Expr::Atomic {
op,
buffer: self.string()?.into(),
index: Box::new(self.expr()?),
expected: if self.u8()? == 0 {
None
} else {
Some(Box::new(self.expr()?))
},
value: Box::new(self.expr()?),
ordering,
})
}
16 => Ok(Expr::Fma {
a: Box::new(self.expr()?),
b: Box::new(self.expr()?),
c: Box::new(self.expr()?),
}),
17 => Ok(Expr::SubgroupAdd {
value: Box::new(self.expr()?),
}),
18 => Ok(Expr::SubgroupShuffle {
value: Box::new(self.expr()?),
lane: Box::new(self.expr()?),
}),
19 => Ok(Expr::SubgroupBallot {
cond: Box::new(self.expr()?),
}),
20 => Ok(Expr::SubgroupLocalId),
21 => Ok(Expr::SubgroupSize),
0x80 => {
let kind = self.string()?;
let payload_len = self.bounded_len(MAX_ARGS * 1024, "opaque expression payload")?;
let payload = self.bytes(payload_len)?;
crate::extension::decode_opaque_expr(&kind, &payload)
}
tag => Err(format!(
"Fix: unknown IR expression tag {tag}; use a Program serializer compatible with this vyre version."
)),
}
}
}