Skip to content

fix: a Map or a Set serializes and reads back, as a queued job or a cached value holds one - #158

Merged
heyJordanParker merged 2 commits into
masterfrom
fix/map-serialization
Oct 17, 2026
Merged

heyJordanParker merged 2 commits into
masterfrom
fix/map-serialization

Conversation

@heyJordanParker

@heyJordanParker heyJordanParker commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

A queued job, a cached value or a session that holds a PHP# Map or Set threw Exception: Serialization of 'Sharp\Map' is not allowed, alone or as a field of a struct or a class. A real Laravel app keeps these values through serialize(), so moving any code that keeps a Map in a job or a cache broke. Spec section 11 already assumed it works: "an object serialized as it is keeps its type arguments".

What a PHP# developer can now write

  • A Map or a Set serializes and reads back as itself, with its entries in the same order, alone, in a List, in a struct field or in a class field, in the same process or a fresh one, with and without preloading. The PHP# reader gets a Map, never an array.
  • Keys come back equal. An enum key is the same case, a struct key and a key of a class that declares == are equal values, so a lookup with a fresh equal key finds them. A plain PHP object key, which has no ==, is the object read back beside it in the same payload.
  • A collection's type is its entries, so the copy is Map<string, int> exactly when the original is.
  • unserialize always allows a Map and a Set, as it allows an array, whatever allowed_classes says. Each key's own class still goes through the check. Sharp.Data.PhpSerializer takes PHP# class names in its allowed list, as "Queued.Crate".
  • Tampered data fails with an error. A field whose read-back Map, Set or List<Map<…>> holds a wrong key, value or element throws the TypeError a write from plain PHP throws. A Map's data that is not each key followed by its value, and a Set's that is no list, throw a ValueError that says so.
  • A second constructor call or a direct __unserialize on a Map or Set that holds entries throws, since it would write through every holder of a shared one.

Engine

  • ext/sharp/sharp.c: Sharp\Map and Sharp\Set drop ZEND_ACC_NOT_SERIALIZABLE and serialize through __serialize and __unserialize, as ArrayObject and SplObjectStorage do. Map::__serialize writes each key then its value, the list the constructor takes, and Set::__serialize is toList.
  • ext/standard/var_unserializer.re: each object with a PHP# field queues a check that var_destroy runs after the __unserialize of every Map and Set inside it. sharp_object_properties_accept replaces sharp_object_open_properties_accept and checks every PHP#-typed field there, the open fields of a generic class included. Sharp\Map and Sharp\Set pass the allowed_classes check.
  • sharp/mago: the checker's stub declares __serialize and __unserialize.
  • docs/spec.md section 11 states the rule.

Tests

table_serialize.phpt and table_serialize_preload.phpt run the same rows without and with preloading: a Map<string, int>, a Map keyed by an enum, by a struct, by a class that declares == and by a plain PHP object, a Map in a List, a struct field, a class field, a Set, an empty Map, the bytes serialize writes, a read-back in a fresh process, a bad key count, a Set under named keys, a Set where a Map is declared, a wrong value in a class field, a wrong key in a struct field, a wrong element in a Set field, a wrong value inside a List<Map<…>> field, a generic class field, PhpSerializer's default, PhpSerializer allowing a PHP# name, allowed_classes => false, and the two refusals to fill a table again. table_crossings.phpt loses the row that expected the refusal.

Not in this PR, after v1

  • var_export of a Map or a Set prints an empty __set_state(array()).
  • A NAN key reads back as a key no lookup finds.
  • The JSON key message names a PHP class, as "A JSON object key is text, and Probe\Point is not".
  • A type error mixes PHP and PHP# names, as "Cannot assign Sharp.Set to property Queued\Shipment::$labels".
  • type_arguments_out_of_memory.phpt fails under USE_ZEND_ALLOC=0, which enforces no memory_limit.

…ached value holds one

Problem

Laravel's queue, cache and sessions run serialize() on the value they keep.
A value that holds a PHP# Map or Set, itself or in a field of a struct or a
class, threw "Serialization of 'Sharp\Map' is not allowed", because the
engine's table class carried ZEND_ACC_NOT_SERIALIZABLE. Spec section 11
already assumed the opposite: "an object serialized as it is keeps its type
arguments".

Solution

