From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 9554C382CD6E; Sun, 19 Mar 2023 05:31:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9554C382CD6E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679203869; bh=scHEl3oKkpBhrNUUvLoRPvgaPKR+hnBd0jwimkk+pEw=; h=From:To:Subject:Date:From; b=itW9OYmiI8bKvFL/f5O2e9reLIIMfdT1BBOX91+gF52NBADJ1KWVEuiwzu9tBAQ3+ VJn7k/Fap7wK3YqmdfF6RTz7z8NFVhtouIuU+TCA4ryhwuql3Eq+r5fnBpb+94EKFY PeBFwASDoTjUf5HDUGjSCmNFWbkmlQgcK8bmvPDA= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-9292] c, ubsan: Instrument even shortened divisions [PR109151] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: d1b61495f60141dd9c8cad4a699d5adcecc56b85 X-Git-Newrev: ffb22189be90216e6af386aaef8b76d2dcd05b84 Message-Id: <20230319053109.9554C382CD6E@sourceware.org> Date: Sun, 19 Mar 2023 05:31:09 +0000 (GMT) List-Id: https://gcc.gnu.org/g:ffb22189be90216e6af386aaef8b76d2dcd05b84 commit r12-9292-gffb22189be90216e6af386aaef8b76d2dcd05b84 Author: Jakub Jelinek Date: Fri Mar 17 16:10:14 2023 +0100 c, ubsan: Instrument even shortened divisions [PR109151] 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. 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. (cherry picked from commit 103d423f6ce72ccb03d55b7b1dfa2dabd5854371) Diff: --- gcc/c-family/c-ubsan.cc | 4 ++-- gcc/testsuite/c-c++-common/ubsan/div-by-zero-8.c | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/gcc/c-family/c-ubsan.cc b/gcc/c-family/c-ubsan.cc index 48f948745f8..4cfb49226b3 100644 --- a/gcc/c-family/c-ubsan.cc +++ b/gcc/c-family/c-ubsan.cc @@ -53,7 +53,7 @@ ubsan_instrument_division (location_t loc, tree op0, tree op1) 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 loc, tree op0, tree op1) 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)) { diff --git a/gcc/testsuite/c-c++-common/ubsan/div-by-zero-8.c b/gcc/testsuite/c-c++-common/ubsan/div-by-zero-8.c new file mode 100644 index 00000000000..02e1d7cfc95 --- /dev/null +++ b/gcc/testsuite/c-c++-common/ubsan/div-by-zero-8.c @@ -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" } */