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 127283A4690; Tue, 1 Aug 2023 20:55:07 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 127283A4690 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1690912507; bh=77i9+o9fK+8mLd3cGOK024YJN/klux1jm6jZyX5uTv4=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=NZDng5fO6CQTQmyVH1VE2Te8UH3C8SWwral5bJHKIZmJ0GkEF2qLvHcaLYb+198Y2 DN3m3LJ2YivNZ1wNGaGUwzKfnAwouB34Q1dKGoLqJvrkEYUK6wzpPZKOG9MPNtVkrV XwMGIhCe9ZapkBs7m6eE1Bq+w9uOGoglQb8Wr0gs= Received: from mail-lf1-f54.google.com (mail-lf1-f54.google.com [209.85.167.54]) (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 C46E43A4690 for ; Tue, 1 Aug 2023 20:55:05 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C46E43A4690 Received: by mail-lf1-f54.google.com with SMTP id 2adb3069b0e04-4fe0eb0ca75so9489480e87.2 for ; Tue, 01 Aug 2023 10:55:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690912505; x=1691517305; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=j/IolC767rAPx50xv/AN/9oXkAr8+B4Z618cMqI2tNc=; b=RT7pF0J9dIho4qQofdKlkH0YVg94cp/acUl4pEe+wuEDr8KufGcYiJGzTIDGmvboJ1 PAbrfoHtuhaTzEuiqJFbOTFmLhj6xdwNBRVY1TT9vKjdGRGfs3qnoGiq8xdcGB/LGe31 rqmXA6dXAFCjvw4/2mR6/QhYJpEMzVF7CWG+JzKKf3Jzz3FrVM70yh7m7oQ+/7GrBHYR oasuqc7vMEg3DGyrAGMO9S/k3l5vuhPEGHN8Uo0RXJJuGF5JsegcQNasvYFHB9t8/Zpe SlUV4gdEeodnsKulG8EoEDCzkZE4y9/tsKi/kGdt4uR7j0ldPPjnB49HOYiiduKUVTsr RXJA== X-Gm-Message-State: ABy/qLbEj1WOiQ6eTt9ap7Cm8V3xyyijBUcjkXDZeqrUw0SrD9UcQ3cY j79zPzJcxieHa79T3BGSS7DcBNsnVpahgA== X-Google-Smtp-Source: APBJJlHbYANV0x/8N9T62HVVHm2z0pvtd0dlhLQdt2UFg3/nWH/Ha5GYCESmyo9hjSl5+nj7Z8RRqg== X-Received: by 2002:a05:6512:2826:b0:4fb:7675:1ff9 with SMTP id cf38-20020a056512282600b004fb76751ff9mr2851201lfb.9.1690912504195; Tue, 01 Aug 2023 10:55:04 -0700 (PDT) Received: from localhost.localdomain (95-24-1-220.broadband.corbina.ru. [95.24.1.220]) by smtp.gmail.com with ESMTPSA id f6-20020a19ae06000000b004fe1efe3e38sm2239445lfc.225.2023.08.01.10.55.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Aug 2023 10:55:03 -0700 (PDT) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, skaplun@tarantool.org, sergeyb@tarantool.org Date: Tue, 1 Aug 2023 20:55:00 +0300 Message-ID: <20230801175500.3164466-1-m.kokryashkin@tarantool.org> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit v2] 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 --- Changes in v2: - Fixed comments as per review by Sergey Kaplun - Tried to optimize execution time with sizing adjustments, faster executions are unstable. PR: https://github.com/tarantool/tarantool/pull/8909 Branch: https://github.com/tarantool/luajit/tree/fckxorg/lj-1004-oom-error-frame src/lj_err.c | 4 +++ test/tarantool-tests/CMakeLists.txt | 1 + .../lj-1004-oom-error-frame.test.lua | 34 +++++++++++++++++++ .../lj-1004-oom-error-frame/CMakeLists.txt | 1 + .../lj-1004-oom-error-frame/testoomframe.c | 17 ++++++++++ 5 files changed, 57 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 9903d273..09729791 100644 --- a/src/lj_err.c +++ b/src/lj_err.c @@ -802,6 +802,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..82af5fc8 --- /dev/null +++ b/test/tarantool-tests/lj-1004-oom-error-frame.test.lua @@ -0,0 +1,34 @@ +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'), +}) + +test:plan(1) + +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, _ = pcall(chomp) +test:ok(st == false, 'on-trace error handled successfully') +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