From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Valentin Gatien-Baron Newsgroups: gmane.emacs.bugs Subject: bug#27587: 25.1; in the dynamic modules api, env->free_global_ref doesn't free anything Date: Mon, 10 Jul 2017 11:10:04 -0400 Message-ID: References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="001a114574f44c467e0553f7fa6d" X-Trace: blaine.gmane.org 1499728101 18768 195.159.176.226 (10 Jul 2017 23:08:21 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 10 Jul 2017 23:08:21 +0000 (UTC) Cc: 27587-done@debbugs.gnu.org To: Philipp Stephani Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Jul 11 01:08:17 2017 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dUhmn-0004VU-JD for geb-bug-gnu-emacs@m.gmane.org; Tue, 11 Jul 2017 01:08:13 +0200 Original-Received: from localhost ([::1]:43380 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dUhmt-0001gy-2y for geb-bug-gnu-emacs@m.gmane.org; Mon, 10 Jul 2017 19:08:19 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:34614) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dUhmk-0001fW-As for bug-gnu-emacs@gnu.org; Mon, 10 Jul 2017 19:08:11 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dUhmd-0003Xi-Uz for bug-gnu-emacs@gnu.org; Mon, 10 Jul 2017 19:08:08 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:58680) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dUhmd-0003Xd-Pa for bug-gnu-emacs@gnu.org; Mon, 10 Jul 2017 19:08:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1dUhmd-0000hF-Ki for bug-gnu-emacs@gnu.org; Mon, 10 Jul 2017 19:08:03 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Valentin Gatien-Baron Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 10 Jul 2017 23:08:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 27587 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 27587-done@debbugs.gnu.org id=D27587.14997280772643 (code D ref 27587); Mon, 10 Jul 2017 23:08:03 +0000 Original-Received: (at 27587-done) by debbugs.gnu.org; 10 Jul 2017 23:07:57 +0000 Original-Received: from localhost ([127.0.0.1]:33121 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dUhmX-0000gY-1I for submit@debbugs.gnu.org; Mon, 10 Jul 2017 19:07:57 -0400 Original-Received: from mxout1.mail.janestreet.com ([38.105.200.78]:54683) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dUaKD-0002JJ-I6 for 27587-done@debbugs.gnu.org; Mon, 10 Jul 2017 11:10:14 -0400 Original-Received: from [172.27.56.106] (helo=tot-qpr-mailcore2) by mxout1.mail.janestreet.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89) (envelope-from ) id 1dUaK8-0005dd-1k for 27587-done@debbugs.gnu.org; Mon, 10 Jul 2017 11:10:08 -0400 X-JS-Flow: external X-JS-Scanner-attachment: (ok) No attachments Original-Received: by tot-qpr-mailcore2 with ocaml/mailcore/mailcore 1.0+97 (cbf81801a3e2) (envelope-from ) id BZY5jQ-AjPWpP-BQ; 2017-07-10 11:10:08.045003-04:00 Original-Received: from mail-qt0-f200.google.com ([209.85.216.200]) by mxgoog1.mail.janestreet.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.89) (envelope-from ) id 1dUaK7-0000Nb-W0 for 27587-done@debbugs.gnu.org; Mon, 10 Jul 2017 11:10:08 -0400 Original-Received: by mail-qt0-f200.google.com with SMTP id o3so52370441qto.15 for <27587-done@debbugs.gnu.org>; Mon, 10 Jul 2017 08:10:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=janestreet.com; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=90urAnqJ1hhAWH4aTjFGyvMbZ7kvp980uMY3gli4CcI=; b=yBd4Bup1jzfUE5YsbV4v8HPwQ6MXXZlWJHgCUd6N1QtuO6BJ8jN4OJbppCjX7EN8fi 65G3BLrrNYwc9ORtPYn/KxUv7+M0tJBtvtvURMG8pCZWec/Gf9OBm1deVEJeIMA56yqT 8qkmofXfMSAd4JOD1cos/zghT61UcbiPvi9FE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=90urAnqJ1hhAWH4aTjFGyvMbZ7kvp980uMY3gli4CcI=; b=AwEesQuCZeee1v+R9S+diBo900FAcGoIia8t35F6Zi1HpeZCZR57nqPLaInFrut/3D IBBQKe7mOGBmLApnI1Q52DL0UrPfBvWP8ssho6PD5eKlxUbXlImzoGsCT7uuGZtC2zCO 4+p40bnoP+VfyVPtem0jTur+WIZGkqQEM4qXr2Zbs2SSgk2zZfajQUPxE3n4XDmE/9dp jN6tBOYkvRnYfOeelk+8i3HDQsNNoZOy87PO9NQdOPiislVQxTR5MD/XPz9OrpyanGAO CjodC/x5FWL/2xmO1NoQ+0bw6TJbwbaAxvvDc2tUtdnBtiYfKIggw4n6//8HC68Q+Dp8 UDFQ== X-Gm-Message-State: AIVw113xqo/HU5bPwi8jlFyY7I3Y3d31DMZQ0H+g6DooW7Wd/KKgi4RB bT2wPNbNfQAGcFHoEOQ1qIC7Euz+ZZ2fVLMVR+PZXjGsq3avEPT/GinxJnJmuGYvsHonK+fUBcN hx0s7mZuKzUiHm58kERkjAJXeBp0= X-Received: by 10.200.53.243 with SMTP id l48mr4947069qtb.7.1499699406207; Mon, 10 Jul 2017 08:10:06 -0700 (PDT) X-Received: by 10.200.53.243 with SMTP id l48mr4946841qtb.7.1499699404537; Mon, 10 Jul 2017 08:10:04 -0700 (PDT) Original-Received: by 10.12.156.10 with HTTP; Mon, 10 Jul 2017 08:10:04 -0700 (PDT) In-Reply-To: X-JS-Exim-Data-Received: 2017-07-10 11:10:08-0400 X-JS-Processed-by: mailcore X-Mailman-Approved-At: Mon, 10 Jul 2017 19:07:55 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:134414 Archived-At: --001a114574f44c467e0553f7fa6d Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable =E2=80=8BThanks! By the way, the assert that refcount =3D=3D 0 you added as= a further commit wouldn't have triggered, as the refcount in the hash table would stay at 1 after the call to free_global_ref. On Sun, Jul 9, 2017 at 6:17 PM, Philipp Stephani wrote: > > > Valentin Gatien-Baron schrieb am Mi., 5. > Juli 2017 um 17:07 Uhr: > >> The following dynamic module takes unbounded memory: >> >> /* gcc -I . -g -ggdb -fPIC foo.c -shared -o foo.so && echo running && >> emacs -Q -L . -batch -l foo */ >> #include >> >> int plugin_is_GPL_compatible; >> >> int emacs_module_init(struct emacs_runtime *ert) { >> emacs_env *env =3D ert->get_environment(ert); >> while (1) { >> int i; >> for (i =3D 0; i < 10000; i++) { >> emacs_value v =3D env->make_string(env, "asdads", 3); >> env->free_global_ref(env, env->make_global_ref(env, v)); >> } >> env->funcall(env, env->intern(env, "garbage-collect"), 0, NULL); >> } >> } >> >> >> This is because env->make_global_ref/env->free_global_ref leak memory. >> env->free_global_ref fails to remove values from the hash table of >> refcounts. The following patch makes the program above run in constant >> space. >> >> >> --- src/emacs-module.c 2017-06-30 16:00:36.776301646 -0400 >> +++ src/emacs-module.c 2017-06-30 16:05:01.660120043 -0400 >> @@ -308,7 +308,7 @@ >> set_hash_value_slot (h, i, value); >> } >> else >> - hash_remove_from_table (h, value); >> + hash_remove_from_table (h, obj); >> } >> } >> >> > Good catch, installed as 22af69906cca871fdb893e06d6f10dbbab4518e6. > --001a114574f44c467e0553f7fa6d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
=E2=80=8BThanks! By the way, the assert that refcount = =3D=3D 0 you added as a further commit wouldn't have triggered, as the = refcount in the hash table would stay at 1 after the call to free_global_re= f.


