From patchwork Wed Nov 25 00:45:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 1405786 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=pass (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=e0RR/wJe; 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 4Cghxs3Pc5z9sSf for ; Wed, 25 Nov 2020 11:45:18 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F18BA385042E; Wed, 25 Nov 2020 00:45:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F18BA385042E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1606265115; bh=UBL521TWGI+cm7T2k8gR1hU9LG+62dAj9OtdJIHIn74=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=e0RR/wJeth7aNkiVmZGzNaliODrE4yjpXPZcrEXyXGDdSmnzYeNPIgXnea84qeooY K820VeffxE40VdY4giiNKTDcWUmmLioYm4LIgcyIDjXzRKWfh2N8Bs3p7z8OSaLsDh H+glM8Q5YV4pkBDUyRBkZa6/QOnYNsXYNeAHjVu8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qt1-x841.google.com (mail-qt1-x841.google.com [IPv6:2607:f8b0:4864:20::841]) by sourceware.org (Postfix) with ESMTPS id 66E653857804 for ; Wed, 25 Nov 2020 00:45:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 66E653857804 Received: by mail-qt1-x841.google.com with SMTP id t5so523501qtp.2 for ; Tue, 24 Nov 2020 16:45:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=UBL521TWGI+cm7T2k8gR1hU9LG+62dAj9OtdJIHIn74=; b=jsL1XQoXRz8vntFDuzZ2J+W3n5uIRbjBoFT1jQbqUtetfX7iE4r3P87kEFv4bMAk5i maDRISHH7vq6NEJjLWCe8wSBVLa9A/5bXAHgPeLVDPdOwSlyD5ZBToJO0ga7sw3jsw7b XjS49k21vnJ2M/4Pw/mjAK3fYijIGP9dyK2RCa6NJ/56AQkin7O8sk2iI6dPy+WXIKNA B95hdTdVVHNVp1OcffS6rZ1ZmKw67ajJpUgxM8fJoTLplXT06Uaw45czmje7ES0EnuXn 7zOtjLRNhcu9eHp3ZVY8uLMPuw0gG6JW+81Jy29absd6fzEvN/sZcCBfOYK/oYiwae3V 5aMw== X-Gm-Message-State: AOAM533tI0pPC+Qcpws6KIjFR8RApQpVjA3/g8W7Vf174zBdFHnNjXzh QMw8huHDzXwv4AmkBswB69wnwoJQPjo= X-Google-Smtp-Source: ABdhPJwrjZt2/7uidUcyEPV4+EdL32T5fabO4JgddqyWml1eIdsta+n3/l9DFlteJvWJ2aC7XhiKXg== X-Received: by 2002:ac8:7749:: with SMTP id g9mr809003qtu.303.1606265111807; Tue, 24 Nov 2020 16:45:11 -0800 (PST) Received: from [192.168.0.41] (75-166-106-198.hlrn.qwest.net. [75.166.106.198]) by smtp.gmail.com with ESMTPSA id s3sm891057qtd.49.2020.11.24.16.45.10 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 24 Nov 2020 16:45:11 -0800 (PST) To: gcc-patches Subject: [PATCH] make POINTER_PLUS offset sizetype (PR 97956) Message-ID: <10756ea2-c660-9add-b60f-f3cf49260e38@gmail.com> Date: Tue, 24 Nov 2020 17:45:10 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 Content-Language: en-US X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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" Offsets in pointer expressions are signed but GCC prefers to represent them as sizetype instead, and sometimes (though not always) crashes during GIMPLE verification when they're not. The sometimes-but-not-always part makes it easy for mistakes to slip in and go undetected for months, until someone either trips over it by accident, or deliberately tries to break things (the test case in the bug relies on declaring memchr with the third argument of type signed long which is what's apparently needed to trigger the ICE). The attached patch corrects a couple of such mistakes. Martin PS It would save us the time and effort dealing with these bugs to either detect (or even correct) the mistakes early, at the time the POINTER_PLUS_EXPR is built. Adding an assert to gimple_build_assign()) to verify that it has the expected type (or converting the operand to sizetype) as in the change below does that. I'm pretty sure I submitted a patch like it in the past but it was rejected. If I'm wrong or if there are no objections to it now I'll be happy to commit it as well. Both patches were tested on x86_64-linux. PR middle-end/97956 - ICE due to type mismatch in pointer_plus_expr during memchr folding gcc/ChangeLog: PR middle-end/97956 * expr.c (constant_byte_string): Use sizetype for pointer offsets. * gimple-fold.c (gimple_fold_builtin_memchr): Ditto. gcc/testsuite/ChangeLog: PR middle-end/97956 * gcc.dg/memchr-3.c: New test. diff --git a/gcc/expr.c b/gcc/expr.c index 25e93b6d46f..b2197f613c7 100644 --- a/gcc/expr.c +++ b/gcc/expr.c @@ -11893,7 +11893,7 @@ constant_byte_string (tree arg, tree *ptr_offset, tree *mem_size, tree *decl, init = TREE_OPERAND (init, 0); *mem_size = size_int (TREE_STRING_LENGTH (init)); - *ptr_offset = wide_int_to_tree (ssizetype, base_off); + *ptr_offset = wide_int_to_tree (sizetype, base_off); if (decl) *decl = array; diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c index 905c0a057cb..2e98a2c70cd 100644 --- a/gcc/gimple-fold.c +++ b/gcc/gimple-fold.c @@ -2689,7 +2689,7 @@ gimple_fold_builtin_memchr (gimple_stmt_iterator *gsi) gimple_seq stmts = NULL; if (lhs != NULL_TREE) { - tree offset_cst = build_int_cst (TREE_TYPE (len), offset); + tree offset_cst = build_int_cst (sizetype, offset); gassign *stmt = gimple_build_assign (lhs, POINTER_PLUS_EXPR, arg1, offset_cst); gimple_seq_add_stmt_without_update (&stmts, stmt); diff --git a/gcc/testsuite/gcc.dg/memchr-3.c b/gcc/testsuite/gcc.dg/memchr-3.c new file mode 100644 index 00000000000..c1f4e9e10dc --- /dev/null +++ b/gcc/testsuite/gcc.dg/memchr-3.c @@ -0,0 +1,25 @@ +/* PR middle-end/97956 - ICE due to type mismatch in pointer_plus_expr + during memchr folding + { dg-do compile } + { dg-options "-O2 -Wall" } */ + +typedef __INT8_TYPE__ int8_t; +typedef __INT32_TYPE__ int32_t; + +extern void* memchr (const void*, int, long); + +struct SX +{ + int32_t n; + int8_t a[]; +}; + +const struct SX sx = { 0x1221 }; +const char sx_rep[] = { }; + +void test_find (void) +{ + int n = 0, nb = (const char*)&sx.a - (const char*)&sx; + const char *p = (const char*)&sx, *q = sx_rep; + n += p + 1 == memchr (p, q[1], nb); +}