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
// Copyright (c) 2026 Erik Lening (hollowpointer) and Contributors
//
// This file is part of Zond Engine, licensed under the GNU Affero General
// Public License, version 3 or later. See the LICENSE file for details, or
// <https://www.gnu.org/licenses/agpl-3.0.html>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later
//! # What a module is held to, and how a run can end
//!
//! A compute module is code, so unlike a [flow](crate::detect::flow) its cost is
//! not knowable before it runs: it must be metered, and a breach must be a
//! fact the report can state rather than a scan that silently stalls. This module
//! is the vocabulary for both: the [`Budget`] a run is bounded by, and the
//! [`RunOutcome`] that names why a run ended abnormally.
//!
//! ## Three bounds, three questions
//!
//! [`Budget`] carries bounds that answer different questions and bite in
//! different places. `fuel` bounds work done, independent of machine speed, and
//! is what a busy loop hits. `deadline` bounds wall-clock, and is what a run
//! stalled in a slow exchange hits, which fuel cannot see. `max_memory` bounds
//! allocation. `max_bytes` and `max_connections` bound the I/O at the seam:
//! they are spent inside [`speak`](super::Capabilities::speak), the one place a
//! module reaches the network, so a module cannot exceed them because the thing
//! that would spend them refuses to.
//!
//! ## An abnormal end is not an empty result
//!
//! A clean run that found nothing returns `Ok(vec![])`, ran, no finding. A run
//! that hit a bound, was refused a call, or broke returns `Err(RunOutcome)`, a
//! different fact, so a reader never reads "it ran out of fuel" as "it cleared the
//! host." This is the honesty the whole subsystem is built for, carried into the
//! one place a detection can fail.
use Duration;
use ;
use crate;
/// The bounds a compute module runs under.
///
/// Resolved from the detection's declared budget and the operator's envelope into
/// the concrete numbers the runtime enforces. Where each bound is checked is not
/// uniform: `fuel`, `deadline`, and `max_memory` are enforced by the runtime that
/// runs the code, while `max_bytes` and `max_connections` are enforced by the
/// [`Capabilities`](super::Capabilities) that serve its I/O, which is the point,
/// because the seam that spends a byte is the seam that can refuse to.
///
/// A scan resolves one from the detection and the envelope, so the fields stay
/// public to read. To build one directly, for driving a module outside a scan, start
/// from [`new`](Self::new) and tighten a ceiling with a `with_*` setter;
/// [`non_exhaustive`], so a bound added later is not a breaking change.
///
/// [`non_exhaustive`]: https://doc.rust-lang.org/reference/attributes/type-system.html#the-non_exhaustive-attribute
/// Which bound a run hit. Each is a deterministic trap at a known point, not a
/// timing accident, so the same inputs trap at the same place every time.
/// A granted capability that refused a specific call.
///
/// The narrow case: not a capability the module was never given, that one is
/// absent, and a module that names it fails without a `Denial`, because there
/// is nothing there to refuse, but a capability the module holds declining a
/// particular use of it, such as [`resolve`](super::Capabilities::resolve) of a
/// name the envelope's scope forbids.
/// The module itself broke, as opposed to hitting a bound or being refused.
/// Why a run ended abnormally.
///
/// The `Err` half of a [`run`](super::ComputeRuntime::run): an `Ok(vec)` is a
/// clean run (an empty vector its clean no-finding case), and every other way a
/// run can end is one of these, recorded rather than swallowed.