Skip to content

Instantly share code, notes, and snippets.

View Muqsit's full-sized avatar
👑
A beacon of hope.

Muqsit Muqsit

👑
A beacon of hope.
View GitHub Profile
@Muqsit
Muqsit / segfault-debug-php.md
Created July 26, 2025 12:50
Segfault? PHP / PocketMine-MP steps to generate a backtrace on linux

A ssegmentation fault in PHP is often the cause of a PHP extensions written in C, and more specifically one with a memory management error. However, I have encountered my share of segfaults during network programming, but I pair these with PHP's pthreads extension, so it would explain why. It is also possible to encounter segfaults from infinite recursion, but the root cause is still more or less the same - something weird in memory management.

Step 1: Compile PHP binary with debugging symbols

Below I compile with -g flag to enable PHP's GD extension, -t linux64 because I am running on a Ubuntu server, and -P5 to install PM5's dependencies and the right versions for those dependencies. YMMY. See PHP-Binaries for more flags and info.

git clone https://github.com/pmmp/PHP-Binaries.git
cd PHP-Binaries
<?php
declare(strict_types=1);
/**
* @name DispenserInvMenuPlugin
* @main muqsit\dispenserinvmenuplugin\Loader
* @api 5.0.0
* @version 0.0.1
*/
@Muqsit
Muqsit / run.php
Created April 7, 2025 05:10
PHP: Execute a python script with stdin data using proc_open() and print stdout
<?php
declare(strict_types=1);
function run_python(string $path, string $payload) : ?string{
$process = proc_open(["/usr/bin/python3", $path], [
0 => ["pipe", "r"],
1 => ["pipe", "w"],
2 => ["pipe", "w"],
], $pipes);
@Muqsit
Muqsit / nbt-tag-equals-vs-equality-operator.php
Created January 30, 2025 04:47
NBT CompoundTag::equals vs. Equality operator (PHP)
<?php
declare(strict_types=1);
use pocketmine\nbt\BigEndianNbtSerializer;
$n_runs = 100;
// wget https://gist.githubusercontent.com/Muqsit/08391495f934f5c47be0f837ac2907ab/raw/6d36652ca8b557bbaf96115a846ed25a1e45e7c6/inventory.json
$inventory = file_get_contents("/home/cosmicpe/dev/plugin_data/Scripter/inventory.json");
@Muqsit
Muqsit / apply_destructor_hooks.php
Created January 1, 2025 18:32
PHP: Apply destructor hooks to all class in a .phar file to debug memory leaks
<?php
declare(strict_types=1);
function apply_destructor_hook(string $contents) : ?string{
$destructor_body = PHP_EOL;
$destructor_body .= "echo 'Destroyed ', \$this::class,";
$destructor_body .= "' (', \spl_object_id(\$this), ')...',";
$destructor_body .= "\PHP_EOL,";
$destructor_body .= "(new \Exception)->getTraceAsString(),";
This file has been truncated, but you can view the full file.
UEsDBAoAAAAAAOl7nlgAAAAAAAAAAAAAAAAFABwAZGF0YS9VVAkAA3bIMGbfyDBmdXgLAAEEAAAAAAQAAAAAUEsDBBQAAAAIABZ6nlg7m8oxPwMAAJEDAAAKABwAZGF0YS8wLnBuZ1VUCQADC8UwZpLIMGZ1eAsAAQQAAAAABAAAAADrDPBz5+WS4mJgYOD19HAJAtIKIMzBBCT/SLxdzMDAHOHp4hjiwbv1oH2GaLkMw4fzon+Yp3m4VlxQ1byq2MgT5iIisn3KNIHbixY4OTrKiAju2f7HR3wiT53z7jue66On+vmpWnhsX7RokcDj+S87w5pXOB9Rj2RKyt/50KGgar6/mCCbZjdH8xTXFsdTSocUUgUTJ2zqMPCIdMl4nfdvwbcH9kdmCc4PElWtOl7nsOZXULSY4K1ds+1X2P7g35h2ZHmIa+e9jfICq/ddmebasm2VxsXqXyyz3fYd2ih8vNYndMqPj7//WtS/lz9yr/nWpo4LYUBj/OcnXD4RblH2aEf/yi4h+7r98s/f/22+UR0AMnnVIpf3L3W3OrsLRpf9nAE0bneNo+58Nx+5CzDjq7zfXgg8Mi3WbpmW1xe9+IDTywUns29JFVy4VsvrzY193NP3/WWbezl94vIlgaKq++Tkjl0tXn5hplWTjWou0Euru4Sst70/tDR/v+PzL5FPNDOB7l/f3l749Y5mybI+BX9PiCOOvLhh8eyfxeKyHypJSfPdQqe8iN23YFshx9y1k08EX4I4xdDKrG+n/bkVsnadM24sB1pWNV1/Ra6MU/prlU9ToA7q6evNPlO7reXG5vDFQOeu+9N58p1O3v09bXDXLE5+LFU8X1fx25urK7qE9mrXJ22y7r5tvl4R7BSp0MBF5v/N1fj9klN39v/8zQ20/cW9v+LC+r8i9uZxaJ+uPXM/3mHeJDeSlK4AWzvjt2549feot0+VrOznuwIlvj+Uzdn9m0N6342Gq6/bNmy6c/zq1/eCu5dPcGWBhkjd6av39oZV7+w4dx4U
<?php
declare(strict_types=1);
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
use pocketmine\math\VoxelRayTrace;
use pocketmine\player\Player;
use pocketmine\scheduler\CancelTaskException;
@Muqsit
Muqsit / build-pathfinding-mesh.php
Created April 23, 2024 00:53
Pathfinding Block Position Mesh for Arbitrary-Sized Mobs
<?php
declare(strict_types=1);
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
use pocketmine\player\Player;
use pocketmine\scheduler\CancelTaskException;
use pocketmine\scheduler\ClosureTask;
<?php
declare(strict_types=1);
/**
* @name KitEditorPlugin
* @main muqsit\kiteditorplugin\KitEditorPlugin
* @api 5.0.0
* @version 0.0.1
*/
@Muqsit
Muqsit / GeneratorMethodNotYieldingRule.php
Last active April 5, 2024 00:55
PHPStan rule to find unused instantiated generators
<?php
declare(strict_types=1);
namespace pocketmine\phpstan\rules;
use Generator;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Assign;