segfault when lua_newtable called 100 times in a row

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

segfault when lua_newtable called 100 times in a row

Jura Pk
Hello, community!
I am stuck with a problem: when I'm trying to add 100 tables in Lua c stack, I receive segfault on function end.
Code example:

Maybe I'm doing it wrong? What setting I should increase to be able to add 100 tables?
local variables limit is 200 by default, registers limit is 254, so I don't get it.

static int lua_example_test( lua_State *L ) {
    for ( int i=0; i<100; i++ ) {
        lua_newtable(L);
    }
    return 1;
}

Reply | Threaded
Open this post in threaded view
|

Re: segfault when lua_newtable called 100 times in a row

Rena
On Sun, Apr 10, 2016 at 1:16 PM, Jura Pk <[hidden email]> wrote:
Hello, community!
I am stuck with a problem: when I'm trying to add 100 tables in Lua c stack, I receive segfault on function end.
Code example:

Maybe I'm doing it wrong? What setting I should increase to be able to add 100 tables?
local variables limit is 200 by default, registers limit is 254, so I don't get it.

static int lua_example_test( lua_State *L ) {
    for ( int i=0; i<100; i++ ) {
        lua_newtable(L);
    }
    return 1;
}


What are you trying to achieve here? You're creating 100 tables but doing nothing with them, then only returning one?

--
Sent from my Game Boy.
Reply | Threaded
Open this post in threaded view
|

Re: segfault when lua_newtable called 100 times in a row

Jura Pk
Rena, this example is extremely shortened. My goal is to decode json files full of tables.
for example, [[[[[[[[[[[[[]]]]]]]]]]]]]

2016-04-10 21:17 GMT+03:00 Rena <[hidden email]>:
On Sun, Apr 10, 2016 at 1:16 PM, Jura Pk <[hidden email]> wrote:
Hello, community!
I am stuck with a problem: when I'm trying to add 100 tables in Lua c stack, I receive segfault on function end.
Code example:

Maybe I'm doing it wrong? What setting I should increase to be able to add 100 tables?
local variables limit is 200 by default, registers limit is 254, so I don't get it.

static int lua_example_test( lua_State *L ) {
    for ( int i=0; i<100; i++ ) {
        lua_newtable(L);
    }
    return 1;
}


What are you trying to achieve here? You're creating 100 tables but doing nothing with them, then only returning one?

--
Sent from my Game Boy.

Reply | Threaded
Open this post in threaded view
|

Re: segfault when lua_newtable called 100 times in a row

Soni "They/Them" L.
In reply to this post by Jura Pk


On 10/04/16 02:16 PM, Jura Pk wrote:

> Hello, community!
> I am stuck with a problem: when I'm trying to add 100 tables in Lua c
> stack, I receive segfault on function end.
> Code example:
> lua_example.c: http://pastebin.com/GFvLwMY2
> Makefile: http://pastebin.com/3EWJiVz7
> test.lua: http://pastebin.com/1y22XvzK
>
> Maybe I'm doing it wrong? What setting I should increase to be able to
> add 100 tables?
> local variables limit is 200 by default, registers limit is 254, so I
> don't get it.
>
>     static int lua_example_test( lua_State *L ) {
>         for ( int i=0; i<100; i++ ) {
>             lua_newtable(L);
>         }
>         return 1;
>     }
>
>
You need to grow the stack.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: segfault when lua_newtable called 100 times in a row

Shmuel Zeigerman
In reply to this post by Jura Pk
On 10/04/2016 20:16, Jura Pk wrote:
> I am stuck with a problem: when I'm trying to add 100 tables in Lua c
> stack, I receive segfault on function end.
> Code example:
> lua_example.c: http://pastebin.com/GFvLwMY2
> Makefile: http://pastebin.com/3EWJiVz7
> test.lua: http://pastebin.com/1y22XvzK
>

See https://www.lua.org/manual/5.3/manual.html#lua_checkstack

--
Shmuel


Reply | Threaded
Open this post in threaded view
|

Re: segfault when lua_newtable called 100 times in a row

Jura Pk
Problem solved, thanks to Shmuel Zeigerman and Soni L.
After wrapping in lua_checkstack, the code works perfectly even with 1000 tables.
Seems, like lua_checkstack grows the stack =)

2016-04-10 21:27 GMT+03:00 Shmuel Zeigerman <[hidden email]>:
On 10/04/2016 20:16, Jura Pk wrote:
I am stuck with a problem: when I'm trying to add 100 tables in Lua c stack, I receive segfault on function end.
Code example:
lua_example.c: http://pastebin.com/GFvLwMY2
Makefile: http://pastebin.com/3EWJiVz7
test.lua: http://pastebin.com/1y22XvzK


See https://www.lua.org/manual/5.3/manual.html#lua_checkstack

--
Shmuel



