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 25D0645AE2; Tue, 8 Oct 2024 17:45:48 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 8DE0140ED9; Tue, 8 Oct 2024 17:43:38 +0200 (CEST) Received: from mail-pg1-f171.google.com (mail-pg1-f171.google.com [209.85.215.171]) by mails.dpdk.org (Postfix) with ESMTP id 8866440E44 for ; Tue, 8 Oct 2024 17:43:27 +0200 (CEST) Received: by mail-pg1-f171.google.com with SMTP id 41be03b00d2f7-7cd8803fe0aso3952935a12.0 for ; Tue, 08 Oct 2024 08:43:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1728402207; x=1729007007; 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=ZC3uMa078ys6VQBUGH8lCcNQL4RDh6ttTAN0TwZhXdv4/m/wh9IbBufykYHFjrIT9I gF2L8iF1x+KZrJTpJKT91CRs+BevpeGA0jaJ8TWHkDE6InLPiTGHhZjNyZ97iwBcPKxd nRd/qNye6X7oZuJCtA/C/TnWID9dXIJH1DJcxuoU7CRKBr2OUzQ7x9ED5vbGkXJo3m3U aQ4WsznSSQ5D0n9gjJuUo5M9lheN0G4RJZLpyZdEihCxxPjUBnuwM9D2f9SEZLjkQkzK 2I60yop56up/+cETXZ0crnyHjnRH29Vjrfez23JI/gtcJZUql0R+n05kWxAaq0uFY4OL C2xQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728402207; x=1729007007; 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=wDLIeRDENulVJ/UoF7Uzj596LO1SECl9fd6H3U9xaXxlKQEuzYfzp7IouX8fmamYB2 bMrIC4jdFS5NKHmO+P3Jl/T9Jp3XXiD5SQEr6fJcM7G6lAxhuwZTfC11/PJbquxicmi9 DAXpftTLHyVnb2oTXWl8F4MU5Ifo0wtnPproFcHDrytaigtqvdRc2smduH8lbGUdIncf e08flVOPR9DykyIYwMQsepH3FzBZXLGLMT9W6nZNPkiPUHqOmKY1iXtrjGhe+vuvicnP yqwByYVO+a0JAIy+E/wgZrmKRjm79zLNNy26gv/v7RKjQge1+k5Wjkco4LP9hHvSFSnU agDg== X-Gm-Message-State: AOJu0YwVRvnGf0UR6bEgTVbXZTcmJxpoeeYBem95SnZhuH5heMNQo0Po JeaR9opHntS0Bmrek0rbfteTjp9/BxdnV5SiUPIU2iE9gl/Wf+jmxufEHeIDBT2UUO9RLh1N22G zQvBBZw== X-Google-Smtp-Source: AGHT+IGzVClJp0Yv5l7jFEyYovKLGZEWCKZEI6IjwancnU3Go+IVx7CarGfvQaroaDoxdstUQT/yIw== X-Received: by 2002:a05:6a20:b40a:b0:1d2:e903:432f with SMTP id adf61e73a8af0-1d6dfae338bmr20332914637.46.1728402206655; Tue, 08 Oct 2024 08:43:26 -0700 (PDT) Received: from hermes.local (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0ccf01dsm6293527b3a.70.2024.10.08.08.43.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Oct 2024 08:43:26 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Chengwen Feng , Anatoly Burakov , Tyler Retzlaff Subject: [PATCH v8 17/17] eal: add function attributes for allocation functions Date: Tue, 8 Oct 2024 08:41:47 -0700 Message-ID: <20241008154301.66192-18-stephen@networkplumber.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241008154301.66192-1-stephen@networkplumber.org> References: <20240927204742.546164-1-stephen@networkplumber.org> <20241008154301.66192-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