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 A6612A04C5; Sat, 5 Sep 2020 00:11:54 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 3899E1BC25; Sat, 5 Sep 2020 00:11:54 +0200 (CEST) Received: from mail-pj1-f67.google.com (mail-pj1-f67.google.com [209.85.216.67]) by dpdk.org (Postfix) with ESMTP id BCAA6E07 for ; Sat, 5 Sep 2020 00:11:52 +0200 (CEST) Received: by mail-pj1-f67.google.com with SMTP id np15so5658329pjb.0 for ; Fri, 04 Sep 2020 15:11:52 -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:mime-version :content-transfer-encoding; bh=zc42YtOImu1RtavJiNN6nPYNtxe31SlKAHy/wieN6rc=; b=0Rm2X0nqYX//T/+WqPODPiO6g3AX7FaP5z5RgMZ2GvH68wB6scjrz013SIDQqPNthU /cBeVjpbS8rMD+R06KwbwX76JeigU/5twIpj62DCO7GqsVVpUaUdH6mhL36EoWBRdC3W kfcZR8ytyB6DTNNlAX4QyR+PRcHaa9sI4Fg1kn7Gr0jitsMCD/rxvdCMtfX29KXUcNdd VN92HU3OZqtRspfXaM8afWfT6xWXn7c49rjXUXRSD7t8T39FNefaGVMmuFU05d4e0OoT qi08N2SMUEyJ34Z4p6MPNSWTqo9EA8RFSIuCxIeIPwlrZ3Rvj6FBN1+Tu6pBFbowJ49s as/Q== 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:mime-version :content-transfer-encoding; bh=zc42YtOImu1RtavJiNN6nPYNtxe31SlKAHy/wieN6rc=; b=QLk32u2MiCxo7ueuc0G7PaDxE+w+JMA3k/BXS6rU6R+z7ecogVGFFOrp8CcaPvj7cP 3JvfGekZCHBO+GRNMpWXWdYszNTPdw8ks/H3kC9NcYnnALuYN5XXEq3dlezSVX8Tvemt /PXb8JtPskYhUjZUf5kX3UNShk4OSGm3miDoY88IobTxJRCZ0lyOSrJZl+CsbNkzvSdP R/WqgFdvTbh/l8lxjsIsR6snyruvxUGN+xRapokmWCJH1jEMlnwnAVpMTe8bfLVWms+U BCAUu4x0Y+eJsJCuarjKhje+hvEGMJXc70WxBC+H/ijigrT43GzihhXV4dOGGk/VafVC feMw== X-Gm-Message-State: AOAM532cGXq6qS2w+mfAhbE5m8ILhktydmywxcTMuFJyVu+rUb/asX4u Gvr1xJpPxji8ADOPK0AyB7ivYpNda+DKVQ== X-Google-Smtp-Source: ABdhPJy6Amb3C1PAkyMSgyjoi86/ql9EqY1wb+PWuXtsh1GzxWPdI/QF8xo8iMZhtyQhCzns+W4xVw== X-Received: by 2002:a17:90a:414d:: with SMTP id m13mr9615637pjg.163.1599257511772; Fri, 04 Sep 2020 15:11:51 -0700 (PDT) Received: from hermes.lan (204-195-22-127.wavecable.com. [204.195.22.127]) by smtp.gmail.com with ESMTPSA id b144sm7507592pfb.48.2020.09.04.15.11.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2020 15:11:50 -0700 (PDT) From: Stephen Hemminger To: Anatoly Burakov Cc: dev@dpdk.org, Stephen Hemminger Date: Fri, 4 Sep 2020 15:11:43 -0700 Message-Id: <20200904221143.19744-1-stephen@networkplumber.org> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH] 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 --- app/test/test_malloc.c | 5 ++++- lib/librte_eal/include/rte_common.h | 7 ++++--- lib/librte_eal/include/rte_malloc.h | 28 ++++++++++++++++------------ 3 files changed, 24 insertions(+), 16 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 6b85374c0fe5..0c2e03234d8d 100644 --- a/lib/librte_eal/include/rte_common.h +++ b/lib/librte_eal/include/rte_common.h @@ -117,10 +117,11 @@ typedef uint16_t unaligned_uint16_t; * cannot alias any other valid pointer and that the memory * contents are undefined. I.e behaves like malloc. */ -#if RTE_CC_IS_GNU -#define __rte_malloc __attribute__((malloc)) +#if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG) +#define __rte_alloc_size(...) \ + __attribute__((alloc_size(__VA_ARGS__))) #else -#define __rte_malloc +#define __rte_alloc_size(...) #endif /** diff --git a/lib/librte_eal/include/rte_malloc.h b/lib/librte_eal/include/rte_malloc.h index 721560122c70..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. @@ -79,9 +80,9 @@ rte_malloc(const char *type, size_t size, unsigned align); * align is not a power of two). * - Otherwise, the pointer to the allocated object. */ -__rte_malloc 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,9 +107,9 @@ rte_zmalloc(const char *type, size_t size, unsigned align); * align is not a power of two). * - Otherwise, the pointer to the allocated object. */ -__rte_malloc 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 @@ -131,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 @@ -157,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 @@ -182,9 +185,9 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket); * align is not a power of two). * - Otherwise, the pointer to the allocated object. */ -__rte_malloc 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. @@ -212,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 @@ -239,9 +243,9 @@ rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket); * align is not a power of two). * - Otherwise, the pointer to the allocated object. */ -__rte_malloc 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