forked from ircmaxell/php-compiler
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVM.php
More file actions
615 lines (560 loc) · 24.2 KB
/
Copy pathVM.php
File metadata and controls
615 lines (560 loc) · 24.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<?php
/*
* This file is part of PHP-Compiler, a PHP CFG Compiler for PHP code
*
* @copyright 2015 Anthony Ferrara. All rights reserved
* @license MIT See LICENSE at the root of the project for more info
*/
namespace PHPCompiler;
require_once __DIR__.'/OpCodeNames.php';
require_once __DIR__.'/VM/OutgoingCallTempRelease.php';
require_once __DIR__.'/VM/Concern/ObjectPropertyIssetEmptyUnset.php';
require_once __DIR__.'/VM/Concern/ObjectPropertyCollectAndSerialize.php';
require_once __DIR__.'/VM/Concern/ObjectPropertyHooks.php';
require_once __DIR__.'/VM/Concern/ObjectPropertyReadonlyAndVisibility.php';
require_once __DIR__.'/VM/Concern/ClassTraitComposition.php';
require_once __DIR__.'/VM/Concern/ClassInheritDefineAndConstDeclare.php';
require_once __DIR__.'/VM/Concern/ObjectPropertyMagicAndClone.php';
require_once __DIR__.'/VM/Concern/PropertyFetchDestAndHookedDimWrite.php';
require_once __DIR__.'/VM/Concern/TypedIntRecursiveAndCountedLoopFastPath.php';
require_once __DIR__.'/VM/Concern/ExecuteIncDecAndScopeOperandRead.php';
require_once __DIR__.'/VM/Concern/TryCatchFinallyAndUncaughtDispatch.php';
require_once __DIR__.'/VM/Concern/BuiltinHostExceptionDispatch.php';
require_once __DIR__.'/VM/Concern/UserInvokeArrayAccessAndClosureCall.php';
require_once __DIR__.'/VM/Concern/FrameObjectRefAndDeadTempRelease.php';
require_once __DIR__.'/VM/Concern/GeneratorForeachAndYieldFrom.php';
require_once __DIR__.'/VM/Concern/FiberStartResumeAndThrow.php';
require_once __DIR__.'/VM/Concern/FrameActivationThisAndIncludeScope.php';
require_once __DIR__.'/VM/Concern/DeprecationNoticeEmit.php';
require_once __DIR__.'/VM/Concern/ReturnTypeEnforce.php';
require_once __DIR__.'/VM/Concern/VirtualPropertyHookEnforce.php';
require_once __DIR__.'/VM/Concern/MethodCallAndStaticCallableInit.php';
require_once __DIR__.'/VM/Concern/InheritanceFinalVarianceAndConstFetch.php';
require_once __DIR__.'/VM/Concern/ClassConstAndPropertyDefaultMaterialize.php';
require_once __DIR__.'/VM/Concern/OutgoingCallArgResolve.php';
require_once __DIR__.'/VM/Concern/InternalHandlerExecuteAndAssignCopy.php';
require_once __DIR__.'/VM/Concern/ClosureBindAndFunctionStatic.php';
require_once __DIR__.'/VM/Concern/ClassScopeAndStaticPropertyResolve.php';
require_once __DIR__.'/VM/Concern/IncludePathAndClassPseudoConst.php';
require_once __DIR__.'/VM/Concern/IteratorToArrayConvert.php';
require_once __DIR__.'/VM/Concern/PropertyHookFrameAndStaticLink.php';
require_once __DIR__.'/VM/Concern/ConstructMarkAndPendingOutboundCall.php';
require_once __DIR__.'/VM/Concern/ObjectPropertyFetchDispatch.php';
require_once __DIR__.'/VM/Concern/ArrayDimFetchDispatch.php';
require_once __DIR__.'/VM/Concern/ArrayInitSpreadDispatch.php';
require_once __DIR__.'/VM/Concern/StaticPropertyFetchDispatch.php';
require_once __DIR__.'/VM/Concern/UnsetDispatch.php';
require_once __DIR__.'/VM/Concern/AssignDispatch.php';
require_once __DIR__.'/VM/Concern/FuncCallExecDispatch.php';
require_once __DIR__.'/VM/Concern/ArgRecvDispatch.php';
require_once __DIR__.'/VM/Concern/ScalarCastDispatch.php';
require_once __DIR__.'/VM/Concern/ScalarCompareDispatch.php';
require_once __DIR__.'/VM/Concern/ScalarArithBitwiseUnaryDispatch.php';
require_once __DIR__.'/VM/Concern/ScalarCastCompareArithConcatDispatch.php';
require_once __DIR__.'/VM/Concern/ClassConstFetchDispatch.php';
require_once __DIR__.'/VM/Concern/IssetDispatch.php';
require_once __DIR__.'/VM/Concern/IncludeDispatch.php';
require_once __DIR__.'/VM/Concern/FuncCallInitDispatch.php';
require_once __DIR__.'/VM/Concern/NewDispatch.php';
require_once __DIR__.'/VM/Concern/MethodCallInitDispatch.php';
require_once __DIR__.'/VM/Concern/EchoPrintEvalDispatch.php';
require_once __DIR__.'/VM/Concern/ForeachIterDispatch.php';
require_once __DIR__.'/VM/Concern/CoalesceNullsafeSilenceExitDispatch.php';
require_once __DIR__.'/VM/Concern/JumpCaseDispatch.php';
require_once __DIR__.'/VM/Concern/ConstFetchStaticCallInstanceofDispatch.php';
require_once __DIR__.'/VM/Concern/DeclareClassLikeDispatch.php';
require_once __DIR__.'/VM/Concern/ArgSendDispatch.php';
require_once __DIR__.'/VM/Concern/VarFetchGlobalAndFunctionStaticDispatch.php';
require_once __DIR__.'/VM/Concern/ListUnpackAndSpreadAssignDispatch.php';
require_once __DIR__.'/VM/Concern/FromCallableAndClosureDispatch.php';
require_once __DIR__.'/VM/Concern/EmptyAndBooleanNotDispatch.php';
require_once __DIR__.'/VM/Concern/YieldAndYieldFromDispatch.php';
require_once __DIR__.'/VM/Concern/TryCatchThrowDispatch.php';
require_once __DIR__.'/VM/Concern/CloneDispatch.php';
require_once __DIR__.'/VM/Concern/FuncDefAndGlobalConstDispatch.php';
require_once __DIR__.'/VM/Concern/ScriptMagicAndTickDispatch.php';
require_once __DIR__.'/VM/Concern/ReturnDispatch.php';
require_once __DIR__.'/VM/Concern/ClassInstanceAndStaticCallSupport.php';
require_once __DIR__.'/VM/Concern/RunFramesInner.php';
use PHPCompiler\BuiltinByRefParams;
use PHPCompiler\Compiler\AttributeNames;
use PHPCompiler\Compiler\NoDiscardMetadata;
use PHPCompiler\Compiler\SourceLocation;
use PHPCompiler\Func;
use PHPCompiler\ext\standard\VmForwardStaticCall;
use PHPCompiler\ext\standard\VmString;
use PHPCompiler\VM\ForeachIterator;
use PHPCompiler\VM\Context;
use PHPCompiler\VM\CastSupport;
use PHPCompiler\VM\DnfCheck;
use PHPCompiler\VM\CallableCheck;
use PHPCompiler\VM\CycleCollector;
use PHPCompiler\VM\EnumCaseSupport;
use PHPCompiler\VM\FiberState;
use PHPCompiler\VM\GeneratorState;
use PHPCompiler\VM\HashTable;
use PHPCompiler\VM\IterableCheck;
use PHPCompiler\VM\NamedArgs;
use PHPCompiler\VM\ObjectEntry;
use PHPCompiler\VM\ObjectLifetime;
use PHPCompiler\VM\ReferencableCheck;
use PHPCompiler\VM\ReflectionPropertyHookSupport;
use PHPCompiler\VM\ScriptExit;
use PHPCompiler\VM\TypeCheck;
use PHPCompiler\VM\TraitCompositionConflictMessage;
use PHPCompiler\VM\TypedPropertyReadSignal;
use PHPCompiler\VM\VmIncDec;
use PHPCompiler\VM\VmIsset;
use PHPCompiler\VM\WeakRefRegistry;
use PHPCompiler\VM\Variable;
use PHPCompiler\Web\Superglobals;
class VM {
use OutgoingCallTempRelease;
use ObjectPropertyIssetEmptyUnset;
use ObjectPropertyCollectAndSerialize;
use ObjectPropertyHooks;
use ObjectPropertyReadonlyAndVisibility;
use ClassTraitComposition;
use ClassInheritDefineAndConstDeclare;
use ObjectPropertyMagicAndClone;
use PropertyFetchDestAndHookedDimWrite;
use TypedIntRecursiveAndCountedLoopFastPath;
use ExecuteIncDecAndScopeOperandRead;
use TryCatchFinallyAndUncaughtDispatch;
use BuiltinHostExceptionDispatch;
use UserInvokeArrayAccessAndClosureCall;
use FrameObjectRefAndDeadTempRelease;
use GeneratorForeachAndYieldFrom;
use FiberStartResumeAndThrow;
use FrameActivationThisAndIncludeScope;
use DeprecationNoticeEmit;
use ReturnTypeEnforce;
use VirtualPropertyHookEnforce;
use MethodCallAndStaticCallableInit;
use InheritanceFinalVarianceAndConstFetch;
use ClassConstAndPropertyDefaultMaterialize;
use OutgoingCallArgResolve;
use InternalHandlerExecuteAndAssignCopy;
use ClosureBindAndFunctionStatic;
use ClassScopeAndStaticPropertyResolve;
use IncludePathAndClassPseudoConst;
use IteratorToArrayConvert;
use PropertyHookFrameAndStaticLink;
use ConstructMarkAndPendingOutboundCall;
use ObjectPropertyFetchDispatch;
use ArrayDimFetchDispatch;
use ArrayInitSpreadDispatch;
use StaticPropertyFetchDispatch;
use UnsetDispatch;
use AssignDispatch;
use FuncCallExecDispatch;
use ArgRecvDispatch;
use ScalarCastDispatch;
use ScalarCompareDispatch;
use ScalarArithBitwiseUnaryDispatch;
use ScalarCastCompareArithConcatDispatch;
use ClassConstFetchDispatch;
use IssetDispatch;
use IncludeDispatch;
use FuncCallInitDispatch;
use NewDispatch;
use MethodCallInitDispatch;
use EchoPrintEvalDispatch;
use ForeachIterDispatch;
use CoalesceNullsafeSilenceExitDispatch;
use JumpCaseDispatch;
use ConstFetchStaticCallInstanceofDispatch;
use DeclareClassLikeDispatch;
use ArgSendDispatch;
use VarFetchGlobalAndFunctionStaticDispatch;
use ListUnpackAndSpreadAssignDispatch;
use FromCallableAndClosureDispatch;
use EmptyAndBooleanNotDispatch;
use YieldAndYieldFromDispatch;
use TryCatchThrowDispatch;
use CloneDispatch;
use FuncDefAndGlobalConstDispatch;
use ScriptMagicAndTickDispatch;
use ReturnDispatch;
use ClassInstanceAndStaticCallSupport;
use RunFramesInner;
const SUCCESS = 1;
const FAILURE = 2;
/** Sentinel from {@see ReturnDispatch}: caller must `goto nextframe`. */
const RETURN_NEXTFRAME = 5;
private static ?self $running = null;
/** Frame executing the current opcode (property hook ref read/write, #6426). */
private ?Frame $executingFrame = null;
/** Active builtin handler while {@see executeInternalHandler} bridges a throw (#11677). */
private ?Frame $builtinHandlerFrameForTrace = null;
/** Reused ++/-- scratch slots — avoid per-iteration Variable alloc in hot loops (#15906, #36148). */
private ?Variable $incDecScratchWorking = null;
private ?Variable $incDecScratchBefore = null;
/** @internal Active VM during runFrames (#3429 typed property errors). */
public static function running(): ?self
{
return self::$running;
}
/** Builtin handler frame while {@see executeInternalHandler} runs (#16409). */
public function builtinHandlerFrame(): ?Frame
{
return $this->builtinHandlerFrameForTrace;
}
/**
* Named locals, dynamic locals, globals, and in-flight builtin args — not compiler temps (#14103).
*
* @param callable(Variable): void $visitVar
*/
public function visitNamedStrongRefRoots(callable $visitVar): void
{
if (null !== $this->executingFrame) {
$frame = $this->executingFrame;
if (null !== $frame->closureCall || null !== $frame->parent) {
self::visitFrameNamedStrongRefRoots($frame, $visitVar);
}
}
if (null !== $this->builtinHandlerFrameForTrace) {
foreach ($this->builtinHandlerFrameForTrace->calledArgs as $arg) {
$visitVar($arg);
}
}
foreach ($this->context->runStackFrames() as $frame) {
self::visitFrameNamedStrongRefRoots($frame, $visitVar);
}
$this->context->visitGlobalVariables($visitVar);
}
/** @param callable(Variable): void $visitVar */
private static function visitFrameNamedStrongRefRoots(Frame $frame, callable $visitVar): void
{
if (null !== $frame->block) {
foreach ($frame->block->eachNamedScopeSlot() as [, $slot]) {
if (isset($frame->scope[$slot])) {
$visitVar($frame->scope[$slot]);
}
}
}
foreach ($frame->dynamicLocals as $var) {
$visitVar($var);
}
}
/**
* Visit slots that may strongly retain weak-ref targets — active opcode frame,
* in-flight builtin args, suspended callers, globals (#13923).
*
* @param callable(Variable): void $visitVar
*/
public function visitStrongRefRoots(callable $visitVar, bool $includeBuiltinHandler = true): void
{
if (null !== $this->executingFrame) {
$frame = $this->executingFrame;
// Script-root scope mirrors globals — visit globals only (#13474, #13923).
if (null !== $frame->closureCall || null !== $frame->parent) {
CycleCollector::markFrameRoots($frame, $visitVar);
}
}
if ($includeBuiltinHandler && null !== $this->builtinHandlerFrameForTrace) {
CycleCollector::markFrameRoots($this->builtinHandlerFrameForTrace, $visitVar, false);
}
foreach ($this->context->runStackFrames() as $frame) {
CycleCollector::markFrameRoots($frame, $visitVar);
}
$this->context->visitGlobalVariables($visitVar);
}
/** Generator body suspended at `yield` (issue #167). */
const GENERATOR_YIELD = 3;
/** Fiber callback suspended at Fiber::suspend() (issue #3130). */
const FIBER_SUSPEND = 4;
public Context $context;
public function __construct(Context $context) {
$this->context = $context;
}
public function run(Block $block): int {
ObjectLifetime::setVm($this);
CycleCollector::captureRequestBaseline();
try {
if (!is_null($block->handler)) {
$frame = $block->getFrame($this->context);
$this->seedScriptPath($frame);
$block->handler->execute($frame);
return self::SUCCESS;
}
$frame = $block->getFrame($this->context);
$this->seedScriptPath($frame);
$frame->vmContext = $this->context;
$this->context->executionLimits->begin();
$this->context->push($frame);
$result = $this->runFrames();
if ('' !== $frame->scriptPath) {
$this->context->scriptStack->pop();
}
return $result;
} finally {
ObjectLifetime::runShutdownDestructors();
ObjectLifetime::clearVm();
}
}
/**
* Execute dynamically compiled eval() code in the caller variable scope (#3358).
*
* Outer try/catch handlers active before eval must not run inside this nested runFrames —
* that resumes the try body after catch (#25816; same shape as #24138 / #14104).
*/
public function executeEvalBlock(Block $block, Frame $caller): Variable
{
$out = new Variable();
$child = $block->getFrame($this->context, $caller);
$child->ephemeral = true;
// ZEND_INCLUDE_OR_EVAL copies EX(This) into the eval unit (#31902).
$this->inheritIncludeThis($child, $caller);
// Scope comes from getFrame($caller); parent must stay null so nested runFrames exits.
$child->parent = null;
$child->returnVar = $out;
// Zend __FILE__/__DIR__: enclosing script path + call site (#25809, zend_eval_string).
[$evalFile] = VM\ExceptionSupport::evalFatalSite($caller, 1);
$child->scriptPath = $evalFile;
// zend_eval_string copies called_scope AND func->scope (self ≠ static on subclass) (#31912).
$this->inheritEvalClassScope($child, $caller);
// ZEND_INCLUDE_OR_EVAL also copies EX(This); isolation must not drop object context (#4410).
$this->inheritIncludeThis($child, $caller);
$this->context->scriptStack->push($child->scriptPath);
$prevDeferDepth = $this->context->deferCatchBelowTryHandlerDepth;
$this->context->deferCatchBelowTryHandlerDepth = \count($this->context->activeTryHandlerFrames);
// Isolate nested runFrames so eval completion does not continue the caller (#31912).
// Isolated stack — nested eval return must not pop the outer method/script
// frame that is executing TYPE_EVAL (#31902; same shape as coercion invoke).
$savedStack = $this->context->swapRunStack(null);
try {
$this->context->push($child);
$result = $this->runFrames();
if (self::SUCCESS !== $result) {
throw new \LogicException('eval() execution failed in this compiler build');
}
} catch (VM\BuiltinCallbackCatchRedirect $redirect) {
$this->context->swapRunStack($savedStack);
$savedStack = null;
throw $redirect;
} finally {
if (null !== $savedStack) {
$this->context->swapRunStack($savedStack);
}
$this->context->deferCatchBelowTryHandlerDepth = $prevDeferDepth;
// Ephemeral eval finish already pops this path; pop only if still on top.
if ($this->context->scriptStack->current() === $evalFile
|| $this->context->scriptStack->current() === VM\ScriptStack::normalize($evalFile)
) {
$this->context->scriptStack->pop();
}
}
return $out->resolveIndirect();
}
/**
* Compile and execute a PHP file once (require_once semantics for manifest includes / PSR-4).
*/
public function executeCompileUnit(string $path): void
{
$resolved = VM\ScriptStack::normalize($path);
if ('' === $resolved || !is_file($resolved)) {
return;
}
if ($this->context->isCompileUnitLoaded($resolved)) {
return;
}
$this->context->recordIncludedFile($resolved);
$savedStack = $this->context->swapRunStack(null);
try {
$this->context->scriptStack->push($resolved);
$block = $this->context->runtime->parseAndCompileFile($resolved);
if (null === $block) {
return;
}
$this->run($block);
} finally {
$this->context->swapRunStack($savedStack);
}
}
private function seedScriptPath(Frame $frame): void
{
if ('' !== $frame->scriptPath) {
$this->context->scriptStack->push($frame->scriptPath);
$this->context->recordIncludedFile($frame->scriptPath);
if ('-' !== $frame->scriptPath) {
VmString::realpath($frame->scriptPath);
}
}
}
private function maybeRunTick(): void
{
if (VM\TickQueue::isRunning() || $this->context->tickInterval <= 0) {
return;
}
--$this->context->tickCounter;
if ($this->context->tickCounter > 0) {
return;
}
$this->context->tickCounter = $this->context->tickInterval;
VM\TickQueue::run($this->context);
}
private function runFrames(): int
{
$previous = self::$running;
self::$running = $this;
try {
return $this->runFramesInner();
} finally {
self::$running = $previous;
}
}
/**
* Build a catchable VM Error object for engine-thrown failures (#3429).
*
* When an opcode is executing, stamp user file/line like zend_throw_exception
* so caught Errors (typed-property reads, by-ref fetch, …) match Zend getFile()/getLine()
* (#31859, zend_exceptions.c / zend_object_handlers.c).
*/
public function makeEngineError(string $message, string $className = 'Error'): Variable
{
$lc = strtolower($className);
if (!isset($this->context->classes[$lc])) {
throw new \LogicException("Engine error class {$className} is not registered");
}
$obj = new ObjectEntry($this->context->classes[$lc]);
$obj->constructed = true;
$obj->getProperty('message')->string($message);
// php-src zend_throw_exception / zend_exception_get_props: default code is 0.
$obj->getProperty(VM\ExceptionSupport::PROP_CODE)->int(0);
if (null !== $this->executingFrame) {
[$file, $line] = VM\ExceptionSupport::userFatalSite($this->executingFrame);
VM\ExceptionSupport::stampThrowableSite($obj, $file, $line);
}
$thrown = new Variable();
$thrown->object($obj);
return $thrown;
}
private function normalizeThrownVariable(Variable $thrown): Variable
{
if (VM\ExceptionSupport::isThrowableVariable($thrown, $this->context)) {
return $thrown;
}
return $this->makeEngineError(
VM\ExceptionSupport::throwNormalizeErrorMessage($thrown),
VM\ExceptionSupport::CLASS_ERROR
);
}
private function dispatchEngineThrow(Frame $frame, Variable $thrown): ?Frame
{
$thrown = $this->normalizeThrownVariable($thrown);
VM\ExceptionTrace::captureOnThrow($this->context, $frame, $thrown);
// Zend: throw in finally discards a pending return (#5331).
$inFinally = $this->frameIsInFinallyBody($frame);
if ($inFinally) {
$this->clearPendingReturnState();
}
$pendingBeforeThrow = null;
if (null !== $this->context->pendingException) {
$pendingBeforeThrow = new Variable();
$pendingBeforeThrow->copyFrom($this->context->pendingException);
}
$gen = $this->findGeneratorState($frame);
if (null !== $gen) {
$catchFrame = $this->findCatchFrameForGeneratorThrow($gen, $thrown);
VM\ExceptionTrace::captureGeneratorThrowSite($this->context, $frame, $thrown);
if (null !== $catchFrame) {
$catchFrame->generatorState = $gen;
$gen->frame = $catchFrame;
return $catchFrame;
}
$gen->frame = null;
$gen->markClosedWithoutReturn();
throw new VM\GeneratorUncaughtThrow($thrown, $frame);
}
// Zend/zend_fibers.c: uncaught throw inside a fiber transfers to the resume()/throw()
// caller — never jump into the caller's try/catch while still inside runFiberExecution
// (#19592; mirrors GeneratorUncaughtThrow above).
$fiber = $this->context->currentFiber;
if (null !== $fiber && $this->findFiberState($frame) === $fiber) {
$catchFrame = $this->findCatchFrameForFiberThrow($fiber, $thrown);
if (null !== $catchFrame) {
$catchFrame->fiberState = $fiber;
$fiber->frame = $catchFrame;
return $catchFrame;
}
throw new VM\FiberUncaughtThrow($thrown);
}
$catchFrame = $this->findCatchFrameForThrow($frame, $thrown);
if (null !== $catchFrame) {
if ($this->context->isolatedDestructorInvoke) {
throw new VM\DestructorThrowCatchSignal($catchFrame);
}
return $catchFrame;
}
// Zend: finally-over-try uncaught fatal cites pending try exception first (#5867, #6457, #7342).
if ($inFinally && null !== $pendingBeforeThrow) {
$this->raiseUncaughtExceptionWithNext($pendingBeforeThrow, $thrown);
return null;
}
$uncaught = $this->context->pendingException ?? $thrown;
$this->raiseUncaughtException($uncaught);
return null;
}
/**
* ++/-- on ArrayAccess when offsetGet returns by value — Notice, no offsetSet (#32015, zend_vm_def.h).
*/
protected function raise(string $message, Frame $frame): int
{
$where = '' !== $frame->scriptPath ? $frame->scriptPath : 'script';
throw new \LogicException($message.' in '.$where);
}
/**
* After a catch match, skip remaining TYPE_CATCH / CFG entry TYPE_JUMP on the handler
* block so merge fallthrough does not re-enter the try body (#2084).
*/
private function skipTryCatchHandlerTail(Frame $handler): void
{
while ($handler->pos < $handler->block->nOpCodes) {
$op = $handler->block->opCodes[$handler->pos];
if (OpCode::TYPE_CATCH === $op->type || OpCode::TYPE_FINALLY === $op->type) {
$handler->pos++;
continue;
}
if (OpCode::TYPE_JUMP === $op->type) {
$handler->pos++;
continue;
}
break;
}
}
private function catchTypesMatch(OpCode $op, Variable $thrown): bool
{
return VM\VmTryCatch::encodedTypesMatchOpcode($op, $thrown, $this->context);
}
/**
* unset($GLOBALS['name']) on the script $GLOBALS operand (#5868).
*/
private function isGlobalsSuperglobalUnset(Frame $frame, int $containerSlot, string $name): bool
{
if ('' === $name) {
return false;
}
$globalsSlot = $frame->block->slotIndexForVariableName('GLOBALS');
return null !== $globalsSlot && $globalsSlot === $containerSlot;
}
/**
* Reject unset($scalar[$key]) — Zend ZEND_UNSET_DIM on non-array/string (#4880, zend_execute.c).
*
* Route through {@see dispatchVmError} so getFile()/getLine() stamp the user unset site
* (#31883, re-#31859).
*
* @return Frame|null catch frame when try/catch (Error) handles the throw
*/
private function dispatchUnsetDimNonContainerError(Frame $frame, string $message): ?Frame
{
return $this->dispatchVmError($message, $frame);
}
protected function scopeSlot(Frame $frame, int $slot): Variable
{
if (!isset($frame->scope[$slot])) {
$frame->scope[$slot] = new Variable();
}
return $frame->scope[$slot];
}
}