Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safe API for calling lua functions from rust #5

Closed
volt0 opened this issue Sep 29, 2021 · 1 comment
Closed

Safe API for calling lua functions from rust #5

volt0 opened this issue Sep 29, 2021 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@volt0
Copy link
Contributor

volt0 commented Sep 29, 2021

This will require implementing a wrapper structure around the lua state. It should be possible to get an instance of the structure from the global state. Also add the call method, which takes as input a reference to the structure containing the arguments and returns a Result containing the result of the lua function or an error.

For example:

#[derive(ToLuaTable)]
struct User {
    username: String,
    email: String,
    id: u64,
    active: bool,
}

#[derive(FromLuaTable)]
struct CreateUserResult {
    ok: bool,
    id: u64,
}

...

let lua = Lua::global()
let result: CreateUserResult = lua.call("some_module.create_user", &user)?;
if result.ok {
    ...
}
-- some_module.lua
function create_user(username, email, id, active)
    ...
    return true, id
end
@volt0 volt0 added the enhancement New feature or request label Sep 29, 2021
@volt0 volt0 self-assigned this Sep 29, 2021
@t1-10
Copy link
Contributor

t1-10 commented Dec 29, 2021

Implemented in v5.0.0

@t1-10 t1-10 closed this as completed Dec 29, 2021
DmitryTravyan pushed a commit that referenced this issue Oct 16, 2024
This is due to the fact that lua_error calls _Unwind_RaiseException
under the hood, which causes rust's extern "C" functions to misbehave
when an exception passes through their frame.

Without this change we crash with a following log:

```
i1  | 2024-10-09 02:13:28.940 [120075] main/111/main C> panicked at library/core/src/panicking.rs:221:5:
i1  | panic in a function that cannot unwind
i1  | 2024-10-09 02:13:28.940 [120075] main/111/main C> backtrace:
i1  | disabled backtrace
i1  | 2024-10-09 02:13:28.940 [120075] main/111/main C> aborting due to panic
i1  | [supervisor:120065] no ipc message from child
i1  | [supervisor:120065] subprocess 120075 was signaled with SIGABRT
i1  | [supervisor:120065] core dumped
```

picodata repro:

```shell
pipenv run pytest -k test_submit_sql_after_revoke
```

backtrace:

```
#0  0x00007e04dc2a53f4 in ?? () from /usr/lib/libc.so.6
#1  0x00007e04dc24c120 in raise () from /usr/lib/libc.so.6
#2  0x00007e04dc2334c3 in abort () from /usr/lib/libc.so.6
#3  0x0000000005961a6a in std::sys::pal::unix::abort_internal () at library/std/src/sys/pal/unix/mod.rs:370
#4  0x0000000001e3227a in std::process::abort () at library/std/src/process.rs:2388
#5  0x0000000002e2d476 in picodata::cli::run::main::{closure#0}::{closure#1} (info=0x7e04bb8800f8) at src/cli/run.rs:120
#6  0x000000000595691f in alloc::boxed::{impl#50}::call<(&std::panic::PanicHookInfo), (dyn core::ops::function::Fn<(&std::panic::PanicHookInfo), Output=()> + core::marker::Send + core::marker::Sync), alloc::alloc::Global> ()
    at library/alloc/src/boxed.rs:2084
#7  std::panicking::rust_panic_with_hook () at library/std/src/panicking.rs:808
#8  0x0000000005956513 in std::panicking::begin_panic_handler::{closure#0} () at library/std/src/panicking.rs:667
#9  0x0000000005953c99 in std::sys::backtrace::__rust_end_short_backtrace<std::panicking::begin_panic_handler::{closure_env#0}, !> () at library/std/src/sys/backtrace.rs:168
#10 0x00000000059561d4 in std::panicking::begin_panic_handler () at library/std/src/panicking.rs:665
#11 0x0000000001e34e35 in core::panicking::panic_nounwind_fmt::runtime () at library/core/src/panicking.rs:112
#12 core::panicking::panic_nounwind_fmt () at library/core/src/panicking.rs:122
#13 0x0000000001e34ec2 in core::panicking::panic_nounwind () at library/core/src/panicking.rs:221
#14 0x0000000001e350e6 in core::panicking::panic_cannot_unwind () at library/core/src/panicking.rs:309
#15 0x0000000002c17f7a in tlua::functions_write::wrapper<tlua::functions_write::Function<picodata::set_login_check::{closure_env#1}, (alloc::string::String, bool, *mut tlua::ffi::lua_State), ()>, (alloc::string::String, bool, *mut tlua::ffi::lua_State), ()> (lua=0x40c2cc90) at tarantool/tlua/src/functions_write.rs:455
#16 0x000000000321d513 in lj_BC_FUNCC () at buildvm_x86.dasc:811
#17 0x0000000003224253 in lua_pcall (L=L@entry=0x40c2cc90, nargs=<optimized out>, nresults=nresults@entry=-1, errfunc=errfunc@entry=0) at picodata/tarantool-sys/third_party/luajit/src/lj_api.c:1172
#18 0x000000000301b47b in luaT_call (L=L@entry=0x40c2cc90, nargs=<optimized out>, nreturns=nreturns@entry=-1) at picodata/tarantool-sys/src/lua/utils.c:619
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants