From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 934A66F805D; Wed, 22 Nov 2023 17:36:42 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 934A66F805D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1700663802; bh=pvDPL5FxAvm0NmifCKcVIYwVppWJ3C86twZ7g/krBDU=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=uvS5FzKMR6fUXWCqnxLCLGYdIAfvgnhjbbbKzb3HmCC7lFFS+1/iWgXEIPQxTFFUv ZEx23Tu/Hhh/vLqpSLH9taJzmMmGKZkb7yPF5YaItpZxX9f+zBg8Eq6xE9Q+wlrwte rBGGdZgUK/Vg+fDpNi4+nqqIJNKWujXjp03v3OtM= Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id D597D6F805B for ; Wed, 22 Nov 2023 17:35:47 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D597D6F805B Received: by mail-lf1-f46.google.com with SMTP id 2adb3069b0e04-5094cb3a036so9761156e87.2 for ; Wed, 22 Nov 2023 06:35:47 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700663747; x=1701268547; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aQ2wsVc4JiisHAQ4YkHL3KM75x1KmIAGNNthAGSvDjM=; b=QpI5xb0a0EkmrUsOq+/aE41zZYEerC38YNkgvkXHiJvq8PhQMfMsKPCxpbuUAz3Ls+ OAgYroIki9XHAev4XviLJXis7g2GABA24I6X/RimTopVnTsYMUdd8fVluEB5ORpcaysf 4DCyZrWIMvSCQSen0WqLd2kHHgZUgW+gUE0sOE9HxbSHW7GAOh5XYw9e3o+HAqG1wERx m4gpqCVyNutnfkrT0zf9FNjGqoFj60DSdYJr+HNvCTf9QzD+mj67Qz4ITZXKGxGtvzgQ vQ2td3n7CxyWTI1vQTfGyrGnjdtaBWG/UM3mQkRmzTdnqAxMeKgF1hu0c4iWetG/Fm99 aOAQ== X-Gm-Message-State: AOJu0Yz3p2SUa1yztVKZM9UFSEBnq70VR3Q2Ax9IQKB49PuXM040J2ZN w9NJhCjsmEpcTA/JJB15qUiGcU38kqZgLVbc X-Google-Smtp-Source: AGHT+IELyeTmt8PerQOC2NWxr5t2MWA2uFj6lD30NbnBXjwaspWJNvOcZZodlQEQs9xLSwpMfbplDQ== X-Received: by 2002:ac2:5d70:0:b0:503:3808:389a with SMTP id h16-20020ac25d70000000b005033808389amr1727451lft.11.1700663746521; Wed, 22 Nov 2023 06:35:46 -0800 (PST) Received: from localhost.localdomain ([185.205.79.42]) by smtp.gmail.com with ESMTPSA id x15-20020a19e00f000000b005079a3c872esm1894570lfg.240.2023.11.22.06.35.45 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 22 Nov 2023 06:35:46 -0800 (PST) To: tarantool-patches@dev.tarantool.org, sergeyb@tarantool.org, skaplun@tarantool.org, m.kokryashkin@tarantool.org Date: Wed, 22 Nov 2023 17:35:33 +0300 Message-Id: <20231122143534.11330-3-max.kokryashkin@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20231122143534.11330-1-max.kokryashkin@gmail.com> References: <20231122143534.11330-1-max.kokryashkin@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit v3 2/3] Cleanup stack overflow handling. X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Maksim Kokryashkin via Tarantool-patches Reply-To: Maksim Kokryashkin Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Mike Pall Reported by Peter Cawley. (cherry-picked from commit d2f6c55b05c716e5dbb479b7e684abaee7cf6e12) After the previous patch, it is possible to trigger the `stack overflow` error prematurely. Consider the following situation: there are already 33000 slots allocated on a Lua stack, and then there are 30 additional slots needed. In this case, the actual allocated amount would be twice the already allocated size, shrunk to the `LJ_STACK_MAXEX` size, which would lead to the stack overflow error, despite the fact there is plenty of unused space. This patch completely reworks the logic of error handling during stack growth to address the issue. Another important thing to notice is that the `LJ_ERR_STKOV` is thrown only if the `L->status` is `LUA_OK` and that status is set to `LUA_ERRRUN` just before throwing the error. The status is set to `LUA_ERRRUN` to avoid the second stack overflow during the `err_msgv` execution. Maxim Kokryashkin: * added the description and the test for the problem Part of tarantool/tarantool#9145 --- src/lj_state.c | 15 +++-- .../lj-962-premature-stack-overflow.test.c | 63 +++++++++++++++++++ 2 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 test/tarantool-c-tests/lj-962-premature-stack-overflow.test.c diff --git a/src/lj_state.c b/src/lj_state.c index 76153bad..d8a5134c 100644 --- a/src/lj_state.c +++ b/src/lj_state.c @@ -121,8 +121,17 @@ void lj_state_shrinkstack(lua_State *L, MSize used) void LJ_FASTCALL lj_state_growstack(lua_State *L, MSize need) { MSize n; - if (L->stacksize > LJ_STACK_MAXEX) /* Overflow while handling overflow? */ - lj_err_throw(L, LUA_ERRERR); + if (L->stacksize >= LJ_STACK_MAXEX) { + /* 4. Throw 'error in error handling' when we are _over_ the limit. */ + if (L->stacksize > LJ_STACK_MAXEX) + lj_err_throw(L, LUA_ERRERR); /* Does not invoke an error handler. */ + /* 1. We are _at_ the limit after the last growth. */ + if (!L->status) { /* 2. Throw 'stack overflow'. */ + L->status = LUA_ERRRUN; /* Prevent ending here again for pushed msg. */ + lj_err_msg(L, LJ_ERR_STKOV); /* May invoke an error handler. */ + } + /* 3. Add space (over the limit) for pushed message and error handler. */ + } n = L->stacksize + need; if (n > LJ_STACK_MAX) { n += 2*LUA_MINSTACK; @@ -132,8 +141,6 @@ void LJ_FASTCALL lj_state_growstack(lua_State *L, MSize need) n = LJ_STACK_MAX; } resizestack(L, n); - if (L->stacksize >= LJ_STACK_MAXEX) - lj_err_msg(L, LJ_ERR_STKOV); } void LJ_FASTCALL lj_state_growstack1(lua_State *L) diff --git a/test/tarantool-c-tests/lj-962-premature-stack-overflow.test.c b/test/tarantool-c-tests/lj-962-premature-stack-overflow.test.c new file mode 100644 index 00000000..12cb9004 --- /dev/null +++ b/test/tarantool-c-tests/lj-962-premature-stack-overflow.test.c @@ -0,0 +1,63 @@ +#include "lua.h" +#include "lauxlib.h" + +#include "test.h" +#include "utils.h" + +/* + * XXX: The "lj_obj.h" header is included to calculate the + * number of stack slots used from the bottom of the stack. + */ +#include "lj_obj.h" + +static int cur_slots = -1; + +static int fill_stack(lua_State *L) +{ + cur_slots = L->base - tvref(L->stack); + + while(lua_gettop(L) < LUAI_MAXSTACK) { + cur_slots += 1; + lua_pushinteger(L, 42); + } + + return 0; +} + +static int premature_stackoverflow(void *test_state) +{ + lua_State *L = test_state; + lua_cpcall(L, fill_stack, NULL); + assert_true(cur_slots == LUAI_MAXSTACK - 1); + return TEST_EXIT_SUCCESS; +} + +/* + * XXX: This test should fail neither before the patch + * nor after it. + */ +static int stackoverflow_during_stackoverflow(void *test_state) +{ + lua_State *L = test_state; + /* + * XXX: `fill_stack` acts here as its own error handler, + * causing the second stack overflow. + */ + lua_pushcfunction(L, fill_stack); + lua_pushcfunction(L, fill_stack); + int status = lua_pcall(L, 0, 0, -2); + assert_true(status == LUA_ERRERR); + return TEST_EXIT_SUCCESS; +} + +int main(void) +{ + lua_State *L = utils_lua_init(); + const struct test_unit tgroup[] = { + test_unit_def(premature_stackoverflow), + test_unit_def(stackoverflow_during_stackoverflow), + }; + const int test_result = test_run_group(tgroup, L); + utils_lua_close(L); + return test_result; +} -- 2.39.3 (Apple Git-145)