Skip to content

A panic in the Lua::exec_raw closure unwinds through lua_pcall and leaves a dangling error handler (segfault) #762

Closed

Description

@marius-momeu

Lua::exec_raw has no # Safety section, but the closure it takes runs inside lua_pcall through an extern "C-unwind" trampoline (protect_lua_closure, whose comment says the function must not panic). If the closure panics, the panic unwinds through Lua's C frames, which restore nothing. L->errorJmp keeps pointing at the jump buffer of the unwound luaD_rawrunprotected frame. When exec_raw runs inside a Rust callback, mlua catches the panic and raises it with lua_error, which then longjmps into the dead frame.

use mlua::{Lua, Result};

fn main() -> Result<()> {
    let lua = Lua::new();
    let f = lua.create_function(|lua, ()| unsafe {
        lua.exec_raw::<()>((1, 2, 3), |_state| panic!("closure panicked"))
    })?;
    lua.globals().set("f", f)?;
    let _ = lua.load("return pcall(f)").eval::<(bool, mlua::Value)>();
    Ok(())
}

With vendored Lua built in release under AddressSanitizer:

thread 'main' panicked at src/main.rs:9:48:
closure panicked
==265530==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x7f95369f9110 bp 0xe0b4cfd1ca92a58b sp 0x35c0cfd1deaaa22a T0)

Without the sanitizer it segfaults. Reproduced on mlua 0.12.2 and on master at a0ad453, with Lua 5.5.1 (vendored, lua55) and rustc 1.102.0-nightly (8eb778d04 2026-11-12), on x86_64 Linux.

A possible fix: catch the panic inside the trampoline, return normally to lua_pcall and resume the unwind after it returns. Also document exec_raw's safety requirements.

Found by Crustify, an experimental UB/soundness auditing agent developed at UC Berkeley and running on Claude Opus 5.5, then manually reviewed and independently reproduced.

Activity

  1. added
    ai-report
    on Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai-report

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions