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 73CFE45A5D; Sun, 29 Sep 2024 17:43:14 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 03E2040A80; Sun, 29 Sep 2024 17:41:48 +0200 (CEST) Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by mails.dpdk.org (Postfix) with ESMTP id 84CB740673 for ; Sun, 29 Sep 2024 17:41:35 +0200 (CEST) Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-7db233cef22so3002925a12.0 for ; Sun, 29 Sep 2024 08:41:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1727624495; x=1728229295; 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=R8Ok1+kq8bi6yHExAh8y81w0B9yxmLJ61X/1EwvijG0=; b=x0RlP+/JqaHXwCaHgltzIx4UBeSxQCRDWUBC8I1IkV5GNJvw7uAv8st7Tc8tpBJEEx vtvrkAohQewzhIs+l4Hft3RxB+Aal04xbNQR+3qsokRCEGKMcJ1ZWkBxjS1zi+ntx2mc /XjIYdLEd0+J+dLHfN6e62flP6wgeC8benOP0ULDfnjZgyZZBlT0Ue8Ic0sbDzpKHI8t AkOoRUU1J9kD0+YkyAHe7RDRgfwJ5TFs9JcymJ1wi1X5jAG7V9FqMHDsppUG9OCSSQT+ YvReUAwQvUiwyolsA0rKM6E+xyivsZew2d3Q45lhIx+wySjLrrYicZ+4/v7nEWitd2h0 //zA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727624495; x=1728229295; 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=R8Ok1+kq8bi6yHExAh8y81w0B9yxmLJ61X/1EwvijG0=; b=aHekT9X8ZGwfx49BYSkVjLk0p/7Tu2aZSPcPl+ULubdMOtkUQsUIeN5+draxhyrGry 6DEyAIL4qSVjMfn2Nm4IZHmNtT91dN/fXuXVpcTXmGhalZ0gL67q7eQ5nV/Bljj7jzQf 43HAHxXW0IXmURzMok89JDUn4ZNtaCwi7KVjdhL7hJTi66CSSPxPEfBSBs7Iqa2rc0eA BxoNj4BSJM+hZ+casWLczulnbk+DPjSKJhyPWER4C26mUED/RocJT0cjCQkbHhfaVdnM rJfHRTpWXjKHpE3c95XbUiFwpxHCQVUe0aHLXqGtrWCK9CWDnVsvB6v6ozONVEpyrZu9 6xMA== X-Gm-Message-State: AOJu0YyKWt08KuqjK35+EBtekTo3eNnkSA/UOmK7HBf7fdAAZz0t6dTc YRHbER4K3vboKFxp0hv5t/v/WEKak+EjV5VgNcT4PA3pewt2gbSQInYjED4AmKvTz+a+hm/WJNc dPAI= X-Google-Smtp-Source: AGHT+IG4MUCIu/kv7Mvdchjd+Bkr3eZKqbmntL3OSWhm/6/BISUVeCeyJP4H2WfJNwyJG0s0RHFzJA== X-Received: by 2002:a05:6a21:114d:b0:1c6:a680:ef3d with SMTP id adf61e73a8af0-1d4fa6bb313mr16520225637.28.1727624494621; Sun, 29 Sep 2024 08:41:34 -0700 (PDT) Received: from hermes.local (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71b2649c775sm4707416b3a.29.2024.09.29.08.41.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Sep 2024 08:41:34 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Anatoly Burakov , Tyler Retzlaff Subject: [PATCH v3 18/18] eal: add alloc_function attribute to rte_malloc Date: Sun, 29 Sep 2024 08:34:43 -0700 Message-ID: <20240929154107.62539-19-stephen@networkplumber.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240929154107.62539-1-stephen@networkplumber.org> References: <20240927204742.546164-1-stephen@networkplumber.org> <20240929154107.62539-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 Use the GCC function attribute to detect cases where memory is allocated with rte_malloc and freed incorrectly with libc version of free (and vice versa). Also will detect some other pointer mismatches. Signed-off-by: Stephen Hemminger --- doc/guides/rel_notes/release_24_11.rst | 8 ++++ lib/eal/include/rte_malloc.h | 55 +++++++++++++++----------- 2 files changed, 40 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_malloc.h b/lib/eal/include/rte_malloc.h index cf3c174022..c7af96fcba 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 @@ -55,7 +71,8 @@ struct rte_malloc_socket_stats { void * rte_malloc(const char *type, size_t size, unsigned align) __rte_alloc_size(2) - __rte_alloc_align(3); + __rte_alloc_align(3) + __rte_alloc_func(rte_free, 1); /** * Allocate zeroed memory from the heap. @@ -83,7 +100,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_align(3); + __rte_alloc_align(3) + __rte_alloc_func(rte_free, 1); /** * Replacement function for calloc(), using huge-page memory. Memory area is @@ -111,7 +129,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_align(4); + __rte_alloc_align(4) + __rte_alloc_func(rte_free, 1); /** * Replacement function for realloc(), using huge-page memory. Reserved area @@ -136,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_align(3); + __rte_alloc_align(3) + __rte_alloc_func(rte_free, 1); /** * Replacement function for realloc(), using huge-page memory. Reserved area @@ -163,7 +183,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_align(3); + __rte_alloc_align(3) + __rte_alloc_func(rte_free, 1); /** * This function allocates memory from the huge-page area of memory. The memory @@ -191,7 +212,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_align(3); + __rte_alloc_align(3) + __rte_alloc_func(rte_free, 1); /** * Allocate zeroed memory from the heap. @@ -221,7 +243,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_align(3); + __rte_alloc_align(3) + __rte_alloc_func(rte_free, 1); /** * Replacement function for calloc(), using huge-page memory. Memory area is @@ -251,22 +274,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) - __rte_alloc_align(4); - -/** - * 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_align(4) + __rte_alloc_func(rte_free, 1); /** * If malloc debug is enabled, check a memory block for header -- 2.45.2