Skip to contentSkip to conversation

malloc: Enable profiling dlmalloc with valgrind

PATCH Superseded Sean Anderson 2 participants 12 messages May 3, 2021 – May 5, 2021

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover malloc: Enable profiling dlmalloc with valgrind

This series adds support for running valgrind against U-Boot's internal malloc. This allows for much more useful reports to be generated.

Sean Anderson (4):
Add valgrind headers to U-Boot
malloc: Annotate allocator for valgrind
doc: sandbox: Document how to run sandbox with valgrind
malloc: Fix sbrk clearing memory after freeing it instead of before

 Kconfig                     |   14 +
 Licenses/README             |    1 +
 Licenses/bzip2-1.0.6.txt    |   30 +
 common/dlmalloc.c           |   39 +-
 common/malloc_simple.c      |   10 +
 doc/arch/sandbox.rst        |   14 +-
 include/malloc.h            |    4 +
 include/valgrind/memcheck.h |  251 ++
 include/valgrind/valgrind.h | 7106 +++++++++++++++++++++++++++++++++++
 scripts/u-boot.supp         |   30 +
 10 files changed, 7490 insertions(+), 9 deletions(-)
 create mode 100644 Licenses/bzip2-1.0.6.txt
 create mode 100644 include/valgrind/memcheck.h
 create mode 100644 include/valgrind/valgrind.h
 create mode 100644 scripts/u-boot.supp
Signature · 1 line
2.31.0
#1 lore raw

PATCH 1/4 Add valgrind headers to U-Boot

Valgrind uses magic code sequences to define an ABI that the client may use to request behavior from the host. In particular, this may be used to inform valgrind about custom allocators, such as the one used in U-Boot.

This adds headers defining these sequences to U-Boot. It also adds a config option to disable emission of these sequences entirely, in the (likely) event that the user does not wish to use valgrind. Note that this option was originally called NVALGRIND, but was renamed (and inverted) to CONFIG_VALGRIND.

These headers were copied from valgrind 3.16.1-4 as distributed in Arch Linux. They are licensed with the bzip2 1.16 license. This appears to be a BSD license with some clauses from Zlib.

---

 Kconfig                     |   14 +
 Licenses/README             |    1 +
 Licenses/bzip2-1.0.6.txt    |   30 +
 include/valgrind/memcheck.h |  251 ++
 include/valgrind/valgrind.h | 7106 +++++++++++++++++++++++++++++++++++
 5 files changed, 7402 insertions(+)
 create mode 100644 Licenses/bzip2-1.0.6.txt
 create mode 100644 include/valgrind/memcheck.h
 create mode 100644 include/valgrind/valgrind.h
Kconfig +14 -0
index 86f0a39bb0..c05f304325 100644
@@ -240,6 +240,20 @@ config TPL_SYS_MALLOC_F_LEN
 	  particular needs this to operate, so that it can allocate the
 	  initial serial device and any others that are needed.
 
+config VALGRIND
+	bool "Inform valgrind about memory allocations"
+	help
+	  Valgrind is an instrumentation framework for building dynamic analysis
+	  tools. In particular, it may be used to detect memory management bugs
+	  in U-Boot. It relies on knowing when heap blocks are allocated in
+	  order to give accurate results. This happens automatically for
+	  standard allocator functions provided by the host OS. However, this
+	  doesn't automatically happen for U-Boot's malloc implementation.
+
+	  Enable this option to annotate U-Boot's malloc implementation so that
+	  it can be handled accurately by Valgrind. If you aren't planning on
+	  using valgrind to debug U-Boot, say 'n'.
+
 menuconfig EXPERT
 	bool "Configure standard U-Boot features (expert users)"
 	default y
Licenses/README +1 -0
index c23ad216fc..d09f0fe2cb 100644
@@ -139,6 +139,7 @@ License identifier syntax
 
 Full name					SPDX Identifier	OSI Approved	File name		URI
 =======================================================================================================================================
+bzip2 and libbzip2 License v1.0.6		bzip2-1.0.6			bzip2-1.0.6.txt		https://spdx.org/licenses/bzip2-1.0.6.html
 GNU General Public License v2.0 only		GPL-2.0		Y		gpl-2.0.txt		http://www.gnu.org/licenses/gpl-2.0.txt
 GNU General Public License v2.0 or later	GPL-2.0+	Y		gpl-2.0.txt		http://www.gnu.org/licenses/gpl-2.0.txt
 GNU Library General Public License v2 or later	LGPL-2.0+	Y		lgpl-2.0.txt		http://www.gnu.org/licenses/old-licenses/lgpl-2.0.txt
