Skip to content

Commit 2b0d48e

Browse files
authored
Merge pull request #6 from reliforp/claude/test-oom-dump
Add tests/oom_dump.phpt for the memory_limit auto-dump
2 parents 46f7970 + 25cd4b9 commit 2b0d48e

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Initial release.
4646
<file name="dump.phpt" role="test"/>
4747
<file name="file_perms.phpt" role="test"/>
4848
<file name="full_flag.phpt" role="test"/>
49+
<file name="oom_dump.phpt" role="test"/>
4950
<file name="overwrite_inode.phpt" role="test"/>
5051
<file name="safe_read.phpt" role="test"/>
5152
<file name="set_oom_dump.phpt" role="test"/>

tests/oom_dump.phpt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)