Skip to content

Commit

Permalink
Introduce a way to (de)serialize subclasses of un(de)serializable cla…
Browse files Browse the repository at this point in the history
…sses if the appropriate methods are present

This introduces the class flag ZEND_ACC_SUBCLASS_SERIALIZABLE, which
allows classes which are not serializable / deserializable to become
that if a subclass implements the appropriate methods.
Setting this flag through the stubs can be done using
@subclass-serializable.
Introducing this through behaviour a new flag allows for opt-in
behaviour, which is backwards compatible.

Fixes phpGH-8996
  • Loading branch information
nielsdos committed Jan 12, 2023
1 parent 55514a1 commit 59d680c
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 18 deletions.
7 changes: 6 additions & 1 deletion Zend/zend_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ typedef struct _zend_oparray_context {
/* or IS_CONSTANT_VISITED_MARK | | | */
#define ZEND_CLASS_CONST_IS_CASE (1 << 6) /* | | | X */
/* | | | */
/* Class Flags (unused: 15,21,30,31) | | | */
/* Class Flags (unused: 15,21,31) | | | */
/* =========== | | | */
/* | | | */
/* Special class types | | | */
Expand Down Expand Up @@ -297,6 +297,11 @@ typedef struct _zend_oparray_context {
/* Class cannot be serialized or unserialized | | | */
#define ZEND_ACC_NOT_SERIALIZABLE (1 << 29) /* X | | | */
/* | | | */
/* Subclass can be serialized or unserialized even if | | | */
/* the parent cannot be, on the condition that the | | | */
/* subclass implements the appropriate methods. | | | */
#define ZEND_ACC_SUBCLASS_SERIALIZABLE (1 << 30) /* X | | | */
/* | | | */
/* Function Flags (unused: 27-30) | | | */
/* ============== | | | */
/* | | | */
Expand Down
10 changes: 9 additions & 1 deletion Zend/zend_inheritance.c
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,15 @@ ZEND_API void zend_do_inheritance_ex(zend_class_entry *ce, zend_class_entry *par
ce->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
}
}
ce->ce_flags |= parent_ce->ce_flags & (ZEND_HAS_STATIC_IN_METHODS | ZEND_ACC_HAS_TYPE_HINTS | ZEND_ACC_USE_GUARDS | ZEND_ACC_NOT_SERIALIZABLE);
ce->ce_flags |= parent_ce->ce_flags & (ZEND_HAS_STATIC_IN_METHODS | ZEND_ACC_HAS_TYPE_HINTS | ZEND_ACC_USE_GUARDS | ZEND_ACC_NOT_SERIALIZABLE | ZEND_ACC_SUBCLASS_SERIALIZABLE);

if ((parent_ce->ce_flags & (ZEND_ACC_NOT_SERIALIZABLE | ZEND_ACC_SUBCLASS_SERIALIZABLE)) == (ZEND_ACC_NOT_SERIALIZABLE | ZEND_ACC_SUBCLASS_SERIALIZABLE)) {
if (ce->__serialize || ce->__unserialize
|| zend_hash_find_known_hash(&ce->function_table, ZSTR_KNOWN(ZEND_STR_SLEEP))
|| zend_hash_find_known_hash(&ce->function_table, ZSTR_KNOWN(ZEND_STR_WAKEUP))) {
ce->ce_flags &= ~(ZEND_ACC_NOT_SERIALIZABLE | ZEND_ACC_SUBCLASS_SERIALIZABLE);
}
}
}
/* }}} */

Expand Down
16 changes: 16 additions & 0 deletions build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,8 @@ class ClassInfo {
public $isStrictProperties;
/** @var bool */
public $isNotSerializable;
/** @var bool */
public $isSubclassSerializable;
/** @var Name[] */
public $extends;
/** @var Name[] */
Expand Down Expand Up @@ -1673,6 +1675,7 @@ public function __construct(
bool $isDeprecated,
bool $isStrictProperties,
bool $isNotSerializable,
bool $isSubclassSerializable,
array $extends,
array $implements,
array $propertyInfos,
Expand All @@ -1687,6 +1690,7 @@ public function __construct(
$this->isDeprecated = $isDeprecated;
$this->isStrictProperties = $isStrictProperties;
$this->isNotSerializable = $isNotSerializable;
$this->isSubclassSerializable = $isSubclassSerializable;
$this->extends = $extends;
$this->implements = $implements;
$this->propertyInfos = $propertyInfos;
Expand Down Expand Up @@ -1794,6 +1798,10 @@ private function getFlagsAsString(): string
$flags[] = "ZEND_ACC_NOT_SERIALIZABLE";
}

if ($this->isSubclassSerializable) {
$flags[] = "ZEND_ACC_SUBCLASS_SERIALIZABLE";
}

return implode("|", $flags);
}

Expand Down Expand Up @@ -2437,6 +2445,7 @@ function parseClass(
$isDeprecated = false;
$isStrictProperties = false;
$isNotSerializable = false;
$isSubclassSerializable = false;

if ($comment) {
$tags = parseDocComment($comment);
Expand All @@ -2449,10 +2458,16 @@ function parseClass(
$isStrictProperties = true;
} else if ($tag->name === 'not-serializable') {
$isNotSerializable = true;
} else if ($tag->name == 'subclass-serializable') {
$isSubclassSerializable = true;
}
}
}

if (!$isNotSerializable && $isSubclassSerializable) {
throw new Exception("@subclass-serializable without @not-serializable is a no-op");
}

$extends = [];
$implements = [];

Expand Down Expand Up @@ -2484,6 +2499,7 @@ function parseClass(
$isDeprecated,
$isStrictProperties,
$isNotSerializable,
$isSubclassSerializable,
$extends,
$implements,
$properties,
Expand Down
5 changes: 4 additions & 1 deletion ext/curl/curl_file.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

/** @generate-class-entries */

/** @not-serializable */
/**
* @not-serializable
* @subclass-serializable
*/
class CURLFile
{
public string $name = "";
Expand Down
4 changes: 2 additions & 2 deletions ext/curl/curl_file_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 0d09bd2f3b0a155cef25ca343319ecf470424d71 */
* Stub hash: 18acd3602d7ae2e87772a53d17fdde6cf8954dde */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_CURLFile___construct, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
Expand Down Expand Up @@ -60,7 +60,7 @@ static zend_class_entry *register_class_CURLFile(void)

INIT_CLASS_ENTRY(ce, "CURLFile", class_CURLFile_methods);
class_entry = zend_register_internal_class_ex(&ce, NULL);
class_entry->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
class_entry->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE|ZEND_ACC_SUBCLASS_SERIALIZABLE;

zval property_name_default_value;
ZVAL_EMPTY_STRING(&property_name_default_value);
Expand Down
15 changes: 12 additions & 3 deletions ext/dom/php_dom.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public function after(...$nodes): void;
public function replaceWith(...$nodes): void;
}

/** @not-serializable */
/**
* @not-serializable
* @subclass-serializable
*/
class DOMNode
{
/** @readonly */
Expand Down Expand Up @@ -156,7 +159,10 @@ public function removeChild(DOMNode $child) {}
public function replaceChild(DOMNode $node, DOMNode $child) {}
}

/** @not-serializable */
/**
* @not-serializable
* @subclass-serializable
*/
class DOMNameSpaceNode
{
/** @readonly */
Expand Down Expand Up @@ -666,7 +672,10 @@ public function __construct(string $name, string $value = "") {}
}

#ifdef LIBXML_XPATH_ENABLED
/** @not-serializable */
/**
* @not-serializable
* @subclass-serializable
*/
class DOMXPath
{
/** @readonly */
Expand Down
8 changes: 4 additions & 4 deletions ext/dom/php_dom_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 74698bea9c5e0635cf91345e8512b9677489510c */
* Stub hash: 683450c0a374ccefad061e9a9a94e750259f7ce5 */

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_dom_import_simplexml, 0, 1, DOMElement, 0)
ZEND_ARG_TYPE_INFO(0, node, IS_OBJECT, 0)
Expand Down Expand Up @@ -989,7 +989,7 @@ static zend_class_entry *register_class_DOMNode(void)

INIT_CLASS_ENTRY(ce, "DOMNode", class_DOMNode_methods);
class_entry = zend_register_internal_class_ex(&ce, NULL);
class_entry->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
class_entry->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE|ZEND_ACC_SUBCLASS_SERIALIZABLE;

zval property_nodeName_default_value;
ZVAL_UNDEF(&property_nodeName_default_value);
Expand Down Expand Up @@ -1104,7 +1104,7 @@ static zend_class_entry *register_class_DOMNameSpaceNode(void)

INIT_CLASS_ENTRY(ce, "DOMNameSpaceNode", class_DOMNameSpaceNode_methods);
class_entry = zend_register_internal_class_ex(&ce, NULL);
class_entry->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
class_entry->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE|ZEND_ACC_SUBCLASS_SERIALIZABLE;

zval property_nodeName_default_value;
ZVAL_UNDEF(&property_nodeName_default_value);
Expand Down Expand Up @@ -1656,7 +1656,7 @@ static zend_class_entry *register_class_DOMXPath(void)

INIT_CLASS_ENTRY(ce, "DOMXPath", class_DOMXPath_methods);
class_entry = zend_register_internal_class_ex(&ce, NULL);
class_entry->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
class_entry->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE|ZEND_ACC_SUBCLASS_SERIALIZABLE;

zend_string *property_document_class_DOMDocument = zend_string_init("DOMDocument", sizeof("DOMDocument")-1, 1);
zval property_document_default_value;
Expand Down
5 changes: 4 additions & 1 deletion ext/intl/formatter/formatter.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

/** @generate-class-entries */

/** @not-serializable */
/**
* @not-serializable
* @subclass-serializable
*/
class NumberFormatter
{
public function __construct(string $locale, int $style, ?string $pattern = null) {}
Expand Down
4 changes: 2 additions & 2 deletions ext/intl/formatter/formatter_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: f76ad76b08b7ca47883659fabfcc0882a2820c43 */
* Stub hash: 156855567aa4910e6954afca06158852d006c2d6 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_NumberFormatter___construct, 0, 0, 2)
ZEND_ARG_TYPE_INFO(0, locale, IS_STRING, 0)
Expand Down Expand Up @@ -126,7 +126,7 @@ static zend_class_entry *register_class_NumberFormatter(void)

INIT_CLASS_ENTRY(ce, "NumberFormatter", class_NumberFormatter_methods);
class_entry = zend_register_internal_class_ex(&ce, NULL);
class_entry->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
class_entry->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE|ZEND_ACC_SUBCLASS_SERIALIZABLE;

return class_entry;
}
5 changes: 4 additions & 1 deletion ext/simplexml/simplexml.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ function simplexml_load_string(string $data, ?string $class_name = SimpleXMLElem

function simplexml_import_dom(SimpleXMLElement|DOMNode $node, ?string $class_name = SimpleXMLElement::class): ?SimpleXMLElement {}

/** @not-serializable */
/**
* @not-serializable
* @subclass-serializable
*/
class SimpleXMLElement implements Stringable, Countable, RecursiveIterator
{
/** @tentative-return-type */
Expand Down
4 changes: 2 additions & 2 deletions ext/simplexml/simplexml_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 06c88dc2fb5582a6d21c11aee6ac0a0538e70cbc */
* Stub hash: 373d138420453f0b7a7e6fdd711b869229e97c5a */

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_simplexml_load_file, 0, 1, SimpleXMLElement, MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
Expand Down Expand Up @@ -170,7 +170,7 @@ static zend_class_entry *register_class_SimpleXMLElement(zend_class_entry *class

INIT_CLASS_ENTRY(ce, "SimpleXMLElement", class_SimpleXMLElement_methods);
class_entry = zend_register_internal_class_ex(&ce, NULL);
class_entry->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
class_entry->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE|ZEND_ACC_SUBCLASS_SERIALIZABLE;
zend_class_implements(class_entry, 3, class_entry_Stringable, class_entry_Countable, class_entry_RecursiveIterator);

return class_entry;
Expand Down

0 comments on commit 59d680c

Please sign in to comment.