Skip to content

Commit 5440100

Browse files
committed
Merge branch 'PHP-7.4'
* PHP-7.4: Allow multiple cache instances per user/host on Windows
2 parents 6d00cff + e2ed7e6 commit 5440100

9 files changed

+62
-51
lines changed

.appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ environment:
3333
PARALLEL: -j2
3434
- THREAD_SAFE: 1
3535
OPCACHE: 1
36-
PARALLEL:
36+
PARALLEL: -j2
3737
INTRINSICS: AVX
3838

3939
services:

ext/opcache/ZendAccelerator.c

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "zend_file_cache.h"
4646
#include "ext/pcre/php_pcre.h"
4747
#include "ext/standard/md5.h"
48+
#include "ext/hash/php_hash.h"
4849

4950
#ifdef HAVE_JIT
5051
# include "jit/zend_jit.h"
@@ -58,6 +59,7 @@
5859
typedef int uid_t;
5960
typedef int gid_t;
6061
#include <io.h>
62+
#include <lmcons.h>
6163
#endif
6264

6365
#ifndef ZEND_WIN32
@@ -101,6 +103,9 @@ zend_accel_shared_globals *accel_shared_globals = NULL;
101103

102104
/* true globals, no need for thread safety */
103105
char accel_system_id[32];
106+
#ifdef ZEND_WIN32
107+
char accel_uname_id[32];
108+
#endif
104109
zend_bool accel_startup_ok = 0;
105110
static char *zps_failure_reason = NULL;
106111
char *zps_api_failure_reason = NULL;
@@ -2171,6 +2176,26 @@ zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type)
21712176
return zend_accel_load_script(persistent_script, from_shared_memory);
21722177
}
21732178

2179+
#ifdef ZEND_WIN32
2180+
static int accel_gen_uname_id(void)
2181+
{
2182+
PHP_MD5_CTX ctx;
2183+
unsigned char digest[16];
2184+
wchar_t uname[UNLEN + 1];
2185+
DWORD unsize = UNLEN;
2186+
2187+
if (!GetUserNameW(uname, &unsize)) {
2188+
return FAILURE;
2189+
}
2190+
PHP_MD5Init(&ctx);
2191+
PHP_MD5Update(&ctx, (void *) uname, (unsize - 1) * sizeof(wchar_t));
2192+
PHP_MD5Update(&ctx, ZCG(accel_directives).cache_id, strlen(ZCG(accel_directives).cache_id));
2193+
PHP_MD5Final(digest, &ctx);
2194+
php_hash_bin2hex(accel_uname_id, digest, sizeof digest);
2195+
return SUCCESS;
2196+
}
2197+
#endif
2198+
21742199
/* zend_stream_open_function() replacement for PHP 5.3 and above */
21752200
static int persistent_stream_open_function(const char *filename, zend_file_handle *handle)
21762201
{
@@ -2612,9 +2637,7 @@ static void accel_globals_ctor(zend_accel_globals *accel_globals)
26122637
static void accel_gen_system_id(void)
26132638
{
26142639
PHP_MD5_CTX context;
2615-
unsigned char digest[16], c;
2616-
char *md5str = accel_system_id;
2617-
int i;
2640+
unsigned char digest[16];
26182641

26192642
PHP_MD5Init(&context);
26202643
PHP_MD5Update(&context, PHP_VERSION, sizeof(PHP_VERSION)-1);
@@ -2626,14 +2649,7 @@ static void accel_gen_system_id(void)
26262649
PHP_MD5Update(&context, __TIME__, sizeof(__TIME__)-1);
26272650
}
26282651
PHP_MD5Final(digest, &context);
2629-
for (i = 0; i < 16; i++) {
2630-
c = digest[i] >> 4;
2631-
c = (c <= 9) ? c + '0' : c - 10 + 'a';
2632-
md5str[i * 2] = c;
2633-
c = digest[i] & 0x0f;
2634-
c = (c <= 9) ? c + '0' : c - 10 + 'a';
2635-
md5str[(i * 2) + 1] = c;
2636-
}
2652+
php_hash_bin2hex(accel_system_id, digest, sizeof digest);
26372653
}
26382654

26392655
#ifdef HAVE_HUGE_CODE_PAGES
@@ -2824,6 +2840,13 @@ static int accel_startup(zend_extension *extension)
28242840
return FAILURE;
28252841
}
28262842

2843+
#ifdef ZEND_WIN32
2844+
if (UNEXPECTED(accel_gen_uname_id() == FAILURE)) {
2845+
zps_startup_failure("Unable to get user name", NULL, accelerator_remove_cb);
2846+
return SUCCESS;
2847+
}
2848+
#endif
2849+
28272850
#ifdef HAVE_HUGE_CODE_PAGES
28282851
if (ZCG(accel_directives).huge_code_pages &&
28292852
(strcmp(sapi_module.name, "cli") == 0 ||

ext/opcache/ZendAccelerator.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ typedef struct _zend_accel_directives {
185185
zend_bool huge_code_pages;
186186
#endif
187187
char *preload;
188+
#ifdef ZEND_WIN32
189+
char *cache_id;
190+
#endif
188191
#ifdef HAVE_JIT
189192
zend_long jit;
190193
zend_long jit_buffer_size;
@@ -278,6 +281,9 @@ typedef struct _zend_accel_shared_globals {
278281
} zend_accel_shared_globals;
279282

280283
extern char accel_system_id[32];
284+
#ifdef ZEND_WIN32
285+
extern char accel_uname_id[32];
286+
#endif
281287
extern zend_bool accel_startup_ok;
282288
extern zend_bool file_cache_only;
283289
#if ENABLE_FILE_CACHE_FALLBACK

ext/opcache/shared_alloc_win32.c

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
+----------------------------------------------------------------------+
2020
*/
2121

22+
#include "php.h"
2223
#include "ZendAccelerator.h"
2324
#include "zend_shared_alloc.h"
2425
#include "zend_accelerator_util_funcs.h"
@@ -67,38 +68,24 @@ static void zend_win_error_message(int type, char *msg, int err)
6768

6869
static char *create_name_with_username(char *name)
6970
{
70-
static char newname[MAXPATHLEN + UNLEN + 4 + 1 + 32];
71-
char *uname;
72-
73-
uname = php_win32_get_username();
74-
if (!uname) {
75-
return NULL;
76-
}
77-
snprintf(newname, sizeof(newname) - 1, "%s@%s@%.32s", name, uname, accel_system_id);
78-
79-
free(uname);
71+
static char newname[MAXPATHLEN + 32 + 4 + 1 + 32];
72+
snprintf(newname, sizeof(newname) - 1, "%s@%.32s@%.32s", name, accel_uname_id, accel_system_id);
8073

8174
return newname;
8275
}
8376

8477
static char *get_mmap_base_file(void)
8578
{
86-
static char windir[MAXPATHLEN+UNLEN + 3 + sizeof("\\\\@") + 1 + 32];
87-
char *uname;
79+
static char windir[MAXPATHLEN+ 32 + 3 + sizeof("\\\\@") + 1 + 32];
8880
int l;
8981

90-
uname = php_win32_get_username();
91-
if (!uname) {
92-
return NULL;
93-
}
9482
GetTempPath(MAXPATHLEN, windir);
9583
l = strlen(windir);
9684
if ('\\' == windir[l-1]) {
9785
l--;
9886
}
99-
snprintf(windir + l, sizeof(windir) - l - 1, "\\%s@%s@%.32s", ACCEL_FILEMAP_BASE, uname, accel_system_id);
10087

101-
free(uname);
88+
snprintf(windir + l, sizeof(windir) - l - 1, "\\%s@%.32s@%.32s", ACCEL_FILEMAP_BASE, accel_uname_id, accel_system_id);
10289

10390
return windir;
10491
}

ext/opcache/zend_accelerator_module.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ ZEND_INI_BEGIN()
325325
STD_PHP_INI_BOOLEAN("opcache.huge_code_pages" , "0" , PHP_INI_SYSTEM, OnUpdateBool, accel_directives.huge_code_pages, zend_accel_globals, accel_globals)
326326
#endif
327327
STD_PHP_INI_ENTRY("opcache.preload" , "" , PHP_INI_SYSTEM, OnUpdateStringUnempty, accel_directives.preload, zend_accel_globals, accel_globals)
328+
#if ZEND_WIN32
329+
STD_PHP_INI_ENTRY("opcache.cache_id" , "" , PHP_INI_SYSTEM, OnUpdateString, accel_directives.cache_id, zend_accel_globals, accel_globals)
330+
#endif
328331
#ifdef HAVE_JIT
329332
STD_PHP_INI_ENTRY("opcache.jit" , ZEND_JIT_DEFAULT, PHP_INI_SYSTEM, OnUpdateLong, accel_directives.jit, zend_accel_globals, accel_globals)
330333
STD_PHP_INI_ENTRY("opcache.jit_buffer_size" , "0" , PHP_INI_SYSTEM, OnUpdateLong, accel_directives.jit_buffer_size, zend_accel_globals, accel_globals)
@@ -789,6 +792,9 @@ static ZEND_FUNCTION(opcache_get_configuration)
789792
add_assoc_bool(&directives, "opcache.huge_code_pages", ZCG(accel_directives).huge_code_pages);
790793
#endif
791794
add_assoc_string(&directives, "opcache.preload", STRING_NOT_NULL(ZCG(accel_directives).preload));
795+
#if ZEND_WIN32
796+
add_assoc_string(&directives, "opcache.cache_id", STRING_NOT_NULL(ZCG(accel_directives).cache_id));
797+
#endif
792798
#ifdef HAVE_JIT
793799
add_assoc_long(&directives, "opcache.jit", ZCG(accel_directives).jit);
794800
add_assoc_long(&directives, "opcache.jit_buffer_size", ZCG(accel_directives).jit_buffer_size);

ext/opcache/zend_file_cache.c

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -835,31 +835,13 @@ static char *zend_file_cache_get_bin_file_path(zend_string *script_path)
835835
memcpy(filename + len + 33, ZSTR_VAL(script_path), ZSTR_LEN(script_path));
836836
memcpy(filename + len + 33 + ZSTR_LEN(script_path), SUFFIX, sizeof(SUFFIX));
837837
#else
838-
PHP_MD5_CTX ctx;
839-
char md5uname[32];
840-
unsigned char digest[16], c;
841-
size_t i;
842-
char *uname = php_win32_get_username();
843-
844-
PHP_MD5Init(&ctx);
845-
PHP_MD5Update(&ctx, uname, strlen(uname));
846-
PHP_MD5Final(digest, &ctx);
847-
for (i = 0; i < 16; i++) {
848-
c = digest[i] >> 4;
849-
c = (c <= 9) ? c + '0' : c - 10 + 'a';
850-
md5uname[i * 2] = c;
851-
c = digest[i] & 0x0f;
852-
c = (c <= 9) ? c + '0' : c - 10 + 'a';
853-
md5uname[(i * 2) + 1] = c;
854-
}
855-
856838
len = strlen(ZCG(accel_directives).file_cache);
857839

858840
filename = emalloc(len + 33 + 33 + ZSTR_LEN(script_path) + sizeof(SUFFIX));
859841

860842
memcpy(filename, ZCG(accel_directives).file_cache, len);
861843
filename[len] = '\\';
862-
memcpy(filename + 1 + len, md5uname, 32);
844+
memcpy(filename + 1 + len, accel_uname_id, 32);
863845
len += 1 + 32;
864846
filename[len] = '\\';
865847

@@ -889,7 +871,6 @@ static char *zend_file_cache_get_bin_file_path(zend_string *script_path)
889871
memcpy(filename + len + 33, ZSTR_VAL(script_path), ZSTR_LEN(script_path));
890872
memcpy(filename + len + 33 + ZSTR_LEN(script_path), SUFFIX, sizeof(SUFFIX));
891873
}
892-
free(uname);
893874
#endif
894875

895876
return filename;

php.ini-development

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,10 @@ ldap.max_links = -1
18231823
; errors.
18241824
;opcache.mmap_base=
18251825

1826+
; Facilitates multiple OPcache instances per user (for Windows only). All PHP
1827+
; processes with the same cache ID and user share an OPcache instance.
1828+
;opcache.cache_id=
1829+
18261830
; Enables and sets the second level cache directory.
18271831
; It should improve performance when SHM memory is full, at server restart or
18281832
; SHM reset. The default "" disables file based caching.

php.ini-production

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,6 +1825,10 @@ ldap.max_links = -1
18251825
; errors.
18261826
;opcache.mmap_base=
18271827

1828+
; Facilitates multiple OPcache instances per user (for Windows only). All PHP
1829+
; processes with the same cache ID and user share an OPcache instance.
1830+
;opcache.cache_id=
1831+
18281832
; Enables and sets the second level cache directory.
18291833
; It should improve performance when SHM memory is full, at server restart or
18301834
; SHM reset. The default "" disables file based caching.

run-tests.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2076,7 +2076,7 @@ function run_test($php, $file, $env)
20762076
}
20772077

20782078
// Default ini settings
2079-
$ini_settings = array();
2079+
$ini_settings = $workerID ? array('opcache.cache_id' => "worker$workerID") : array();
20802080

20812081
// Additional required extensions
20822082
if (array_key_exists('EXTENSIONS', $section_text)) {

0 commit comments

Comments
 (0)