From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Sergey Vinokurov Newsgroups: gmane.emacs.bugs Subject: bug#32479: Fix tracking of freed emacs_values with enabled --module-assertions Date: Mon, 20 Aug 2018 09:27:46 +0100 Message-ID: <05d6bad8-a739-8fcc-bd32-1291423c85a5@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------2316E864C30E8E7C6E949B81" X-Trace: blaine.gmane.org 1534753633 8807 195.159.176.226 (20 Aug 2018 08:27:13 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 20 Aug 2018 08:27:13 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 To: 32479@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Mon Aug 20 10:27:09 2018 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 1frfWn-000285-4q for geb-bug-gnu-emacs@m.gmane.org; Mon, 20 Aug 2018 10:27:09 +0200 Original-Received: from localhost ([::1]:45499 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1frfYr-0000QX-U5 for geb-bug-gnu-emacs@m.gmane.org; Mon, 20 Aug 2018 04:29:17 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:58328) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1frfYl-0000QH-9T for bug-gnu-emacs@gnu.org; Mon, 20 Aug 2018 04:29:12 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1frfYf-0005Jw-8x for bug-gnu-emacs@gnu.org; Mon, 20 Aug 2018 04:29:11 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:50000) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1frfYc-0005J0-R3 for bug-gnu-emacs@gnu.org; Mon, 20 Aug 2018 04:29:05 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1frfYc-00083j-NO for bug-gnu-emacs@gnu.org; Mon, 20 Aug 2018 04:29:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Sergey Vinokurov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 20 Aug 2018 08:29:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 32479 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.153475370530926 (code B ref -1); Mon, 20 Aug 2018 08:29:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 20 Aug 2018 08:28:25 +0000 Original-Received: from localhost ([127.0.0.1]:55018 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1frfY0-00082k-KI for submit@debbugs.gnu.org; Mon, 20 Aug 2018 04:28:24 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:37141) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1frfXz-00082X-Gs for submit@debbugs.gnu.org; Mon, 20 Aug 2018 04:28:23 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1frfXf-0004XV-UR for submit@debbugs.gnu.org; Mon, 20 Aug 2018 04:28:10 -0400 Original-Received: from lists.gnu.org ([2001:4830:134:3::11]:46772) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1frfXd-0004WM-2s for submit@debbugs.gnu.org; Mon, 20 Aug 2018 04:28:02 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:40745) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1frfXb-00005d-VV for bug-gnu-emacs@gnu.org; Mon, 20 Aug 2018 04:28:00 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1frfXV-0004Sf-S5 for bug-gnu-emacs@gnu.org; Mon, 20 Aug 2018 04:27:59 -0400 Original-Received: from mail-wm0-x22e.google.com ([2a00:1450:400c:c09::22e]:34274) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1frfXU-0004Qh-Jg for bug-gnu-emacs@gnu.org; Mon, 20 Aug 2018 04:27:53 -0400 Original-Received: by mail-wm0-x22e.google.com with SMTP id l2-v6so6494614wme.1 for ; Mon, 20 Aug 2018 01:27:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:openpgp:autocrypt:message-id:date:user-agent :mime-version:content-language; bh=k98kCarX9sJfdh08EftqazDnqdJ1+8HAd3rIpDPDhQQ=; b=bBG2UvVwyx/g/YruNYg27GFm9aLvs2gnRJJ3fcnjcCcIglBR3WXV3CJufMIL9+O6QJ MoH70TXtPM3cJi5G6eksZH3Na4hWdyzkM3r/7xQ1qS6BhQ8zo2uXZykx1yd901XJ6YWl /8tv5T3SCJI1+c/xGe+72yU1o+DDKXwmRKVDTFzCCUKAMnWgIWBPKeAFHE2Ol+3jEKjv bDtMAKLcV7U48LsltbfvljHYEGUlA/ufy66aEmSNe1rLa4mgJsyPnHJzQ7EpnQFwosAn itERlJII7vXtbRSLjeO+Xeh4Mbbv3M4f6xvUXGFFZh0FmPl32mptkHY80DsrfgYAxY/U Wxvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:openpgp:autocrypt:message-id :date:user-agent:mime-version:content-language; bh=k98kCarX9sJfdh08EftqazDnqdJ1+8HAd3rIpDPDhQQ=; b=KJKkYw+IIPiLgP7JFiQAMBHtPZEnmHvNpLYCGYNlCi8Q38xiXKDfE/CYn0b1jG6NY9 fMfZs/fOE88ZAKsrLPWkWkOyJDDpI/DKCDFYHgO0j9HIhuyJ4PNYi1W+jXqkH580BIcf JTqFWEhefIiq7uEtGTg+kgnm0i56WmoYtPKMROLqnqE3MRMnH2Z4qkyW4BUyHZq6rRQT xjwIPSEz0EcxZWcYE9IncHFZfiL6yRe+lA8XjgDyV3UJhUV5TTWvwBgP+2GGdn9GMVLz 72PqwN9Ux7rt3xximYUERRZuP+0CEIz+p5jnQgcTGOLcg3RdPAIRMR1Vy/WJE5q1Vv7l ItSg== X-Gm-Message-State: AOUpUlFiUJFnl0zNfq1H0MjtI+bDHDSpOaH4+UeC2NisRgfB6VSdQX4P 44AgnSJl5fHcZM9hYelq+tJ3CmOH X-Google-Smtp-Source: AA+uWPyRGm+e0PyuBgU4FbpkdVIu9TCGtyGSTf3i8P2XpTr6+kcK0eMM7juY8t+gx6nbKiuHFyHlmQ== X-Received: by 2002:a1c:ef0f:: with SMTP id n15-v6mr23710900wmh.116.1534753668442; Mon, 20 Aug 2018 01:27:48 -0700 (PDT) Original-Received: from [192.168.0.12] (cpc130868-walt25-2-0-cust106.know.cable.virginm.net. [81.106.113.107]) by smtp.gmail.com with ESMTPSA id z101-v6sm20619031wrb.55.2018.08.20.01.27.47 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 20 Aug 2018 01:27:47 -0700 (PDT) Openpgp: preference=signencrypt Autocrypt: addr=serg.foo@gmail.com; prefer-encrypt=mutual; keydata= mDMEWAFaIBYJKwYBBAHaRw8BAQdAA34Npg0byu0HOyNveV8PmF5iboU5XINejFpZ3hpn3m+0 JVNlcmdleSBWaW5va3Vyb3YgPHNlcmcuZm9vQGdtYWlsLmNvbT6IjgQTFgoANgIbAwIeAQIX gAULCQgHAwUVCgkICwUWAgMBAAUCWAJLwRQYaHR0cDovL3BncC5taXQuZWR1LwAKCRDWzSlT D5jWuCVlAP9t20yizGOHY7whNzv1/BbKsmW3aXNO+M81B+1a2DQSSAEA57iixKcD+USg4+N9 mb4cTqyjoFfA5deysH50DQe28we4OARYAVogEgorBgEEAZdVAQUBAQdATL7hRInDIvDfuygN SiVJz4wy8IX7csTzOtIwkvsZT2IDAQgHiGEEGBYKAAkFAlgBWiACGwwACgkQ1s0pUw+Y1rg/ RgD+MSa1uOSXDCUyuUJaW8ekiTxlQ+Ei8SYQjG4VdgQqubIA+wXGP/R18n3LAjMICL6adsme vatj/jek8FLEqAUTriYK Content-Language: en-GB X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x 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:149613 Archived-At: This is a multi-part message in MIME format. --------------2316E864C30E8E7C6E949B81 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Hi, I was developing a dynamic module when I noticed that when '--module-assertions' are enabled I got strange complaints regarding value to be freed. Namely, Emacs was producing an error message like the following one: Emacs module assertion: Global value was not found in list of 10 globals I tracked it down to a bug in module_free_global_ref. It's traversal of the linked list of all allocated global values was flawed and only considered the head of the list. Please find attached my attempt at fixing it and a test in Emacs test suite that reproduces the issue. Regards, Sergey --------------2316E864C30E8E7C6E949B81 Content-Type: text/x-patch; name="0001-Fix-detection-of-freed-emacs_values.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="0001-Fix-detection-of-freed-emacs_values.patch" =46rom a432465c57f14157f444540af3cea73261580dd6 Mon Sep 17 00:00:00 2001 From: Sergey Vinokurov Date: Sun, 19 Aug 2018 21:31:01 +0100 Subject: [PATCH] Fix detection of freed emacs_values Compare a value to be freed with all entries of the list * src/emacs-module.c: Fix traversal of the list that contains all emacs_values allocated by a dynamic module --- src/emacs-module.c | 8 ++++---- test/data/emacs-module/mod-test.c | 19 +++++++++++++++++++ test/src/emacs-module-tests.el | 3 +++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/emacs-module.c b/src/emacs-module.c index e7ba17426b..f2844c40d0 100644 --- a/src/emacs-module.c +++ b/src/emacs-module.c @@ -344,20 +344,20 @@ module_free_global_ref (emacs_env *env, emacs_value= ref) Lisp_Object globals =3D global_env_private.values; Lisp_Object prev =3D Qnil; ptrdiff_t count =3D 0; - for (Lisp_Object tail =3D global_env_private.values; CONSP (tail);= + for (Lisp_Object tail =3D globals; CONSP (tail); tail =3D XCDR (tail)) { - emacs_value global =3D xmint_pointer (XCAR (globals)); + emacs_value global =3D xmint_pointer (XCAR (tail)); if (global =3D=3D ref) { if (NILP (prev)) global_env_private.values =3D XCDR (globals); else - XSETCDR (prev, XCDR (globals)); + XSETCDR (prev, XCDR (tail)); return; } ++count; - prev =3D globals; + prev =3D tail; } module_abort ("Global value was not found in list of %"pD"d global= s", count); diff --git a/test/data/emacs-module/mod-test.c b/test/data/emacs-module/m= od-test.c index db05e90bc4..a9b459b4cc 100644 --- a/test/data/emacs-module/mod-test.c +++ b/test/data/emacs-module/mod-test.c @@ -156,6 +156,24 @@ Fmod_test_globref_make (emacs_env *env, ptrdiff_t na= rgs, emacs_value args[], return env->make_global_ref (env, lisp_str); } =20 +/* Create a few global references from arguments and free them. */ +static emacs_value +Fmod_test_globref_free (emacs_env *env, ptrdiff_t nargs, emacs_value arg= s[], + void *data) +{ + emacs_value refs[10]; + for (int i =3D 0; i < 10; i++) + { + refs[i] =3D env->make_global_ref (env, args[i % nargs]); + } + for (int i =3D 0; i < 10; i++) + { + env->free_global_ref (env, refs[i]); + } + return env->intern (env, "ok"); +} + + =20 /* Return a copy of the argument string where every 'a' is replaced with 'b'. */ @@ -339,6 +357,7 @@ emacs_module_init (struct emacs_runtime *ert) DEFUN ("mod-test-non-local-exit-funcall", Fmod_test_non_local_exit_fun= call, 1, 1, NULL, NULL); DEFUN ("mod-test-globref-make", Fmod_test_globref_make, 0, 0, NULL, NU= LL); + DEFUN ("mod-test-globref-free", Fmod_test_globref_free, 4, 4, NULL, NU= LL); DEFUN ("mod-test-string-a-to-b", Fmod_test_string_a_to_b, 1, 1, NULL, = NULL); DEFUN ("mod-test-userptr-make", Fmod_test_userptr_make, 1, 1, NULL, NU= LL); DEFUN ("mod-test-userptr-get", Fmod_test_userptr_get, 1, 1, NULL, NULL= ); diff --git a/test/src/emacs-module-tests.el b/test/src/emacs-module-tests= =2Eel index 90cd37a98a..c67190be5c 100644 --- a/test/src/emacs-module-tests.el +++ b/test/src/emacs-module-tests.el @@ -148,6 +148,9 @@ multiply-string (garbage-collect) ;; XXX: not enough to really test but it's somethi= ng.. (should (string=3D ref-str mod-str)))) =20 +(ert-deftest mod-test-globref-free-test () + (should (eq (mod-test-globref-free 1 'a "test" 'b) 'ok))) + (ert-deftest mod-test-string-a-to-b-test () (should (string=3D (mod-test-string-a-to-b "aaa") "bbb"))) =20 --=20 2.18.0 --------------2316E864C30E8E7C6E949B81--