Skip to content

Commit 31b2b14

Browse files
committed
Fixed bug #75601 Thread race in PCRE JIT support
1 parent 71a460b commit 31b2b14

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

ext/pcre/php_pcre.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ PHPAPI ZEND_DECLARE_MODULE_GLOBALS(pcre)
6767
#define PCRE_JIT_STACK_MAX_SIZE (64 * 1024)
6868
ZEND_TLS pcre_jit_stack *jit_stack = NULL;
6969
#endif
70+
#if defined(ZTS) && defined(HAVE_PCRE_JIT_SUPPORT)
71+
static MUTEX_T pcre_mt = NULL;
72+
#define php_pcre_mutex_alloc() if (!pcre_mt) pcre_mt = tsrm_mutex_alloc();
73+
#define php_pcre_mutex_free() if (pcre_mt) tsrm_mutex_free(pcre_mt);
74+
#define php_pcre_mutex_lock() tsrm_mutex_lock(pcre_mt);
75+
#define php_pcre_mutex_unlock() tsrm_mutex_unlock(pcre_mt);
76+
#else
77+
#define php_pcre_mutex_alloc()
78+
#define php_pcre_mutex_free()
79+
#define php_pcre_mutex_lock()
80+
#define php_pcre_mutex_unlock()
81+
#endif
7082

7183
static void pcre_handle_exec_error(int pcre_code) /* {{{ */
7284
{
@@ -185,6 +197,8 @@ static PHP_MINIT_FUNCTION(pcre)
185197
{
186198
REGISTER_INI_ENTRIES();
187199

200+
php_pcre_mutex_alloc();
201+
188202
REGISTER_LONG_CONSTANT("PREG_PATTERN_ORDER", PREG_PATTERN_ORDER, CONST_CS | CONST_PERSISTENT);
189203
REGISTER_LONG_CONSTANT("PREG_SET_ORDER", PREG_SET_ORDER, CONST_CS | CONST_PERSISTENT);
190204
REGISTER_LONG_CONSTANT("PREG_OFFSET_CAPTURE", PREG_OFFSET_CAPTURE, CONST_CS | CONST_PERSISTENT);
@@ -211,6 +225,8 @@ static PHP_MSHUTDOWN_FUNCTION(pcre)
211225
{
212226
UNREGISTER_INI_ENTRIES();
213227

228+
php_pcre_mutex_free();
229+
214230
return SUCCESS;
215231
}
216232
/* }}} */
@@ -220,7 +236,9 @@ static PHP_MSHUTDOWN_FUNCTION(pcre)
220236
static PHP_RINIT_FUNCTION(pcre)
221237
{
222238
if (PCRE_G(jit) && jit_stack == NULL) {
239+
php_pcre_mutex_lock();
223240
jit_stack = pcre_jit_stack_alloc(PCRE_JIT_STACK_MIN_SIZE,PCRE_JIT_STACK_MAX_SIZE);
241+
php_pcre_mutex_unlock();
224242
}
225243

226244
return SUCCESS;

0 commit comments

Comments
 (0)