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 7AD3A522D64; Wed, 12 Jul 2023 12:52:20 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 7AD3A522D64 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1689155540; bh=8ULQxBl51HjGDNZaqJlYQM6y7fQKR/3W9zuCBxw5AsQ=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=eYuYVBhQqQUhlblBybKBTvfWjXuTfvfZOrdXfpJU2br50k/P6HgB3G/NdADKsv9iA RmYMP6nY3P080R/9yyYkvNDULJIwJkFvaWvUhx/aoN3CS7OwWG/Ww8J2NSODQoYVzb V9g10cDgH59UpASLKu8oO55KQ+zP04EtdGWnyGgM= Received: from mail-lj1-f178.google.com (mail-lj1-f178.google.com [209.85.208.178]) (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 CDCC04F18AF for ; Wed, 12 Jul 2023 12:52:18 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org CDCC04F18AF Received: by mail-lj1-f178.google.com with SMTP id 38308e7fff4ca-2b702319893so108282161fa.3 for ; Wed, 12 Jul 2023 02:52:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689155538; x=1691747538; 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=NTxhLnSNL1IcK5ZTCuBhwgT0GYJUT1sB/01KKCrNYPA=; b=G1Y53N5yI93Jc979YRxfg3YGqhc8CJ8lC6Xjqd/RaXhOqtQERtGKjwFV7PkDw4oDe+ pYT8hvdUyOy4pamnSCrEOOYWFVS0xyw2s3TrZ/mzMmKyJGBlg2CF2roSA1E7dsTyOLGd nVPLZDJKzRdBjNqOyeQWz6ZKtcmA5kqkEJYqkDhSI610G38FWl5CZyNYvYw2cibTf+eE dE/1EyI3rvUajFOe3LN5Op+34836kV+bFIySxNuZ5Fqt6aLBk3ynh89mjjMbJ8NjG2bz CCW20D3/4kr83GRb5hDxUx8EcizRMRz9nT9yYm6/YpIKGAdGIHm5XiD761E+SoTPPFJ9 CZYg== X-Gm-Message-State: ABy/qLb10hg/Z8H6Pgyrvv6251Nqmi9yPJ2+M0S0qzTxOU3bC00HQhxY 5qEuJN894S0uVGYfuBLk6k3acAU5XssdJM3K X-Google-Smtp-Source: APBJJlHZBiViulEIMOEzj3s7jsYbueIT2H8aVCrBErM4zLcRD5bf1vFVIMYv7MxK+0FT7ER5Cj8nCA== X-Received: by 2002:a2e:8688:0:b0:2b6:c0c5:1085 with SMTP id l8-20020a2e8688000000b002b6c0c51085mr14464915lji.19.1689155537512; Wed, 12 Jul 2023 02:52:17 -0700 (PDT) Received: from localhost.localdomain ([185.205.79.48]) by smtp.gmail.com with ESMTPSA id e16-20020a2e9310000000b002b6cb40e9aasm847536ljh.103.2023.07.12.02.52.16 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 12 Jul 2023 02:52:16 -0700 (PDT) To: tarantool-patches@dev.tarantool.org, sergeyb@tarantool.org, skaplun@tarantool.org, m.kokryashkin@tarantool.org Date: Wed, 12 Jul 2023 12:52:10 +0300 Message-Id: <20230712095212.20480-1-max.kokryashkin@gmail.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH] Mark CONV as non-weak, to prevent elimination of its side-effect. 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 An unused guarded CONV int.num cannot be omitted in general. (cherry-picked from commit 881d02d3117838acaf4fb844332c8e33cc95c8c5) In some cases, an unused `CONV` omission may lead to a guard absence, resulting in invalid control flow branching and undefined behavior. For a comprehensive example of the described situation, please refer to the comment in `test/tarantool-tests/mark-conv-non-weak.test.lua`. Maxim Kokryashkin: * added the description and the test for the problem Part of tarantool/tarantool#8825 --- src/lj_ir.h | 2 +- .../mark-conv-non-weak.test.lua | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 test/tarantool-tests/mark-conv-non-weak.test.lua diff --git a/src/lj_ir.h b/src/lj_ir.h index e8bca275..bf9b9292 100644 --- a/src/lj_ir.h +++ b/src/lj_ir.h @@ -132,7 +132,7 @@ _(XBAR, S , ___, ___) \ \ /* Type conversions. */ \ - _(CONV, NW, ref, lit) \ + _(CONV, N , ref, lit) \ _(TOBIT, N , ref, ref) \ _(TOSTR, N , ref, lit) \ _(STRTO, N , ref, ___) \ diff --git a/test/tarantool-tests/mark-conv-non-weak.test.lua b/test/tarantool-tests/mark-conv-non-weak.test.lua new file mode 100644 index 00000000..aad39058 --- /dev/null +++ b/test/tarantool-tests/mark-conv-non-weak.test.lua @@ -0,0 +1,58 @@ +local tap = require('tap') +local test = tap.test('mark-conv-non-weak'):skipcond({ + ['Test requires JIT enabled'] = not jit.status(), +}) + +test:plan(1) + +local data = {0.1, 0, 0.1, 0, 0 / 0} +local sum = 0 + +jit.opt.start('hotloop=1', 'hotexit=1') + +-- When the last trace is recorded, the traced bytecode +-- is the following before the patch: +-- ---- TRACE 4 start 2/3 test.lua:6 +-- 0018 ADDVV 1 1 6 +-- 0019 ITERC 5 3 3 +-- 0000 . FUNCC ; ipairs_aux +-- 0020 JITERL 5 1 +-- 0021 GGET 2 7 ; "assert" +-- 0022 ISEQV 1 1 +-- 0023 JMP 4 => 0026 +-- 0024 KPRI 4 1 +-- 0025 JMP 5 => 0027 +-- 0027 CALL 2 1 2 +-- 0000 . FUNCC ; assert +-- +-- And the following after the patch: +-- ---- TRACE 4 start 2/2 test.lua:5 +-- 0016 ISNEV 6 6 +-- 0017 JMP 7 => 0019 +-- 0019 ITERC 5 3 3 +-- 0000 . FUNCC ; ipairs_aux +-- 0020 JITERL 5 1 +-- 0021 GGET 2 7 ; "assert" +-- 0022 ISEQV 1 1 +-- 0023 JMP 4 => 0026 +-- 0026 KPRI 4 2 +-- 0027 CALL 2 1 2 +-- 0000 . FUNCC ; assert +-- 0028 RET0 0 1 +-- +-- The crucial difference here is the abscent +-- `ISNEV` in the first case, which produces the +-- desired guarded `CONV`, when translated to IR. +-- +-- Since there is no guard, NaN is added to the sum, +-- despite the test case logic. + +for _, val in ipairs(data) do + if val == val then + sum = sum + val + end +end + +test:ok(sum == sum, 'NaN check was not omitted') + +os.exit(test:check() and 0 or 1) -- 2.39.2 (Apple Git-143)