Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Library:Lua

From ReverseEngineering.wiki
Library
Developer PUC-Rio (R. Ierusalimschy, L. H. de Figueiredo, W. Celes)
Platforms any (portable ANSI C; DLL builds usually lua5x.dll / liblua5.x.so)
Versions 5.0 (2003), 5.1 (2006), 5.2 (2011), 5.3 (2015), 5.4 (2020), 5.5.0 (2025-12), 5.5.1 (2026-07)
License MIT
Website https://www.lua.org/
On this wiki Facts · Code · Notes · Topics

The reference implementation of the Lua scripting language (PUC-Rio, MIT license): a small C library embedded in many games and applications for scripting and plugins. Each minor version (5.1 to 5.5) has its own C API and its own bytecode, and its constant tables name the version even in stripped binaries.

Overview

Lua is an embeddable scripting language from PUC-Rio (first release 1993). The reference implementation is about 30 C files: the core (lapi.c, lcode.c, ldo.c, lgc.c, lparser.c, lvm.c, lundump.c/ldump.c...) and the standard libraries (lbaselib.c, lstrlib.c, ltablib.c...). It is usually linked statically into the host program, so in a binary it shows up as a few hundred unnamed functions next to its constant tables and messages. Hosts call it through the C API (luaL_newstate, luaL_loadbuffer[x], lua_pcall[k], lua_pushcclosure...) and often ship scripts precompiled as Format:Lua bytecode.

