remedy-lua 0.1.0

A Lua 5.4 superset in safe Rust for running untrusted scripts: exact fuel, memory and depth limits, an incremental collector, an optional typed dialect and a capability-based host API.
Documentation
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! Diagnostics: errors and warnings with where they are (chunk, line, column).

use std::fmt;

/// A place in source text: line and column, both from 1.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Pos {
    /// The line, from 1.
    pub line: u32,
    /// The column (byte in the line), from 1.
    pub col: u32,
}

/// How serious a diagnostic is. More levels may come: match with a `_` arm.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum Severity {
    /// The code cannot run (syntax) or will not do what its annotations say (types).
    Error,
    /// Probably a mistake.
    Warning,
}

/// One message about source text.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Diagnostic {
    /// How serious.
    pub severity: Severity,
    /// The chunk's name (a file name, usually).
    pub chunk: String,
    /// Where the text it is about starts.
    pub pos: Pos,
    /// Where that text ends: the line and column just past its last character (`pos` when
    /// it is about a point, such as the end of input).
    pub end: Pos,
    /// The line Lua's own error message gives: where C Lua notices the problem, which can
    /// lie after `end` (an undefined `goto` is noticed where its function ends).
    pub line: u32,
    /// What is wrong, for the person who wrote it.
    pub message: String,
    /// The message as bytes when it quotes source that is not UTF-8 (`message` has those
    /// bytes replaced): what Lua's own message holds, as C Lua prints them (S6-FRONT-7).
    raw: Option<Box<[u8]>>,
}

impl Diagnostic {
    pub(crate) fn error(chunk: &str, pos: Pos, msg: impl Into<String>) -> Self {
        Diagnostic { severity: Severity::Error, chunk: chunk.to_owned(), pos, end: pos, line: pos.line, message: msg.into(), raw: None }
    }

    pub(crate) fn warning(chunk: &str, pos: Pos, msg: impl Into<String>) -> Self {
        Diagnostic { severity: Severity::Warning, chunk: chunk.to_owned(), pos, end: pos, line: pos.line, message: msg.into(), raw: None }
    }

    /// An error whose message is `raw` (read as text with invalid UTF-8 replaced).
    pub(crate) fn error_bytes(chunk: &str, pos: Pos, raw: Vec<u8>) -> Self {
        let mut d = Diagnostic::error(chunk, pos, String::from_utf8_lossy(&raw).into_owned());
        if std::str::from_utf8(&raw).is_err() {
            d.raw = Some(raw.into_boxed_slice());
        }
        d
    }

    /// The same, about the text from `pos` to `end`.
    pub(crate) fn to(mut self, end: Pos) -> Self {
        self.end = end.max(self.pos);
        self
    }

    /// The same, with Lua's message giving `line`.
    pub(crate) fn lua_line(mut self, line: u32) -> Self {
        self.line = line;
        self
    }

    /// As Lua's own error message reads: `chunk:line: message`.
    pub fn lua_message(&self) -> String {
        format!("{}:{}: {}", self.chunk, self.line, self.message)
    }

    /// Lua's own error message as the bytes C Lua's holds: source it quotes as written.
    pub(crate) fn lua_message_bytes(&self) -> Vec<u8> {
        match &self.raw {
            None => self.lua_message().into_bytes(),
            Some(raw) => {
                let mut b = format!("{}:{}: ", self.chunk, self.line).into_bytes();
                b.extend_from_slice(raw);
                b
            }
        }
    }
}

impl fmt::Display for Diagnostic {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let sev = match self.severity {
            Severity::Error => "",
            Severity::Warning => "warning: ",
        };
        write!(f, "{}:{}:{}: {sev}{}", self.chunk, self.pos.line, self.pos.col, self.message)
    }
}

impl std::error::Error for Diagnostic {}

// ---- the host's interrupt and deadline while compiling ----

/// Lexer, parser and compiler steps between looks at the watch.
/// (A compiler step can take a microsecond, a local made or a constant found: 4,096
/// steps were 3 to 4 ms between looks; at 1,024, one atomic load per 1,024 steps.)
pub(crate) const COMPILE_WATCH_EVERY: u32 = 1024;

/// Units of scan work (`compile_work`) that count as one step between looks at the watch.
/// One step can scan thousands of locals, labels or gotos: counted as one, 4,096 of them
/// kept the interrupt and deadline unread for over 100 ms (S5C-FRONT-2).
const COMPILE_WORK_PER_TICK: u64 = 16;

/// The message of the error a stopped compile unwinds with (turned into the limit).
pub(crate) const COMPILE_STOPPED: &str = "compile stopped by the host";

thread_local! {
    /// The running compile's watch (the state's interrupt and deadline), steps since the
    /// last look, and the limit that stopped it.
    static COMPILE_WATCH: std::cell::RefCell<Option<(crate::stdlib::pattern::Watch, Option<crate::LimitKind>)>> =
        const { std::cell::RefCell::new(None) };
    static COMPILE_TICKS: std::cell::Cell<u32> = const { std::cell::Cell::new(0) };
    /// The running compile's scan work (`compile_work`) and the most it may do.
    static COMPILE_WORK: std::cell::Cell<(u64, u64)> = const { std::cell::Cell::new((0, u64::MAX)) };
    /// Whether the running compile must stop (its watch or its scan work said so).
    static COMPILE_STOP: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
    /// What the parse reading from `load`'s reader holds as it asks for more, and the room
    /// it has after the reader ran (`parse_reading`).
    static PARSE_READING: std::cell::Cell<(usize, Option<usize>)> = const { std::cell::Cell::new((0, None)) };
}

/// Exchanged around `load`'s reader: the parser sets what it holds before it reads on,
/// `load` counts that as used while the reader runs and sets the room left after it
/// (SEC-M13: the tree was in no account while the reader ran, so the reader's allocations
/// and an inner `load`'s room came on top of it).
pub(crate) fn parse_reading(set: (Option<usize>, Option<usize>)) -> (usize, Option<usize>) {
    PARSE_READING.with(|c| {
        let (held, room) = c.get();
        c.set((set.0.unwrap_or(held), set.1));
        (held, room)
    })
}

/// What can be freed a piece at a time (`free_deferred`).
pub(crate) trait Piecewise: Send {
    /// Frees up to `n` pieces: whether all of it is freed.
    fn free_some(&mut self, n: usize) -> bool;
    /// About the bytes it holds (charged to the state's memory while it waits).
    fn bytes(&self) -> usize;
}

impl<T: Send> Piecewise for Vec<T> {
    fn bytes(&self) -> usize {
        self.capacity().saturating_mul(std::mem::size_of::<T>())
    }

    fn free_some(&mut self, n: usize) -> bool {
        self.truncate(self.len().saturating_sub(n));
        crate::ast::shed(self);
        if self.is_empty() {
            *self = Vec::new();
        }
        self.is_empty()
    }
}

impl Piecewise for crate::ast::Dropper {
    fn free_some(&mut self, n: usize) -> bool {
        self.step(n)
    }
    fn bytes(&self) -> usize {
        self.bytes()
    }
}

/// Bytes past which a vector of the front end grows by `grow_watched`.
/// (1 MB: page faults on fresh memory cost about 0.45 ms per MB, so growing a 16 MB list
/// in one `realloc` took 10 to 25 ms.)
const GROW_WATCHED: usize = 1 << 20;

/// Elements moved between steps while a big vector grows.
const GROW_STRETCH: usize = 1 << 15;

/// Grows `v` to hold `cap` elements. A big buffer is moved to the new one a stretch at a
/// time, each a look at the host's interrupt (S6-FRONT-4: growing a 2M-field table
/// constructor's list, 320 MB, took 190 ms in one `realloc`). Whether the compile was told
/// to stop meanwhile: then `v` is left empty, and what it held is freed later.
pub(crate) fn grow_watched<T: Send + 'static>(v: &mut Vec<T>, cap: usize) -> bool {
    if cap <= v.capacity() {
        return false;
    }
    // (With nothing to watch, as for `check` and `format`, at once: `realloc` can often
    // move a big buffer without copying it.)
    if cap.saturating_mul(std::mem::size_of::<T>()) < GROW_WATCHED || !watching() {
        v.reserve_exact(cap - v.len());
        return false;
    }
    let mut new = Vec::with_capacity(cap);
    let mut old = std::mem::take(v);
    // Reversed in place, then moved from its end, so the old buffer is given back a
    // stretch at a time as it empties (`shed`): freeing it whole once moved (tens of
    // megabytes) was 5 to 10 ms of its own. (A stop leaves it in any order: it is only
    // freed then.)
    let n = old.len();
    let stopped = 'grow: {
        let mut i = 0;
        while i < n / 2 {
            let end = (i + GROW_STRETCH).min(n / 2);
            for k in i..end {
                old.swap(k, n - 1 - k);
            }
            i = end;
            if compile_ticks(COMPILE_WATCH_EVERY) {
                break 'grow true;
            }
        }
        while !old.is_empty() {
            new.extend(std::iter::from_fn(|| old.pop()).take(GROW_STRETCH));
            crate::ast::shed(&mut old);
            if compile_ticks(COMPILE_WATCH_EVERY) {
                break 'grow true;
            }
        }
        false
    };
    if stopped {
        defer_pieces(new);
        defer_pieces(old);
        return true;
    }
    *v = new;
    false
}

/// Whether a compile with a watch (the host's interrupt) is running on this thread.
fn watching() -> bool {
    COMPILE_WATCH.with(|w| w.borrow().is_some())
}

/// What a map key holds on the heap besides its slot (for `ShardMap::into_pieces`).
pub(crate) trait KeyHeap {
    fn heap(&self) -> usize {
        0
    }
}

impl KeyHeap for Vec<u8> {
    fn heap(&self) -> usize {
        self.capacity()
    }
}

impl KeyHeap for String {
    fn heap(&self) -> usize {
        self.capacity()
    }
}

impl KeyHeap for std::sync::Arc<str> {
    /// Its bytes and counts when this is the last reference (else another owner keeps them).
    fn heap(&self) -> usize {
        if std::sync::Arc::strong_count(self) == 1 { self.len() + 16 } else { 0 }
    }
}

/// The bytes the bucket array of a `HashMap` of capacity `cap` takes, as std's (hashbrown)
/// allocates it: a power-of-two number of buckets (`cap` is 7/8 of them, or one less
/// than a small one), each an entry and a control byte, and a group of control bytes more.
pub(crate) fn table_bytes<K, V>(cap: usize) -> usize {
    if cap == 0 {
        return 0;
    }
    let buckets = if cap < 8 { (cap + 1).next_power_of_two() } else { (cap * 8 / 7).next_power_of_two() };
    let entries = (buckets * std::mem::size_of::<(K, V)>()).next_multiple_of(16);
    entries + buckets + 16
}

impl<K: KeyHeap + Send, V: Send> Piecewise for crate::shardmap::ShardPieces<K, V> {
    fn free_some(&mut self, n: usize) -> bool {
        crate::shardmap::ShardPieces::free_some(self, n)
    }
    /// Exactly: the shards' bucket arrays and their keys' own bytes.
    fn bytes(&self) -> usize {
        self.held()
    }
}

/// Anything else, freed at once (and its size).
struct Whole(Option<Box<dyn std::any::Any + Send>>, usize);

impl Piecewise for Whole {
    fn free_some(&mut self, _: usize) -> bool {
        self.0 = None;
        true
    }
    fn bytes(&self) -> usize {
        self.1
    }
}

thread_local! {
    /// What a compile from the host left to free (S5C-FRONT-12, S6-FRONT-4): freed at the
    /// next compile or call from the host instead of before this one returns (freeing a
    /// multi-megabyte source's tokens and syntax tree took tens of milliseconds, after
    /// the interrupt or after a successful compile, with no look at the interrupt).
    static DEFERRED: std::cell::RefCell<Deferred> = const { std::cell::RefCell::new(Vec::new()) };
}

/// What waits to be freed, each with the bytes charged for it.
pub(crate) type Deferred = Vec<(Box<dyn Piecewise>, usize)>;

/// Pieces freed between looks at the interrupt (`free_deferred`): well under a millisecond.
const FREE_PIECES: usize = 1024;

/// Keeps `v` to free later, at once (`free_deferred`).
pub(crate) fn defer_drop<T: Send + 'static>(v: T) {
    defer_pieces(Whole(Some(Box::new(v)), std::mem::size_of::<T>()));
}

/// Keeps `v` to free later, a piece at a time.
pub(crate) fn defer_pieces<T: Piecewise + 'static>(v: T) {
    let bytes = v.bytes();
    DEFERRED.with(|d| d.borrow_mut().push((Box::new(v), bytes)));
}

/// What compiles on this thread deferred since this was last called: the compiling state
/// takes it (to charge and free; `Lua::adopt_deferred`).
pub(crate) fn take_deferred() -> Deferred {
    DEFERRED.with(|d| std::mem::take(&mut *d.borrow_mut()))
}

thread_local! {
    /// Whether what the running compile is done with is freed later (`release`): for a
    /// compile from the host, whose next call frees it.
    static DEFERRING: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}

/// Sets whether `release` defers, for the compile about to run: what it was.
pub(crate) fn set_deferring(on: bool) -> bool {
    DEFERRING.with(|d| d.replace(on))
}

/// Frees `v` now, or (in a compile from the host) later, a piece at a time.
pub(crate) fn release<T: Piecewise + 'static>(v: T) {
    if DEFERRING.with(std::cell::Cell::get) {
        defer_pieces(v);
    } else {
        // Inside a call: now, but a piece at a time between looks (a `load` in a call of
        // 600,000 string constants freed its maps in one 30 ms stretch).
        free_watched(v);
    }
}

/// Keeps a syntax tree of about `bytes` to free later, a node at a time.
pub(crate) fn defer_tree(b: crate::ast::Block, bytes: usize) {
    defer_pieces(crate::ast::Dropper::of_block(b).with_bytes(bytes));
}

/// Frees `list` a piece at a time until `stop` says to stop (the host's interrupt): what
/// is left is freed at a later call. The bytes of each item freed whole: released.
pub(crate) fn free_deferred(list: &mut Deferred, stop: impl Fn() -> bool) -> usize {
    let mut released = 0usize;
    while let Some((mut v, mut bytes)) = list.pop() {
        while !v.free_some(FREE_PIECES) {
            // What each piece gave back is released as it goes (never more than charged).
            let now = v.bytes().min(bytes);
            released = released.saturating_add(bytes - now);
            bytes = now;
            settle_frees();
            if stop() {
                list.push((v, bytes));
                return released;
            }
        }
        drop(v);
        released = released.saturating_add(bytes);
        settle_frees();
        if stop() {
            return released;
        }
    }
    released
}

/// Frees `v` now, a piece at a time, each a step of the running compile; told to stop,
/// it leaves the rest to `free_deferred`.
pub(crate) fn free_watched<T: Piecewise + 'static>(mut v: T) {
    while !v.free_some(FREE_PIECES * 4) {
        settle_frees();
        if compile_ticks(COMPILE_WATCH_EVERY) {
            defer_pieces(v);
            return;
        }
    }
}

/// Asks the allocator for a block larger than the small ones just freed, and gives it back:
/// an allocator that keeps small freed blocks on quick lists (glibc's) merges them on such
/// a request, so it does that for each piece now, not for all of a tree at once at some
/// later allocation (S6-FRONT-4: the first sizeable allocation after freeing a 3M-node
/// tree took 300 ms).
fn settle_frees() {
    drop(std::hint::black_box(Vec::<u8>::with_capacity(4096)));
}

