Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions sapi/cgi/cgi_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2305,17 +2305,23 @@ consult the installation file that came with this distribution, or visit \n\
CG(compiler_options) |= ZEND_COMPILE_EXTENDED_INFO;
break;

case 'f': /* parse file */
if (script_file) {
efree(script_file);
}
script_file = estrdup(php_optarg);
case 'f': { /* parse file */
/* script_file outlives the request: in benchmark mode
* it is reused after php_request_shutdown() has reset
* the memory manager. It is also installed as argv[0],
* which overwrites the argv slot php_optarg refers to,
* so copy before freeing the current buffer. */
char *new_script_file = strdup(php_optarg);

free(script_file);
script_file = new_script_file;
no_headers = 1;
break;
}

case 'i': /* php info & quit */
if (script_file) {
efree(script_file);
free(script_file);
}
if (php_request_startup() == FAILURE) {
SG(server_context) = NULL;
Expand All @@ -2340,7 +2346,7 @@ consult the installation file that came with this distribution, or visit \n\

case 'm': /* list compiled in modules */
if (script_file) {
efree(script_file);
free(script_file);
}
SG(headers_sent) = true;
php_printf("[PHP Modules]\n");
Expand All @@ -2359,7 +2365,7 @@ consult the installation file that came with this distribution, or visit \n\

case 'v': /* show php version & quit */
if (script_file) {
efree(script_file);
free(script_file);
}
no_headers = 1;
if (php_request_startup() == FAILURE) {
Expand Down Expand Up @@ -2389,7 +2395,10 @@ consult the installation file that came with this distribution, or visit \n\
if (script_file) {
/* override path_translated if -f on command line */
if (SG(request_info).path_translated) efree(SG(request_info).path_translated);
SG(request_info).path_translated = script_file;
/* script_file must not be aliased: it would be freed along with
* path_translated at the end of the request and reused as a
* dangling pointer on the next benchmark iteration. */
SG(request_info).path_translated = estrdup(script_file);
/* before registering argv to module exchange the *new* argv[0] */
/* we can achieve this without allocating more memory */
SG(request_info).argc = argc - (php_optind - 1);
Expand Down Expand Up @@ -2595,19 +2604,31 @@ consult the installation file that came with this distribution, or visit \n\
time(&start);
#endif
}
/* When the script was given with -f, script_file is a
* process-lifetime allocation that survives the memory
* manager reset in php_request_shutdown(), so it can be
* reused as is. Otherwise the script was given as a
* positional argument, and the getopt state must be
* reset to re-parse it for the next iteration. */
if (!script_file) {
php_optind = orig_optind;
php_optarg = orig_optarg;
}
continue;
} else {
repeats--;
if (repeats > 0) {
script_file = NULL;
php_optind = orig_optind;
php_optarg = orig_optarg;
if (!script_file) {
php_optind = orig_optind;
php_optarg = orig_optarg;
}
continue;
}
}
}
if (behavior == PHP_MODE_LINT && argc - 1 > php_optind) {
php_optind++;
free(script_file);
script_file = NULL;
goto do_repeat;
}
Expand Down
41 changes: 41 additions & 0 deletions sapi/cgi/tests/benchmark_warmup_repeats.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
Benchmark warmup repeats (-T N,M) must not reuse a freed script path
--ENV--
USE_ZEND_ALLOC=0
--FILE--
<?php
include __DIR__ . "/include.inc";

$php = get_cgi_path();
reset_env_vars();

$script = tempnam(sys_get_temp_dir(), "phpbench") . ".php";
file_put_contents($script, "<?php echo 'run\n';");

function run_benchmark(string $php, string $args): string
{
$out = shell_exec("$php -n $args 2>&1");
/* The elapsed time is written to stderr; drop it so that any leak report
* produced at request shutdown remains visible in the comparison. */
return preg_replace("/\nElapsed time: [0-9.]+ sec\n/", "", $out);
}

/* The script must be executed warmup_repeats + repeats times, each time with
* a valid script path (previously path_translated aliased script_file, so the
* buffer was freed after the first request and reused as a dangling pointer).
* The exact output also catches a leak report at request shutdown. */
$out = run_benchmark($php, "-f " . escapeshellarg($script) . " -T 3,2");
var_dump($out === str_repeat("run\n", 5));

/* The script can also be given as a positional argument, in which case the
* getopt state has to be reset for every benchmark iteration. */
$out = run_benchmark($php, "-q -T 3,2 " . escapeshellarg($script));
var_dump($out === str_repeat("run\n", 5));

unlink($script);
echo "Done\n";
?>
--EXPECT--
bool(true)
bool(true)
Done
Loading