[gdb/guile] Don't allow libguile to change libgmp mem fns Since gdb commit 880ae75a2b7 "gdb delay guile initialization until gdbscm_finish_initialization" I'm running into: ... (gdb) print My_Var > 10.0^M free(): invalid pointer^M ERROR: GDB process no longer exists GDB process exited with wait status 5995 exp9 0 0 CHILDKILLED SIGABRT SIGABRT UNRESOLVED: gdb.ada/fixed_cmp.exp: gnat_encodings=all: print My_Var > 10.0 ... The problem is that both gdb and libguile try to set the libgmp memory functions, and since the gdb commit the ones from libguile are effective, which results in gdb freeing some memory in a way that is not compatible with the way that memory was actually allocated. The fact that libguile tries to set the libgmp memory functions is a bug which should be fixed starting version v3.0.6. Meanwhile, work around this in gdb by not allowing libguile to set the libgomp memory functions. Tested on x86_64-linux. gdb/ChangeLog: 2021-05-03 Tom de Vries PR guile/27806 * guile/guile.c (struct scoped_restore_libgmp_memory_functions): New struct. (gdbscm_initialize): Use scoped_restore_libgmp_memory_functions to save and restore libgmp memory functions. --- gdb/guile/guile.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/gdb/guile/guile.c b/gdb/guile/guile.c index bdf15cd498b..2d121b19505 100644 --- a/gdb/guile/guile.c +++ b/gdb/guile/guile.c @@ -637,6 +637,29 @@ call_initialize_gdb_module (void *data) return NULL; } +/* RAII class used to restore libgmp memory functions. */ + +struct scoped_restore_libgmp_memory_functions +{ + scoped_restore_libgmp_memory_functions () + { + mp_get_memory_functions (&alloc_func, &realloc_func, &free_func); + } + + ~scoped_restore_libgmp_memory_functions () + { + mp_set_memory_functions (alloc_func, realloc_func, free_func); + } + + DISABLE_COPY_AND_ASSIGN (scoped_restore_libgmp_memory_functions); + +private: + /* The saved libgmp memory functions that need to be restored. */ + void *(*alloc_func) (size_t); + void *(*realloc_func) (void *, size_t, size_t); + void (*free_func) (void *, size_t); +}; + /* A callback to initialize Guile after gdb has finished all its initialization. This is the extension_language_ops.initialize "method". */ @@ -662,6 +685,22 @@ gdbscm_initialize (const struct extension_language_defn *extlang) { gdb::block_signals blocker; + /* There are libguile versions (f.i. v3.0.5) that by default call + mp_get_memory_functions during initialization to install custom + libgmp memory functions. This is considered a bug and should be + fixed starting v3.0.6. + Before gdb commit 880ae75a2b7 "gdb delay guile initialization until + gdbscm_finish_initialization", that bug had no effect for gdb, + because gdb subsequently called mp_get_memory_functions to install + its own custom functions in _initialize_gmp_utils. However, since + aforementioned gdb commit the initialization order is reversed, + allowing libguile to install a custom malloc that is incompatible + with the custom free as used in gmp-utils.c, resulting in a + "double free or corruption (out)" error. + Work around the libguile bug by saving the libgmp memory functions + before guile initialization, and restoring them afterwards. */ + scoped_restore_libgmp_memory_functions restore_libgmp_memory_functions; + /* scm_with_guile is the most portable way to initialize Guile. Plus we need to initialize the Guile support while in Guile mode (e.g., called from within a call to scm_with_guile). */