blob: 9e9755e44f36a59e4e46e55564ffd04bad62b4ba [file] [log] [blame]
Andrew Geisslereff27472021-10-29 15:35:00 -05001From 7799441b9aa55324160deefbc65f9d918b8c94c1 Mon Sep 17 00:00:00 2001
2From: Xi Ruoyao <xry111@mengyan1223.wang>
3Date: Tue, 10 Aug 2021 18:52:56 +0800
4Subject: [PATCH] jsauthority: ensure to call JS_Init() and JS_ShutDown()
5 exactly once
6
7Before this commit, we were calling JS_Init() in
8polkit_backend_js_authority_class_init and never called JS_ShutDown.
9This is actually a misusage of SpiderMonkey API. Quote from a comment
10in js/Initialization.h (both mozjs-78 and mozjs-91):
11
12 It is currently not possible to initialize SpiderMonkey multiple
13 times (that is, calling JS_Init/JSAPI methods/JS_ShutDown in that
14 order, then doing so again).
15
16This misusage does not cause severe issues with mozjs-78. However, when
17we eventually port jsauthority to use mozjs-91, bad thing will happen:
18see the test failure mentioned in #150.
19
20This commit is tested with both mozjs-78 and mozjs-91, all tests pass
21with it.
22
23Upstream-Status: Submitted [https://gitlab.freedesktop.org/polkit/polkit/-/merge_requests/91]
24Signed-off-by: Alexander Kanavin <alex@linutronix.de>
25---
26 src/polkitbackend/polkitbackendjsauthority.cpp | 10 +++++++---
27 1 file changed, 7 insertions(+), 3 deletions(-)
28
29diff --git a/src/polkitbackend/polkitbackendjsauthority.cpp b/src/polkitbackend/polkitbackendjsauthority.cpp
30index 41d8d5c..38dc001 100644
31--- a/src/polkitbackend/polkitbackendjsauthority.cpp
32+++ b/src/polkitbackend/polkitbackendjsauthority.cpp
33@@ -75,6 +75,13 @@
34
35 /* ---------------------------------------------------------------------------------------------------- */
36
37+static class JsInitHelperType
38+{
39+public:
40+ JsInitHelperType() { JS_Init(); }
41+ ~JsInitHelperType() { JS_ShutDown(); }
42+} JsInitHelper;
43+
44 struct _PolkitBackendJsAuthorityPrivate
45 {
46 gchar **rules_dirs;
47@@ -589,7 +596,6 @@ polkit_backend_js_authority_finalize (GObject *object)
48 delete authority->priv->js_polkit;
49
50 JS_DestroyContext (authority->priv->cx);
51- /* JS_ShutDown (); */
52
53 G_OBJECT_CLASS (polkit_backend_js_authority_parent_class)->finalize (object);
54 }
55@@ -665,8 +671,6 @@ polkit_backend_js_authority_class_init (PolkitBackendJsAuthorityClass *klass)
56
57
58 g_type_class_add_private (klass, sizeof (PolkitBackendJsAuthorityPrivate));
59-
60- JS_Init ();
61 }
62
63 /* ---------------------------------------------------------------------------------------------------- */