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 8C6415ECD63; Tue, 5 Sep 2023 13:39:54 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 8C6415ECD63 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1693910394; bh=ZctGFB8wgyzUuTVjRX64W+ev37OUeOEsJloMMxba66c=; 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=Ag5oHpBcFmW5dJtbf6Xd2T7badq2WkzfJZw1mbTvKHhch08pwnjn6LSte6/OWQNkH O82oHN6mg5osGElascoEFh7PZEvL7UoLd251zoasWhTIIb+jV2p8piHeGQ00cdOYkv lpNecWuP4AFZpdkXhoh203rUAkC3Lhe9iZACqW8U= Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) (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 0D0085ECD63 for ; Tue, 5 Sep 2023 13:39:26 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 0D0085ECD63 Received: by mail-lf1-f44.google.com with SMTP id 2adb3069b0e04-5009969be25so3652340e87.3 for ; Tue, 05 Sep 2023 03:39:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693910365; x=1694515165; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9q5yn1KP9C1mRaLYwGVOvAEvLYjb2bBMY4FLWsvEOmI=; b=emIjMC+I4bI2FRNXcGW3cNzcsKV16D1yC5jg06/WNkstnewCWfqVlLOJ8+p61571Gg s9D/c2Flxzomn3E0GoQsm3uh/fSQkHQc01TbIR77mZy+MEN8Q5OyOUozdufw6sbk7EVV xXU+FA5P5zMFZvK/i6Gu/3HtFVpJ3n/rSOvmImDdKycIHf+7Tv2jKdvof4Kw1iz7hB+g Xn5OARHBPG/deglnx/1qhifqdNhoAHGzilq9S4b+r/h9NlQ3MU8QRWm8vKsuIHddwK5H CbTDCycLzAr4MLtrw8bYyo2EVErlWdPrrf87vSqUlz4CleN+PqVRG73RkTkSOF1Jksjo HO8g== X-Gm-Message-State: AOJu0Yzu7JcRNLYOMRHwgDH9WGFpI8SeLpeM8dAl+JvltmxGdNnmQBSX hxMGWlLgQctYyv9V4GkYzjXqn93Kn+p3dQ== X-Google-Smtp-Source: AGHT+IHbI7tQ+34eepSCJmyLiZSWXjBoLn1D3SKV3VAz8iUK2dGtp4puql1rvtRG+f+njn3J/r6KWQ== X-Received: by 2002:ac2:58dc:0:b0:500:8fcd:c3b4 with SMTP id u28-20020ac258dc000000b005008fcdc3b4mr7580283lfo.69.1693910364818; Tue, 05 Sep 2023 03:39:24 -0700 (PDT) Received: from fckxorg.mail.msk ([2a00:1148:b0ba:16:a3e8:bdc1:dbed:dbc8]) by smtp.gmail.com with ESMTPSA id x25-20020ac24899000000b004fb7388360esm2258574lfc.188.2023.09.05.03.39.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Sep 2023 03:39:24 -0700 (PDT) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, skaplun@tarantool.org, sergeyb@tarantool.org Date: Tue, 5 Sep 2023 13:39:14 +0300 Message-ID: <1adf54160bcf5f2383f875fbc5fb0b903f68c8fc.1693910172.git.m.kokryashkin@tarantool.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit 1/2] Fix frame for on-trace out-of-memory error. 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: Maxim Kokryashkin via Tarantool-patches Reply-To: Maxim Kokryashkin Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Reported by ruidong007. (cherry-picked from commit 2d8300c1944f3a62c10f0829e9b7847c5a6f0482) When an on-trace OOM error is triggered from a frame that is child in regard to `jit_base`, and `L->base` is not updated correspondingly (FUNCC, for example), it is possible to encounter an inconsistent Lua stack in the error handler. This patch adds a fixup for OOM errors on the trace that always sets the Lua stack base to `jit_base`, so the stack is now consistent. Part of tarantool/tarantool#8825 --- src/lj_err.c | 4 +++ test/tarantool-tests/CMakeLists.txt | 1 + .../lj-1004-oom-error-frame.test.lua | 36 +++++++++++++++++++ .../lj-1004-oom-error-frame/CMakeLists.txt | 1 + .../lj-1004-oom-error-frame/testoomframe.c | 17 +++++++++ 5 files changed, 59 insertions(+) create mode 100644 test/tarantool-tests/lj-1004-oom-error-frame.test.lua create mode 100644 test/tarantool-tests/lj-1004-oom-error-frame/CMakeLists.txt create mode 100644 test/tarantool-tests/lj-1004-oom-error-frame/testoomframe.c diff --git a/src/lj_err.c b/src/lj_err.c index 89c51e98..763746e6 100644 --- a/src/lj_err.c +++ b/src/lj_err.c @@ -777,6 +777,10 @@ LJ_NOINLINE void lj_err_mem(lua_State *L) { if (L->status == LUA_ERRERR+1) /* Don't touch the stack during lua_open. */ lj_vm_unwind_c(L->cframe, LUA_ERRMEM); + if (LJ_HASJIT) { + TValue *base = tvref(G(L)->jit_base); + if (base) L->base = base; + } if (curr_funcisL(L)) L->top = curr_topL(L); setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRMEM)); lj_err_throw(L, LUA_ERRMEM); diff --git a/test/tarantool-tests/CMakeLists.txt b/test/tarantool-tests/CMakeLists.txt index 6218f76a..93230677 100644 --- a/test/tarantool-tests/CMakeLists.txt +++ b/test/tarantool-tests/CMakeLists.txt @@ -66,6 +66,7 @@ add_subdirectory(lj-416-xor-before-jcc) add_subdirectory(lj-601-fix-gc-finderrfunc) add_subdirectory(lj-727-lightuserdata-itern) add_subdirectory(lj-flush-on-trace) +add_subdirectory(lj-1004-oom-error-frame) # The part of the memory profiler toolchain is located in tools # directory, jit, profiler, and bytecode toolchains are located diff --git a/test/tarantool-tests/lj-1004-oom-error-frame.test.lua b/test/tarantool-tests/lj-1004-oom-error-frame.test.lua new file mode 100644 index 00000000..b6b5a9f2 --- /dev/null +++ b/test/tarantool-tests/lj-1004-oom-error-frame.test.lua @@ -0,0 +1,36 @@ +local tap = require('tap') +local ffi = require('ffi') +local test = tap.test('lj-1004-oom-error-frame'):skipcond({ + ['Test requires JIT enabled'] = not jit.status(), + ['Test requires GC64 mode disabled'] = ffi.abi('gc64'), + ['Disabled on MacOS due to #8652'] = jit.os == 'OSX', +}) + +test:plan(2) + +local testoomframe = require('testoomframe') + +local anchor_memory = {} -- luacheck: no unused +local function eatchunks(size) + while true do + anchor_memory[ffi.new('char[?]', size)] = 1 + end +end + +pcall(eatchunks, 512 * 1024 * 1024) + +local anchor = {} +local function extra_frame(val) + table.insert(anchor, val) +end + +local function chomp() + while true do + extra_frame(testoomframe.allocate_userdata()) + end +end + +local st, err = pcall(chomp) +test:ok(st == false, 'on-trace error handled successfully') +test:like(err, 'not enough memory', 'error is OOM') +test:done(true) diff --git a/test/tarantool-tests/lj-1004-oom-error-frame/CMakeLists.txt b/test/tarantool-tests/lj-1004-oom-error-frame/CMakeLists.txt new file mode 100644 index 00000000..3bca5df8 --- /dev/null +++ b/test/tarantool-tests/lj-1004-oom-error-frame/CMakeLists.txt @@ -0,0 +1 @@ +BuildTestCLib(testoomframe testoomframe.c) diff --git a/test/tarantool-tests/lj-1004-oom-error-frame/testoomframe.c b/test/tarantool-tests/lj-1004-oom-error-frame/testoomframe.c new file mode 100644 index 00000000..a54eac63 --- /dev/null +++ b/test/tarantool-tests/lj-1004-oom-error-frame/testoomframe.c @@ -0,0 +1,17 @@ +#include +#include + +static int allocate_userdata(lua_State *L) { + lua_newuserdata(L, 1); + return 1; +} + +static const struct luaL_Reg testoomframe[] = { + {"allocate_userdata", allocate_userdata}, + {NULL, NULL} +}; + +LUA_API int luaopen_testoomframe(lua_State *L) { + luaL_register(L, "testoomframe", testoomframe); + return 1; +} -- 2.41.0