Skip to content

PCRE2 support #2857

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

Closed
wants to merge 45 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
94e8a0c
Path core for PCRE2 interoperation
weltling Oct 12, 2017
04218b4
Fix config.w32
weltling Oct 13, 2017
f4f94b6
Fix ret evaluation
weltling Oct 13, 2017
a1f4603
Fix config.m4
weltling Oct 14, 2017
cf9e419
Hide the pcre_cache_entry implementation
weltling Oct 14, 2017
1198ea9
Fix refcount type and add assert
weltling Oct 14, 2017
61db9ee
Fix config.h
weltling Oct 14, 2017
f944361
Add comment
weltling Oct 14, 2017
55f8a49
Fix visibility and double free
weltling Oct 14, 2017
e7a0c8a
Move to pemalloc/pefree
weltling Oct 14, 2017
d86233f
Fix datatype
weltling Oct 14, 2017
23232f9
Only need to assign stack once
weltling Oct 14, 2017
886d28c
Next refactoring round
weltling Oct 15, 2017
b803838
Implement setting extra compilation option and fix X modifier
weltling Oct 15, 2017
62e0e06
Fix JIT ini and subsequent handling
weltling Oct 15, 2017
ec63246
Rework MINFO and add version constants
weltling Oct 15, 2017
fe37d6d
Reorder pce items
weltling Oct 15, 2017
01e6852
Fix test
weltling Oct 15, 2017
0c36aa6
Add missing free
weltling Oct 15, 2017
85bbf2a
Rework comment
weltling Oct 16, 2017
80b9adc
Info table item
weltling Oct 16, 2017
4634d42
More robust PCRE2 initialization
weltling Oct 22, 2017
7151a34
Drop unused var
weltling Oct 22, 2017
fbe37cd
Fix start offset datatype and handling
weltling Oct 22, 2017
de10427
Retry PCRE2 init also in MINIT
weltling Oct 22, 2017
5c51c1d
Fix datatype
weltling Oct 22, 2017
7eda1e1
Not needed anymore with PCRE2
weltling Oct 22, 2017
5f3b8d7
Remove TODO
weltling Oct 22, 2017
34e1a35
Avoid unnecessary scoped var
weltling Oct 22, 2017
bebc1b0
Remove unused files
weltling Oct 31, 2017
14a366e
Bad UTF error is handled another way
weltling Nov 5, 2017
425c933
Check match data creation
weltling Nov 5, 2017
15e5094
More error checks
weltling Nov 5, 2017
08e0739
Error checks done and otherwise these functions return zero
weltling Nov 5, 2017
a4efe41
Missed error check
weltling Nov 5, 2017
8869ba3
Fix external PCRE2 version check
weltling Nov 9, 2017
15bb41a
Sync jit availability checks
weltling Nov 9, 2017
c9b4822
Fix symbol check for external pcre2
weltling Nov 9, 2017
0864586
Fix add library for external pcre2
weltling Nov 9, 2017
be984c0
Preallocate pcre2_match_data for offsets num <= 32
weltling Nov 13, 2017
ec68a9f
Zero global mdata after free
weltling Nov 13, 2017
cd2c26c
Check jit in pattern by flag instead of doing it on demand
dstogov Nov 13, 2017
5cc3525
Memorize match data usage
dstogov Nov 13, 2017
e110793
Don't overwrite poptions
weltling Nov 13, 2017
beabacb
Expand on preallocated match data usage
weltling Nov 13, 2017
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
Prev Previous commit
Next Next commit
More robust PCRE2 initialization
  • Loading branch information
weltling committed Nov 9, 2017
commit 4634d42c13df2e74a9e5fe3fc44efa251c147851
127 changes: 101 additions & 26 deletions ext/pcre/php_pcre.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ ZEND_TLS pcre2_general_context *gctx = NULL;
contexts at all, but creates for every pce. */
ZEND_TLS pcre2_compile_context *cctx = NULL;
ZEND_TLS pcre2_match_context *mctx = NULL;
ZEND_TLS uint8_t pcre2_init_ok = 0;

static void pcre_handle_exec_error(int pcre_code) /* {{{ */
{
Expand Down Expand Up @@ -153,46 +154,96 @@ static void php_pcre_free(void *block, void *data)

#define PHP_PCRE_DEFAULT_EXTRA_COPTIONS PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL

static PHP_GINIT_FUNCTION(pcre) /* {{{ */
{
zend_hash_init(&pcre_globals->pcre_cache, 0, NULL, php_free_pcre_cache, 1);
pcre_globals->backtrack_limit = 0;
pcre_globals->recursion_limit = 0;
pcre_globals->error_code = PHP_PCRE_NO_ERROR;
static void php_pcre_init_pcre2(uint8_t jit)
{/*{{{*/
if (!gctx) {
gctx = pcre2_general_context_create(php_pcre_malloc, php_pcre_free, NULL);
if (!gctx) {
pcre2_init_ok = 0;
return;
}
}

if (!cctx) {
cctx = pcre2_compile_context_create(gctx);
if (!cctx) {
pcre2_init_ok = 0;
return;
}
}

/* XXX error check! */
gctx = pcre2_general_context_create(php_pcre_malloc, php_pcre_free, NULL);
cctx = pcre2_compile_context_create(gctx);
/* XXX The 'X' modifier is the default behavior in PCRE2. This option is
called dangerous in the manual, as typos in patterns can cause
unexpected results. We might want to to switch to the default PCRE2
behavior, too, thus causing a certain BC break. */
pcre2_set_compile_extra_options(cctx, PHP_PCRE_DEFAULT_EXTRA_COPTIONS);
mctx = pcre2_match_context_create(gctx);

if (!mctx) {
mctx = pcre2_match_context_create(gctx);
if (!mctx) {
pcre2_init_ok = 0;
return;
}
}

#ifdef HAVE_PCRE_JIT_SUPPORT
jit_stack = pcre2_jit_stack_create(PCRE_JIT_STACK_MIN_SIZE, PCRE_JIT_STACK_MAX_SIZE, gctx);
if (jit && !jit_stack) {
jit_stack = pcre2_jit_stack_create(PCRE_JIT_STACK_MIN_SIZE, PCRE_JIT_STACK_MAX_SIZE, gctx);
if (!jit_stack) {
pcre2_init_ok = 0;
return;
}
}
#endif
}
/* }}} */

static PHP_GSHUTDOWN_FUNCTION(pcre) /* {{{ */
{
zend_hash_destroy(&pcre_globals->pcre_cache);
pcre2_init_ok = 1;
}/*}}}*/

static void php_pcre_shutdown_pcre2(void)
{/*{{{*/
if (gctx) {
pcre2_general_context_free(gctx);
gctx = NULL;
}

pcre2_general_context_free(gctx);
gctx = NULL;
pcre2_compile_context_free(cctx);
cctx = NULL;
pcre2_match_context_free(mctx);
mctx = NULL;
if (cctx) {
pcre2_compile_context_free(cctx);
cctx = NULL;
}

if (mctx) {
pcre2_match_context_free(mctx);
mctx = NULL;
}

#ifdef HAVE_PCRE_JIT_SUPPORT
/* Stack may only be destroyed when no cached patterns
possibly associated with it do exist. */
pcre2_jit_stack_free(jit_stack);
jit_stack = NULL;
if (jit_stack) {
pcre2_jit_stack_free(jit_stack);
jit_stack = NULL;
}
#endif

pcre2_init_ok = 0;
}/*}}}*/

static PHP_GINIT_FUNCTION(pcre) /* {{{ */
{
zend_hash_init(&pcre_globals->pcre_cache, 0, NULL, php_free_pcre_cache, 1);
pcre_globals->backtrack_limit = 0;
pcre_globals->recursion_limit = 0;
pcre_globals->error_code = PHP_PCRE_NO_ERROR;

php_pcre_init_pcre2(1);
}
/* }}} */

static PHP_GSHUTDOWN_FUNCTION(pcre) /* {{{ */
{
zend_hash_destroy(&pcre_globals->pcre_cache);

php_pcre_shutdown_pcre2();
}
/* }}} */

Expand Down Expand Up @@ -298,7 +349,12 @@ static PHP_MINFO_FUNCTION(pcre)
/* {{{ PHP_MINIT_FUNCTION(pcre) */
static PHP_MINIT_FUNCTION(pcre)
{
char *version = _pcre2_config_str(PCRE2_CONFIG_VERSION);
char *version;

if (!pcre2_init_ok) {
return FAILURE;
}

REGISTER_INI_ENTRIES();

REGISTER_LONG_CONSTANT("PREG_PATTERN_ORDER", PREG_PATTERN_ORDER, CONST_CS | CONST_PERSISTENT);
Expand All @@ -317,6 +373,7 @@ static PHP_MINIT_FUNCTION(pcre)
REGISTER_LONG_CONSTANT("PREG_BAD_UTF8_ERROR", PHP_PCRE_BAD_UTF8_ERROR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PREG_BAD_UTF8_OFFSET_ERROR", PHP_PCRE_BAD_UTF8_OFFSET_ERROR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PREG_JIT_STACKLIMIT_ERROR", PHP_PCRE_JIT_STACKLIMIT_ERROR, CONST_CS | CONST_PERSISTENT);
version = _pcre2_config_str(PCRE2_CONFIG_VERSION);
REGISTER_STRING_CONSTANT("PCRE_VERSION", version, CONST_CS | CONST_PERSISTENT);
free(version);
REGISTER_LONG_CONSTANT("PCRE_VERSION_MAJOR", PCRE2_MAJOR, CONST_CS | CONST_PERSISTENT);
Expand All @@ -335,6 +392,21 @@ static PHP_MSHUTDOWN_FUNCTION(pcre)
}
/* }}} */

/* {{{ PHP_RINIT_FUNCTION(pcre) */
static PHP_RINIT_FUNCTION(pcre)
{
if (!pcre2_init_ok) {
/* Retry. */
php_pcre_init_pcre2(PCRE_G(jit));
if (!pcre2_init_ok) {
return FAILURE;
}
}

return SUCCESS;
}
/* }}} */

/* {{{ static pcre_clean_cache */
static int pcre_clean_cache(zval *data, void *arg)
{
Expand Down Expand Up @@ -2689,7 +2761,7 @@ zend_module_entry pcre_module_entry = {
pcre_functions,
PHP_MINIT(pcre),
PHP_MSHUTDOWN(pcre),
NULL,
PHP_RINIT(pcre),
NULL,
PHP_MINFO(pcre),
PHP_PCRE_VERSION,
Expand Down Expand Up @@ -2723,17 +2795,20 @@ PHPAPI pcre2_compile_context *php_pcre_cctx(void)

PHPAPI void php_pcre_pce_incref(pcre_cache_entry *pce)
{/*{{{*/
assert(NULL != pce);
pce->refcount++;
}/*}}}*/

PHPAPI void php_pcre_pce_decref(pcre_cache_entry *pce)
{/*{{{*/
assert(NULL != pce);
assert(0 != pce->refcount);
pce->refcount--;
}/*}}}*/

PHPAPI pcre2_code *php_pcre_pce_re(pcre_cache_entry *pce)
{/*{{{*/
assert(NULL != pce);
return pce->re;
}/*}}}*/

Expand Down