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: 1878885 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=XkB/01Pb; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SwhK73wZcz1ypX for ; Thu, 21 Dec 2023 18:05:31 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7492D386184B for ; Thu, 21 Dec 2023 07:05:29 +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+incoming=patchwork.ozlabs.org@gcc.gnu.org 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" } */