Skip to content

Support for JIT option for PCRE2 #2840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
DD mmm YYYY - 2.9.x (to be released)
-------------------

* Support for JIT option for PCRE2
[Issue #2840 - @martinhsv]
* Use uid for user if apr_uid_name_get() fails
[Issue #2046 - @arminabf, @marcstern]
* Fix: handle error with SecConnReadStateLimit configuration
Expand Down
35 changes: 24 additions & 11 deletions apache2/msc_pcre.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ void *msc_pregcomp_ex(apr_pool_t *pool, const char *pattern, int options,
return NULL;
}

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

/* Setup the pcre2 match context */
regex->match_context = NULL;
Expand Down Expand Up @@ -242,27 +244,38 @@ int msc_regexec_ex(msc_regex_t *regex, const char *s, unsigned int slen,
PCRE2_SPTR pcre2_s;
int pcre2_ret;
pcre2_match_data *match_data;
PCRE2_SIZE *pcre2_ovector = NULL;
PCRE2_SIZE *pcre2_ovector = NULL;

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

pcre2_ret = pcre2_match(regex->re, pcre2_s, (PCRE2_SIZE)strlen(s),
#ifdef WITH_PCRE_JIT
if (regex->jit_compile_rc == 0) {
pcre2_ret = pcre2_jit_match(regex->re, pcre2_s, (PCRE2_SIZE)strlen(s),
(PCRE2_SIZE)(startoffset), (uint32_t)options, match_data, regex->match_context);
}
if (regex->jit_compile_rc != 0 || pcre2_ret == PCRE2_ERROR_JIT_STACKLIMIT) {
pcre2_ret = pcre2_match(regex->re, pcre2_s, (PCRE2_SIZE)strlen(s),
(PCRE2_SIZE)(startoffset), (PCRE2_NO_JIT | (uint32_t)options), match_data, regex->match_context);
}
#else
pcre2_ret = pcre2_match(regex->re, pcre2_s, (PCRE2_SIZE)strlen(s),
(PCRE2_SIZE)(startoffset), (uint32_t)options, match_data, regex->match_context);
if (match_data != NULL) {
if (ovector != NULL) {
pcre2_ovector = pcre2_get_ovector_pointer(match_data);
if (pcre2_ovector != NULL) {
#endif
if (match_data != NULL) {
if (ovector != NULL) {
pcre2_ovector = pcre2_get_ovector_pointer(match_data);
if (pcre2_ovector != NULL) {
for (int i = 0; ((i < pcre2_ret) && ((i*2) <= ovecsize)); i++) {
if ((i*2) < ovecsize) {
ovector[2*i] = pcre2_ovector[2*i];
ovector[2*i+1] = pcre2_ovector[2*i+1];
}
}
}
}
}
}
}
pcre2_match_data_free(match_data);
}
}
if ((pcre2_ret*2) > ovecsize) {
return 0;
} else {
Expand Down
4 changes: 4 additions & 0 deletions apache2/msc_pcre.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ struct msc_regex_t {
#ifdef WITH_PCRE2
pcre2_code *re;
pcre2_match_context *match_context;
#ifdef WITH_PCRE_JIT
int jit_compile_rc;
#endif

#else
void *re;
void *pe;
Expand Down
28 changes: 28 additions & 0 deletions apache2/re_operators.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,11 @@ static int msre_op_validateHash_param_init(msre_rule *rule, char **error_msg) {

#ifdef WITH_PCRE_STUDY
#ifdef WITH_PCRE_JIT
#ifdef WITH_PCRE2
rc = regex->jit_compile_rc;
#else
rc = msc_fullinfo(regex, PCRE_INFO_JIT, &jit);
#endif
if ((rc != 0) || (jit != 1)) {
*error_msg = apr_psprintf(rule->ruleset->mp,
"Rule %pp [id \"%s\"][file \"%s\"][line \"%d\"] - "
Expand Down Expand Up @@ -808,7 +812,11 @@ static int msre_op_validateHash_execute(modsec_rec *msr, msre_rule *rule, msre_v
#ifdef WITH_PCRE_STUDY
#ifdef WITH_PCRE_JIT
if (msr->txcfg->debuglog_level >= 4) {
#ifdef WITH_PCRE2
rc = regex->jit_compile_rc;
#else
rc = msc_fullinfo(regex, PCRE_INFO_JIT, &jit);
#endif
if ((rc != 0) || (jit != 1)) {
*error_msg = apr_psprintf(msr->mp,
"Rule %pp [id \"%s\"][file \"%s\"][line \"%d\"] - "
Expand Down Expand Up @@ -973,7 +981,11 @@ static int msre_op_rx_param_init(msre_rule *rule, char **error_msg) {

#ifdef WITH_PCRE_STUDY
#ifdef WITH_PCRE_JIT
#ifdef WITH_PCRE2
rc = regex->jit_compile_rc;
#else
rc = msc_fullinfo(regex, PCRE_INFO_JIT, &jit);
#endif
if ((rc != 0) || (jit != 1)) {
*error_msg = apr_psprintf(rule->ruleset->mp,
"Rule %pp [id \"%s\"][file \"%s\"][line \"%d\"] - "
Expand Down Expand Up @@ -1060,7 +1072,11 @@ static int msre_op_rx_execute(modsec_rec *msr, msre_rule *rule, msre_var *var, c
#ifdef WITH_PCRE_STUDY
#ifdef WITH_PCRE_JIT
if (msr->txcfg->debuglog_level >= 4) {
#ifdef WITH_PCRE2
rc = regex->jit_compile_rc;
#else
rc = msc_fullinfo(regex, PCRE_INFO_JIT, &jit);
#endif
if ((rc != 0) || (jit != 1)) {
*error_msg = apr_psprintf(msr->mp,
"Rule %pp [id \"%s\"][file \"%s\"][line \"%d\"] - "
Expand Down Expand Up @@ -2842,7 +2858,11 @@ static int msre_op_verifyCC_execute(modsec_rec *msr, msre_rule *rule, msre_var *
#ifdef WITH_PCRE_STUDY
#ifdef WITH_PCRE_JIT
if (msr->txcfg->debuglog_level >= 4) {
#ifdef WITH_PCRE2
rc = regex->jit_compile_rc;
#else
rc = msc_fullinfo(regex, PCRE_INFO_JIT, &jit);
#endif
if ((rc != 0) || (jit != 1)) {
*error_msg = apr_psprintf(msr->mp,
"Rule %pp [id \"%s\"][file \"%s\"][line \"%d\"] - "
Expand Down Expand Up @@ -3169,7 +3189,11 @@ static int msre_op_verifyCPF_execute(modsec_rec *msr, msre_rule *rule, msre_var
#ifdef WITH_PCRE_STUDY
#ifdef WITH_PCRE_JIT
if (msr->txcfg->debuglog_level >= 4) {
#ifdef WITH_PCRE2
rc = regex->jit_compile_rc;
#else
rc = msc_fullinfo(regex, PCRE_INFO_JIT, &jit);
#endif
if ((rc != 0) || (jit != 1)) {
*error_msg = apr_psprintf(msr->mp,
"Rule %pp [id \"%s\"][file \"%s\"][line \"%d\"] - "
Expand Down Expand Up @@ -3479,7 +3503,11 @@ static int msre_op_verifySSN_execute(modsec_rec *msr, msre_rule *rule, msre_var
#ifdef WITH_PCRE_STUDY
#ifdef WITH_PCRE_JIT
if (msr->txcfg->debuglog_level >= 4) {
#ifdef WITH_PCRE2
rc = regex->jit_compile_rc;
#else
rc = msc_fullinfo(regex, PCRE_INFO_JIT, &jit);
#endif
if ((rc != 0) || (jit != 1)) {
*error_msg = apr_psprintf(msr->mp,
"Rule %pp [id \"%s\"][file \"%s\"][line \"%d\"] - "
Expand Down