From patchwork Sun Aug 30 21:20:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 1354030 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=Or0MMW/r; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BfmT63SpKz9sSP for ; Mon, 31 Aug 2020 07:20:25 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0684C3857C42; Sun, 30 Aug 2020 21:20:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0684C3857C42 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1598822421; bh=CWjHubK4jFcpPTnka+rP4vE4Aa1orNf9zeMlq9UwHKo=; h=Subject:To:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Or0MMW/rd7p6YD05DJ6kO8lwAPnr64molArPE/IzfplZhT0UBXU6zG6FiIgj2IKvo 0WzvRjCGl2AdjxwjTdyzrtC2u96UcCZteLvK6RlOgz5uMGcXhu2RxXKw8dnbPf9das /0ITn/xPsNiIYK7uCXiBiMH3jzdJmmo1R+3shKWM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-il1-x12a.google.com (mail-il1-x12a.google.com [IPv6:2607:f8b0:4864:20::12a]) by sourceware.org (Postfix) with ESMTPS id A06BA3858D35 for ; Sun, 30 Aug 2020 21:20:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A06BA3858D35 Received: by mail-il1-x12a.google.com with SMTP id o16so4919235ilq.0 for ; Sun, 30 Aug 2020 14:20:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:message-id:date:user-agent :mime-version:content-language; bh=CWjHubK4jFcpPTnka+rP4vE4Aa1orNf9zeMlq9UwHKo=; b=WRYy4jyWfgPubErLorgtRNNMSLYIUouaCOATf2i91RXDWMfINcZAbTu5Wvi2BsqBPO b+NtzV9asYejKe1zy7aRxSjdpwfOLQmYc7oE0OxFLc/0OFmS9rf6/S1nrhbbI2ZDk8fG MYkb+XQpubvkCqQsD1Vr7B31sItm/TrZbH5XMeN8gv/ljkJTsLcIEA6h4A6l8Si2TRZn PR4xd/x/1keVWnMxMVSK4597h9JXGGgXzScorwtg8kvOO1601IhyPytn41hILrMnQ9Ft UVX5fkgJww+Qpb2+EzzaXgLu2roCCIp8PL9rzU153OSYIXB1kQqHCh/P8QRgjdykVzLJ 5Fzg== X-Gm-Message-State: AOAM530uMiMNf9JnZnagC2WxdeVAdy4kbKut0do5CQvW34HIh2IBPwE5 qvp1muOuOkiETpUV5iPdkLxuTyqSKrQ= X-Google-Smtp-Source: ABdhPJzQS+71UGjjqsiVuzj5yB9EcIL96tJ0QDqXMqTPBsfEheec4b2fdt2G90niYAHhu3GRsM/fCQ== X-Received: by 2002:a92:d386:: with SMTP id o6mr6767627ilo.292.1598822416892; Sun, 30 Aug 2020 14:20:16 -0700 (PDT) Received: from [192.168.0.41] (174-16-103-228.hlrn.qwest.net. [174.16.103.228]) by smtp.gmail.com with ESMTPSA id k18sm3426711ils.42.2020.08.30.14.20.15 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 30 Aug 2020 14:20:16 -0700 (PDT) Subject: [committed] use get_size_range instead of get_range to obtain range of valid sizes To: gcc-patches Message-ID: <980f35fb-f5bd-b3d6-d338-97974321bf3a@gmail.com> Date: Sun, 30 Aug 2020 15:20:15 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 Content-Language: en-US X-Spam-Status: No, score=-7.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Martin Sebor via Gcc-patches From: Martin Sebor Reply-To: Martin Sebor Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" The get_size_range() function is more appropriate to call than get_range() in contexts where the range of object sizes is needed because (as also mentioned in the patch for PR 92942 I submitted last Friday) it has the necessary logic to constrain the range to just the values that are valid for object sizes. The attached change makes use of get_size_range() to get consistent results regardless of the data model (ILP32 vs LP64) and avoid failures on ILP32 in a couple of tests I recently committed, as pointed out in: https://gcc.gnu.org/pipermail/gcc-patches/2020-August/552956.html https://gcc.gnu.org/pipermail/gcc-patches/2020-August/552961.html I have committed the change in r11-2941. Martin Use get_size_range instead of get_range to obtain range of valid sizes. gcc/ChangeLog: * builtins.c (access_ref::access_ref): Call get_size_range instead of get_range. gcc/testsuite/ChangeLog: * gcc.dg/Wstringop-overread-3.c: New test. diff --git a/gcc/builtins.c b/gcc/builtins.c index df121f98b95..bc35b071f02 100644 --- a/gcc/builtins.c +++ b/gcc/builtins.c @@ -214,8 +214,13 @@ access_ref::access_ref (tree bound /* = NULL_TREE */, /* When BOUND is nonnull and a range can be extracted from it, set the bounds of the access to reflect both it and MINACCESS. BNDRNG[0] is the size of the minimum access. */ - if (bound && get_range (bound, UNSIGNED, bndrng)) - bndrng[0] = bndrng[0] > 0 && minaccess ? 1 : 0; + tree rng[2]; + if (bound && get_size_range (bound, rng, true)) + { + bndrng[0] = wi::to_offset (rng[0]); + bndrng[1] = wi::to_offset (rng[1]); + bndrng[0] = bndrng[0] > 0 && minaccess ? 1 : 0; + } } /* Return true if NAME starts with __builtin_ or __sync_. */ diff --git a/gcc/testsuite/gcc.dg/Wstringop-overread-3.c b/gcc/testsuite/gcc.dg/Wstringop-overread-3.c new file mode 100644 index 00000000000..6c2c6b6a29d --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wstringop-overread-3.c @@ -0,0 +1,188 @@ +/* Verify that calling strndup and strnlen with an unknown bound isn't + diagnosed regardless of the size of the array and the type of the bound. + { dg-do compile } + { dg-options "-O -Wall" } */ + +#define NOIPA __attribute__ ((noipa)) + +typedef __SIZE_TYPE__ size_t; + +extern char* strndup (const char*, size_t); +extern size_t strnlen (const char*, size_t); + +/* TO DO: Passing a zero-length array to any function is almost certainly + a bug and should be diagnosed except perpaphs when the function also + takes a bound and its value is known to be zero. When this is + implemented this test will need to be adjusted. */ +extern char a0[0]; + +extern char a1[1]; + +NOIPA char* strndup_a0_si (short n) +{ + return strndup (a0, n); +} + +NOIPA char* strndup_a0_i (int n) +{ + return strndup (a0, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + +NOIPA char* strndup_a0_li (long n) +{ + return strndup (a0, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + +NOIPA char* strndup_a0_lli (long long n) +{ + return strndup (a0, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + + +NOIPA char* strndup_a0_usi (unsigned short n) +{ + return strndup (a0, n); +} + +NOIPA char* strndup_a0_ui (unsigned n) +{ + return strndup (a0, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + +NOIPA char* strndup_a0_uli (unsigned long n) +{ + return strndup (a0, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + +NOIPA char* strndup_a0_ulli (unsigned long long n) +{ + return strndup (a0, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + + + +NOIPA char* strndup_a1_si (short n) +{ + return strndup (a1, n); +} + +NOIPA char* strndup_a1_i (int n) +{ + return strndup (a1, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + +NOIPA char* strndup_a1_li (long n) +{ + return strndup (a1, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + +NOIPA char* strndup_a1_lli (long long n) +{ + return strndup (a1, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + + +NOIPA char* strndup_a1_usi (unsigned short n) +{ + return strndup (a1, n); +} + +NOIPA char* strndup_a1_ui (unsigned n) +{ + return strndup (a1, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + +NOIPA char* strndup_a1_uli (unsigned long n) +{ + return strndup (a1, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + +NOIPA char* strndup_a1_ulli (unsigned long long n) +{ + return strndup (a1, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + + +NOIPA size_t strnlen_a0_si (short n) +{ + return strnlen (a0, n); +} + +NOIPA size_t strnlen_a0_i (int n) +{ + return strnlen (a0, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + +NOIPA size_t strnlen_a0_li (long n) +{ + return strnlen (a0, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + +NOIPA size_t strnlen_a0_lli (long long n) +{ + return strnlen (a0, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + + +NOIPA size_t strnlen_a0_usi (unsigned short n) +{ + return strnlen (a0, n); +} + +NOIPA size_t strnlen_a0_ui (unsigned n) +{ + return strnlen (a0, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + +NOIPA size_t strnlen_a0_uli (unsigned long n) +{ + return strnlen (a0, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + +NOIPA size_t strnlen_a0_ulli (unsigned long long n) +{ + return strnlen (a0, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + + + +NOIPA size_t strnlen_a1_si (short n) +{ + return strnlen (a1, n); +} + +NOIPA size_t strnlen_a1_i (int n) +{ + return strnlen (a1, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + +NOIPA size_t strnlen_a1_li (long n) +{ + return strnlen (a1, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + +NOIPA size_t strnlen_a1_lli (long long n) +{ + return strnlen (a1, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + + +NOIPA size_t strnlen_a1_usi (unsigned short n) +{ + return strnlen (a1, n); +} + +NOIPA size_t strnlen_a1_ui (unsigned n) +{ + return strnlen (a1, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + +NOIPA size_t strnlen_a1_uli (unsigned long n) +{ + return strnlen (a1, n); // { dg-bogus "\\\[-Wstringop-overread" } +} + +NOIPA size_t strnlen_a1_ulli (unsigned long long n) +{ + return strnlen (a1, n); // { dg-bogus "\\\[-Wstringop-overread" } +}