Skip to content

Commit

Permalink
Merge pull request #138 from sj-i/0.3.x
Browse files Browse the repository at this point in the history
0.3.x to 0.4.x
  • Loading branch information
sj-i committed Dec 25, 2021
2 parents 493c77f + 4155a4b commit f05ad5b
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 21 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"squizlabs/php_codesniffer": "3.6.2",
"mockery/mockery": "1.4.4",
"psalm/plugin-mockery": "0.9.1",
"jetbrains/phpstorm-stubs": "2021.2",
"jetbrains/phpstorm-stubs": "2021.3",
"php-coveralls/php-coveralls": "2.5.2"
},
"autoload": {
Expand Down
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions src/Lib/FFI/CannotAllocateBufferException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* This file is part of the sj-i/php-profiler package.
*
* (c) sji <sji@sj-i.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace PhpProfiler\Lib\FFI;

final class CannotAllocateBufferException extends \Exception
{
}
18 changes: 18 additions & 0 deletions src/Lib/FFI/CannotCastCDataException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* This file is part of the sj-i/php-profiler package.
*
* (c) sji <sji@sj-i.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace PhpProfiler\Lib\FFI;

final class CannotCastCDataException extends \Exception
{
}
28 changes: 28 additions & 0 deletions src/Lib/FFI/CannotGetTypeForCDataException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* This file is part of the sj-i/php-profiler package.
*
* (c) sji <sji@sj-i.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace PhpProfiler\Lib\FFI;

use Throwable;

final class CannotGetTypeForCDataException extends \Exception
{
public function __construct(
string $message = "",
int $code = 0,
?Throwable $previous = null,
public string $type = '',
) {
parent::__construct($message, $code, $previous);
}
}
18 changes: 18 additions & 0 deletions src/Lib/FFI/CannotLoadCHeaderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* This file is part of the sj-i/php-profiler package.
*
* (c) sji <sji@sj-i.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace PhpProfiler\Lib\FFI;

final class CannotLoadCHeaderException extends \Exception
{
}
4 changes: 3 additions & 1 deletion src/Lib/File/NativeFileReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace PhpProfiler\Lib\File;

use FFI;
use PhpProfiler\Lib\FFI\CannotAllocateBufferException;

class NativeFileReader implements FileReaderInterface
{
Expand All @@ -32,7 +33,8 @@ public function __construct()

public function readAll(string $path): string
{
$buffer = $this->ffi->new("unsigned char[4096]");
$buffer = $this->ffi->new("unsigned char[4096]")
?? throw new CannotAllocateBufferException('cannot allocate buffer');

$fd = $this->ffi->open($path, 0);
$result = "";
Expand Down
14 changes: 10 additions & 4 deletions src/Lib/Libc/Sys/Ptrace/PtraceX64.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use FFI\CData;
use FFI\CInteger;
use PhpProfiler\Lib\FFI\CannotAllocateBufferException;
use PhpProfiler\Lib\FFI\CannotCastCDataException;
use PhpProfiler\Lib\Libc\Addressable;

class PtraceX64 implements Ptrace
Expand Down Expand Up @@ -95,15 +97,19 @@ public function ptrace(
): int {
if (is_null($addr) or is_int($addr)) {
/** @var CInteger */
$addr_holder = \FFI::new('long');
$addr_holder = \FFI::new('long')
?? throw new CannotAllocateBufferException('cannot allocate buffer');
$addr_holder->cdata = (int)$addr;
$addr = \FFI::cast('void *', $addr_holder);
$addr = \FFI::cast('void *', $addr_holder)
?? throw new CannotCastCDataException('cannot cast buffer');
}
if (is_null($data) or is_int($data)) {
/** @var CInteger */
$data_holder = \FFI::new('long');
$data_holder = \FFI::new('long')
?? throw new CannotAllocateBufferException('cannot allocate buffer');
$data_holder->cdata = (int)$data;
$data = \FFI::cast('void *', $data_holder);
$data = \FFI::cast('void *', $data_holder)
?? throw new CannotCastCDataException('cannot cast buffer');
}

$addr_pointer = $addr instanceof Addressable ? $addr->toVoidPointer() : $addr;
Expand Down
13 changes: 10 additions & 3 deletions src/Lib/PhpInternals/ZendTypeReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use FFI;
use FFI\CData;
use PhpProfiler\Lib\FFI\CannotGetTypeForCDataException;
use PhpProfiler\Lib\FFI\CannotLoadCHeaderException;

final class ZendTypeReader
{
Expand Down Expand Up @@ -52,7 +54,8 @@ private function loadHeader(string $php_version): FFI
if ($php_version === self::V81) {
$php_version = self::V80;
}
$this->ffi = FFI::load(__DIR__ . "/Headers/{$php_version}.h");
$this->ffi = FFI::load(__DIR__ . "/Headers/{$php_version}.h")
?? throw new CannotLoadCHeaderException('cannot load headers for zend engine');
}
return $this->ffi;
}
Expand All @@ -66,7 +69,11 @@ public function readAs(string $type, CData $cdata): ZendTypeCData
public function sizeOf(string $type): int
{
$ffi = $this->loadHeader($this->php_version);
$type = $ffi->type($type);
return FFI::sizeof($type);
$cdata_type = $ffi->type($type)
?? throw new CannotGetTypeForCDataException(
message: 'cannot get type for a C Data',
type: $type
);
return FFI::sizeof($cdata_type);
}
}
13 changes: 9 additions & 4 deletions src/Lib/Process/MemoryReader/MemoryReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use FFI;
use FFI\CData;
use PhpProfiler\Lib\FFI\CannotAllocateBufferException;

final class MemoryReader implements MemoryReaderInterface
{
Expand All @@ -39,9 +40,12 @@ public function __construct()
unsigned long riovcnt,
unsigned long flags);
', 'libc.so.6');
$this->local_iov = $this->ffi->new('struct iovec');
$this->remote_iov = $this->ffi->new('struct iovec');
$this->remote_base = $this->ffi->new('long');
$this->local_iov = $this->ffi->new('struct iovec')
?? throw new CannotAllocateBufferException('cannot allocate buffer');
$this->remote_iov = $this->ffi->new('struct iovec')
?? throw new CannotAllocateBufferException('cannot allocate buffer');
$this->remote_base = $this->ffi->new('long')
?? throw new CannotAllocateBufferException('cannot allocate buffer');
}

/**
Expand All @@ -50,7 +54,8 @@ public function __construct()
*/
public function read(int $pid, int $remote_address, int $size): CData
{
$buffer = $this->ffi->new("unsigned char[{$size}]");
$buffer = $this->ffi->new("unsigned char[{$size}]")
?? throw new CannotAllocateBufferException('cannot allocate buffer');

/**
* @var FFI\Libc\iovec $this->local_iov
Expand Down

0 comments on commit f05ad5b

Please sign in to comment.