From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by dpdk.org (Postfix) with ESMTP id 70F7A493D for ; Thu, 19 Jul 2018 18:20:33 +0200 (CEST) Received: by mail-pg1-f196.google.com with SMTP id y4-v6so4205326pgp.9 for ; Thu, 19 Jul 2018 09:20:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=QvK0UsH4TqPMirZEPfKx93vg3IrrZZnq3Jhh3P08o2c=; b=lCfY9TtSRcjLsJscskX9KbCyRt2onevsUB8BH5W1g6wHka0wU6stU2NMY9Ii/0YX9p 5hnsLdfiCifJI77YCJ705Rqs2GYTPf41QcVI9Q+fol6HuGVr37CAS9GtOyDeEJYc1z+2 aQfJkBAWLMNSM3psuBInTYdItJm1psk7pREqT1NI/c9ZdTsmrHHo7YyTh8LQGpVtswSA 4J/Wg5o0cPvERvbF5z/rBDXanESCDA2bB8PnqZanEUFLjiumT5I8Yat3XMlnP/tGvXBJ pIPK9y8SCTBHWZCuQD3JFSDV4yAOti/kBD4zDQHadnnh18obRE7Qr3Zs3XPfg/SaFcpo S0wA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=QvK0UsH4TqPMirZEPfKx93vg3IrrZZnq3Jhh3P08o2c=; b=NuytzXnQVubzG4Qg5l0Jxe6Xo62QtibmjqETprMthvz2vyhrO2rOqUN1ZcZX7SHSYm csWzZQJY9hgCAkTVpOo+uum/ZaS9pfsXL9wVgIM+xm4GK1nixKLtqf2QjxYnmf/QPDgR UtMX8uGhi8mKmy+jydIgiUfKX+GbBjhFj4fSzLiLXMmWEWWZS397VqtzRGq5DAKrqosp kpL7pczLS7aBVmonq4QrJU/kzPtHfcw787SSa5qLqxFP5QIEAx2wlnr019d4ERu9316U trEIwiTv/hU/NcVXFfJYbSrXNuV3gn8PElRSpnhu/yAFxwFxhTppbzYlpzl4dz/ULj/c Cuyg== X-Gm-Message-State: AOUpUlFTT8GlhkBs9cUC8i0cNhtCwUNVJYnGTL0xAIMPdLrvlyQTzy4K N2Y6L7ANST5MiIzYGvJdgZrA767XEZ8= X-Google-Smtp-Source: AAOMgpetHZSZr3ls+s2WdAHROr8FN7Wab35lZlZE2CoOpynlZVeco1i/AjV2l80Iyw7ub+XGo9OKSA== X-Received: by 2002:a65:5304:: with SMTP id m4-v6mr10498234pgq.250.1532017232189; Thu, 19 Jul 2018 09:20:32 -0700 (PDT) Received: from xeon-e3.wavecable.com (204-195-22-127.wavecable.com. [204.195.22.127]) by smtp.gmail.com with ESMTPSA id v82-v6sm21465151pfd.64.2018.07.19.09.20.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Jul 2018 09:20:31 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Date: Thu, 19 Jul 2018 09:20:29 -0700 Message-Id: <20180719162029.30962-1-stephen@networkplumber.org> X-Mailer: git-send-email 2.18.0 Subject: [dpdk-dev] [RFC v2] mem: poison memory when freed X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 19 Jul 2018 16:20:33 -0000 DPDK malloc library allows broken programs to work because the semantics of zmalloc and malloc are the same. This patch enables a more secure model which will catch (and crash) programs that reuse memory already freed if RTE_MALLOC_DEBUG is enabled. Signed-off-by: Stephen Hemminger --- v2 - keep original behavior unless RTE_MALLOC_DEBUG is enabled. lib/librte_eal/common/malloc_elem.c | 17 ++++++++++++++--- lib/librte_eal/common/rte_malloc.c | 8 +++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/librte_eal/common/malloc_elem.c b/lib/librte_eal/common/malloc_elem.c index efcb82677198..6fc63c53a75d 100644 --- a/lib/librte_eal/common/malloc_elem.c +++ b/lib/librte_eal/common/malloc_elem.c @@ -23,6 +23,16 @@ #include "malloc_elem.h" #include "malloc_heap.h" +/* If debugging is enabled, freed memory is set to poison value + * to catch buggy programs. Otherwise, freed memory is zerod + * to avoid having to zero in zmalloc + */ +#ifdef RTE_MALLOC_DEBUG +#define MALLOC_POISON 0x6b +#else +#define MALLOC_POISON 0 +#endif + size_t malloc_elem_find_max_iova_contig(struct malloc_elem *elem, size_t align) { @@ -476,7 +486,7 @@ malloc_elem_join_adjacent_free(struct malloc_elem *elem) join_elem(elem, elem->next); /* erase header, trailer and pad */ - memset(erase, 0, erase_len); + memset(erase, MALLOC_POISON, erase_len); } /* @@ -500,7 +510,7 @@ malloc_elem_join_adjacent_free(struct malloc_elem *elem) join_elem(new_elem, elem); /* erase header, trailer and pad */ - memset(erase, 0, erase_len); + memset(erase, MALLOC_POISON, erase_len); elem = new_elem; } @@ -531,7 +541,8 @@ malloc_elem_free(struct malloc_elem *elem) /* decrease heap's count of allocated elements */ elem->heap->alloc_count--; - memset(ptr, 0, data_len); + /* poison memory */ + memset(ptr, MALLOC_POISON, data_len); return elem; } diff --git a/lib/librte_eal/common/rte_malloc.c b/lib/librte_eal/common/rte_malloc.c index b51a6d111bde..ed253149bb6b 100644 --- a/lib/librte_eal/common/rte_malloc.c +++ b/lib/librte_eal/common/rte_malloc.c @@ -70,7 +70,13 @@ rte_malloc(const char *type, size_t size, unsigned align) void * rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket) { - return rte_malloc_socket(type, size, align, socket); + void *ptr = rte_malloc_socket(type, size, align, socket); + +#ifndef RTE_MALLOC_DEBUG + if (ptr != NULL) + memset(ptr, 0, size); +#endif + return ptr; } /* -- 2.18.0