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 D34DC45A7E; Tue, 1 Oct 2024 18:39:08 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id C406A40E22; Tue, 1 Oct 2024 18:37:45 +0200 (CEST) Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by mails.dpdk.org (Postfix) with ESMTP id CC4A240A7F for ; Tue, 1 Oct 2024 18:37:34 +0200 (CEST) Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-717934728adso4484386b3a.2 for ; Tue, 01 Oct 2024 09:37:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1727800654; x=1728405454; 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=yEjSuNlLdOrZw5bvsDXNHkJONjqNnrNE+5cx8YMpLKk=; b=DuXe+oLoDxSs0cub+3caZYDDGPEdkp6qOHw0VcrI7sKR/v5iCkjA/pdVAy+rf4z/Cp riYzPrmdfUbKftyWktPSBXVr2/bXce2t29Ml5lydNy8eUFWuuu9NCp0r/PxIFGuI5qhN yZnif4rM6j9poUoxSdBajwtPb+I/n6++4LH0IGTizmz1voLAw5b/NRxf/hOV8EjmoI3q +/G8DRFpvtve9BcRjHmehOdbojWgzgXhqACEE2dQZyRhHZs5BUfU3YHrnF/tOM8uDoXM y0R0SD9VFdoyiJR7gRsvtDSzreAIHxLty8+chKbAXrLgSiQxu0sDatftiOdshg5PE2Pr CG4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727800654; x=1728405454; 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=yEjSuNlLdOrZw5bvsDXNHkJONjqNnrNE+5cx8YMpLKk=; b=mzVBoKWuLzYr+YGO5iDitHKlK4oCFjIrOmOwFhvcnEXC43dP9jLwohICFK1lAnbGuZ kg7vBFr8OVtemGscncIzqSOj+4c/RqZ9n0Kz2XgX0bICh6lTVygcOeZr+JHLhrt18LcX 9JKwajwiFId0GYh/ZeXGegNtwY+BAFAFudZ1kh5oJINrhfMgnadTK66Rm5ZLjCWkUwWw pZzl8PqOh0WtCQCJkiFVhwLnyg2POu0gszohQy91lxnAKJjqiInxbSrLuOtXn+FTJB6c N7xZkF4WqJ4N9VqhXoqoSnpnQOfHxOY2N71+Yr7jcFZhSVYBfNtwuRqqKklmHsNquZIO a1dw== X-Gm-Message-State: AOJu0YxUnq7OO+BXrOIy+X6DUIlCuiaKH6j+4sp74Xow53O+QDR3upoV 0/lAOosuTnXYXyPM6FTrZxLFMqyF5K2n6yVepmvzxjuerls3gsEzUG7RmVN1xnmJf1WNWTPoNL1 ++Sw= X-Google-Smtp-Source: AGHT+IFo0maJjZUvokb8rml0k1yLXRtvhgEc39z/5mF+WuGG9xuhAJ/YUNHvK5mCVIYOy73043ExgQ== X-Received: by 2002:a05:6a00:178e:b0:714:3831:ec91 with SMTP id d2e1a72fcca58-71dc5d87c59mr326835b3a.25.1727800653969; Tue, 01 Oct 2024 09:37:33 -0700 (PDT) Received: from hermes.local (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71b26518818sm8213565b3a.129.2024.10.01.09.37.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2024 09:37:33 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Chengwen Feng , Anatoly Burakov , Tyler Retzlaff Subject: [PATCH v5 17/17] eal: add function attributes for allocation functions Date: Tue, 1 Oct 2024 09:35:40 -0700 Message-ID: <20241001163708.355128-18-stephen@networkplumber.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241001163708.355128-1-stephen@networkplumber.org> References: <20240927204742.546164-1-stephen@networkplumber.org> <20241001163708.355128-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 | 55 +++++++++++++++----------- 3 files changed, 74 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..595cadd5b8 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 + * 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