From patchwork Wed Oct 2 23:15:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 1170947 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-510105-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="ryhu6uh+"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="n+3fIdHg"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46kBpP2PMbz9sN1 for ; Thu, 3 Oct 2019 09:16:10 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=LxPOQZXDb65e9pDZI0qBaVSzDEk7EIR7daQKupqM9l0TZMB6L1 5p/3aoX2Ft56Kh7HIvu0Zj0nX6yXCaFOtQ6cLL0UclqUeEkRFoY3AZ66Nhb+u+wP N382fNtQSiktMVtjqjEM80cnGBG1qImrwGhu52iVN9TH7Sx6VAuDuiF+4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=ggMWotRDDyZPSCK/H85tqlqU15Y=; b=ryhu6uh+m2RMYU+c9p7T qB8yjefSdj+7O1jBntxwxB8MKorakAF+iCkDqpf0WuYtaJTTLxjyZc6U3M9XXlY6 9JkrhViETu9mK6dBH4eID3iSrC05NWK9+JEwZQsNzJFLEgKrDCm/g2ufnc8Rvr4U qjEmEWnca7iV2Q4y6DkQyKY= Received: (qmail 46307 invoked by alias); 2 Oct 2019 23:16:03 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 45493 invoked by uid 89); 2 Oct 2019 23:15:54 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-12.1 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: mail-io1-f49.google.com Received: from mail-io1-f49.google.com (HELO mail-io1-f49.google.com) (209.85.166.49) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 02 Oct 2019 23:15:52 +0000 Received: by mail-io1-f49.google.com with SMTP id n26so1207558ioj.8 for ; Wed, 02 Oct 2019 16:15:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-language; bh=w7wGiZZXduvqznIj3ohEY1LdJ+041z3+bbP+UeFLqqk=; b=n+3fIdHgH6hP3+wXvL/Q9odkL2/H90kDq7GJttVZ5FieXj2Tk9Am11uiy2pi6UlIPF FLyqM4RJr6T+8+aec3utEWZRBD5g6C36BpScwfQ0I9GZfvhfyveU3/1PkalNYLR8TvCs hdDDgJYDiv9fL5XzZYg6BzNj3VzlMp11MyoPccHOLthMBetbQZeC2/+kLU4UjfktIIeS ayHPgaKmNlNIv/F0NP7OZuvIiL0PyKkL4HmFMCbIYaaQdoz2a+TbC6/7hpHHKc7TfvJ6 P2QSeMvQvnscOL1achuYBIjp4lGAzjdLz8pK1vztFIKiCR+kAbK6sgHXjaV5gsPgPiqW oXRg== Received: from [192.168.0.41] ([71.218.12.255]) by smtp.gmail.com with ESMTPSA id u25sm463995ill.4.2019.10.02.16.15.48 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 02 Oct 2019 16:15:49 -0700 (PDT) To: gcc-patches From: Martin Sebor Subject: [PATCH] mention referenced object in more -Wstringop-overflow instances Message-ID: Date: Wed, 2 Oct 2019 17:15:48 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 X-IsSubscribed: yes The attached patch adds an optional argument to compute_builtin_object_size to let it return the DECL of the object whose size it's called to compute. This lets -Wstringop-overflow point to the object in more instances of the warning than it does now (but by no means all of them -- more work is needed to make that happen). This can be helpful when the object is declared in a different function than the access. Tested on x86_64-linux. Martin gcc/ChangeLog: * builtins.c (compute_objsize): Add an argument. * tree-object-size.c (addr_object_size): Same. (compute_builtin_object_size): Same. * tree-object-size.h (compute_builtin_object): Same. gcc/testsuite/ChangeLog: * gcc.dg/Wstringop-overflow-17.c: New test. Index: gcc/builtins.c =================================================================== --- gcc/builtins.c (revision 276472) +++ gcc/builtins.c (working copy) @@ -3587,7 +3587,7 @@ compute_objsize (tree dest, int ostype, tree *pdec /* Only the two least significant bits are meaningful. */ ostype &= 3; - if (compute_builtin_object_size (dest, ostype, &size)) + if (compute_builtin_object_size (dest, ostype, &size, pdecl)) return build_int_cst (sizetype, size); if (TREE_CODE (dest) == SSA_NAME) Index: gcc/tree-object-size.h =================================================================== --- gcc/tree-object-size.h (revision 276472) +++ gcc/tree-object-size.h (working copy) @@ -22,6 +22,7 @@ along with GCC; see the file COPYING3. If not see extern void init_object_sizes (void); extern void fini_object_sizes (void); -extern bool compute_builtin_object_size (tree, int, unsigned HOST_WIDE_INT *); +extern bool compute_builtin_object_size (tree, int, unsigned HOST_WIDE_INT *, + tree * = NULL); #endif // GCC_TREE_OBJECT_SIZE_H Index: gcc/tree-object-size.c =================================================================== --- gcc/tree-object-size.c (revision 276491) +++ gcc/tree-object-size.c (working copy) @@ -54,7 +54,8 @@ static const unsigned HOST_WIDE_INT unknown[4] = { static tree compute_object_offset (const_tree, const_tree); static bool addr_object_size (struct object_size_info *, - const_tree, int, unsigned HOST_WIDE_INT *); + const_tree, int, unsigned HOST_WIDE_INT *, + tree * = NULL); static unsigned HOST_WIDE_INT alloc_object_size (const gcall *, int); static tree pass_through_call (const gcall *); static void collect_object_sizes_for (struct object_size_info *, tree); @@ -172,10 +173,15 @@ compute_object_offset (const_tree expr, const_tree static bool addr_object_size (struct object_size_info *osi, const_tree ptr, - int object_size_type, unsigned HOST_WIDE_INT *psize) + int object_size_type, unsigned HOST_WIDE_INT *psize, + tree *pdecl /* = NULL */) { tree pt_var, pt_var_size = NULL_TREE, var_size, bytes; + tree dummy; + if (!pdecl) + pdecl = &dummy; + gcc_assert (TREE_CODE (ptr) == ADDR_EXPR); /* Set to unknown and overwrite just before returning if the size @@ -195,7 +201,7 @@ addr_object_size (struct object_size_info *osi, co || TREE_CODE (TREE_OPERAND (pt_var, 0)) != SSA_NAME) { compute_builtin_object_size (TREE_OPERAND (pt_var, 0), - object_size_type & ~1, &sz); + object_size_type & ~1, &sz, pdecl); } else { @@ -232,7 +238,10 @@ addr_object_size (struct object_size_info *osi, co && DECL_P (pt_var) && tree_fits_uhwi_p (DECL_SIZE_UNIT (pt_var)) && tree_to_uhwi (DECL_SIZE_UNIT (pt_var)) < offset_limit) - pt_var_size = DECL_SIZE_UNIT (pt_var); + { + *pdecl = pt_var; + pt_var_size = DECL_SIZE_UNIT (pt_var); + } else if (pt_var && TREE_CODE (pt_var) == STRING_CST && TYPE_SIZE_UNIT (TREE_TYPE (pt_var)) @@ -478,13 +487,16 @@ pass_through_call (const gcall *call) /* Compute __builtin_object_size value for PTR and set *PSIZE to - the resulting value. OBJECT_SIZE_TYPE is the second argument - to __builtin_object_size. Return true on success and false - when the object size could not be determined. */ + the resulting value. If the declared object is known and PDECL + is nonnull, sets *PDECL to the object's DECL. OBJECT_SIZE_TYPE + is the second argument to __builtin_object_size. + Returns true on success and false when the object size could not + be determined. */ bool compute_builtin_object_size (tree ptr, int object_size_type, - unsigned HOST_WIDE_INT *psize) + unsigned HOST_WIDE_INT *psize, + tree *pdecl /* = NULL */) { gcc_assert (object_size_type >= 0 && object_size_type <= 3); @@ -496,7 +508,7 @@ compute_builtin_object_size (tree ptr, int object_ init_offset_limit (); if (TREE_CODE (ptr) == ADDR_EXPR) - return addr_object_size (NULL, ptr, object_size_type, psize); + return addr_object_size (NULL, ptr, object_size_type, psize, pdecl); if (TREE_CODE (ptr) != SSA_NAME || !POINTER_TYPE_P (TREE_TYPE (ptr))) @@ -520,7 +532,8 @@ compute_builtin_object_size (tree ptr, int object_ ptr = gimple_assign_rhs1 (def); if (tree_fits_shwi_p (offset) - && compute_builtin_object_size (ptr, object_size_type, psize)) + && compute_builtin_object_size (ptr, object_size_type, + psize, pdecl)) { /* Return zero when the offset is out of bounds. */ unsigned HOST_WIDE_INT off = tree_to_shwi (offset); Index: gcc/testsuite/gcc.dg/Wstringop-overflow-17.c =================================================================== --- gcc/testsuite/gcc.dg/Wstringop-overflow-17.c (nonexistent) +++ gcc/testsuite/gcc.dg/Wstringop-overflow-17.c (working copy) @@ -0,0 +1,20 @@ +/* Test to verify that -Wstringop-overflow mentions the referenced object + i. + { dg-do compile } + { dg-options "-O2 -Wall" } */ + +static void copy_n (char *d, const char *s, int n) +{ + while (n--) + *d++ = *s++; + *d = 0; // { dg-warning "writing 1 byte into a region of size 0" } +} + +void sink (void*); + +void call_copy_n (const char *s) +{ + char a[3]; // { dg-message "destination object declared here" } + copy_n (a, "1234567", 7); + sink (a); +}