Skip to content

illegal hardware instruction when using ffi+luajit on beta and nightly >=2021-03-11 #83541

Description

@khvzak

Hello, I'm mlua maintainer and found a regression when running tests for the library on the latest beta and nightly.
I narrowed it down to a minimal example:

Code

main.rs

mod ffi {
    use std::os::raw::{c_int, c_void};

    type LuaCFn = unsafe extern "C" fn(L: *mut c_void) -> c_int;

    extern "C" {
        pub fn luaL_newstate() -> *mut c_void;
        pub fn lua_pcall(L: *mut c_void, nargs: c_int, nresults: c_int, errfunc: c_int) -> c_int;
        pub fn lua_gettable(L: *mut c_void, idx: c_int);
        pub fn lua_pushcclosure(L: *mut c_void, f: LuaCFn, n: c_int);
        pub fn lua_pushnil(L: *mut c_void);
    }

    // #[inline(always)]
    pub unsafe fn lua_gettable2(state: *mut c_void, idx: c_int) {
        lua_gettable(state, idx);
    }
}

pub unsafe fn test_me() {
    let state = ffi::luaL_newstate();
    assert!(!state.is_null());

    unsafe extern "C" fn run_me(state: *mut std::os::raw::c_void) -> std::os::raw::c_int {
        ffi::lua_pushnil(state);
        ffi::lua_pushnil(state);
        ffi::lua_gettable2(state, -2); // Changing this to `ffi::lua_gettable` solves the problem
        1
    }

    ffi::lua_pushcclosure(state, run_me, 0);
    assert!(ffi::lua_pcall(state, 0, -1, 0) != 0);
}

fn main() {
    unsafe { test_me() }
}

Cargo.toml

[package]
name = "luajit_test"
version = "0.1.0"
edition = "2018"
build = "build.rs"

[build-dependencies]
luajit-src = "*"

build.rs

fn main() {
    let artifacts = luajit_src::Build::new().build();
    artifacts.print_cargo_metadata();
    println!("cargo:rerun-if-changed=build.rs");
}

Repo: https://github.com/khvzak/luajit_bug

It works on stable rust, but fails on beta and nightly with illegal hardware instruction on macos 10.15 and linux (at least).

It works if uncomment #[inline(always)] OR replace ffi::lua_gettable2 call with ffi::lua_gettable.
Reproducible only using LuaJIT, but not on Lua 5.1.

Version it worked on

It most recently worked on: nightly-2021-03-10. Does not work on nightly-2021-03-11.

Version with regression

rustc --version --verbose:

rustc 1.52.0-nightly (f98721f88 2021-03-10)
binary: rustc
commit-hash: f98721f886ab52d32d622ad0a46216ad03f3e525
commit-date: 2021-03-10
host: x86_64-unknown-linux-gnu
release: 1.52.0-nightly
LLVM version: 12.0.0

No activity

Activity on this issue will appear here.

Activity

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

    A-FFIArea: Foreign function interface (FFI)regression-from-stable-to-betaPerformance or correctness regression from stable to beta.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions