Skip to content

Commit 49380b5

Browse files
committed
Fix #81679: Tracing JIT crashes on reattaching
When a new process reattaches to OPcache, tracing JIT causes segfaults, because each new process allocates its own `zend_jit_traces` and `zend_jit_exit_groups` in SHM, although these need to be shared between all processes. We solve that by only allocating these structs for the first process, and store the pointers in `accel_shared_globals`, so we can reassign them when a new process reattaches. Closes GH-7776.
1 parent cd8e6f5 commit 49380b5

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ PHP NEWS
1515
. Fixed bug GH-7765 (php_oci_cleanup_global_handles segfaults at second
1616
call). (cmb)
1717

18+
- OPcache:
19+
. Fixed bug #81679 (Tracing JIT crashes on reattaching). (cmb)
20+
1821
- PDO_PGSQL:
1922
. Fixed error message allocation of PDO PgSQL. (SATO Kentaro)
2023

ext/opcache/ZendAccelerator.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ typedef struct _zend_accel_shared_globals {
285285
/* uninitialized HashTable Support */
286286
uint32_t uninitialized_bucket[-HT_MIN_MASK];
287287

288+
/* Tracing JIT */
289+
void *jit_traces;
290+
const void **jit_exit_groups;
291+
288292
/* Interned Strings Support (must be the last element) */
289293
zend_string_table interned_strings;
290294
} zend_accel_shared_globals;

ext/opcache/jit/zend_jit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4390,7 +4390,7 @@ ZEND_EXT_API int zend_jit_startup(void *buf, size_t size, zend_bool reattached)
43904390
#endif
43914391
}
43924392

4393-
if (zend_jit_trace_startup() != SUCCESS) {
4393+
if (zend_jit_trace_startup(reattached) != SUCCESS) {
43944394
return FAILURE;
43954395
}
43964396

ext/opcache/jit/zend_jit_trace.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,33 @@ static zend_always_inline const char *zend_jit_trace_star_desc(uint8_t trace_fla
4848
}
4949
}
5050

51-
static int zend_jit_trace_startup(void)
51+
static int zend_jit_trace_startup(zend_bool reattached)
5252
{
53-
zend_jit_traces = (zend_jit_trace_info*)zend_shared_alloc(sizeof(zend_jit_trace_info) * JIT_G(max_root_traces));
54-
if (!zend_jit_traces) {
55-
return FAILURE;
56-
}
57-
zend_jit_exit_groups = (const void**)zend_shared_alloc(sizeof(void*) * (ZEND_JIT_TRACE_MAX_EXITS/ZEND_JIT_EXIT_POINTS_PER_GROUP));
58-
if (!zend_jit_exit_groups) {
59-
return FAILURE;
53+
if (!reattached) {
54+
zend_jit_traces = (zend_jit_trace_info*)zend_shared_alloc(sizeof(zend_jit_trace_info) * JIT_G(max_root_traces));
55+
if (!zend_jit_traces) {
56+
return FAILURE;
57+
}
58+
zend_jit_exit_groups = (const void**)zend_shared_alloc(sizeof(void*) * (ZEND_JIT_TRACE_MAX_EXITS/ZEND_JIT_EXIT_POINTS_PER_GROUP));
59+
if (!zend_jit_exit_groups) {
60+
return FAILURE;
61+
}
62+
ZEND_JIT_TRACE_NUM = 1;
63+
ZEND_JIT_COUNTER_NUM = 0;
64+
ZEND_JIT_EXIT_NUM = 0;
65+
ZEND_JIT_EXIT_COUNTERS = 0;
66+
ZCSG(jit_traces) = zend_jit_traces;
67+
ZCSG(jit_exit_groups) = zend_jit_exit_groups;
68+
} else {
69+
zend_jit_traces = ZCSG(jit_traces);
70+
if (!zend_jit_traces) {
71+
return FAILURE;
72+
}
73+
zend_jit_exit_groups = ZCSG(jit_exit_groups);
74+
if (!zend_jit_exit_groups) {
75+
return FAILURE;
76+
}
6077
}
61-
ZEND_JIT_TRACE_NUM = 1;
62-
ZEND_JIT_COUNTER_NUM = 0;
63-
ZEND_JIT_EXIT_NUM = 0;
64-
ZEND_JIT_EXIT_COUNTERS = 0;
6578

6679
memset(&dummy_op_array, 0, sizeof(dummy_op_array));
6780
dummy_op_array.fn_flags = ZEND_ACC_DONE_PASS_TWO;

0 commit comments

Comments
 (0)