3v4l.org

run code in 300+ PHP versions simultaneously
<?php ini_set('error_reporting', '-1'); ini_set('display_errors', '1'); class SerializableClass implements \Serializable { private $foo = 'bar'; public function serialize() { return serialize([$this->foo]); } public function unserialize($serialized) { list($this->foo) = unserialize($serialized); } } class SleepableClass { private $foo = 'bar'; public function __sleep() { return ['foo']; } } function safelyUnserialize($file) { $e = null; $meta = false; // $content = file_get_contents($file); $content = $file; $signalingException = new \UnexpectedValueException(); $prevUnserializeHandler = ini_set('unserialize_callback_func', ''); $prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = []) use (&$prevErrorHandler, $signalingException) { if (__FILE__ === $file) { throw $signalingException; } return $prevErrorHandler ? $prevErrorHandler($type, $msg, $file, $line, $context) : false; }); try { $meta = unserialize($content); } catch (\Throwable $e) { if ($e !== $signalingException) { throw $e; } } finally { restore_error_handler(); ini_set('unserialize_callback_func', $prevUnserializeHandler); } return $meta; } echo "\n---------- [ Serializable ] ----------\n\n"; $serializableObject = new SerializableClass(); $serializableValidPayload = serialize($serializableObject); $serializableInvalidPayload = str_replace('"SerializableClass"', '"SerializableClazz"', $serializableValidPayload); echo "\n----- Invalid payload:\n\n"; var_dump($serializableInvalidPayload); echo "\n----- unserialize()\n\n"; var_dump(unserialize($serializableInvalidPayload)); echo "\n----- safelyUnserialize()\n\n"; var_dump(safelyUnserialize($serializableInvalidPayload)); echo "\n---------- [ Sleep / Wakeup ] ----------\n\n"; $sleepableObject = new SleepableClass(); $sleepableValidPayload = serialize($sleepableObject); $sleepableInvalidPayload = str_replace('"SleepableClass"', '"SleepableClazz"', $sleepableValidPayload); echo "\n----- Invalid payload:\n\n"; var_dump($sleepableInvalidPayload); echo "\n----- unserialize()\n\n"; var_dump(unserialize($sleepableInvalidPayload)); echo "\n----- safelyUnserialize()\n\n"; var_dump(safelyUnserialize($sleepableInvalidPayload));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0Chn9
function name:  (null)
number of ops:  74
compiled vars:  !0 = $serializableObject, !1 = $serializableValidPayload, !2 = $serializableInvalidPayload, !3 = $sleepableObject, !4 = $sleepableValidPayload, !5 = $sleepableInvalidPayload
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                               'ini_set'
          1        SEND_VAL                                                 'error_reporting'
          2        SEND_VAL                                                 '-1'
          3        DO_ICALL                                                 
    4     4        INIT_FCALL                                               'ini_set'
          5        SEND_VAL                                                 'display_errors'
          6        SEND_VAL                                                 '1'
          7        DO_ICALL                                                 
    6     8        DECLARE_CLASS                                            'serializableclass'
   61     9        ECHO                                                     '%0A----------+%5B+Serializable+%5D+----------%0A%0A'
   63    10        NEW                                              $8      'SerializableClass'
         11        DO_FCALL                                      0          
         12        ASSIGN                                                   !0, $8
   64    13        INIT_FCALL                                               'serialize'
         14        SEND_VAR                                                 !0
         15        DO_ICALL                                         $11     
         16        ASSIGN                                                   !1, $11
   65    17        INIT_FCALL                                               'str_replace'
         18        SEND_VAL                                                 '%22SerializableClass%22'
         19        SEND_VAL                                                 '%22SerializableClazz%22'
         20        SEND_VAR                                                 !1
         21        DO_ICALL                                         $13     
         22        ASSIGN                                                   !2, $13
   67    23        ECHO                                                     '%0A-----+Invalid+payload%3A%0A%0A'
   69    24        INIT_FCALL                                               'var_dump'
         25        SEND_VAR                                                 !2
         26        DO_ICALL                                                 
   71    27        ECHO                                                     '%0A-----+unserialize%28%29%0A%0A'
   73    28        INIT_FCALL                                               'var_dump'
         29        INIT_FCALL                                               'unserialize'
         30        SEND_VAR                                                 !2
         31        DO_ICALL                                         $16     
         32        SEND_VAR                                                 $16
         33        DO_ICALL                                                 
   75    34        ECHO                                                     '%0A-----+safelyUnserialize%28%29%0A%0A'
   77    35        INIT_FCALL                                               'var_dump'
         36        INIT_FCALL                                               'safelyunserialize'
         37        SEND_VAR                                                 !2
         38        DO_FCALL                                      0  $18     
         39        SEND_VAR                                                 $18
         40        DO_ICALL                                                 
   79    41        ECHO                                                     '%0A----------+%5B+Sleep+%2F+Wakeup+%5D+----------%0A%0A'
   81    42        NEW                                              $20     'SleepableClass'
         43        DO_FCALL                                      0          
         44        ASSIGN                                                   !3, $20
   82    45        INIT_FCALL                                               'serialize'
         46        SEND_VAR                                                 !3
         47        DO_ICALL                                         $23     
         48        ASSIGN                                                   !4, $23
   83    49        INIT_FCALL                                               'str_replace'
         50        SEND_VAL                                                 '%22SleepableClass%22'
         51        SEND_VAL                                                 '%22SleepableClazz%22'
         52        SEND_VAR                                                 !4
         53        DO_ICALL                                         $25     
         54        ASSIGN                                                   !5, $25
   85    55        ECHO                                                     '%0A-----+Invalid+payload%3A%0A%0A'
   87    56        INIT_FCALL                                               'var_dump'
         57        SEND_VAR                                                 !5
         58        DO_ICALL                                                 
   89    59        ECHO                                                     '%0A-----+unserialize%28%29%0A%0A'
   91    60        INIT_FCALL                                               'var_dump'
         61        INIT_FCALL                                               'unserialize'
         62        SEND_VAR                                                 !5
         63        DO_ICALL                                         $28     
         64        SEND_VAR                                                 $28
         65        DO_ICALL                                                 
   93    66        ECHO                                                     '%0A-----+safelyUnserialize%28%29%0A%0A'
   95    67        INIT_FCALL                                               'var_dump'
         68        INIT_FCALL                                               'safelyunserialize'
         69        SEND_VAR                                                 !5
         70        DO_FCALL                                      0  $30     
         71        SEND_VAR                                                 $30
         72        DO_ICALL                                                 
         73      > RETURN                                                   1

Function safelyunserialize:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
2 jumps found. (Code = 162) Position 1 = 30, Position 2 = 29
Branch analysis from position: 30
1 jumps found. (Code = 163) Position 1 = -2
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 24
Branch analysis from position: 24
2 jumps found. (Code = 107) Position 1 = 25, Position 2 = -2
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 28
Branch analysis from position: 27
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 28
filename:       /in/0Chn9
function name:  safelyUnserialize
number of ops:  39
compiled vars:  !0 = $file, !1 = $e, !2 = $meta, !3 = $content, !4 = $signalingException, !5 = $prevUnserializeHandler, !6 = $prevErrorHandler
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
   33     1        ASSIGN                                                   !1, null
   34     2        ASSIGN                                                   !2, <false>
   36     3        ASSIGN                                                   !3, !0
   37     4        NEW                                              $10     'UnexpectedValueException'
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !4, $10
   38     7        INIT_FCALL                                               'ini_set'
          8        SEND_VAL                                                 'unserialize_callback_func'
          9        SEND_VAL                                                 ''
         10        DO_ICALL                                         $13     
         11        ASSIGN                                                   !5, $13
   39    12        INIT_FCALL                                               'set_error_handler'
         13        DECLARE_LAMBDA_FUNCTION                          ~15     [0]
         14        BIND_LEXICAL                                             ~15, !6
         15        BIND_LEXICAL                                             ~15, !4
   45    16        SEND_VAL                                                 ~15
   39    17        DO_ICALL                                         $16     
         18        ASSIGN                                                   !6, $16
   48    19        INIT_FCALL                                               'unserialize'
         20        SEND_VAR                                                 !3
         21        DO_ICALL                                         $19     
         22        ASSIGN                                                   !2, $19
         23      > JMP                                                      ->28
   49    24  E > > CATCH                                       last         'Throwable'
   50    25    >   IS_NOT_IDENTICAL                                         !1, !4
         26      > JMPZ                                                     ~21, ->28
   51    27    > > THROW                                         0          !1
   53    28    > > FAST_CALL                                                ->30
         29    > > JMP                                                      ->37
   54    30    >   INIT_FCALL                                               'restore_error_handler'
         31        DO_ICALL                                                 
   55    32        INIT_FCALL                                               'ini_set'
         33        SEND_VAL                                                 'unserialize_callback_func'
         34        SEND_VAR                                                 !5
         35        DO_ICALL                                                 
         36      > FAST_RET                                                 
   58    37    > > RETURN                                                   !2
   59    38*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 10
Branch analysis from position: 9
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 20
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0Chn9
function name:  {closure}
number of ops:  23
compiled vars:  !0 = $type, !1 = $msg, !2 = $file, !3 = $line, !4 = $context, !5 = $prevErrorHandler, !6 = $signalingException
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
          4        RECV_INIT                                        !4      <array>
          5        BIND_STATIC                                              !5
          6        BIND_STATIC                                              !6
   40     7        IS_IDENTICAL                                             !2, '%2Fin%2F0Chn9'
          8      > JMPZ                                                     ~7, ->10
   41     9    > > THROW                                         0          !6
   44    10    > > JMPZ                                                     !5, ->20
         11    >   INIT_DYNAMIC_CALL                                        !5
         12        SEND_VAR_EX                                              !0
         13        SEND_VAR_EX                                              !1
         14        SEND_VAR_EX                                              !2
         15        SEND_VAR_EX                                              !3
         16        SEND_VAR_EX                                              !4
         17        DO_FCALL                                      0  $8      
         18        QM_ASSIGN                                        ~9      $8
         19      > JMP                                                      ->21
         20    >   QM_ASSIGN                                        ~9      <false>
         21    > > RETURN                                                   ~9
   45    22*     > RETURN                                                   null

End of Dynamic Function 0

End of function safelyunserialize

Class SerializableClass:
Function serialize:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0Chn9
function name:  serialize
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   INIT_FCALL                                               'serialize'
          1        FETCH_OBJ_R                                      ~0      'foo'
          2        INIT_ARRAY                                       ~1      ~0
          3        SEND_VAL                                                 ~1
          4        DO_ICALL                                         $2      
          5      > RETURN                                                   $2
   13     6*     > RETURN                                                   null

End of function serialize

Function unserialize:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0Chn9
function name:  unserialize
number of ops:  9
compiled vars:  !0 = $serialized
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
   17     1        INIT_FCALL                                               'unserialize'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $1      
          4        FETCH_LIST_R                                     $2      $1, 0
          5        ASSIGN_OBJ                                               'foo'
          6        OP_DATA                                                  $2
          7        FREE                                                     $1
   18     8      > RETURN                                                   null

End of function unserialize

End of class SerializableClass.

Class SleepableClass:
Function __sleep:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0Chn9
function name:  __sleep
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E > > RETURN                                                   <array>
   28     1*     > RETURN                                                   null

End of function __sleep

End of class SleepableClass.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
308.56 ms | 1036 KiB | 22 Q