-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix GH-13071: Copying large files using mmap-able source streams may …
…exhaust available memory and fail Commit 5cbe5a5 disabled chunking for all writes to streams. However, user streams have a callback where code is executed on data that is subject to the memory limit. Therefore, when using large writes or stream_copy_to_stream/copy the memory limit can easily be hit with large enough data. To solve this, we reintroduce chunking for userspace streams. Users have control over the chunk size, which is neat because they can improve the performance by setting the chunk size if that turns out to be a bottleneck. In an ideal world, we add an option so we can "ask" the stream whether it "prefers" chunked writes, similar to how we have php_stream_mmap_supported & friends. However, that cannot be done on stable branches. Closes GH-13136.
- Loading branch information
Showing
6 changed files
with
80 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--TEST-- | ||
GH-13071 (Copying large files using mmap-able source streams may exhaust available memory and fail) | ||
--FILE-- | ||
<?php | ||
|
||
class CustomStream { | ||
public $context; | ||
protected $file; | ||
protected $seekable; | ||
public static int $writes = 0; | ||
|
||
public function stream_open($path, $mode, $options, &$opened_path) { | ||
$path = $this->trim_path($path); | ||
$this->file = fopen($path, $mode); | ||
return true; | ||
} | ||
|
||
public function stream_close() { | ||
fclose($this->file); | ||
return true; | ||
} | ||
|
||
public function stream_write($data) { | ||
self::$writes++; | ||
return fwrite($this->file, $data); | ||
} | ||
|
||
public function url_stat($path, $flags) { | ||
return false; | ||
} | ||
|
||
private function trim_path(string $path): string { | ||
return substr($path, strlen("up://")); | ||
} | ||
} | ||
|
||
file_put_contents(__DIR__ . "/gh13071.tmp", str_repeat("a", 1024 * 1024 * 8)); | ||
|
||
stream_wrapper_register("up", CustomStream::class, STREAM_IS_URL); | ||
|
||
$old_limit = ini_get("memory_limit"); | ||
ini_set("memory_limit", memory_get_usage(true) + 5 * 1024 * 1024); | ||
copy(__DIR__ . "/gh13071.tmp", "up://" . __DIR__ . "/gh13071.out.tmp"); | ||
ini_set("memory_limit", $old_limit); | ||
|
||
echo "Done ", CustomStream::$writes, " writes\n"; | ||
|
||
?> | ||
--CLEAN-- | ||
<?php | ||
@unlink(__DIR__ . "/gh13071.tmp"); | ||
@unlink(__DIR__ . "/gh13071.out.tmp"); | ||
?> | ||
--EXPECT-- | ||
Done 1024 writes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,5 +34,6 @@ bool(true) | |
option: 3, 2, 50 | ||
int(-1) | ||
int(8192) | ||
size: 70 | ||
size: 42 | ||
size: 28 | ||
int(70) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,4 +39,5 @@ option: %d, %d, %d | |
int(%i) | ||
int(%d) | ||
size: %d | ||
size: 28 | ||
int(%d) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters