From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Maxime Devos Newsgroups: gmane.lisp.guile.devel Subject: Re: GC + Java finalization Date: Sat, 20 Nov 2021 09:19:42 +0000 Message-ID: <1f1c8b5b4db817493341688c385994a11fd13ae6.camel@telenet.be> References: <9ce77d5e08d50456eddc575179b68ac17afc9bf6.camel@hahnjo.de> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="16256"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Evolution 3.38.3-1 To: Jonas Hahnfeld , guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Sat Nov 20 10:20:09 2021 Return-path: Envelope-to: guile-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1moMXZ-00042V-GF for guile-devel@m.gmane-mx.org; Sat, 20 Nov 2021 10:20:09 +0100 Original-Received: from localhost ([::1]:44274 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1moMXV-0008A6-UB for guile-devel@m.gmane-mx.org; Sat, 20 Nov 2021 04:20:06 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:40668) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1moMXG-00089m-MT for guile-devel@gnu.org; Sat, 20 Nov 2021 04:19:51 -0500 Original-Received: from [2a02:1800:110:4::f00:18] (port=50868 helo=michel.telenet-ops.be) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1moMXC-0006z1-Fv for guile-devel@gnu.org; Sat, 20 Nov 2021 04:19:49 -0500 Original-Received: from ptr-bvsjgyhxw7psv60dyze.18120a2.ip6.access.telenet.be ([IPv6:2a02:1811:8c09:9d00:3c5f:2eff:feb0:ba5a]) by michel.telenet-ops.be with bizsmtp id LZKi2600B4UW6Th06ZKjev; Sat, 20 Nov 2021 10:19:43 +0100 In-Reply-To: <9ce77d5e08d50456eddc575179b68ac17afc9bf6.camel@hahnjo.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telenet.be; s=r21; t=1637399983; bh=3K5iBtQZ8+aRRBmgOtlyTH+a827L7e6LhAzbKGUJX5k=; h=Subject:From:To:Date:In-Reply-To:References; b=mfO+9CSbtRf7yNhq9iMpI5XvXYokHdINknFezKfKQEB+lkdVwK+PByowUXElT4zr0 LHLfsIJ7uOgLWuDFMT5AfcUuTyt23gQVXCzR70m7hnQ9nCnmNPyTQdUeZc9lSLfzR0 YQqo2loxQu2FKSt7mgUNJ/6RC9V7nY8ZeTj//AOXepftU5YseJY3CkjVOlmwGbpYYx eqImnVmqodE6jxrWN4d0lwUPmXXwkiGVvKW6Y3nLC78oOD+3JGTx2SiO5sZRb1veFz JxTtvazrNwRah5lzTrktoqsYOE1FYFWmfdvrdVBGmY2BbHDG0BrHcGqP+xX5N6TK3i Zd1NafDa8dBDA== X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a02:1800:110:4::f00:18 (failed) Received-SPF: pass client-ip=2a02:1800:110:4::f00:18; envelope-from=maximedevos@telenet.be; helo=michel.telenet-ops.be X-Spam_score_int: -19 X-Spam_score: -2.0 X-Spam_bar: -- X-Spam_report: (-2.0 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Original-Sender: "guile-devel" Xref: news.gmane.io gmane.lisp.guile.devel:20976 Archived-At: Jonas Hahnfeld schreef op do 15-07-2021 om 20:44 [+0200]: > diff --git a/libguile/random.c b/libguile/random.c > index 63da7f5d6..ac400a9fd 100644 > --- a/libguile/random.c > +++ b/libguile/random.c > @@ -324,9 +324,7 @@ scm_c_random_bignum (scm_t_rstate *state, SCM m) >    /* we know the result will be this big */ >    mpz_realloc2 (SCM_I_BIG_MPZ (result), m_bits); >   > -  random_chunks = > -    (uint32_t *) scm_gc_calloc (num_chunks * sizeof (uint32_t), > -                                     "random bignum chunks"); > +  random_chunks = (uint32_t *) scm_calloc (num_chunks * sizeof > (uint32_t)); >   >    do >      { > @@ -363,9 +361,7 @@ scm_c_random_bignum (scm_t_rstate *state, SCM m) >        /* if result >= m, regenerate it (it is important to > regenerate >   all bits in order not to get a distorted distribution) */ >      } while (mpz_cmp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (m)) >= > 0); > -  scm_gc_free (random_chunks, > -               num_chunks * sizeof (uint32_t), > -               "random bignum chunks"); > +  free (random_chunks); >    return scm_i_normbig (result); >  } As I understand it, the idea of this change is to avoid boehmgc having to track the memory random_chunks (‘memory registration’). However, in-between the scm_calloc and free, mpz_import is called. Looking at libguile/mini-gmp.c, this causes gmp_allocate_func to be called. This variable is set by mp_set_memory_functions, which is called by scm_init_number with the allocation function custom_gmp_malloc/custom_gmp_realloc, which uses scm_gc_malloc_pointerless/scm_gc_realloc. Note that that these functions signal an error in case of out-of-memory (at least, that's what 6.19.2 Memory Blocks states). As such, in the following situation a memory leak can happen after the proposed patch: (catch 'out-of-memory (lambda () ;; have enough memory to allocate random_chunks, ;; but not enough for mpz_import (random some-large-number)) (lambda _ ;; random_chunks won't ever be freed! (pk 'oops-not-enough-memory _))) At least Artanis tries to behave somewhat nicely in case of OOM (https://git.savannah.gnu.org/cgit/artanis.git/tree/artanis/server/ragnarok.scm#n659), so I'd prefer to keep using scm_gc_calloc (+ scm_gc_free) instead of scm_calloc + free. Greetings, Maxime.