Skip to contentSkip to conversation

malloc: Enable profiling dlmalloc with valgrind

PATCH In review Sean Anderson 2 participants 8 messages Mar 23, 2022 – Apr 11, 2022

lore
Share & tools
lore
#0 lore raw

v3 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.

Some example output of valgrind run against u-boot/master with this branch applied may be found at [1]. Note that valgrind gives up around acpi. This feature still needs a lot of work on suppressions/hints to filter out the noise properly.

[1] https://gist.githubusercontent.com/Forty-Bot/199bf06f9cdd6871e54f8f484c16e111/raw/2a2f99108eef84b48e27a54332f3f71f4e2e5342/gistfile1.txt

Changes in v3:
- Fix some warnings by converting some macros to use VALGRIND_DO_CLIENT_REQUEST_STMT
- Fix an additional rEALLOc branch missing VALGRIND_RESIZEINPLACE_BLOCK

Changes in v2:
- Fix one branch of rEALLOc missing a VALGRIND_*_BLOCK call
- Add some additional suppressions for cALLOc and rEALLOc
- Simplify calloc clearing logic
- Drop incorrect sbrk patch

Sean Anderson (3):
Add valgrind headers to U-Boot
malloc: Annotate allocator for valgrind
doc: sandbox: Document how to run sandbox with valgrind

 Kconfig                     |   14 +
 Licenses/README             |    1 +
 Licenses/bzip2-1.0.6.txt    |   30 +
 common/dlmalloc.c           |   31 +-
 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         |   53 +
 10 files changed, 7509 insertions(+), 5 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.35.1
#5 lore raw

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

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

---

(no changes since v1)

 doc/arch/sandbox.rst | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
doc/arch/sandbox.rst +10 -4
index f8804e1f41..e1119492b4 100644
@@ -477,14 +477,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.35.1
#3 lore raw

PATCH v3 2/3 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. We don't record the original request size of a block, and neither does valgrind. For this reason, we pretend that the old size of the allocation was for 0 bytes. This marks the whole allocaton as undefined, so in order to mark all bits correctly, we must make the whole new allocation defined with VALGRIND_MAKE_MEM_DEFINED. This may cause us to miss some invalid reads, but there is no way to detect these without recording the original size of the allocation.

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

---

Changes in v3:
- Fix an additional rEALLOc branch missing VALGRIND_RESIZEINPLACE_BLOCK

Changes in v2:
- Fix one branch of rEALLOc missing a VALGRIND_*_BLOCK call
- Add some additional suppressions for cALLOc and rEALLOc
- Simplify calloc clearing logic

 common/dlmalloc.c      | 31 +++++++++++++++++++++++-
 common/malloc_simple.c | 10 ++++++++
 include/malloc.h       |  4 ++++
 scripts/u-boot.supp    | 53 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 97 insertions(+), 1 deletion(-)
 create mode 100644 scripts/u-boot.supp
common/dlmalloc.c +30 -1
index 11729e8c85..f48cd2a333 100644
@@ -18,6 +18,7 @@
 
 #include <malloc.h>
 #include <asm/io.h>
+#include <valgrind/memcheck.h>
 
 #ifdef DEBUG
 #if __STD_C
@@ -1339,6 +1340,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);
     }
 
@@ -1366,6 +1368,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);
       }
     }
@@ -1389,6 +1392,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);
     }
 
@@ -1398,6 +1402,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);
     }
 
@@ -1453,6 +1458,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);
 	  }
 
@@ -1461,6 +1467,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);
 	  }
 
@@ -1509,6 +1516,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
 
@@ -1523,6 +1531,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);
 
 }
@@ -1571,8 +1580,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 */
@@ -1594,6 +1605,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 */
   {
@@ -1782,6 +1794,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), 0, bytes, SIZE_SZ);
+	  VALGRIND_MAKE_MEM_DEFINED(chunk2mem(oldp), bytes);
 	  return chunk2mem(oldp);
 	}
       }
@@ -1791,6 +1805,8 @@ Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes;
       {
 	unlink(next, bck, fwd);
 	newsize  += nextsize;
+	VALGRIND_RESIZEINPLACE_BLOCK(chunk2mem(oldp), 0, bytes, SIZE_SZ);
+	VALGRIND_MAKE_MEM_DEFINED(chunk2mem(oldp), bytes);
 	goto split;
       }
     }
@@ -1820,10 +1836,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;
 	  }
 	}
@@ -1836,6 +1854,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;
 	}
@@ -1848,6 +1867,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;
       }
@@ -1874,6 +1894,9 @@ Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes;
     MALLOC_COPY(newmem, oldmem, oldsize - SIZE_SZ);
     fREe(oldmem);
     return newmem;
+  } else {
+    VALGRIND_RESIZEINPLACE_BLOCK(oldmem, 0, bytes, SIZE_SZ);
+    VALGRIND_MAKE_MEM_DEFINED(oldmem, bytes);
   }
 
 
@@ -1886,6 +1909,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
@@ -2043,6 +2068,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);
   }
@@ -2056,6 +2082,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));
   }
 
@@ -2159,6 +2187,7 @@ Void_t* cALLOc(n, elem_size) size_t n; size_t elem_size;
 #endif
 
     MALLOC_ZERO(mem, csz - SIZE_SZ);
+    VALGRIND_MAKE_MEM_DEFINED(mem, sz);
     return mem;
   }
 }