Licenses/bzip2-1.0.6.txt new file +30 -0
new file mode 100644
index 0000000000..2adb26c394
@@ -0,0 +1,30 @@
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+1. Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+2. The origin of this software must not be misrepresented; you must 
+   not claim that you wrote the original software.  If you use this 
+   software in a product, an acknowledgment in the product 
+   documentation would be appreciated but is not required.
+
+3. Altered source versions must be plainly marked as such, and must
+   not be misrepresented as being the original software.
+
+4. The name of the author may not be used to endorse or promote 
+   products derived from this software without specific prior written 
+   permission.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
include/valgrind/memcheck.h new file +251 -0

251 lines not on this page · raw message

include/valgrind/valgrind.h new file +7106 -0

2,692 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.31.0

Part of the diff is not shown here. View the raw message.

#2 lore raw

PATCH 1/4 Add valgrind headers to U-Boot

Quoted 18 lines · Sean Anderson

Valgrind uses magic code sequences to define an ABI that the client may use to request behavior from the host. In particular, this may be used to inform valgrind about custom allocators, such as the one used in U-Boot.

This adds headers defining these sequences to U-Boot. It also adds a config option to disable emission of these sequences entirely, in the (likely) event that the user does not wish to use valgrind. Note that this option was originally called NVALGRIND, but was renamed (and inverted) to CONFIG_VALGRIND.

These headers were copied from valgrind 3.16.1-4 as distributed in Arch Linux. They are licensed with the bzip2 1.16 license. This appears to be a BSD license with some clauses from Zlib.

---

Quoted 9 lines
 Kconfig                     |   14 +
 Licenses/README             |    1 +
 Licenses/bzip2-1.0.6.txt    |   30 +
 include/valgrind/memcheck.h |  251 ++
 include/valgrind/valgrind.h | 7106 +++++++++++++++++++++++++++++++++++
 5 files changed, 7402 insertions(+)
 create mode 100644 Licenses/bzip2-1.0.6.txt
 create mode 100644 include/valgrind/memcheck.h
 create mode 100644 include/valgrind/valgrind.h
#3 lore raw

PATCH 2/4 malloc: Annotate allocator for valgrind

In reply to Sean Anderson

This annotates malloc and friends so that valgrind can track the heap. To do this, we need to follow a few rules:

* Call VALGRIND_MALLOCLIKE_BLOCK whenever we malloc something
* Call VALGRIND_FREELIKE_BLOCK whenever we free something (generally after we have done our bookkeeping)
* Call VALGRIND_RESIZEINPLACE_BLOCK whenever we change the size of an allocation. Generally this just needs to happen in realloc, but only if the address stays the same.

In addition to the above, dlmalloc itself tends to make a lot of accesses which we know are safe, but which would be unsafe outside of dlmalloc. For this reason, we provide a suppression file which ignores errors ocurring in dlmalloc.c

---

 common/dlmalloc.c      | 29 ++++++++++++++++++++++++++++-
 common/malloc_simple.c | 10 ++++++++++
 include/malloc.h       |  4 ++++
 scripts/u-boot.supp    | 30 ++++++++++++++++++++++++++++++
 4 files changed, 72 insertions(+), 1 deletion(-)
 create mode 100644 scripts/u-boot.supp
common/dlmalloc.c +28 -1
index cf0270a9c1..05c8fd87e7 100644
@@ -8,6 +8,7 @@
 
 #include <malloc.h>
 #include <asm/io.h>
+#include <valgrind/memcheck.h>
 
 #ifdef DEBUG
 #if __STD_C
@@ -1329,6 +1330,7 @@ Void_t* mALLOc(bytes) size_t bytes;
       unlink(victim, bck, fwd);
       set_inuse_bit_at_offset(victim, victim_size);
       check_malloced_chunk(victim, nb);
+      VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
       return chunk2mem(victim);
     }
 
@@ -1356,6 +1358,7 @@ Void_t* mALLOc(bytes) size_t bytes;
 	unlink(victim, bck, fwd);
 	set_inuse_bit_at_offset(victim, victim_size);
 	check_malloced_chunk(victim, nb);
+        VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
 	return chunk2mem(victim);
       }
     }
@@ -1379,6 +1382,7 @@ Void_t* mALLOc(bytes) size_t bytes;
       set_head(remainder, remainder_size | PREV_INUSE);
       set_foot(remainder, remainder_size);
       check_malloced_chunk(victim, nb);
+      VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
       return chunk2mem(victim);
     }
 
@@ -1388,6 +1392,7 @@ Void_t* mALLOc(bytes) size_t bytes;
     {
       set_inuse_bit_at_offset(victim, victim_size);
       check_malloced_chunk(victim, nb);
+      VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
       return chunk2mem(victim);
     }
 
@@ -1443,6 +1448,7 @@ Void_t* mALLOc(bytes) size_t bytes;
 	    set_head(remainder, remainder_size | PREV_INUSE);
 	    set_foot(remainder, remainder_size);
 	    check_malloced_chunk(victim, nb);
+	    VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
 	    return chunk2mem(victim);
 	  }
 
@@ -1451,6 +1457,7 @@ Void_t* mALLOc(bytes) size_t bytes;
 	    set_inuse_bit_at_offset(victim, victim_size);
 	    unlink(victim, bck, fwd);
 	    check_malloced_chunk(victim, nb);
+	    VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
 	    return chunk2mem(victim);
 	  }
 
@@ -1499,6 +1506,7 @@ Void_t* mALLOc(bytes) size_t bytes;
     /* If big and would otherwise need to extend, try to use mmap instead */
     if ((unsigned long)nb >= (unsigned long)mmap_threshold &&
 	(victim = mmap_chunk(nb)))
+      VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
       return chunk2mem(victim);
 #endif
 
@@ -1513,6 +1521,7 @@ Void_t* mALLOc(bytes) size_t bytes;
   top = chunk_at_offset(victim, nb);
   set_head(top, remainder_size | PREV_INUSE);
   check_malloced_chunk(victim, nb);
+  VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(victim), bytes, SIZE_SZ, false);
   return chunk2mem(victim);
 
 }
@@ -1561,8 +1570,10 @@ void fREe(mem) Void_t* mem;
 
 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
 	/* free() is a no-op - all the memory will be freed on relocation */
-	if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT))
+	if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) {
+		VALGRIND_FREELIKE_BLOCK(mem, SIZE_SZ);
 		return;
+	}
 #endif
 
   if (mem == NULL)                              /* free(0) has no effect */
@@ -1584,6 +1595,7 @@ void fREe(mem) Void_t* mem;
   sz = hd & ~PREV_INUSE;
   next = chunk_at_offset(p, sz);
   nextsz = chunksize(next);
+  VALGRIND_FREELIKE_BLOCK(mem, SIZE_SZ);
 
   if (next == top)                            /* merge with top */
   {
@@ -1772,6 +1784,8 @@ Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes;
 	  top = chunk_at_offset(oldp, nb);
 	  set_head(top, (newsize - nb) | PREV_INUSE);
 	  set_head_size(oldp, nb);
+	  VALGRIND_RESIZEINPLACE_BLOCK(chunk2mem(oldp), oldsize, bytes,
+				       SIZE_SZ);
 	  return chunk2mem(oldp);
 	}
       }
@@ -1810,10 +1824,12 @@ Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes;
 	    newp = prev;
 	    newsize += prevsize + nextsize;
 	    newmem = chunk2mem(newp);
+	    VALGRIND_MALLOCLIKE_BLOCK(newmem, bytes, SIZE_SZ, false);
 	    MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ);
 	    top = chunk_at_offset(newp, nb);
 	    set_head(top, (newsize - nb) | PREV_INUSE);
 	    set_head_size(newp, nb);
+	    VALGRIND_FREELIKE_BLOCK(oldmem, SIZE_SZ);
 	    return newmem;
 	  }
 	}
@@ -1826,6 +1842,7 @@ Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes;
 	  newp = prev;
 	  newsize += nextsize + prevsize;
 	  newmem = chunk2mem(newp);
+	  VALGRIND_MALLOCLIKE_BLOCK(newmem, bytes, SIZE_SZ, false);
 	  MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ);
 	  goto split;
 	}
@@ -1838,6 +1855,7 @@ Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes;
 	newp = prev;
 	newsize += prevsize;
 	newmem = chunk2mem(newp);
+	VALGRIND_MALLOCLIKE_BLOCK(newmem, bytes, SIZE_SZ, false);
 	MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ);
 	goto split;
       }
@@ -1876,6 +1894,8 @@ Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes;
     set_head_size(newp, nb);
     set_head(remainder, remainder_size | PREV_INUSE);
     set_inuse_bit_at_offset(remainder, remainder_size);
