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
42 changes: 42 additions & 0 deletions ext/hash/HashContextDebugInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace PHPCompiler\ext\hash;

use PHPCompiler\Frame;
use PHPCompiler\VM\Builtin\VmClassMethod;
use PHPCompiler\VM\HashTable;
use PHPCompiler\VM\Variable;

/**
* HashContext::__debugInfo() — expose algorithm name only (php-src ext/hash/hash.c; #7084).
*/
final class HashContextDebugInfo extends VmClassMethod
{
public function __construct()
{
parent::__construct('__debugInfo');
}

public function execute(Frame $frame): void
{
if (null === $frame->returnVar) {
return;
}
if (\count($frame->calledArgs) < 1) {
throw new \LogicException('HashContext::__debugInfo() expects a HashContext receiver');
}
$receiver = $frame->calledArgs[0]->resolveIndirect();
if (Variable::TYPE_OBJECT !== $receiver->type) {
throw new \LogicException('HashContext::__debugInfo() expects a HashContext receiver');
}
$object = $receiver->toObject();
$algoName = VmHashContext::debugInfoAlgoName($object);
$ht = new HashTable();
$algoVar = new Variable();
$algoVar->string($algoName);
$ht->addNew('algo', $algoVar);
$frame->returnVar->array($ht);
}
}
12 changes: 12 additions & 0 deletions ext/hash/VmHashContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,21 @@ public static function registerClass(Context $ctx): void
}
$entry = new \PHPCompiler\VM\ClassEntry('HashContext');
$entry->isInternal = true;
$entry->methods['__debuginfo'] = new HashContextDebugInfo();
$entry->methodNames['__debuginfo'] = '__debugInfo';
$ctx->classes[self::CLASS_LC] = $entry;
}

public static function debugInfoAlgoName(ObjectEntry $entry): string
{
$state = self::$store[$entry->id] ?? null;
if (null === $state || self::CLASS_LC !== strtolower($entry->class->name)) {
throw new \LogicException('HashContext::__debugInfo() expects a HashContext receiver');
}

return VmHashNative::resolveAlgoName($state['algo']);
}

public static function init(Context $vmCtx, string $algo): Variable
{
$algoId = VmHashNative::resolveAlgoId($algo);
Expand Down
16 changes: 16 additions & 0 deletions ext/standard/VmHashNative.php
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,22 @@ public static function resolveAlgoId(string $algo): int
return self::algoId($algo);
}

/** Canonical registered algorithm name for HashContext::__debugInfo() (php-src ops->algo). */
public static function resolveAlgoName(int $algoId): string
{
if (1 === $algoId) {
return 'sha256';
}
if (2 === $algoId) {
return 'sha1';
}
if (3 === $algoId) {
return 'md5';
}

throw new \LogicException('Unsupported incremental hash algorithm id: '.$algoId);
}

/**
* @return array<string, mixed>
*/
Expand Down
18 changes: 18 additions & 0 deletions test/compliance/cases/stdlib/hash_context_debug_info.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Stdlib: HashContext::__debugInfo() — var_dump hides digest state (#7084)
--FILE--
<?php
declare(strict_types=1);

$ctx = hash_init('sha256');
var_dump($ctx->__debugInfo());
var_dump($ctx);
--EXPECTF--
array(1) {
["algo"]=>
string(6) "sha256"
}
object(HashContext)#%d (1) {
["algo"]=>
string(6) "sha256"
}
16 changes: 16 additions & 0 deletions test/unit/HashModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,20 @@ public function test_hash_init_invalid_algo_value_error(): void
$this->expectExceptionMessage('hash_init(): Argument #1 ($algo) must be a valid hashing algorithm');
$fn->execute($frame);
}

public function test_hash_context_debug_info(): void
{
$runtime = new Runtime();
$code = <<<'PHP'
<?php
$ctx = hash_init('sha256');
$info = $ctx->__debugInfo();
echo (int) is_array($info);
echo isset($info['algo']) ? $info['algo'] : '';
PHP;
$block = $runtime->parseAndCompile($code, 'hash_context_debug_info.php');
ob_start();
$runtime->run($block);
self::assertSame('1sha256', ob_get_clean());
}
}