-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix OPcache memory protection race under ZTS #23081
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
Open
realFlowControl
wants to merge
2
commits into
php:PHP-8.4
Choose a base branch
from
realFlowControl:florian/fix-opcache-protect-memory-zts-race
base: PHP-8.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+40
−7
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -55,6 +55,11 @@ static const char *g_shared_model; | |
| /* pointer to globals allocated in SHM and shared across processes */ | ||
| ZEND_EXT_API zend_smm_shared_globals *smm_shared_globals; | ||
|
|
||
| #ifdef ZTS | ||
| static MUTEX_T zts_protect_lock; | ||
| static uint32_t zts_unprotected_threads; | ||
| #endif | ||
|
|
||
| #ifndef ZEND_WIN32 | ||
| #ifdef ZTS | ||
| static MUTEX_T zts_lock; | ||
|
|
@@ -184,6 +189,11 @@ int zend_shared_alloc_startup(size_t requested_size, size_t reserved_size) | |
| int res = ALLOC_FAILURE; | ||
| int i; | ||
|
|
||
| #ifdef ZTS | ||
| zts_protect_lock = tsrm_mutex_alloc(); | ||
| zts_unprotected_threads = 0; | ||
| #endif | ||
|
|
||
| /* shared_free must be valid before we call zend_shared_alloc() | ||
| * - make it temporarily point to a local variable | ||
| */ | ||
|
|
@@ -338,6 +348,9 @@ void zend_shared_alloc_shutdown(void) | |
| tsrm_mutex_free(zts_lock); | ||
| # endif | ||
| #endif | ||
| #ifdef ZTS | ||
| tsrm_mutex_free(zts_protect_lock); | ||
| #endif | ||
| } | ||
|
|
||
| static size_t zend_shared_alloc_get_largest_free_block(void) | ||
|
|
@@ -625,25 +638,37 @@ const char *zend_accel_get_shared_model(void) | |
|
|
||
| void zend_accel_shared_protect(bool protected) | ||
| { | ||
| #ifdef HAVE_MPROTECT | ||
| #if defined(HAVE_MPROTECT) || defined(ZEND_WIN32) | ||
| int i; | ||
|
|
||
| if (!smm_shared_globals) { | ||
| return; | ||
| } | ||
|
|
||
| #ifdef ZTS | ||
| /* Memory protection is process-wide, so overlapping writers must be tracked across threads. */ | ||
| tsrm_mutex_lock(zts_protect_lock); | ||
| if (protected) { | ||
| if (ZCG(unprotect_depth) && --ZCG(unprotect_depth) == 0) { | ||
| ZEND_ASSERT(zts_unprotected_threads > 0); | ||
| zts_unprotected_threads--; | ||
| } | ||
| if (zts_unprotected_threads) { | ||
| tsrm_mutex_unlock(zts_protect_lock); | ||
| return; | ||
| } | ||
| } else if (ZCG(unprotect_depth)++ == 0) { | ||
| zts_unprotected_threads++; | ||
| } | ||
| #endif | ||
|
|
||
| #ifdef HAVE_MPROTECT | ||
| const int mode = protected ? PROT_READ : PROT_READ|PROT_WRITE; | ||
|
|
||
| for (i = 0; i < ZSMMG(shared_segments_count); i++) { | ||
| mprotect(ZSMMG(shared_segments)[i]->p, ZSMMG(shared_segments)[i]->end, mode); | ||
| } | ||
| #elif defined(ZEND_WIN32) | ||
| int i; | ||
|
|
||
| if (!smm_shared_globals) { | ||
| return; | ||
| } | ||
|
|
||
| const int mode = protected ? PAGE_READONLY : PAGE_READWRITE; | ||
|
|
||
| for (i = 0; i < ZSMMG(shared_segments_count); i++) { | ||
|
|
@@ -653,6 +678,11 @@ void zend_accel_shared_protect(bool protected) | |
| } | ||
| } | ||
| #endif | ||
|
|
||
| #ifdef ZTS | ||
|
Member
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. Ditto |
||
| tsrm_mutex_unlock(zts_protect_lock); | ||
| #endif | ||
| #endif | ||
| } | ||
|
|
||
| bool zend_accel_in_shm(void *ptr) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please indent nested preprocessor directives: