From patchwork Thu Apr 18 07:21:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1924833 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=aFES+Z2H; 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 4VKq3b4j1jz1yZ2 for ; Thu, 18 Apr 2024 17:22:17 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4B0BA3858404 for ; Thu, 18 Apr 2024 07:22:11 +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 130D13858D20 for ; Thu, 18 Apr 2024 07:21:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 130D13858D20 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 130D13858D20 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=1713424915; cv=none; b=xBgmLNbKh3f5mSsHIcF9+X4OnYwMpqlmoYrIU624ZTypeH7Y5FpwTitvzrYj3Df064V6/6IXHtR9H8t0fbvjrpQSwT/f3X9WKB5BRBJDv3XoSHEnVHIleb/zGp2Hfwag2fvonDxTHenu3PBjiRgBx2b6HuY9cnSQGKGr+BBpJtQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713424915; c=relaxed/simple; bh=QtHOM2I8hdBkwReY18TukiyjCvoXluEX68OH2RY7DEo=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=Ct23zx/5c8xMCrhOXuVc9+ZTcXZ3SE6gKW0VIQmI0gChEaom162gMfZUEAQjFkX8AgrSjSfUSuWENVmcfV9hGsfS08h29Zrbw4xOx6gx7f9jTUai+tdJaqdKNmbkRR9F9tvJhDQSXceZja6VRuB97oR+zSGGhVAzz6u1mOZRWIc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713424913; 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=30jo6r782mzqe6Xb/rHTFTCBeJACe+uP4CJEUFu5Djg=; b=aFES+Z2HPih3ynUSEr38WzVAv7JfRHBvUiDMwCV6ZmfSzC1uAUJl4bfZ6DR8tKEMVCo2xR 0AstVTjMISoCiqpAbPfXwGjI7coR02mm4xfVYFfJRLo1u4OPZxilF2PnYKbS+nOyOjrP7d vNtAs4yDHlb6cy95Tk6BPYfEH2LCBno= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-308-mFuKGOsYOw-4CFz973DyAg-1; Thu, 18 Apr 2024 03:21:50 -0400 X-MC-Unique: mFuKGOsYOw-4CFz973DyAg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (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 ECF62810BDC; Thu, 18 Apr 2024 07:21:49 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.5]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AF64320B45A7; Thu, 18 Apr 2024 07:21:49 +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 43I7Lgnu1267868 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 18 Apr 2024 09:21:43 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 43I7Lgdh1267867; Thu, 18 Apr 2024 09:21:42 +0200 Date: Thu, 18 Apr 2024 09:21:42 +0200 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] internal-fn: Temporarily disable flag_trapv during .{ADD,SUB,MUL}_OVERFLOW etc. expansion [PR114753] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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! __builtin_{add,sub,mul}_overflow{,_p} builtins are well defined for all inputs even for -ftrapv, and the -fsanitize=signed-integer-overflow ifns shouldn't abort in libgcc but emit the desired ubsan diagnostics or abort depending on -fsanitize* setting regardless of -ftrapv. The expansion of these internal functions uses expand_expr* in various places (e.g. MULT_EXPR at least in 2 spots), so temporarily disabling flag_trapv in all those spots would be hard. The following patch disables it around the bodies of 3 functions which can do the expand_expr calls. If it was in the C++ FE, I'd use some RAII sentinel, but I don't think we have one in the middle-end. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-04-18 Jakub Jelinek PR middle-end/114753 * internal-fn.cc (expand_mul_overflow): Save flag_trapv and temporarily clear it for the duration of the function, then restore previous value. (expand_vector_ubsan_overflow): Likewise. (expand_arith_overflow): Likewise. * gcc.dg/pr114753.c: New test. Jakub --- gcc/internal-fn.cc.jj 2024-03-23 08:22:50.490607002 +0100 +++ gcc/internal-fn.cc 2024-04-17 13:44:21.673594413 +0200 @@ -1631,7 +1631,11 @@ expand_mul_overflow (location_t loc, tre rtx target = NULL_RTX; signop sign; enum insn_code icode; + int save_flag_trapv = flag_trapv; + /* We don't want any __mulv?i3 etc. calls from the expansion of + these internal functions, so disable -ftrapv temporarily. */ + flag_trapv = 0; done_label = gen_label_rtx (); do_error = gen_label_rtx (); @@ -2479,6 +2483,7 @@ expand_mul_overflow (location_t loc, tre else expand_arith_overflow_result_store (lhs, target, mode, res); } + flag_trapv = save_flag_trapv; } /* Expand UBSAN_CHECK_* internal function if it has vector operands. */ @@ -2499,7 +2504,11 @@ expand_vector_ubsan_overflow (location_t rtx resvr = NULL_RTX; unsigned HOST_WIDE_INT const_cnt = 0; bool use_loop_p = (!cnt.is_constant (&const_cnt) || const_cnt > 4); + int save_flag_trapv = flag_trapv; + /* We don't want any __mulv?i3 etc. calls from the expansion of + these internal functions, so disable -ftrapv temporarily. */ + flag_trapv = 0; if (lhs) { optab op; @@ -2629,6 +2638,7 @@ expand_vector_ubsan_overflow (location_t } else if (resvr) emit_move_insn (lhsr, resvr); + flag_trapv = save_flag_trapv; } /* Expand UBSAN_CHECK_ADD call STMT. */ @@ -2707,7 +2717,11 @@ expand_arith_overflow (enum tree_code co prec0 = MIN (prec0, pr); pr = get_min_precision (arg1, uns1_p ? UNSIGNED : SIGNED); prec1 = MIN (prec1, pr); + int save_flag_trapv = flag_trapv; + /* We don't want any __mulv?i3 etc. calls from the expansion of + these internal functions, so disable -ftrapv temporarily. */ + flag_trapv = 0; /* If uns0_p && uns1_p, precop is minimum needed precision of unsigned type to hold the exact result, otherwise precop is minimum needed precision of signed type to @@ -2748,6 +2762,7 @@ expand_arith_overflow (enum tree_code co ops.location = loc; rtx tem = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL); expand_arith_overflow_result_store (lhs, target, mode, tem); + flag_trapv = save_flag_trapv; return; } @@ -2771,6 +2786,7 @@ expand_arith_overflow (enum tree_code co if (integer_zerop (arg0) && !unsr_p) { expand_neg_overflow (loc, lhs, arg1, false, NULL); + flag_trapv = save_flag_trapv; return; } /* FALLTHRU */ @@ -2781,6 +2797,7 @@ expand_arith_overflow (enum tree_code co case MULT_EXPR: expand_mul_overflow (loc, lhs, arg0, arg1, unsr_p, unsr_p, unsr_p, false, NULL); + flag_trapv = save_flag_trapv; return; default: gcc_unreachable (); @@ -2826,6 +2843,7 @@ expand_arith_overflow (enum tree_code co else expand_mul_overflow (loc, lhs, arg0, arg1, unsr_p, uns0_p, uns1_p, false, NULL); + flag_trapv = save_flag_trapv; return; } --- gcc/testsuite/gcc.dg/pr114753.c.jj 2024-04-17 13:55:16.246482369 +0200 +++ gcc/testsuite/gcc.dg/pr114753.c 2024-04-17 13:54:14.035352376 +0200 @@ -0,0 +1,14 @@ +/* PR middle-end/114753 */ +/* { dg-do run } */ +/* { dg-options "-O2 -ftrapv" } */ + +int +main () +{ + volatile long long i = __LONG_LONG_MAX__; + volatile long long j = 2; + long long k; + if (!__builtin_mul_overflow (i, j, &k) || k != -2LL) + __builtin_abort (); + return 0; +}