packages/deriver/tests/Unit/Deriver/Evaluator/Execution/Call/AllocationTest.php
1<?php
2
3declare (strict_types=1);
4
5namespace Tests\Unit\Deriver\Evaluator\Execution\Call;
6
7use Deriver\Deriver;
8use Deriver\Deriver\Candidate\ResultRef;
9use Deriver\Deriver\Candidate\Snapshot;
10use Deriver\Deriver\Candidate\Statistics;
11use Deriver\Deriver\Contributions;
12use Deriver\Deriver\DeriverConfig;
13use Deriver\Deriver\Evaluator\Call\BoundInstruction;
14use Deriver\Deriver\Evaluator\Call\Evaluators;
15use Deriver\Deriver\Evaluator\Call\Extensions;
16use Deriver\Deriver\Evaluator\Call\Provider\DispatchDecision;
17use Deriver\Deriver\Evaluator\Call\Provider\DispatchRequest;
18use Deriver\Deriver\Evaluator\Call\Registrations;
19use Deriver\Deriver\Evaluator\Call\State\ClonePolicy;
20use Deriver\Deriver\Evaluator\Call\State\StateSlot;
21use Deriver\Deriver\Evaluator\Call\StateRegistry;
22use Deriver\Deriver\Evaluator\Domain\Lattice;
23use Deriver\Deriver\Evaluator\EvaluatorConfig;
24use Deriver\Deriver\Evaluator\Evidence\Frontier;
25use Deriver\Deriver\Evaluator\Execution\Call\Allocation;
26use Deriver\Deriver\Evaluator\Execution\Call\ArgumentBinding;
27use Deriver\Deriver\Evaluator\Execution\Call\ArgumentOrder;
28use Deriver\Deriver\Evaluator\Execution\Call\CallExecutor;
29use Deriver\Deriver\Evaluator\Execution\Call\CallResolution;
30use Deriver\Deriver\Evaluator\Execution\Call\Creation\Access;
31use Deriver\Deriver\Evaluator\Execution\Call\Member\Invocation;
32use Deriver\Deriver\Evaluator\Execution\Call\MethodInvocation;
33use Deriver\Deriver\Evaluator\Execution\Call\Native\Invocation as NativeInvocation;
34use Deriver\Deriver\Evaluator\Execution\Call\Native\Properties;
35use Deriver\Deriver\Evaluator\Execution\Call\Native\Signatures;
36use Deriver\Deriver\Evaluator\Execution\Call\ParameterBinding;
37use Deriver\Deriver\Evaluator\Execution\Call\PassedArgument;
38use Deriver\Deriver\Evaluator\Execution\Call\Preparation\Arguments;
39use Deriver\Deriver\Evaluator\Execution\Call\Preparation\Creation;
40use Deriver\Deriver\Evaluator\Execution\Call\Preparation\Methods;
41use Deriver\Deriver\Evaluator\Execution\Call\Preparation\Modes;
42use Deriver\Deriver\Evaluator\Execution\Call\Preparation\Resolution;
43use Deriver\Deriver\Evaluator\Execution\Call\Preparation\Target;
44use Deriver\Deriver\Evaluator\Execution\Call\Preparation\Transfer;
45use Deriver\Deriver\Evaluator\Execution\Call\ProviderDispatch;
46use Deriver\Deriver\Evaluator\Execution\Call\TypeBinding;
47use Deriver\Deriver\Evaluator\Execution\Call\TypeCheck;
48use Deriver\Deriver\Evaluator\Execution\Completion\Alternative;
49use Deriver\Deriver\Evaluator\Execution\Completion\Assessment;
50use Deriver\Deriver\Evaluator\Execution\Completion\Completion;
51use Deriver\Deriver\Evaluator\Execution\Completion\CompletionKind;
52use Deriver\Deriver\Evaluator\Execution\Completion\StorageSnapshot;
53use Deriver\Deriver\Evaluator\Execution\Constant\ClassNames;
54use Deriver\Deriver\Evaluator\Execution\Context;
55use Deriver\Deriver\Evaluator\Execution\Control\ExceptionChain;
56use Deriver\Deriver\Evaluator\Execution\Control\ExceptionMatch;
57use Deriver\Deriver\Evaluator\Execution\Control\Handler;
58use Deriver\Deriver\Evaluator\Execution\Control\ObservationLimit;
59use Deriver\Deriver\Evaluator\Execution\Control\StateJoin;
60use Deriver\Deriver\Evaluator\Execution\Control\Unwinding;
61use Deriver\Deriver\Evaluator\Execution\Demand\Cell;
62use Deriver\Deriver\Evaluator\Execution\Demand\Components;
63use Deriver\Deriver\Evaluator\Execution\Demand\Discovery;
64use Deriver\Deriver\Evaluator\Execution\Demand\Key;
65use Deriver\Deriver\Evaluator\Execution\Demand\Table;
66use Deriver\Deriver\Evaluator\Execution\Dependencies;
67use Deriver\Deriver\Evaluator\Execution\Evaluator;
68use Deriver\Deriver\Evaluator\Execution\EvaluatorConfig as ExecutionEvaluatorConfig;
69use Deriver\Deriver\Evaluator\Execution\Havoc;
70use Deriver\Deriver\Evaluator\Execution\InstructionTransfer;
71use Deriver\Deriver\Evaluator\Execution\Memory\Location;
72use Deriver\Deriver\Evaluator\Execution\Memory\Materialization;
73use Deriver\Deriver\Evaluator\Execution\Memory\Memory;
74use Deriver\Deriver\Evaluator\Execution\Memory\ReferenceConstraint;
75use Deriver\Deriver\Evaluator\Execution\Memory\Slots\SlotReference;
76use Deriver\Deriver\Evaluator\Execution\Memory\Slots\StateStorage;
77use Deriver\Deriver\Evaluator\Execution\Memory\StorageCapture;
78use Deriver\Deriver\Evaluator\Execution\ObservationCollector;
79use Deriver\Deriver\Evaluator\Execution\Offset\Address;
80use Deriver\Deriver\Evaluator\Execution\Offset\Path;
81use Deriver\Deriver\Evaluator\Execution\Offset\Reader;
82use Deriver\Deriver\Evaluator\Execution\Offset\Transfer as OffsetTransfer;
83use Deriver\Deriver\Evaluator\Execution\Operation\Conversions;
84use Deriver\Deriver\Evaluator\Execution\Operation\ScalarErrors;
85use Deriver\Deriver\Evaluator\Execution\State;
86use Deriver\Deriver\Evaluator\Execution\Summary\CompletionRecord;
87use Deriver\Deriver\Evaluator\Execution\Summary\Evaluation;
88use Deriver\Deriver\Evaluator\Execution\Summary\Invocation as SummaryInvocation;
89use Deriver\Deriver\Evaluator\Execution\Summary\Isolation;
90use Deriver\Deriver\Evaluator\Execution\Trace\Derivation;
91use Deriver\Deriver\Evaluator\Execution\Transfer\MemoryStep;
92use Deriver\Deriver\Evaluator\Execution\Transfer\ObjectAccess;
93use Deriver\Deriver\Evaluator\Execution\Transfer\PropertyAccessCheck;
94use Deriver\Deriver\Evaluator\Execution\Transfer\PropertyLookup;
95use Deriver\Deriver\Evaluator\Execution\Transfer\PropertyReference;
96use Deriver\Deriver\Evaluator\Execution\Transfer\PropertySlot;
97use Deriver\Deriver\Evaluator\Execution\Transfer\PropertyTransfer;
98use Deriver\Deriver\Evaluator\Execution\Transfer\PureStep;
99use Deriver\Deriver\Evaluator\Execution\Transfer\ReferenceAssignment;
100use Deriver\Deriver\Evaluator\Request\Budget;
101use Deriver\Deriver\Evaluator\Request\EntryPoint;
102use Deriver\Deriver\Evaluator\Request\QueryScope;
103use Deriver\Deriver\Evaluator\Request\ReturnQuery;
104use Deriver\Deriver\Evaluator\Scalar\Arithmetic;
105use Deriver\Deriver\Evaluator\Scalar\Arrays;
106use Deriver\Deriver\Evaluator\Scalar\Identity;
107use Deriver\Deriver\Evaluator\Scalar\Increment;
108use Deriver\Deriver\Evaluator\Scalar\Operations;
109use Deriver\Deriver\Evaluator\TargetProfile;
110use Deriver\Deriver\Evaluator\Term;
111use Deriver\Deriver\Evaluator\TermKind;
112use Deriver\Deriver\Execution\DerivationResult;
113use Deriver\Deriver\Execution\QueryExecution;
114use Deriver\Deriver\Execution\ResultAssessment;
115use Deriver\Deriver\Identity\QueryEncoding;
116use Deriver\Deriver\Identity\ValueGraph;
117use Deriver\Deriver\Lowerer\AggregateLowering;
118use Deriver\Deriver\Lowerer\AssignmentLowering;
119use Deriver\Deriver\Lowerer\CallLowering;
120use Deriver\Deriver\Lowerer\Control\DestructuringLowering;
121use Deriver\Deriver\Lowerer\Control\ExceptionLowering;
122use Deriver\Deriver\Lowerer\Declaration\CallableIdentity;
123use Deriver\Deriver\Lowerer\Declaration\CallableSource;
124use Deriver\Deriver\Lowerer\Declaration\ConstantSignatures;
125use Deriver\Deriver\Lowerer\Declaration\Index;
126use Deriver\Deriver\Lowerer\Declaration\Indexer;
127use Deriver\Deriver\Lowerer\Declaration\Metadata\ClassConstant;
128use Deriver\Deriver\Lowerer\Declaration\Metadata\ClassDeclaration;
129use Deriver\Deriver\Lowerer\Declaration\Metadata\PropertyDeclaration;
130use Deriver\Deriver\Lowerer\Declaration\Traits\Composition;
131use Deriver\Deriver\Lowerer\Declaration\Traits\Members;
132use Deriver\Deriver\Lowerer\Declaration\Traits\PropertyScope;
133use Deriver\Deriver\Lowerer\EffectInspection;
134use Deriver\Deriver\Lowerer\ExpressionLowering;
135use Deriver\Deriver\Lowerer\Graph\Argument;
136use Deriver\Deriver\Lowerer\Graph\BasicBlock;
137use Deriver\Deriver\Lowerer\Graph\CallableGraph;
138use Deriver\Deriver\Lowerer\Graph\CatchTarget;
139use Deriver\Deriver\Lowerer\Graph\ExceptionRegion;
140use Deriver\Deriver\Lowerer\Graph\Instruction;
141use Deriver\Deriver\Lowerer\Graph\Parameter;
142use Deriver\Deriver\Lowerer\Graph\Terminator;
143use Deriver\Deriver\Lowerer\Hierarchy\Access as HierarchyAccess;
144use Deriver\Deriver\Lowerer\Hierarchy\Builtins;
145use Deriver\Deriver\Lowerer\Hierarchy\Dispatch;
146use Deriver\Deriver\Lowerer\InstructionEmitter;
147use Deriver\Deriver\Lowerer\Lowerer;
148use Deriver\Deriver\Lowerer\LoweringScope;
149use Deriver\Deriver\Lowerer\StatementLowering;
150use Deriver\Deriver\MicroEvaluatorConversion;
151use Deriver\Deriver\Parser\Node\File;
152use Deriver\Deriver\Parser\Parser;
153use Deriver\Deriver\Parser\ParserConfig;
154use Deriver\Deriver\Parser\TargetSyntax;
155use Deriver\Deriver\Providers\ProviderExpansion;
156use Deriver\Deriver\Resolver\AssignmentPatterns;
157use Deriver\Deriver\Resolver\ClassScope;
158use Deriver\Deriver\Resolver\ExpressionValidation;
159use Deriver\Deriver\Resolver\MagicContext;
160use Deriver\Deriver\Resolver\Resolver;
161use Deriver\Deriver\Selection\QueryValidation;
162use Deriver\Deriver\SnapshotIdentity;
163use Deriver\Deriver\Sources\LineMap;
164use Deriver\Deriver\Sources\Path as SourcesPath;
165use Deriver\Deriver\Sources\Source;
166use Deriver\Deriver\Sources\SourceRef;
167use Deriver\Deriver\Sources\Sources;
168use Deriver\Exception\ParserException;
169use Deriver\Exception\ResolutionException;
170use PHPUnit\Framework\Attributes\CoversClass;
171use PHPUnit\Framework\Attributes\DataProvider;
172use PHPUnit\Framework\Attributes\Small;
173use PHPUnit\Framework\Attributes\UsesClass;
174use PHPUnit\Framework\TestCase;
175
176#[UsesClass(Resolver::class)]
177#[UsesClass(ExpressionValidation::class)]
178#[UsesClass(ResolutionException::class)]
179#[UsesClass(ParserConfig::class)]
180#[UsesClass(ParserException::class)]
181#[CoversClass(Allocation::class)]
182#[UsesClass(QueryExecution::class)]
183#[UsesClass(QueryValidation::class)]
184#[UsesClass(ResultAssessment::class)]
185#[UsesClass(Deriver::class)]
186#[UsesClass(Argument::class)]
187#[UsesClass(BasicBlock::class)]
188#[UsesClass(CallableGraph::class)]
189#[UsesClass(CallableIdentity::class)]
190#[UsesClass(CatchTarget::class)]
191#[UsesClass(ClassConstant::class)]
192#[UsesClass(ClassDeclaration::class)]
193#[UsesClass(ExceptionRegion::class)]
194#[UsesClass(Instruction::class)]
195#[UsesClass(Parameter::class)]
196#[UsesClass(PropertyDeclaration::class)]
197#[UsesClass(Terminator::class)]
198#[UsesClass(ArgumentBinding::class)]
199#[UsesClass(ArgumentOrder::class)]
200#[UsesClass(CallExecutor::class)]
201#[UsesClass(CallResolution::class)]
202#[UsesClass(Access::class)]
203#[UsesClass(Builtins::class)]
204#[UsesClass(Dispatch::class)]
205#[UsesClass(HierarchyAccess::class)]
206#[UsesClass(Invocation::class)]
207#[UsesClass(MethodInvocation::class)]
208#[UsesClass(NativeInvocation::class)]
209#[UsesClass(Properties::class)]
210#[UsesClass(Signatures::class)]
211#[UsesClass(ParameterBinding::class)]
212#[UsesClass(PassedArgument::class)]
213#[UsesClass(Arguments::class)]
214#[UsesClass(Creation::class)]
215#[UsesClass(Methods::class)]
216#[UsesClass(Modes::class)]
217#[UsesClass(Resolution::class)]
218#[UsesClass(Target::class)]
219#[UsesClass(Transfer::class)]
220#[UsesClass(ProviderDispatch::class)]
221#[UsesClass(TypeBinding::class)]
222#[UsesClass(TypeCheck::class)]
223#[UsesClass(Completion::class)]
224#[UsesClass(ClassNames::class)]
225#[UsesClass(Context::class)]
226#[UsesClass(ExceptionChain::class)]
227#[UsesClass(ExceptionMatch::class)]
228#[UsesClass(Handler::class)]
229#[UsesClass(ObservationLimit::class)]
230#[UsesClass(StateJoin::class)]
231#[UsesClass(Unwinding::class)]
232#[UsesClass(Cell::class)]
233#[UsesClass(Components::class)]
234#[UsesClass(Discovery::class)]
235#[UsesClass(Key::class)]
236#[UsesClass(Table::class)]
237#[UsesClass(Dependencies::class)]
238#[UsesClass(Havoc::class)]
239#[UsesClass(InstructionTransfer::class)]
240#[UsesClass(Evaluator::class)]
241#[UsesClass(SlotReference::class)]
242#[UsesClass(StateStorage::class)]
243#[UsesClass(ObservationCollector::class)]
244#[UsesClass(Address::class)]
245#[UsesClass(Path::class)]
246#[UsesClass(Reader::class)]
247#[UsesClass(OffsetTransfer::class)]
248#[UsesClass(Conversions::class)]
249#[UsesClass(ScalarErrors::class)]
250#[UsesClass(State::class)]
251#[UsesClass(CompletionRecord::class)]
252#[UsesClass(Evaluation::class)]
253#[UsesClass(SummaryInvocation::class)]
254#[UsesClass(Isolation::class)]
255#[UsesClass(MemoryStep::class)]
256#[UsesClass(ObjectAccess::class)]
257#[UsesClass(PropertyAccessCheck::class)]
258#[UsesClass(PropertyLookup::class)]
259#[UsesClass(PropertyReference::class)]
260#[UsesClass(PropertySlot::class)]
261#[UsesClass(PropertyTransfer::class)]
262#[UsesClass(PureStep::class)]
263#[UsesClass(ReferenceAssignment::class)]
264#[UsesClass(Location::class)]
265#[UsesClass(Materialization::class)]
266#[UsesClass(Memory::class)]
267#[UsesClass(ReferenceConstraint::class)]
268#[UsesClass(StorageCapture::class)]
269#[UsesClass(DispatchDecision::class)]
270#[UsesClass(DispatchRequest::class)]
271#[UsesClass(Extensions::class)]
272#[UsesClass(ProviderExpansion::class)]
273#[UsesClass(Evaluators::class)]
274#[UsesClass(StateRegistry::class)]
275#[UsesClass(StateSlot::class)]
276#[UsesClass(DeriverConfig::class)]
277#[UsesClass(EntryPoint::class)]
278#[UsesClass(Sources::class)]
279#[UsesClass(SourcesPath::class)]
280#[UsesClass(Snapshot::class)]
281#[UsesClass(Source::class)]
282#[UsesClass(TargetProfile::class)]
283#[UsesClass(Budget::class)]
284#[UsesClass(QueryScope::class)]
285#[UsesClass(ReturnQuery::class)]
286#[UsesClass(ResultRef::class)]
287#[UsesClass(SourceRef::class)]
288#[UsesClass(Alternative::class)]
289#[UsesClass(Assessment::class)]
290#[UsesClass(Derivation::class)]
291#[UsesClass(DerivationResult::class)]
292#[UsesClass(Frontier::class)]
293#[UsesClass(QueryEncoding::class)]
294#[UsesClass(ValueGraph::class)]
295#[UsesClass(Statistics::class)]
296#[UsesClass(StorageSnapshot::class)]
297#[UsesClass(Parser::class)]
298#[UsesClass(File::class)]
299#[UsesClass(AggregateLowering::class)]
300#[UsesClass(AssignmentLowering::class)]
301#[UsesClass(CallLowering::class)]
302#[UsesClass(Lowerer::class)]
303#[UsesClass(DestructuringLowering::class)]
304#[UsesClass(ExceptionLowering::class)]
305#[UsesClass(EffectInspection::class)]
306#[UsesClass(ExpressionLowering::class)]
307#[UsesClass(InstructionEmitter::class)]
308#[UsesClass(LoweringScope::class)]
309#[UsesClass(StatementLowering::class)]
310#[UsesClass(ConstantSignatures::class)]
311#[UsesClass(CallableSource::class)]
312#[UsesClass(Indexer::class)]
313#[UsesClass(Index::class)]
314#[UsesClass(Composition::class)]
315#[UsesClass(Members::class)]
316#[UsesClass(PropertyScope::class)]
317#[UsesClass(LineMap::class)]
318#[UsesClass(MagicContext::class)]
319#[UsesClass(AssignmentPatterns::class)]
320#[UsesClass(ClassScope::class)]
321#[UsesClass(TargetSyntax::class)]
322#[UsesClass(Arithmetic::class)]
323#[UsesClass(Arrays::class)]
324#[UsesClass(Identity::class)]
325#[UsesClass(Increment::class)]
326#[UsesClass(Lattice::class)]
327#[UsesClass(Operations::class)]
328#[UsesClass(Term::class)]
329#[Small]
330final class AllocationTest extends TestCase
331{
332 public function testApplyPreservesTheSemanticContract(): void
333 {
334 $source = '<?php class Box{public $child; public $value=0;}function target(){$a=new Box;$a->child=new Box;$b=clone $a;$b->child->value=3;return $a->child->value;}';
335 $symbol = 'target';
336 $input = new Sources([new Source('fixture.php', $source)]);
337 $configuration = new DeriverConfig();
338 $contributions = new Contributions();
339 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
340 $providers = new ProviderExpansion($input, array_map(Term::fromNative(...), $configuration->environment), $contributions);
341 $contributions1 = $providers->contributions;
342 $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
343 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
344 $identity = new SnapshotIdentity($providers->input, $settings, $evaluators, $providers->manifest);
345 $snapshotId = $identity->id;
346 $input1 = $providers->input;
347 $parser = new Parser(new ParserConfig($target->version));
348 $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(SourcesPath::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input1, false));
349 $program = new Index($snapshotId, $trees);
350 $result = (new QueryExecution($program, new ExecutionEvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators), $evaluators, $identity->snapshot(array_map(Frontier::fromDiagnostic(...), $program->diagnostics()))))->derive(new ReturnQuery($symbol));
351 self::assertSame(3, $result->normalOutcomes[0]->values['return']->native());
352 self::assertSame([], $result->frontiers);
353 self::assertSame([], $result->exceptionalOutcomes);
354 }
355 public function testInitializeEvaluatesEachAllocationDefaultIndependently(): void
356 {
357 $source = '<?php class B{public $x=[1];}function target(){$a=new B;$b=new B;$a->x[0]=2;return [$a->x,$b->x];}';
358 $symbol = 'target';
359 $input = new Sources([new Source('fixture.php', $source)]);
360 $configuration = new DeriverConfig();
361 $contributions = new Contributions();
362 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
363 $providers = new ProviderExpansion($input, array_map(Term::fromNative(...), $configuration->environment), $contributions);
364 $contributions1 = $providers->contributions;
365 $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
366 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
367 $identity = new SnapshotIdentity($providers->input, $settings, $evaluators, $providers->manifest);
368 $snapshotId = $identity->id;
369 $input1 = $providers->input;
370 $parser = new Parser(new ParserConfig($target->version));
371 $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(SourcesPath::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input1, false));
372 $program = new Index($snapshotId, $trees);
373 $result = (new QueryExecution($program, new ExecutionEvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators), $evaluators, $identity->snapshot(array_map(Frontier::fromDiagnostic(...), $program->diagnostics()))))->derive(new ReturnQuery($symbol));
374 self::assertSame([], $result->frontiers);
375 self::assertSame([], $result->exceptionalOutcomes);
376 self::assertCount(1, $result->normalOutcomes);
377 self::assertSame([[2], [1]], $result->normalOutcomes[0]->values['return']->native());
378 }
379 public function testRecordCreatesDistinctAbstractStatePerObject(): void
380 {
381 $source = '<?php function target(){return 1;}';
382 $budget = new Budget();
383 $configuration = new DeriverConfig();
384 $contributions = new Contributions(stateSlots: [new StateSlot('example.slot', 'int', Term::constant(1))]);
385 $input = new Sources([new Source('fixture.php', $source)]);
386 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
387 $providers = new ProviderExpansion($input, array_map(Term::fromNative(...), $configuration->environment), $contributions);
388 $contributions1 = $providers->contributions;
389 $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
390 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
391 $identity = new SnapshotIdentity($providers->input, $settings, $evaluators, $providers->manifest);
392 $snapshotId = $identity->id;
393 $input1 = $providers->input;
394 $parser = new Parser(new ParserConfig($target->version));
395 $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(SourcesPath::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input1, false));
396 $program1 = new Index($snapshotId, $trees);
397 $execution = new QueryExecution($program1, new ExecutionEvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators), $evaluators, $identity->snapshot(array_map(Frontier::fromDiagnostic(...), $program1->diagnostics())));
398 $snapshotId1 = 'test';
399 $input2 = new Sources([new Source('fixture.php', $source)]);
400 $profile = $execution->configuration->target;
401 $parser1 = new Parser(new ParserConfig($profile->version));
402 $trees1 = array_map(static fn (Source $source2): File => (new Resolver())->resolve($parser1->parse(new Source(SourcesPath::normalize($source2->path), $source2->contents, $source2->declarationsOnly))), iterator_to_array($input2, false));
403 $program = new Index($snapshotId1, $trees1);
404 $context = new Context($program, new ReturnQuery('target', budget: $budget), $execution->configuration, $execution->evaluators);
405 $state = new State();
406 $storage = new StateStorage($context);
407 $instruction = new BoundInstruction('new', 'new', new SourceRef('test', 'fixture.php', 0, 1));
408 $allocation = new Allocation(new Evaluator($context));
409 $a = $allocation->record($instruction, $state, 'Box', Term::constant('Box'), false);
410 $b = $allocation->record($instruction, $state, 'Box', Term::constant('Box'), false);
411 self::assertNotSame($a->literal, $b->literal);
412 self::assertSame(1, $state->memory->read(new Location('evaluator:' . $a->literal, ['example.slot']))->native());
413 }
414 public function testRecordClonesHeapAndModelStateWithTheirDeclaredSharingRules(): void
415 {
416 $source1 = '<?php function target(){return 1;}';
417 $budget = new Budget();
418 $configuration = new DeriverConfig();
419 $contributions = new Contributions(stateSlots: [new StateSlot('example.copy', 'int', Term::constant(1)), new StateSlot('example.reset', 'int', Term::constant(2), ClonePolicy::Reset)]);
420 $input = new Sources([new Source('fixture.php', $source1)]);
421 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
422 $providers = new ProviderExpansion($input, array_map(Term::fromNative(...), $configuration->environment), $contributions);
423 $contributions1 = $providers->contributions;
424 $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
425 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
426 $identity = new SnapshotIdentity($providers->input, $settings, $evaluators, $providers->manifest);
427 $snapshotId = $identity->id;
428 $input1 = $providers->input;
429 $parser = new Parser(new ParserConfig($target->version));
430 $trees = array_map(static fn (Source $source2): File => (new Resolver())->resolve($parser->parse(new Source(SourcesPath::normalize($source2->path), $source2->contents, $source2->declarationsOnly))), iterator_to_array($input1, false));
431 $program1 = new Index($snapshotId, $trees);
432 $execution = new QueryExecution($program1, new ExecutionEvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators), $evaluators, $identity->snapshot(array_map(Frontier::fromDiagnostic(...), $program1->diagnostics())));
433 $snapshotId1 = 'test';
434 $input2 = new Sources([new Source('fixture.php', $source1)]);
435 $profile = $execution->configuration->target;
436 $parser1 = new Parser(new ParserConfig($profile->version));
437 $trees1 = array_map(static fn (Source $source3): File => (new Resolver())->resolve($parser1->parse(new Source(SourcesPath::normalize($source3->path), $source3->contents, $source3->declarationsOnly))), iterator_to_array($input2, false));
438 $program = new Index($snapshotId1, $trees1);
439 $context = new Context($program, new ReturnQuery('target', budget: $budget), $execution->configuration, $execution->evaluators);
440 $symbol = 'target';
441 $body = $context->program->callable($symbol);
442 self::assertNotNull($body);
443 $state = new State();
444 $source = new Term(TermKind::Object, 'original', attributes: ['class' => 'Box']);
445 $shared = $state->memory->allocate(Term::constant(3));
446 $reference = new Term(TermKind::Cell, $shared->root);
447 $child = new Term(TermKind::Object, 'nested', attributes: ['class' => 'Child']);
448 $state->memory->cells['object:original'] = Term::array(['value' => Term::constant(4), 'shared' => $reference, 'child' => $child]);
449 $state->memory->propertyTypes['object:original'] = ['value' => 'int', 'shared' => 'int', 'child' => 'Child'];
450 $state->memory->cells['evaluator:original'] = Term::array(['example.copy' => Term::constant(8), 'example.reset' => Term::constant(9)]);
451 $object = (new Allocation(new Evaluator($context)))->record(new BoundInstruction('site', 'clone', $body->source), $state, 'Box', $source, true);
452 self::assertNotSame('original', $object->literal);
453 self::assertSame('Box', $object->attributes['class']);
454 self::assertSame('Box', $state->memory->classes[(string) $object->literal]);
455 $root = 'object:' . $object->literal;
456 self::assertSame($reference, $state->memory->cells[$root]->operands['shared']);
457 self::assertSame($child, $state->memory->cells[$root]->operands['child']);
458 self::assertSame(['value' => 'int', 'shared' => 'int', 'child' => 'Child'], $state->memory->propertyTypes[$root]);
459 self::assertSame(['value' => true, 'shared' => true, 'child' => true], $state->memory->cloneWrites[$root]);
460 self::assertSame(8, $state->memory->read(new Location('evaluator:' . $object->literal, ['example.copy']))->native());
461 self::assertSame(2, $state->memory->read(new Location('evaluator:' . $object->literal, ['example.reset']))->native());
462 $state->memory->write(new Location($root, ['value']), Term::constant(10));
463 $state->memory->write(new Location($root, ['shared']), Term::constant(11));
464 self::assertSame(4, $state->memory->read(new Location('object:original', ['value']))->native());
465 self::assertSame(11, $state->memory->read(new Location('object:original', ['shared']))->native());
466 }
467 public function testRecordUnknownClonesRetainOpenPropertyStorage(): void
468 {
469 $source = '<?php function target(){return 1;}';
470 $budget = new Budget();
471 $configuration = new DeriverConfig();
472 $contributions = new Contributions();
473 $input = new Sources([new Source('fixture.php', $source)]);
474 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
475 $providers = new ProviderExpansion($input, array_map(Term::fromNative(...), $configuration->environment), $contributions);
476 $contributions1 = $providers->contributions;
477 $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
478 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
479 $identity = new SnapshotIdentity($providers->input, $settings, $evaluators, $providers->manifest);
480 $snapshotId = $identity->id;
481 $input1 = $providers->input;
482 $parser = new Parser(new ParserConfig($target->version));
483 $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(SourcesPath::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input1, false));
484 $program1 = new Index($snapshotId, $trees);
485 $execution = new QueryExecution($program1, new ExecutionEvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators), $evaluators, $identity->snapshot(array_map(Frontier::fromDiagnostic(...), $program1->diagnostics())));
486 $snapshotId1 = 'test';
487 $input2 = new Sources([new Source('fixture.php', $source)]);
488 $profile = $execution->configuration->target;
489 $parser1 = new Parser(new ParserConfig($profile->version));
490 $trees1 = array_map(static fn (Source $source2): File => (new Resolver())->resolve($parser1->parse(new Source(SourcesPath::normalize($source2->path), $source2->contents, $source2->declarationsOnly))), iterator_to_array($input2, false));
491 $program = new Index($snapshotId1, $trees1);
492 $context = new Context($program, new ReturnQuery('target', budget: $budget), $execution->configuration, $execution->evaluators);
493 $symbol = 'target';
494 $body = $context->program->callable($symbol);
495 self::assertNotNull($body);
496 $state = new State();
497 $object = (new Allocation(new Evaluator($context)))->record(new BoundInstruction('site', 'clone', $body->source), $state, 'External', new Term(TermKind::Object, 'external'), true);
498 $root = 'object:' . $object->literal;
499 self::assertSame(TermKind::Array, $state->memory->cells[$root]->kind);
500 self::assertTrue($state->memory->cells[$root]->attributes['open']);
501 self::assertSame([], $state->memory->propertyTypes[$root]);
502 self::assertSame([], $state->memory->cloneWrites[$root]);
503 }
504 public function testRecordFreshObjectsStartWithClosedStorageAndNoClonePermissions(): void
505 {
506 $source = '<?php function target(){return 1;}';
507 $budget = new Budget();
508 $configuration = new DeriverConfig();
509 $contributions = new Contributions();
510 $input = new Sources([new Source('fixture.php', $source)]);
511 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
512 $providers = new ProviderExpansion($input, array_map(Term::fromNative(...), $configuration->environment), $contributions);
513 $contributions1 = $providers->contributions;
514 $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
515 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
516 $identity = new SnapshotIdentity($providers->input, $settings, $evaluators, $providers->manifest);
517 $snapshotId = $identity->id;
518 $input1 = $providers->input;
519 $parser = new Parser(new ParserConfig($target->version));
520 $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(SourcesPath::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input1, false));
521 $program1 = new Index($snapshotId, $trees);
522 $execution = new QueryExecution($program1, new ExecutionEvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators), $evaluators, $identity->snapshot(array_map(Frontier::fromDiagnostic(...), $program1->diagnostics())));
523 $snapshotId1 = 'test';
524 $input2 = new Sources([new Source('fixture.php', $source)]);
525 $profile = $execution->configuration->target;
526 $parser1 = new Parser(new ParserConfig($profile->version));
527 $trees1 = array_map(static fn (Source $source2): File => (new Resolver())->resolve($parser1->parse(new Source(SourcesPath::normalize($source2->path), $source2->contents, $source2->declarationsOnly))), iterator_to_array($input2, false));
528 $program = new Index($snapshotId1, $trees1);
529 $context = new Context($program, new ReturnQuery('target', budget: $budget), $execution->configuration, $execution->evaluators);
530 $symbol = 'target';
531 $body = $context->program->callable($symbol);
532 self::assertNotNull($body);
533 $state = new State();
534 $state->memory->cells['object:Box'] = Term::fromNative(['old' => 9]);
535 $object = (new Allocation(new Evaluator($context)))->record(new BoundInstruction('site', 'new', $body->source), $state, 'Box', Term::constant('Box'), false);
536 $root = 'object:' . $object->literal;
537 self::assertSame([], $state->memory->cells[$root]->native());
538 self::assertSame([], $state->memory->cloneWrites);
539 self::assertSame('Box', $state->memory->classes[(string) $object->literal]);
540 }
541 public function testInitializeKeepsParentPrivateSlotsAndSkipsStaticProperties(): void
542 {
543 $source = '<?php class ParentBox{private int $value=3;protected int $inherited=4;public static int $global=99;}class Box extends ParentBox{private int $value=7;public $plain;public string $typed;}function target(){}';
544 $budget = new Budget();
545 $configuration = new DeriverConfig();
546 $contributions = new Contributions();
547 $input = new Sources([new Source('fixture.php', $source)]);
548 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
549 $providers = new ProviderExpansion($input, array_map(Term::fromNative(...), $configuration->environment), $contributions);
550 $contributions1 = $providers->contributions;
551 $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
552 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
553 $identity = new SnapshotIdentity($providers->input, $settings, $evaluators, $providers->manifest);
554 $snapshotId = $identity->id;
555 $input1 = $providers->input;
556 $parser = new Parser(new ParserConfig($target->version));
557 $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(SourcesPath::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input1, false));
558 $program1 = new Index($snapshotId, $trees);
559 $execution = new QueryExecution($program1, new ExecutionEvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators), $evaluators, $identity->snapshot(array_map(Frontier::fromDiagnostic(...), $program1->diagnostics())));
560 $snapshotId1 = 'test';
561 $input2 = new Sources([new Source('fixture.php', $source)]);
562 $profile = $execution->configuration->target;
563 $parser1 = new Parser(new ParserConfig($profile->version));
564 $trees1 = array_map(static fn (Source $source2): File => (new Resolver())->resolve($parser1->parse(new Source(SourcesPath::normalize($source2->path), $source2->contents, $source2->declarationsOnly))), iterator_to_array($input2, false));
565 $program = new Index($snapshotId1, $trees1);
566 $context = new Context($program, new ReturnQuery('target', budget: $budget), $execution->configuration, $execution->evaluators);
567 $state = new State();
568 $object = new Term(TermKind::Object, 'box', attributes: ['class' => 'Box']);
569 $state->memory->cells['object:box'] = Term::array([]);
570 $paths = (new Allocation(new Evaluator($context)))->initialize('Box', $object, $state);
571 self::assertCount(1, $paths);
572 $memory = $paths[0]->memory;
573 self::assertSame(3, $memory->read(new Location('object:box', ['ParentBox::value']))->native());
574 self::assertSame(7, $memory->read(new Location('object:box', ['Box::value']))->native());
575 self::assertSame(4, $memory->read(new Location('object:box', ['inherited']))->native());
576 self::assertNull($memory->read(new Location('object:box', ['plain']))->native());
577 self::assertSame(TermKind::Uninitialized, $memory->read(new Location('object:box', ['typed']))->kind);
578 self::assertSame('string', $memory->read(new Location('object:box', ['typed']))->attributes['type']);
579 self::assertArrayNotHasKey('global', $memory->cells['object:box']->operands);
580 self::assertSame(['ParentBox::value' => 'int', 'inherited' => 'int', 'Box::value' => 'int', 'plain' => 'mixed', 'typed' => 'string'], $memory->propertyTypes['object:box']);
581 }
582 public function testInitializeResolvesRelativePropertyTypesInTheDeclaringClass(): void
583 {
584 $source = '<?php class Ancestor{}class ParentBox extends Ancestor{public self $self;public parent $parent;}class Box extends ParentBox{}function target(){}';
585 $budget = new Budget();
586 $configuration = new DeriverConfig();
587 $contributions = new Contributions();
588 $input = new Sources([new Source('fixture.php', $source)]);
589 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
590 $providers = new ProviderExpansion($input, array_map(Term::fromNative(...), $configuration->environment), $contributions);
591 $contributions1 = $providers->contributions;
592 $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
593 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
594 $identity = new SnapshotIdentity($providers->input, $settings, $evaluators, $providers->manifest);
595 $snapshotId = $identity->id;
596 $input1 = $providers->input;
597 $parser = new Parser(new ParserConfig($target->version));
598 $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(SourcesPath::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input1, false));
599 $program1 = new Index($snapshotId, $trees);
600 $execution = new QueryExecution($program1, new ExecutionEvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators), $evaluators, $identity->snapshot(array_map(Frontier::fromDiagnostic(...), $program1->diagnostics())));
601 $snapshotId1 = 'test';
602 $input2 = new Sources([new Source('fixture.php', $source)]);
603 $profile = $execution->configuration->target;
604 $parser1 = new Parser(new ParserConfig($profile->version));
605 $trees1 = array_map(static fn (Source $source2): File => (new Resolver())->resolve($parser1->parse(new Source(SourcesPath::normalize($source2->path), $source2->contents, $source2->declarationsOnly))), iterator_to_array($input2, false));
606 $program = new Index($snapshotId1, $trees1);
607 $context = new Context($program, new ReturnQuery('target', budget: $budget), $execution->configuration, $execution->evaluators);
608 $state = new State();
609 $object = new Term(TermKind::Object, 'box', attributes: ['class' => 'Box']);
610 $state->memory->cells['object:box'] = Term::array([]);
611 $paths = (new Allocation(new Evaluator($context)))->initialize('Box', $object, $state);
612 self::assertSame(['self' => 'ParentBox', 'parent' => 'Ancestor'], $paths[0]->memory->propertyTypes['object:box']);
613 }
614 public function testInitializeCycleGuardsDoNotOverwriteExistingState(): void
615 {
616 $source = '<?php class Box{public $value=9;}function target(){}';
617 $budget = new Budget();
618 $configuration = new DeriverConfig();
619 $contributions = new Contributions();
620 $input = new Sources([new Source('fixture.php', $source)]);
621 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
622 $providers = new ProviderExpansion($input, array_map(Term::fromNative(...), $configuration->environment), $contributions);
623 $contributions1 = $providers->contributions;
624 $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
625 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
626 $identity = new SnapshotIdentity($providers->input, $settings, $evaluators, $providers->manifest);
627 $snapshotId = $identity->id;
628 $input1 = $providers->input;
629 $parser = new Parser(new ParserConfig($target->version));
630 $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(SourcesPath::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input1, false));
631 $program1 = new Index($snapshotId, $trees);
632 $execution = new QueryExecution($program1, new ExecutionEvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators), $evaluators, $identity->snapshot(array_map(Frontier::fromDiagnostic(...), $program1->diagnostics())));
633 $snapshotId1 = 'test';
634 $input2 = new Sources([new Source('fixture.php', $source)]);
635 $profile = $execution->configuration->target;
636 $parser1 = new Parser(new ParserConfig($profile->version));
637 $trees1 = array_map(static fn (Source $source2): File => (new Resolver())->resolve($parser1->parse(new Source(SourcesPath::normalize($source2->path), $source2->contents, $source2->declarationsOnly))), iterator_to_array($input2, false));
638 $program = new Index($snapshotId1, $trees1);
639 $context = new Context($program, new ReturnQuery('target', budget: $budget), $execution->configuration, $execution->evaluators);
640 $state = new State();
641 $object = new Term(TermKind::Object, 'box', attributes: ['class' => 'Box']);
642 $state->memory->cells['object:box'] = Term::fromNative(['value' => 4]);
643 $paths = (new Allocation(new Evaluator($context)))->initialize('Box', $object, $state, ['Box']);
644 self::assertSame([$state], $paths);
645 self::assertSame(['value' => 4], $state->memory->cells['object:box']->native());
646 self::assertSame([], $state->memory->propertyTypes);
647 }
648 public function testInitializeNativeParentsBeforeSourcePropertyDefaults(): void
649 {
650 $source = '<?php class Problem extends RuntimeException{public $tag="source";}function target(){}';
651 $budget = new Budget();
652 $configuration = new DeriverConfig();
653 $contributions = new Contributions();
654 $input = new Sources([new Source('fixture.php', $source)]);
655 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
656 $providers = new ProviderExpansion($input, array_map(Term::fromNative(...), $configuration->environment), $contributions);
657 $contributions1 = $providers->contributions;
658 $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
659 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
660 $identity = new SnapshotIdentity($providers->input, $settings, $evaluators, $providers->manifest);
661 $snapshotId = $identity->id;
662 $input1 = $providers->input;
663 $parser = new Parser(new ParserConfig($target->version));
664 $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(SourcesPath::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input1, false));
665 $program1 = new Index($snapshotId, $trees);
666 $execution = new QueryExecution($program1, new ExecutionEvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators), $evaluators, $identity->snapshot(array_map(Frontier::fromDiagnostic(...), $program1->diagnostics())));
667 $snapshotId1 = 'test';
668 $input2 = new Sources([new Source('fixture.php', $source)]);
669 $profile = $execution->configuration->target;
670 $parser1 = new Parser(new ParserConfig($profile->version));
671 $trees1 = array_map(static fn (Source $source2): File => (new Resolver())->resolve($parser1->parse(new Source(SourcesPath::normalize($source2->path), $source2->contents, $source2->declarationsOnly))), iterator_to_array($input2, false));
672 $program = new Index($snapshotId1, $trees1);
673 $context = new Context($program, new ReturnQuery('target', budget: $budget), $execution->configuration, $execution->evaluators);
674 $state = new State();
675 $object = new Term(TermKind::Object, 'problem', attributes: ['class' => 'Problem']);
676 $state->memory->cells['object:problem'] = Term::array([]);
677 $paths = (new Allocation(new Evaluator($context)))->initialize('Problem', $object, $state);
678 self::assertCount(1, $paths);
679 self::assertSame('source', $paths[0]->memory->read(new Location('object:problem', ['tag']))->native());
680 self::assertSame('', $paths[0]->memory->read(new Location('object:problem', ['message']))->native());
681 self::assertSame(0, $paths[0]->memory->read(new Location('object:problem', ['code']))->native());
682 self::assertNull($paths[0]->memory->read(new Location('object:problem', ['Exception::previous']))->native());
683 }
684 /**
685 * @param string $source Captured source
686 * @param mixed $expected Returned observable state
687 */
688 #[DataProvider('providerAllocationPrograms')]
689 public function testApplyHonorsInitializationAndLifecycleEffects(string $source, mixed $expected): void
690 {
691 $symbol = 'target';
692 $input = new Sources([new Source('fixture.php', $source)]);
693 $configuration = new DeriverConfig();
694 $contributions = new Contributions();
695 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
696 $providers = new ProviderExpansion($input, array_map(Term::fromNative(...), $configuration->environment), $contributions);
697 $contributions1 = $providers->contributions;
698 $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
699 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
700 $identity = new SnapshotIdentity($providers->input, $settings, $evaluators, $providers->manifest);
701 $snapshotId = $identity->id;
702 $input1 = $providers->input;
703 $parser = new Parser(new ParserConfig($target->version));
704 $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(SourcesPath::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input1, false));
705 $program = new Index($snapshotId, $trees);
706 $result = (new QueryExecution($program, new ExecutionEvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators), $evaluators, $identity->snapshot(array_map(Frontier::fromDiagnostic(...), $program->diagnostics()))))->derive(new ReturnQuery($symbol));
707 self::assertCount(1, $result->normalOutcomes);
708 self::assertSame($expected, $result->normalOutcomes[0]->values['return']->native());
709 self::assertSame([], $result->exceptionalOutcomes);
710 self::assertSame([], $result->frontiers);
711 }
712 /**
713 * @return iterable<string,array{string,mixed}>
714 */
715 public static function providerAllocationPrograms(): iterable
716 {
717 yield 'constructor arguments' => ['<?php class Box{public $value=1;function __construct($v){$this->value=$v;}}function target(){return (new Box(7))->value;}', 7];
718 yield 'inherited constructor' => ['<?php class ParentBox{public $value;function __construct($v){$this->value=$v;}}class Box extends ParentBox{}function target(){return (new Box(8))->value;}', 8];
719 yield 'constructor failure' => ['<?php class Box{function __construct(){throw new RuntimeException;}}function target(){try{new Box;return 1;}catch(RuntimeException $e){return 2;}}', 2];
720 yield 'initializer failure before constructor' => ['<?php class Box{public $value=1/0;function __construct(){throw new RuntimeException;}}function target(){try{new Box;return 1;}catch(Error $e){return 2;}}', 2];
721 yield 'clone hook' => ['<?php class Box{public $value=1;function __clone(){$this->value=7;}}function target(){$a=new Box;$a->value=4;$b=clone $a;return [$a->value,$b->value];}', [4, 7]];
722 yield 'clone failure' => ['<?php class Box{function __clone(){throw new RuntimeException;}}function target(){$a=new Box;try{$b=clone $a;return 1;}catch(RuntimeException $e){return 2;}}', 2];
723 yield 'clone does not call constructor' => ['<?php class Box{public static $calls=0;function __construct(){self::$calls++;}}function target(){$a=new Box;$b=clone $a;return Box::$calls;}', 1];
724 yield 'readonly clone reinitialization' => ['<?php class Box{function __construct(public readonly int $value){}function __clone(){$this->value=7;}}function target(){$a=new Box(4);$b=clone $a;return [$a->value,$b->value];}', [4, 7]];
725 yield 'readonly permission ends after clone' => ['<?php class Box{function __construct(public readonly int $value){}function __clone(){$this->value=7;}}function target(){$a=new Box(4);$b=clone $a;try{$b->value=8;}catch(Error $e){}return $b->value;}', 7];
726 yield 'private property scope' => ['<?php class A{private $x=1;function a(){return $this->x;}}class B extends A{private $x=2;function b(){return $this->x;}}function target(){$b=new B;return [$b->a(),$b->b()];}', [1, 2]];
727 yield 'trait property' => ['<?php trait Values{public $value=4;}class Box{use Values;}function target(){return (new Box)->value;}', 4];
728 yield 'native constructor' => ['<?php function target(){$e=new RuntimeException("message",7);return [$e->getMessage(),$e->getCode()];}', ['message', 7]];
729 yield 'abstract allocation fails' => ['<?php abstract class Box{}function target(){try{new Box;return 1;}catch(Error $e){return 2;}}', 2];
730 yield 'private constructor fails' => ['<?php class Box{private function __construct(){}}function target(){try{new Box;return 1;}catch(Error $e){return 2;}}', 2];
731 yield 'enum allocation fails' => ['<?php enum Box{case One;}function target(){try{new Box;return 1;}catch(Error $e){return 2;}}', 2];
732 yield 'clone scalar fails' => ['<?php function target(){$x=7;try{$a=clone $x;return 1;}catch(Error $e){return 2;}}', 2];
733 yield 'constructorless positional arguments' => ['<?php class Box{public $x=7;}function target(){return (new Box(1,2))->x;}', 7];
734 yield 'constructorless named argument fails' => ['<?php class Box{}function target(){try{new Box(value:7);return 1;}catch(Error $e){return 2;}}', 2];
735 }
736 public function testApplyDestructorLifetimeSealsInclusiveOutcomesBeforeInventingObjectState(): void
737 {
738 $source = '<?php class Box{function __destruct(){}}function target(){}';
739 $budget = new Budget();
740 $configuration = new DeriverConfig();
741 $contributions = new Contributions();
742 $input = new Sources([new Source('fixture.php', $source)]);
743 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
744 $providers = new ProviderExpansion($input, array_map(Term::fromNative(...), $configuration->environment), $contributions);
745 $contributions1 = $providers->contributions;
746 $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
747 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
748 $identity = new SnapshotIdentity($providers->input, $settings, $evaluators, $providers->manifest);
749 $snapshotId = $identity->id;
750 $input1 = $providers->input;
751 $parser = new Parser(new ParserConfig($target->version));
752 $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(SourcesPath::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input1, false));
753 $program1 = new Index($snapshotId, $trees);
754 $execution = new QueryExecution($program1, new ExecutionEvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators), $evaluators, $identity->snapshot(array_map(Frontier::fromDiagnostic(...), $program1->diagnostics())));
755 $snapshotId1 = 'test';
756 $input2 = new Sources([new Source('fixture.php', $source)]);
757 $profile = $execution->configuration->target;
758 $parser1 = new Parser(new ParserConfig($profile->version));
759 $trees1 = array_map(static fn (Source $source2): File => (new Resolver())->resolve($parser1->parse(new Source(SourcesPath::normalize($source2->path), $source2->contents, $source2->declarationsOnly))), iterator_to_array($input2, false));
760 $program = new Index($snapshotId1, $trees1);
761 $context = new Context($program, new ReturnQuery('target', budget: $budget), $execution->configuration, $execution->evaluators);
762 $symbol = 'target';
763 $body = $context->program->callable($symbol);
764 self::assertNotNull($body);
765 $state = new State();
766 $state->registers['class'] = Term::constant('Box');
767 $paths = (new Allocation(new Evaluator($context)))->apply($body, new BoundInstruction('new', 'new', $body->source, 'result', ['class']), $state, []);
768 self::assertCount(2, $paths);
769 self::assertSame([CompletionKind::Return, CompletionKind::Throw], array_column(array_column($paths, 'completion'), 'kind'));
770 self::assertSame('UNSUPPORTED_LANGUAGE_FEATURE', $paths[0]->completion->value?->literal);
771 self::assertSame('Throwable', $paths[1]->completion->value?->literal);
772 self::assertSame(TermKind::Opaque, $paths[0]->registers['result']->kind);
773 self::assertContains('destructor-lifetime', array_column($context->frontiers, 'operation'));
774 self::assertSame([], $paths[0]->memory->classes);
775 }
776}
777