diff --git a/src/operators/verify_cc.cc b/src/operators/verify_cc.cc index bdb63f3452..ee39902d81 100644 --- a/src/operators/verify_cc.cc +++ b/src/operators/verify_cc.cc @@ -105,6 +105,7 @@ bool VerifyCC::init(const std::string ¶m2, std::string *error) { if (m_pc == NULL) { return false; } + m_pcje = pcre2_jit_compile(m_pc, PCRE2_JIT_COMPLETE); #else const char *errptr = NULL; int erroffset = 0; @@ -142,8 +143,16 @@ bool VerifyCC::evaluate(Transaction *t, RuleWithActions *rule, PCRE2_SPTR pcre2_i = reinterpret_cast(i.c_str()); pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL); + int ret; for (offset = 0; offset < target_length; offset++) { - int ret = pcre2_match(m_pc, pcre2_i, target_length, offset, 0, match_data, NULL); + + if (m_pcje == 0) { + ret = pcre2_jit_match(m_pc, pcre2_i, target_length, offset, 0, match_data, NULL); + } + + if (m_pcje != 0 || ret == PCRE2_ERROR_JIT_STACKLIMIT) { + ret = pcre2_match(m_pc, pcre2_i, target_length, offset, PCRE2_NO_JIT, match_data, NULL); + } /* If there was no match, then we are done. */ if (ret < 0) { diff --git a/src/operators/verify_cc.h b/src/operators/verify_cc.h index 91195378a4..e2a5e24280 100644 --- a/src/operators/verify_cc.h +++ b/src/operators/verify_cc.h @@ -39,7 +39,12 @@ class VerifyCC : public Operator { explicit VerifyCC(std::unique_ptr param) : Operator("VerifyCC", std::move(param)), #if WITH_PCRE2 - m_pc(NULL) { } + m_pc(NULL) + { +#if WITH_PCRE2 + m_pcje = PCRE2_ERROR_JIT_BADOPTION; +#endif + } #else m_pc(NULL), m_pce(NULL) { } @@ -53,6 +58,7 @@ class VerifyCC : public Operator { private: #if WITH_PCRE2 pcre2_code *m_pc; + int m_pcje; #else pcre *m_pc; pcre_extra *m_pce; diff --git a/src/utils/regex.cc b/src/utils/regex.cc index 45878ed09f..0143a0dcca 100644 --- a/src/utils/regex.cc +++ b/src/utils/regex.cc @@ -73,6 +73,7 @@ Regex::Regex(const std::string& pattern_, bool ignoreCase) PCRE2_SIZE erroroffset = 0; m_pc = pcre2_compile(pcre2_pattern, PCRE2_ZERO_TERMINATED, pcre2_options, &errornumber, &erroroffset, NULL); + m_pcje = pcre2_jit_compile(m_pc, PCRE2_JIT_COMPLETE); #else const char *errptr = NULL; int erroffset; @@ -118,8 +119,15 @@ std::list Regex::searchAll(const std::string& s) const { pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL); do { - rc = pcre2_match(m_pc, pcre2_s, s.length(), - offset, 0, match_data, NULL); + if (m_pcje == 0) { + rc = pcre2_jit_match(m_pc, pcre2_s, s.length(), + offset, 0, match_data, NULL); + } + + if (m_pcje != 0 || rc == PCRE2_ERROR_JIT_STACKLIMIT) { + rc = pcre2_match(m_pc, pcre2_s, s.length(), + offset, PCRE2_NO_JIT, match_data, NULL); + } PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data); #else const char *subject = s.c_str(); @@ -159,7 +167,14 @@ bool Regex::searchOneMatch(const std::string& s, std::vector& cap #ifdef WITH_PCRE2 PCRE2_SPTR pcre2_s = reinterpret_cast(s.c_str()); pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL); - int rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, NULL); + int rc; + if (m_pcje == 0) { + rc = pcre2_jit_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, NULL); + } + + if (m_pcje != 0 || rc == PCRE2_ERROR_JIT_STACKLIMIT) { + rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, PCRE2_NO_JIT, match_data, NULL); + } PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data); #else const char *subject = s.c_str(); @@ -198,7 +213,7 @@ bool Regex::searchGlobal(const std::string& s, std::vector& captu pcre2_options = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED; } int rc = pcre2_match(m_pc, pcre2_s, s.length(), - startOffset, pcre2_options, match_data, NULL); + startOffset, pcre2_options, match_data, NULL); PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data); #else @@ -270,9 +285,16 @@ int Regex::search(const std::string& s, SMatch *match) const { #ifdef WITH_PCRE2 PCRE2_SPTR pcre2_s = reinterpret_cast(s.c_str()); pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL); - int ret = pcre2_match(m_pc, pcre2_s, s.length(), - 0, 0, match_data, NULL) > 0; - + int ret; + if (m_pcje == 0) { + ret = pcre2_match(m_pc, pcre2_s, s.length(), + 0, 0, match_data, NULL) > 0; + } + + if (m_pcje != 0 || ret == PCRE2_ERROR_JIT_STACKLIMIT) { + ret = pcre2_match(m_pc, pcre2_s, s.length(), + 0, PCRE2_NO_JIT, match_data, NULL) > 0; + } if (ret > 0) { // match PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data); #else @@ -297,7 +319,14 @@ int Regex::search(const std::string& s) const { #ifdef WITH_PCRE2 PCRE2_SPTR pcre2_s = reinterpret_cast(s.c_str()); pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL); - int rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, NULL); + int rc; + if (m_pcje == 0) { + rc = pcre2_jit_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, NULL); + } + + if (m_pcje != 0 || rc == PCRE2_ERROR_JIT_STACKLIMIT) { + rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, PCRE2_NO_JIT, match_data, NULL); + } pcre2_match_data_free(match_data); if (rc > 0) { return 1; // match diff --git a/src/utils/regex.h b/src/utils/regex.h index 6bd8ce927d..41755d4418 100644 --- a/src/utils/regex.h +++ b/src/utils/regex.h @@ -85,6 +85,7 @@ class Regex { private: #if WITH_PCRE2 pcre2_code *m_pc; + int m_pcje; #else pcre *m_pc = NULL; pcre_extra *m_pce = NULL;