From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 7DBB2A04DB; Thu, 15 Oct 2020 02:55:52 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id A46161DB21; Thu, 15 Oct 2020 02:55:40 +0200 (CEST) Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) by dpdk.org (Postfix) with ESMTP id 610201DB1D for ; Thu, 15 Oct 2020 02:55:35 +0200 (CEST) Received: by mail-pl1-f176.google.com with SMTP id y1so630196plp.6 for ; Wed, 14 Oct 2020 17:55:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NoBwUz0SnVZNcZyM/hXmiRLYhU3PN9Xc6eF2mQ2bUWA=; b=xbBQcvvUlBvJdNYSYbzFamyg75MfZ6DLHUm6FTi5pjkYSRkmXjcSzWjZndmwb1t8KG 41iqyjmVRckqWmObJ7ESNVPp4LvX6PMD/gWrYZSJPxMaf5+6NH4uKtRpxq/Vu9nSyav9 gn0d3+Al+6Mvt4Hujtn/ysifsvXqOQTnoq5FU05i8kHP8O+SzVB4+HWJHQTwfNxRgoWT J2oPfFqLN2gIBd5dURoDHeTPFP7KrjlZHjGOcID2IfBl1yKgziJHKE99x21JwiTdIa7O 0li8We8Qp+12Jrs6kQEwmODERRYAAj4zJ095eFbavPuPE9YLiY5h/58jbUbmcrtau3N5 aJ6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NoBwUz0SnVZNcZyM/hXmiRLYhU3PN9Xc6eF2mQ2bUWA=; b=n48ei1ZjeTeolPfNYWJMeOvyc37rWTuqF8KN9nLDL2zMXbOG9DC7RlzRg+14cmLPTF iYqDdobtAGcUbnS1KtUP9d82/J2hNZ4pPnL2L0juip13s0+BXrUMytiM7hz+VxFCuaXr 88ztDBCMcYPz3BC9IDunnG0CgzstvbtpqQn+77V1zfjnQVYIFW3PMwIQLi+o9fP8Goyk XJDAn/2HnzkMFedKHwsMQfh4e/yEmCvGP1QcJ6vpRzBqM1GNMBHV3LwbiGJdQNSWqWLX H5inLm7k6hzNLuB3ETiZPJONBqm9Xhzn3hUFv6DbneutmK/FhB5OWepR/EtX4YaHS7r7 WqSw== X-Gm-Message-State: AOAM5322K8w+thfMlSrOu09tXKKeUjZdQSKG0fFp40R2JlbtP+qhzZHe TXYxFV031DqH3cLMDK6RSvRsvSOP6KvMpw== X-Google-Smtp-Source: ABdhPJzL0wZofd6L2gwTB7xHc07Zz4lPgkm+nGr0Q+CkDbb1NIGrMXR/dFT5plYbqtdWPWwwuTYb1Q== X-Received: by 2002:a17:902:be0d:b029:d2:8084:cb19 with SMTP id r13-20020a170902be0db02900d28084cb19mr1544881pls.45.1602723332926; Wed, 14 Oct 2020 17:55:32 -0700 (PDT) Received: from hermes.corp.microsoft.com (204-195-22-127.wavecable.com. [204.195.22.127]) by smtp.gmail.com with ESMTPSA id kb15sm698596pjb.17.2020.10.14.17.55.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Oct 2020 17:55:32 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Date: Wed, 14 Oct 2020 17:55:24 -0700 Message-Id: <20201015005524.24659-1-stephen@networkplumber.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v2] eal/rte_malloc: add alloc_size() attribute to allocation functions X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" By using the alloc_size() attribute the compiler can optimize better and detect errors at compile time. For example, Gcc will fail one of the invalid allocation examples in app/test/test_malloc.c because the allocation is outside the limits of memory. Signed-off-by: Stephen Hemminger --- v2 - rebase onto correct branch (main) app/test/test_malloc.c | 5 ++++- lib/librte_eal/include/rte_common.h | 12 ++++++++++++ lib/librte_eal/include/rte_malloc.h | 24 ++++++++++++++++-------- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/app/test/test_malloc.c b/app/test/test_malloc.c index 71b3cfdde5cf..fdf77b4f6a14 100644 --- a/app/test/test_malloc.c +++ b/app/test/test_malloc.c @@ -846,6 +846,9 @@ test_malloc_bad_params(void) if (bad_ptr != NULL) goto err_return; +#if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG) + /* this test can not be built, will get trapped at compile time! */ +#else /* rte_malloc expected to return null with size will cause overflow */ align = RTE_CACHE_LINE_SIZE; size = (size_t)-8; @@ -857,7 +860,7 @@ test_malloc_bad_params(void) bad_ptr = rte_realloc(NULL, size, align); if (bad_ptr != NULL) goto err_return; - +#endif return 0; err_return: diff --git a/lib/librte_eal/include/rte_common.h b/lib/librte_eal/include/rte_common.h index 2920255fc1e3..e63ef0f1de5e 100644 --- a/lib/librte_eal/include/rte_common.h +++ b/lib/librte_eal/include/rte_common.h @@ -134,6 +134,18 @@ typedef uint16_t unaligned_uint16_t; __attribute__((format(printf, format_index, first_arg))) #endif +/** + * Tells compiler that the function returns a value that points to + * memory, where the size is given by the one or two arguments. + * Used by compiler to validate object size. + */ +#if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG) +#define __rte_alloc_size(...) \ + __attribute__((alloc_size(__VA_ARGS__))) +#else +#define __rte_alloc_size(...) +#endif + #define RTE_PRIORITY_LOG 101 #define RTE_PRIORITY_BUS 110 #define RTE_PRIORITY_CLASS 120 diff --git a/lib/librte_eal/include/rte_malloc.h b/lib/librte_eal/include/rte_malloc.h index 42ca05182f8e..3af64f87618e 100644 --- a/lib/librte_eal/include/rte_malloc.h +++ b/lib/librte_eal/include/rte_malloc.h @@ -54,7 +54,8 @@ struct rte_malloc_socket_stats { * - Otherwise, the pointer to the allocated object. */ void * -rte_malloc(const char *type, size_t size, unsigned align); +rte_malloc(const char *type, size_t size, unsigned align) + __rte_alloc_size(2); /** * Allocate zero'ed memory from the heap. @@ -80,7 +81,8 @@ rte_malloc(const char *type, size_t size, unsigned align); * - Otherwise, the pointer to the allocated object. */ void * -rte_zmalloc(const char *type, size_t size, unsigned align); +rte_zmalloc(const char *type, size_t size, unsigned align) + __rte_alloc_size(2); /** * Replacement function for calloc(), using huge-page memory. Memory area is @@ -106,7 +108,8 @@ rte_zmalloc(const char *type, size_t size, unsigned align); * - Otherwise, the pointer to the allocated object. */ void * -rte_calloc(const char *type, size_t num, size_t size, unsigned align); +rte_calloc(const char *type, size_t num, size_t size, unsigned align) + __rte_alloc_size(2, 3); /** * Replacement function for realloc(), using huge-page memory. Reserved area @@ -129,7 +132,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align); * - Otherwise, the pointer to the reallocated memory. */ void * -rte_realloc(void *ptr, size_t size, unsigned int align); +rte_realloc(void *ptr, size_t size, unsigned int align) + __rte_alloc_size(2); /** * Replacement function for realloc(), using huge-page memory. Reserved area @@ -155,7 +159,8 @@ rte_realloc(void *ptr, size_t size, unsigned int align); */ __rte_experimental void * -rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket); +rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) + __rte_alloc_size(2, 3); /** * This function allocates memory from the huge-page area of memory. The memory @@ -181,7 +186,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket); * - Otherwise, the pointer to the allocated object. */ void * -rte_malloc_socket(const char *type, size_t size, unsigned align, int socket); +rte_malloc_socket(const char *type, size_t size, unsigned align, int socket) + __rte_alloc_size(2); /** * Allocate zero'ed memory from the heap. @@ -209,7 +215,8 @@ rte_malloc_socket(const char *type, size_t size, unsigned align, int socket); * - Otherwise, the pointer to the allocated object. */ void * -rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket); +rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket) + __rte_alloc_size(2); /** * Replacement function for calloc(), using huge-page memory. Memory area is @@ -237,7 +244,8 @@ rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket); * - Otherwise, the pointer to the allocated object. */ void * -rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket); +rte_calloc_socket(const char *type, size_t num, size_t size, unsigned align, int socket) + __rte_alloc_size(2, 3); /** * Frees the memory space pointed to by the provided pointer. -- 2.27.0