From patchwork Thu Mar 24 03:09:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1608870 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gotplt.org header.i=@gotplt.org header.a=rsa-sha256 header.s=dreamhost header.b=Zaf35WTW; dkim-atps=neutral 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+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) 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 bilbo.ozlabs.org (Postfix) with ESMTPS id 4KP9Ff4T5tz9sFk for ; Thu, 24 Mar 2022 14:10:12 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AF9183889806 for ; Thu, 24 Mar 2022 03:10:09 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from burlywood.elm.relay.mailchannels.net (burlywood.elm.relay.mailchannels.net [23.83.212.26]) by sourceware.org (Postfix) with ESMTPS id E235D3858C2C for ; Thu, 24 Mar 2022 03:09:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E235D3858C2C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gotplt.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 71C3F802127; Thu, 24 Mar 2022 03:09:54 +0000 (UTC) Received: from pdx1-sub0-mail-a305.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id AF2D280208D; Thu, 24 Mar 2022 03:09:53 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1648091394; a=rsa-sha256; cv=none; b=DfKtTFypuqoAs6y+sGjixPbf6CEqF6+dugWZYkmGWutwdZdQYnu2UBAoeK7mOCh4PNhuGW sWVMCdkQZgl/8MFRPdmBcGAH4kPfvQ3VeP+McC5gCLszK2guQxyT61HTiPAzkmALyEiY10 PwDM0iQN6AHiBilLDFtghyfvtX5vA3lOiCTRbZVz389ppiorZG7wVzTUJ7UtobqOIAnLbY L6dOxe3Lzx0Jh88memHXSEQw1WS5JMFk4GwlQq2Bd7FwnbiiJSO9Q3A1z/5X43ZbzzbG5O XwPpwBJSbVqbvKZaZaqOIbn/V4FwZWsDpXEOXzRKoU5nB7aZLa5r4KTj+nhroQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1648091394; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:dkim-signature; bh=2ZdVZ5f+ZeMD58QDCYKxAXWbmMU1RvJN5RHDnKAJxZQ=; b=JzyPC5cSnTUzY8IKjv7EU6o45nRYWA+eldk5oFIZX9EI4cWbxGh4G835f9LMhgiXXprXOE cdYdnd0myz3Wpfi9h8gOZD0dIzlGoOeo4wm0VecniAjGD8ognGVy7lr+b/QMeZKWgJpn0z kPvjFTnyjxBV3CmRU9wRa13mFwLQ5D0zPsiAS0w6WAyUTaW0800SEcUaI5rmCl81TIzaCS 78peDnZdpNTQXwMfA4xtNnsWk25McWUQ+VR9sPgc+5ng/WwnFTm+E3dvWZXsXPDc59GNPi ij3+1wgHipZRxICt0nPbKNBAhapM6km9CssXlb9OGSHYvtKaqvc7zbTd5XgnVg== ARC-Authentication-Results: i=1; rspamd-548fb75c45-vktpq; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a305.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.107.255.147 (trex/6.5.3); Thu, 24 Mar 2022 03:09:54 +0000 X-MC-Relay: Junk X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Attack-Battle: 76a2a1092026f256_1648091394259_4115501371 X-MC-Loop-Signature: 1648091394259:177804186 X-MC-Ingress-Time: 1648091394259 Received: from rhbox.redhat.com (unknown [1.186.223.130]) (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) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a305.dreamhost.com (Postfix) with ESMTPSA id 4KP9FC5y8rz1PD; Wed, 23 Mar 2022 20:09:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gotplt.org; s=dreamhost; t=1648091393; bh=2ZdVZ5f+ZeMD58QDCYKxAXWbmMU1RvJN5RHDnKAJxZQ=; h=From:To:Cc:Subject:Date:Content-Transfer-Encoding; b=Zaf35WTWt4beIeG1foPqo7TiQmE0MdaksRHLD+whH/du/LjD7Kh0iyZqcsy0lWuf6 +Z5R7mHJ5Vg4axfpQbe7OAtj2v6JkYUv/uVe6cSAh3yHoxPBmhoOfpIelSWAunLNaW +4YQablYHXXSBMDeypgjmLldlDw7U/gzXZl0V0fsL+RcTubYz4Q0flKC6A0ppU7GtR aock1NuieFVvwIzP7aQ0shkM5x8/6zn1t3aSoeEc2M5Q70GVEf0jfA8Pc+g8eoOknW x3TX+rMPw6wWFMFmvhKRvfuzZjSqZVg0e7yeOAY99FUL9NsSf3XlM8JiBLIpt9oWI9 0CDzKdzYlQi8A== From: Siddhesh Poyarekar To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/104970: Limit size computation for access attribute Date: Thu, 24 Mar 2022 08:39:44 +0530 Message-Id: <20220324030944.44287-1-siddhesh@gotplt.org> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3038.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: , Cc: jakub@redhat.com Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Limit object size computation only to the simple case where access attribute has been explicitly specified. The object passed to __builtin_dynamic_object_size could either be a pointer or a VLA whose size has been described only using access attribute. Further, return a valid size only if the object is a void * pointer or points to (or is a VLA of) a type that has a constant size. gcc/ChangeLog: PR tree-optimization/104970 * tree-object-size.cc (parm_object_size): Restrict size computation scenarios to explicit access attributes. gcc/testsuite/ChangeLog: PR tree-optimization/104970 * gcc.dg/builtin-dynamic-object-size-0.c (test_parmsz_simple2, test_parmsz_simple3, test_parmsz_extern, test_parmsz_internal, test_parmsz_internal2, test_parmsz_internal3): New tests. (main): Use them. Signed-off-by: Siddhesh Poyarekar --- Tested: - x86_64 bootstrap and test - x86_64 ubsan bootstrap - i686 test .../gcc.dg/builtin-dynamic-object-size-0.c | 71 +++++++++++++++++++ gcc/tree-object-size.cc | 11 ++- 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c index e5dc23a908d..b5b0b3a677c 100644 --- a/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c +++ b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c @@ -380,6 +380,22 @@ test_parmsz_simple (void *obj, size_t sz) return __builtin_dynamic_object_size (obj, 0); } +size_t +__attribute__ ((access (__read_write__, 2, 1))) +__attribute__ ((noinline)) +test_parmsz_simple2 (size_t sz, char obj[]) +{ + return __builtin_dynamic_object_size (obj, 0); +} + +/* Implicitly constructed access attributes not supported yet. */ +size_t +__attribute__ ((noinline)) +test_parmsz_simple3 (size_t sz, char obj[sz]) +{ + return __builtin_dynamic_object_size (obj, 0); +} + size_t __attribute__ ((noinline)) __attribute__ ((access (__read_write__, 1, 2))) @@ -412,6 +428,38 @@ test_parmsz_unknown (void *obj, void *unknown, size_t sz, int cond) return __builtin_dynamic_object_size (cond ? obj : unknown, 0); } +struct S; +size_t +__attribute__ ((access (__read_write__, 1, 2))) +__attribute__ ((noinline)) +test_parmsz_extern (struct S *obj, size_t sz) +{ + return __builtin_dynamic_object_size (obj, 0); +} + +/* Implicitly constructed access attributes not supported yet. */ +size_t +__attribute__ ((noinline)) +test_parmsz_internal (size_t sz, double obj[][sz]) +{ + return __builtin_dynamic_object_size (obj, 0); +} + +size_t +__attribute__ ((access (__read_write__, 2, 1))) +__attribute__ ((noinline)) +test_parmsz_internal2 (size_t sz, double obj[][sz]) +{ + return __builtin_dynamic_object_size (obj, 0); +} + +size_t +__attribute__ ((noinline)) +test_parmsz_internal3 (size_t sz1, size_t sz2, double obj[sz1][sz2]) +{ + return __builtin_dynamic_object_size (obj, 0); +} + /* Loops. */ size_t @@ -532,9 +580,22 @@ main (int argc, char **argv) if (test_parmsz_simple (argv[0], __builtin_strlen (argv[0]) + 1) != __builtin_strlen (argv[0]) + 1) FAIL (); + if (test_parmsz_simple2 (__builtin_strlen (argv[0]) + 1, argv[0]) + != __builtin_strlen (argv[0]) + 1) + FAIL (); + /* Only explicitly added access attributes are supported for now. */ + if (test_parmsz_simple3 (__builtin_strlen (argv[0]) + 1, argv[0]) != -1) + FAIL (); int arr[42]; if (test_parmsz_scaled (arr, 42) != sizeof (arr)) FAIL (); + if (test_parmsz_scaled (arr, 40) != 40 * sizeof (int)) + FAIL (); + /* __bdos cannot see the actual size of ARR, so it will return what it was + passed. Fortunately though the overflow warnings see this caller side and + warns of the problematic size. */ + if (test_parmsz_scaled (arr, 44) != 44 * sizeof (int)) /* { dg-warning "-Wstringop-overflow=" } */ + FAIL (); if (test_parmsz_unknown (argv[0], argv[0], __builtin_strlen (argv[0]) + 1, 0) != -1) if (test_parmsz (argv[0], __builtin_strlen (argv[0]) + 1, -1) != 0) @@ -550,6 +611,16 @@ main (int argc, char **argv) FAIL (); if (test_parmsz_scaled_off (arr, 42, 2) != 40 * sizeof (int)) FAIL (); + struct S *s; + if (test_parmsz_extern (s, 42) != -1) + FAIL (); + double obj[4][4]; + if (test_parmsz_internal (4, obj) != -1) + FAIL (); + if (test_parmsz_internal2 (4, obj) != -1) + FAIL (); + if (test_parmsz_internal3 (4, 4, obj) != -1) + FAIL (); if (test_loop (arr, 42, 0, 32, 1) != 10 * sizeof (int)) FAIL (); if (test_loop (arr, 42, 32, -1, -1) != 0) diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc index b0b50774936..fc062b94d76 100644 --- a/gcc/tree-object-size.cc +++ b/gcc/tree-object-size.cc @@ -1477,14 +1477,19 @@ parm_object_size (struct object_size_info *osi, tree var) tree typesize = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (parm))); tree sz = NULL_TREE; - if (access && access->sizarg != UINT_MAX) + /* If we have an explicit access attribute with a usable size argument... */ + if (access && access->sizarg != UINT_MAX && !access->internal_p + /* ... and either PARM is void * or has a type that is complete and has a + constant size... */ + && ((typesize && poly_int_tree_p (typesize)) + || (!typesize && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (parm)))))) { tree fnargs = DECL_ARGUMENTS (fndecl); tree arg = NULL_TREE; unsigned argpos = 0; - /* Walk through the parameters to pick the size parameter and safely - scale it by the type size. */ + /* ... then walk through the parameters to pick the size parameter and + safely scale it by the type size if needed. */ for (arg = fnargs; arg; arg = TREE_CHAIN (arg), ++argpos) if (argpos == access->sizarg && INTEGRAL_TYPE_P (TREE_TYPE (arg))) {