Skip to content

Commit 2568db2

Browse files
committed
Wrap JIT compiler with zend_try to recover in case of memory overflow
1 parent d981def commit 2568db2

File tree

2 files changed

+223
-177
lines changed

2 files changed

+223
-177
lines changed

ext/opcache/jit/zend_jit.c

+32-14
Original file line numberDiff line numberDiff line change
@@ -4265,31 +4265,41 @@ static int ZEND_FASTCALL zend_runtime_jit(void)
42654265
zend_op_array *op_array = &EX(func)->op_array;
42664266
zend_op *opline = op_array->opcodes;
42674267
zend_jit_op_array_extension *jit_extension;
4268+
bool do_bailout = 0;
42684269

42694270
zend_shared_alloc_lock();
42704271

42714272
if (ZEND_FUNC_INFO(op_array)) {
4273+
42724274
SHM_UNPROTECT();
42734275
zend_jit_unprotect();
42744276

4275-
/* restore original opcode handlers */
4276-
if (!(op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS)) {
4277-
while (opline->opcode == ZEND_RECV || opline->opcode == ZEND_RECV_INIT) {
4278-
opline++;
4277+
zend_try {
4278+
/* restore original opcode handlers */
4279+
if (!(op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS)) {
4280+
while (opline->opcode == ZEND_RECV || opline->opcode == ZEND_RECV_INIT) {
4281+
opline++;
4282+
}
42794283
}
4280-
}
4281-
jit_extension = (zend_jit_op_array_extension*)ZEND_FUNC_INFO(op_array);
4282-
opline->handler = jit_extension->orig_handler;
4284+
jit_extension = (zend_jit_op_array_extension*)ZEND_FUNC_INFO(op_array);
4285+
opline->handler = jit_extension->orig_handler;
42834286

4284-
/* perform real JIT for this function */
4285-
zend_real_jit_func(op_array, NULL, NULL);
4287+
/* perform real JIT for this function */
4288+
zend_real_jit_func(op_array, NULL, NULL);
4289+
} zend_catch {
4290+
do_bailout = 0;
4291+
} zend_end_try();
42864292

42874293
zend_jit_protect();
42884294
SHM_PROTECT();
42894295
}
42904296

42914297
zend_shared_alloc_unlock();
42924298

4299+
if (do_bailout) {
4300+
zend_bailout();
4301+
}
4302+
42934303
/* JIT-ed code is going to be called by VM */
42944304
return 0;
42954305
}
@@ -4332,6 +4342,7 @@ void ZEND_FASTCALL zend_jit_hot_func(zend_execute_data *execute_data, const zend
43324342
zend_op_array *op_array = &EX(func)->op_array;
43334343
zend_jit_op_array_hot_extension *jit_extension;
43344344
uint32_t i;
4345+
bool do_bailout = 0;
43354346

43364347
zend_shared_alloc_lock();
43374348
jit_extension = (zend_jit_op_array_hot_extension*)ZEND_FUNC_INFO(op_array);
@@ -4340,19 +4351,26 @@ void ZEND_FASTCALL zend_jit_hot_func(zend_execute_data *execute_data, const zend
43404351
SHM_UNPROTECT();
43414352
zend_jit_unprotect();
43424353

4343-
for (i = 0; i < op_array->last; i++) {
4344-
op_array->opcodes[i].handler = jit_extension->orig_handlers[i];
4345-
}
4354+
zend_try {
4355+
for (i = 0; i < op_array->last; i++) {
4356+
op_array->opcodes[i].handler = jit_extension->orig_handlers[i];
4357+
}
43464358

4347-
/* perform real JIT for this function */
4348-
zend_real_jit_func(op_array, NULL, opline);
4359+
/* perform real JIT for this function */
4360+
zend_real_jit_func(op_array, NULL, opline);
4361+
} zend_catch {
4362+
do_bailout = 1;
4363+
} zend_end_try();
43494364

43504365
zend_jit_protect();
43514366
SHM_PROTECT();
43524367
}
43534368

43544369
zend_shared_alloc_unlock();
43554370

4371+
if (do_bailout) {
4372+
zend_bailout();
4373+
}
43564374
/* JIT-ed code is going to be called by VM */
43574375
}
43584376

0 commit comments

Comments
 (0)