Skip to content

Commit 792f63d

Browse files
committed
Fix unstable get_iterator pointer for hooked classes in shm on Windows
Closes GH-17034
1 parent 8491730 commit 792f63d

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ PHP NEWS
1010
- Core:
1111
. Fixed bug OSS-Fuzz #382922236 (Duplicate dynamic properties in hooked object
1212
iterator properties table). (ilutov)
13+
. Fixed unstable get_iterator pointer for hooked classes in shm on Windows.
14+
(ilutov)
1315

1416
- DBA:
1517
. Skip test if inifile is disabled. (orlitzky)

Zend/zend_compile.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8575,10 +8575,13 @@ static void zend_compile_property_hooks(
85758575

85768576
ce->num_hooked_props++;
85778577

8578+
/* See zend_link_hooked_object_iter(). */
8579+
#ifndef ZEND_OPCACHE_SHM_REATTACHMENT
85788580
if (!ce->get_iterator) {
85798581
/* Will be removed again, in case of Iterator or IteratorAggregate. */
85808582
ce->get_iterator = zend_hooked_object_get_iterator;
85818583
}
8584+
#endif
85828585

85838586
if (!prop_info->ce->parent_name) {
85848587
zend_verify_hooked_property(ce, prop_info, prop_name);
@@ -9104,6 +9107,10 @@ static void zend_compile_class_decl(znode *result, zend_ast *ast, bool toplevel)
91049107

91059108
/* We currently don't early-bind classes that implement interfaces or use traits */
91069109
if (!ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9110+
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
9111+
/* See zend_link_hooked_object_iter(). */
9112+
&& !ce->num_hooked_props
9113+
#endif
91079114
&& !(CG(compiler_options) & ZEND_COMPILE_WITHOUT_EXECUTION)) {
91089115
if (toplevel) {
91099116
if (extends_ast) {

Zend/zend_inheritance.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1746,6 +1746,27 @@ 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_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. */
1753+
static void zend_link_hooked_object_iter(zend_class_entry *ce) {
1754+
if (!ce->get_iterator && ce->num_hooked_props) {
1755+
ce->get_iterator = zend_hooked_object_get_iterator;
1756+
ce->ce_flags &= ~ZEND_ACC_CACHEABLE;
1757+
if (CG(current_linking_class) == ce) {
1758+
# if ZEND_DEBUG
1759+
/* This check is executed before inheriting any elements that can
1760+
* track dependencies. */
1761+
HashTable *ht = (HashTable*)ce->inheritance_cache;
1762+
ZEND_ASSERT(!ht);
1763+
# endif
1764+
CG(current_linking_class) = NULL;
1765+
}
1766+
}
1767+
}
1768+
#endif
1769+
17491770
ZEND_API void zend_do_inheritance_ex(zend_class_entry *ce, zend_class_entry *parent_ce, bool checked) /* {{{ */
17501771
{
17511772
zend_property_info *property_info;
@@ -3405,7 +3426,7 @@ static zend_class_entry *zend_lazy_class_load(zend_class_entry *pce)
34053426
return ce;
34063427
}
34073428

3408-
#ifndef ZEND_WIN32
3429+
#ifndef ZEND_OPCACHE_SHM_REATTACHMENT
34093430
# define UPDATE_IS_CACHEABLE(ce) do { \
34103431
if ((ce)->type == ZEND_USER_CLASS) { \
34113432
is_cacheable &= (ce)->ce_flags; \
@@ -3550,6 +3571,10 @@ ZEND_API zend_class_entry *zend_do_link_class(zend_class_entry *ce, zend_string
35503571
zend_enum_register_funcs(ce);
35513572
}
35523573

3574+
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
3575+
zend_link_hooked_object_iter(ce);
3576+
#endif
3577+
35533578
if (parent) {
35543579
if (!(parent->ce_flags & ZEND_ACC_LINKED)) {
35553580
add_dependency_obligation(ce, parent);
@@ -3838,6 +3863,10 @@ ZEND_API zend_class_entry *zend_try_early_bind(zend_class_entry *ce, zend_class_
38383863
zend_begin_record_errors();
38393864
}
38403865

3866+
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
3867+
zend_link_hooked_object_iter(ce);
3868+
#endif
3869+
38413870
zend_do_inheritance_ex(ce, parent_ce, status == INHERITANCE_SUCCESS);
38423871
if (parent_ce && parent_ce->num_interfaces) {
38433872
zend_do_inherit_interfaces(ce, parent_ce);

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 */

ext/opcache/tests/dump_property_hooks.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ opcache.enable_cli=1
66
opcache.opt_debug_level=0x20000
77
--EXTENSIONS--
88
opcache
9+
--SKIPIF--
10+
<?php
11+
if (PHP_OS_FAMILY === 'Windows') {
12+
die('skip Windows emits additional DECLARE_CLASS_DELAYED opcode');
13+
}
14+
?>
915
--FILE--
1016
<?php
1117

0 commit comments

Comments
 (0)