reused hash tables in large data structures in PHP7

php.internals

Adam Baratz

10 years ago
Hi, I've encountered a hard-to-consistently-reproduce issue with HashTable zvals. I have code that will generate big nested \stdClass structures for JSON encoding. It does so using classes that have methods that generate those fields. For example, you could have a class like this: class Block { public function css_classes() { return ['x', 'y', 'z']; } } Other code would translate it into a value that would var_dump() as this: object(stdClass)#1 (1) { ["css_classes"]=> array(3) { [0]=> string(1) "x" [1]=> string(1) "y" [2]=> string(1) "z" } } The template data could include multiple instances of Block. As in, a \stdClass could be generated with multiple copies of that array content. That data could get sent to json_encode(), which uses ZEND_HASH_APPLY_PROTECTION/etc. to avoid recursion. I've seen situations where that array of strings triggers that recursion check. But it's not always, and a php-fpm restart can make it go away. Can anyone think of why this might happen? Thanks, Adam

Laruence

10 years ago
Hey: On Tue, May 17, 2016 at 6:00 AM, Adam Baratz <adam.baratz@gmail.com> wrote:
> Hi, > > I've encountered a hard-to-consistently-reproduce issue with HashTable > zvals. I have code that will generate big nested \stdClass structures for > JSON encoding. It does so using classes that have methods that generate > those fields. For example, you could have a class like this: > > class Block { > public function css_classes() { > return ['x', 'y', 'z']; > } > } > > Other code would translate it into a value that would var_dump() as this: > > object(stdClass)#1 (1) { > ["css_classes"]=> > array(3) { > [0]=> > string(1) "x" > [1]=> > string(1) "y" > [2]=> > string(1) "z" > } > } > > The template data could include multiple instances of Block. As in, a > \stdClass could be generated with multiple copies of that array content. > That data could get sent to json_encode(), which > uses ZEND_HASH_APPLY_PROTECTION/etc. to avoid recursion. I've seen > situations where that array of strings triggers that recursion check. But >
the array might be stored in shared memory(immutable array), in this case, you should not edit the apply count , you should check it, like what json does: https://github.com/php/php-src/blob/master/ext/json/json_encoder.c#L156 thanks it's not always, and a php-fpm restart can make it go away.
> > Can anyone think of why this might happen? > > Thanks, > Adam >
-- Xinchen Hui @Laruence http://www.laruence.com/