From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E275B385840D; Fri, 10 Mar 2023 18:25:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E275B385840D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678472734; bh=HofLu73GEWRmBSna9JJqdAgbmboKUvEk1TBJ6lc88Zs=; h=From:To:Subject:Date:In-Reply-To:References:From; b=x9bgT0JzS0Tm74k660OHkg9UxzaSHSUXnUKtI6oASj6TC+3Tdh/TGrQGFoecYg9JF WztMtgRKJYTULg8At2ZuEYmX2jGd8DdCniByQZ/hw+xrOUJTR3nzEoJpKP1V6JQRXR O+Lw3EPLqfJZiz+VyBSr1ZgwCc6QV61gdf1Zthzs= From: "cvs-commit at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug sanitizer/109050] UBsan failed to detect out-of-bound at -O0/1/2/s Date: Fri, 10 Mar 2023 18:25:34 +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: RESOLVED X-Bugzilla-Resolution: DUPLICATE X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- 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=3D109050 --- Comment #2 from CVS Commits --- The trunk branch has been updated by Marek Polacek : https://gcc.gnu.org/g:4d0baeae315ebe7d0ec7682ea3e7c0516027c2b8 commit r13-6593-g4d0baeae315ebe7d0ec7682ea3e7c0516027c2b8 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.=