Skip to content

Windows C shmget(IPC_PRIVATE, ...) returns IPC_PRIVATE(0) instead of distinct id for new section #9944

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
TysonAndre opened this issue Nov 13, 2022 · 0 comments

Comments

@TysonAndre
Copy link
Contributor

TysonAndre commented Nov 13, 2022

Description

The following code:

https://github.com/php/php-src/blob/php-8.2.0RC6/TSRM/tsrm_win32.c#L613-L683

        size_t size = 8192;
	key_t key = IPC_PRIVATE;	/* shm key */

	oflag = IPC_CREAT | 0666;
	if ((shmid = shmget(key, size, oflag)) < 0) {
		zend_error_noreturn(E_CORE_ERROR, "shmget failed");
	}
	fprintf(stderr, "shmid %d\n", shmid);

Resulted in this output because nothing sets key for IPC_PRIVATE:

shmid 0

But I expected this output instead:

shmid $distinct_nonzero_integer

If code calls shmget(IPC_PRIVATE) multiple times on Windows and gets IPC_PRIVATE as a result, applications would need distinct ids to distinguish them in operations managing the shared memory segments

Noticed while investigating krakjoe/apcu#445 when using more than one shared segment

		if (flags & IPC_CREAT) {
			shm_handle	= CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, high, low, key == IPC_PRIVATE ? NULL : shm_segment);

It seems like it should immediately choose a distinct id if key == IPC_PRIVATE - it'd be clobbering the existing entry with the key 0, which would prevent detaching segments other than the most recent one created with IPC_PRIVATE

https://github.com/php/php-src/blob/php-8.2.0RC6/TSRM/tsrm_win32.c#L652-L657


https://man7.org/linux/man-pages/man2/shmget.2.html

RETURN VALUE         [top](https://man7.org/linux/man-pages/man2/shmget.2.html#top_of_page)
       On success, a valid shared memory identifier is returned.  On
       error, -1 is returned, and [errno](https://man7.org/linux/man-pages/man3/errno.3.html) is set to indicate the error.

DESCRIPTION
       shmget() returns the identifier of the System V shared memory
       segment associated with the value of the argument key.  It may be
       used either to obtain the identifier of a previously created
       shared memory segment (when shmflg is zero and key does not have
       the value IPC_PRIVATE), or to create a new set.

       A new shared memory segment, with size equal to the value of size
       rounded up to a multiple of PAGE_SIZE, is created if key has the
       value IPC_PRIVATE or key isn't IPC_PRIVATE, no shared memory
       segment corresponding to key exists, and IPC_CREAT is specified
       in shmflg.

PHP Version

8.2. Possibly goes back to 7.4 for https://bugs.php.net/bug.php?id=79566 ?

Operating System

Windows

TysonAndre added a commit to TysonAndre/php-src that referenced this issue Nov 13, 2022
Closes php#9944

https://man7.org/linux/man-pages/man2/shmget.2.html notes

   The name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW
   would more clearly show its function.
TysonAndre added a commit to TysonAndre/php-src that referenced this issue Nov 13, 2022
Closes php#9944

https://man7.org/linux/man-pages/man2/shmget.2.html notes

   The name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW
   would more clearly show its function.
TysonAndre added a commit to TysonAndre/php-src that referenced this issue Nov 13, 2022
Closes php#9944

https://man7.org/linux/man-pages/man2/shmget.2.html notes

   The name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW
   would more clearly show its function.
TysonAndre added a commit to TysonAndre/php-src that referenced this issue Nov 13, 2022
Closes php#9944

https://man7.org/linux/man-pages/man2/shmget.2.html notes

   The name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW
   would more clearly show its function.
TysonAndre added a commit to TysonAndre/php-src that referenced this issue Nov 13, 2022
Closes php#9944

https://man7.org/linux/man-pages/man2/shmget.2.html notes

   The name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW
   would more clearly show its function.
@cmb69 cmb69 self-assigned this Nov 15, 2022
@cmb69 cmb69 linked a pull request Nov 15, 2022 that will close this issue
@cmb69 cmb69 closed this as completed in 7a983e2 Dec 13, 2022
cmb69 added a commit that referenced this issue Dec 13, 2022
The recently committed fix for GH-9944 did only indirectly cater to
that, namely because in this case `CreateFileMapping()` with a zero
size couldn't be created.  As of PHP 8.2.0, the mappings of the actual
SHM and the info segment have been merged, so creating a zero size SHM
would be possible unless we explicitly prohibit this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants