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 3958545A6D; Mon, 30 Sep 2024 20:47:59 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id CFA7340DDA; Mon, 30 Sep 2024 20:46:40 +0200 (CEST) Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) by mails.dpdk.org (Postfix) with ESMTP id C8CC4409FA for ; Mon, 30 Sep 2024 20:46:25 +0200 (CEST) Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-7e6d04f74faso4052897a12.1 for ; Mon, 30 Sep 2024 11:46:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1727721985; x=1728326785; 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=UQOUEa1HPNhDqwE4iMVctoApRU2HdlZ8qolWZxbmgfY=; b=IuZbDcBJJv3qo1BT94badqA9g/KAbaKllBqXysERYL+z6A+EffspKi6nr5+XfURQOz 2/mLzuzcFyyvPfSuhNIZlk/SRnCQiSjTzlDm0nQQaQqYXjTPJ3Qo7g4+FHV8yUWGXpo+ 2lStPEOgOQIYT8kXGH73I5w9nDq4lK/kSUi0DCAnQnGn2+dEKYQukzDi/1cOapudptiz OT3l+zSfUJGXZeHA5/UCq4uztMa4pocaXhfTx/qgNBwNZXT37E94U2FAt8prCuyyH3ti 8fiG18YyETO7H3NOrvlcsG228QXWqZdVRlpikc1pk3eI2PLLcCbuxJv2QA28MZCPjqs5 m//g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727721985; x=1728326785; 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=UQOUEa1HPNhDqwE4iMVctoApRU2HdlZ8qolWZxbmgfY=; b=sLaRxOuO/cycQALg6EPelPe83pO5mNo2+U/1F2onHIG7Owf7inx5QtjYREr8BC+pzj Va2rWxA71rplKCA+lsznm2qdmI+8Yr8w4sSmuZKs7RISXQ/ENk2orkcLniso/jr5+t6H G10hrce4S9ClkXoczrk/LJULU+FQEHOS0sDgbz3OwZLMds+tHtqUbmc012JQkCkd24VA X+X0DmrC7I7fEVxjvwx6Ay0zABgloATya5rN2Rgr7JpMFYPpBepNlrhKkvOsklVeIoz1 nLGhRsoqKyGWum/12A4RRgFelOL2yYtsPYbx+omLXnZhdF33jMl8FgvPHuO30Cd0BecP e8tg== X-Gm-Message-State: AOJu0YwNe4cWmZhFfdraHuRPOgbQQZN0YbTvYLQ6TEDPmIf/daFKYo++ zNjkBeU75jOlJCQkTaVHxU4vjEwVLMUoXJVyuWSsdr5C26GSF/8+Xdzf7d9i6p4bI19kTTifHQ3 +u7mc/w== X-Google-Smtp-Source: AGHT+IHYGsYOA0w7W4ebLBgB2WG9EzFmZNkLTc8XMFBVOIipWEpOfYJxT1cGEtq2VAXAklFRo6geZA== X-Received: by 2002:a05:6a20:db0a:b0:1d5:2fc7:a81a with SMTP id adf61e73a8af0-1d52fc7aa9bmr26054637.20.1727721984901; Mon, 30 Sep 2024 11:46:24 -0700 (PDT) Received: from hermes.local (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71b264bcca6sm6581895b3a.78.2024.09.30.11.46.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Sep 2024 11:46:24 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Chengwen Feng , Tyler Retzlaff , Anatoly Burakov Subject: [PATCH v4 17/17] eal: add function attributes for allocation functions Date: Mon, 30 Sep 2024 11:44:11 -0700 Message-ID: <20240930184600.7092-18-stephen@networkplumber.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240930184600.7092-1-stephen@networkplumber.org> References: <20240927204742.546164-1-stephen@networkplumber.org> <20240930184600.7092-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. Recent 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(). Signed-off-by: Stephen Hemminger Acked-by: Chengwen Feng --- doc/guides/rel_notes/release_24_11.rst | 8 ++++ lib/eal/include/rte_common.h | 36 +++++++++++++++++ lib/eal/include/rte_malloc.h | 55 +++++++++++++++----------- 3 files changed, 76 insertions(+), 23 deletions(-) diff --git a/doc/guides/rel_notes/release_24_11.rst b/doc/guides/rel_notes/release_24_11.rst index 0ff70d9057..f27a37eac4 100644 --- a/doc/guides/rel_notes/release_24_11.rst +++ b/doc/guides/rel_notes/release_24_11.rst @@ -55,6 +55,14 @@ New Features Also, make sure to start the actual text at the margin. ======================================================= +* **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..d94d2b9a12 100644 --- a/lib/eal/include/rte_common.h +++ b/lib/eal/include/rte_common.h @@ -228,6 +228,42 @@ 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 + * dealloctor function is used for this pointer. + */ +#if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 110000) +#define __rte_dealloc(dealloc, argno) \ + __attribute__((malloc(dealloc, argno))) +#define __rte_dealloc_free __rte_dealloc(rte_free, 1) +#else +#define __rte_dealloc(dealloc, argno) +#define __rte_dealloc_free +#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..9261605939 100644 --- a/lib/eal/include/rte_malloc.h +++ b/lib/eal/include/rte_malloc.h @@ -31,6 +31,22 @@ struct rte_malloc_socket_stats { size_t heap_allocsz_bytes; /**< Total allocated bytes on heap */ }; + +/** + * 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 +70,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 +98,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 +126,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 +151,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 +178,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 +206,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 +236,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 +266,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