common/malloc_simple.c +10 -0
index 67ee623850..0a004d40e1 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 1fbaf3755c..e8c8b254c0 100644
@@ -887,7 +887,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 +53 -0

53 lines not on this page · raw message

Signature · 1 line
2.35.1
#1 lore raw

PATCH v3 1/3 Add valgrind headers to U-Boot

In reply to 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 is called NVALGRIND upstream, but was renamed (and inverted) to CONFIG_VALGRIND. Aside from this and the conversion of a few instances of VALGRIND_DO_CLIENT_REQUEST_EXPR to STMT, these headers are unmodified.

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.

---

Changes in v3:
- Fix some warnings by converting some macros to use VALGRIND_DO_CLIENT_REQUEST_STMT

 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 9dd9ec7f6d..70553073a5 100644
@@ -279,6 +279,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.35.1

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

#7 lore raw
In reply to Sean Anderson

Hi all,

Quoted 9 lines · Sean Anderson

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

Some example output of valgrind run against u-boot/master with this branch applied may be found at [1]. Note that valgrind gives up around acpi. This feature still needs a lot of work on suppressions/hints to filter out the noise properly.

[1] https://gist.githubusercontent.com/Forty-Bot/199bf06f9cdd6871e54f8f484c16e111/raw/2a2f99108eef84b48e27a54332f3f71f4e2e5342/gistfile1.txt

This is a bit of a follow up to the above. Over the past week I've sent

a few patches [1-4] with various bugs found with valgrind. Hopefully

this gives a good idea of the kind of problems valgrind is equipped to

find. In particular, it is good at determining where we go past the end

of buffers or otherwise read uninitialized memory.

I've also discovered why I originally stopped working on this series:

supressions don't "un-taint" uninitialized memory accesses. Currently, I

have dlmalloc's reads its bookkeeping information marked as a "red

zone." This means that all reads to it are marked as illegal by

valgrind. This is fine for regular code, but dlmalloc really does need

to access this area, so we suppress its violations. However, if dlmalloc

then passes a result calculated from a "tainted" access, that result is

still tainted. So the first accessor will raise a warning. This means

that every construct like

	foo = malloc(sizeof(*foo));

	if (!foo)

		return -ENOMEM;

will raise a warning when we check the result of malloc. Whoops.

There are three ways (as I see it) to address this:

- Don't mark dlmalloc bookkeeping information as a red zone. This is the

simplest solution, but reduces the power of valgrind immensely, since

we can no longer determine that (e.g.) access past the end of an array

is undefined.

- Implement red zones properly. This would involve growing every

allocation by a fixed amount (16 bytes or so) and then using that

extra space for a real red zone that neither regular code nor dlmalloc

needs to access. Unfortunately, this would probably some fairly

intensive surgery to dlmalloc to add/remove the offset appropriately.

- Mark bookkeeping information as valid before we use it in dlmalloc,

and then mark it invalid before returning. This would be the most

correct, but it would be very tricky to implement since there are so

many code paths to mark. I think it would be the most effort out of

the three options here.

Until one of the above options are implemented, it will remain difficult

to sift through the massive amount of spurious warnings.

That said, if anyone wants to play around with this a bit, I have some

additional invocation suggestions which I have been using, but which are

not documented

- "--error-limit=no" will enable printing more than 1000 errors in a

single session.

- "--vgdb=yes --vgdb-error=0" will let you use gdb to attach like

gdb -ex "target remote | vgdb" u-boot

This is very helpful for inspecting the program state when there is

an error.

- "t cooked" to U-Boot will keep the console in a sane state if you

terminate it early (instead of having to run tset).

The above should probably be documented in patch 3. I will add it if I

do a v4 (or I will send a follow-up).

--Sean

[1] https://patchwork.ozlabs.org/project/uboot/list/?series=291719

[2] https://patchwork.ozlabs.org/project/uboot/list/?series=291739

[3] https://patchwork.ozlabs.org/project/uboot/list/?series=292710

[4] https://patchwork.ozlabs.org/project/uboot/list/?series=292712

#2 lore raw

Re: [PATCH v3 1/3] Add valgrind headers to U-Boot

In reply to Sean Anderson
Quoted 11 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 is called NVALGRIND upstream, but was renamed (and inverted) to CONFIG_VALGRIND. Aside from this and the conversion of a few instances of VALGRIND_DO_CLIENT_REQUEST_EXPR to STMT, these headers are unmodified.

Quoted 6 lines

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.

Applied to u-boot/master, thanks!

Signature · 1 line
Tom
#4 lore raw

Re: [PATCH v3 2/3] malloc: Annotate allocator for valgrind

In reply to Sean Anderson
Quoted 15 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. We don't record the original request size of a block, and neither does valgrind. For this reason, we pretend that the old size of the allocation was for 0 bytes. This marks the whole allocaton as undefined, so in order to mark all bits correctly, we must make the whole new allocation defined with VALGRIND_MAKE_MEM_DEFINED. This may cause us to miss some invalid reads, but there is no way to detect these without recording the original size of the allocation.

Quoted 7 lines

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

Applied to u-boot/master, thanks!

Signature · 1 line
Tom
#6 lore raw

Re: [PATCH v3 3/3] doc: sandbox: Document how to run sandbox with valgrind

In reply to Sean Anderson
Quoted 5 lines · Sean Anderson

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

Applied to u-boot/master, thanks! And per your follow-up to the cover letter, yes, please do a follow-up with more examples in the documentation and summarize the "how to make this better" options.

Signature · 1 line
Tom