From patchwork Thu Dec 21 07:05:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 182018 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2483:b0:fb:cd0c:d3e with SMTP id q3csp234004dyi; Wed, 20 Dec 2023 23:05:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IEKzSk+fOmhWwBGKw40v5hf9JN469kJ/nsc6xP28s5EBPwtN5Z4W7IQm44iCaRLqrB4UCnn X-Received: by 2002:a05:6214:528a:b0:67f:2a02:7f40 with SMTP id kj10-20020a056214528a00b0067f2a027f40mr10849203qvb.78.1703142342002; Wed, 20 Dec 2023 23:05:42 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1703142341; cv=pass; d=google.com; s=arc-20160816; b=OLtoR5k+I4OIEP0Wh/dCCGEwndLZz0n6zG29xx+TWDQx6EkUA/Gm1iTaSa3GR0w24s 53N1miQeH7Z4ZYb48n67MrZHDV0vvyStNgmOwP2vXGbcaRTpkGZD2nIU+m5kvtSD0FyT Y8csPWqEr4KDOEC+qLdBkU0qwvDhXMpjK5IRJilmrFUNkcemLxAU/NyZTkAUVDcOwKCW KlwmS1bu2DKHLXH4tt7hc2Zfya0QBp0z4orcwvr2mt6UzHujg6UiTE2+s5GMjmsvsGy0 bn1WJKtJrgFB7TVOEzhns+v+RK1bL1lJkeJ4OSiol1AZQQ6xshnmvk2s08W4qgy1MUg6 1LTA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=4SS1DpSjsEfPePC1Q6zN1ErUrT38A92MXVMu0wFKLZM=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=RILZjB0P8/JnO7XtnNHoYcUYtHY/bRKz8R/DPrz7IQdOVCA4XpZ2WjQ/cv2LNY7TWO 5JZItoRA4up2uVAib42Ez7qfG/eR19OK+e0r8lPHX/AQvF8T6BAOvtIBVBXQogbaAgxz +TLzSIweFUICnSpvSx9ri8haLrJBIHWBHyn/GWY/lLTtaPUKva3+bUtLlDYS2jXjG1Y5 /LH0OPtkUQwxnkTpJHHCykcoB2z8riSUxQNguc4yTyhLtWqbYosS5O72o+BWAF1aQayW jIqcPtKX7JQVQCT59IJS/DFjednGdc1nqNsl84e66bgFfp+UU+nVPVIAIPvsftOhTxj1 ClDw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="XkB/01Pb"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id d27-20020a0cb2db000000b0067f3bfc3e80si1521652qvf.321.2023.12.20.23.05.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 23:05:41 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="XkB/01Pb"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AA420386188F for ; Thu, 21 Dec 2023 07:05:41 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 6E13E3858407 for ; Thu, 21 Dec 2023 07:05:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6E13E3858407 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6E13E3858407 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703142318; cv=none; b=jEeUJFF+kNiU4tirBosmP4Fs8jzGQkYfsN9VUG6uDxZfUJxpsPjDq6UZNtBD4no4qLxdTVO3rfrkf4zlmmO4h5e+TW8V6YaR3yQlMtclpdKXi2G5JemsFztoyrq8uWBkBb4tnkm4EVw1KU4DYuyQvIN9KiI/luw1OFV5OM/kc8I= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703142318; c=relaxed/simple; bh=YpZ5ZGMOfnk7aKm6acBHTURy8GhUI8LtPJybGmEC7h4=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=Lbr71Jyqms9Obnoj1vhri74IKMgtjs8MiQkopsX/II9IWsyTwPylJ3m1O7IhLPE3xwpuXzxRGXVY3j3NUEVpCSb96px44kSGVIZrbovqzY3AdQZsvYzm4bOSMKfoC/RJR24TUjVmonrtVKox6m+AM4J3KI0674G9jhT3JiI7/l0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1703142316; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=4SS1DpSjsEfPePC1Q6zN1ErUrT38A92MXVMu0wFKLZM=; b=XkB/01PbrpO9aF8MJV3PQIWaGkoPLT3apFaJnc7pLTmX6PdWPtJl2ixJPyzyClx8nRhIjC SwDbGat20rdC/s5Rz5vW3CSsF1ez1dHoXqpSNJXkUXngCRqGouTPStta981qhDVg4Q8YVl tBEEpmsIHEXS7UdJzTML+VNvAjxwyL8= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-668-GvP1dwcyNeam-vW0d256aQ-1; Thu, 21 Dec 2023 02:05:14 -0500 X-MC-Unique: GvP1dwcyNeam-vW0d256aQ-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4F771386915A; Thu, 21 Dec 2023 07:05:14 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 13DEA492BE6; Thu, 21 Dec 2023 07:05:13 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 3BL75Bea1608213 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 21 Dec 2023 08:05:12 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3BL75AXU1608212; Thu, 21 Dec 2023 08:05:10 +0100 Date: Thu, 21 Dec 2023 08:05:10 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] ubsan: Add workaround for missing bitint libubsan support for shifts [PR112941] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, PDS_BTC_ID, PDS_BTC_MSGID, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785874184090994700 X-GMAIL-MSGID: 1785874184090994700 Hi! libubsan still doesn't support bitints, so ubsan contains a workaround and emits value 0 and TK_Unknown kind for those. If shift second operand has the large/huge _BitInt type, this results in internal errors in libubsan though, so the following patch provides a temporary workaround for that - in the rare case where the last operand has _BitInt type wider than __int128 (or long long on 32-bit arches), it will pretend the shift count has that type saturated to its range. IMHO better than crashing in the library. If the value fits into the __int128 (or long long) range, it will be printed correctly (just print that it has __int128/long long type rather than say _BitInt(255)), if it doesn't, user will at least know that it is a very large negative or very large positive value. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-12-21 Jakub Jelinek PR sanitizer/113092 * c-ubsan.cc (ubsan_instrument_shift): Workaround for missing ubsan _BitInt support for the shift count. * gcc.dg/ubsan/bitint-4.c: New test. Jakub --- gcc/c-family/c-ubsan.cc.jj 2023-09-06 17:29:47.917880370 +0200 +++ gcc/c-family/c-ubsan.cc 2023-12-20 15:13:53.515851635 +0100 @@ -256,6 +256,32 @@ ubsan_instrument_shift (location_t loc, tt = build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TRAP), 0); else { + if (TREE_CODE (type1) == BITINT_TYPE + && TYPE_PRECISION (type1) > MAX_FIXED_MODE_SIZE) + { + /* Workaround for missing _BitInt support in libsanitizer. + Instead of crashing in the library, pretend values above + maximum value of normal integral type or below minimum value + of that type are those extremes. */ + tree type2 = build_nonstandard_integer_type (MAX_FIXED_MODE_SIZE, + TYPE_UNSIGNED (type1)); + tree op2 = op1; + if (!TYPE_UNSIGNED (type1)) + { + op2 = fold_build2 (LT_EXPR, boolean_type_node, unshare_expr (op1), + fold_convert (type1, TYPE_MIN_VALUE (type2))); + op2 = fold_build3 (COND_EXPR, type2, op2, TYPE_MIN_VALUE (type2), + fold_convert (type2, unshare_expr (op1))); + } + else + op2 = fold_convert (type2, op1); + tree op3 + = fold_build2 (GT_EXPR, boolean_type_node, unshare_expr (op1), + fold_convert (type1, TYPE_MAX_VALUE (type2))); + op1 = fold_build3 (COND_EXPR, type2, op3, TYPE_MAX_VALUE (type2), + op2); + type1 = type2; + } tree utd0 = ubsan_type_descriptor (type0, UBSAN_PRINT_FORCE_INT); tree data = ubsan_create_data ("__ubsan_shift_data", 1, &loc, utd0, ubsan_type_descriptor (type1), NULL_TREE, --- gcc/testsuite/gcc.dg/ubsan/bitint-4.c.jj 2023-12-20 15:24:18.908027190 +0100 +++ gcc/testsuite/gcc.dg/ubsan/bitint-4.c 2023-12-20 15:24:14.681086830 +0100 @@ -0,0 +1,22 @@ +/* PR sanitizer/113092 */ +/* { dg-do run { target bitint575 } } */ +/* { dg-options "-fsanitize=shift -fsanitize-recover=shift" } */ + +int +main () +{ + volatile _BitInt(255) bi = 12984732985743985734598574358943wb; + bi = 0 >> bi; + bi = 329847329847239847239847329847239857489657986759867549867594875984375wb; + bi = 0 >> bi; + bi = -12984732985743985734598574358943wb; + bi = 0 >> bi; + bi = -329847329847239847239847329847239857489657986759867549867594875984375wb; + bi = 0 >> bi; + return 0; +} + +/* { dg-output "shift exponent \[0-9a-fx]* is too large for \[0-9]*-bit type 'int'\[^\n\r]*(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*shift exponent \[0-9a-fx]* is too large for \[0-9]*-bit type 'int'\[^\n\r]*(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*shift exponent \[0-9a-fx-]* is negative\[^\n\r]*(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*shift exponent \[0-9a-fx-]* is negative" } */