Sharp\Map and Sharp\Set serialize through the engine's own hooks, as
ArrayObject and SplObjectStorage do. Map::__serialize writes each key then
its value, the list the constructor takes, and Set::__serialize is toList.
Each __unserialize fills the table through the code its constructor runs,
so a later key replaces an earlier one, and an odd or non-list Map list
throws the constructor's ValueError. unserialize runs __unserialize once
the whole payload is read, so a struct or class key is complete when the
table hashes it.

A Map carries no type arguments of its own, so the copy is a
Map<string, int> exactly when its entries are. An enum key comes back as
the same case, a struct key or a key of a class that declares == as an
equal value, and a plain PHP object key as the object read back beside it.

table_serialize.phpt runs each row with and without preloading: a
Map<string, int>, a Map keyed by an enum, by a struct and by a class, a Map
in a List, a struct field, a class field, a Set, an empty Map, the bytes
serialize writes, a read-back in a fresh process, a bad key count and a Set
where a Map is declared.

php-sharp/
├── docs/spec.md                      section 11: serialize keeps a collection
├── ext/sharp/sharp.c                 __serialize and __unserialize on Sharp\Map and Sharp\Set
└── Zend/tests/sharp/
    ├── Queued/                       new: Speed, Parcel, Shipment, Ledger, Courier, Payloads
    ├── table_serialize.inc           new: the rows
    ├── table_serialize.phpt          new: the rows without preloading
    ├── table_serialize_preload.inc   new: the preload script
    ├── table_serialize_preload.phpt  new: the rows under preloading
    └── table_crossings.phpt          the row that expected the refusal goes
…holds them, and unserialize always allows them

Problem

unserialize checks each PHP# field when it reads it, but fills a Map or a
Set only once the whole payload is read. So a tampered payload whose
Map<string, int> field held a string value or an int key, whose Set<Speed>
field held an int, or whose List<Map<string, int>> field held a wrong
value, was accepted, where the same tamper in a List<int> field throws.
The check of a generic class's open field ran at the same point, and
passed the same way.

unserialize with allowed_classes, which Sharp.Data.PhpSerializer passes as
[] by default, gave a Map back as __PHP_Incomplete_Class, and PhpSerializer
took only PHP's names for the classes it allows.

A second constructor call or a direct __unserialize call on a Map or a Set
that held entries wrote through every holder of a shared one.

Solution

Each object with a PHP# field queues a check when its last property is
read. var_destroy runs it after the __unserialize of each Map and Set the
object holds, which their own data queued first, so every entry is in
place. sharp_object_properties_accept checks each PHP#-typed field there,
the open fields of a generic class included, and a wrong entry throws the
"Cannot assign" TypeError a write from plain PHP throws.

unserialize allows Sharp\Map and Sharp\Set whatever allowed_classes says,
as it allows an array, and checks each key's own class as before.
PhpSerializer turns each PHP# name it is given, as `Queued.Crate`, into the
class PHP names.

A serialized Map that is not each key followed by its value throws "A
serialized Map must hold each key followed by its value", and a Set's data
that is no list throws "A serialized Set must hold a list of its elements".
Filling a Map or a Set that holds entries throws the Error offsetSet's
refusal words.

The checker's stub declares __serialize and __unserialize on Sharp\Map and
Sharp\Set.

php-sharp/
├── docs/
│   ├── spec.md                            section 11: whatever classes unserialize allows, and a wrong entry throws
│   └── architecture/slices/11-generics.md the delayed check
├── ext/
│   ├── sharp/
│   │   ├── php_sharp.h                    sharp_object_properties_accept replaces sharp_object_open_properties_accept
│   │   └── sharp.c                        the messages and the refusal to fill a table again
│   └── standard/var_unserializer.re       VAR_SHARP_CHECK_FLAG, and Map and Set always allowed
├── sharp/
│   ├── composer/library/Sharp/Data/PhpSerializer.sharp  PHP# class names in allowedClasses
│   └── mago/crates/prelude/assets/extensions/sharp.php  __serialize and __unserialize
└── Zend/tests/sharp/
    ├── Queued/                            new: Crate, Box; Payloads reads through PhpSerializer
    ├── table_serialize.inc                rows for each tamper, the allowed classes and the refusals
    ├── table_serialize.phpt
    ├── table_serialize_preload.inc
    └── table_serialize_preload.phpt
@heyJordanParker
heyJordanParker merged commit 10cf7e3 into master Oct 17, 2026
24 checks passed
@heyJordanParker
heyJordanParker deleted the fix/map-serialization branch November 3, 2026 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant