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 AF78E733EC2; Thu, 14 Dec 2023 14:45:08 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org AF78E733EC2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1702554308; bh=VHFoNR8RAiZxxGG+7c4MH/HT7CKcZAkjGbKeexu4TzE=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=IUEiIY9mzx3Otz3yr/iCWaMG51UyvOB9UUf1T6zXw66M4ZlLoliUJTIaU7XwnFoeH LVNa49iGhbxWW3pM1bgQlLVGOCEy/9qVEbYro8rce1SVUZy7dekb7BCCYQlZILuFYO j1s4N73mIuAagiH0dt8T2PMh6Llz0bWlihM5CuYE= Received: from mail-lj1-f171.google.com (mail-lj1-f171.google.com [209.85.208.171]) (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 B2C2E528499 for ; Thu, 14 Dec 2023 14:45:06 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org B2C2E528499 Received: by mail-lj1-f171.google.com with SMTP id 38308e7fff4ca-2c9f84533beso91814901fa.1 for ; Thu, 14 Dec 2023 03:45:06 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702554306; x=1703159106; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=e2bO3sANpi82Ni7fA6ysIWrRha/PTqfG3k1RAZgRs/E=; b=bjXWw6PPVEeHV8ra282uV9DapiZMS4nRloEvq5DzjHqCQadq6EnxrPjnDxdpcJxmoY 4YLO7aj0Id4va9sjEQcpfg2UatQra0wS1iNBqLaVgs42QlncARN7qhChUQncSsdsAZq+ bogsUpBtvBnyej7mm124VJBshI362MfWkbDjCmcGyfCsHHVD7DeqtosTgLVvNPG5MjWl Tt3a3K2hFPB6lDxtQVjY64QgvWBWfduUJm+wljipNpGr8ktABkw19a9jrk6OFMKbo/o/ qKkYytv8qh52ESoOKJ2zcIcst1+7qMbegBWhsv4NNFCBOOFW8a7RR6sroiPaRAxN8c7G yliA== X-Gm-Message-State: AOJu0Yw7tvSDRq3lcIHdJU1tFUmxx/ghgAVU5fK7jhli0+XWzfdM1Y0w tQpfmQvPxuf5Do+s3p4ifmt8ND7ohS4gCP7q X-Google-Smtp-Source: AGHT+IFe0NUVR3iIYFKwPmms4X6qd4dQ+/Bt410MCE/mPQhNvavzGQlmoXwK4rC1zxDqcCsxaHZ7RQ== X-Received: by 2002:a2e:a595:0:b0:2cb:2b4d:2574 with SMTP id m21-20020a2ea595000000b002cb2b4d2574mr4129176ljp.21.1702554305271; Thu, 14 Dec 2023 03:45:05 -0800 (PST) Received: from fckxorg.mail.msk ([2a00:1148:b0ba:16:a3e8:bdc1:dbed:dbc8]) by smtp.gmail.com with ESMTPSA id 13-20020a2e154d000000b002ca03d01360sm2229070ljv.23.2023.12.14.03.45.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 03:45:04 -0800 (PST) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, skaplun@tarantool.org, sergeyb@tarantool.org Date: Thu, 14 Dec 2023 14:44:49 +0300 Message-ID: <20231214114458.17929-1-m.kokryashkin@tarantool.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit] Avoid assertion in case of stack overflow from stitched trace. 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" From: Mike Pall Reported by Sergey Bronnikov. Fixed by Sergey Kaplun. (cherry-picked from commit 1d75cd4d7be638babe6d4e47bf73ea05fc65d81c) When we exit from a stitched trace due to the Lua stack overflow error, the Lua and JIT stacks are not synchronized, and they won't be as long as the mentioned error is raised. Because of that, we get the incorrect bytecode instruction in `debug_framepc`. This patch fixes this behavior, so the `debug_framepc` now returns `NO_BCPOS` for this case. Maxim Kokryashkin: * added the description and the test for the problem Part of tarantool/tarantool#9145 --- Branch: https://github.com/tarantool/luajit/tree/fckxorg/lj-913-avoid-assertion-stkov-from-stitched-trace PR: https://github.com/tarantool/tarantool/pull/9484 Issues: https://github.com/tarantool/tarantool/issues/9145 https://github.com/LuaJIT/LuaJIT/issues/913 src/lj_debug.c | 9 +++++--- ...-913-stackoverflow-stitched-trace.test.lua | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 test/tarantool-tests/lj-913-stackoverflow-stitched-trace.test.lua diff --git a/src/lj_debug.c b/src/lj_debug.c index 46c442c6..107f464c 100644 --- a/src/lj_debug.c +++ b/src/lj_debug.c @@ -101,9 +101,12 @@ static BCPos debug_framepc(lua_State *L, GCfunc *fn, cTValue *nextframe) pos = proto_bcpos(pt, ins) - 1; #if LJ_HASJIT if (pos > pt->sizebc) { /* Undo the effects of lj_trace_exit for JLOOP. */ - GCtrace *T = (GCtrace *)((char *)(ins-1) - offsetof(GCtrace, startins)); - lj_assertL(bc_isret(bc_op(ins[-1])), "return bytecode expected"); - pos = proto_bcpos(pt, mref(T->startpc, const BCIns)); + if (bc_isret(bc_op(ins[-1]))) { + GCtrace *T = (GCtrace *)((char *)(ins-1) - offsetof(GCtrace, startins)); + pos = proto_bcpos(pt, mref(T->startpc, const BCIns)); + } else { + pos = NO_BCPOS; /* Punt in case of stack overflow for stitched trace. */ + } } #endif return pos; diff --git a/test/tarantool-tests/lj-913-stackoverflow-stitched-trace.test.lua b/test/tarantool-tests/lj-913-stackoverflow-stitched-trace.test.lua new file mode 100644 index 00000000..3c12f0d9 --- /dev/null +++ b/test/tarantool-tests/lj-913-stackoverflow-stitched-trace.test.lua @@ -0,0 +1,23 @@ +local tap = require('tap') +-- Test to demonstrate the incorrect LuaJIT behavior when exiting +-- from a snapshot for stitched trace. +local test = tap.test('lj-913-stackoverflow-stitched-trace'):skipcond({ + ['Test requires JIT enabled'] = not jit.status(), +}) + +test:plan(3) + +-- Recursion to cause stack overflow. +local function callee() + -- `math.fmod()` is NYI, so trace will be stitched here. + local _ = math.fmod(42, 42) + callee() +end + +local st, err = pcall(callee) + +test:ok(true, 'assertion is not triggered') +test:ok(not st, 'error happened') +test:like(err, 'stack overflow', 'stack overflow happened') + +test:done(true) -- 2.43.0