From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2a07:de40:b251:101:10:150:64:1]) by sourceware.org (Postfix) with ESMTPS id 74BFC3858D20 for ; Thu, 18 Apr 2024 07:39:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 74BFC3858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 74BFC3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713425980; cv=none; b=Tgkoej4tmLEbf/98yApagm5ggdr1n+9MPgDOvz1EMGEdGTGW/3Q5bt9ybbz1pIYD0X5VuQI34qzG8maZJOITR3wQWCX1m1Zuk7hgK/WJ49R3a31wbdGLbIB0IodOPOiXN4VKriwyzyKV3nCVQof7EQqx1zS9+NQEl/m0KRNBRsM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713425980; c=relaxed/simple; bh=vf0jm/TUppgvozUyfJpFToRppyT5kS5+qbauVk6I1Rc=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:Message-ID:MIME-Version; b=d5ebYNO780lHJfXMAGNp5EYCB5ZPmcp2rEkRR2fM5XFC3kjh3CyYS89nrDMB6ioOY4sYcS7JS1uuirffs1LvZraSxyRBvooWbUG7hH/lzDezq5LWaDkagCxZOGkzKCnfJh7XV/rPmOKn6UXgd6Yqf1c/Ryy94P4AqiPfgXoja9E= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from [10.168.5.241] (unknown [10.168.5.241]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 56DB5339C9; Thu, 18 Apr 2024 07:39:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1713425977; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=BgafBSsMkHS25vDNOBzLrjCHLvzPg56kdIMzBn7XXOY=; b=H++cqUw81q601UrwY5CganYxRGQr1aZ1tPKeRBz61HQSGunJn5u+OvAf8PvE/SvqKVuAhN kskiMpgttFt+mopBqOR42iVA/Av9PvLVPcVtleh4T/O8a7+R8kuYqgr/dSpDBCAfAGScdu 5dZ+j1LSvULSC/D7Zd6SFYRrcOtIFXI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1713425977; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=BgafBSsMkHS25vDNOBzLrjCHLvzPg56kdIMzBn7XXOY=; b=csumVGH5Z4D3L3gbyNclw5B8iK9WCQIzToIiEjLMhYTM8kgfNtuAFev+wRVtP0tKYSbJUF NWbO0SPCV06AhdCQ== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1713425977; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=BgafBSsMkHS25vDNOBzLrjCHLvzPg56kdIMzBn7XXOY=; b=H++cqUw81q601UrwY5CganYxRGQr1aZ1tPKeRBz61HQSGunJn5u+OvAf8PvE/SvqKVuAhN kskiMpgttFt+mopBqOR42iVA/Av9PvLVPcVtleh4T/O8a7+R8kuYqgr/dSpDBCAfAGScdu 5dZ+j1LSvULSC/D7Zd6SFYRrcOtIFXI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1713425977; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=BgafBSsMkHS25vDNOBzLrjCHLvzPg56kdIMzBn7XXOY=; b=csumVGH5Z4D3L3gbyNclw5B8iK9WCQIzToIiEjLMhYTM8kgfNtuAFev+wRVtP0tKYSbJUF NWbO0SPCV06AhdCQ== Date: Thu, 18 Apr 2024 09:39:37 +0200 (CEST) From: Richard Biener To: Jakub Jelinek cc: gcc-patches@gcc.gnu.org Subject: Re: [PATCH] internal-fn: Temporarily disable flag_trapv during .{ADD,SUB,MUL}_OVERFLOW etc. expansion [PR114753] In-Reply-To: Message-ID: <30286sn9-558r-9s57-2nn0-no14sp837q5r@fhfr.qr> References: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Score: -4.30 X-Spam-Level: X-Spamd-Result: default: False [-4.30 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-0.20)[-0.997]; MIME_GOOD(-0.10)[text/plain]; RCVD_COUNT_ZERO(0.00)[0]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; MISSING_XM_UA(0.00)[]; RCPT_COUNT_TWO(0.00)[2]; FROM_HAS_DN(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FROM_EQ_ENVFROM(0.00)[]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:email] X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS,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 List-Id: On Thu, 18 Apr 2024, Jakub Jelinek wrote: > 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? OK. Thanks, Richard. > 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. > > --- 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; > +} > > Jakub > > -- Richard Biener SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, Germany; GF: Ivo Totev, Andrew McDonald, Werner Knoblich; (HRB 36809, AG Nuernberg)