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 8146C6F153; Fri, 9 Sep 2022 15:15:38 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 8146C6F153 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1662725738; bh=pMACdxo00xvqOuzEu5/P8MxecPP6Rvf7aQfzmRsOb1U=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=tBkCSkl0fYgm0mGtdfkRq/vuEphoWSsIJzwYznY307NDTESviYI2zX2pEhmAEiyVs 17qYOQi8y7GvDI/v77NizXWFU/Limm7h4mPgrzL5shljUOH7KF/OUesOgS1AqCAm/N hVLnS5Voj91h/gBivXLvsmuH6VV8vHXEw4m3lHiE= Received: from mail-lf1-f52.google.com (mail-lf1-f52.google.com [209.85.167.52]) (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 3AA756F153 for ; Fri, 9 Sep 2022 15:15:36 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 3AA756F153 Received: by mail-lf1-f52.google.com with SMTP id k10so2358963lfm.4 for ; Fri, 09 Sep 2022 05:15:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date; bh=pYcM85NHGbjwfynz5267rVp/qp6AsBntp6HMSFeGf0Q=; b=O4vz1LIUuhQm1vDfP+0qzOC/IwL14/wA7UPd1/6sjs1/bLNJyU7YA1PYtVSFsrRcV7 6QouddHlMEVmemcRdGIb6SOMRslhNUEqfALsR+BoxEiC6S9kbfdyhz1yX3/SYMkO8K1e zQGeSD3Vyrbb3Cscnz0narWWYIGFXmqi6KBGy1ndubfCRyZQkOc+ys76yMfo8sZGKWtE nzAlLLznJ5TfsWY6RkYk0oaPztivYLSXN8Oxoa60gv4pr5T3OJGd1VkE6AebMO+2DPWc +FuyHj6fzuNmjEkhPtZMR9Y0btypXCUs/wwC3+Rr7yi0QrsEFfFS12z3KktwhbgtvEFd u8nA== X-Gm-Message-State: ACgBeo28vJtnW2/a8GAZFkFDZzGjjU/pGkUi6EpJ/sNdvc+JB98IDZA3 C7JP0/GO8tp+ISudRPwlqY9tk1hjWjoebQ== X-Google-Smtp-Source: AA6agR6CJd5aES+4QohFGh89Ph8C8o8OXqGRJK9sYZLc8/qF3/hVXxKxOXyniT2BTQoPHNnJULF4fA== X-Received: by 2002:a19:5f59:0:b0:497:a72e:e0a1 with SMTP id a25-20020a195f59000000b00497a72ee0a1mr3880245lfj.259.1662725734969; Fri, 09 Sep 2022 05:15:34 -0700 (PDT) Received: from localhost.localdomain ([93.175.28.39]) by smtp.gmail.com with ESMTPSA id i21-20020ac25235000000b00498fc3d4cfdsm57519lfl.189.2022.09.09.05.15.34 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 09 Sep 2022 05:15:34 -0700 (PDT) To: tarantool-patches@dev.tarantool.org, imun@tarantool.org, skaplun@tarantool.org Date: Fri, 9 Sep 2022 15:15:28 +0300 Message-Id: <20220909121531.87545-1-max.kokryashkin@gmail.com> X-Mailer: git-send-email 2.32.1 (Apple Git-133) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit] Fix narrowing of unary minus. 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: Maksim Kokryashkin via Tarantool-patches Reply-To: Maksim Kokryashkin Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Mike Pall With DUALNUM mode disabled, unary minus narrowing can produce inconsistent results on zeros, returning both -0 and 0 on the same chunk of code. This patch fixes the inconsistency by adding checks for zeros. Part of tarantool/tarantool#7230 Resolves tarantool/tarantool#6976 --- Issue: https://github.com/tarantool/tarantool/issues/6976 Branch: https://github.com/tarantool/luajit/tree/fckxorg/gh-6976-narrowing-of-unary-minus PR: https://github.com/tarantool/tarantool/pull/7662 src/lj_opt_narrow.c | 9 +++-- .../gh-6976-narrowing-of-unary-minus.test.lua | 34 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 test/tarantool-tests/gh-6976-narrowing-of-unary-minus.test.lua diff --git a/src/lj_opt_narrow.c b/src/lj_opt_narrow.c index cd96ca4b..bb61f97b 100644 --- a/src/lj_opt_narrow.c +++ b/src/lj_opt_narrow.c @@ -551,8 +551,13 @@ TRef lj_opt_narrow_unm(jit_State *J, TRef rc, TValue *vc) { rc = conv_str_tonum(J, rc, vc); if (tref_isinteger(rc)) { - if ((uint32_t)numberVint(vc) != 0x80000000u) - return emitir(IRTGI(IR_SUBOV), lj_ir_kint(J, 0), rc); + uint32_t k = (uint32_t)numberVint(vc); + if ((LJ_DUALNUM || k != 0) && k != 0x80000000u) { + TRef zero = lj_ir_kint(J, 0); + if (!LJ_DUALNUM) + emitir(IRTGI(IR_NE), rc, zero); + return emitir(IRTGI(IR_SUBOV), zero, rc); + } rc = emitir(IRTN(IR_CONV), rc, IRCONV_NUM_INT); } return emitir(IRTN(IR_NEG), rc, lj_ir_ksimd(J, LJ_KSIMD_NEG)); diff --git a/test/tarantool-tests/gh-6976-narrowing-of-unary-minus.test.lua b/test/tarantool-tests/gh-6976-narrowing-of-unary-minus.test.lua new file mode 100644 index 00000000..caae8c34 --- /dev/null +++ b/test/tarantool-tests/gh-6976-narrowing-of-unary-minus.test.lua @@ -0,0 +1,34 @@ +require("utils").skipcond( + jit.arch ~= "x86" and jit.arch ~= "x64", + "DUALNUM mode must be disabled" +) + +local tap = require("tap") +local test = tap.test("gh-6976-narrowing-of-unary-minus") +test:plan(1) + +jit.off() +jit.flush() +jit.opt.start('hotloop=1') +jit.on() + +local has_error = false + +-- The first two iterations are needed to write trace in a way +-- where `a` is treated like a non-zero integer value. After the +-- first two iterations 'a' becomes a zero integer, but it is still +-- processed by the same trace. Finally, roughly ten loop +-- iterations later, a new trace is formed, where `a` is treated +-- like a number. +for i=1,20 do + local a = 1 + if i > 2 then + a = 0 + end + a = -a + has_error = has_error or tostring(a):match("%-%d") == nil +end + +test:ok(has_error == false, "inconsistent unary minus result") +os.exit(test:check() and 0 or 1) + -- 2.32.1 (Apple Git-133)