Skip to content

Commit 9aa7dbe

Browse files
Mike Pallmkokryashkin
Mike Pall
authored andcommitted
Follow-up fix for stack overflow handling cleanup.
(cherry-picked from commit aa6b15c) The stack overflow error is thrown in `lj_state_growstack` only if the coroutine status is `OK`, however, stack overflow can happen on a yielded coroutine too. This patch fixes the condition for status, so now the error thrown on yielded coroutines too. Maxim Kokryashkin: * added the description and the test for the patch Part of tarantool/tarantool#9145
1 parent fd696c7 commit 9aa7dbe

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/lj_state.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void LJ_FASTCALL lj_state_growstack(lua_State *L, MSize need)
126126
if (L->stacksize > LJ_STACK_MAXEX)
127127
lj_err_throw(L, LUA_ERRERR); /* Does not invoke an error handler. */
128128
/* 1. We are _at_ the limit after the last growth. */
129-
if (!L->status) { /* 2. Throw 'stack overflow'. */
129+
if (L->status < LUA_ERRRUN) { /* 2. Throw 'stack overflow'. */
130130
L->status = LUA_ERRRUN; /* Prevent ending here again for pushed msg. */
131131
lj_err_msg(L, LJ_ERR_STKOV); /* May invoke an error handler. */
132132
}

test/tarantool-c-tests/lj-962-premature-stack-overflow.test.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ static int fill_stack(lua_State *L)
2121
return 0;
2222
}
2323

24+
static int immediate_yield(lua_State *L)
25+
{
26+
return lua_yield(L, 0);
27+
}
28+
29+
static int overflow_suspended_coro(lua_State *L)
30+
{
31+
lua_State *newL = lua_newthread(L);
32+
lua_pushcfunction(newL, immediate_yield);
33+
lua_resume(newL, 0);
34+
fill_stack(newL);
35+
return 0;
36+
}
37+
2438
static int premature_stackoverflow(void *test_state)
2539
{
2640
lua_State *L = test_state;
@@ -29,11 +43,20 @@ static int premature_stackoverflow(void *test_state)
2943
return TEST_EXIT_SUCCESS;
3044
}
3145

46+
static int stackoverflow_on_suspended_coro(void *test_state)
47+
{
48+
lua_State *L = test_state;
49+
int status = lua_cpcall(L, overflow_suspended_coro, NULL);
50+
assert_true(status == LUA_ERRRUN);
51+
return TEST_EXIT_SUCCESS;
52+
}
53+
3254
int main(void)
3355
{
3456
lua_State *L = utils_lua_init();
3557
const struct test_unit tgroup[] = {
3658
test_unit_def(premature_stackoverflow),
59+
test_unit_def(stackoverflow_on_suspended_coro),
3760
};
3861
const int test_result = test_run_group(tgroup, L);
3962
utils_lua_close(L);

0 commit comments

Comments
 (0)