+    VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(remainder), remainder_size, SIZE_SZ,
+			      false);
     fREe(chunk2mem(remainder)); /* let free() deal with it */
   }
   else
@@ -2033,6 +2053,7 @@ Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes;
     set_head_size(p, leadsize);
     fREe(chunk2mem(p));
     p = newp;
+    VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(p), bytes, SIZE_SZ, false);
 
     assert (newsize >= nb && (((unsigned long)(chunk2mem(p))) % alignment) == 0);
   }
@@ -2046,6 +2067,8 @@ Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes;
     remainder = chunk_at_offset(p, nb);
     set_head(remainder, remainder_size | PREV_INUSE);
     set_head_size(p, nb);
+    VALGRIND_MALLOCLIKE_BLOCK(chunk2mem(remainder), remainder_size, SIZE_SZ,
+			      false);
     fREe(chunk2mem(remainder));
   }
 
@@ -2148,7 +2171,11 @@ Void_t* cALLOc(n, elem_size) size_t n; size_t elem_size;
 #endif
 #endif
 
+    /* Prevent valgrind complaining that we're zeroing unallocated space */
+    VALGRIND_RESIZEINPLACE_BLOCK(mem, sz, csz - SIZE_SZ, SIZE_SZ);
     MALLOC_ZERO(mem, csz - SIZE_SZ);
+    VALGRIND_RESIZEINPLACE_BLOCK(mem, csz - SIZE_SZ, sz, SIZE_SZ);
+    VALGRIND_MAKE_MEM_DEFINED(mem, sz);
     return mem;
   }
 }
common/malloc_simple.c +10 -0
index 0267fb6bec..205e2c3c11 100644
@@ -13,6 +13,7 @@
 #include <mapmem.h>
 #include <asm/global_data.h>
 #include <asm/io.h>
+#include <valgrind/valgrind.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -45,6 +46,7 @@ void *malloc_simple(size_t bytes)
 		return ptr;
 
 	log_debug("%lx\n", (ulong)ptr);
+	VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false);
 
 	return ptr;
 }
@@ -57,6 +59,7 @@ void *memalign_simple(size_t align, size_t bytes)
 	if (!ptr)
 		return ptr;
 	log_debug("aligned to %lx\n", (ulong)ptr);
+	VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false);
 
 	return ptr;
 }
@@ -74,6 +77,13 @@ void *calloc(size_t nmemb, size_t elem_size)
 
 	return ptr;
 }
+
+#if IS_ENABLED(CONFIG_VALGRIND)
+void free_simple(void *ptr)
+{
+	VALGRIND_FREELIKE_BLOCK(ptr, 0);
+}
+#endif
 #endif
 
 void malloc_simple_info(void)
include/malloc.h +4 -0
index 024b18be00..6f6087571b 100644
@@ -886,7 +886,11 @@ void malloc_simple_info(void);
 #define malloc malloc_simple
 #define realloc realloc_simple
 #define memalign memalign_simple
+#if IS_ENABLED(CONFIG_VALGRIND)
+#define free free_simple
+#else
 static inline void free(void *ptr) {}
+#endif
 void *calloc(size_t nmemb, size_t size);
 void *realloc_simple(void *ptr, size_t size);
 #else
scripts/u-boot.supp new file +30 -0
new file mode 100644
index 0000000000..bbd0426bb7
@@ -0,0 +1,30 @@
+{
+	dlmalloc
+	Memcheck:Addr1
+	src:dlmalloc.c
+}
+{
+	dlmalloc
+	Memcheck:Addr4
+	src:dlmalloc.c
+}
+{
+	dlmalloc
+	Memcheck:Addr8
+	src:dlmalloc.c
+}
+{
+	dlmalloc
+	Memcheck:Value4
+	src:dlmalloc.c
+}
+{
+	dlmalloc
+	Memcheck:Value8
+	src:dlmalloc.c
+}
+{
+	dlmalloc
+	Memcheck:Cond
+	src:dlmalloc.c
+}
Signature · 1 line
2.31.0
#4 lore raw

PATCH 2/4 malloc: Annotate allocator for valgrind

Quoted 17 lines · Sean Anderson

This annotates malloc and friends so that valgrind can track the heap. To do this, we need to follow a few rules:

* Call VALGRIND_MALLOCLIKE_BLOCK whenever we malloc something
* Call VALGRIND_FREELIKE_BLOCK whenever we free something (generally after we have done our bookkeeping)
* Call VALGRIND_RESIZEINPLACE_BLOCK whenever we change the size of an allocation. Generally this just needs to happen in realloc, but only if the address stays the same.

