Skip to content

Commit 75a2ee7

Browse files
committed
Fixed bug #75601 Thread race in PCRE JIT support
1 parent ed2434a commit 75a2ee7

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
@@ -68,6 +68,18 @@ PHPAPI ZEND_DECLARE_MODULE_GLOBALS(pcre)
6868
#define PCRE_JIT_STACK_MAX_SIZE (64 * 1024)
6969
ZEND_TLS pcre_jit_stack *jit_stack = NULL;
7070
#endif
71+
#if defined(ZTS) && defined(HAVE_PCRE_JIT_SUPPORT)
72+
static MUTEX_T pcre_mt = NULL;
73+
#define php_pcre_mutex_alloc() if (tsrm_is_main_thread() && !pcre_mt) pcre_mt = tsrm_mutex_alloc();
74+
#define php_pcre_mutex_free() if (tsrm_is_main_thread() && pcre_mt) tsrm_mutex_free(pcre_mt);
75+
#define php_pcre_mutex_lock() tsrm_mutex_lock(pcre_mt);
76+
#define php_pcre_mutex_unlock() tsrm_mutex_unlock(pcre_mt);
77+
#else
78+
#define php_pcre_mutex_alloc()
79+
#define php_pcre_mutex_free()
80+
#define php_pcre_mutex_lock()
81+
#define php_pcre_mutex_unlock()
82+
#endif
7183

7284
static void pcre_handle_exec_error(int pcre_code) /* {{{ */
7385
{
@@ -190,6 +202,8 @@ static PHP_MINIT_FUNCTION(pcre)
190202
{
191203
REGISTER_INI_ENTRIES();
192204

205+
php_pcre_mutex_alloc();
206+
193207
REGISTER_LONG_CONSTANT("PREG_PATTERN_ORDER", PREG_PATTERN_ORDER, CONST_CS | CONST_PERSISTENT);
194208
REGISTER_LONG_CONSTANT("PREG_SET_ORDER", PREG_SET_ORDER, CONST_CS | CONST_PERSISTENT);
195209
REGISTER_LONG_CONSTANT("PREG_OFFSET_CAPTURE", PREG_OFFSET_CAPTURE, CONST_CS | CONST_PERSISTENT);
@@ -217,6 +231,8 @@ static PHP_MSHUTDOWN_FUNCTION(pcre)
217231
{
218232
UNREGISTER_INI_ENTRIES();
219233

234+
php_pcre_mutex_free();
235+
220236
return SUCCESS;
221237
}
222238
/* }}} */
@@ -226,7 +242,9 @@ static PHP_MSHUTDOWN_FUNCTION(pcre)
226242
static PHP_RINIT_FUNCTION(pcre)
227243
{
228244
if (PCRE_G(jit) && jit_stack == NULL) {
245+
php_pcre_mutex_lock();
229246
jit_stack = pcre_jit_stack_alloc(PCRE_JIT_STACK_MIN_SIZE,PCRE_JIT_STACK_MAX_SIZE);
247+
php_pcre_mutex_unlock();
230248
}
231249

232250
return SUCCESS;

0 commit comments

Comments
 (0)