Skip to content

Commit da0f327

Browse files
committed
Merge branch 'PHP-7.4'
* PHP-7.4: Revert "Merge branch 'PHP-7.3' into PHP-7.4"
2 parents 687e14d + 826b90a commit da0f327

File tree

1 file changed

+58
-13
lines changed

1 file changed

+58
-13
lines changed

ext/opcache/shared_alloc_win32.c

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@
3333

3434
#define ACCEL_FILEMAP_NAME "ZendOPcache.SharedMemoryArea"
3535
#define ACCEL_MUTEX_NAME "ZendOPcache.SharedMemoryMutex"
36+
#define ACCEL_FILEMAP_BASE_DEFAULT 0x01000000
37+
#define ACCEL_FILEMAP_BASE "ZendOPcache.MemoryBase"
3638
#define ACCEL_EVENT_SOURCE "Zend OPcache"
3739

38-
/* address of mapping base and address of execute_ex */
39-
#define ACCEL_BASE_POINTER_SIZE (2 * sizeof(void*))
40-
4140
static HANDLE memfile = NULL, memory_mutex = NULL;
4241
static void *mapping_base;
4342

@@ -76,6 +75,22 @@ static char *create_name_with_username(char *name)
7675
return newname;
7776
}
7877

78+
static char *get_mmap_base_file(void)
79+
{
80+
static char windir[MAXPATHLEN+ 32 + 3 + sizeof("\\\\@") + 1 + 32 + 21];
81+
int l;
82+
83+
GetTempPath(MAXPATHLEN, windir);
84+
l = strlen(windir);
85+
if ('\\' == windir[l-1]) {
86+
l--;
87+
}
88+
89+
snprintf(windir + l, sizeof(windir) - l - 1, "\\%s@%.32s@%.20s@%.32s", ACCEL_FILEMAP_BASE, accel_uname_id, sapi_module.name, accel_system_id);
90+
91+
return windir;
92+
}
93+
7994
void zend_shared_alloc_create_lock(void)
8095
{
8196
memory_mutex = CreateMutex(NULL, FALSE, create_name_with_username(ACCEL_MUTEX_NAME));
@@ -104,20 +119,39 @@ static int zend_shared_alloc_reattach(size_t requested_size, char **error_in)
104119
{
105120
int err;
106121
void *wanted_mapping_base;
122+
char *mmap_base_file = get_mmap_base_file();
123+
FILE *fp = fopen(mmap_base_file, "r");
107124
MEMORY_BASIC_INFORMATION info;
108125
void *execute_ex_base;
109126
int execute_ex_moved;
110127

111-
mapping_base = MapViewOfFileEx(memfile, FILE_MAP_ALL_ACCESS, 0, 0, ACCEL_BASE_POINTER_SIZE, NULL);
112-
if (mapping_base == NULL) {
128+
if (!fp) {
129+
err = GetLastError();
130+
zend_win_error_message(ACCEL_LOG_WARNING, mmap_base_file, err);
131+
zend_win_error_message(ACCEL_LOG_FATAL, "Unable to open base address file", err);
132+
*error_in="fopen";
133+
return ALLOC_FAILURE;
134+
}
135+
if (!fscanf(fp, "%p", &wanted_mapping_base)) {
113136
err = GetLastError();
114137
zend_win_error_message(ACCEL_LOG_FATAL, "Unable to read base address", err);
115138
*error_in="read mapping base";
139+
fclose(fp);
116140
return ALLOC_FAILURE;
117141
}
118-
wanted_mapping_base = ((void**)mapping_base)[0];
119-
execute_ex_base = ((void**)mapping_base)[1];
120-
UnmapViewOfFile(mapping_base);
142+
if (!fscanf(fp, "%p", &execute_ex_base)) {
143+
err = GetLastError();
144+
zend_win_error_message(ACCEL_LOG_FATAL, "Unable to read execute_ex base address", err);
145+
*error_in="read execute_ex base";
146+
fclose(fp);
147+
return ALLOC_FAILURE;
148+
}
149+
fclose(fp);
150+
151+
if (0 > win32_utime(mmap_base_file, NULL)) {
152+
err = GetLastError();
153+
zend_win_error_message(ACCEL_LOG_WARNING, mmap_base_file, err);
154+
}
121155

122156
execute_ex_moved = (void *)execute_ex != execute_ex_base;
123157

@@ -182,7 +216,7 @@ static int zend_shared_alloc_reattach(size_t requested_size, char **error_in)
182216
}
183217
}
184218

185-
smm_shared_globals = (zend_smm_shared_globals *) ((char*)mapping_base + ACCEL_BASE_POINTER_SIZE);
219+
smm_shared_globals = (zend_smm_shared_globals *) mapping_base;
186220

187221
return SUCCESSFULLY_REATTACHED;
188222
}
@@ -300,18 +334,29 @@ static int create_segments(size_t requested_size, zend_shared_segment ***shared_
300334
*error_in = "MapViewOfFile";
301335
return ALLOC_FAILURE;
302336
} else {
337+
char *mmap_base_file;
338+
void *execute_ex_base = (void *)execute_ex;
339+
FILE *fp;
303340
DWORD old;
304341

305342
if (!VirtualProtect(mapping_base, requested_size, PAGE_READWRITE, &old)) {
306343
err = GetLastError();
307344
zend_win_error_message(ACCEL_LOG_FATAL, "VirtualProtect() failed", err);
308345
return ALLOC_FAILURE;
309346
}
310-
311-
((void**)mapping_base)[0] = mapping_base;
312-
((void**)mapping_base)[1] = (void*)execute_ex;
313-
((char*)shared_segment->p) += ACCEL_BASE_POINTER_SIZE;
347+
mmap_base_file = get_mmap_base_file();
348+
fp = fopen(mmap_base_file, "w");
349+
if (!fp) {
350+
err = GetLastError();
351+
zend_shared_alloc_unlock_win32();
352+
zend_win_error_message(ACCEL_LOG_WARNING, mmap_base_file, err);
353+
zend_win_error_message(ACCEL_LOG_FATAL, "Unable to write base address", err);
354+
return ALLOC_FAILURE;
314355
}
356+
fprintf(fp, "%p\n", mapping_base);
357+
fprintf(fp, "%p\n", execute_ex_base);
358+
fclose(fp);
359+
}
315360

316361
shared_segment->pos = 0;
317362
shared_segment->size = requested_size;

0 commit comments

Comments
 (0)