From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 34170385735E; Wed, 15 Mar 2023 18:35:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 34170385735E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678905306; bh=ograplJgK2G35eNxBSm4wIU4fUUezcF7lKa7Tg6P0/A=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Olypdc9X87Hb+u7c8LQLklMrNiR5PR87VJoaXtOdq6lE06qDK4ry4RgB3fOie5ijZ LRBUn7hq/xeFPIVUIXot7Rvktj5iEnv0vtCoY8sl4ER0DlrAaC3/QlWCPb/18m7xUi fC5ngWxuzQ57lZ0hxxkcKzs7vUGDzZ00nHQQhhZg= 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: 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: 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 #3 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)=