Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions lib/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,20 @@ public function registerConstant(Operand $operand, Variable $const): int {
return $slot;
}

/**
* Per-run copy of a compile-time constant for frame scope (#12040, ServeCompileCache).
*
* Cached {@see Block} instances must not share {@see Variable} cells with execution
* scope — builtins may coerce/mutate argument operands in place.
*/
private function scopeConstantVariable(int $slot): Variable
{
$copy = new Variable();
$copy->duplicateFrom($this->constants[$slot]);

return $copy;
}

/**
* Copy cfg Var root slot mappings from a sibling branch (?: / if merge, #3790).
*/
Expand Down Expand Up @@ -875,7 +889,7 @@ public function getFrame(Context $context, ?Frame $frame = null): Frame {
// Variable reads in args must still resolve (#3787 merge + literal arm).
if (isset($scope[$pos]) && !$this->args->contains($op)) {
if (isset($this->constants[$pos])) {
$scope[$pos] = $this->constants[$pos];
$scope[$pos] = $this->scopeConstantVariable($pos);
}

continue;
Expand Down Expand Up @@ -908,7 +922,7 @@ public function getFrame(Context $context, ?Frame $frame = null): Frame {
}

if (isset($this->constants[$pos]) && !$this->args->contains($op)) {
$scope[$pos] = $this->constants[$pos];
$scope[$pos] = $this->scopeConstantVariable($pos);
} elseif (isset($this->closureCaptureSlots[$pos])) {
$scope[$pos] = self::initialVariableForOperand($op, $context, $pos, $this);
} elseif ($this->isArgRecvParameterSlot($pos)) {
Expand Down Expand Up @@ -1055,13 +1069,13 @@ private function ensureOpcodeReferencedSlots(array &$scope): void
for ($i = 0; $i <= $max; ++$i) {
if (isset($scope[$i])) {
if (isset($this->constants[$i])) {
$scope[$i] = $this->constants[$i];
$scope[$i] = $this->scopeConstantVariable($i);
}

continue;
}
if (isset($this->constants[$i])) {
$scope[$i] = $this->constants[$i];
$scope[$i] = $this->scopeConstantVariable($i);

continue;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/Web/DevServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,9 @@ public static function respond($conn, int $status, string $contentType, string $
$out .= "Content-Type: {$contentType}\r\n";
$out .= 'Content-Length: '.strlen($body)."\r\n";
foreach ($extraHeaders as $line) {
if ('' === $line) {
continue;
}
if (stripos($line, 'Content-Type:') === 0) {
continue;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/Web/ResponseContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ public static function assertSafeHeaderLine(string $line): void

public static function addHeader(string $line, bool $replace = true): void
{
if ('' === $line) {
return;
}
self::assertSafeHeaderLine($line);
if (preg_match('#^HTTP/\d(?:\.\d)?\s+(\d{3})#', $line, $m)) {
self::setStatus((int) $m[1]);
Expand Down
74 changes: 74 additions & 0 deletions test/unit/Web/ServeCompileCacheConstantsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace PHPCompiler;

use PHPCompiler\VM\OutputBuffer;
use PHPCompiler\VM\ShutdownQueue;
use PHPCompiler\ext\standard\VmSession;
use PHPCompiler\ext\standard\VmStatCache;
use PHPCompiler\Web\DevServer;
use PHPCompiler\Web\ProjectBootstrap;
use PHPCompiler\Web\ResponseContext;
use PHPCompiler\Web\ServeCompileCache;
use PHPCompiler\Web\Superglobals;
use PHPUnit\Framework\TestCase;

/** ServeCompileCache must not share mutable constant cells across VM runs (#12040). */
final class ServeCompileCacheConstantsTest extends TestCase
{
protected function tearDown(): void
{
ServeCompileCache::reset();
parent::tearDown();
}

public function testCachedEntryScriptKeepsHeaderLiteralAcrossRuns(): void
{
$script = realpath(__DIR__.'/../../../examples/002-StaticWeb/example.php');
$this->assertNotFalse($script);
$docroot = dirname($script);

ServeCompileCache::enable();
$runtime = new Runtime();
[$projectDir, $manifest] = ProjectBootstrap::resolveFromScript($script);
ProjectBootstrap::prepare($runtime, $projectDir, $manifest);
$block = ServeCompileCache::getFile($runtime, $script);
$this->assertNotNull($block);

foreach (['/example.php', '/example.php'] as $requestUri) {
ResponseContext::reset();
ResponseContext::enableHeaderQueue();
VmSession::reset();
VmStatCache::reset();
OutputBuffer::reset();
ShutdownQueue::reset();
DevServer::clearHttpServerKeys();

putenv('REQUEST_METHOD=GET');
putenv('QUERY_STRING=');
putenv('REQUEST_BODY=');
putenv('SCRIPT_NAME=/example.php');
putenv('SCRIPT_FILENAME='.$script);
putenv('REQUEST_URI='.$requestUri);
putenv('DOCUMENT_ROOT='.$docroot);
putenv('SERVER_PROTOCOL=HTTP/1.1');
putenv('PATH_INFO');
Superglobals::applyHttpHeaders(['host' => 'localhost', 'connection' => 'close']);

$run = new Runtime();
Superglobals::populateFromEnvironment($run->vmContext, '', '', $script);
ProjectBootstrap::prepare($run, $projectDir, $manifest);
$cached = ServeCompileCache::getFile($run, $script);
$this->assertSame($block, $cached);
$run->run($cached);

$this->assertSame(
['Content-Type: text/html; charset=UTF-8'],
ResponseContext::listHeaders(),
'request '.$requestUri
);
}
}
}