moonseed 0.2.0

Deterministic, checkpointable Lua 5.4 runtime in safe Rust
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
//! The `math` and `table` libraries (ADR 0032, ADR 0033): which functions
//! exist, their registry symbols, the library state a runtime keeps, and
//! the progress of a table function (or `math.min` / `math.max`) that runs
//! over more than one step. The VM implements them in `runtime/library.rs`.

use crate::host::{Builtin, HostRegistry};

/// A `math` function.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum MathFn {
    Abs,
    Acos,
    Asin,
    Atan,
    Ceil,
    Cos,
    Deg,
    Exp,
    Floor,
    Fmod,
    Log,
    Max,
    Min,
    Modf,
    Rad,
    Random,
    RandomSeed,
    Sin,
    Sqrt,
    Tan,
    ToInteger,
    Type,
    Ult,
}

/// A `table` function.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum TableFn {
    Concat,
    Insert,
    Move,
    Pack,
    Remove,
    Sort,
    Unpack,
}

/// The `math` functions: field name, registry symbol, function.
pub(crate) const MATH_FUNCTIONS: [(&str, &str, MathFn); 23] = [
    ("abs", "math.abs", MathFn::Abs),
    ("acos", "math.acos", MathFn::Acos),
    ("asin", "math.asin", MathFn::Asin),
    ("atan", "math.atan", MathFn::Atan),
    ("ceil", "math.ceil", MathFn::Ceil),
    ("cos", "math.cos", MathFn::Cos),
    ("deg", "math.deg", MathFn::Deg),
    ("exp", "math.exp", MathFn::Exp),
    ("floor", "math.floor", MathFn::Floor),
    ("fmod", "math.fmod", MathFn::Fmod),
    ("log", "math.log", MathFn::Log),
    ("max", "math.max", MathFn::Max),
    ("min", "math.min", MathFn::Min),
    ("modf", "math.modf", MathFn::Modf),
    ("rad", "math.rad", MathFn::Rad),
    ("random", "math.random", MathFn::Random),
    ("randomseed", "math.randomseed", MathFn::RandomSeed),
    ("sin", "math.sin", MathFn::Sin),
    ("sqrt", "math.sqrt", MathFn::Sqrt),
    ("tan", "math.tan", MathFn::Tan),
    ("tointeger", "math.tointeger", MathFn::ToInteger),
    ("type", "math.type", MathFn::Type),
    ("ult", "math.ult", MathFn::Ult),
];

/// The `table` functions: field name, registry symbol, function.
pub(crate) const TABLE_FUNCTIONS: [(&str, &str, TableFn); 7] = [
    ("concat", "table.concat", TableFn::Concat),
    ("insert", "table.insert", TableFn::Insert),
    ("move", "table.move", TableFn::Move),
    ("pack", "table.pack", TableFn::Pack),
    ("remove", "table.remove", TableFn::Remove),
    ("sort", "table.sort", TableFn::Sort),
    ("unpack", "table.unpack", TableFn::Unpack),
];

/// Register the `math` functions under their `math.*` symbols.
pub fn register_math(registry: &mut HostRegistry) {
    for (_, symbol, function) in MATH_FUNCTIONS {
        registry.register_builtin(symbol, Builtin::Math(function));
    }
}

/// Register the `table` functions under their `table.*` symbols.
pub fn register_table(registry: &mut HostRegistry) {
    for (_, symbol, function) in TABLE_FUNCTIONS {
        registry.register_builtin(symbol, Builtin::Table(function));
    }
}

/// Register the base library, `math`, `table`, and `string`: every
/// function `Runtime::install_standard` installs.
pub fn register_standard(registry: &mut HostRegistry) {
    crate::base::register_base(registry);
    crate::oslib::register_os(registry);
    register_math(registry);
    register_table(registry);
    crate::strlib::register_string(registry);
    crate::utf8lib::register_utf8(registry);
    crate::package::register_package(registry);
    crate::corolib::register_coroutine(registry);
    crate::iolib::register_io(registry);
}

/// Library state a runtime keeps for its life (ADR 0032). Snapshot state:
/// it is the same on every target and replays exactly.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct LibraryState {
    /// `math.random`'s xoshiro256** state, as Lua 5.4 keeps it. Never all
    /// zero: seeding sets a nonzero word.
    pub(crate) rng: [u64; 4],
    /// The deterministic entropy stream that seeds the generator when
    /// `math` is installed and when `math.randomseed()` gets no argument
    /// and the host gives no entropy: a splitmix64 counter, started from
    /// `Config::entropy`.
    pub(crate) entropy: u64,
}

impl LibraryState {
    pub(crate) fn new(entropy: u64) -> Self {
        let mut state = Self {
            rng: [0; 4],
            entropy,
        };
        state.seed(0, 0);
        state
    }

