From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id OdOPIEy6j2D9OwAAWB0awg (envelope-from ) for ; Mon, 03 May 2021 04:54:36 -0400 Received: by simark.ca (Postfix, from userid 112) id 78C9E1F11C; Mon, 3 May 2021 04:54:36 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 8F6931E54D for ; Mon, 3 May 2021 04:54:35 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1C088385703F; Mon, 3 May 2021 08:54:35 +0000 (GMT) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by sourceware.org (Postfix) with ESMTPS id DF114385782A for ; Mon, 3 May 2021 08:54:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org DF114385782A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tdevries@suse.de X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 10CA4AEE6; Mon, 3 May 2021 08:54:31 +0000 (UTC) Date: Mon, 3 May 2021 10:54:29 +0200 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH][gdb/guile] Don't allow libguile to change libgmp mem fns Message-ID: <20210503085428.GA20738@delia> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ludovic =?iso-8859-1?Q?Court=E8s?= Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" Hi, 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. Any comments? Thanks, - Tom [gdb/guile] Don't allow libguile to change libgmp mem fns gdb/ChangeLog: 2021-05-03 Tom de Vries PR guile/27806 * guile/guile.c (gdbscm_initialize): Save and restore libgmp memory functions. --- gdb/guile/guile.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/gdb/guile/guile.c b/gdb/guile/guile.c index bdf15cd498b..6ee8b3f47ce 100644 --- a/gdb/guile/guile.c +++ b/gdb/guile/guile.c @@ -662,10 +662,32 @@ 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. */ + void *(*alloc_func) (size_t); + void *(*realloc_func) (void *, size_t, size_t); + void (*free_func) (void *, size_t); + mp_get_memory_functions (&alloc_func, &realloc_func, &free_func); + /* 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). */ scm_with_guile (call_initialize_gdb_module, NULL); + + /* Restore libgmp memory functions. */ + mp_set_memory_functions (alloc_func, realloc_func, free_func); } /* Set Guile's backtrace to match the "set guile print-stack" default.