|
| 1 | +--TEST-- |
| 2 | +rdump.oom_dump writes a well-formed RDUMP dump when memory_limit is exhausted |
| 3 | +--SKIPIF-- |
| 4 | +<?php |
| 5 | +if (!extension_loaded("rdump")) die("skip rdump not loaded"); |
| 6 | +if (PHP_OS_FAMILY !== "Linux") die("skip Linux only"); |
| 7 | +if (!is_file(dirname(__DIR__) . "/modules/rdump.so")) die("skip built modules/rdump.so not found"); |
| 8 | +if (!function_exists("shell_exec") || !shell_exec("echo ok")) die("skip shell_exec disabled"); |
| 9 | +?> |
| 10 | +--FILE-- |
| 11 | +<?php |
| 12 | +// A memory_limit error is fatal, so it can only be observed from a separate |
| 13 | +// process: spawn a child that exhausts memory with rdump.oom_dump set, then |
| 14 | +// verify from here that the extension auto-dumped at the moment of death. |
| 15 | +$so = dirname(__DIR__) . "/modules/rdump.so"; |
| 16 | +$php = getenv("TEST_PHP_EXECUTABLE"); |
| 17 | +if ($php === false || $php === "") { |
| 18 | + $php = PHP_BINARY; |
| 19 | +} |
| 20 | + |
| 21 | +$dump = sys_get_temp_dir() . "/rdump_oom_" . getmypid() . ".rdump"; |
| 22 | +$child = sys_get_temp_dir() . "/rdump_oom_child_" . getmypid() . ".php"; |
| 23 | +@unlink($dump); |
| 24 | +@unlink($dump . ".done"); |
| 25 | +file_put_contents($child, '<?php $a = []; while (true) { $a[] = str_repeat("x", 4096); }'); |
| 26 | + |
| 27 | +$cmd = escapeshellarg($php) |
| 28 | + . " -n" |
| 29 | + . " -d extension=" . escapeshellarg($so) |
| 30 | + . " -d memory_limit=16M" |
| 31 | + . " -d rdump.oom_dump=" . escapeshellarg($dump) |
| 32 | + . " -d rdump.oom_dump_marker=1" |
| 33 | + . " " . escapeshellarg($child) |
| 34 | + . " 2>&1"; |
| 35 | +$output = (string) shell_exec($cmd); |
| 36 | + |
| 37 | +// The child died on the memory_limit error... |
| 38 | +var_dump(strpos($output, "Allowed memory size") !== false); |
| 39 | + |
| 40 | +// ...and the OOM hook wrote the dump without any rdump_dump() call. |
| 41 | +var_dump(is_file($dump)); |
| 42 | + |
| 43 | +// The dump is a well-formed RDUMP file. |
| 44 | +var_dump(file_get_contents($dump, false, null, 0, 8) === "RDUMP\0\0\0"); |
| 45 | + |
| 46 | +// The completion marker was written (rdump.oom_dump_marker=1). |
| 47 | +var_dump(is_file($dump . ".done")); |
| 48 | + |
| 49 | +@unlink($dump); |
| 50 | +@unlink($dump . ".done"); |
| 51 | +@unlink($child); |
| 52 | +?> |
| 53 | +--EXPECT-- |
| 54 | +bool(true) |
| 55 | +bool(true) |
| 56 | +bool(true) |
| 57 | +bool(true) |
0 commit comments