Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VarDumper] Fix FFI caster test #57397

Merged
merged 1 commit into from
Jun 22, 2024
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
[VarDumper] Fix FFI caster test
  • Loading branch information
alexandre-daubois committed Jun 22, 2024
commit 8594b2fd1a9d28f6b7b155fab0e321b6d629aaf7
14 changes: 12 additions & 2 deletions src/Symfony/Component/VarDumper/Caster/FFICaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,21 @@
private static function castFFIStringValue(CData $data): string|CutStub
{
$result = [];
$ffi = \FFI::cdef(<<<C
size_t zend_get_page_size(void);
C);

for ($i = 0; $i < self::MAX_STRING_LENGTH; ++$i) {
$pageSize = $ffi->zend_get_page_size();

// get cdata address
$start = $ffi->cast('uintptr_t', $ffi->cast('char*', $data))->cdata;
// accessing memory in the same page as $start is safe
$max = min(self::MAX_STRING_LENGTH, ($start | ($pageSize - 1)) - $start);

for ($i = 0; $i < $max; ++$i) {
$result[$i] = $data[$i];

if ("\0" === $result[$i]) {
if ("\0" === $data[$i]) {

Check failure on line 132 in src/Symfony/Component/VarDumper/Caster/FFICaster.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedMethod

src/Symfony/Component/VarDumper/Caster/FFICaster.php:132:26: UndefinedMethod: Method FFI\CData::offsetGet does not exist (see https://psalm.dev/022)

Check failure on line 132 in src/Symfony/Component/VarDumper/Caster/FFICaster.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedMethod

src/Symfony/Component/VarDumper/Caster/FFICaster.php:132:26: UndefinedMethod: Method FFI\CData::offsetGet does not exist (see https://psalm.dev/022)
return implode('', $result);
}
}
Expand Down
18 changes: 2 additions & 16 deletions src/Symfony/Component/VarDumper/Tests/Caster/FFICasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\VarDumper\Tests\Caster;

use PHPUnit\Framework\TestCase;
use Symfony\Component\VarDumper\Caster\FFICaster;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;

/**
Expand Down Expand Up @@ -191,34 +190,21 @@ public function testCastCuttedPointerToChar()
PHP, $pointer);
}

/**
* It is worth noting that such a test can cause SIGSEGV, as it breaks
* into "foreign" memory. However, this is only theoretical, since
* memory is allocated within the PHP process and almost always "garbage
* data" will be read from the PHP process itself.
*
* If this test fails for some reason, please report it: We may have to
* disable the dumping of strings ("char*") feature in VarDumper.
*
* @see FFICaster::castFFIStringValue()
*/
public function testCastNonTrailingCharPointer()
{
$actualMessage = 'Hello World!';
$actualLength = \strlen($actualMessage);

$string = \FFI::cdef()->new('char['.$actualLength.']');
$string = \FFI::cdef()->new('char['.($actualLength + 1).']');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

$pointer = \FFI::addr($string[0]);

\FFI::memcpy($pointer, $actualMessage, $actualLength);

// Remove automatically addition of the trailing "\0" and remove trailing "\0"
$pointer = \FFI::cdef()->cast('char*', \FFI::cdef()->cast('void*', $pointer));
$pointer[$actualLength] = "\x01";

$this->assertDumpMatchesFormat(<<<PHP
FFI\CData<char*> size 8 align 8 {
cdata: "$actualMessage%s"
cdata: %A"$actualMessage%s"
}
PHP, $pointer);
}
Expand Down
Loading