Skip to contentSkip to conversation

Fixes and enhancements for memory debugging

PATCH In review Glenn Washburn 2 participants 9 messages Feb 15, 2022 – Mar 2, 2022

lore
Share & tools
lore
#0 lore raw

cover Fixes and enhancements for memory debugging

The first patch supercedes a patch sent earlier of the same subject. I've removed the #undef if --mm-debug is not passwed to configure. The idea is that even if --mm-debug is not passed to configure, the user should still be able to enable memory debugging by passing -DMM_DEBUG in CFLAGS.

The second patch I found useful in dumping the memory allocation state from a module.

And the third patch is a bug fix, without which enabling memory debugging leads to a certain infinite recursion crash (when also enabling grub_mm_debug). As an aside, this leads me to believe that no one has used this feature in a very long time (and is not currently using it).

Glenn

Glenn Washburn (3):
configure: Properly handle MM_DEBUG
mm: Export grub_mm_dump and grub_mm_dump_free
mm: Temporarily disable grub_mm_debug while calling grub_vprintf in
grub_printf

 config.h.in           |  4 ++++
 configure.ac          |  6 ++++--
 grub-core/kern/misc.c | 19 +++++++++++++++++++
 include/grub/mm.h     |  4 ++--
 4 files changed, 29 insertions(+), 4 deletions(-)
Signature · 1 line
2.27.0
#7 lore raw

PATCH 3/3 mm: Temporarily disable grub_mm_debug while calling grub_vprintf in grub_printf

To prevent infinite recursion when grub_mm_debug is on, disable it when calling grub_vprintf. One such call loop is:
grub_vprintf -> parse_printf_args -> parse_printf_arg_fmt

    -> grub_debug_calloc -> grub_printf -> grub_vprintf
---
 grub-core/kern/misc.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
grub-core/kern/misc.c +19 -0
index de40f566d6..cd6f460a21 100644
@@ -113,10 +113,29 @@ grub_printf (const char *fmt, ...)
   va_list ap;
   int ret;
 
+#if defined(MM_DEBUG) && !defined(GRUB_UTIL) && !defined (GRUB_MACHINE_EMU)
+  /*
+   * To prevent infinite recursion when grub_mm_debug is on, disable it
+   * when calling grub_vprintf. One such call loop is:
+   *   grub_vprintf -> parse_printf_args -> parse_printf_arg_fmt
+   *     -> grub_debug_calloc -> grub_printf -> grub_vprintf
+   */
+  int grub_mm_debug_save = 0;
+  if (grub_mm_debug)
+    {
+      grub_mm_debug_save = grub_mm_debug;
+      grub_mm_debug = 0;
+    }
+#endif
+
   va_start (ap, fmt);
   ret = grub_vprintf (fmt, ap);
   va_end (ap);
 
+#if defined(MM_DEBUG) && !defined(GRUB_UTIL) && !defined (GRUB_MACHINE_EMU)
+  grub_mm_debug = grub_mm_debug_save;
+#endif
+
   return ret;
 }
 
Signature · 1 line
2.27.0
#1 lore raw

PATCH 1/3 configure: Properly handle MM_DEBUG

In reply to Glenn Washburn

Define MM_DEBUG in config.h when --enable-mm-debug is passed to configure. It was being defined in config-util.h which only gets used when building GRUB utilities for the host side. The enabling of debugging for memory management in include/grub/mm.h explicitly does not happen when compiling for the GRUB utilities. So this debugging code effectively could never be enabled. Note, that MM_DEBUG is defined in an #if directive because the enabling of debugging checks if MM_DEBUG is defined, not what its value is. So even if MM_DEBUG were defined to nothing, the debugging code would still be enabled.

---
 config.h.in  | 4 ++++
 configure.ac | 6 ++++--
 2 files changed, 8 insertions(+), 2 deletions(-)
config.h.in +4 -0
index 9e8f9911b1..c1323a88e3 100644
@@ -9,6 +9,10 @@
 #define GCRYPT_NO_DEPRECATED 1
 #define HAVE_MEMMOVE 1
 
+#if @MM_DEBUG@
+#define MM_DEBUG @MM_DEBUG@
+#endif
+
 /* Define to 1 to enable disk cache statistics.  */
 #define DISK_CACHE_STATS @DISK_CACHE_STATS@
 #define BOOT_TIME_STATS @BOOT_TIME_STATS@
configure.ac +4 -2
index 5c01af0fab..65d7f8a3be 100644
@@ -1519,9 +1519,11 @@ AC_ARG_ENABLE([mm-debug],
 	      AS_HELP_STRING([--enable-mm-debug],
                              [include memory manager debugging]))
 if test x$enable_mm_debug = xyes; then
