mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-08-15 23:55:03 +03:00
Merge pull request #2791 from wfjsw/feature/pcre2-jit
Add JIT support for PCRE2
This commit is contained in:
commit
f037bd2685
@ -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<PCRE2_SPTR>(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) {
|
||||
|
@ -39,7 +39,12 @@ class VerifyCC : public Operator {
|
||||
explicit VerifyCC(std::unique_ptr<RunTimeString> 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;
|
||||
|
@ -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<SMatch> 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(),
|
||||
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<SMatchCapture>& cap
|
||||
#ifdef WITH_PCRE2
|
||||
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(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();
|
||||
@ -270,9 +285,16 @@ int Regex::search(const std::string& s, SMatch *match) const {
|
||||
#ifdef WITH_PCRE2
|
||||
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(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(),
|
||||
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<PCRE2_SPTR>(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
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user