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 D32253858D33 for ; Tue, 28 Feb 2023 09:11:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D32253858D33 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=1677575509; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references; bh=Hi7aN51DpvTJwNdbgmMYZbDaBRp0ik+vfcfQrRKI54g=; b=hLKVaZAsSgfR3bHn68PtZh5SKPEdCSbyYFe8/NnY9KRhlwVE/QuE3Vlv12xL6kFgoTYWUE adVIXPvFhmCESqiHrA5MVXtCnPatEddrpT/QB3eOR9oPmmMftAO9ExphaKw9ejrvYremCP 89Duekb/u3dPljMZQNVWbTmu7S0OBf0= 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-434-iPAs8B8TMeWuBA7hVpa5HA-1; Tue, 28 Feb 2023 04:11:48 -0500 X-MC-Unique: iPAs8B8TMeWuBA7hVpa5HA-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 C80D885D062; Tue, 28 Feb 2023 09:11:47 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.101]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8B8812166B2A; Tue, 28 Feb 2023 09:11:47 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 31S9Bj6D1109149 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 28 Feb 2023 10:11:45 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 31S9BiSM1109148; Tue, 28 Feb 2023 10:11:44 +0100 Date: Tue, 28 Feb 2023 10:11:44 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: Re: [PATCH] ubsan: Honor -fstrict-flex-arrays= in -fsanitize=bounds [PR108894] Message-ID: Reply-To: Jakub Jelinek References: MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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: On Tue, Feb 28, 2023 at 09:02:47AM +0000, Richard Biener wrote: > > While this isn't really a regression, the -fstrict-flex-arrays* > > option is new in GCC 13 and so I think we should make -fsanitize=bounds > > play with it well from the beginning. > > > > The current behavior is that -fsanitize=bounds considers all trailing > > arrays as flexible member-like arrays and both -fsanitize=bounds and > > -fsanitize=bounds-strict because of a bug don't even instrument > > [0] arrays at all, not as trailing nor when followed by other members. > > > > I think -fstrict-flex-arrays* options can be considered as language > > mode changing options, by default flexible member-like arrays are > > handled like flexible arrays, but that option can change the set of > > the arrays which are treated like that. So, -fsanitize=bounds should > > change with that on what is considered acceptable and what isn't. > > While -fsanitize=bounds-strict should reject them all always to > > continue previous behavior. > > > > The following patch implements that. To support [0] array instrumentation, > > I had to change the meaning of the bounds argument to .UBSAN_BOUNDS, > > previously it was the TYPE_MAX_VALUE of the domain unless ignore_off_by_one > > (used for taking address of the array element rather than accessing it; > > in that case 1 is added to the bound argument) and the later lowered checks > > were if (index > bound) report_failure (). > > The problem with that is that for [0] arrays where (at least for C++) > > the max value is all ones, for accesses that condition will be never true; > > for addresses of elements it was working (in C++) correctly before. > > This patch changes it to add 1 + ignore_off_by_one, so -1 becomes 0 or > > 1 for &array_ref and changing the lowering to be if (index >= bound) > > report_failure (). Furthermore, as C represents the [0] arrays with > > NULL TYPE_MAX_VALUE, I treated those like the C++ ones. > > LGTM. Btw, what does -fsanitize=bounds do for C++ code which lacks > flexible arrays? Does it treat all trailing arrays as fixed? As -fstrict-flex-arrays* options and strict_flex_array attribute are basically ignored right now for C++, I've kept the previous behavior for C++ (except for fixing handling of [0] arrays), which is that it like the rest of the compiler treats all trailing arrays as flexible member like (ok, there is a loop looking for nested references, so struct S { int a; struct T { int b; int c[1]; } d; int e; }; that the e member results in c not being treated flexible member-like). If/when -fstrict-flex-arrays* support is added for C++, we'll need to drop one of the !c_dialect_cxx () guards there even in c-ubsan.cc. Jakub