In addition to the above, dlmalloc itself tends to make a lot of accesses which we know are safe, but which would be unsafe outside of dlmalloc. For this reason, we provide a suppression file which ignores errors ocurring in dlmalloc.c

Quoted 8 lines

---

 common/dlmalloc.c      | 29 ++++++++++++++++++++++++++++-
 common/malloc_simple.c | 10 ++++++++++
 include/malloc.h       |  4 ++++
 scripts/u-boot.supp    | 30 ++++++++++++++++++++++++++++++
 4 files changed, 72 insertions(+), 1 deletion(-)
 create mode 100644 scripts/u-boot.supp
#5 lore raw

PATCH 3/4 doc: sandbox: Document how to run sandbox with valgrind

In reply to Sean Anderson

This documents how to get more detailed results from valgrind made possible by the last two commits.

---

 doc/arch/sandbox.rst | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
doc/arch/sandbox.rst +10 -4
index e052b6bdb0..9bc4fec5fe 100644
@@ -481,14 +481,20 @@ Using valgrind / memcheck
 
 It is possible to run U-Boot under valgrind to check memory allocations::
 
-   valgrind u-boot
+    valgrind ./u-boot
+
+For more detailed results, enable `CONFIG_VALGRIND`. There are many false
+positives due to `malloc` itself. Suppress these with::
+
+    valgrind --suppressions=scripts/u-boot.supp ./u-boot
 
 If you are running sandbox SPL or TPL, then valgrind will not by default
 notice when U-Boot jumps from TPL to SPL, or from SPL to U-Boot proper. To
-fix this, use::
-
-   valgrind --trace-children=yes u-boot
+fix this, use `--trace-children=yes`. To show who alloc'd some troublesome
+memory, use `--track-origins=yes`. To uncover possible errors, try running all
+unit tests with::
 
+    valgrind --track-origins=yes --suppressions=scripts/u-boot.supp ./u-boot -Tc 'ut all'
 
 Testing
 -------
Signature · 1 line
2.31.0
#6 lore raw

PATCH 3/4 doc: sandbox: Document how to run sandbox with valgrind

Hi Sean,

Quoted 9 lines · Sean Anderson

This documents how to get more detailed results from valgrind made possible by the last two commits.

---

 doc/arch/sandbox.rst | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

This looks good. I wonder if CONFIG_VALGRIND could become a build variable as well, so you don't need to update the CONFIG?

Regards,
Simon

#7 lore raw

PATCH 3/4 doc: sandbox: Document how to run sandbox with valgrind

Quoted 10 lines · Simon Glass

Hi Sean,

Quoted 7 lines · Sean Anderson

This documents how to get more detailed results from valgrind made possible by the last two commits.

---

Quoted 7 lines
Quoted 2 lines
  doc/arch/sandbox.rst | 14 ++++++++++----
  1 file changed, 10 insertions(+), 4 deletions(-)

This looks good. I wonder if CONFIG_VALGRIND could become a build variable as well, so you don't need to update the CONFIG?

Are there any existing examples of things which work like that?

--Sean

#8 lore raw

PATCH 3/4 doc: sandbox: Document how to run sandbox with valgrind

Hi Sean,

Quoted 15 lines · Sean Anderson
Quoted 13 lines · Simon Glass

Hi Sean,

Quoted 9 lines · Sean Anderson

This documents how to get more detailed results from valgrind made possible by the last two commits.

---

  doc/arch/sandbox.rst | 14 ++++++++++----
  1 file changed, 10 insertions(+), 4 deletions(-)
Quoted 6 lines
Quoted 4 lines

This looks good. I wonder if CONFIG_VALGRIND could become a build variable as well, so you don't need to update the CONFIG?

Are there any existing examples of things which work like that?

Yes, see 'DEVICE_TREE' in Makefile

Regards,
Simon

#9 lore raw

PATCH 4/4 malloc: Fix sbrk clearing memory after freeing it instead of before

In reply to Sean Anderson

This fixes memory being cleared after releasing it. Instead, clear memory before releasing it. In addition, suppress valgrind warnings about writing to free'd memory.

---

 common/dlmalloc.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
common/dlmalloc.c +6 -4
index 05c8fd87e7..ea51bdf6a6 100644
@@ -592,11 +592,13 @@ void *sbrk(ptrdiff_t increment)
 	ulong new = old + increment;
 
 	/*
-	 * if we are giving memory back make sure we clear it out since
-	 * we set MORECORE_CLEARS to 1
+	 * if we are allocating memory make sure we clear it out since we set
+	 * MORECORE_CLEARS to 1
 	 */
