[FUN] pmain

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

[FUN] pmain

Egor Skriptunoff-2
[FUN] here means both "fun" and "function" :-)

function segfault()
   for k = 1, 1/0 do
      x = debug.getinfo(k) or x.func()
   end
end

Actually, this code invokes pmain()
https://www.lua.org/source/5.3/lua.c.html#pmain
Vanilla Lua crashes (why?)
LuaJIT indeed invokes "nested interpreter" (press Ctrl-D to return back to "parent interpreter")
v
Reply | Threaded
Open this post in threaded view
|

Re: [FUN] pmain

v
On Sat, 2019-08-17 at 11:26 +0300, Egor Skriptunoff wrote:
> function segfault()
>    for k = 1, 1/0 do
>       x = debug.getinfo(k) or x.func()
>    end
> end

To make it a bit more clear:

for k = 1, math.maxinteger or math.huge do
    x = debug.getinfo(k) or x.func()
end

So this calls lowest function from Lua stack. Kinda interesting.

> Vanilla Lua crashes (why?)

I guess you're simply not supposed to access it normally. I would
actually like to see "Dumb ways to die" but with how to ruin Lua with
`debug` library :-). This one would make a decent contribution.

--
v <[hidden email]>


Reply | Threaded
Open this post in threaded view
|

Re: [FUN] pmain

Sean Conner
In reply to this post by Egor Skriptunoff-2
It was thus said that the Great Egor Skriptunoff once stated:
> [FUN] here means both "fun" and "function" :-)
>
> function segfault()
>    for k = 1, 1/0 do
>       x = debug.getinfo(k) or x.func()
>    end
> end
>
> Actually, this code invokes pmain()

  And it actually crashes in collectargs() because argv is NULL and there
are no checks for that.  This also happens in Lua 5.1 and 5.2 as well.  Now,
*why* argv is NULL is another question ...

  -spc


Reply | Threaded
Open this post in threaded view
|

Re: [FUN] pmain

Luiz Henrique de Figueiredo
> > Actually, this code invokes pmain()
>
>   And it actually crashes in collectargs() because argv is NULL and there
> are no checks for that. [...]  Now, *why* argv is NULL is another question ...

Because pmain is being called in luaD_precall with no upvalues.
The point is that pmain is being called *again*, the second time
without preparation.