[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Can I 'lua_resume' a 'luaL_newstate' and 'lua_close' it as 'LUA_YIELD'?
- From: Renato Maia <maia@...>
- Date: Sun, 31 May 2020 14:20:08 -0300
Hi list.
Is is legal to do as in the example below?
```
/* main.c */
#include <assert.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
static int loadmain (lua_State *L) {
int status;
luaL_openlibs(L);
status = luaL_loadfile(L, "main.lua");
assert(status == LUA_OK);
return 1;
}
int main (int argc, char **argv) {
int status;
lua_State *L = luaL_newstate();
(void)argc;
(void)argv;
assert(L);
lua_pushcfunction(L, &loadmain);
status = lua_pcall(L, 0, 1, 0);
assert(status == LUA_OK);
status = lua_resume(L, NULL, 0);
assert(status == LUA_YIELD);
lua_close(L);
return 0;
}
```
```
-- main.lua
require "_G"
require "coroutine"
setmetatable({}, { __gc = function () io.open(nil) end })
coroutine.yield()
```
Because I'm getting a spurious segmentation fault while handling the
error in the finalizer in `lua_close`. The example above can reproduce
the problem with Lua 5.3.5 most of the time in my environment. But I
was not able to reproduce it with Lua 5.2.4 nor Lua 5.4.0 alpha.
```
$ gdb main
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from main...done.
(gdb) run
Starting program: .../lua_closeyielded/main
Program received signal SIGSEGV, Segmentation fault.
0x0000000000407e40 in lua_getinfo (L=0x643018, what=0x43599e "n",
ar=0x7fffffffd860) at ldebug.c:328
328 cl = ttisclosure(func) ? clvalue(func) : NULL;
(gdb) bt
#0 0x0000000000407e40 in lua_getinfo (L=0x643018, what=0x43599e "n",
ar=0x7fffffffd860) at ldebug.c:328
#1 0x000000000041f2c9 in luaL_argerror (L=0x643018, arg=1,
extramsg=0x649138 "string expected, got nil") at lauxlib.c:168
#2 0x000000000041f476 in typeerror (L=0x643018, arg=1, tname=0x435085
"string") at lauxlib.c:192
#3 0x000000000041f4af in tag_error (L=0x643018, arg=1, tag=4) at lauxlib.c:197
#4 0x000000000041fbce in luaL_checklstring (L=0x643018, arg=1,
len=0x0) at lauxlib.c:392
#5 0x0000000000428e8c in io_open (L=0x643018) at liolib.c:257
#6 0x0000000000409de0 in luaD_precall (L=0x643018, func=0x643670,
nresults=0) at ldo.c:434
#7 0x000000000041da04 in luaV_execute (L=0x643018) at lvm.c:1134
#8 0x000000000040a17c in luaD_call (L=0x643018, func=0x643660,
nResults=0) at ldo.c:499
#9 0x000000000040a1da in luaD_callnoyield (L=0x643018, func=0x643660,
nResults=0) at ldo.c:509
#10 0x000000000040d99a in dothecall (L=0x643018, ud=0x0) at lgc.c:803
#11 0x0000000000409110 in luaD_rawrunprotected (L=0x643018, f=0x40d96a
<dothecall>, ud=0x0) at ldo.c:142
#12 0x000000000040a9ad in luaD_pcall (L=0x643018, func=0x40d96a
<dothecall>, u=0x0, old_top=48, ef=0) at ldo.c:729
#13 0x000000000040db15 in GCTM (L=0x643018, propagateerrors=0) at lgc.c:823
#14 0x000000000040dc90 in callallpendingfinalizers (L=0x643018) at lgc.c:862
#15 0x000000000040dfe1 in luaC_freeallobjects (L=0x643018) at lgc.c:971
#16 0x0000000000414f28 in close_state (L=0x643018) at lstate.c:245
#17 0x000000000041541b in lua_close (L=0x643018) at lstate.c:344
#18 0x0000000000403dd8 in main (argc=1, argv=0x7fffffffe3d8) at main.c:28
(gdb) print *func
Cannot access memory at address 0xc8c790
```
--
Renato Maia