/// Runs a compile under `watch`, with at most `max_work` units of scan work: the result,
/// the limit that stopped it (if one did), and the scan work done.
pub(crate) fn watched<T>(
    watch: crate::stdlib::pattern::Watch,
    max_work: u64,
    f: impl FnOnce() -> T,
) -> (T, Option<crate::LimitKind>, u64) {
    let outer = COMPILE_WATCH.with(|w| w.replace(Some((watch, None))));
    let outer_work = COMPILE_WORK.with(|w| w.replace((0, max_work)));
    let outer_stop = COMPILE_STOP.with(|s| s.replace(false));
    let r = f();
    COMPILE_STOP.with(|s| s.set(outer_stop));
    let (work, _) = COMPILE_WORK.with(|w| w.replace(outer_work));
    let stop = COMPILE_WATCH.with(|w| w.replace(outer)).and_then(|(_, k)| k);
    (r, stop, work)
}

/// Scan work in the compiler that grows with the program, not with its length: looking a
/// name up among the active locals and upvalues, labels and pending gotos, and the like.
/// (With no limit on locals, labels or gotos, these made compiling quadratic: 40,000
/// locals took 5 s against about 2M units of fuel for `load`'s up-front charge.) Counted
/// and paid for as fuel by the caller; past the most a compile may do, it stops on the
/// fuel limit at the next step. Every `COMPILE_WORK_PER_TICK` units are also a step
/// between looks at the host's interrupt and deadline.
#[inline]
pub(crate) fn compile_work(n: usize) {
    let steps = COMPILE_WORK.with(|w| {
        let (before, max) = w.get();
        let done = before.saturating_add(n as u64);
        w.set((done, max));
        if done > max {
            COMPILE_STOP.with(|s| s.set(true));
            COMPILE_WATCH.with(|c| {
                if let Some((_, stop)) = c.borrow_mut().as_mut() {
                    stop.get_or_insert(crate::LimitKind::Fuel);
                }
            });
        }
        done / COMPILE_WORK_PER_TICK - before / COMPILE_WORK_PER_TICK
    });
    if steps > 0 {
        // A stop found here is kept (`COMPILE_STOP`): the next step reports it.
        compile_ticks(u32::try_from(steps).unwrap_or(u32::MAX));
    }
}

/// Whether the running compile was told to stop (by its watch or its scan work).
#[inline]
pub(crate) fn compile_stopped() -> bool {
    COMPILE_STOP.with(std::cell::Cell::get)
}

/// One step of the lexer, parser or compiler: true when the host's interrupt or deadline
/// says to stop (looked at every `COMPILE_WATCH_EVERY` steps).
#[inline]
pub(crate) fn compile_tick() -> bool {
    compile_ticks(1)
}

/// `n` steps of the lexer, parser or compiler at once (a token that took long to read):
/// true when the host's interrupt or deadline says to stop.
#[inline]
pub(crate) fn compile_ticks(n: u32) -> bool {
    let (before, t) = COMPILE_TICKS.with(|c| {
        let before = c.get();
        let t = before.wrapping_add(n.max(1));
        c.set(t);
        (before, t)
    });
    if t / COMPILE_WATCH_EVERY == before / COMPILE_WATCH_EVERY {
        // Past its scan work, or told to stop during it, a compile stops at once.
        return COMPILE_STOP.with(std::cell::Cell::get);
    }
    let stop = COMPILE_WATCH.with(|w| {
        let mut w = w.borrow_mut();
        let Some((watch, stop)) = w.as_mut() else { return false };
        if stop.is_none() {
            *stop = watch.stop();
        }
        stop.is_some()
    });
    if stop {
        COMPILE_STOP.with(|s| s.set(true));
    }
    stop
}