On Sun, Jul 9, 2017 at 6:17 PM, Philipp Stephani <p.stephani= 2@gmail.com> wrote:


Valentin Gati= en-Baron <vgatien-baron@janestreet.com> schrieb am Mi., 5. Juli 2017 um 17= :07=C2=A0Uhr:
The following dynamic module takes unbounded memory:=

/* gcc -I . -g -ggdb -fPIC foo.c -shar= ed -o foo.so && echo running && emacs -Q -L . -batch -l foo= */
#include <emacs-module.h>

= int plugin_is_GPL_compatible;

int emacs_module_ini= t(struct emacs_runtime *ert) {
=C2=A0 emacs_env *env =3D ert->= get_environment(ert);
=C2=A0 while (1) {
=C2=A0 =C2= =A0 int i;
=C2=A0 =C2=A0 for (i =3D 0; i < 10000; i++) {
=C2=A0 =C2=A0 =C2=A0 emacs_value v =3D env->make_string(env, "= asdads", 3);
=C2=A0 =C2=A0 =C2=A0 env->free_global_ref(en= v, env->make_global_ref(env, v));
=C2=A0 =C2=A0 }
=
= =C2=A0 =C2=A0 env->funcall(env, env->intern(env, "garbage-collec= t"), 0, NULL);
=C2=A0 }
}

<= /div>

This is because env->make_global_ref/env->free= _global_ref leak memory.
env->free_global_ref fails to remove = values from the hash table of
refcounts. The following patch make= s the program above run in constant
space.

<= /div>

--- src/emacs-module.c 2017-06-30 16:00:36.776301646 -0400
+++ src/emacs-modul= e.c 2017-06-30 16:05:01.6601200= 43 -0400
@@ -308,7 +308,7 @@
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0set_hash_value_slot (h, i, value);
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0}
=C2=A0 =C2=A0 =C2=A0 =C2=A0else<= /div>
- hash_remove_from_table (h,= value);
+ hash_remov= e_from_table (h, obj);
=C2=A0 =C2=A0 =C2=A0}
=C2=A0}


Good= catch, installed as 22af69906cca871fdb893e06d6f10dbbab4518e6.=C2=A0

--001a114574f44c467e0553f7fa6d--