From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by sourceware.org (Postfix) with ESMTPS id 618AF385781F for ; Fri, 17 Mar 2023 09:34:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 618AF385781F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 5C53221A6D; Fri, 17 Mar 2023 09:34:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1679045669; 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=+NnYMD8ZbWH+nJsWPlB+DT8iNSEb5XgFbCwmsP+Nq0w=; b=E6J+o+K1NTfsxsLFl61ybilPhFp5T0wLFlPnwE2h52QOT2sOuMR/dX6D7ID+CqapTxMb/q GWDwJSeS7qn2G8INirCx1DVTLfyi1uIBv8MswDG+t/ySLW2PZoESpNXUm5GR8SvJziz7WF BXIwrEOFnPA2YMShcAy7EJTJYxtBou0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1679045669; 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=+NnYMD8ZbWH+nJsWPlB+DT8iNSEb5XgFbCwmsP+Nq0w=; b=4i3rVzQ2nD7nHLGjmWQI9qQ2FNQMQ9dBQPZ09ZeS0+TNiNNdj4tVM0Cltf3aRk3ivhEB52 kJQXu2RXbz5LYzBw== Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 4170C2C141; Fri, 17 Mar 2023 09:34:29 +0000 (UTC) Date: Fri, 17 Mar 2023 09:34:29 +0000 (UTC) From: Richard Biener To: Jakub Jelinek cc: "Joseph S. Myers" , Marek Polacek , gcc-patches@gcc.gnu.org Subject: Re: [PATCH] c, ubsan: Instrument even shortened divisions [PR109151] In-Reply-To: Message-ID: References: User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-5.0 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 Fri, 17 Mar 2023, Jakub Jelinek wrote: > Hi! > > On the following testcase, the C FE decides to shorten the division because > it has a guarantee that INT_MIN / -1 division won't be encountered, the > first operand is widened from narrower unsigned and/or the second operand is > a constant other than all ones (in this case both are true). > The problem is that the narrower type in this case is _Bool and > ubsan_instrument_division only instruments it if op0's type is INTEGER_TYPE > or REAL_TYPE. Strangely this doesn't happen in C++ FE. > Anyway, we only shorten divisions if the INT_MIN / -1 case is impossible, > so I think we should be fine even with -fstrict-enums in C++ in case it > shortened to ENUMERAL_TYPEs. > > The following patch just instruments those on the ubsan_instrument_division > side. Perhaps only the first hunk and testcase might be needed because > we shouldn't shorten if the other case could be triggered. > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk, > or shall I omit the second hunk? LGTM as-is Richard. > 2023-03-17 Jakub Jelinek > > PR c/109151 > * c-ubsan.cc (ubsan_instrument_division): Handle all scalar integral > types rather than just INTEGER_TYPE. > > * c-c++-common/ubsan/div-by-zero-8.c: New test. > > --- gcc/c-family/c-ubsan.cc.jj 2023-02-28 11:38:28.965868044 +0100 > +++ gcc/c-family/c-ubsan.cc 2023-03-16 09:49:51.651126302 +0100 > @@ -53,7 +53,7 @@ ubsan_instrument_division (location_t lo > op0 = unshare_expr (op0); > op1 = unshare_expr (op1); > > - if (TREE_CODE (type) == INTEGER_TYPE > + if (INTEGRAL_TYPE_P (type) > && sanitize_flags_p (SANITIZE_DIVIDE)) > t = fold_build2 (EQ_EXPR, boolean_type_node, > op1, build_int_cst (type, 0)); > @@ -68,7 +68,7 @@ ubsan_instrument_division (location_t lo > t = NULL_TREE; > > /* We check INT_MIN / -1 only for signed types. */ > - if (TREE_CODE (type) == INTEGER_TYPE > + if (INTEGRAL_TYPE_P (type) > && sanitize_flags_p (SANITIZE_SI_OVERFLOW) > && !TYPE_UNSIGNED (type)) > { > --- gcc/testsuite/c-c++-common/ubsan/div-by-zero-8.c.jj 2023-03-16 10:01:31.626824994 +0100 > +++ gcc/testsuite/c-c++-common/ubsan/div-by-zero-8.c 2023-03-16 10:03:05.510443440 +0100 > @@ -0,0 +1,14 @@ > +/* PR c/109151 */ > +/* { dg-do run } */ > +/* { dg-options "-fsanitize=integer-divide-by-zero -Wno-div-by-zero -fno-sanitize-recover=integer-divide-by-zero" } */ > +/* { dg-shouldfail "ubsan" } */ > + > +int d; > + > +int > +main () > +{ > + d = ((short) (d == 1 | d > 9)) / 0; > +} > + > +/* { dg-output "division by zero" } */ > > Jakub > > -- Richard Biener SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, Germany; GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman; HRB 36809 (AG Nuernberg)