From patchwork Sun Dec 4 17:50:18 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 86463 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp1138592qgi; Sun, 4 Dec 2016 09:50:50 -0800 (PST) X-Received: by 10.84.134.3 with SMTP id 3mr115917455plg.90.1480873849973; Sun, 04 Dec 2016 09:50:49 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id d17si11868696pgi.176.2016.12.04.09.50.49 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Dec 2016 09:50:49 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-443435-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org; spf=pass (google.com: domain of gcc-patches-return-443435-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-443435-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE dis=NONE) header.from=gmail.com DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :subject:to:cc:message-id:date:mime-version:content-type; q=dns; s=default; b=OsH5iMw9QZ3UBrUhIIObFKVsEcwLM1LPF+22hhoysvq2tGfyx+ eoHR5ebgWKYmemYBNIYj5UUC0J2xwmGALRDmDjsxHxPo3ZtXo4Se3o214OHOWYgH xQLpYwIyJ+SKDRR3ltOIv+jhZxW4zBHr0EYcQ9NzaQg7kAzKNSctTTvdI= 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:from :subject:to:cc:message-id:date:mime-version:content-type; s= default; bh=JnBc9qqxSZ0BLimyLjzhXUQP9pI=; b=DHO0T8btbU11ZzrbTIS8 IOKEhIYobI8eort2N+9qRPjIMsT1bPfkxbtLK+PO4Yz2vK+ig9xUxirFXF4Oiqcc ySqOkofRWkPeF/ZUxgx2KLGYttos4XRUZj2yvXkbtp0nH49gHsgsA4ejJfstluXl eHVKv7mhdnN6/2Fc4rKTtsg= Received: (qmail 27794 invoked by alias); 4 Dec 2016 17:50:34 -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 27688 invoked by uid 89); 4 Dec 2016 17:50:32 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.5 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS, UPPERCASE_50_75 autolearn=ham version=3.3.2 spammy=1516, Allocation, built-ins X-HELO: mail-qt0-f174.google.com Received: from mail-qt0-f174.google.com (HELO mail-qt0-f174.google.com) (209.85.216.174) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 04 Dec 2016 17:50:22 +0000 Received: by mail-qt0-f174.google.com with SMTP id c47so295338064qtc.2 for ; Sun, 04 Dec 2016 09:50:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:subject:to:cc:message-id:date:user-agent :mime-version; bh=2HYvm1eOUBk51s0aMa6a9FZrUC9UQqB7zzvZhBFw7qY=; b=c1KzAER9Y5wqZnQ6qmAOWcCRjbaxupnHViHMRXyjmmFRQirMZywfjtT4WLgcS2lk2j lw0WvoPOzgMA9FZFQwi1DHz+FUf8P/LX2bmGMpxo9P7K0ETAGrFDRmTnghmHNlp1coFT iQJWVX0AI/nWLJOITZuSjLt9KDEO+E6q0a4H7L39KOCPOaBJcZy2v8Cb0MWuzelC5XGw /U85Rkn8YiSeh4SScH0cEx+8z/D3MNb3ZXdueJMzLNPCepYSetbRVwcUY0vorf9OR1SB +ajQxKqa7W8d2JY98i95j5d6XDJrlH+mjuUkwDkn3DQeLKFdmvkD+pVuKB9X8y1ise4+ BS+w== X-Gm-Message-State: AKaTC00IFG80cqCDTWchK9fbV1UZGCFtsB6wijPUyYBeKaEM2+pVGE53qGS1uDKOYHgOkg== X-Received: by 10.237.56.40 with SMTP id j37mr51041164qte.134.1480873820646; Sun, 04 Dec 2016 09:50:20 -0800 (PST) Received: from [192.168.0.26] (97-122-174-22.hlrn.qwest.net. [97.122.174.22]) by smtp.gmail.com with ESMTPSA id z22sm2334976qkb.22.2016.12.04.09.50.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Dec 2016 09:50:20 -0800 (PST) From: Martin Sebor Subject: [Committed] add missing attribute alloc_size to allocation builtins (PR 78668) To: Gcc Patch List Cc: Jeff Law Message-ID: Date: Sun, 4 Dec 2016 10:50:18 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 MIME-Version: 1.0 X-IsSubscribed: yes In a review of my patch to warn on overflow in calls to allocation functions (bugs 77531 and 78284) Jeff asked[*]me to commit the bits that add the missing alloc_attribute to GCC memory allocation built ins. The patch also add the missing attribute returns_nonnull to __builtin_alloca. Tested on x86_64 and committed to trunk. Martin [*] https://gcc.gnu.org/ml/gcc-patches/2016-12/msg00260.html PR c/78668 - aligned_alloc, realloc, et al. missing attribute alloc_size gcc/ChangeLog: PR c/78668 * builtin-attrs.def (ATTR_ALLOC_SIZE, ATTR_RETURNS_NONNULL): New identifier tree nodes. (ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST): New attribute list. (ATTR_MALLOC_SIZE_1_NOTHROW_LIST): Same. (ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST): Same. (ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST): Same. (ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST): Same. * builtins.def (aligned_alloc, calloc, malloc, realloc): Add attribute alloc_size. (alloca): Add attribute alloc_size and returns_nonnull. gcc/testsuite/ChangeLog: PR c/78668 * gcc.dg/builtin-alloc-size.c: New test. diff --git a/gcc/builtin-attrs.def b/gcc/builtin-attrs.def index 88c9bd1..1520d15 100644 --- a/gcc/builtin-attrs.def +++ b/gcc/builtin-attrs.def @@ -83,6 +83,7 @@ DEF_LIST_INT_INT (5,6) #undef DEF_LIST_INT_INT /* Construct trees for identifiers. */ +DEF_ATTR_IDENT (ATTR_ALLOC_SIZE, "alloc_size") DEF_ATTR_IDENT (ATTR_COLD, "cold") DEF_ATTR_IDENT (ATTR_CONST, "const") DEF_ATTR_IDENT (ATTR_FORMAT, "format") @@ -151,6 +152,26 @@ DEF_ATTR_TREE_LIST (ATTR_SENTINEL_NOTHROW_LEAF_LIST, ATTR_SENTINEL, \ DEF_ATTR_TREE_LIST (ATTR_COLD_CONST_NORETURN_NOTHROW_LEAF_LIST, ATTR_CONST,\ ATTR_NULL, ATTR_COLD_NORETURN_NOTHROW_LEAF_LIST) +/* Allocation functions like malloc and realloc whose first argument + specifies the size of the allocated object. */ +DEF_ATTR_TREE_LIST (ATTR_MALLOC_SIZE_1_NOTHROW_LIST, ATTR_ALLOC_SIZE, \ + ATTR_LIST_1, ATTR_MALLOC_NOTHROW_LIST) +DEF_ATTR_TREE_LIST (ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST, ATTR_ALLOC_SIZE, \ + ATTR_LIST_1, ATTR_MALLOC_NOTHROW_LEAF_LIST) +/* Alloca is just like malloc except that it never returns null. */ +DEF_ATTR_TREE_LIST (ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST, ATTR_RETURNS_NONNULL, + ATTR_NULL, ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST) + +/* Allocation functions like calloc the product of whose first two arguments + specifies the size of the allocated object. */ +DEF_ATTR_TREE_LIST (ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST, ATTR_ALLOC_SIZE, \ + ATTR_LIST_1_2, ATTR_MALLOC_NOTHROW_LEAF_LIST) + +/* Allocation functions like realloc whose second argument specifies + the size of the allocated object. */ +DEF_ATTR_TREE_LIST (ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST, ATTR_ALLOC_SIZE, \ + ATTR_LIST_2, ATTR_NOTHROW_LEAF_LIST) + /* Functions whose pointer parameter(s) are all nonnull. */ DEF_ATTR_TREE_LIST (ATTR_NONNULL_LIST, ATTR_NONNULL, ATTR_NULL, ATTR_NULL) /* Functions whose first parameter is a nonnull pointer. */ diff --git a/gcc/builtins.def b/gcc/builtins.def index 6766975..9cd24e8 100644 --- a/gcc/builtins.def +++ b/gcc/builtins.def @@ -297,7 +297,7 @@ DEF_C99_BUILTIN (BUILT_IN_ACOSH, "acosh", BT_FN_DOUBLE_DOUBLE, ATTR_MATHF DEF_C99_BUILTIN (BUILT_IN_ACOSHF, "acoshf", BT_FN_FLOAT_FLOAT, ATTR_MATHFN_FPROUNDING_ERRNO) DEF_C99_BUILTIN (BUILT_IN_ACOSHL, "acoshl", BT_FN_LONGDOUBLE_LONGDOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO) DEF_C99_C90RES_BUILTIN (BUILT_IN_ACOSL, "acosl", BT_FN_LONGDOUBLE_LONGDOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO) -DEF_C11_BUILTIN (BUILT_IN_ALIGNED_ALLOC, "aligned_alloc", BT_FN_PTR_SIZE_SIZE, ATTR_MALLOC_NOTHROW_LIST) +DEF_C11_BUILTIN (BUILT_IN_ALIGNED_ALLOC, "aligned_alloc", BT_FN_PTR_SIZE_SIZE, ATTR_MALLOC_SIZE_1_NOTHROW_LIST) DEF_LIB_BUILTIN (BUILT_IN_ASIN, "asin", BT_FN_DOUBLE_DOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO) DEF_C99_C90RES_BUILTIN (BUILT_IN_ASINF, "asinf", BT_FN_FLOAT_FLOAT, ATTR_MATHFN_FPROUNDING_ERRNO) DEF_C99_BUILTIN (BUILT_IN_ASINH, "asinh", BT_FN_DOUBLE_DOUBLE, ATTR_MATHFN_FPROUNDING) @@ -777,7 +777,7 @@ DEF_GCC_BUILTIN (BUILT_IN_UMULLL_OVERFLOW, "umulll_overflow", BT_FN_BOOL_ DEF_LIB_BUILTIN (BUILT_IN_ABORT, "abort", BT_FN_VOID, ATTR_TMPURE_NORETURN_NOTHROW_LEAF_LIST) DEF_LIB_BUILTIN (BUILT_IN_ABS, "abs", BT_FN_INT_INT, ATTR_CONST_NOTHROW_LEAF_LIST) DEF_GCC_BUILTIN (BUILT_IN_AGGREGATE_INCOMING_ADDRESS, "aggregate_incoming_address", BT_FN_PTR_VAR, ATTR_LEAF_LIST) -DEF_EXT_LIB_BUILTIN (BUILT_IN_ALLOCA, "alloca", BT_FN_PTR_SIZE, ATTR_MALLOC_NOTHROW_LEAF_LIST) +DEF_EXT_LIB_BUILTIN (BUILT_IN_ALLOCA, "alloca", BT_FN_PTR_SIZE, ATTR_ALLOCA_SIZE_1_NOTHROW_LEAF_LIST) DEF_GCC_BUILTIN (BUILT_IN_APPLY, "apply", BT_FN_PTR_PTR_FN_VOID_VAR_PTR_SIZE, ATTR_NULL) DEF_GCC_BUILTIN (BUILT_IN_APPLY_ARGS, "apply_args", BT_FN_PTR_VAR, ATTR_LEAF_LIST) DEF_GCC_BUILTIN (BUILT_IN_BSWAP16, "bswap16", BT_FN_UINT16_UINT16, ATTR_CONST_NOTHROW_LEAF_LIST) @@ -785,7 +785,7 @@ DEF_GCC_BUILTIN (BUILT_IN_BSWAP32, "bswap32", BT_FN_UINT32_UINT32, ATTR_C DEF_GCC_BUILTIN (BUILT_IN_BSWAP64, "bswap64", BT_FN_UINT64_UINT64, ATTR_CONST_NOTHROW_LEAF_LIST) DEF_EXT_LIB_BUILTIN (BUILT_IN_CLEAR_CACHE, "__clear_cache", BT_FN_VOID_PTR_PTR, ATTR_NOTHROW_LEAF_LIST) /* [trans-mem]: Adjust BUILT_IN_TM_CALLOC if BUILT_IN_CALLOC is changed. */ -DEF_LIB_BUILTIN (BUILT_IN_CALLOC, "calloc", BT_FN_PTR_SIZE_SIZE, ATTR_MALLOC_NOTHROW_LEAF_LIST) +DEF_LIB_BUILTIN (BUILT_IN_CALLOC, "calloc", BT_FN_PTR_SIZE_SIZE, ATTR_MALLOC_SIZE_1_2_NOTHROW_LEAF_LIST) DEF_GCC_BUILTIN (BUILT_IN_CLASSIFY_TYPE, "classify_type", BT_FN_INT_VAR, ATTR_LEAF_LIST) DEF_GCC_BUILTIN (BUILT_IN_CLZ, "clz", BT_FN_INT_UINT, ATTR_CONST_NOTHROW_LEAF_LIST) DEF_GCC_BUILTIN (BUILT_IN_CLZIMAX, "clzimax", BT_FN_INT_UINTMAX, ATTR_CONST_NOTHROW_LEAF_LIST) @@ -861,7 +861,7 @@ DEF_LIB_BUILTIN (BUILT_IN_LABS, "labs", BT_FN_LONG_LONG, ATTR_CONST_NOTHR DEF_C99_BUILTIN (BUILT_IN_LLABS, "llabs", BT_FN_LONGLONG_LONGLONG, ATTR_CONST_NOTHROW_LEAF_LIST) DEF_GCC_BUILTIN (BUILT_IN_LONGJMP, "longjmp", BT_FN_VOID_PTR_INT, ATTR_NORETURN_NOTHROW_LIST) /* [trans-mem]: Adjust BUILT_IN_TM_MALLOC if BUILT_IN_MALLOC is changed. */ -DEF_LIB_BUILTIN (BUILT_IN_MALLOC, "malloc", BT_FN_PTR_SIZE, ATTR_MALLOC_NOTHROW_LEAF_LIST) +DEF_LIB_BUILTIN (BUILT_IN_MALLOC, "malloc", BT_FN_PTR_SIZE, ATTR_MALLOC_SIZE_1_NOTHROW_LEAF_LIST) DEF_GCC_BUILTIN (BUILT_IN_NEXT_ARG, "next_arg", BT_FN_PTR_VAR, ATTR_LEAF_LIST) DEF_GCC_BUILTIN (BUILT_IN_PARITY, "parity", BT_FN_INT_UINT, ATTR_CONST_NOTHROW_LEAF_LIST) DEF_GCC_BUILTIN (BUILT_IN_PARITYIMAX, "parityimax", BT_FN_INT_UINTMAX, ATTR_CONST_NOTHROW_LEAF_LIST) @@ -873,7 +873,7 @@ DEF_GCC_BUILTIN (BUILT_IN_POPCOUNTL, "popcountl", BT_FN_INT_ULONG, ATTR_C DEF_GCC_BUILTIN (BUILT_IN_POPCOUNTLL, "popcountll", BT_FN_INT_ULONGLONG, ATTR_CONST_NOTHROW_LEAF_LIST) DEF_EXT_LIB_BUILTIN (BUILT_IN_POSIX_MEMALIGN, "posix_memalign", BT_FN_INT_PTRPTR_SIZE_SIZE, ATTR_NOTHROW_NONNULL_LEAF) DEF_GCC_BUILTIN (BUILT_IN_PREFETCH, "prefetch", BT_FN_VOID_CONST_PTR_VAR, ATTR_NOVOPS_LEAF_LIST) -DEF_LIB_BUILTIN (BUILT_IN_REALLOC, "realloc", BT_FN_PTR_PTR_SIZE, ATTR_NOTHROW_LEAF_LIST) +DEF_LIB_BUILTIN (BUILT_IN_REALLOC, "realloc", BT_FN_PTR_PTR_SIZE, ATTR_ALLOC_SIZE_2_NOTHROW_LEAF_LIST) DEF_GCC_BUILTIN (BUILT_IN_RETURN, "return", BT_FN_VOID_PTR, ATTR_NORETURN_NOTHROW_LEAF_LIST) DEF_GCC_BUILTIN (BUILT_IN_RETURN_ADDRESS, "return_address", BT_FN_PTR_UINT, ATTR_LEAF_LIST) DEF_GCC_BUILTIN (BUILT_IN_SAVEREGS, "saveregs", BT_FN_PTR_VAR, ATTR_NULL) diff --git a/gcc/testsuite/gcc.dg/builtin-alloc-size.c b/gcc/testsuite/gcc.dg/builtin-alloc-size.c new file mode 100644 index 0000000..5a40862 --- /dev/null +++ b/gcc/testsuite/gcc.dg/builtin-alloc-size.c @@ -0,0 +1,72 @@ +/* PR c/78668 - aligned_alloc, realloc, et al. missing attribute alloc_size + Test to verify that memory allocation built-ins are decorated with + attribute alloc_size that __builtin_object_size can make use of (or + are treated as if they were for that purpose).. + { dg-do compile } + { dg-additional-options "-O2 -fdump-tree-optimized" } */ + +void sink (void*); + +unsigned size (unsigned n) +{ + return n; +} + +void test_aligned_alloc (unsigned a) +{ + unsigned n = size (7); + + void *p = __builtin_aligned_alloc (n, a); + if (__builtin_object_size (p, 0) != n) + __builtin_abort (); + sink (p); +} + +void test_alloca (void) +{ + unsigned n = size (13); + + void *p = __builtin_alloca (n); + + /* Also verify that alloca is declared with attribute returns_nonnull + (or treated as it were as the case may be). */ + if (!p) + __builtin_abort (); + + if (__builtin_object_size (p, 0) != n) + __builtin_abort (); + sink (p); +} + +void test_calloc (void) +{ + unsigned m = size (19); + unsigned n = size (23); + + void *p = __builtin_calloc (m, n); + if (__builtin_object_size (p, 0) != m * n) + __builtin_abort (); + sink (p); +} + +void test_malloc (void) +{ + unsigned n = size (17); + + void *p = __builtin_malloc (n); + if (__builtin_object_size (p, 0) != n) + __builtin_abort (); + sink (p); +} + +void test_realloc (void *p) +{ + unsigned n = size (31); + + p = __builtin_realloc (p, n); + if (__builtin_object_size (p, 0) != n) + __builtin_abort (); + sink (p); +} + +/* { dg-final { scan-tree-dump-not "abort" "optimized" } } */