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

Stack overflow cannot be properly reported by xpcall #962

Closed
nlebedenco opened this issue Feb 20, 2023 · 3 comments
Closed

Stack overflow cannot be properly reported by xpcall #962

nlebedenco opened this issue Feb 20, 2023 · 3 comments

Comments

@nlebedenco
Copy link

The stack overflow check in lj_state_growstack does not account for a potential error handler invoked by xpcall. Changing it to if (L->stacksize >= LJ_STACK_MAXEX) allows the error handler to be called without causing a second error. The way it is now when the error handler requests a stack slot for the error object it bumps into the check at the beginning of lj_state_growstack and raises a LUA_ERRERR.

Current behaviour:

$> luajit.exe -e "f = function() f() end; f()"
error in error handling

Expected behaviour:

$> luajit.exe -e "f = function() f() end; f()"
luajit.exe: (command line):1: stack overflow
stack traceback:
        (command line):1: in function 'f'
        (command line):1: in function 'f'
        (command line):1: in function 'f'
        (command line):1: in function 'f'
        (command line):1: in function 'f'
        (command line):1: in function 'f'
        (command line):1: in function 'f'
        (command line):1: in function 'f'
        (command line):1: in function 'f'
        (command line):1: in function 'f'
        (command line):1: in function 'f'
        ...
        (command line):1: in function 'f'
        (command line):1: in function 'f'
        (command line):1: in function 'f'
        (command line):1: in function 'f'
        (command line):1: in function 'f'
        (command line):1: in function 'f'
        (command line):1: in function 'f'
        (command line):1: in function 'f'
        (command line):1: in main chunk
        [C]: at 0x7ff6418a5630
nlebedenco added a commit to DrunkenLemurs/LuaJIT that referenced this issue Feb 20, 2023
…ck size reaches LJ_STACK_MAXEX so that a potential error handler invoked by xpcall can still grow the stack one last time instead of causing a LUA_ERRERR.

See LuaJIT#962
@MikePall
Copy link
Member

Fixed. Thanks!

@corsix
Copy link

corsix commented Sep 4, 2023

This change (8135de2) has the unfortunate side effect of causing stack overflows to be reported long before they need to be; suppose L->stacksize is 33000 and lj_state_growstack is called with need=30, then lj_state_growstack will choose to double the stack to LJ_STACK_MAX, immediately causing L->stacksize == LJ_STACK_MAXEX and thus lj_err_msg(L, LJ_ERR_STKOV). This despite the fact that there is ample space to grow the stack by 30 slots.

MikePall pushed a commit that referenced this issue Sep 21, 2023
Reported by Peter Cawley. #962
@MikePall
Copy link
Member

I rewrote the stack overflow handling to resolve your concerns.

mkokryashkin pushed a commit to tarantool/luajit that referenced this issue Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants