Skip to content

Commit

Permalink
add getPointer() to Dereferencable
Browse files Browse the repository at this point in the history
Some internal structures like zend_object or zend_string uses C struct hack heavily. And the implementation of the memory profiler often needs to know the remote address and it's size of structures. So it's convenient if we can always get the original remote address and size used to dereference the objects.
  • Loading branch information
sj-i committed Nov 12, 2023
1 parent f4b6099 commit 9cc472e
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 42 deletions.
8 changes: 7 additions & 1 deletion src/Lib/PhpInternals/Types/C/RawDouble.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ final class RawDouble implements Dereferencable
/** @param CastedCData<CInteger> $casted_cdata */
public function __construct(
private CastedCData $casted_cdata,
private Pointer $pointer,
) {
$this->value = $this->casted_cdata->casted->cdata;
}
Expand All @@ -39,6 +40,11 @@ public static function fromCastedCData(
Pointer $pointer
): static {
/** @var CastedCData<CInteger> $casted_cdata */
return new self($casted_cdata);
return new self($casted_cdata, $pointer);
}

public function getPointer(): Pointer
{
return $this->pointer;
}
}
8 changes: 7 additions & 1 deletion src/Lib/PhpInternals/Types/C/RawInt32.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ final class RawInt32 implements Dereferencable
/** @param CastedCData<CInteger> $casted_cdata */
public function __construct(
private CastedCData $casted_cdata,
private Pointer $pointer,
) {
$this->value = $this->casted_cdata->casted->cdata;
}
Expand All @@ -39,6 +40,11 @@ public static function fromCastedCData(
Pointer $pointer
): static {
/** @var CastedCData<CInteger> $casted_cdata */
return new self($casted_cdata);
return new self($casted_cdata, $pointer);
}

public function getPointer(): Pointer
{
return $this->pointer;
}
}
8 changes: 7 additions & 1 deletion src/Lib/PhpInternals/Types/C/RawString.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class RawString implements Dereferencable
public function __construct(
private CastedCData $cdata,
private int $len,
private Pointer $pointer,
) {
unset($this->value);
}
Expand Down Expand Up @@ -57,6 +58,11 @@ public static function fromCastedCData(
CastedCData $casted_cdata,
Pointer $pointer
): static {
return new self($casted_cdata, $pointer->size);
return new self($casted_cdata, $pointer->size, $pointer);
}

public function getPointer(): Pointer
{
return $this->pointer;
}
}
8 changes: 7 additions & 1 deletion src/Lib/PhpInternals/Types/Php/SapiGlobalsStruct.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ final class SapiGlobalsStruct implements Dereferencable
/** @param CastedCData<sapi_globals_struct> $casted_cdata */
public function __construct(
private CastedCData $casted_cdata,
private Pointer $pointer,
) {
unset($this->global_request_time);
}
Expand All @@ -45,6 +46,11 @@ public static function getCTypeName(): string
public static function fromCastedCData(CastedCData $casted_cdata, Pointer $pointer): static
{
/** @var CastedCData<sapi_globals_struct> $casted_cdata */
return new self($casted_cdata);
return new self($casted_cdata, $pointer);
}

public function getPointer(): Pointer
{
return $this->pointer;
}
}
21 changes: 17 additions & 4 deletions src/Lib/PhpInternals/Types/Zend/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ final class Bucket implements Dereferencable
*/
public Pointer $key;

/** @param CastedCData<ZendBucket> $casted_cdata */
/**
* @param CastedCData<\FFI\PhpInternals\Bucket> $casted_cdata
* @param Pointer<Bucket> $pointer
*/
public function __construct(
private CastedCData $casted_cdata,
protected CastedCData $casted_cdata,
protected Pointer $pointer,
) {
unset($this->val);
unset($this->h);
Expand Down Expand Up @@ -60,7 +64,16 @@ public static function fromCastedCData(
CastedCData $casted_cdata,
Pointer $pointer
): static {
/** @var CastedCData<ZendBucket> $casted_cdata */
return new self($casted_cdata);
/**
* @var CastedCData<\FFI\PhpInternals\Bucket> $casted_cdata
* @var Pointer<Bucket> $pointer
*/
return new static($casted_cdata, $pointer);
}

/** @return Pointer<Bucket> */
public function getPointer(): Pointer
{
return $this->pointer;
}
}
23 changes: 18 additions & 5 deletions src/Lib/PhpInternals/Types/Zend/ZendArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
* zend_long nNextFreeElement;
* dtor_func_t pDestructor;
* }; */
final class ZendArray implements Dereferencable
/** @psalm-consistent-constructor */
class ZendArray implements Dereferencable
{
/** @psalm-suppress PropertyNotSetInConstructor */
public int $flags;
Expand All @@ -64,9 +65,13 @@ final class ZendArray implements Dereferencable
/** @psalm-suppress PropertyNotSetInConstructor */
public int $nNextFreeElement;

/** @param CastedCData<zend_array> $casted_cdata */
/**
* @param CastedCData<zend_array> $casted_cdata
* @param Pointer<ZendArray> $pointer,
*/
public function __construct(
private CastedCData $casted_cdata
protected CastedCData $casted_cdata,
protected Pointer $pointer,
) {
unset($this->flags);
unset($this->nTableMask);
Expand Down Expand Up @@ -158,7 +163,15 @@ public static function getCTypeName(): string

public static function fromCastedCData(CastedCData $casted_cdata, Pointer $pointer): static
{
/** @var CastedCData<zend_array> $casted_cdata */
return new self($casted_cdata);
/**
* @var CastedCData<zend_array> $casted_cdata
* @var Pointer<ZendArray> $pointer
*/
return new static($casted_cdata, $pointer);
}

public function getPointer(): Pointer
{
return $this->pointer;
}
}
18 changes: 15 additions & 3 deletions src/Lib/PhpInternals/Types/Zend/ZendClassEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ final class ZendClassEntry implements Dereferencable
*/
public Pointer $name;

/** @param CastedCData<zend_class_entry> $casted_cdata */
/**
* @param CastedCData<zend_class_entry> $casted_cdata
* @param Pointer<ZendClassEntry> $pointer
*/
public function __construct(
private CastedCData $casted_cdata,
private Pointer $pointer,
) {
unset($this->name);
}
Expand All @@ -53,8 +57,16 @@ public static function fromCastedCData(
CastedCData $casted_cdata,
Pointer $pointer
): static {
/** @var CastedCData<zend_class_entry> $casted_cdata */
return new self($casted_cdata);
/**
* @var CastedCData<zend_class_entry> $casted_cdata
* @var Pointer<ZendClassEntry> $pointer
*/
return new self($casted_cdata, $pointer);
}

public function getPointer(): Pointer
{
return $this->pointer;
}

public function getClassName(Dereferencer $dereferencer): string
Expand Down
19 changes: 16 additions & 3 deletions src/Lib/PhpInternals/Types/Zend/ZendExecuteData.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ final class ZendExecuteData implements Dereferencable
/** @var Pointer<ZendOp>|null */
public ?Pointer $opline;

/** @param CastedCData<zend_execute_data> $casted_cdata */
/**
* @param CastedCData<zend_execute_data> $casted_cdata
* @param Pointer<ZendExecuteData> $pointer
*/
public function __construct(
private CastedCData $casted_cdata,
private Pointer $pointer,
) {
unset($this->func);
unset($this->prev_execute_data);
Expand Down Expand Up @@ -78,8 +82,17 @@ public static function fromCastedCData(
CastedCData $casted_cdata,
Pointer $pointer
): static {
/** @var CastedCData<zend_execute_data> $casted_cdata */
return new self($casted_cdata);
/**
* @var CastedCData<zend_execute_data> $casted_cdata
* @var Pointer<ZendExecuteData> $pointer
*/
return new self($casted_cdata, $pointer);
}

/** @return Pointer<ZendExecuteData> */
public function getPointer(): Pointer
{
return $this->pointer;
}

public function getFunctionName(Dereferencer $dereferencer): ?string
Expand Down
19 changes: 16 additions & 3 deletions src/Lib/PhpInternals/Types/Zend/ZendExecutorGlobals.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ final class ZendExecutorGlobals implements Dereferencable
/** @var Pointer<ZendExecuteData>|null */
public ?Pointer $current_execute_data;

/** @param CastedCData<zend_executor_globals> $casted_cdata */
/**
* @param CastedCData<zend_executor_globals> $casted_cdata
* @param Pointer<ZendExecutorGlobals> $pointer
*/
public function __construct(
private CastedCData $casted_cdata,
private Pointer $pointer,
) {
unset($this->current_execute_data);
}
Expand All @@ -52,7 +56,16 @@ public static function fromCastedCData(
CastedCData $casted_cdata,
Pointer $pointer
): static {
/** @var CastedCData<zend_executor_globals> $casted_cdata */
return new self($casted_cdata);
/**
* @var CastedCData<zend_executor_globals> $casted_cdata
* @var Pointer<ZendExecutorGlobals> $pointer
*/
return new self($casted_cdata, $pointer);
}

/** @return Pointer<ZendExecutorGlobals> */
public function getPointer(): Pointer
{
return $this->pointer;
}
}
15 changes: 13 additions & 2 deletions src/Lib/PhpInternals/Types/Zend/ZendFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ final class ZendFunction implements Dereferencable

/**
* @param CastedCData<zend_function> $casted_cdata
* @param Pointer<ZendFunction> $pointer
*/
public function __construct(
private CastedCData $casted_cdata,
private Pointer $pointer,
) {
unset($this->type);
unset($this->function_name);
Expand Down Expand Up @@ -84,8 +86,17 @@ public static function fromCastedCData(
CastedCData $casted_cdata,
Pointer $pointer
): static {
/** @var CastedCData<zend_function> $casted_cdata */
return new self($casted_cdata);
/**
* @var CastedCData<zend_function> $casted_cdata
* @var Pointer<ZendFunction> $pointer
*/
return new static($casted_cdata, $pointer);
}

/** @return Pointer<ZendFunction> */
public function getPointer(): Pointer
{
return $this->pointer;
}

public function getFullyQualifiedFunctionName(Dereferencer $dereferencer): string
Expand Down
18 changes: 15 additions & 3 deletions src/Lib/PhpInternals/Types/Zend/ZendModuleEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ final class ZendModuleEntry implements Dereferencable
*/
public Pointer $version;

/** @param CastedCData<zend_module_entry> $casted_cdata */
/**
* @param CastedCData<zend_module_entry> $casted_cdata
* @param Pointer<ZendModuleEntry> $pointer
*/
public function __construct(
private CastedCData $casted_cdata,
private Pointer $pointer,
) {
unset($this->zts);
unset($this->version);
Expand Down Expand Up @@ -65,7 +69,15 @@ public static function fromCastedCData(
CastedCData $casted_cdata,
Pointer $pointer
): static {
/** @var CastedCData<zend_module_entry> $casted_cdata */
return new self($casted_cdata);
/**
* @var CastedCData<zend_module_entry> $casted_cdata
* @var Pointer<ZendModuleEntry> $pointer
*/
return new self($casted_cdata, $pointer);
}

public function getPointer(): Pointer
{
return $this->pointer;
}
}
18 changes: 15 additions & 3 deletions src/Lib/PhpInternals/Types/Zend/ZendOp.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ final class ZendOp implements Dereferencable
/** @psalm-suppress PropertyNotSetInConstructor */
public int $extended_value;

/** @param CastedCData<zend_op> $casted_cdata */
/**
* @param CastedCData<zend_op> $casted_cdata
* @param Pointer<ZendOp> $pointer
*/
public function __construct(
private CastedCData $casted_cdata,
private Pointer $pointer,
) {
unset($this->op1);
unset($this->op2);
Expand Down Expand Up @@ -79,7 +83,15 @@ public static function fromCastedCData(
CastedCData $casted_cdata,
Pointer $pointer
): static {
/** @var CastedCData<zend_op> $casted_cdata */
return new self($casted_cdata);
/**
* @var CastedCData<zend_op> $casted_cdata
* @var Pointer<ZendOp> $pointer
*/
return new self($casted_cdata, $pointer);
}

public function getPointer(): Pointer
{
return $this->pointer;
}
}
Loading

0 comments on commit 9cc472e

Please sign in to comment.