-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix GH-17951: Addition of max_memory_limit INI #18011
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 6 commits
9341c6e
25c4028
2d61446
9e42b57
da70576
72f4df4
ffe7913
1d49976
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -317,11 +317,39 @@ static PHP_INI_MH(OnSetSerializePrecision) | |
static PHP_INI_MH(OnChangeMemoryLimit) | ||
{ | ||
size_t value; | ||
|
||
if (new_value) { | ||
value = zend_ini_parse_uquantity_warn(new_value, entry->name); | ||
} else { | ||
value = Z_L(1)<<30; /* effectively, no limit */ | ||
value = Z_L(1) << 30; /* effectively, no limit */ | ||
} | ||
|
||
/* If max_memory_limit is not set to unlimited, verify change */ | ||
if (PG(max_memory_limit) != -1) { | ||
if (value == -1) { | ||
zend_error( | ||
stage == ZEND_INI_STAGE_STARTUP ? E_ERROR : E_WARNING, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at this again, I think it would be better to stick to a warning in all cases. Erroring is very harsh, especially on startup, taking down the entire website. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll change all cases to a warning :) |
||
"Failed to set memory_limit to unlimited. memory_limit (currently: " ZEND_LONG_FMT " bytes) cannot be set to unlimited if max_memory_limit (" ZEND_LONG_FMT " bytes) is not unlimited", | ||
PG(memory_limit), | ||
PG(max_memory_limit) | ||
); | ||
|
||
return FAILURE; | ||
} | ||
|
||
if (value > PG(max_memory_limit)) { | ||
zend_error( | ||
stage == ZEND_INI_STAGE_STARTUP ? E_ERROR : E_WARNING, | ||
"Failed to set memory_limit to %zd bytes. memory_limit (currently: " ZEND_LONG_FMT " bytes) cannot exceed max_memory_limit (" ZEND_LONG_FMT " bytes)", | ||
value, | ||
PG(memory_limit), | ||
PG(max_memory_limit) | ||
); | ||
|
||
return FAILURE; | ||
} | ||
} | ||
|
||
if (zend_set_memory_limit(value) == FAILURE) { | ||
/* When the memory limit is reset to the original level during deactivation, we may be | ||
* using more memory than the original limit while shutdown is still in progress. | ||
|
@@ -332,7 +360,30 @@ static PHP_INI_MH(OnChangeMemoryLimit) | |
return FAILURE; | ||
} | ||
} | ||
|
||
iluuu1994 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PG(memory_limit) = value; | ||
return SUCCESS; | ||
} | ||
/* }}} */ | ||
|
||
/* {{{ PHP_INI_MH */ | ||
iluuu1994 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
static PHP_INI_MH(OnChangeMaxMemoryLimit) | ||
{ | ||
size_t value; | ||
if (new_value) { | ||
value = zend_ini_parse_uquantity_warn(new_value, entry->name); | ||
} else { | ||
value = Z_L(1) << 30; /* effectively, no limit */ | ||
} | ||
|
||
if (zend_set_memory_limit(value) == FAILURE) { | ||
zend_error(E_ERROR, "Failed to set memory limit to %zd bytes (Current memory usage is %zd bytes)", value, zend_memory_usage(true)); | ||
return FAILURE; | ||
} | ||
|
||
PG(memory_limit) = value; | ||
PG(max_memory_limit) = value; | ||
|
||
return SUCCESS; | ||
} | ||
/* }}} */ | ||
|
@@ -800,7 +851,10 @@ PHP_INI_BEGIN() | |
STD_PHP_INI_BOOLEAN("mail.mixed_lf_and_crlf", "0", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateBool, mail_mixed_lf_and_crlf, php_core_globals, core_globals) | ||
STD_PHP_INI_ENTRY("mail.log", NULL, PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateMailLog, mail_log, php_core_globals, core_globals) | ||
PHP_INI_ENTRY("browscap", NULL, PHP_INI_SYSTEM, OnChangeBrowscap) | ||
PHP_INI_ENTRY("memory_limit", "128M", PHP_INI_ALL, OnChangeMemoryLimit) | ||
|
||
PHP_INI_ENTRY("max_memory_limit", "-1", PHP_INI_SYSTEM, OnChangeMaxMemoryLimit) | ||
PHP_INI_ENTRY("memory_limit", "128M", PHP_INI_ALL, OnChangeMemoryLimit) | ||
|
||
PHP_INI_ENTRY("precision", "14", PHP_INI_ALL, OnSetPrecision) | ||
PHP_INI_ENTRY("sendmail_from", NULL, PHP_INI_ALL, NULL) | ||
PHP_INI_ENTRY("sendmail_path", DEFAULT_SENDMAIL_PATH, PHP_INI_SYSTEM, NULL) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--TEST-- | ||
GH-17951 INI Parse 1 | ||
--CREDITS-- | ||
Frederik Milling Pytlick | ||
[email protected] | ||
--INI-- | ||
memory_limit=128M | ||
max_memory_limit=-1 | ||
--FILE-- | ||
<?php | ||
echo ini_get('max_memory_limit') . PHP_EOL; | ||
echo ini_get('memory_limit') . PHP_EOL; | ||
--EXPECT-- | ||
-1 | ||
128M |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--TEST-- | ||
GH-17951 INI Parse 2 | ||
--CREDITS-- | ||
Frederik Milling Pytlick | ||
[email protected] | ||
--INI-- | ||
memory_limit=-1 | ||
max_memory_limit=-1 | ||
--FILE-- | ||
<?php | ||
echo ini_get('max_memory_limit') . PHP_EOL; | ||
echo ini_get('memory_limit') . PHP_EOL; | ||
--EXPECT-- | ||
-1 | ||
-1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--TEST-- | ||
GH-17951 INI Parse 3 | ||
--CREDITS-- | ||
Frederik Milling Pytlick | ||
[email protected] | ||
--INI-- | ||
memory_limit=128M | ||
max_memory_limit=256M | ||
--FILE-- | ||
<?php | ||
echo ini_get('max_memory_limit') . PHP_EOL; | ||
echo ini_get('memory_limit') . PHP_EOL; | ||
--EXPECT-- | ||
256M | ||
128M |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--TEST-- | ||
GH-17951 INI Parse 4 | ||
--CREDITS-- | ||
Frederik Milling Pytlick | ||
[email protected] | ||
--INI-- | ||
memory_limit=-1 | ||
max_memory_limit=128M | ||
--FILE-- | ||
<?php | ||
echo ini_get('max_memory_limit') . PHP_EOL; | ||
echo ini_get('memory_limit') . PHP_EOL; | ||
--EXPECTF-- | ||
Fatal error: Failed to set memory_limit to unlimited. memory_limit (currently: %d bytes) cannot be set to unlimited if max_memory_limit (%d bytes) is not unlimited in %s | ||
128M | ||
128M |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--TEST-- | ||
GH-17951 INI Parse 5 | ||
--CREDITS-- | ||
Frederik Milling Pytlick | ||
[email protected] | ||
--INI-- | ||
memory_limit=256M | ||
max_memory_limit=128M | ||
--FILE-- | ||
<?php | ||
echo ini_get('max_memory_limit') . PHP_EOL; | ||
echo ini_get('memory_limit') . PHP_EOL; | ||
--EXPECTF-- | ||
Fatal error: Failed to set memory_limit to %d bytes. memory_limit (currently: %d bytes) cannot exceed max_memory_limit (%d bytes) in %s | ||
128M | ||
128M |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--TEST-- | ||
GH-17951 Runtime Change 1 | ||
--CREDITS-- | ||
Frederik Milling Pytlick | ||
[email protected] | ||
--INI-- | ||
memory_limit=128M | ||
max_memory_limit=512M | ||
--FILE-- | ||
<?php | ||
ini_set('memory_limit', '256M'); | ||
echo ini_get('memory_limit') . PHP_EOL . PHP_EOL; | ||
--EXPECT-- | ||
256M |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--TEST-- | ||
GH-17951 Runtime Change 2 | ||
--CREDITS-- | ||
Frederik Milling Pytlick | ||
[email protected] | ||
--INI-- | ||
memory_limit=128M | ||
max_memory_limit=512M | ||
--FILE-- | ||
<?php | ||
ini_set('memory_limit', '512M'); | ||
echo ini_get('memory_limit') . PHP_EOL . PHP_EOL; | ||
--EXPECT-- | ||
512M |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--TEST-- | ||
GH-17951 Runtime Change 3 | ||
--CREDITS-- | ||
Frederik Milling Pytlick | ||
[email protected] | ||
--INI-- | ||
memory_limit=128M | ||
max_memory_limit=512M | ||
--FILE-- | ||
<?php | ||
ini_set('memory_limit', '1024M'); | ||
echo ini_get('memory_limit'); | ||
--EXPECTF-- | ||
Warning: Failed to set memory_limit to %d bytes. memory_limit (currently: %d bytes) cannot exceed max_memory_limit (%d bytes) in %s | ||
128M |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--TEST-- | ||
GH-17951 Runtime Change 4 | ||
--CREDITS-- | ||
Frederik Milling Pytlick | ||
[email protected] | ||
--INI-- | ||
memory_limit=128M | ||
max_memory_limit=512M | ||
--FILE-- | ||
<?php | ||
ini_set('memory_limit', '-1'); | ||
echo ini_get('memory_limit'); | ||
--EXPECTF-- | ||
Warning: Failed to set memory_limit to unlimited. memory_limit (currently: %d bytes) cannot be set to unlimited if max_memory_limit (%d bytes) is not unlimited in %s | ||
128M |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--TEST-- | ||
GH-17951 Runtime Change 5 | ||
--CREDITS-- | ||
Frederik Milling Pytlick | ||
[email protected] | ||
--INI-- | ||
memory_limit=128M | ||
max_memory_limit=512M | ||
--FILE-- | ||
<?php | ||
var_dump(ini_set('max_memory_limit', '128M')); | ||
var_dump(ini_set('max_memory_limit', '256M')); | ||
var_dump(ini_set('max_memory_limit', '512M')); | ||
var_dump(ini_set('max_memory_limit', '-1')); | ||
--EXPECT-- | ||
bool(false) | ||
bool(false) | ||
bool(false) | ||
bool(false) |
Uh oh!
There was an error while loading. Please reload this page.