Skip to content
SocletsPublic

About

Pyro Lua: Lua 5.4 in SPARK, proven not to crash

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

62 Commits

Folders and files

Repository files navigation

Pyro Lua

Linux

Pyro Lua is abandoned (2026-11-10). The owner has stopped working on it. The code and its proofs stay here as they were left: milestones M1 to M4 are done (a proven front end, compiler and virtual machine that run Lua 5.4 scripts), M5 and M6 were never started. See Status.

Pyro Lua is a memory-safe Lua 5.4 for running scripts you do not trust: game mods and plugins, server-side scripting in the style of Redis or nginx, rules engines. It is written in SPARK (the formally verifiable subset of Ada), and the goal is a machine-checked proof that no script can make the interpreter itself overflow a buffer, read out of bounds or use freed memory, so that a hostile script can only fail with a Lua error or run out of the limits its host gave it.

  • Language: Lua 5.4, as defined by the Lua 5.4 Reference Manual.
  • Embedding: Pyro's own C API (pyrolua.h) for C and C++ hosts, and an Ada/SPARK API, both thin layers over the same proven core. See Scope.
  • Core only: no standard library (string, table, math, io, os, …). The host program decides which functions a script can call.
  • Proof: absence of runtime errors (SPARK Silver) for all of Pyro, plus functional proofs (Gold) for the parts where a bug would mean memory unsafety: the heap and garbage collector, bytecode validity, the VM stack and tables. The host's own code, C or Ada, is outside the proofs. See docs/PROOFS.md, and tests/cve/ for how Pyro handles the public PUC Lua CVEs.
  • Behaviour vs. reference Lua: checked by differential testing against the reference implementation (PUC Lua 5.4), not by proof.

Status

Abandoned on 2026-11-10. Nothing more is planned. Where it stopped:

  • Done, proven and tested (M1 to M4): the lexer, parser and resolver; the compiler, whose every prototype is proven valid; values, heap, strings, tables and a mark-and-sweep garbage collector proven never to free a reachable object; and the virtual machine (Silver, with the frame invariant at Gold). pyrolua file.lua runs Lua 5.4 scripts byte-identically to PUC Lua 5.4.9 on the runtime corpus, and --check --lua-style and --bytecode --lua-style match luac 5.4.9 exactly. The public PUC Lua CVEs up to M4 in tests/cve/ all end cleanly.
  • Never started: M5 (coroutines, weak tables, finalisers) and M6 (the C and Ada embedding APIs, sandbox limits as a public interface). Without M6 there is no way to embed Pyro Lua in another program.
  • Known weakness at the time: speed. An optimised build was 2 to 15 times slower than PUC Lua 5.4.9 on small benchmarks (13.5 times on function calls), and no profiling or tuning was ever done.

The milestones and their completion criteria are in docs/ROADMAP.md.

Documentation

Document Contents
docs/DESIGN.md Architecture, data representation, key design decisions
docs/PROOFS.md What is proven, what is trusted, what is only tested, proof rules
docs/ROADMAP.md Milestones and their completion criteria
docs/STYLE.md How code is written, tested and committed; working rules

Scope

In scope:

  • The full Lua 5.4 language: syntax, semantics, metatables and metamethods, integer/float numbers, closures and upvalues, varargs, goto, coroutines, <const> and <close> variables, garbage collection.
  • Pyro's own C API (pyrolua.h, static and shared libraries) for C and C++ hosts, and an Ada/SPARK API: load a chunk, call functions, exchange values, register host functions, and reach the primitives a host needs (metatables, protected calls, coroutine resume/yield, table traversal).
  • Sandbox limits: instruction budget, memory limit and depth limits, so untrusted scripts can be stopped cleanly, and a random hash seed per state, so scripts cannot craft table keys that all collide.
  • A small pyrolua command-line program for running and inspecting .lua files.

Out of scope:

  • The Lua standard library. A host may implement any of it on top of the embedding API, but Pyro does not ship it.
  • Drop-in compatibility with PUC Lua's C API (lua.h). Its shared stack addressed by index, its error handling by longjmp and its C modules, which run unproven code inside the interpreter, would each undo what the proofs guarantee. Pyro's C API has opaque handles instead of stack slots and returns errors as codes.
  • Binary compatibility with PUC Lua (bytecode format, .luac files).
  • Proving that Pyro's behaviour matches the reference manual in full. That is what differential testing is for.

A consequence worth knowing: in Lua 5.4, automatic string-to-number coercion in arithmetic ("10" + 1) is implemented by the string library's metamethods, not the core. Pyro follows the core, so "10" + 1 raises an error unless the host installs a string metatable that provides it. Number-to-string coercion in concatenation (1 .. "") is core behaviour and works.

Requirements

  • Alire 2.x (alr), which provides:
    • GNAT native toolchain (developed with GNAT 16.1)
    • gprbuild
    • gnatprove (developed with 16.1)
  • Reference Lua 5.4 (for tests only): brew install lua@5.4 (5.4.9). Note that the plain lua Homebrew formula is now 5.5, which is not a valid reference: 5.5 changes syntax and semantics. The lua@5.4 binaries are shadowed by 5.5's on PATH, so always call them by full path: /opt/homebrew/opt/lua@5.4/bin/lua and /opt/homebrew/opt/lua@5.4/bin/luac (or $(brew --prefix lua@5.4)/bin/…). Test scripts take the folder holding lua and luac from PYRO_REF_LUA, defaulting to /opt/homebrew/opt/lua@5.4/bin.

Building, proving, testing

Layout and commands:

alr build                                   # build the library
cd cli && alr build                         # build the pyrolua executable
alr exec -- gnatprove -P pyro_lua.gpr       # run the proofs (settings live in pyro_lua.gpr)
tests/run                                   # everything: build, proofs, tests (5 min cap)
tests/run --full                            # clean proofs and long fuzzing (30 min cap)

Command-line program (M1 subset):

pyrolua --tokens file.lua      # dump the token stream
pyrolua --ast file.lua         # dump the syntax tree
pyrolua --check file.lua       # syntax and scope check, every certain error in Pyro's layout
pyrolua --check --lua-style file.lua      # the one error `luac -p` would print, in its words
pyrolua --bytecode file.lua    # the compiled prototypes
pyrolua --bytecode --lua-style file.lua   # the same, as `luac -l -l` prints them

Before the first tests/run, fetch the official Lua test suite and Unicode's grapheme break test:

tools/fetch-lua-tests
tools/generate-grapheme-tables

Later milestones add pyrolua file.lua (run).

Repository layout

pyro_lua.gpr, alire.toml   Library crate (all SPARK code lives here)
src/                       Library sources, packages named Pyro.*, one folder per
                           pipeline stage (common, front_end, compiler, runtime,
                           embedding)
cli/                       Nested crate for the `pyrolua` executable
tests/                     Test runner, corpus, golden files
tools/                     Helper scripts (reference Lua setup, corpus download, fuzzing)
docs/                      Design, proof and roadmap documents

Development rules

The full rules are in docs/STYLE.md. The essentials:

  • Every library unit is SPARK_Mode => On. The only exceptions are the trusted units listed in docs/PROOFS.md, and adding one means adding it to that list with a reason.
  • gnatprove must report zero unproved checks on main. A change that leaves a check unproved is not finished.
  • No exceptions are raised or handled in library code. Errors are values (status records) or, at Lua level, VM unwinding. See docs/DESIGN.md.
  • No unbounded recursion on the Ada stack. Every recursive subprogram carries a depth parameter with a proven bound.
  • When behaviour is in doubt, the reference is PUC Lua 5.4 (lua@5.4), then the reference manual. The source of PUC Lua (llex.c, lparser.c, lvm.c, …) is the best guide to edge cases.
  • Documentation is part of the change: when a design decision changes, update docs/ in the same commit.

License

Every source file starts with an SPDX line:

--  SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

About

Pyro Lua: Lua 5.4 in SPARK, proven not to crash

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages