Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
49 changes: 48 additions & 1 deletion TSRM/tsrm_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,37 @@ static void tsrm_win32_dtor(tsrm_win32_globals *globals)
}
}/*}}}*/

/**
* Converts Windows GetLastError() codes to POSIX errno values
* @param win32_error
*/
static void tsrm_set_errno_from_win32_error(const DWORD win32_error)
{/*{{{*/
switch (win32_error) {
case ERROR_ACCESS_DENIED:
errno = EACCES;
break;
case ERROR_NOT_ENOUGH_MEMORY:
case ERROR_OUTOFMEMORY:
errno = ENOMEM;
break;
case ERROR_INVALID_PARAMETER:
case ERROR_INVALID_HANDLE:
errno = EINVAL;
break;
case ERROR_ALREADY_EXISTS:
errno = EEXIST;
break;
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
errno = ENOENT;
break;
default:
errno = EINVAL;
break;
}
}/*}}}*/

TSRM_API void tsrm_win32_startup(void)
{/*{{{*/
#ifdef ZTS
Expand Down Expand Up @@ -651,6 +682,7 @@ TSRM_API int shmget(key_t key, size_t size, int flags)
if (!shm_handle) {
if (flags & IPC_CREAT) {
if (size == 0 || size > SIZE_MAX - sizeof(shm->descriptor)) {
errno = EINVAL;
return -1;
}
size += sizeof(shm->descriptor);
Expand All @@ -665,11 +697,13 @@ TSRM_API int shmget(key_t key, size_t size, int flags)
created = TRUE;
}
if (!shm_handle) {
tsrm_set_errno_from_win32_error(GetLastError());
return -1;
}
} else {
if (flags & IPC_EXCL) {
CloseHandle(shm_handle);
errno = EEXIST;
return -1;
}
}
Expand All @@ -690,6 +724,7 @@ TSRM_API int shmget(key_t key, size_t size, int flags)
shm = shm_get(key, NULL);
if (!shm) {
CloseHandle(shm_handle);
errno = ENOMEM;
return -1;
}
shm->segment = shm_handle;
Expand All @@ -716,6 +751,7 @@ TSRM_API int shmget(key_t key, size_t size, int flags)
}
UnmapViewOfFile(shm->descriptor);
shm->descriptor = NULL;
errno = EINVAL;
return -1;
}

Expand Down Expand Up @@ -744,6 +780,7 @@ TSRM_API int shmdt(const void *shmaddr)
int ret;

if (!shm || !shm->segment) {
errno = EINVAL;
return -1;
}

Expand All @@ -753,7 +790,10 @@ TSRM_API int shmdt(const void *shmaddr)

ret = 0;
if (shm->descriptor->shm_nattch <= 0) {
ret = UnmapViewOfFile(shm->descriptor) ? 0 : -1;
if (!UnmapViewOfFile(shm->descriptor)) {
tsrm_set_errno_from_win32_error(GetLastError());
ret = -1;
}
shm->descriptor = NULL;
}
return ret;
Expand All @@ -764,6 +804,7 @@ TSRM_API int shmctl(int key, int cmd, struct shmid_ds *buf)
shm_pair *shm = shm_get(key, NULL);

if (!shm || !shm->segment) {
errno = EINVAL;
return -1;
}

Expand All @@ -782,10 +823,16 @@ TSRM_API int shmctl(int key, int cmd, struct shmid_ds *buf)
case IPC_RMID:
if (shm->descriptor->shm_nattch < 1) {
shm->descriptor->shm_perm.key = -1;
/* Close handle to allow Windows to destroy the named mapping object */
if (shm->segment && shm->segment != INVALID_HANDLE_VALUE) {
CloseHandle(shm->segment);
shm->segment = INVALID_HANDLE_VALUE;
}
}
return 0;

default:
errno = EINVAL;
return -1;
}
}/*}}}*/
Expand Down
2 changes: 1 addition & 1 deletion Zend/zend.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#ifndef ZEND_H
#define ZEND_H

#define ZEND_VERSION "4.5.1-dev"
#define ZEND_VERSION "4.5.1"

#define ZEND_ENGINE_3

Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dnl Basic autoconf initialization, generation of config.nice.
dnl ----------------------------------------------------------------------------

AC_PREREQ([2.68])
AC_INIT([PHP],[8.5.1-dev],[https://github.com/php/php-src/issues],[php],[https://www.php.net])
AC_INIT([PHP],[8.5.1],[https://github.com/php/php-src/issues],[php],[https://www.php.net])
AC_CONFIG_SRCDIR([main/php_version.h])
AC_CONFIG_AUX_DIR([build])
AC_PRESERVE_HELP_ORDER
Expand Down
21 changes: 21 additions & 0 deletions ext/shmop/tests/shmop_errno_codes_win32.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
shmop errno codes on Windows
--EXTENSIONS--
shmop
--SKIPIF--
<?php
if (PHP_OS_FAMILY !== "Windows") die("skip for Windows only");
?>
--FILE--
<?php
$key = 0xABCDEF01;
$shm1 = shmop_open($key, 'n', 0644, 1024);
if ($shm1 !== false) {
$shm2 = shmop_open($key, 'n', 0644, 1024);
shmop_delete($shm1);
}
echo "done\n";
?>
--EXPECTF--
Warning: shmop_open(): Unable to attach or create shared memory segment "File exists" in %s on line %d
done
40 changes: 40 additions & 0 deletions ext/shmop/tests/shmop_errno_mapping_win32.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
shmop Windows errno mapping
--EXTENSIONS--
shmop
--SKIPIF--
<?php
if (PHP_OS_FAMILY !== "Windows") die("skip for Windows only");
?>
--FILE--
<?php
echo "EEXIST test:\n";
$key = 0xEEEEEEEE;
$shm1 = shmop_open($key, 'n', 0644, 512);
if ($shm1 !== false) {
$shm2 = shmop_open($key, 'n', 0644, 512);
shmop_delete($shm1);
}

echo "\nENOENT attach test:\n";
$shm = shmop_open(0x99999999, 'a', 0644, 0);

echo "\nENOENT write test:\n";
$shm = shmop_open(0x77777777, 'w', 0644, 0);

echo "\ndone\n";
?>
--EXPECTF--
EEXIST test:

Warning: shmop_open(): Unable to attach or create shared memory segment "File exists" in %s on line %d

ENOENT attach test:

Warning: shmop_open(): Unable to attach or create shared memory segment "No such file or directory" in %s on line %d

ENOENT write test:

Warning: shmop_open(): Unable to attach or create shared memory segment "No such file or directory" in %s on line %d

done
65 changes: 65 additions & 0 deletions ext/shmop/tests/shmop_errno_tests.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
--TEST--
shmop errno handling tests
--EXTENSIONS--
shmop
--SKIPIF--
<?php
if (PHP_OS_FAMILY !== "Windows") die("skip for Windows only");
?>
--FILE--
<?php
$key = 0x12345678;
$size = 1024;

echo "create segment: ";
$shm1 = shmop_open($key, 'n', 0644, $size);
if ($shm1 === false) {
die("failed\n");
}
echo "ok\n";

echo "duplicate with IPC_EXCL: ";
$shm2 = shmop_open($key, 'n', 0644, $size);
if ($shm2 === false) {
echo "ok\n";
} else {
echo "failed\n";
shmop_delete($shm2);
}
shmop_delete($shm1);

echo "attach non-existent: ";
$shm = shmop_open(0x99999999, 'a', 0644, 0);
echo ($shm === false) ? "ok\n" : "failed\n";

echo "write mode non-existent: ";
$shm = shmop_open(0x88888888, 'w', 0644, 0);
echo ($shm === false) ? "ok\n" : "failed\n";

echo "create/attach/write sequence: ";
$key = 0x45678901;
$shm_c = shmop_open($key, 'c', 0644, 512);
$shm_a = shmop_open($key, 'a', 0644, 0);
$shm_w = shmop_open($key, 'w', 0644, 0);
if ($shm_c && $shm_a && $shm_w) {
$data = "test";
$written = shmop_write($shm_w, $data, 0);
$read = shmop_read($shm_a, 0, strlen($data));
echo ($read === $data) ? "ok\n" : "failed\n";
shmop_delete($shm_c);
} else {
echo "failed\n";
}
?>
--EXPECTF--
create segment: ok
duplicate with IPC_EXCL:
Warning: shmop_open(): Unable to attach or create shared memory segment%s in %s on line %d
ok
attach non-existent:
Warning: shmop_open(): Unable to attach or create shared memory segment%s in %s on line %d
ok
write mode non-existent:
Warning: shmop_open(): Unable to attach or create shared memory segment%s in %s on line %d
ok
create/attach/write sequence: ok
82 changes: 82 additions & 0 deletions ext/shmop/tests/shmop_error_conditions_win32.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
--TEST--
shmop error conditions on Windows
--EXTENSIONS--
shmop
--SKIPIF--
<?php
if (PHP_OS_FAMILY !== "Windows") die("skip for Windows only");
?>
--FILE--
<?php
echo "duplicate segment: ";
$key = 0xFEEDBEEF;
$shm1 = shmop_open($key, 'n', 0644, 1024);
$shm2 = shmop_open($key, 'n', 0644, 1024);
echo ($shm1 && !$shm2) ? "ok\n" : "failed\n";
shmop_delete($shm1);

echo "non-existent attach: ";
$shm = shmop_open(0xDEADC0DE, 'a', 0644, 0);
echo ($shm === false) ? "ok\n" : "failed\n";

echo "non-existent write: ";
$shm = shmop_open(0xBADF00D, 'w', 0644, 0);
echo ($shm === false) ? "ok\n" : "failed\n";

echo "zero size: ";
try {
shmop_open(0x12340000, 'n', 0644, 0);
echo "failed\n";
} catch (ValueError $e) {
echo "ok\n";
}

echo "write/read test: ";
$key = 0xC0FFEE00;
$shm_c = shmop_open($key, 'c', 0644, 1024);
$shm_w = shmop_open($key, 'w', 0644, 0);
$shm_a = shmop_open($key, 'a', 0644, 0);
if ($shm_c && $shm_w && $shm_a) {
$data = "test data";
$written = shmop_write($shm_w, $data, 0);
$read = shmop_read($shm_a, 0, strlen($data));
echo ($read === $data) ? "ok\n" : "failed\n";
shmop_delete($shm_c);
} else {
echo "failed\n";
}

echo "multiple segments: ";
$segments = [];
for ($i = 0; $i < 3; $i++) {
$shm = shmop_open(0xA0000000 + $i, 'c', 0644, 256);
if ($shm) $segments[] = $shm;
}
echo (count($segments) === 3) ? "ok\n" : "failed\n";
foreach ($segments as $shm) {
shmop_delete($shm);
}

echo "large segment: ";
$shm = shmop_open(0xBEEF0000, 'n', 0644, 1024 * 1024);
if ($shm) {
echo (shmop_size($shm) >= 1024 * 1024) ? "ok\n" : "failed\n";
shmop_delete($shm);
} else {
echo "failed\n";
}
?>
--EXPECTF--
duplicate segment:
Warning: shmop_open(): Unable to attach or create shared memory segment%s in %s on line %d
ok
non-existent attach:
Warning: shmop_open(): Unable to attach or create shared memory segment%s in %s on line %d
ok
non-existent write:
Warning: shmop_open(): Unable to attach or create shared memory segment%s in %s on line %d
ok
zero size: ok
write/read test: ok
multiple segments: ok
large segment: ok
4 changes: 2 additions & 2 deletions main/php_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
#define PHP_MAJOR_VERSION 8
#define PHP_MINOR_VERSION 5
#define PHP_RELEASE_VERSION 1
#define PHP_EXTRA_VERSION "-dev"
#define PHP_VERSION "8.5.1-dev"
#define PHP_EXTRA_VERSION ""
#define PHP_VERSION "8.5.1"
#define PHP_VERSION_ID 80501
Loading