1b5975d6bSopenharmony_ciFrom 842a105464f6390a433da8791d7b19b65df16f47 Mon Sep 17 00:00:00 2001 2b5975d6bSopenharmony_ciFrom: Aleksei Rybalkin <aleksei@rybalkin.org> 3b5975d6bSopenharmony_ciDate: Mon, 14 Aug 2023 20:32:48 +0200 4b5975d6bSopenharmony_ciSubject: [PATCH 1/2] gregex: remove redundant call to 5b5975d6bSopenharmony_ci enable_jit_with_match_options 6b5975d6bSopenharmony_ci 7b5975d6bSopenharmony_ciThere is no point to enable jit in g_regex_new, since JIT will be only 8b5975d6bSopenharmony_ciused when we do a first match, and at that point 9b5975d6bSopenharmony_cienable_jit_with_match_options will be called again already and will 10b5975d6bSopenharmony_ciupdate the options set in g_regex_new. Instead just run it at first 11b5975d6bSopenharmony_cimatch for the first time, to the same end result. 12b5975d6bSopenharmony_ci 13b5975d6bSopenharmony_ciConflict:NA 14b5975d6bSopenharmony_ciReference:https://gitlab.gnome.org/GNOME/glib/-/commit/842a105464f6390a433da8791d7b19b65df16f47 15b5975d6bSopenharmony_ci 16b5975d6bSopenharmony_ci--- 17b5975d6bSopenharmony_ci glib/gregex.c | 1 - 18b5975d6bSopenharmony_ci 1 file changed, 1 deletion(-) 19b5975d6bSopenharmony_ci 20b5975d6bSopenharmony_cidiff --git a/glib/gregex.c b/glib/gregex.c 21b5975d6bSopenharmony_ciindex 39b9edeecd..f6b2b716fc 100644 22b5975d6bSopenharmony_ci--- a/glib/gregex.c 23b5975d6bSopenharmony_ci+++ b/glib/gregex.c 24b5975d6bSopenharmony_ci@@ -1764,7 +1764,6 @@ G_GNUC_END_IGNORE_DEPRECATIONS 25b5975d6bSopenharmony_ci regex->orig_compile_opts = compile_options; 26b5975d6bSopenharmony_ci regex->match_opts = pcre_match_options; 27b5975d6bSopenharmony_ci regex->orig_match_opts = match_options; 28b5975d6bSopenharmony_ci- regex->jit_status = enable_jit_with_match_options (regex, regex->match_opts); 29b5975d6bSopenharmony_ci 30b5975d6bSopenharmony_ci return regex; 31b5975d6bSopenharmony_ci } 32b5975d6bSopenharmony_ci-- 33b5975d6bSopenharmony_ciGitLab 34b5975d6bSopenharmony_ci 35b5975d6bSopenharmony_ci 36b5975d6bSopenharmony_ciFrom c3ff5b8eb39f1ab31383604910ae12f325e5afee Mon Sep 17 00:00:00 2001 37b5975d6bSopenharmony_ciFrom: Aleksei Rybalkin <aleksei@rybalkin.org> 38b5975d6bSopenharmony_ciDate: Mon, 14 Aug 2023 20:41:40 +0200 39b5975d6bSopenharmony_ciSubject: [PATCH 2/2] gregex: set default max stack size for PCRE2 JIT compiler 40b5975d6bSopenharmony_ci to 512KiB 41b5975d6bSopenharmony_ci 42b5975d6bSopenharmony_ciPrevious default used was 32KiB (the library default) which caused some 43b5975d6bSopenharmony_cicomplex patterns to fail, see #2824. The memory will not be allocated 44b5975d6bSopenharmony_ciunless used. 45b5975d6bSopenharmony_ci 46b5975d6bSopenharmony_ciConflict:Move test_string to fix declaration-after-statement 47b5975d6bSopenharmony_ciReference:https://gitlab.gnome.org/GNOME/glib/-/commit/c3ff5b8eb39f1ab31383604910ae12f325e5afee 48b5975d6bSopenharmony_ci 49b5975d6bSopenharmony_ci--- 50b5975d6bSopenharmony_ci glib/gregex.c | 22 ++++++++++++++-------- 51b5975d6bSopenharmony_ci glib/tests/regex.c | 9 +++++++++ 52b5975d6bSopenharmony_ci 2 files changed, 23 insertions(+), 8 deletions(-) 53b5975d6bSopenharmony_ci 54b5975d6bSopenharmony_cidiff --git a/glib/gregex.c b/glib/gregex.c 55b5975d6bSopenharmony_ciindex f6b2b716fc..5ce034db41 100644 56b5975d6bSopenharmony_ci--- a/glib/gregex.c 57b5975d6bSopenharmony_ci+++ b/glib/gregex.c 58b5975d6bSopenharmony_ci@@ -232,6 +232,7 @@ struct _GMatchInfo 59b5975d6bSopenharmony_ci gssize string_len; /* length of string, in bytes */ 60b5975d6bSopenharmony_ci pcre2_match_context *match_context; 61b5975d6bSopenharmony_ci pcre2_match_data *match_data; 62b5975d6bSopenharmony_ci+ pcre2_jit_stack *jit_stack; 63b5975d6bSopenharmony_ci }; 64b5975d6bSopenharmony_ci 65b5975d6bSopenharmony_ci typedef enum 66b5975d6bSopenharmony_ci@@ -896,22 +897,22 @@ recalc_match_offsets (GMatchInfo *match_info, 67b5975d6bSopenharmony_ci } 68b5975d6bSopenharmony_ci 69b5975d6bSopenharmony_ci static JITStatus 70b5975d6bSopenharmony_ci-enable_jit_with_match_options (GRegex *regex, 71b5975d6bSopenharmony_ci+enable_jit_with_match_options (GMatchInfo *match_info, 72b5975d6bSopenharmony_ci uint32_t match_options) 73b5975d6bSopenharmony_ci { 74b5975d6bSopenharmony_ci gint retval; 75b5975d6bSopenharmony_ci uint32_t old_jit_options, new_jit_options; 76b5975d6bSopenharmony_ci 77b5975d6bSopenharmony_ci- if (!(regex->orig_compile_opts & G_REGEX_OPTIMIZE)) 78b5975d6bSopenharmony_ci+ if (!(match_info->regex->orig_compile_opts & G_REGEX_OPTIMIZE)) 79b5975d6bSopenharmony_ci return JIT_STATUS_DISABLED; 80b5975d6bSopenharmony_ci 81b5975d6bSopenharmony_ci- if (regex->jit_status == JIT_STATUS_DISABLED) 82b5975d6bSopenharmony_ci+ if (match_info->regex->jit_status == JIT_STATUS_DISABLED) 83b5975d6bSopenharmony_ci return JIT_STATUS_DISABLED; 84b5975d6bSopenharmony_ci 85b5975d6bSopenharmony_ci if (match_options & G_REGEX_PCRE2_JIT_UNSUPPORTED_OPTIONS) 86b5975d6bSopenharmony_ci return JIT_STATUS_DISABLED; 87b5975d6bSopenharmony_ci 88b5975d6bSopenharmony_ci- old_jit_options = regex->jit_options; 89b5975d6bSopenharmony_ci+ old_jit_options = match_info->regex->jit_options; 90b5975d6bSopenharmony_ci new_jit_options = old_jit_options | PCRE2_JIT_COMPLETE; 91b5975d6bSopenharmony_ci if (match_options & PCRE2_PARTIAL_HARD) 92b5975d6bSopenharmony_ci new_jit_options |= PCRE2_JIT_PARTIAL_HARD; 93b5975d6bSopenharmony_ci@@ -920,13 +921,16 @@ enable_jit_with_match_options (GRegex *regex, 94b5975d6bSopenharmony_ci 95b5975d6bSopenharmony_ci /* no new options enabled */ 96b5975d6bSopenharmony_ci if (new_jit_options == old_jit_options) 97b5975d6bSopenharmony_ci- return regex->jit_status; 98b5975d6bSopenharmony_ci+ return match_info->regex->jit_status; 99b5975d6bSopenharmony_ci 100b5975d6bSopenharmony_ci- retval = pcre2_jit_compile (regex->pcre_re, new_jit_options); 101b5975d6bSopenharmony_ci+ retval = pcre2_jit_compile (match_info->regex->pcre_re, new_jit_options); 102b5975d6bSopenharmony_ci switch (retval) 103b5975d6bSopenharmony_ci { 104b5975d6bSopenharmony_ci case 0: /* JIT enabled successfully */ 105b5975d6bSopenharmony_ci- regex->jit_options = new_jit_options; 106b5975d6bSopenharmony_ci+ match_info->regex->jit_options = new_jit_options; 107b5975d6bSopenharmony_ci+ /* Set min stack size for JIT to 32KiB and max to 512KiB */ 108b5975d6bSopenharmony_ci+ match_info->jit_stack = pcre2_jit_stack_create (1 << 15, 1 << 19, NULL); 109b5975d6bSopenharmony_ci+ pcre2_jit_stack_assign (match_info->match_context, NULL, match_info->jit_stack); 110b5975d6bSopenharmony_ci return JIT_STATUS_ENABLED; 111b5975d6bSopenharmony_ci case PCRE2_ERROR_NOMEMORY: 112b5975d6bSopenharmony_ci g_debug ("JIT compilation was requested with G_REGEX_OPTIMIZE, " 113b5975d6bSopenharmony_ci@@ -1023,6 +1027,8 @@ g_match_info_unref (GMatchInfo *match_info) 114b5975d6bSopenharmony_ci g_regex_unref (match_info->regex); 115b5975d6bSopenharmony_ci if (match_info->match_context) 116b5975d6bSopenharmony_ci pcre2_match_context_free (match_info->match_context); 117b5975d6bSopenharmony_ci+ if (match_info->jit_stack) 118b5975d6bSopenharmony_ci+ pcre2_jit_stack_free (match_info->jit_stack); 119b5975d6bSopenharmony_ci if (match_info->match_data) 120b5975d6bSopenharmony_ci pcre2_match_data_free (match_info->match_data); 121b5975d6bSopenharmony_ci g_free (match_info->offsets); 122b5975d6bSopenharmony_ci@@ -1091,7 +1097,7 @@ g_match_info_next (GMatchInfo *match_info, 123b5975d6bSopenharmony_ci 124b5975d6bSopenharmony_ci opts = match_info->regex->match_opts | match_info->match_opts; 125b5975d6bSopenharmony_ci 126b5975d6bSopenharmony_ci- jit_status = enable_jit_with_match_options (match_info->regex, opts); 127b5975d6bSopenharmony_ci+ jit_status = enable_jit_with_match_options (match_info, opts); 128b5975d6bSopenharmony_ci if (jit_status == JIT_STATUS_ENABLED) 129b5975d6bSopenharmony_ci { 130b5975d6bSopenharmony_ci match_info->matches = pcre2_jit_match (match_info->regex->pcre_re, 131b5975d6bSopenharmony_cidiff --git a/glib/tests/regex.c b/glib/tests/regex.c 132b5975d6bSopenharmony_ciindex cf2bb8199d..821fc59608 100644 133b5975d6bSopenharmony_ci--- a/glib/tests/regex.c 134b5975d6bSopenharmony_ci+++ b/glib/tests/regex.c 135b5975d6bSopenharmony_ci@@ -51,6 +51,9 @@ 136b5975d6bSopenharmony_ci /* A random value use to mark untouched integer variables. */ 137b5975d6bSopenharmony_ci #define UNTOUCHED -559038737 138b5975d6bSopenharmony_ci 139b5975d6bSopenharmony_ci+/* A length of the test string in JIT stack test */ 140b5975d6bSopenharmony_ci+#define TEST_STRING_LEN 20000 141b5975d6bSopenharmony_ci+ 142b5975d6bSopenharmony_ci static gint total; 143b5975d6bSopenharmony_ci 144b5975d6bSopenharmony_ci typedef struct { 145b5975d6bSopenharmony_ci@@ -2481,6 +2484,7 @@ test_jit_unsupported_matching_options (void) 146b5975d6bSopenharmony_ci int 147b5975d6bSopenharmony_ci main (int argc, char *argv[]) 148b5975d6bSopenharmony_ci { 149b5975d6bSopenharmony_ci+ char test_string[TEST_STRING_LEN]; 150b5975d6bSopenharmony_ci setlocale (LC_ALL, ""); 151b5975d6bSopenharmony_ci 152b5975d6bSopenharmony_ci g_test_init (&argc, &argv, NULL); 153b5975d6bSopenharmony_ci@@ -2702,6 +2706,11 @@ G_GNUC_END_IGNORE_DEPRECATIONS 154b5975d6bSopenharmony_ci TEST_MATCH_SIMPLE("\\", "a", 0, 0, FALSE); 155b5975d6bSopenharmony_ci TEST_MATCH_SIMPLE("[", "", 0, 0, FALSE); 156b5975d6bSopenharmony_ci 157b5975d6bSopenharmony_ci+ /* Test that JIT compiler has enough stack */ 158b5975d6bSopenharmony_ci+ memset (test_string, '*', TEST_STRING_LEN); 159b5975d6bSopenharmony_ci+ test_string[TEST_STRING_LEN - 1] = '\0'; 160b5975d6bSopenharmony_ci+ TEST_MATCH_SIMPLE ("^(?:[ \t\n]|[^[:cntrl:]])*$", test_string, 0, 0, TRUE); 161b5975d6bSopenharmony_ci+ 162b5975d6bSopenharmony_ci /* TEST_MATCH(pattern, compile_opts, match_opts, string, 163b5975d6bSopenharmony_ci * string_len, start_position, match_opts2, expected) */ 164b5975d6bSopenharmony_ci TEST_MATCH("a", 0, 0, "a", -1, 0, 0, TRUE); 165b5975d6bSopenharmony_ci-- 166b5975d6bSopenharmony_ciGitLab