Reply | Threaded
Open this post in threaded view
|

Re: segfault when lua_newtable called 100 times in a row

Nagaev Boris
On Sun, Apr 10, 2016 at 6:40 PM, Jura Pk <[hidden email]> wrote:

> Problem solved, thanks to Shmuel Zeigerman and Soni L.
> After wrapping in lua_checkstack, the code works perfectly even with 1000
> tables.
> Seems, like lua_checkstack grows the stack =)
>
> 2016-04-10 21:27 GMT+03:00 Shmuel Zeigerman <[hidden email]>:
>>
>> On 10/04/2016 20:16, Jura Pk wrote:
>>>
>>> I am stuck with a problem: when I'm trying to add 100 tables in Lua c
>>> stack, I receive segfault on function end.
>>> Code example:
>>> lua_example.c: http://pastebin.com/GFvLwMY2
>>> Makefile: http://pastebin.com/3EWJiVz7
>>> test.lua: http://pastebin.com/1y22XvzK
>>>
>>
>> See https://www.lua.org/manual/5.3/manual.html#lua_checkstack
>>
>> --
>> Shmuel
>>
>>
>

By the way, why not to grow the stack automatically in pushing API functions?

--


Best regards,
Boris Nagaev

Reply | Threaded
Open this post in threaded view
|

Re: segfault when lua_newtable called 100 times in a row

Soni "They/Them" L.


On 10/04/16 05:06 PM, Nagaev Boris wrote:

> On Sun, Apr 10, 2016 at 6:40 PM, Jura Pk <[hidden email]> wrote:
>> Problem solved, thanks to Shmuel Zeigerman and Soni L.
>> After wrapping in lua_checkstack, the code works perfectly even with 1000
>> tables.
>> Seems, like lua_checkstack grows the stack =)
>>
>> 2016-04-10 21:27 GMT+03:00 Shmuel Zeigerman <[hidden email]>:
>>> On 10/04/2016 20:16, Jura Pk wrote:
>>>> I am stuck with a problem: when I'm trying to add 100 tables in Lua c
>>>> stack, I receive segfault on function end.
>>>> Code example:
>>>> lua_example.c: http://pastebin.com/GFvLwMY2
>>>> Makefile: http://pastebin.com/3EWJiVz7
>>>> test.lua: http://pastebin.com/1y22XvzK
>>>>
>>> See https://www.lua.org/manual/5.3/manual.html#lua_checkstack
>>>
>>> --
>>> Shmuel
>>>
>>>
> By the way, why not to grow the stack automatically in pushing API functions?
>
Because of overhead. Imagine checking the stack every operation vs
checking the stack once for 1000 operations, or however many you need.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

[OT] Re: segfault when lua_newtable called 100 times in a row

Soni "They/Them" L.


On 10/04/16 05:44 PM, Soni L. wrote:

>
>
> On 10/04/16 05:06 PM, Nagaev Boris wrote:
>> On Sun, Apr 10, 2016 at 6:40 PM, Jura Pk <[hidden email]> wrote:
>>> Problem solved, thanks to Shmuel Zeigerman and Soni L.
>>> After wrapping in lua_checkstack, the code works perfectly even with
>>> 1000
>>> tables.
>>> Seems, like lua_checkstack grows the stack =)
>>>
>>> 2016-04-10 21:27 GMT+03:00 Shmuel Zeigerman <[hidden email]>:
>>>> On 10/04/2016 20:16, Jura Pk wrote:
>>>>> I am stuck with a problem: when I'm trying to add 100 tables in Lua c
>>>>> stack, I receive segfault on function end.
>>>>> Code example:
>>>>> lua_example.c: http://pastebin.com/GFvLwMY2
>>>>> Makefile: http://pastebin.com/3EWJiVz7
>>>>> test.lua: http://pastebin.com/1y22XvzK
>>>>>
>>>> See https://www.lua.org/manual/5.3/manual.html#lua_checkstack
>>>>
>>>> --
>>>> Shmuel
>>>>
>>>>
>> By the way, why not to grow the stack automatically in pushing API
>> functions?
>>
> Because of overhead. Imagine checking the stack every operation vs
> checking the stack once for 1000 operations, or however many you need.
>
A note about overhead: You can significantly speed up a Lua VM by using
a "microcoded" VM and optimizing the "microcode", that is, splitting
large operations into smaller operations and optimizing access.

E.g. for this code:

local t = {}
t[1] = true
t[2] = false
print(t[1], t[2])

Each table access needs to check if the table is nil and if the table
has a metatable. But with a "microcoded" VM, access to `t` could skip
those checks. It would however make the debug library significantly more
dangerous, and the VM would be significantly larger.

LuaJIT actually does this. The bytecode includes type hints and is
dynamically translated into IR and optimized, which then gets translated
into assembly. Altho I'm not sure if it includes the whole "making the
debug library dangerous" part.

