From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 301013858D33 for ; Wed, 8 Mar 2023 21:09:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 301013858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678309779; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=+z6pZ5Qysjj2T9waJ8+oUI5DDNH57eZY+VHWxE7pb/c=; b=axq2XInD+rCIeNGejmM7nH0c5DMKCitgrmXtkVjzcBUy7ghg1e5tk+uRr2i9C5/lgDhL4y Mq48mDE5yYFcjGZAcRBTnUUxo45aS8HQ4N8ahuepZkptIPo4mZa726YnLuGHqPpD9zqvIL uasgl4PDDzdToQ2vM5VAMpE4SGg9j84= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-212-eAfxJHvaPqi8U4r-_Zgp0w-1; Wed, 08 Mar 2023 16:09:36 -0500 X-MC-Unique: eAfxJHvaPqi8U4r-_Zgp0w-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EC35D85A588; Wed, 8 Mar 2023 21:09:35 +0000 (UTC) Received: from pdp-11.lan (unknown [10.22.17.115]) by smtp.corp.redhat.com (Postfix) with ESMTP id C3DB72166B26; Wed, 8 Mar 2023 21:09:35 +0000 (UTC) From: Marek Polacek To: GCC Patches Cc: Jakub Jelinek , Richard Biener Subject: [PATCH] ubsan: missed -fsanitize=bounds for compound ops [PR108060] Date: Wed, 8 Mar 2023 16:09:30 -0500 Message-Id: <20230308210930.128620-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,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: 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] = 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 ;] = a[b] | c; because the LHS is walked before the RHS. Since r7-1900, we gimplify the RHS before the LHS. So the statement above gets gimplified into _1 = a[b]; c.0_2 = c; b.1 = b; .UBSAN_BOUNDS (0B, b.1, 8); With this patch we produce: a[b] = a[.UBSAN_BOUNDS (0B, SAVE_EXPR , 8);, SAVE_EXPR ;] | c; which gets gimplified into: b.0 = b; .UBSAN_BOUNDS (0B, b.0, 8); _1 = 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 = 0; a[b] = (a[b], b = -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. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/12? 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. --- gcc/c-family/c-gimplify.cc | 12 ++++++++++++ gcc/testsuite/c-c++-common/ubsan/bounds-17.c | 17 +++++++++++++++++ gcc/testsuite/c-c++-common/ubsan/bounds-18.c | 17 +++++++++++++++++ gcc/testsuite/c-c++-common/ubsan/bounds-19.c | 20 ++++++++++++++++++++ gcc/testsuite/c-c++-common/ubsan/bounds-20.c | 16 ++++++++++++++++ 5 files changed, 82 insertions(+) create mode 100644 gcc/testsuite/c-c++-common/ubsan/bounds-17.c create mode 100644 gcc/testsuite/c-c++-common/ubsan/bounds-18.c create mode 100644 gcc/testsuite/c-c++-common/ubsan/bounds-19.c create mode 100644 gcc/testsuite/c-c++-common/ubsan/bounds-20.c diff --git a/gcc/c-family/c-gimplify.cc b/gcc/c-family/c-gimplify.cc index 74b276b2b26..ef5c7d919fc 100644 --- a/gcc/c-family/c-gimplify.cc +++ b/gcc/c-family/c-gimplify.cc @@ -106,6 +106,18 @@ ubsan_walk_array_refs_r (tree *tp, int *walk_subtrees, void *data) } else if (TREE_CODE (*tp) == ARRAY_REF) ubsan_maybe_instrument_array_ref (tp, false); + else if (TREE_CODE (*tp) == MODIFY_EXPR) + { + /* Since r7-1900, we gimplify RHS before LHS. Consider + a[b] |= c; + wherein we can have a single shared tree a[b] in both LHS and RHS. + If we only instrument the LHS and the access is invalid, the program + could crash before emitting a UBSan error. So instrument the RHS + first. */ + *walk_subtrees = 0; + walk_tree (&TREE_OPERAND (*tp, 1), ubsan_walk_array_refs_r, pset, pset); + walk_tree (&TREE_OPERAND (*tp, 0), ubsan_walk_array_refs_r, pset, pset); + } return NULL_TREE; } diff --git a/gcc/testsuite/c-c++-common/ubsan/bounds-17.c b/gcc/testsuite/c-c++-common/ubsan/bounds-17.c new file mode 100644 index 00000000000..b727e3235b8 --- /dev/null +++ b/gcc/testsuite/c-c++-common/ubsan/bounds-17.c @@ -0,0 +1,17 @@ +/* PR sanitizer/108060 */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=bounds" } */ +/* { dg-skip-if "" { *-*-* } "-flto" } */ +/* { dg-shouldfail "ubsan" } */ + +int a[8]; +int c; + +int +main () +{ + int b = -32768; + a[b] |= c; +} + +/* { dg-output "index -32768 out of bounds for type 'int \\\[8\\\]'" } */ diff --git a/gcc/testsuite/c-c++-common/ubsan/bounds-18.c b/gcc/testsuite/c-c++-common/ubsan/bounds-18.c new file mode 100644 index 00000000000..556abc0e1c0 --- /dev/null +++ b/gcc/testsuite/c-c++-common/ubsan/bounds-18.c @@ -0,0 +1,17 @@ +/* PR sanitizer/108060 */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=bounds" } */ +/* { dg-skip-if "" { *-*-* } "-flto" } */ +/* { dg-shouldfail "ubsan" } */ + +int a[8]; +int c; + +int +main () +{ + int b = -32768; + a[b] = a[b] | c; +} + +/* { dg-output "index -32768 out of bounds for type 'int \\\[8\\\]'" } */ diff --git a/gcc/testsuite/c-c++-common/ubsan/bounds-19.c b/gcc/testsuite/c-c++-common/ubsan/bounds-19.c new file mode 100644 index 00000000000..54217ae399f --- /dev/null +++ b/gcc/testsuite/c-c++-common/ubsan/bounds-19.c @@ -0,0 +1,20 @@ +/* PR sanitizer/108060 */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=bounds" } */ +/* { dg-skip-if "" { *-*-* } "-flto" } */ +/* { dg-shouldfail "ubsan" } */ + +int a[8]; +int a2[18]; +int c; + +int +main () +{ + int b = 0; + a[0] = (a2[b], b = -32768, a[0] | c); + b = 0; + a[b] = (a[b], b = -32768, a[0] | c); +} + +/* { dg-output "index -32768 out of bounds for type 'int \\\[8\\\]'" } */ diff --git a/gcc/testsuite/c-c++-common/ubsan/bounds-20.c b/gcc/testsuite/c-c++-common/ubsan/bounds-20.c new file mode 100644 index 00000000000..a78c67129e0 --- /dev/null +++ b/gcc/testsuite/c-c++-common/ubsan/bounds-20.c @@ -0,0 +1,16 @@ +/* PR sanitizer/109050 */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=bounds -fno-sanitize-recover=all" } */ +/* { dg-shouldfail "ubsan" } */ + +long a; +int b; +int +main () +{ + int c[4] = {0, 1, 2, 3}; + a = 0; + c[a - 9806816] |= b; +} + +/* { dg-output "index -9806816 out of bounds for type 'int \\\[4\\\]'" } */ base-commit: 2e3dd14dd287ca94b72c36ed28a1ae30887f77ce -- 2.39.2