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 09881656C0D; Wed, 15 May 2024 15:36:51 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 09881656C0D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1715776611; bh=F2SLKCcClMn3lEByaXPkZ1wA7iyWkBoHML5DMNLtvew=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=K/XDsMeGiKgtL4+RdYkZOnTxQNP+N8dies2feBUTFlMzz4MyDHOYa0vDbXlVLY0Rm UnZYloby72P+J7csnWtqvioJ7ZgFvoNB6uToM7TTC0sy8GMNQQAfpVh/cS51aNaNbt qV4MMbPnPtz83XvCmhau8yC9vtPCY5oKmm7EVJYE= Received: from smtp55.i.mail.ru (smtp55.i.mail.ru [95.163.41.93]) (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 AB527656C0D for ; Wed, 15 May 2024 15:36:21 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org AB527656C0D Received: by smtp55.i.mail.ru with esmtpa (envelope-from ) id 1s7Drn-00000007iTn-2CnR; Wed, 15 May 2024 15:36:20 +0300 To: Maxim Kokryashkin , Sergey Bronnikov Date: Wed, 15 May 2024 15:32:00 +0300 Message-ID: <6f8a08e1823bfceebb4057207ee2f2bdb7d2d47c.1715776117.git.skaplun@tarantool.org> X-Mailer: git-send-email 2.45.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD947734D04B31285446F57B590FDB78589CF549734B559200C00894C459B0CD1B95F47E1FADD2B5352E4A55148009453FF5959A582DC2BD1547116BF2B354146CF2890F9C47E136152 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7E64685AF2BAE6CDFEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637CE6693660FA336BEEA1F7E6F0F101C6723150C8DA25C47586E58E00D9D99D84E1BDDB23E98D2D38B043BF0FB74779F36D4A718F9C9B6013FF3A03E2E7C173D790FD679566EF87560A471835C12D1D9774AD6D5ED66289B5278DA827A17800CE77FFCE1C639F4728C9FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C390D92131081DE748117882F4460429728AD0CFFFB425014E868A13BD56FB6657E2021AF6380DFAD1A18204E546F3947CD2DCF9CF1F528DBC2E808ACE2090B5E1725E5C173C3A84C327ED053E960B195E089D37D7C0E48F6C8AA50765F7900637BC468E7E89D8C5D6EFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-C1DE0DAB: 0D63561A33F958A54CADA80372B9753A5002B1117B3ED696F672BE080E32C537BFF4097FFC9E796F823CB91A9FED034534781492E4B8EEADB30A456A8F293845C79554A2A72441328621D336A7BC284946AD531847A6065A535571D14F44ED41 X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CF36E653847FB15169BA0797AA060465EB81ADFC70340924BDC0C40F472C42A2AADDCE95BB4BF576A68B4B06B5E1E5FA5CCABF1798C1C0BF4E006D5F5C0420F1D60335DC02F3BAFD66C226CC413062362A913E6812662D5F2A5EAB5682573093F7837F15F2B5E4A70B33F2C28C22F508233FCF178C6DD14203 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj1+ZIz0g+CV4d6tvr0v0iAw== X-DA7885C5: 98F1B20A1AC23638F255D290C0D534F9A5EB5BF6DD2ADCB97209594F4121BE6FEC492B9AC8DBF9875B1A4C17EAA7BC4BEF2421ABFA55128DAF83EF9164C44C7E X-Mailru-Sender: 689FA8AB762F7393C6D0B12EA33CAA9BEC598D7A7DEC916E319E4B23862C99EB001EF69133F97F87E49D44BB4BD9522A059A1ED8796F048DB274557F927329BE89D5A3BC2B10C37545BD1C3CC395C826B4A721A3011E896F X-Mras: Ok Subject: [Tarantool-patches] [PATCH luajit 1/2] build: introduce LUAJIT_USE_UBSAN option 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" This patch adds Undefined Behaviour Sanitizer [1] support. It enables all checks except several that are not useful for LuaJIT. Also, it instruments all known issues to be fixed in future patches (except `kfold_intop()` since cdata arithmetic relies on integer overflow). [1]: https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html Resolves tarantool/tarantool#8473 --- CMakeLists.txt | 45 ++++++++++++++++++++++++++++++++++++++ cmake/SetDynASMFlags.cmake | 11 ++++++++++ src/lj_carith.c | 5 +++++ src/lj_opt_fold.c | 5 +++++ src/lj_parse.c | 5 +++++ src/lj_snap.c | 7 ++++++ src/lj_strfmt.c | 5 +++++ 7 files changed, 83 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2355ce17..edf2012f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -300,6 +300,51 @@ if(LUAJIT_USE_ASAN) ) endif() +option(LUAJIT_USE_UBSAN "Build LuaJIT with UndefinedBehaviorSanitizer" OFF) +if(LUAJIT_USE_UBSAN) + # Use all recommendations from the UndefinedBehaviorSanitizer + # documentation: + # https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html. + string(JOIN "," UBSAN_IGNORE_OPTIONS + # Misaligned pseudo-pointers are used to determine internal + # variable names inside the `for` cycle. + alignment + # Not interested in float cast overflow errors. + float-cast-overflow + # NULL checking is disabled because this is not a UB and + # raises lots of false-positive fails. + null + # Not interested in checking arithmetic with NULL. + pointer-overflow + # Shifts of negative numbers are widely used in parsing ULEB, + # cdata arithmetic, vmevent hash calculation, etc. + shift-base + ) + if(NOT CMAKE_C_COMPILER_ID STREQUAL "GNU") + string(JOIN "," UBSAN_IGNORE_OPTIONS + ${UBSAN_IGNORE_OPTIONS} + # Not interested in function type mismatch errors. + function + ) + endif() + AppendFlags(CMAKE_C_FLAGS + # Enable hints for UndefinedBehaviorSanitizer. + -DLUAJIT_USE_UBSAN + # XXX: To get nicer stack traces in error messages. + -fno-omit-frame-pointer + # Enable UndefinedBehaviorSanitizer support. + # This flag enables all supported options (the documentation + # on cite is not correct about that moment, unfortunately) + # except float-divide-by-zero. Floating point division by zero + # behaviour is defined without -ffast-math and uses the + # IEEE 754 standard on which all NaN tagging is based. + -fsanitize=undefined + -fno-sanitize=${UBSAN_IGNORE_OPTIONS} + # Print a verbose error report and exit the program. + -fno-sanitize-recover=undefined + ) +endif() + # Enable code coverage support. option(LUAJIT_ENABLE_COVERAGE "Enable code coverage support (gcovr)" OFF) if(LUAJIT_ENABLE_COVERAGE) diff --git a/cmake/SetDynASMFlags.cmake b/cmake/SetDynASMFlags.cmake index 7eead6e9..ae3c75b1 100644 --- a/cmake/SetDynASMFlags.cmake +++ b/cmake/SetDynASMFlags.cmake @@ -136,5 +136,16 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL ${CMAKE_HOST_SYSTEM_NAME}) endif() endif() +if(LUAJIT_USE_UBSAN) + # XXX: Skip checks for now to avoid build failures due to + # sanitizer errors. + # Need to backprot commits that fix the following issues first: + # https://github.com/LuaJIT/LuaJIT/pull/969, + # https://github.com/LuaJIT/LuaJIT/pull/970, + # https://github.com/LuaJIT/LuaJIT/issues/1041, + # https://github.com/LuaJIT/LuaJIT/pull/1044. + AppendFlags(HOST_C_FLAGS -fno-sanitize=undefined) +endif() + unset(LUAJIT_ARCH) unset(TESTARCH) diff --git a/src/lj_carith.c b/src/lj_carith.c index 4e1d450a..1d9d6fe1 100644 --- a/src/lj_carith.c +++ b/src/lj_carith.c @@ -159,6 +159,11 @@ static int carith_ptr(lua_State *L, CTState *cts, CDArith *ca, MMS mm) } /* 64 bit integer arithmetic. */ +#if LUAJIT_USE_UBSAN +/* See https://github.com/LuaJIT/LuaJIT/issues/928. */ +static int carith_int64(lua_State *L, CTState *cts, CDArith *ca, MMS mm) + __attribute__((no_sanitize("signed-integer-overflow"))); +#endif static int carith_int64(lua_State *L, CTState *cts, CDArith *ca, MMS mm) { if (ctype_isnum(ca->ct[0]->info) && ca->ct[0]->size <= 8 && diff --git a/src/lj_opt_fold.c b/src/lj_opt_fold.c index 96780fa2..b9326c65 100644 --- a/src/lj_opt_fold.c +++ b/src/lj_opt_fold.c @@ -260,6 +260,11 @@ LJFOLDF(kfold_numcomp) /* -- Constant folding for 32 bit integers -------------------------------- */ +#if LUAJIT_USE_UBSAN +/* Cdata arithmetic depends on the interger overflow. */ +static int32_t kfold_intop(int32_t k1, int32_t k2, IROp op) + __attribute__((no_sanitize("signed-integer-overflow"))); +#endif static int32_t kfold_intop(int32_t k1, int32_t k2, IROp op) { switch (op) { diff --git a/src/lj_parse.c b/src/lj_parse.c index 5a4ab7c8..acceed17 100644 --- a/src/lj_parse.c +++ b/src/lj_parse.c @@ -939,6 +939,11 @@ static void bcemit_binop(FuncState *fs, BinOpr op, ExpDesc *e1, ExpDesc *e2) } /* Emit unary operator. */ +#if LUAJIT_USE_UBSAN +/* See https://github.com/LuaJIT/LuaJIT/issues/928. */ +static void bcemit_unop(FuncState *fs, BCOp op, ExpDesc *e) + __attribute__((no_sanitize("signed-integer-overflow"))); +#endif static void bcemit_unop(FuncState *fs, BCOp op, ExpDesc *e) { if (op == BC_NOT) { diff --git a/src/lj_snap.c b/src/lj_snap.c index 5a00b5cd..7dc4fe35 100644 --- a/src/lj_snap.c +++ b/src/lj_snap.c @@ -756,6 +756,13 @@ static void snap_restoreval(jit_State *J, GCtrace *T, ExitState *ex, } #if LJ_HASFFI +# if LUAJIT_USE_UBSAN +/* See https://github.com/LuaJIT/LuaJIT/issues/1193. */ +static void snap_restoredata(jit_State *J, GCtrace *T, ExitState *ex, + SnapNo snapno, BloomFilter rfilt, + IRRef ref, void *dst, CTSize sz) + __attribute__((no_sanitize("bounds"))); +# endif /* Restore raw data from the trace exit state. */ static void snap_restoredata(jit_State *J, GCtrace *T, ExitState *ex, SnapNo snapno, BloomFilter rfilt, diff --git a/src/lj_strfmt.c b/src/lj_strfmt.c index ff5568c3..9592eff1 100644 --- a/src/lj_strfmt.c +++ b/src/lj_strfmt.c @@ -93,6 +93,11 @@ retlit: { uint32_t d = (x*(((1<>sh; x -= d*sc; *p++ = (char)('0'+d); } /* Write integer to buffer. */ +#if LUAJIT_USE_UBSAN +/* See https://github.com/LuaJIT/LuaJIT/issues/928. */ +char * LJ_FASTCALL lj_strfmt_wint(char *p, int32_t k) + __attribute__((no_sanitize("signed-integer-overflow"))); +#endif char * LJ_FASTCALL lj_strfmt_wint(char *p, int32_t k) { uint32_t u = (uint32_t)k; -- 2.45.0