If you need performance, you do it in C.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: segfault when lua_newtable called 100 times in a row

Andrew Starks-2
In reply to this post by Nagaev Boris
Do we have verification?
On Sun, Apr 10, 2016 at 15:07 Nagaev Boris <[hidden email]> wrote:
On Sun, Apr 10, 2016 at 6:40 PM, Jura Pk <[hidden email]> wrote:
> Problem solved, thanks to Shmuel Zeigerman and Soni L.
> After wrapping in lua_checkstack, the code works perfectly even with 1000
> tables.
> Seems, like lua_checkstack grows the stack =)
>
> 2016-04-10 21:27 GMT+03:00 Shmuel Zeigerman <[hidden email]>:
>>
>> On 10/04/2016 20:16, Jura Pk wrote:
>>>
>>> I am stuck with a problem: when I'm trying to add 100 tables in Lua c
>>> stack, I receive segfault on function end.
>>> Code example:
>>> lua_example.c: http://pastebin.com/GFvLwMY2
>>> Makefile: http://pastebin.com/3EWJiVz7
>>> test.lua: http://pastebin.com/1y22XvzK
>>>
>>
>> See https://www.lua.org/manual/5.3/manual.html#lua_checkstack
>>
>> --
>> Shmuel
>>
>>
>

By the way, why not to grow the stack automatically in pushing API functions?

--


Best regards,
Boris Nagaev

Reply | Threaded
Open this post in threaded view
|

Re: segfault when lua_newtable called 100 times in a row

Nagaev Boris
In reply to this post by Soni "They/Them" L.
On Sun, Apr 10, 2016 at 8:44 PM, Soni L. <[hidden email]> wrote:

>
>
> On 10/04/16 05:06 PM, Nagaev Boris wrote:
>>
>> On Sun, Apr 10, 2016 at 6:40 PM, Jura Pk <[hidden email]> wrote:
>>>
>>> Problem solved, thanks to Shmuel Zeigerman and Soni L.
>>> After wrapping in lua_checkstack, the code works perfectly even with 1000
>>> tables.
>>> Seems, like lua_checkstack grows the stack =)
>>>
>>> 2016-04-10 21:27 GMT+03:00 Shmuel Zeigerman <[hidden email]>:
>>>>
>>>> On 10/04/2016 20:16, Jura Pk wrote:
>>>>>
>>>>> I am stuck with a problem: when I'm trying to add 100 tables in Lua c
>>>>> stack, I receive segfault on function end.
>>>>> Code example:
>>>>> lua_example.c: http://pastebin.com/GFvLwMY2
>>>>> Makefile: http://pastebin.com/3EWJiVz7
>>>>> test.lua: http://pastebin.com/1y22XvzK
>>>>>
>>>> See https://www.lua.org/manual/5.3/manual.html#lua_checkstack
>>>>
>>>> --
>>>> Shmuel
>>>>
>>>>
>> By the way, why not to grow the stack automatically in pushing API
>> functions?
>>
> Because of overhead. Imagine checking the stack every operation vs checking
> the stack once for 1000 operations, or however many you need.
>

If the stack has room for new element, than the check is just one
comparison, which would be perfectly predicted.

--


Best regards,
Boris Nagaev

Reply | Threaded
Open this post in threaded view
|

Re: segfault when lua_newtable called 100 times in a row

Soni "They/Them" L.


On 10/04/16 06:49 PM, Nagaev Boris wrote:

> On Sun, Apr 10, 2016 at 8:44 PM, Soni L. <[hidden email]> wrote:
>>
>> On 10/04/16 05:06 PM, Nagaev Boris wrote:
>>> On Sun, Apr 10, 2016 at 6:40 PM, Jura Pk <[hidden email]> wrote:
>>>> Problem solved, thanks to Shmuel Zeigerman and Soni L.
>>>> After wrapping in lua_checkstack, the code works perfectly even with 1000
>>>> tables.
>>>> Seems, like lua_checkstack grows the stack =)
>>>>
>>>> 2016-04-10 21:27 GMT+03:00 Shmuel Zeigerman <[hidden email]>:
>>>>> On 10/04/2016 20:16, Jura Pk wrote:
>>>>>> I am stuck with a problem: when I'm trying to add 100 tables in Lua c
>>>>>> stack, I receive segfault on function end.
>>>>>> Code example:
>>>>>> lua_example.c: http://pastebin.com/GFvLwMY2
>>>>>> Makefile: http://pastebin.com/3EWJiVz7
>>>>>> test.lua: http://pastebin.com/1y22XvzK
>>>>>>
>>>>> See https://www.lua.org/manual/5.3/manual.html#lua_checkstack
>>>>>
>>>>> --
>>>>> Shmuel
>>>>>
>>>>>
>>> By the way, why not to grow the stack automatically in pushing API
>>> functions?
>>>
>> Because of overhead. Imagine checking the stack every operation vs checking
>> the stack once for 1000 operations, or however many you need.
>>
> If the stack has room for new element, than the check is just one
> comparison, which would be perfectly predicted.
>
And a branch.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: segfault when lua_newtable called 100 times in a row

