From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 1DCC845AE3; Tue, 8 Oct 2024 18:51:13 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id F126B41153; Tue, 8 Oct 2024 18:49:20 +0200 (CEST) Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) by mails.dpdk.org (Postfix) with ESMTP id 67CA840E8A for ; Tue, 8 Oct 2024 18:49:09 +0200 (CEST) Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-207115e3056so53994345ad.2 for ; Tue, 08 Oct 2024 09:49:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1728406148; x=1729010948; darn=dpdk.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hN37CdvcqkowJN/1Ku1rBDNhicbJKixoKSGKt6GGrzI=; b=SJKEMsaHGC6JQV32l0bcgG7Lc4QD8TTJx7+mGvvElADYfs7/kG9wehdhicSCj+/hbA E2KRMej0u1iFHdjm02jVFI+5WIw72J8AXzAzB1o6YD2UcDaEUm5FPsA1KeH2nZixDAHs Nct5DvP+n8z9RDHZkpSXeBiGkm5jFbojqwcOxaKEEjxItSF2xgjlY/Tq8XLEtYkQt6BI nWHA0NCGX94dl4pTf2cxTli/PXUf+tEQG8jU6Uue8bjznDBrd+DxRK5V37RmaRz5+sd1 iReQHHSAMPM2aZf6/JVzkPeu88W6FaMFAG5h+obLp4MJAQm8BkgeQ1lcj8Fwr273TXnL yL7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728406148; x=1729010948; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hN37CdvcqkowJN/1Ku1rBDNhicbJKixoKSGKt6GGrzI=; b=c9Ni9HW2yiVoH+D6eeAnHBPJc6XgFLdw1P8jBBdjpXCKDpsKkvNCeR6Du+HtU2zk0B uCmJ9goIARtlShh9VkxuvVXZae2ZUwVLSPWRHndxyujtUk2yKIL78WO7k2JVK8HSwHVp TUhEH8LFEA6qz7dW6DB2kdxnVKnwrEjV68Hxom++XWMr80vGAuTdS/ndI2PGQMDaB/He K8lLoxQxB4cVrlFDmJ0DZwJJb5TfNr4LVAfMLeGzQrt0ya9RERdLPwDc70PwsbBnXiiX aFpFDUXwChgR2EKjNiCKRCxkClcQcXNidr+pAA95PITz9Pj74yYxoaJvUF+8tGQIM4To tdMA== X-Gm-Message-State: AOJu0Yw1bRnImzMbKjrsAVYT6Z+LWN450RPfmc6edTcoDMzrpqCz/94n zv8cgcSTH0j7GOi5c83WoXaNvQC6qi3ueNhLT1GIxU9/QTXKzfgnLKrrs+qw62cXp4go0Juo+Dp 5kCIHqQ== X-Google-Smtp-Source: AGHT+IFozQUZl8CtbHysK4H8ThA3wGYxrmpmLIVY7jhlwC+npnSmd5NwP8+6Lhh/pGuM4lekotxpJQ== X-Received: by 2002:a17:902:d484:b0:20b:94a3:19d4 with SMTP id d9443c01a7336-20bff0475d3mr217693305ad.37.1728406148459; Tue, 08 Oct 2024 09:49:08 -0700 (PDT) Received: from hermes.local (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c54056bd9sm13832965ad.85.2024.10.08.09.49.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 09:49:07 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Chengwen Feng , Anatoly Burakov , Tyler Retzlaff Subject: [PATCH v9 17/17] eal: add function attributes for allocation functions Date: Tue, 8 Oct 2024 09:47:21 -0700 Message-ID: <20241008164842.139045-18-stephen@networkplumber.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241008164842.139045-1-stephen@networkplumber.org> References: <20240927204742.546164-1-stephen@networkplumber.org> <20241008164842.139045-1-stephen@networkplumber.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org The allocation functions take a alignment argument that can be useful to hint the compiler optimizer. This is supported by GCC and Clang but only useful with GCC because Clang gives warning if alignment is 0. Newer versions of GCC have a malloc attribute that can be used to find mismatches between allocation and free; the typical problem caught is a pointer allocated with rte_malloc() that is then incorrectly freed using free(). The name of the DPDK wrapper macros for these attributes are chosen to be similar to what GLIBC is using in cdefs.h. Note: The rte_free function prototype was moved ahead of the allocation functions since the dealloc attribute now refers to it. Signed-off-by: Stephen Hemminger Acked-by: Chengwen Feng Acked-by: Anatoly Burakov --- doc/guides/rel_notes/release_24_11.rst | 8 ++++ lib/eal/include/rte_common.h | 34 +++++++++++++++ lib/eal/include/rte_malloc.h | 59 ++++++++++++++++---------- 3 files changed, 78 insertions(+), 23 deletions(-) diff --git a/doc/guides/rel_notes/release_24_11.rst b/doc/guides/rel_notes/release_24_11.rst index e0a9aa55a1..662cb66ebd 100644 --- a/doc/guides/rel_notes/release_24_11.rst +++ b/doc/guides/rel_notes/release_24_11.rst @@ -67,6 +67,14 @@ New Features The new statistics are useful for debugging and profiling. +* **Hardened rte_malloc and related functions.** + + * Added function attributes to ``rte_malloc`` and similar functions + that can catch some obvious bugs at compile time (with GCC 11.0 or later). + Examples: calling ``free()`` on pointer that was allocated with ``rte_malloc`` + (and vice versa); freeing the same pointer twice in the same routine; + freeing an object that was not created by allocation; etc. + Removed Items ------------- diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h index eec0400dad..a53051072e 100644 --- a/lib/eal/include/rte_common.h +++ b/lib/eal/include/rte_common.h @@ -228,6 +228,40 @@ typedef uint16_t unaligned_uint16_t; #define __rte_alloc_size(...) #endif +/** + * Tells the compiler that the function returns a value that points to + * memory aligned by a function argument. + * + * Note: not enabled on Clang because it warns if align argument is zero. + */ +#if defined(RTE_CC_GCC) +#define __rte_alloc_align(argno) \ + __attribute__((alloc_align(argno))) +#else +#define __rte_alloc_align(argno) +#endif + +/** + * Tells the compiler this is a function like malloc and that the pointer + * returned cannot alias any other pointer (ie new memory). + */ +#if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG) +#define __rte_malloc __attribute__((__malloc__)) +#else +#define __rte_malloc +#endif + +/** + * With recent GCC versions also able to track that proper + * deallocator function is used for this pointer. + */ +#if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 110000) +#define __rte_dealloc(dealloc, argno) \ + __attribute__((malloc(dealloc, argno))) +#else +#define __rte_dealloc(dealloc, argno) +#endif + #define RTE_PRIORITY_LOG 101 #define RTE_PRIORITY_BUS 110 #define RTE_PRIORITY_CLASS 120 diff --git a/lib/eal/include/rte_malloc.h b/lib/eal/include/rte_malloc.h index 1f91e7bdde..c8836de67c 100644 --- a/lib/eal/include/rte_malloc.h +++ b/lib/eal/include/rte_malloc.h @@ -31,6 +31,26 @@ struct rte_malloc_socket_stats { size_t heap_allocsz_bytes; /**< Total allocated bytes on heap */ }; +/** + * Functions that expect return value to be freed with rte_free() + */ +#define __rte_dealloc_free __rte_dealloc(rte_free, 1) + +/** + * Frees the memory space pointed to by the provided pointer. + * + * This pointer must have been returned by a previous call to + * rte_malloc(), rte_zmalloc(), rte_calloc() or rte_realloc(). The behaviour of + * rte_free() is undefined if the pointer does not match this requirement. + * + * If the pointer is NULL, the function does nothing. + * + * @param ptr + * The pointer to memory to be freed. + */ +void +rte_free(void *ptr); + /** * This function allocates memory from the huge-page area of memory. The memory * is not cleared. In NUMA systems, the memory allocated resides on the same @@ -54,7 +74,8 @@ struct rte_malloc_socket_stats { */ void * rte_malloc(const char *type, size_t size, unsigned align) - __rte_alloc_size(2); + __rte_alloc_size(2) __rte_alloc_align(3) + __rte_malloc __rte_dealloc_free; /** * Allocate zeroed memory from the heap. @@ -81,7 +102,8 @@ rte_malloc(const char *type, size_t size, unsigned align) */ void * rte_zmalloc(const char *type, size_t size, unsigned align) - __rte_alloc_size(2); + __rte_alloc_size(2) __rte_alloc_align(3) + __rte_malloc __rte_dealloc_free; /** * Replacement function for calloc(), using huge-page memory. Memory area is @@ -108,7 +130,8 @@ rte_zmalloc(const char *type, size_t size, unsigned align) */ void * rte_calloc(const char *type, size_t num, size_t size, unsigned align) - __rte_alloc_size(2, 3); + __rte_alloc_size(2, 3) __rte_alloc_align(4) + __rte_malloc __rte_dealloc_free; /** * Replacement function for realloc(), using huge-page memory. Reserved area @@ -132,7 +155,8 @@ rte_calloc(const char *type, size_t num, size_t size, unsigned align) */ void * rte_realloc(void *ptr, size_t size, unsigned int align) - __rte_alloc_size(2); + __rte_alloc_size(2) __rte_alloc_align(3) + __rte_malloc __rte_dealloc_free; /** * Replacement function for realloc(), using huge-page memory. Reserved area @@ -158,7 +182,8 @@ rte_realloc(void *ptr, size_t size, unsigned int align) */ void * rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) - __rte_alloc_size(2); + __rte_alloc_size(2) __rte_alloc_align(3) + __rte_malloc __rte_dealloc_free; /** * This function allocates memory from the huge-page area of memory. The memory @@ -185,7 +210,8 @@ rte_realloc_socket(void *ptr, size_t size, unsigned int align, int socket) */ void * rte_malloc_socket(const char *type, size_t size, unsigned align, int socket) - __rte_alloc_size(2); + __rte_alloc_size(2) __rte_alloc_align(3) + __rte_malloc __rte_dealloc_free; /** * Allocate zeroed memory from the heap. @@ -214,7 +240,8 @@ rte_malloc_socket(const char *type, size_t size, unsigned align, int socket) */ void * rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket) - __rte_alloc_size(2); + __rte_alloc_size(2) __rte_alloc_align(3) + __rte_malloc __rte_dealloc_free; /** * Replacement function for calloc(), using huge-page memory. Memory area is @@ -243,22 +270,8 @@ rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket) */ void * 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. - * - * This pointer must have been returned by a previous call to - * rte_malloc(), rte_zmalloc(), rte_calloc() or rte_realloc(). The behaviour of - * rte_free() is undefined if the pointer does not match this requirement. - * - * If the pointer is NULL, the function does nothing. - * - * @param ptr - * The pointer to memory to be freed. - */ -void -rte_free(void *ptr); + __rte_alloc_size(2, 3) __rte_alloc_align(4) + __rte_malloc __rte_dealloc_free; /** * If malloc debug is enabled, check a memory block for header -- 2.45.2