]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
Fix pcre jit fast path (one more time)
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 23 Feb 2016 17:47:33 +0000 (17:47 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 23 Feb 2016 17:47:33 +0000 (17:47 +0000)
src/libutil/regexp.c
src/libutil/regexp.h

index 4cc0125679a79ba28b7307c1a68861ecd44881f5..02f734d661b7e7767ae8006f0f9ff5f5e6e63a04 100644 (file)
@@ -181,6 +181,7 @@ rspamd_regexp_post_process (rspamd_regexp_t *r)
        }
        if (pcre2_jit_compile (r->re, jit_flags) < 0) {
                msg_err ("jit compilation of %s is not supported", r->pattern);
+               r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT;
        }
 
        if (pcre2_pattern_info (r->re, PCRE2_INFO_JITSIZE, &jsz) >= 0 && jsz > 0) {
@@ -188,9 +189,10 @@ rspamd_regexp_post_process (rspamd_regexp_t *r)
        }
        else {
                msg_err ("jit compilation of %s is not supported", r->pattern);
+               r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT;
        }
 
-       if (r->jstack) {
+       if (r->jstack && !(r->flags & RSPAMD_REGEXP_FLAG_DISABLE_JIT)) {
                pcre2_jit_stack_assign (r->mcontext, NULL, r->jstack);
        }
 
@@ -199,16 +201,18 @@ rspamd_regexp_post_process (rspamd_regexp_t *r)
 
                if (pcre2_jit_compile (r->raw_re, jit_flags) < 0) {
                        msg_debug ("jit compilation of %s is not supported", r->pattern);
+                       r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT;
                }
 
                if (pcre2_pattern_info (r->raw_re, PCRE2_INFO_JITSIZE, &jsz) >= 0 && jsz > 0) {
                        r->raw_jstack = pcre2_jit_stack_create (32 * 1024, 512 * 1024, NULL);
+                       r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT;
                }
                else {
                        msg_debug ("jit compilation of raw %s is not supported", r->pattern);
                }
 
-               if (r->raw_jstack) {
+               if (r->raw_jstack && !(r->flags & RSPAMD_REGEXP_FLAG_DISABLE_JIT)) {
                        pcre2_jit_stack_assign (r->raw_mcontext, NULL, r->raw_jstack);
                }
        }
@@ -234,6 +238,7 @@ rspamd_regexp_post_process (rspamd_regexp_t *r)
                msg_warn ("cannot optimize regexp pattern: '%s': %s",
                                r->pattern, err_str);
                try_jit = FALSE;
+               r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT;
        }
 
        if (r->raw_re && r->raw_re != r->re) {
@@ -262,6 +267,7 @@ rspamd_regexp_post_process (rspamd_regexp_t *r)
                        if (n != 0 || jit != 1) {
                                msg_debug ("jit compilation of %s is not supported", r->pattern);
                                r->jstack = NULL;
+                               r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT;
                        }
                        else {
                                r->jstack = pcre_jit_stack_alloc (32 * 1024, 512 * 1024);
@@ -273,6 +279,7 @@ rspamd_regexp_post_process (rspamd_regexp_t *r)
        else {
                msg_warn ("cannot optimize regexp pattern: '%s': %s",
                                r->pattern, err_str);
+               r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT;
        }
 
        if (try_raw_jit) {
@@ -292,6 +299,7 @@ rspamd_regexp_post_process (rspamd_regexp_t *r)
                                if (n != 0 || jit != 1) {
                                        msg_debug ("jit compilation of %s is not supported", r->pattern);
                                        r->raw_jstack = NULL;
+                                       r->flags |= RSPAMD_REGEXP_FLAG_DISABLE_JIT;
                                }
                                else {
                                        r->raw_jstack = pcre_jit_stack_alloc (32 * 1024, 512 * 1024);
@@ -593,7 +601,7 @@ rspamd_regexp_search (rspamd_regexp_t *re, const gchar *text, gsize len,
                g_assert (remain > 0);
                g_assert (mt != NULL);
 
-               if (st != NULL) {
+               if (st != NULL && !(re->flags & RSPAMD_REGEXP_FLAG_DISABLE_JIT)) {
                        rc = pcre_jit_exec (r, ext, mt, remain, 0, 0, ovec,
                                        ncaptures, st);
                }
@@ -700,17 +708,24 @@ rspamd_regexp_search (rspamd_regexp_t *re, const gchar *text, gsize len,
                mcontext = re->mcontext;
                match_flags |= PCRE_FLAG(UTF);
 
-               if (!g_utf8_validate (mt, remain, NULL)) {
-                       msg_err ("bad utf8 input for JIT re");
-                       return FALSE;
-               }
        }
 
        match_data = pcre2_match_data_create (re->ncaptures + 1, NULL);
 
 #ifdef HAVE_PCRE_JIT
-       rc = pcre2_jit_match (r,  mt, remain, 0, match_flags, match_data,
-                       mcontext);
+       if (!(re->flags & RSPAMD_REGEXP_FLAG_DISABLE_JIT)) {
+               if (!g_utf8_validate (mt, remain, NULL)) {
+                       msg_err ("bad utf8 input for JIT re");
+                       return FALSE;
+               }
+
+               rc = pcre2_jit_match (r,  mt, remain, 0, match_flags, match_data,
+                               mcontext);
+       }
+       else {
+               rc = pcre2_match (r,  mt, remain, 0, match_flags, match_data,
+                               mcontext);
+       }
 #else
        rc = pcre2_match (r,  mt, remain, 0, match_flags, match_data,
                                        mcontext);
index 72ac6eec7434416ba92c5f95d377e1b0ba06581d..d130dea2ba629647a2840b62acafb206a5308436 100644 (file)
@@ -32,6 +32,7 @@
 #define RSPAMD_REGEXP_FLAG_NOOPT (1 << 2)
 #define RSPAMD_REGEXP_FLAG_FULL_MATCH (1 << 3)
 #define RSPAMD_REGEXP_FLAG_PCRE_ONLY (1 << 4)
+#define RSPAMD_REGEXP_FLAG_DISABLE_JIT (1 << 5)
 
 typedef struct rspamd_regexp_s rspamd_regexp_t;
 struct rspamd_regexp_cache;