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 844F15E8D9D; Thu, 21 Sep 2023 16:15:49 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 844F15E8D9D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1695302149; bh=QG7iV833AW4BwxMJpnj98jYQkBr9HctzJzlOhRnzinU=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=maw4A6X4282wK2QBXg7zKrWEzwFq5j1HbchYJYAoP2HiKgoNvtfpZkvxe1l4fl6CC w4vzHkCSC2C3F4MjAWfc3YCjz9gFnnuP6EeRSwhzN2+jIUHVWGdFkLuc0c/+/rlp33 gyjUxZq/AxdeDk1wMktS6kRP23fnrNJK8jFHOMEo= 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 C7C715A779A for ; Thu, 21 Sep 2023 16:15:47 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C7C715A779A Received: by mail-lf1-f46.google.com with SMTP id 2adb3069b0e04-503065c4b25so1611115e87.1 for ; Thu, 21 Sep 2023 06:15:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695302147; x=1695906947; 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=nvZvP+N0kFn44WOnuXtCM4V9o4I+JmtA5VHYeuxsCGU=; b=uIoV8zLiPqWyqIdAafIK2Tb5SP8nOMXTMFz/nQZk//QWvwEzfoWO45RkmdLL/OO3KC NoyPp1i2M0DnsOXXZ0nk1ERZi6bjTqJdFEW+MjrlsbWFhY0XO5/pDDvasHaxH7xEZFZf TJT4DYYRIvHGQ7wiglA7/T9gSMerRywjEouWUgKKlUpppiLeSvKxc/LpCNyeuksWvKby 7KhG19fKyDmjz5Vr4Ljcp7EHBkAGPhTMJM9Kn6y7cZIpnSXpWyhoDq7m8UkrNU70DS6k UHRn3XcIGsVPKjfQ3E2ZfY2PlZZRf0F8SuLzGbPRRXHb0uzd/R0dZ6qbGAfqGd9M2Hlc RvEA== X-Gm-Message-State: AOJu0YzXiYK/SRP9HC+fpFS0K9VhEmu11/SOX1mHnyXgie5DbrGZkB9F BQEdyBr/R15OMfHqFJA17C5DxntUrczh8w== X-Google-Smtp-Source: AGHT+IH9rvArYAr/4zU3oIyb0f/uSdwDLVd2xZhKekvRjDd7co0G46I/Hw+FIPlk8/9CnwDrG7A7Jw== X-Received: by 2002:a05:6512:3a8e:b0:4fe:1681:9377 with SMTP id q14-20020a0565123a8e00b004fe16819377mr6010516lfu.44.1695302146336; Thu, 21 Sep 2023 06:15:46 -0700 (PDT) Received: from localhost.localdomain ([2a00:1fa0:4818:ca:bf96:559f:ab42:2c9a]) by smtp.gmail.com with ESMTPSA id o10-20020a05651238aa00b00500b19152cbsm292715lft.8.2023.09.21.06.15.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Sep 2023 06:15:45 -0700 (PDT) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, skaplun@tarantool.org, sergeyb@tarantool.org Date: Thu, 21 Sep 2023 16:15:31 +0300 Message-ID: <20230921131539.255389-1-m.kokryashkin@tarantool.org> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit] Fix snapshot PC when linking to BC_JLOOP that was a BC_RET*. 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 Arseny Vakhrushev. Fix contributed by Peter Cawley. As specified in lj_record.c:304, all loops must set `J->pc` to the next instruction. However, the chunk of logic at lj_trace.c:923 expects it to be set to `BC_JLOOP` itself if it used to be a `BC_RET`. This wrong pc results in the execution of random data that goes after BC_JLOOP in the case of restoration from the snapshot. This patch fixes that behavior by adapting the loop recording logic to this specific case. Maxim Kokryashkin: * added the description and the test for the problem Part of tarantool/tarantool#8825 --- Branch: https://github.com/tarantool/luajit/tree/fckxorg/lj-624-jloop-snapshot-pc PR: https://github.com/tarantool/tarantool/pull/9166 NB: The test for this patch triggers the assertion added in this patch, however I had no luck making a __stable__ reproducer for the issue, since it depends on what's in memory after the BC_JLOOP. It is easier to achieve a consitent failures if ASLR is disabled, but it's not suitable for the testing purposes. src/lj_record.c | 9 +++++---- src/lj_snap.c | 3 +++ .../lj-624-jloop-snapshot-pc.test.lua | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 test/tarantool-tests/lj-624-jloop-snapshot-pc.test.lua diff --git a/src/lj_record.c b/src/lj_record.c index 48a5481b..3bdc6134 100644 --- a/src/lj_record.c +++ b/src/lj_record.c @@ -570,10 +570,10 @@ static LoopEvent rec_iterl(jit_State *J, const BCIns iterins) } /* Record LOOP/JLOOP. Now, that was easy. */ -static LoopEvent rec_loop(jit_State *J, BCReg ra) +static LoopEvent rec_loop(jit_State *J, BCReg ra, int skip) { if (ra < J->maxslot) J->maxslot = ra; - J->pc++; + J->pc += skip; return LOOPEV_ENTER; } @@ -2433,7 +2433,7 @@ void lj_record_ins(jit_State *J) rec_loop_interp(J, pc, rec_iterl(J, *pc)); break; case BC_LOOP: - rec_loop_interp(J, pc, rec_loop(J, ra)); + rec_loop_interp(J, pc, rec_loop(J, ra, 1)); break; case BC_JFORL: @@ -2443,7 +2443,8 @@ void lj_record_ins(jit_State *J) rec_loop_jit(J, rc, rec_iterl(J, traceref(J, rc)->startins)); break; case BC_JLOOP: - rec_loop_jit(J, rc, rec_loop(J, ra)); + rec_loop_jit(J, rc, rec_loop(J, ra, + !bc_isret(bc_op(traceref(J, rc)->startins)))); break; case BC_IFORL: diff --git a/src/lj_snap.c b/src/lj_snap.c index 2dc281cb..b50ecfb2 100644 --- a/src/lj_snap.c +++ b/src/lj_snap.c @@ -115,6 +115,9 @@ static MSize snapshot_framelinks(jit_State *J, SnapEntry *map, uint8_t *topslot) #else MSize f = 0; map[f++] = SNAP_MKPC(J->pc); /* The current PC is always the first entry. */ + lj_assertJ(!J->pt || + (J->pc >= proto_bc(J->pt) && + J->pc < proto_bc(J->pt) + J->pt->sizebc), "bad snapshot PC"); #endif while (frame > lim) { /* Backwards traversal of all frames above base. */ if (frame_islua(frame)) { diff --git a/test/tarantool-tests/lj-624-jloop-snapshot-pc.test.lua b/test/tarantool-tests/lj-624-jloop-snapshot-pc.test.lua new file mode 100644 index 00000000..ada290ff --- /dev/null +++ b/test/tarantool-tests/lj-624-jloop-snapshot-pc.test.lua @@ -0,0 +1,16 @@ +local tap = require('tap') +local test = tap.test('lj-624-jloop-snapshot-pc'):skipcond({ + ['Test requires JIT enabled'] = not jit.status(), +}) + +test:plan(1) + +jit.opt.start('hotloop=1', 'hotexit=1') +local function fib(n) + return n < 2 and n or fib(n - 1) + fib(n - 2) +end + +fib(5) + +test:ok(true, 'snapshot pc is correct') +test:done(true) -- 2.42.0