[glib/wip/3v1n0/regex-pcre2-flags-fixes: 10/11] glib/regex: Do not use JIT when using unsupported match options
- From: Marco Trevisan <marcotrevi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/wip/3v1n0/regex-pcre2-flags-fixes: 10/11] glib/regex: Do not use JIT when using unsupported match options
- Date: Fri, 9 Sep 2022 16:52:09 +0000 (UTC)
commit 386ee440d074733e23ee5556209ee69727cea92f
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date: Fri Sep 9 18:30:15 2022 +0200
glib/regex: Do not use JIT when using unsupported match options
Do not store jit status for regex unless during initial compilation.
After that, decide whether to use it depending on matching options.
In fact there are some matching options that are incompatible with JIT,
as the PCRE2 docs states:
Setting PCRE2_ANCHORED or PCRE2_ENDANCHORED at match time is not
supported by the just-in-time (JIT) compiler. If it is set, JIT
matching is disabled and the interpretive code in pcre2_match() is
run. Apart from PCRE2_NO_JIT (obviously), the remaining options are
supported for JIT matching.
Fixes: GNOME/gtksourceview#283
glib/gregex.c | 34 +++++++++++++++++++++-------------
glib/tests/regex.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 68 insertions(+), 13 deletions(-)
---
diff --git a/glib/gregex.c b/glib/gregex.c
index fe7473e628..ac11a879a6 100644
--- a/glib/gregex.c
+++ b/glib/gregex.c
@@ -201,6 +201,9 @@
PCRE2_NEWLINE_CRLF | \
PCRE2_NEWLINE_ANYCRLF)
+#define G_REGEX_PCRE2_JIT_UNSUPPORTED_OPTIONS (PCRE2_ANCHORED | \
+ PCRE2_ENDANCHORED)
+
#define G_REGEX_COMPILE_NEWLINE_MASK (G_REGEX_NEWLINE_CR | \
G_REGEX_NEWLINE_LF | \
G_REGEX_NEWLINE_CRLF | \
@@ -869,7 +872,7 @@ recalc_match_offsets (GMatchInfo *match_info,
return TRUE;
}
-static void
+static JITStatus
enable_jit_with_match_options (GRegex *regex,
uint32_t match_options)
{
@@ -877,9 +880,13 @@ enable_jit_with_match_options (GRegex *regex,
uint32_t old_jit_options, new_jit_options;
if (!(regex->orig_compile_opts & G_REGEX_OPTIMIZE))
- return;
+ return JIT_STATUS_DISABLED;
+
if (regex->jit_status == JIT_STATUS_DISABLED)
- return;
+ return JIT_STATUS_DISABLED;
+
+ if (match_options & G_REGEX_PCRE2_JIT_UNSUPPORTED_OPTIONS)
+ return JIT_STATUS_DISABLED;
old_jit_options = regex->jit_options;
new_jit_options = old_jit_options | PCRE2_JIT_COMPLETE;
@@ -890,34 +897,34 @@ enable_jit_with_match_options (GRegex *regex,
/* no new options enabled */
if (new_jit_options == old_jit_options)
- return;
+ return regex->jit_status;
retval = pcre2_jit_compile (regex->pcre_re, new_jit_options);
switch (retval)
{
case 0: /* JIT enabled successfully */
- regex->jit_status = JIT_STATUS_ENABLED;
regex->jit_options = new_jit_options;
- break;
+ return JIT_STATUS_ENABLED;
case PCRE2_ERROR_NOMEMORY:
g_debug ("JIT compilation was requested with G_REGEX_OPTIMIZE, "
"but JIT was unable to allocate executable memory for the "
"compiler. Falling back to interpretive code.");
- regex->jit_status = JIT_STATUS_DISABLED;
- break;
+ return JIT_STATUS_DISABLED;
case PCRE2_ERROR_JIT_BADOPTION:
g_debug ("JIT compilation was requested with G_REGEX_OPTIMIZE, "
"but JIT support is not available. Falling back to "
"interpretive code.");
- regex->jit_status = JIT_STATUS_DISABLED;
+ return JIT_STATUS_DISABLED;
break;
default:
g_debug ("JIT compilation was requested with G_REGEX_OPTIMIZE, "
"but request for JIT support had unexpectedly failed (error %d). "
"Falling back to interpretive code.", retval);
- regex->jit_status = JIT_STATUS_DISABLED;
+ return JIT_STATUS_DISABLED;
break;
}
+
+ return regex->jit_status;
}
/**
@@ -1039,6 +1046,7 @@ gboolean
g_match_info_next (GMatchInfo *match_info,
GError **error)
{
+ JITStatus jit_status;
gint prev_match_start;
gint prev_match_end;
uint32_t opts;
@@ -1060,8 +1068,8 @@ g_match_info_next (GMatchInfo *match_info,
opts = match_info->regex->match_opts | match_info->match_opts;
- enable_jit_with_match_options (match_info->regex, opts);
- if (match_info->regex->jit_status == JIT_STATUS_ENABLED)
+ jit_status = enable_jit_with_match_options (match_info->regex, opts);
+ if (jit_status == JIT_STATUS_ENABLED)
{
match_info->matches = pcre2_jit_match (match_info->regex->pcre_re,
(PCRE2_SPTR8) match_info->string,
@@ -1727,7 +1735,7 @@ g_regex_new (const gchar *pattern,
regex->orig_compile_opts = compile_options;
regex->match_opts = pcre_match_options;
regex->orig_match_opts = match_options;
- enable_jit_with_match_options (regex, regex->match_opts);
+ regex->jit_status = enable_jit_with_match_options (regex, regex->match_opts);
return regex;
}
diff --git a/glib/tests/regex.c b/glib/tests/regex.c
index 26844d63a7..f1c1f672f3 100644
--- a/glib/tests/regex.c
+++ b/glib/tests/regex.c
@@ -2334,6 +2334,46 @@ test_compile_errors (void)
g_clear_error (&error);
}
+static void
+test_jit_unsupported_matching_options (void)
+{
+ GRegex *regex;
+ GMatchInfo *info;
+
+ regex = g_regex_new ("(\\w+)#(\\w+)", G_REGEX_OPTIMIZE, G_REGEX_MATCH_DEFAULT, NULL);
+
+ g_assert_true (g_regex_match (regex, "aa#bb cc#dd", G_REGEX_MATCH_DEFAULT, &info));
+ g_assert_cmpint (g_match_info_get_match_count (info), ==, 3);
+ g_assert_cmpstr (g_match_info_fetch(info, 1), ==, "aa");
+ g_assert_cmpstr (g_match_info_fetch(info, 2), ==, "bb");
+ g_assert_true (g_match_info_next (info, NULL));
+ g_assert_cmpint (g_match_info_get_match_count (info), ==, 3);
+ g_assert_cmpstr (g_match_info_fetch(info, 1), ==, "cc");
+ g_assert_cmpstr (g_match_info_fetch(info, 2), ==, "dd");
+ g_assert_false (g_match_info_next (info, NULL));
+ g_match_info_free (info);
+
+ g_assert_true (g_regex_match (regex, "aa#bb cc#dd", G_REGEX_MATCH_ANCHORED, &info));
+ g_assert_cmpint (g_match_info_get_match_count (info), ==, 3);
+ g_assert_cmpstr (g_match_info_fetch(info, 1), ==, "aa");
+ g_assert_cmpstr (g_match_info_fetch(info, 2), ==, "bb");
+ g_assert_false (g_match_info_next (info, NULL));
+ g_match_info_free (info);
+
+ g_assert_true (g_regex_match (regex, "aa#bb cc#dd", G_REGEX_MATCH_DEFAULT, &info));
+ g_assert_cmpint (g_match_info_get_match_count (info), ==, 3);
+ g_assert_cmpstr (g_match_info_fetch(info, 1), ==, "aa");
+ g_assert_cmpstr (g_match_info_fetch(info, 2), ==, "bb");
+ g_assert_true (g_match_info_next (info, NULL));
+ g_assert_cmpint (g_match_info_get_match_count (info), ==, 3);
+ g_assert_cmpstr (g_match_info_fetch(info, 1), ==, "cc");
+ g_assert_cmpstr (g_match_info_fetch(info, 2), ==, "dd");
+ g_assert_false (g_match_info_next (info, NULL));
+ g_match_info_free (info);
+
+ g_regex_unref (regex);
+}
+
int
main (int argc, char *argv[])
{
@@ -2352,6 +2392,7 @@ main (int argc, char *argv[])
g_test_add_func ("/regex/explicit-crlf", test_explicit_crlf);
g_test_add_func ("/regex/max-lookbehind", test_max_lookbehind);
g_test_add_func ("/regex/compile-errors", test_compile_errors);
+ g_test_add_func ("/regex/jit-unsupported-matching", test_jit_unsupported_matching_options);
/* TEST_NEW(pattern, compile_opts, match_opts) */
TEST_NEW("[A-Z]+", G_REGEX_CASELESS | G_REGEX_EXTENDED | G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTBOL |
G_REGEX_MATCH_PARTIAL);
@@ -2947,6 +2988,12 @@ main (int argc, char *argv[])
TEST_REPLACE("\\S+", "hello world", 0, "\\U-\\0-", "-HELLO- -WORLD-");
TEST_REPLACE(".", "a", 0, "\\A", NULL);
TEST_REPLACE(".", "a", 0, "\\g", NULL);
+ TEST_REPLACE_OPTIONS("(\\w+)#(\\w+)", "aa#bb cc#dd", 0, "\\2#\\1", "bb#aa dd#cc",
+ G_REGEX_OPTIMIZE|G_REGEX_MULTILINE|G_REGEX_CASELESS,
+ 0);
+ TEST_REPLACE_OPTIONS("(\\w+)#(\\w+)", "aa#bb cc#dd", 0, "\\2#\\1", "bb#aa cc#dd",
+ G_REGEX_OPTIMIZE|G_REGEX_MULTILINE|G_REGEX_CASELESS,
+ G_REGEX_MATCH_ANCHORED);
/* TEST_REPLACE_LIT(pattern, string, start_position, replacement, expected) */
TEST_REPLACE_LIT("a", "ababa", 0, "A", "AbAbA");
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]