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
//! This module provides the recursion guard of FLM, which reports an error for
//! deeply nested content before the content overflows the stack, along with the
//! worker threads on which every guarded operation runs.
//!
//! Parsing, traversing, rewriting and serializing a tree are recursive
//! operations, so deeply nested content uses much stack. A stack overflow ends
//! the process, and the program that embeds FLM cannot recover from it. To
//! prevent the overflow, techy provides a *descent guard*: the guard measures
//! the stack that a recursive operation has used since it started, and stops the
//! operation when this amount exceeds a budget. The operation then reports an
//! error, for instance [`NestingTooDeep`] (`flm.blocks.nesting-too-deep`) for a
//! parse. See also [the recursion guard and worker
//! threads](crate::guide::concepts_overview#the-recursion-guard-and-worker-threads).
//!
//! # Guarded construction
//!
//! techy's default budget is small, and the budget of a language or of a
//! traversal can only be set when the language or the traversal is constructed.
//! FLM therefore constructs every recursive operation with one of the following
//! functions of this module, which install the guard with the budget
//! [`STACK_BUDGET`]:
//!
//! - [`language`], for parsing;
//! - [`walker`], [`restager`] and [`recomposer`], for traversals of a tree;
//! - [`serde_session`], for serialization.
//!
//! The limit is selected with the [`GuardPolicy`] argument that each function
//! takes.
//! [`GuardPolicy::StackBudget`] limits the stack use and is the policy for
//! normal use. [`GuardPolicy::DepthLimit`] limits the number of nested descents
//! and is used in tests only.
//!
//! # The worker-thread requirement
//!
//! The guard limits the stack that an operation uses, but it does not check the
//! size of the stack of the current thread. On a thread with less stack than the
//! budget, an operation overflows the stack before the guard stops it. Every
//! guarded operation must therefore run on a *worker thread*: a thread created
//! with at least [`WORKER_STACK_SIZE`] bytes of stack.
//!
//! The entry points of the library meet this requirement for their callers. They
//! run each guarded operation through [`on_worker_thread`], which runs the
//! operation on the current thread if this thread is marked as a worker thread,
//! and otherwise on a new worker thread. The [parse functions](crate::lang::parse),
//! the [rewrite passes](crate::lang::rewrites), the run of a plan
//! ([`Run`](crate::core::run::Run)) and the emitters of the standard backends work
//! in this way. A program that embeds FLM therefore does not need to create
//! worker threads.
//!
//! If you construct a guarded operation with a function of this module, or if
//! you call [`copy_run_into_value`](crate::lang::copy::copy_run_into_value), run
//! the operation inside [`on_worker_thread`].
//!
//! # Content that exceeds the limit
//!
//! When the guard stops an operation, the operation does not panic. It reports
//! an error and produces no result. For instance, a parse returns
//! [`FragmentError::NestingTooDeep`](crate::lang::FragmentError::NestingTooDeep),
//! which contains the error [`NestingTooDeep`]. A rewrite pass records the same
//! error, and the tree that the pass was rewriting is absent from the output of
//! the run.
use Cell;
use thread;
use Language;
use StdDescentGuardInit;
use DiagnosticInfo;
use TreeRecomposer;
use ;
use TreeRestager;
use TreeWalker;
use crateFlm;
/// The stack size of a worker thread, in bytes (64 MiB).
///
/// A thread that is marked as a worker thread with [`mark_worker_thread`] must
/// have at least this much stack (see [the worker-thread
/// requirement](self#the-worker-thread-requirement)).
pub const WORKER_STACK_SIZE: usize = 64 * 1024 * 1024;
/// The amount of stack, in bytes, that a guarded operation may use before the
/// recursion guard stops it (48 MiB).
///
/// This budget is smaller than [`WORKER_STACK_SIZE`]. The difference leaves room
/// for the stack that the operation uses between two checks of the guard, and
/// for the stack that is used to report the error.
pub const STACK_BUDGET: usize = 48 * 1024 * 1024;
// The budget must leave headroom below the worker stack (checked at compile time).
const _: = assert!;
thread_local!
/// Marks the current thread as a worker thread. [`on_worker_thread`] then runs
/// guarded operations directly on this thread.
///
/// Call this function only on a thread that was created with at least
/// [`WORKER_STACK_SIZE`] bytes of stack, for instance with
/// `std::thread::Builder::stack_size` or with the corresponding setting of a
/// thread pool. On a thread with a smaller stack, a guarded operation can
/// overflow the stack before the recursion guard stops it (see [the
/// worker-thread requirement](self#the-worker-thread-requirement)).
///
/// [`run_with_worker_stack`] marks the thread that it creates, so you do not
/// need to call this function on that thread.
/// Returns whether the current thread has been marked as a worker thread with
/// [`mark_worker_thread`].
/// Runs `f` on a worker thread and returns its result.
///
/// If the current thread is marked as a worker thread ([`is_worker_thread`]),
/// `f` runs on the current thread. Otherwise, `f` runs on a new thread created
/// with [`run_with_worker_stack`].
///
/// The entry points of the library call this function before they run a
/// guarded operation (see [the worker-thread
/// requirement](self#the-worker-thread-requirement)). Call it yourself around a
/// guarded operation that you construct with the functions of this module.
///
/// # Errors
///
/// Returns the error of the operating system if a new thread is needed and the
/// operating system fails to create it. In this case, `f` does not run.
///
/// # Panics
///
/// A panic inside `f` propagates to the caller, on whichever thread `f` ran.
/// The error that is reported when content is nested too deeply to be
/// processed (`flm.blocks.nesting-too-deep`).
///
/// Reported by a parse and by the rewrite passes when the recursion guard stops
/// them (see the [module documentation](self)).
///
/// A parse that reports this error returns no fragment. When a rewrite pass
/// reports this error, the tree that the pass was rewriting is absent from the
/// output of the run.
/// A `GuardPolicy` is the kind of limit that the recursion guard sets on a
/// guarded operation. The default policy,
/// [`StackBudget`](GuardPolicy::StackBudget), is the policy for normal use.
/// Returns `language` with a recursion guard that applies `policy` to every
/// parse with this language.
///
/// [`build_language`](crate::lang::build_language) calls this function on the
/// language that it builds.
/// Returns a [`TreeWalker`] for `visitor`, with a recursion guard that applies
/// `policy`.
///
/// A tree walker traverses a tree without changing it. Use this function in
/// place of `TreeWalker::new`, which installs techy's small default budget.
Sized>
/// Returns a [`TreeRestager`] for `visitor`, with a recursion guard that
/// applies `policy`.
///
/// A tree restager traverses a tree and builds a new tree from it, with
/// changes applied. Use this function in place of `TreeRestager::new`, which
/// installs techy's small default budget.
Sized>
/// Returns a [`TreeRecomposer`] for `recomposer`, with a recursion guard that
/// applies `policy`.
///
/// A tree recomposer traverses a tree and combines it into one value, for
/// instance a string of source text. Use this function in place of
/// `TreeRecomposer::new`, which installs techy's small default budget.
Sized>
/// Returns `session` with a recursion guard that applies `policy` when the
/// session serializes or deserializes a tree.
/// Runs `f` on a new worker thread and returns its result.
///
/// The new thread is created with [`WORKER_STACK_SIZE`] bytes of stack, and it
/// is marked as a worker thread with [`mark_worker_thread`]. This function
/// always creates a thread. In most cases, prefer [`on_worker_thread`], which
/// creates a thread only if the current thread is not a worker thread.
///
/// # Errors
///
/// Returns the error of the operating system if the operating system fails to
/// create the thread. In this case, `f` does not run.
///
/// # Panics
///
/// A panic inside `f` is caught on the new thread and resumed on the calling
/// thread. The caller therefore observes the panic as if `f` had run on the
/// calling thread.