Skip to content

Commit 76ce55a

Browse files
authored
Merge pull request #2840 from martinhsv/v2/master
Support for JIT option for PCRE2
2 parents bc8c370 + a17cbc8 commit 76ce55a

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
DD mmm YYYY - 2.9.x (to be released)
22
-------------------
33

4+
* Support for JIT option for PCRE2
5+
[Issue #2840 - @martinhsv]
46
* Use uid for user if apr_uid_name_get() fails
57
[Issue #2046 - @arminabf, @marcstern]
68
* Fix: handle error with SecConnReadStateLimit configuration

apache2/msc_pcre.c

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ void *msc_pregcomp_ex(apr_pool_t *pool, const char *pattern, int options,
8484
return NULL;
8585
}
8686

87-
/* TODO: Add PCRE2 JIT support */
87+
#ifdef WITH_PCRE_JIT
88+
regex->jit_compile_rc = pcre2_jit_compile(regex->re, PCRE2_JIT_COMPLETE);
89+
#endif
8890

8991
/* Setup the pcre2 match context */
9092
regex->match_context = NULL;
@@ -242,27 +244,38 @@ int msc_regexec_ex(msc_regex_t *regex, const char *s, unsigned int slen,
242244
PCRE2_SPTR pcre2_s;
243245
int pcre2_ret;
244246
pcre2_match_data *match_data;
245-
PCRE2_SIZE *pcre2_ovector = NULL;
247+
PCRE2_SIZE *pcre2_ovector = NULL;
246248

247249
pcre2_s = (PCRE2_SPTR)s;
248250
match_data = pcre2_match_data_create_from_pattern(regex->re, NULL);
249251

250-
pcre2_ret = pcre2_match(regex->re, pcre2_s, (PCRE2_SIZE)strlen(s),
252+
#ifdef WITH_PCRE_JIT
253+
if (regex->jit_compile_rc == 0) {
254+
pcre2_ret = pcre2_jit_match(regex->re, pcre2_s, (PCRE2_SIZE)strlen(s),
255+
(PCRE2_SIZE)(startoffset), (uint32_t)options, match_data, regex->match_context);
256+
}
257+
if (regex->jit_compile_rc != 0 || pcre2_ret == PCRE2_ERROR_JIT_STACKLIMIT) {
258+
pcre2_ret = pcre2_match(regex->re, pcre2_s, (PCRE2_SIZE)strlen(s),
259+
(PCRE2_SIZE)(startoffset), (PCRE2_NO_JIT | (uint32_t)options), match_data, regex->match_context);
260+
}
261+
#else
262+
pcre2_ret = pcre2_match(regex->re, pcre2_s, (PCRE2_SIZE)strlen(s),
251263
(PCRE2_SIZE)(startoffset), (uint32_t)options, match_data, regex->match_context);
252-
if (match_data != NULL) {
253-
if (ovector != NULL) {
254-
pcre2_ovector = pcre2_get_ovector_pointer(match_data);
255-
if (pcre2_ovector != NULL) {
264+
#endif
265+
if (match_data != NULL) {
266+
if (ovector != NULL) {
267+
pcre2_ovector = pcre2_get_ovector_pointer(match_data);
268+
if (pcre2_ovector != NULL) {
256269
for (int i = 0; ((i < pcre2_ret) && ((i*2) <= ovecsize)); i++) {
257270
if ((i*2) < ovecsize) {
258271
ovector[2*i] = pcre2_ovector[2*i];
259272
ovector[2*i+1] = pcre2_ovector[2*i+1];
260273
}
261-
}
262-
}
263-
}
274+
}
275+
}
276+
}
264277
pcre2_match_data_free(match_data);
265-
}
278+
}
266279
if ((pcre2_ret*2) > ovecsize) {
267280
return 0;
268281
} else {

apache2/msc_pcre.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ struct msc_regex_t {
4545
#ifdef WITH_PCRE2
4646
pcre2_code *re;
4747
pcre2_match_context *match_context;
48+
#ifdef WITH_PCRE_JIT
49+
int jit_compile_rc;
50+
#endif
51+
4852
#else
4953
void *re;
5054
void *pe;

apache2/re_operators.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,11 @@ static int msre_op_validateHash_param_init(msre_rule *rule, char **error_msg) {
711711

712712
#ifdef WITH_PCRE_STUDY
713713
#ifdef WITH_PCRE_JIT
714+
#ifdef WITH_PCRE2
715+
rc = regex->jit_compile_rc;
716+
#else
714717
rc = msc_fullinfo(regex, PCRE_INFO_JIT, &jit);
718+
#endif
715719
if ((rc != 0) || (jit != 1)) {
716720
*error_msg = apr_psprintf(rule->ruleset->mp,
717721
"Rule %pp [id \"%s\"][file \"%s\"][line \"%d\"] - "
@@ -808,7 +812,11 @@ static int msre_op_validateHash_execute(modsec_rec *msr, msre_rule *rule, msre_v
808812
#ifdef WITH_PCRE_STUDY
809813
#ifdef WITH_PCRE_JIT
810814
if (msr->txcfg->debuglog_level >= 4) {
815+
#ifdef WITH_PCRE2
816+
rc = regex->jit_compile_rc;
817+
#else
811818
rc = msc_fullinfo(regex, PCRE_INFO_JIT, &jit);
819+
#endif
812820
if ((rc != 0) || (jit != 1)) {
813821
*error_msg = apr_psprintf(msr->mp,
814822
"Rule %pp [id \"%s\"][file \"%s\"][line \"%d\"] - "
@@ -973,7 +981,11 @@ static int msre_op_rx_param_init(msre_rule *rule, char **error_msg) {
973981

974982
#ifdef WITH_PCRE_STUDY
975983
#ifdef WITH_PCRE_JIT
984+
#ifdef WITH_PCRE2
985+
rc = regex->jit_compile_rc;
986+
#else
976987
rc = msc_fullinfo(regex, PCRE_INFO_JIT, &jit);
988+
#endif
977989
if ((rc != 0) || (jit != 1)) {
978990
*error_msg = apr_psprintf(rule->ruleset->mp,
979991
"Rule %pp [id \"%s\"][file \"%s\"][line \"%d\"] - "
@@ -1060,7 +1072,11 @@ static int msre_op_rx_execute(modsec_rec *msr, msre_rule *rule, msre_var *var, c
10601072
#ifdef WITH_PCRE_STUDY
10611073
#ifdef WITH_PCRE_JIT
10621074
if (msr->txcfg->debuglog_level >= 4) {
1075+
#ifdef WITH_PCRE2
1076+
rc = regex->jit_compile_rc;
1077+
#else
10631078
rc = msc_fullinfo(regex, PCRE_INFO_JIT, &jit);
1079+
#endif
10641080
if ((rc != 0) || (jit != 1)) {
10651081
*error_msg = apr_psprintf(msr->mp,
10661082
"Rule %pp [id \"%s\"][file \"%s\"][line \"%d\"] - "
@@ -2842,7 +2858,11 @@ static int msre_op_verifyCC_execute(modsec_rec *msr, msre_rule *rule, msre_var *
28422858
#ifdef WITH_PCRE_STUDY
28432859
#ifdef WITH_PCRE_JIT
28442860
if (msr->txcfg->debuglog_level >= 4) {
2861+
#ifdef WITH_PCRE2
2862+
rc = regex->jit_compile_rc;
2863+
#else
28452864
rc = msc_fullinfo(regex, PCRE_INFO_JIT, &jit);
2865+
#endif
28462866
if ((rc != 0) || (jit != 1)) {
28472867
*error_msg = apr_psprintf(msr->mp,
28482868
"Rule %pp [id \"%s\"][file \"%s\"][line \"%d\"] - "
@@ -3169,7 +3189,11 @@ static int msre_op_verifyCPF_execute(modsec_rec *msr, msre_rule *rule, msre_var
31693189
#ifdef WITH_PCRE_STUDY
31703190
#ifdef WITH_PCRE_JIT
31713191
if (msr->txcfg->debuglog_level >= 4) {
3192+
#ifdef WITH_PCRE2
3193+
rc = regex->jit_compile_rc;
3194+
#else
31723195
rc = msc_fullinfo(regex, PCRE_INFO_JIT, &jit);
3196+
#endif
31733197
if ((rc != 0) || (jit != 1)) {
31743198
*error_msg = apr_psprintf(msr->mp,
31753199
"Rule %pp [id \"%s\"][file \"%s\"][line \"%d\"] - "
@@ -3479,7 +3503,11 @@ static int msre_op_verifySSN_execute(modsec_rec *msr, msre_rule *rule, msre_var
34793503
#ifdef WITH_PCRE_STUDY
34803504
#ifdef WITH_PCRE_JIT
34813505
if (msr->txcfg->debuglog_level >= 4) {
3506+
#ifdef WITH_PCRE2
3507+
rc = regex->jit_compile_rc;
3508+
#else
34823509
rc = msc_fullinfo(regex, PCRE_INFO_JIT, &jit);
3510+
#endif
34833511
if ((rc != 0) || (jit != 1)) {
34843512
*error_msg = apr_psprintf(msr->mp,
34853513
"Rule %pp [id \"%s\"][file \"%s\"][line \"%d\"] - "

0 commit comments

Comments
 (0)