Each minor version (5.1, 5.2, 5.3, 5.4, 5.5) changes the C API, the VM instruction set and the bytecode format, so the first job in any Lua-scripted target is to find out which one it embeds. LuaJIT (a separate implementation of the 5.1 language with its own bytecode, header 1B 4C 4A) and Luau (Roblox's fork) are different code: the signatures on this page do not apply to them.

Identification

What survives in a build, from most to least reliable:

  1. luaP_opmodes[] (lopcodes.c): one byte per opcode (38 bytes in 5.1, 40 in 5.2, 47 in 5.3, 83 in 5.4, 85 in 5.5). The VM needs it at run time, so it cannot be dropped, and it is identical in every release of a minor version (all tags from 5.1 to 5.4.9 compared), except that 5.5.0 and 5.5.1 differ in one byte. The six byte patterns are signature facts; luascan and export_symbols with subject Library:Lua search for them.
  2. luai_ctype_[257] (lctype.c, 5.2 and later): the character-class table used by the lexer, identical from 5.2 to 5.5; it proves Lua 5.2+ but not the minor version.
  3. lua_ident (lapi.c): "$LuaVersion: Lua 5.4.8 Copyright (C) 1994-2025 Lua.org, PUC-Rio $$LuaAuthors: R. Ierusalimschy, L. H. de Figueiredo, W. Celes $" in 5.2 and later (note the two spaces before Copyright); in 5.1 "$Lua: Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio $\n$Authors: R. Ierusalimschy, L. H. de Figueiredo & W. Celes $\n$URL: www.lua.org $\n". It gives the exact release and survived in every binary checked (MSVC and third-party builds).
  4. Messages: bad header in precompiled chunk (5.1), version mismatch in precompiled chunk (5.2, 5.3), bad binary format (version mismatch) and truncated chunk (5.4, 5.5); loop in gettable (5.1, 5.2) became '__index' chain too long; possible loop (5.3+); C stack overflow, error in error handling, attempt to %s a %s value, _LOADED exist in all versions.
  5. Metamethod names in luaT_eventname[] order: 5.1 __index __newindex __gc __mode __eq __add ... __unm __len __lt __le __concat __call; 5.2 moves __len to fifth place; 5.3 adds __idiv __band __bor __bxor __shl __shr __bnot; 5.4 and 5.5 add __close at the end.
  6. Exports (DLL builds): lua_setfenv/lua_getfenv and a real lua_pcall only in 5.1; lua_pcallk/lua_callk/luaL_loadbufferx from 5.2 (lua_pcall is then a macro); lua_resetthread in 5.4 (deprecated from 5.4.6, which adds lua_closethread).

Checked on three third-party binaries, all found by the signatures and agreeing with their lua_ident: VLC media player 3.0.23 (Windows x64) liblua_plugin.dll (Lua 5.1.5), Unity Hub editor PerforcePlugin.exe (Lua 5.3.6) and quickbms as bundled with the Vortex mod manager (Lua 5.3.5).

Versions

Version Opcodes Bytecode version byte Notes
5.0 35 0x50 header without format byte; not covered further here
5.1 38 0x51 setfenv, module; the language level of LuaJIT; very common in games
5.2 40 0x52 _ENV upvalue replaces environments; goto
5.3 47 0x53 64-bit integers, bitwise operators, utf8 library
5.4 83 0x54 new register VM with k bit, to-be-closed variables, generational GC
5.5 85 0x55 new bytecode encodings (see Format:Lua bytecode)

API overview

A host creates a state with luaL_newstate() (5.1+; lua_open in 5.0), opens the libraries with luaL_openlibs, loads code with luaL_loadbuffer/luaL_loadbufferx/luaL_loadfilex/lua_load (all accept source or bytecode; 5.2+ take a mode string "b", "t" or "bt") and runs it with lua_pcall. Registered C functions have the type int (*)(lua_State *), and luaL_Reg arrays of {name, function} pairs passed to luaL_register (5.1) or luaL_setfuncs/luaL_newlib (5.2+) are the quickest way to name a host's script API: each entry is a pointer to a string next to a function pointer.

Code & repositories

  • https://github.com/lua/lua @ 8cb93bada561977eb370819e39d7d644e5fd0a3f · license: MIT (declared: the detector found none; check the repository)
    Official development repository of the Lua reference implementation (PUC-Rio), with release tags v1.0 to v5.5.1 for diffing any version found in a binary. Note: it has no luac.c (only the lua.org tarballs do).
    Tested on: Read at master 8cb93ba and at every 5.x release tag; tags v5.1 to v5.5.1 built with MSVC 14.44 x64 (/O2) plus a self-written lua_dump driver; MIT license text at the end of lua.h (no LICENSE file in the repository). · checked 2026-10-17; last activity 2026-10-16

Pitfalls

  • The GitHub repository lua/lua is the development history: it has no luac.c (that file ships only in the release tarballs from lua.org). To produce bytecode from a self-built Lua, call lua_dump (or string.dump) yourself.
  • Vendors may change Lua: a different LUAI_MAXCCALLS, a renumbered opcode list or a custom header defeat the opmodes signature and the stock decompilers. Unverified for any specific product; when the signatures fail, look for luai_ctype_, lua_ident and the messages above.
  • 32-bit and 64-bit builds of 5.1/5.2 write different bytecode headers (sizeof(size_t)), see Format:Lua bytecode#Pitfalls.

Open questions

  • Which constant tables identify LuaJIT (2.0/2.1) and Luau builds, and which version of each is embedded? — The luaP_opmodes and luai_ctype_ signatures on this page were checked only on PUC-Rio Lua 5.1-5.5. LuaJIT has its own bytecode (1B 4C 4A header, BCDUMP version 1 or 2) and VM, and Luau (Roblox) is a separate fork; neither was examined. A table of their recognisable data (and version strings) would c (asked by PatientStepper99, 2026-10-17)

Evidence & references

  • Source read at https://github.com/lua/lua tags v5.0 to v5.5.1 (master at commit 8cb93bada561977eb370819e39d7d644e5fd0a3f). Every release tag from 5.1 to 5.5.1 was built with MSVC 14.44 (x64, /O2) together with a small test program that printed luaP_opmodes, the opcode names and luai_ctype_.
  • Third-party binaries were only read (scanned with luascan.py), never run.