    /// The next word of the entropy stream (splitmix64).
    pub(crate) fn draw(&mut self) -> u64 {
        self.entropy = self.entropy.wrapping_add(0x9e37_79b9_7f4a_7c15);
        let mut z = self.entropy;
        z = (z ^ (z >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
        z = (z ^ (z >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
        z ^ (z >> 31)
    }

    /// Lua 5.4's `setseed`: the seeds, a fixed nonzero word, and sixteen
    /// discarded outputs to spread them.
    pub(crate) fn seed(&mut self, n1: u64, n2: u64) {
        self.rng = [n1, 0xff, n2, 0];
        for _ in 0..16 {
            self.next();
        }
    }

    /// xoshiro256**, as Lua 5.4's `nextrand`.
    pub(crate) fn next(&mut self) -> u64 {
        let [s0, s1, s2, s3] = self.rng;
        let s2 = s2 ^ s0;
        let s3 = s3 ^ s1;
        let result = s1.wrapping_mul(5).rotate_left(7).wrapping_mul(9);
        self.rng = [s0 ^ s3, s1 ^ s2, s2 ^ (s1 << 17), s3.rotate_left(45)];
        result
    }

    /// Lua 5.4's `project`: a value in `0..=n` without bias. When `n + 1`
    /// is not a power of two, draws are masked to the smallest `2^b - 1`
    /// not below `n` and redrawn until one fits.
    pub(crate) fn project(&mut self, mut random: u64, n: u64) -> u64 {
        if n & n.wrapping_add(1) == 0 {
            return random & n;
        }
        let mut lim = n;
        lim |= lim >> 1;
        lim |= lim >> 2;
        lim |= lim >> 4;
        lim |= lim >> 8;
        lim |= lim >> 16;
        lim |= lim >> 32;
        loop {
            random &= lim;
            if random <= n {
                return random;
            }
            random = self.next();
        }
    }
}

/// Lua 5.4's `I2d`: the top 53 bits of an output as a float in `[0, 1)`.
pub(crate) fn unit_float(random: u64) -> f64 {
    (random >> 11) as f64 * (0.5 / (1u64 << 52) as f64)
}

/// A table function, or `math.min` / `math.max`, in progress (ADR 0033):
/// its work so far and what the call its frame made is for. It holds no
/// Lua values: those are in the frame's scratch slots, on the stack.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct LibTask {
    pub(crate) work: Work,
    pub(crate) wait: Wait,
}

/// What a library frame's call stands in for, and so what its result is.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Wait {
    /// No call: the frame's last step ran out of its operations.
    Nothing,
    /// `__index`: the result goes to scratch slot `into`.
    Get { into: u32 },
    /// `__newindex`: the result is dropped.
    Set,
    /// `__len`: the result is the length.
    Len,
    /// `__lt`, `__eq`, or `table.sort`'s function: the result's truth.
    Truth,
    /// A call whose first two results go to scratch slots `into` and
    /// `into + 1`: a `package.searchers` entry (ADR 0039).
    Pair { into: u32 },
}

/// Where a library function is. Scratch slots are numbered from 0, above
/// the function's arguments.
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum Work {
    /// `math.min` / `math.max`: argument `best` wins so far; `next` is
    /// compared with it.
    Extreme {
        max: bool,
        best: u32,
        next: u32,
    },
    /// `table.insert`.
    Insert {
        stage: Stage,
        pos: i64,
        i: i64,
    },
    /// `table.remove`: the removed value is scratch 0.
    Remove {
        stage: Stage,
        size: i64,
        pos: i64,
    },
    /// `table.move`: element `i` of `n` goes from `f + i` to `t + i`.
    Move {
        stage: Stage,
        f: i64,
        t: i64,
        n: i64,
        i: i64,
        backward: bool,
    },
    /// `table.concat`: the text so far, charged to the logical heap.
    Concat {
        stage: Stage,
        i: i64,
        last: i64,
        text: Vec<u8>,
    },
    /// `table.pack`: the new table is scratch 0; `next` is the next
    /// argument to store, one past the last for the `n` field.
    Pack {
        stage: Stage,
        next: u32,
    },
    /// `table.unpack`: `count` results from `first`, `got` read so far,
    /// each in its scratch slot.
    Unpack {
        stage: Stage,
        first: i64,
        count: u32,
        got: u32,
    },
    /// `table.sort`.
    Sort(Box<SortState>),
    /// A `string` function (ADR 0034).
    Str(Box<crate::strlib::StrWork>),
    /// Bounded UTF-8 work.
    Utf8(Box<crate::utf8lib::Utf8Work>),
    Os(Box<crate::oslib::OsWork>),
    /// `require` or a `package` searcher (ADR 0039).
    Package(Box<crate::package::PackageWork>),
    /// `debug.traceback` (ADR 0040).
    Debug(Box<crate::debuglib::DebugWork>),
}

/// A table function's step, beyond what its counters say.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Stage {
    /// Nothing done yet.
    Start,
    /// The list's length is being taken.
    Length,
    /// Reading the first element (`table.remove`'s result).
    First,
    /// Reading the next element.
    Read,
    /// Writing the element just read.
    Write,
    /// Writing the final element.
    Last,
    /// `table.move` deciding the direction by `__eq`.
    Equal,
    /// Finished.
    Done,
}

/// `table.sort`'s quicksort, Lua 5.4.9's `auxsort` and `partition` with
/// its recursion made a stack of ranges (ADR 0033). Scratch 0 holds the
/// pivot or `a[lo]`, 1 and 2 the elements being compared.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct SortState {
    pub(crate) step: SortStep,
    /// The list's length when the sort began: every index is in `1..=n`.
    pub(crate) n: u32,
    pub(crate) lo: u32,
    pub(crate) up: u32,
    pub(crate) p: u32,
    pub(crate) i: u32,
    pub(crate) j: u32,
    /// Pivot choice for large ranges, 0 for the middle one.
    pub(crate) rnd: u32,
    /// Ranges still to sort, the next last. Each holds the size of the
    /// range sorted before it, to test the partition's balance.
    pub(crate) pending: Vec<SortRange>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct SortRange {
    pub(crate) lo: u32,
    pub(crate) up: u32,
    pub(crate) smaller: u32,
    pub(crate) rnd: u32,
}

/// Whether restore may make a native closure run by `builtin` with these
/// values and numbers (ADR 0035): only a shape the builtin itself makes.
pub(crate) fn closure_fits(
    heap: &crate::heap::Heap,
    builtin: crate::host::Builtin,
    values: &[crate::value::Value],
    state: &[i64],
) -> bool {
    match builtin {
        crate::host::Builtin::String(crate::strlib::StrFn::GmatchStep) => {
            crate::runtime::gmatch_fits(heap, values, state)
        }
        // `require` keeps the `package` table.
        crate::host::Builtin::Package(
            crate::package::PkgFn::Require | crate::package::PkgFn::SearchLua,
        ) => matches!(values, [crate::value::Value::Table(_)]) && state.is_empty(),
        // A `coroutine.wrap` function keeps its coroutine.
        crate::host::Builtin::Coroutine(crate::corolib::CoFn::WrapCall) => {
            matches!(values, [crate::value::Value::Thread(_)]) && state.is_empty()
        }
        crate::host::Builtin::Io(crate::iolib::IoFn::LinesStep) => {
            crate::runtime::io::lines_fits(heap, values, state)
        }
        _ => false,
    }
}

/// The most ranges a sort keeps pending. It always sorts the smaller
/// part first, so the stack is at most one entry per halving of 2^31.
pub(crate) const MAX_SORT_PENDING: usize = 40;

/// Each step of `table.sort`, named by the operation it makes. `Range`
/// makes none: it starts the current range or takes the next.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum SortStep {
    Length,
    Range,
    GetLo,
    GetUp,
    UpLessLo,
    SetLoUp,
    SetUpLo,
    GetP,
    GetLo2,
    PLessLo,
    SetPLo,
    SetLoP,
    GetUp2,
    UpLessP,
    SetPUp,
    SetUpP,
    GetPivot,
    GetUpMinus1,
    SetPToUpMinus1,
    SetUpMinus1ToPivot,
    GetI,
    ILessPivot,
    GetJ,
    PivotLessJ,
    SetIJ,
    SetJI,
    SetUpMinus1I,
    SetIPivot,
}

