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 15B296F873; Thu, 27 Jan 2022 14:55:51 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 15B296F873 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1643284551; bh=AeR6VlXdY8Jo729X2VmlvqA3GCtNKyvwajuCktf8llQ=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=K/71826QSs29wDGK0e/4nxuVSfwA5wwkAHmLgmyd3YYggvEH6ldld+LvJoQ/1J9R2 dIk2dXt1KIONcEB7UrhTyTDweA81N20YgLaXIDmJkNWq6lCFoffbjRkS9Eu4UR3l+p ac6aUENCpkMMQ7NOtInA0777XDGDD17O7tPB9L6o= Received: from smtp35.i.mail.ru (smtp35.i.mail.ru [94.100.177.95]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id C05E06F873 for ; Thu, 27 Jan 2022 14:55:49 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C05E06F873 Received: by smtp35.i.mail.ru with esmtpa (envelope-from ) id 1nD3NU-0007YN-Vq; Thu, 27 Jan 2022 14:55:49 +0300 To: Sergey Ostanevich , Igor Munkin Date: Thu, 27 Jan 2022 14:53:46 +0300 Message-Id: <20220127115346.22800-1-skaplun@tarantool.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9AA78FDF62ECAE61FF13F204EA2BDEB7EE91D6674DF21EB76182A05F5380850404A2D3555BBB91A49B6C831D8FA46D2B645BD993A2C5CECD850DB700B980AA85A X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE73B44982FA5E78411EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637C83F54BD885518138638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8BABDCE40B57F9E744A421C91F1E0475F117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC3A703B70628EAD7BA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352026055571C92BF10FF6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA7E827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8BAEB924C2B054B06E75ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C86C5B328D8C3BEB6A0451328C3FCF10BC8E48F72998882A69C2B6934AE262D3EE7EAB7254005DCED6BB2BF071322A0961E0A4E2319210D9B64D260DF9561598F01A9E91200F654B0F19FF8B1531DFD348E8E86DC7131B365E7726E8460B7C23C X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3419891600CCEF4607FED83E51E8179F36AF4EDC950FCB6CE4E515E8014B30A90D032023956067EDED1D7E09C32AA3244C83377DD98702D93459AA733C79FF72F3435BF7150578642F927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojUxqqD3NEEG3JNFm8G+friQ== X-Mailru-Sender: 3B9A0136629DC91206CBC582EFEF4CB496657DFC614C60DEB869E1393A3AFD45B2026C37A440FDC0F2400F607609286E924004A7DEC283833C7120B22964430C52B393F8C72A41A84198E0F3ECE9B5443453F38A29522196 X-Mras: Ok Subject: [Tarantool-patches] [PATCH luajit] Fix bytecode dump unpatching. 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: Sergey Kaplun via Tarantool-patches Reply-To: Sergey Kaplun Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Mike Pall Reported by Christopher Oliver. (cherry picked from commit 20ac817a747cf8cab044ae81b09c08d23e34342b) When a compiled function with up-recursion RET bytecodes are patched to JLOOP bytecode. During dump of those bytecodes they should be unpatched to the original one. It is done by restoring the opcode by subtraction the diff between JLOOP and ILOOP bytecodes. That gives the LOOP bytecodes instead RET as expected. This patch fixes the bytecode unpatching via copy the original start instruction, that was patched. Sergey Kaplun: * added the description and the test for the problem Part of tarantool/tarantool#6548 --- Branch: https://github.com/tarantool/luajit/tree/skaplun/gh-noticket-wrong-bc-ret Tarantool branch: https://github.com/tarantool/tarantool/tree/skaplun/gh-noticket-wrong-bc-ret-full-ci Related issue: https://github.com/tarantool/tarantool/issues/6548 src/lj_bcwrite.c | 5 +---- .../bc-jit-unpatching.test.lua | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 test/tarantool-tests/bc-jit-unpatching.test.lua diff --git a/src/lj_bcwrite.c b/src/lj_bcwrite.c index 5e05caea..a86d6d00 100644 --- a/src/lj_bcwrite.c +++ b/src/lj_bcwrite.c @@ -219,10 +219,7 @@ static char *bcwrite_bytecode(BCWriteCtx *ctx, char *p, GCproto *pt) q[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_IFORL+BC_FORL); } else if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) { BCReg rd = q[LJ_ENDIAN_SELECT(2, 1)] + (q[LJ_ENDIAN_SELECT(3, 0)] << 8); - BCIns ins = traceref(J, rd)->startins; - q[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_JFORL+BC_FORL); - q[LJ_ENDIAN_SELECT(2, 1)] = bc_c(ins); - q[LJ_ENDIAN_SELECT(3, 0)] = bc_b(ins); + memcpy(q, &traceref(J, rd)->startins, 4); } } } diff --git a/test/tarantool-tests/bc-jit-unpatching.test.lua b/test/tarantool-tests/bc-jit-unpatching.test.lua new file mode 100644 index 00000000..9f9cb390 --- /dev/null +++ b/test/tarantool-tests/bc-jit-unpatching.test.lua @@ -0,0 +1,22 @@ +local tap = require('tap') +local utils = require('utils') + +local test = tap.test('bc-jit-unpatching') +test:plan(1) + +-- Function with up-recursion. +local function f(n) + return n < 2 and n or f(n - 1) + f(n - 2) +end + +local ret1bc = 'RET1%s*1%s*2' +-- Check that this bytecode still persists. +assert(utils.hasbc(loadstring(string.dump(f)), ret1bc)) + +-- Compile function to get JLOOP bytecode in recursion. +f(10) + +test:ok(utils.hasbc(loadstring(string.dump(f)), ret1bc), + 'bytecode unpached correctly') + +os.exit(test:check() and 0 or 1) -- 2.34.1