Skip to content

Commit 8c5aff6

Browse files
committed
Introduce ZEND_OPCACHE_SHM_REATTACHMENT macro
Makes the code more self-documenting, avoiding a comment in all places. Also makes testing easier on platforms that do not support shm reattachment.
1 parent 6c7e28a commit 8c5aff6

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

Zend/zend_compile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8576,7 +8576,7 @@ static void zend_compile_property_hooks(
85768576
ce->num_hooked_props++;
85778577

85788578
/* See zend_link_hooked_object_iter(). */
8579-
#ifndef ZEND_WIN32
8579+
#ifndef ZEND_OPCACHE_SHM_REATTACHMENT
85808580
if (!ce->get_iterator) {
85818581
/* Will be removed again, in case of Iterator or IteratorAggregate. */
85828582
ce->get_iterator = zend_hooked_object_get_iterator;
@@ -9107,7 +9107,7 @@ static void zend_compile_class_decl(znode *result, zend_ast *ast, bool toplevel)
91079107

91089108
/* We currently don't early-bind classes that implement interfaces or use traits */
91099109
if (!ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9110-
#ifdef ZEND_WIN32
9110+
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
91119111
/* See zend_link_hooked_object_iter(). */
91129112
&& !ce->num_hooked_props
91139113
#endif

Zend/zend_inheritance.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,15 +1746,10 @@ ZEND_API inheritance_status zend_verify_property_hook_variance(const zend_proper
17461746
return zend_perform_covariant_type_check(ce, prop_info->type, ce, value_arg_info->type);
17471747
}
17481748

1749-
#ifdef ZEND_WIN32
1750-
/* Hooked properties set get_iterator, which causes issues on Windows. Windows
1751-
* attaches multiple processes to the same shm, with each process potentially
1752-
* having different addresses to the corresponding get_iterator function due to
1753-
* ASLR. This prevents us from caching the class.
1754-
*
1755-
* To at least cache the unlinked class, avoid early-binding on Windows, and set
1756-
* get_iterator during inheritance. The linked class may not use inheritance
1757-
* cache. */
1749+
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
1750+
/* Hooked properties set get_iterator, which causes issues on for shm
1751+
* reattachment. Avoid early-binding on Windows and set get_iterator during
1752+
* inheritance. The linked class may not use inheritance cache. */
17581753
static void zend_link_hooked_object_iter(zend_class_entry *ce) {
17591754
if (!ce->get_iterator && ce->num_hooked_props) {
17601755
ce->get_iterator = zend_hooked_object_get_iterator;
@@ -3431,7 +3426,7 @@ static zend_class_entry *zend_lazy_class_load(zend_class_entry *pce)
34313426
return ce;
34323427
}
34333428

3434-
#ifndef ZEND_WIN32
3429+
#ifndef ZEND_OPCACHE_SHM_REATTACHMENT
34353430
# define UPDATE_IS_CACHEABLE(ce) do { \
34363431
if ((ce)->type == ZEND_USER_CLASS) { \
34373432
is_cacheable &= (ce)->ce_flags; \
@@ -3576,7 +3571,7 @@ ZEND_API zend_class_entry *zend_do_link_class(zend_class_entry *ce, zend_string
35763571
zend_enum_register_funcs(ce);
35773572
}
35783573

3579-
#ifdef ZEND_WIN32
3574+
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
35803575
zend_link_hooked_object_iter(ce);
35813576
#endif
35823577

@@ -3868,7 +3863,7 @@ ZEND_API zend_class_entry *zend_try_early_bind(zend_class_entry *ce, zend_class_
38683863
zend_begin_record_errors();
38693864
}
38703865

3871-
#ifdef ZEND_WIN32
3866+
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
38723867
zend_link_hooked_object_iter(ce);
38733868
#endif
38743869

Zend/zend_portability.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,4 +863,11 @@ static zend_always_inline uint64_t ZEND_BYTES_SWAP64(uint64_t u)
863863
}
864864
#endif
865865

866+
#ifdef ZEND_WIN32
867+
/* Whether it's allowed to reattach to a shm segment from different processes on
868+
* this platform. This prevents pointing to internal structures from shm due to
869+
* ASLR. Currently only possible on Windows. */
870+
# define ZEND_OPCACHE_SHM_REATTACHMENT 1
871+
#endif
872+
866873
#endif /* ZEND_PORTABILITY_H */

0 commit comments

Comments
 (0)