From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id C9D183857B8E; Wed, 15 Mar 2023 18:35:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C9D183857B8E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678905305; bh=qhU0iGV5wsrSVMt1g8xBsQGpW17vys8jmGZK01loCzI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=I0scfoS0hAAKh+E0HCAFL9i4BZ+a5CsNI8gnU4S6b1EBpbpXc9tlRR0Rq7pPjPnqp VE1HRX9UUx+rGJPwFzVLn8yeCDOMNLnDW0Vh8gRTGfZ1dmm/j3kKIpzW+5K/m0de2Z G9Qf2UgdMt+r5uIlQYqp11N9EUUzxLl5E+N3DFt8= From: "cvs-commit at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug sanitizer/108060] [10/11/12 Regression] UBsan missed an out-of-bound bug at -O0 since r7-1900-g8a1b7b7fd75a3847 Date: Wed, 15 Mar 2023 18:35:05 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: sanitizer X-Bugzilla-Version: 13.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: cvs-commit at gcc dot gnu.org X-Bugzilla-Status: ASSIGNED X-Bugzilla-Resolution: X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: mpolacek at gcc dot gnu.org X-Bugzilla-Target-Milestone: 10.5 X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D108060 --- Comment #10 from CVS Commits --- The releases/gcc-12 branch has been updated by Marek Polacek : https://gcc.gnu.org/g:94af33aa4da07269cb4a6645da9f7ddf8d1bad69 commit r12-9264-g94af33aa4da07269cb4a6645da9f7ddf8d1bad69 Author: Marek Polacek Date: Wed Mar 8 09:15:07 2023 -0500 ubsan: missed -fsanitize=3Dbounds for compound ops [PR108060] In this PR we are dealing with a missing .UBSAN_BOUNDS, so the out-of-bounds access in the test makes the program crash before a UBSan diagnostic was emitted. In C and C++, c_genericize gets a[b] =3D a[b] | c; but in C, both a[b] are one identical shared tree (not in C++ because cp_fold/ARRAY_REF created two same but not identical trees). Since ubsan_walk_array_refs_r keeps a pset, in C we produce a[.UBSAN_BOUNDS (0B, SAVE_EXPR , 8);, SAVE_EXPR ;] =3D a[b] | c; because the LHS is walked before the RHS. Since r7-1900, we gimplify the RHS before the LHS. So the statement ab= ove gets gimplified into _1 =3D a[b]; c.0_2 =3D c; b.1 =3D b; .UBSAN_BOUNDS (0B, b.1, 8); With this patch we produce: a[b] =3D a[.UBSAN_BOUNDS (0B, SAVE_EXPR , 8);, SAVE_EXPR ;] | c; which gets gimplified into: b.0 =3D b; .UBSAN_BOUNDS (0B, b.0, 8); _1 =3D a[b.0]; therefore we emit a runtime error before making the bad array access. I think it's OK that only the RHS gets a .UBSAN_BOUNDS, as in few lines above: the instrumented array access dominates the array access on the LHS, and I've verified that b =3D 0; a[b] =3D (a[b], b =3D -32768, a[0] | c); works as expected: the inner a[b] is OK but we do emit an error for the a[b] on the LHS. For GCC 14, we could apply since the copy_node doesn't seem to be needed. PR sanitizer/108060 PR sanitizer/109050 gcc/c-family/ChangeLog: * c-gimplify.cc (ubsan_walk_array_refs_r): For a MODIFY_EXPR, instrument the RHS before the LHS. gcc/testsuite/ChangeLog: * c-c++-common/ubsan/bounds-17.c: New test. * c-c++-common/ubsan/bounds-18.c: New test. * c-c++-common/ubsan/bounds-19.c: New test. * c-c++-common/ubsan/bounds-20.c: New test. * c-c++-common/ubsan/bounds-21.c: New test. (cherry picked from commit 4d0baeae315ebe7d0ec7682ea3e7c0516027c2b8)=