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 C7CA9BD3DD9; Mon, 1 Jul 2024 12:12:04 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C7CA9BD3DD9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1719825124; bh=OstegHNhHjCEdk426+Ps1DAhIWaZIRFN0y5LIcIxTR4=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=XGUYJ72UQ9rcfNKZ5k+tATkuj1rJpvOv+Q4khwiLogq5n5Id+/REubB353j+ZOLOf 2996oTjUtZ6TR+socL/JD+9Exs+F2jvArr1fwGByTXkav1McUc/4YNAspABpDDGzrc bHWjncnfwTkQYFQBNirgwfRaM25R0jUMTsUIalFs= Received: from smtp63.i.mail.ru (smtp63.i.mail.ru [95.163.41.100]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 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 23555BD3DC1 for ; Mon, 1 Jul 2024 12:12:04 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 23555BD3DC1 Received: by exim-smtp-84f7fbf7d8-rcxsz with esmtpa (envelope-from ) id 1sOD4q-0000000028U-23Et; Mon, 01 Jul 2024 12:12:00 +0300 Date: Mon, 1 Jul 2024 12:11:59 +0300 To: Sergey Kaplun Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92EE09148F40C54469CD8C53AAEF396D43D23BC449FA949B1182A05F538085040431AAF8B8532D79B3DE06ABAFEAF6705E8052E10F1FCC0EB60797053C7A6A9788EDB03A1592C0308 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7E357932315C0A3C4EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063779018AFA06A12C538638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D86D7DA94DAD37BFA2B9F24FAA2CD65AAE3A14BB4E9D80B914CC7F00164DA146DAFE8445B8C89999728AA50765F79006370277CA7F994D7EF5389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC886A7C529F68B8E5CF6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA73AA81AA40904B5D9A18204E546F3947C4AB4081B6A6C2E07040F9FF01DFDA4A84AD6D5ED66289B523666184CF4C3C14F6136E347CC761E07725E5C173C3A84C3DB91D437BD62AC3CBA3038C0950A5D36B5C8C57E37DE458B330BD67F2E7D9AF16D1867E19FE14079C09775C1D3CA48CF3D321E7403792E342EB15956EA79C166A417C69337E82CC275ECD9A6C639B01B78DA827A17800CE7F05A7CAACACB19EC731C566533BA786AA5CC5B56E945C8DA X-C1DE0DAB: 0D63561A33F958A506D1F1EF1CCA7C315002B1117B3ED696FB5AAE4368C9401119AC5B239BAD4335823CB91A9FED034534781492E4B8EEAD47A3109F1ACFD409BDAD6C7F3747799A X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF1EE4A15EEB14EC5FF3DA842BB5235660667A9696C16FBEF1CBE3CAF77E21580317C141E43CE0FC0B9DF1169A732047E382ABEC30C64DDA6B900BFD9F93C76CDD52B1F6017F07C1845F4332CA8FE04980913E6812662D5F2A54F6898A6FDCBDC72A617DFBE5FEC2C6383653B6C8D9AE0FD16FCAA6493B703A X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojn+1t2APtxW+fln48aV3Gww== X-Mailru-Sender: 7940E2A4EB16C99705E36C42C2C7747C5C0C24D60B8B980D5ED5C4874F525AA3A99E81A60BCF27D628C44B9C631FF20DE747D2BC1AD30F01FB559BB5D741EB9612EB40C6FA10D71EFD657A8799238ED55FEEDEB644C299C0ED14614B50AE0675 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH luajit 2/2] Avoid negation of signed integers in C that may hold INT*_MIN. 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 Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Hi, Sergey! Thanks for the patch! LGTM, except for a few nits below. On Tue, Jun 25, 2024 at 06:54:25PM GMT, Sergey Kaplun wrote: > From: Mike Pall > > Reported by minoki. > Recent C compilers 'take advantage' of the undefined behavior. > This completely changes the meaning of expressions like (k == -k). > > (cherry picked from commit 8a5e398c52c7f8ca3e1a0e574cc2ba38224b759b) > > This patch changes all possibly dangerous -x operations on integers to > the corresponding two's complement. Also, it removes all related UBSAN > suppressions, since they are fixed. > > Also, this patch limits the `bit.tohex()` result by 254 characters. > > There is no testcase for `strscan_oct()`, `strscan_dec()` or/and > `STRSCAN_U32` format since first the unary minus is parsed first and > only after the number itself is parsed during parsing C syntax. So the > error is raised in `cp_expr_prefix()` instead. For parsing the exponent > header, there is no testcase, since the power is limited by > `STRSCAN_MAXEXP`. > > Sergey Kaplun: > * added the description and the test for the problem > > Part of tarantool/tarantool#9924 > Relates to tarantool/tarantool#8473 > --- > src/lib_base.c | 2 +- > src/lib_bit.c | 3 +- > src/lj_asm_mips.h | 2 +- > src/lj_carith.c | 7 +- > src/lj_cparse.c | 2 +- > src/lj_crecord.c | 3 +- > src/lj_ctype.c | 2 +- > src/lj_emit_arm.h | 2 +- > src/lj_emit_arm64.h | 9 +- > src/lj_obj.h | 2 +- > src/lj_opt_fold.c | 6 +- > src/lj_parse.c | 17 +-- > src/lj_strfmt.c | 9 +- > src/lj_strscan.c | 26 ++-- > src/lj_vmmath.c | 6 +- > .../lj-928-int-min-negation.test.lua | 121 ++++++++++++++++++ > 16 files changed, 164 insertions(+), 55 deletions(-) > create mode 100644 test/tarantool-tests/lj-928-int-min-negation.test.lua > > diff --git a/test/tarantool-tests/lj-928-int-min-negation.test.lua b/test/tarantool-tests/lj-928-int-min-negation.test.lua > new file mode 100644 > index 00000000..26f4ed8e > --- /dev/null > +++ b/test/tarantool-tests/lj-928-int-min-negation.test.lua > @@ -0,0 +1,121 @@ > +local tap = require('tap') > + > +-- Test file to demonstrate LuaJIT's UBSan failures during > +-- `INT*_MIN` negation. > +-- See also: https://github.com/LuaJIT/LuaJIT/issues/928. > + > +local test = tap.test('lj-928-int-min-negation.'):skipcond({ > + ['Test requires JIT enabled'] = not jit.status(), > +}) > + > +local INT32_MIN = -0x80000000 > +local INT64_MIN = -0x8000000000000000 > +local TOBIT_CHAR_MAX = 254 > + > +-- XXX: Many tests (`tonumber()`-related) are failed under UBSan Typo: s/are failed/are failing/ > +-- with DUALNUM enabled. They are included to avoid regressions in > +-- the future if such a build becomes the default. > +local ffi = require('ffi') > +local LL_T = ffi.typeof(1LL) > + > +test:plan(14) > + > +jit.opt.start('hotloop=1') > + > +-- Temporary variable for the results. > +local r Let's name it `result` or `tmp_result` then. > + > +-- :`lj_vm_modi()` > +for _ = 1, 4 do > + -- Use additional variables to avoid folding during parsing. > + -- Operands should be constants on the trace. > + local x = -0x80000000 > + local y = -0x80000000 > + r = x % y > +end > +test:is(r, 0, 'no UB during lj_vm_modi') > + > +-- :`lj_strfmt_wint()` > +for _ = 1, 4 do > + -- Operand should be the constant on the trace. Typo: s/the constant/a constant/ > + r = tostring(bit.tobit(0x80000000)) > +end > +test:is(r, '-2147483648', 'no UB during lj_strfmt_wint') > + > +-- :`lj_strfmt_putfxint()` > +test:is(('%d'):format(INT64_MIN), '-9223372036854775808', > + 'no UB during lj_strfmt_putfxint') > + > +-- :`bcemit_unop()` > +local int64_min_cdata = -0x8000000000000000LL > +test:ok(true, 'no UB during bcemit_unop') > + > +-- :`carith_int64()` > +-- Use the additional variable to avoid folding during > +-- `bcemit_unop()`. > +test:is(-int64_min_cdata, int64_min_cdata, 'no UB during carith_int64') > + > +-- :`lj_ctype_repr_int64()` > +-- Use cast to separate the test case from `bcemit_unop()`. > +test:is(tostring(LL_T(INT64_MIN)), '-9223372036854775808LL', > + 'no UB during lj_ctype_repr_int64') > + > +local TOHEX_EXPECTED = ('0'):rep(TOBIT_CHAR_MAX) > +-- :`bit_tohex()` > +-- The second argument is the number of bytes to be represented. > +-- The negative value stands for uppercase. > +test:is(bit.tohex(0, INT32_MIN), TOHEX_EXPECTED, 'no UB during bit_tohex') > + > +-- :`recff_bit64_tohex()` > +-- The second argument is the number of bytes to be represented. > +-- The negative value stands for uppercase. > +for _ = 1, 4 do > + -- The second argument should be the constant on the trace. > + r = bit.tohex(0, -0x80000000) > +end > +test:is(r, TOHEX_EXPECTED, 'no UB during recording bit.tohex') > + > +-- :`simplify_intsub_k()` > +r = 0 > +for _ = 1, 4 do > + r = r - 0x8000000000000000LL > +end > +test:is(r, 0LL, 'no UB during simplify_intsub_k') > + > +-- :`strscan_hex()` > +test:is(tonumber('-0x80000000'), INT32_MIN, 'no UB during strscan_hex') > + > +-- :`strscan_bin()` > +test:is(tonumber('-0b10000000000000000000000000000000'), INT32_MIN, > + 'no UB during strscan_bin') > + > +-- :`lj_strscan_scan()` > +test:is(tonumber('-2147483648'), INT32_MIN, 'no UB during strscan_scan') > + > +-- Test for 32bit long, just in case. > +-- :`tonumber()` > +test:is(tonumber('-2000000000000000', 4), INT32_MIN, > + 'no UB during tonumber, base 4') > + > +-- :`cp_expr_prefix()` > +-- According to ISO/IEC 9899:2023 [1]: > +-- | Each constant expression shall evaluate to a constant that is > +-- | in the range of representable values for its type. > +-- It means that since 0x80000000 does not fit in the int32_t > +-- range, -0x80000000 does not fit in the int32_t range either. > +-- > +-- In the case when the enumeration has no fixed underlying type, > +-- the type of the enum is implementation defined [2][3]. > +-- > +-- Hence, we used -INT32_MAX - 1 since both values fit into > +-- int32_t, so it can't be ambiguous. > +-- > +-- luacheck: ignore (too long line) > +-- [1]: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf#subsection.6.2.6 > +-- [2]: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf#%5B%7B%22num%22%3A232%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22Fit%22%7D%5D > +-- [3]: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf#subsubsection.6.7.2.2 > +ffi.cdef[[typedef enum {enum_int32_min = -0x7fffffff - 1} enum_t;]] > +test:is(ffi.new('enum_t', 'enum_int32_min'), LL_T(INT32_MIN), > + 'no UB during cp_expr_prefix') > + > +test:done(true) > -- > 2.45.1 >