Nagaev Boris
On Sun, Apr 10, 2016 at 9:54 PM, Soni L. <[hidden email]> wrote:

>
>
> On 10/04/16 06:49 PM, Nagaev Boris wrote:
>>
>> On Sun, Apr 10, 2016 at 8:44 PM, Soni L. <[hidden email]> wrote:
>>>
>>>
>>> On 10/04/16 05:06 PM, Nagaev Boris wrote:
>>>>
>>>> On Sun, Apr 10, 2016 at 6:40 PM, Jura Pk <[hidden email]> wrote:
>>>>>
>>>>> Problem solved, thanks to Shmuel Zeigerman and Soni L.
>>>>> After wrapping in lua_checkstack, the code works perfectly even with
>>>>> 1000
>>>>> tables.
>>>>> Seems, like lua_checkstack grows the stack =)
>>>>>
>>>>> 2016-04-10 21:27 GMT+03:00 Shmuel Zeigerman <[hidden email]>:
>>>>>>
>>>>>> On 10/04/2016 20:16, Jura Pk wrote:
>>>>>>>
>>>>>>> I am stuck with a problem: when I'm trying to add 100 tables in Lua c
>>>>>>> stack, I receive segfault on function end.
>>>>>>> Code example:
>>>>>>> lua_example.c: http://pastebin.com/GFvLwMY2
>>>>>>> Makefile: http://pastebin.com/3EWJiVz7
>>>>>>> test.lua: http://pastebin.com/1y22XvzK
>>>>>>>
>>>>>> See https://www.lua.org/manual/5.3/manual.html#lua_checkstack
>>>>>>
>>>>>> --
>>>>>> Shmuel
>>>>>>
>>>>>>
>>>> By the way, why not to grow the stack automatically in pushing API
>>>> functions?
>>>>
>>> Because of overhead. Imagine checking the stack every operation vs
>>> checking
>>> the stack once for 1000 operations, or however many you need.
>>>
>> If the stack has room for new element, than the check is just one
>> comparison, which would be perfectly predicted.
>>
> And a branch.
... perfectly predicted branch



--


Best regards,
Boris Nagaev

Reply | Threaded
Open this post in threaded view
|

Re: segfault when lua_newtable called 100 times in a row

Soni "They/Them" L.


On 10/04/16 07:01 PM, Nagaev Boris wrote:

> On Sun, Apr 10, 2016 at 9:54 PM, Soni L. <[hidden email]> wrote:
>>
>> On 10/04/16 06:49 PM, Nagaev Boris wrote:
>>> On Sun, Apr 10, 2016 at 8:44 PM, Soni L. <[hidden email]> wrote:
>>>>
>>>> On 10/04/16 05:06 PM, Nagaev Boris wrote:
>>>>> On Sun, Apr 10, 2016 at 6:40 PM, Jura Pk <[hidden email]> wrote:
>>>>>> Problem solved, thanks to Shmuel Zeigerman and Soni L.
>>>>>> After wrapping in lua_checkstack, the code works perfectly even with
>>>>>> 1000
>>>>>> tables.
>>>>>> Seems, like lua_checkstack grows the stack =)
>>>>>>
>>>>>> 2016-04-10 21:27 GMT+03:00 Shmuel Zeigerman <[hidden email]>:
>>>>>>> On 10/04/2016 20:16, Jura Pk wrote:
>>>>>>>> I am stuck with a problem: when I'm trying to add 100 tables in Lua c
>>>>>>>> stack, I receive segfault on function end.
>>>>>>>> Code example:
>>>>>>>> lua_example.c: http://pastebin.com/GFvLwMY2
>>>>>>>> Makefile: http://pastebin.com/3EWJiVz7
>>>>>>>> test.lua: http://pastebin.com/1y22XvzK
>>>>>>>>
>>>>>>> See https://www.lua.org/manual/5.3/manual.html#lua_checkstack
>>>>>>>
>>>>>>> --
>>>>>>> Shmuel
>>>>>>>
>>>>>>>
>>>>> By the way, why not to grow the stack automatically in pushing API
>>>>> functions?
>>>>>
>>>> Because of overhead. Imagine checking the stack every operation vs
>>>> checking
>>>> the stack once for 1000 operations, or however many you need.
>>>>
>>> If the stack has room for new element, than the check is just one
>>> comparison, which would be perfectly predicted.
>>>
>> And a branch.
> ... perfectly predicted branch
>
>
>
And it still has a cost. It's not free.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.