-	if (increment < 0)
-		memset((void *)new, 0, -increment);
+	if (increment > 0) {
+		VALGRIND_MAKE_MEM_UNDEFINED(old, increment);
+		memset((void *)old, 0, increment);
+	}
 
 	if ((new < mem_malloc_start) || (new > mem_malloc_end))
 		return (void *)MORECORE_FAILURE;
Signature · 1 line
2.31.0
#10 lore raw

PATCH 4/4 malloc: Fix sbrk clearing memory after freeing it instead of before

Hi Sean,

Quoted 20 lines · Sean Anderson

This fixes memory being cleared after releasing it. Instead, clear memory before releasing it. In addition, suppress valgrind warnings about writing to free'd memory.

---

 common/dlmalloc.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
common/dlmalloc.c +0 -1
index 05c8fd87e7..ea51bdf6a6 100644
@@ -592,11 +592,13 @@ void *sbrk(ptrdiff_t increment)
        ulong new = old + increment;
 
        /*
-        * if we are giving memory back make sure we clear it out since

common/dlmalloc.c void *sbrk(ptrdiff_t increment) · line 601

Quoted 10 lines
common/dlmalloc.c +6 -3
-        * we set MORECORE_CLEARS to 1
+        * if we are allocating memory make sure we clear it out since we set
+        * MORECORE_CLEARS to 1
         */
-       if (increment < 0)
-               memset((void *)new, 0, -increment);
+       if (increment > 0) {
+               VALGRIND_MAKE_MEM_UNDEFINED(old, increment);
+               memset((void *)old, 0, increment);
+       }

Can you explain this a bit more? What is the difference?

Do you need the cast?

Quoted 6 lines
common/dlmalloc.c +0 -1
        if ((new < mem_malloc_start) || (new > mem_malloc_end))
                return (void *)MORECORE_FAILURE;
--

2.31.0

Regards,
Simon

#11 lore raw

PATCH 4/4 malloc: Fix sbrk clearing memory after freeing it instead of before

Quoted 21 lines · Simon Glass

Hi Sean,

Quoted 18 lines · Sean Anderson

This fixes memory being cleared after releasing it. Instead, clear memory before releasing it. In addition, suppress valgrind warnings about writing to free'd memory.

---

  common/dlmalloc.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
common/dlmalloc.c +0 -0
index 05c8fd87e7..ea51bdf6a6 100644
@@ -592,11 +592,13 @@ void *sbrk(ptrdiff_t increment)
         ulong new = old + increment;
 
Quoted 14 lines

common/dlmalloc.c void *sbrk(ptrdiff_t increment) · line 601

Quoted 12 lines
common/dlmalloc.c +6 -4
         /*
-        * if we are giving memory back make sure we clear it out since
-        * we set MORECORE_CLEARS to 1
+        * if we are allocating memory make sure we clear it out since we set
+        * MORECORE_CLEARS to 1
          */
-       if (increment < 0)
-               memset((void *)new, 0, -increment);
+       if (increment > 0) {
+               VALGRIND_MAKE_MEM_UNDEFINED(old, increment);
+               memset((void *)old, 0, increment);
+       }

Can you explain this a bit more? What is the difference?

As it turns out, this patch is wrong. We need to clear memory when we release it if SYS_MALLOC_CLEAR_ON_INIT is set, since calloc assumes that memory has already been cleared if it gets it from sbrk.

Quoted 1 line

Do you need the cast?

Yes (but this is moot)

common/dlmalloc.c: In function ?sbrk?:
common/dlmalloc.c:600:10: warning: passing argument 1 of ?memset? makes pointer from integer without a cast [-Wint-conversion]

   600 |   memset(old, 0, increment);
       |          ^~~
       |          |
       |          ulong {aka long unsigned int}

In file included from include/common.h:21,
from common/dlmalloc.c:1:
include/linux/string.h:111:22: note: expected ?void *? but argument is of type ?ulong? {aka ?long unsigned int?}
111 | extern void * memset(void *,int,__kernel_size_t);
| ^~~~~~

--Sean

Quoted 11 lines
Quoted 7 lines
common/dlmalloc.c +0 -1
         if ((new < mem_malloc_start) || (new > mem_malloc_end))
                 return (void *)MORECORE_FAILURE;
--

2.31.0

Regards,
Simon