Skip to content

Fix the replacement for shmget in Windows #9946

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

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions TSRM/tsrm_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,9 @@ TSRM_API int shmget(key_t key, size_t size, int flags)

shm_handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, shm_segment);
info_handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, shm_info);
} else {
/* IPC_PRIVATE always creates a new segment even if IPC_CREAT flag isn't passed. */
flags |= IPC_CREAT;
}

if (!shm_handle && !info_handle) {
Expand Down Expand Up @@ -662,6 +665,19 @@ TSRM_API int shmget(key_t key, size_t size, int flags)
}
}

if (key == IPC_PRIVATE) {
/* This should call shm_get with a brand new key id that isn't used yet. See https://man7.org/linux/man-pages/man2/shmget.2.html
* Because shmop_open can be used in userland to attach to shared memory segments, use high positive numbers to avoid accidentally conflicting with userland. */
key = (php_rand() & 0x3fffffff) + 0x40000000;
for (shm_pair *ptr = TWG(shm); ptr < (TWG(shm) + TWG(shm_size)); ptr++) {
if (ptr->descriptor && ptr->descriptor->shm_perm.key == key) {
key = (php_rand() & 0x3fffffff) + 0x40000000;
ptr = TWG(shm);
continue;
}
}
}

shm = shm_get(key, NULL);
if (!shm) {
CloseHandle(shm_handle);
Expand Down