Skip to content

Commit f037bd2

Browse files
authored
Merge pull request #2791 from wfjsw/feature/pcre2-jit
Add JIT support for PCRE2
2 parents 12e6e32 + 54ff1ea commit f037bd2

File tree

4 files changed

+55
-10
lines changed

4 files changed

+55
-10
lines changed

src/operators/verify_cc.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ bool VerifyCC::init(const std::string &param2, std::string *error) {
105105
if (m_pc == NULL) {
106106
return false;
107107
}
108+
m_pcje = pcre2_jit_compile(m_pc, PCRE2_JIT_COMPLETE);
108109
#else
109110
const char *errptr = NULL;
110111
int erroffset = 0;
@@ -142,8 +143,16 @@ bool VerifyCC::evaluate(Transaction *t, RuleWithActions *rule,
142143
PCRE2_SPTR pcre2_i = reinterpret_cast<PCRE2_SPTR>(i.c_str());
143144
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
144145

146+
int ret;
145147
for (offset = 0; offset < target_length; offset++) {
146-
int ret = pcre2_match(m_pc, pcre2_i, target_length, offset, 0, match_data, NULL);
148+
149+
if (m_pcje == 0) {
150+
ret = pcre2_jit_match(m_pc, pcre2_i, target_length, offset, 0, match_data, NULL);
151+
}
152+
153+
if (m_pcje != 0 || ret == PCRE2_ERROR_JIT_STACKLIMIT) {
154+
ret = pcre2_match(m_pc, pcre2_i, target_length, offset, PCRE2_NO_JIT, match_data, NULL);
155+
}
147156

148157
/* If there was no match, then we are done. */
149158
if (ret < 0) {

src/operators/verify_cc.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ class VerifyCC : public Operator {
3939
explicit VerifyCC(std::unique_ptr<RunTimeString> param)
4040
: Operator("VerifyCC", std::move(param)),
4141
#if WITH_PCRE2
42-
m_pc(NULL) { }
42+
m_pc(NULL)
43+
{
44+
#if WITH_PCRE2
45+
m_pcje = PCRE2_ERROR_JIT_BADOPTION;
46+
#endif
47+
}
4348
#else
4449
m_pc(NULL),
4550
m_pce(NULL) { }
@@ -53,6 +58,7 @@ class VerifyCC : public Operator {
5358
private:
5459
#if WITH_PCRE2
5560
pcre2_code *m_pc;
61+
int m_pcje;
5662
#else
5763
pcre *m_pc;
5864
pcre_extra *m_pce;

src/utils/regex.cc

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Regex::Regex(const std::string& pattern_, bool ignoreCase)
7373
PCRE2_SIZE erroroffset = 0;
7474
m_pc = pcre2_compile(pcre2_pattern, PCRE2_ZERO_TERMINATED,
7575
pcre2_options, &errornumber, &erroroffset, NULL);
76+
m_pcje = pcre2_jit_compile(m_pc, PCRE2_JIT_COMPLETE);
7677
#else
7778
const char *errptr = NULL;
7879
int erroffset;
@@ -118,8 +119,15 @@ std::list<SMatch> Regex::searchAll(const std::string& s) const {
118119

119120
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
120121
do {
121-
rc = pcre2_match(m_pc, pcre2_s, s.length(),
122-
offset, 0, match_data, NULL);
122+
if (m_pcje == 0) {
123+
rc = pcre2_jit_match(m_pc, pcre2_s, s.length(),
124+
offset, 0, match_data, NULL);
125+
}
126+
127+
if (m_pcje != 0 || rc == PCRE2_ERROR_JIT_STACKLIMIT) {
128+
rc = pcre2_match(m_pc, pcre2_s, s.length(),
129+
offset, PCRE2_NO_JIT, match_data, NULL);
130+
}
123131
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
124132
#else
125133
const char *subject = s.c_str();
@@ -159,7 +167,14 @@ bool Regex::searchOneMatch(const std::string& s, std::vector<SMatchCapture>& cap
159167
#ifdef WITH_PCRE2
160168
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
161169
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
162-
int rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, NULL);
170+
int rc;
171+
if (m_pcje == 0) {
172+
rc = pcre2_jit_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, NULL);
173+
}
174+
175+
if (m_pcje != 0 || rc == PCRE2_ERROR_JIT_STACKLIMIT) {
176+
rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, PCRE2_NO_JIT, match_data, NULL);
177+
}
163178
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
164179
#else
165180
const char *subject = s.c_str();
@@ -198,7 +213,7 @@ bool Regex::searchGlobal(const std::string& s, std::vector<SMatchCapture>& captu
198213
pcre2_options = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED;
199214
}
200215
int rc = pcre2_match(m_pc, pcre2_s, s.length(),
201-
startOffset, pcre2_options, match_data, NULL);
216+
startOffset, pcre2_options, match_data, NULL);
202217
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
203218

204219
#else
@@ -270,9 +285,16 @@ int Regex::search(const std::string& s, SMatch *match) const {
270285
#ifdef WITH_PCRE2
271286
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
272287
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
273-
int ret = pcre2_match(m_pc, pcre2_s, s.length(),
274-
0, 0, match_data, NULL) > 0;
275-
288+
int ret;
289+
if (m_pcje == 0) {
290+
ret = pcre2_match(m_pc, pcre2_s, s.length(),
291+
0, 0, match_data, NULL) > 0;
292+
}
293+
294+
if (m_pcje != 0 || ret == PCRE2_ERROR_JIT_STACKLIMIT) {
295+
ret = pcre2_match(m_pc, pcre2_s, s.length(),
296+
0, PCRE2_NO_JIT, match_data, NULL) > 0;
297+
}
276298
if (ret > 0) { // match
277299
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
278300
#else
@@ -297,7 +319,14 @@ int Regex::search(const std::string& s) const {
297319
#ifdef WITH_PCRE2
298320
PCRE2_SPTR pcre2_s = reinterpret_cast<PCRE2_SPTR>(s.c_str());
299321
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(m_pc, NULL);
300-
int rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, NULL);
322+
int rc;
323+
if (m_pcje == 0) {
324+
rc = pcre2_jit_match(m_pc, pcre2_s, s.length(), 0, 0, match_data, NULL);
325+
}
326+
327+
if (m_pcje != 0 || rc == PCRE2_ERROR_JIT_STACKLIMIT) {
328+
rc = pcre2_match(m_pc, pcre2_s, s.length(), 0, PCRE2_NO_JIT, match_data, NULL);
329+
}
301330
pcre2_match_data_free(match_data);
302331
if (rc > 0) {
303332
return 1; // match

src/utils/regex.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class Regex {
8585
private:
8686
#if WITH_PCRE2
8787
pcre2_code *m_pc;
88+
int m_pcje;
8889
#else
8990
pcre *m_pc = NULL;
9091
pcre_extra *m_pce = NULL;

0 commit comments

Comments
 (0)