-    AC_DEFINE([MM_DEBUG], [1],
-            [Define to 1 if you enable memory manager debugging.])
+    MM_DEBUG=1
+else
+    MM_DEBUG=0
 fi
+AC_SUBST([MM_DEBUG])
 
 AC_ARG_ENABLE([cache-stats],
 	      AS_HELP_STRING([--enable-cache-stats],
Signature · 1 line
2.27.0
#3 lore raw

PATCH 2/3 mm: Export grub_mm_dump and grub_mm_dump_free

In reply to Glenn Washburn

These functions may be useful within modules as well. Export them so that modules can use them.

---
 include/grub/mm.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/grub/mm.h +2 -2
index 9c38dd3ca5..44fde7cb90 100644
@@ -46,8 +46,8 @@ void grub_mm_check_real (const char *file, int line);
 /* Set this variable to 1 when you want to trace all memory function calls.  */
 extern int EXPORT_VAR(grub_mm_debug);
 
-void grub_mm_dump_free (void);
-void grub_mm_dump (unsigned lineno);
+void EXPORT_FUNC(grub_mm_dump_free) (void);
+void EXPORT_FUNC(grub_mm_dump) (unsigned lineno);
 
 #define grub_calloc(nmemb, size)	\
   grub_debug_calloc (GRUB_FILE, __LINE__, nmemb, size)
Signature · 1 line
2.27.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] configure: Properly handle MM_DEBUG

In reply to Glenn Washburn
Quoted 11 lines · Glenn Washburn

Define MM_DEBUG in config.h when --enable-mm-debug is passed to configure. It was being defined in config-util.h which only gets used when building GRUB utilities for the host side. The enabling of debugging for memory management in include/grub/mm.h explicitly does not happen when compiling for the GRUB utilities. So this debugging code effectively could never be enabled. Note, that MM_DEBUG is defined in an #if directive because the enabling of debugging checks if MM_DEBUG is defined, not what its value is. So even if MM_DEBUG were defined to nothing, the debugging code would still be enabled.

Daniel

#4 lore raw

Re: [PATCH 2/3] mm: Export grub_mm_dump and grub_mm_dump_free

In reply to Glenn Washburn
Quoted 2 lines · Glenn Washburn

These functions may be useful within modules as well. Export them so that modules can use them.

Though there are no users for these functions today. So, I am not convinced we should export them.

Daniel

Reviewed-by on v1 3/3
#8 lore raw

Re: [PATCH 3/3] mm: Temporarily disable grub_mm_debug while calling grub_vprintf in grub_printf

In reply to Glenn Washburn
Quoted 6 lines · Glenn Washburn

To prevent infinite recursion when grub_mm_debug is on, disable it when calling grub_vprintf. One such call loop is:
grub_vprintf -> parse_printf_args -> parse_printf_arg_fmt

    -> grub_debug_calloc -> grub_printf -> grub_vprintf

Daniel

#5 lore raw

Re: [PATCH 2/3] mm: Export grub_mm_dump and grub_mm_dump_free

In reply to Daniel Kiper
Quoted 6 lines · On Tue, 22 Feb 2022 19:03:39 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 2 lines · Glenn Washburn

These functions may be useful within modules as well. Export them so that modules can use them.

Though there are no users for these functions today. So, I am not convinced we should export them.

How do you know there are no users for these functions today? As I see it, the point of these functions is only for doing memory debugging. They should not be enabled in general use. So no modules should ever use them unconditionally. Also, here they are only export _if_ configure was passed --mm-debug or MM_DEBUG is defined, which is never don't by default and likey only done when someone would be more likely to use these functions. I'm not quite following what the concern is.

Glenn

#6 lore raw

Re: [PATCH 2/3] mm: Export grub_mm_dump and grub_mm_dump_free

Quoted 11 lines · Glenn Washburn
Quoted 6 lines · On Tue, 22 Feb 2022 19:03:39 +0100 Daniel Kiper <[email protected]> wrote:
Quoted 2 lines · Glenn Washburn

These functions may be useful within modules as well. Export them so that modules can use them.

Though there are no users for these functions today. So, I am not convinced we should export them.

How do you know there are no users for these functions today? As I see

OK, to be precise currently there are no callers in the GRUB source code.

Quoted 6 lines

it, the point of these functions is only for doing memory debugging. They should not be enabled in general use. So no modules should ever use them unconditionally. Also, here they are only export _if_ configure was passed --mm-debug or MM_DEBUG is defined, which is never don't by default and likey only done when someone would be more likely to use these functions. I'm not quite following what the concern is.

Sorry, I missed they are exported only conditionally. So, taking into account this Reviewed-by: Daniel Kiper <[email protected]>...

Daniel