impl Work {
    /// The scratch slots the work keeps above the function's arguments.
    pub(crate) fn scratch(&self) -> u32 {
        match self {
            Self::Extreme { .. } => 0,
            Self::Insert { .. } | Self::Move { .. } | Self::Concat { .. } | Self::Pack { .. } => 1,
            Self::Remove { .. } => 2,
            Self::Unpack { count, .. } => *count,
            Self::Str(work) => work.scratch(),
            Self::Utf8(work) => work.scratch(),
            Self::Os(_) => 10,
            Self::Package(work) => work.scratch(),
            Self::Debug(work) => work.scratch(),
            Self::Sort(_) => 3,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The output of xoshiro256** from the reference implementation's
    /// state `[1, 2, 3, 4]`.
    #[test]
    fn xoshiro_matches_the_reference_outputs() {
        let mut state = LibraryState::new(0);
        state.rng = [1, 2, 3, 4];
        let outputs: Vec<u64> = (0..3).map(|_| state.next()).collect();
        assert_eq!(outputs, [11520, 0, 1509978240]);
    }

    #[test]
    fn projection_stays_in_range_without_modulo() {
        let mut state = LibraryState::new(7);
        for n in [0u64, 1, 2, 5, 6, 7, 1000, u64::MAX / 3, u64::MAX] {
            for _ in 0..200 {
                let random = state.next();
                assert!(state.project(random, n) <= n);
            }
        }
    }
}