Skip to main content

ruso_runtime/
opcode.rs

1//! Opcode and bytecode wire-format contract (version 1).
2//!
3//! # File layout (`RUSO` + version byte)
4//!
5//! 1. Metadata (name, description, impact, severity, author, report title)
6//! 2. Probe table (HTTP / DNS / TCP specs)
7//! 3. String pool
8//! 4. Matcher pool (`QualifiedMatch`)
9//! 5. Extract pool (`ExtractSource`)
10//! 6. Evidence pool (`EvidenceKind`)
11//! 7. Payload pool (raw bytes for `Send` overrides)
12//! 8. Instruction stream (`Opcode` discriminants)
13//!
14//! # Instruction opcodes (wire `u8`)
15//!
16//! | Byte | Variant     | Payload                          |
17//! |------|-------------|----------------------------------|
18//! | 1    | Set         | `name_id: u32`, `value_id: u32`  |
19//! | 2    | Send        | `probe_id: u32`, optional payload index |
20//! | 3    | Match       | `matcher_id: u32`                |
21//! | 4    | MatchAll    | `start: u32`, `len: u16`         |
22//! | 5    | MatchAny    | `start: u32`, `len: u16`         |
23//! | 6    | Assert      | `matcher_id: u32`                |
24//! | 7    | Extract     | `name_id: u32`, `source_id: u32` |
25//! | 8    | IfMatch     | `matcher_id: u32`, `else_pc: u32`|
26//! | 9    | Save        | `from_id: u32`, `to_id: u32`     |
27//! | 10   | Evidence    | `kind_id: u32`                   |
28//! | 11   | Retry       | `probe_id: u32`, `count: u32`    |
29//! | 12   | RetryDelay  | `duration_id: u32`               |
30//! | 13   | Sleep       | `duration_id: u32`               |
31//! | 14   | Stop        | —                                |
32//! | 15   | Fail        | —                                |
33//! | 16   | Continue    | —                                |
34//! | 17   | Exit        | —                                |
35//! | 18   | (reserved)  | was `Repeat`, removed            |
36//! | 19   | LoopBack    | —                                |
37//! | 20   | Break       | —                                |
38//! | 21   | SetList     | `name_id: u32`, `start: u32`, `len: u16` |
39//! | 22   | ForList     | `item_id: u32`, `start: u32`, `len: u16`, `end_pc: u32` |
40//! | 23   | ForVar      | `item_id: u32`, `list_id: u32`, `end_pc: u32` |
41//!
42//! Compilers must emit [`crate::BytecodeProgram`] compatible with [`crate::VERSION`].
43
44pub use crate::runtime::binary::{MAGIC, VERSION};
45pub use crate::runtime::bytecode::{BytecodeProgram, Instr as Opcode, Pc};
46
47/// Bind a variable: `name_id: u32`, `value_id: u32` (both string-pool indices).
48pub const OP_SET: u8 = 1;
49/// Fire a probe: `probe_id: u32`, plus an optional payload-pool index that
50/// overrides the probe's own body.
51pub const OP_SEND: u8 = 2;
52/// Evaluate one matcher and record the outcome: `matcher_id: u32`.
53pub const OP_MATCH: u8 = 3;
54/// Evaluate a contiguous run of matchers; holds only if *all* of them do.
55/// Payload is `start: u32`, `len: u16`.
56pub const OP_MATCH_ALL: u8 = 4;
57/// Evaluate a contiguous run of matchers; holds if *any* of them does.
58/// Payload is `start: u32`, `len: u16`.
59pub const OP_MATCH_ANY: u8 = 5;
60/// Evaluate a matcher and abort the script when it fails: `matcher_id: u32`.
61pub const OP_ASSERT: u8 = 6;
62/// Pull a value out of the last response into a variable:
63/// `name_id: u32`, `source_id: u32` (an extract-pool index).
64pub const OP_EXTRACT: u8 = 7;
65/// Branch on a matcher: run the following block when it holds, otherwise jump
66/// to `else_pc`. Payload is `matcher_id: u32`, `else_pc: u32`.
67pub const OP_IF_MATCH: u8 = 8;
68/// Copy one variable into another: `from_id: u32`, `to_id: u32`.
69pub const OP_SAVE: u8 = 9;
70/// Attach proof to the finding this script emits: `kind_id: u32`, an
71/// evidence-pool index naming the source (body, whole response, or a header).
72pub const OP_EVIDENCE: u8 = 10;
73/// Re-send a probe up to `count` times while it keeps failing:
74/// `probe_id: u32`, `count: u32`.
75pub const OP_RETRY: u8 = 11;
76/// Set the delay inserted between [`OP_RETRY`] attempts: `duration_id: u32`
77/// (a string-pool duration literal).
78pub const OP_RETRY_DELAY: u8 = 12;
79/// Pause execution: `duration_id: u32`.
80pub const OP_SLEEP: u8 = 13;
81/// Halt the script and report whatever has matched so far.
82pub const OP_STOP: u8 = 14;
83/// Halt the script and mark the run as failed.
84pub const OP_FAIL: u8 = 15;
85/// Skip the rest of the current loop iteration and start the next one.
86pub const OP_CONTINUE: u8 = 16;
87/// Halt the script immediately without emitting a finding.
88pub const OP_EXIT: u8 = 17;
89// 18 reserved: was `Repeat`, removed from the language and VM.
90/// Jump back to the top of the enclosing loop.
91pub const OP_LOOP_BACK: u8 = 19;
92/// Leave the enclosing loop.
93pub const OP_BREAK: u8 = 20;
94/// Bind a list variable from a contiguous run of string-pool entries:
95/// `name_id: u32`, `start: u32`, `len: u16`.
96pub const OP_SET_LIST: u8 = 21;
97/// Loop over a literal list: `item_id: u32` (the loop variable),
98/// `start: u32`, `len: u16` (the elements), `end_pc: u32` (where to resume).
99pub const OP_FOR_LIST: u8 = 22;
100/// Loop over a list held in a variable: `item_id: u32` (the loop variable),
101/// `list_id: u32` (the list variable), `end_pc: u32` (where to resume).
102pub const OP_FOR_VAR: u8 = 23;