Skip to content

Commit 9a744c6

Browse files
committed
Fix #73060: php failed with error after temp folder cleaned up
Instead of storing the mapping base address and the address of `execute_ex()` in a separate file in the temporary folder, we store them right at the beginning of the memory mapping.
1 parent c756f82 commit 9a744c6

File tree

2 files changed

+16
-62
lines changed

2 files changed

+16
-62
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ PHP NEWS
1010
- LDAP:
1111
. Fixed memory leaks. (ptomulik)
1212

13+
- OPcache:
14+
. Fixed bug #73060 (php failed with error after temp folder cleaned up).
15+
(cmb)
16+
1317
?? ??? ????, PHP 7.3.21
1418

1519
- Apache:

ext/opcache/shared_alloc_win32.c

Lines changed: 12 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@
3131

3232
#define ACCEL_FILEMAP_NAME "ZendOPcache.SharedMemoryArea"
3333
#define ACCEL_MUTEX_NAME "ZendOPcache.SharedMemoryMutex"
34-
#define ACCEL_FILEMAP_BASE_DEFAULT 0x01000000
35-
#define ACCEL_FILEMAP_BASE "ZendOPcache.MemoryBase"
3634
#define ACCEL_EVENT_SOURCE "Zend OPcache"
3735

36+
/* address of mapping base and address of execute_ex */
37+
#define ACCEL_BASE_POINTER_SIZE (2 * sizeof(void*))
38+
3839
static HANDLE memfile = NULL, memory_mutex = NULL;
3940
static void *mapping_base;
4041

@@ -93,28 +94,6 @@ static char *create_name_with_username(char *name)
9394
return newname;
9495
}
9596

96-
static char *get_mmap_base_file(void)
97-
{
98-
static char windir[MAXPATHLEN+UNLEN + 3 + sizeof("\\\\@") + 1 + 32 + 21];
99-
char *uname;
100-
int l;
101-
102-
uname = php_win32_get_username();
103-
if (!uname) {
104-
return NULL;
105-
}
106-
GetTempPath(MAXPATHLEN, windir);
107-
l = strlen(windir);
108-
if ('\\' == windir[l-1]) {
109-
l--;
110-
}
111-
snprintf(windir + l, sizeof(windir) - l - 1, "\\%s@%s@%.20s@%.32s", ACCEL_FILEMAP_BASE, uname, sapi_module.name, ZCG(system_id));
112-
113-
free(uname);
114-
115-
return windir;
116-
}
117-
11897
void zend_shared_alloc_create_lock(void)
11998
{
12099
memory_mutex = CreateMutex(NULL, FALSE, create_name_with_username(ACCEL_MUTEX_NAME));
@@ -143,39 +122,20 @@ static int zend_shared_alloc_reattach(size_t requested_size, char **error_in)
143122
{
144123
int err;
145124
void *wanted_mapping_base;
146-
char *mmap_base_file = get_mmap_base_file();
147-
FILE *fp = fopen(mmap_base_file, "r");
148125
MEMORY_BASIC_INFORMATION info;
149126
void *execute_ex_base;
150127
int execute_ex_moved;
151128

152-
if (!fp) {
153-
err = GetLastError();
154-
zend_win_error_message(ACCEL_LOG_WARNING, mmap_base_file, err);
155-
zend_win_error_message(ACCEL_LOG_FATAL, "Unable to open base address file", err);
156-
*error_in="fopen";
157-
return ALLOC_FAILURE;
158-
}
159-
if (!fscanf(fp, "%p", &wanted_mapping_base)) {
129+
mapping_base = MapViewOfFileEx(memfile, FILE_MAP_ALL_ACCESS, 0, 0, ACCEL_BASE_POINTER_SIZE, NULL);
130+
if (mapping_base == NULL) {
160131
err = GetLastError();
161132
zend_win_error_message(ACCEL_LOG_FATAL, "Unable to read base address", err);
162133
*error_in="read mapping base";
163-
fclose(fp);
164-
return ALLOC_FAILURE;
165-
}
166-
if (!fscanf(fp, "%p", &execute_ex_base)) {
167-
err = GetLastError();
168-
zend_win_error_message(ACCEL_LOG_FATAL, "Unable to read execute_ex base address", err);
169-
*error_in="read execute_ex base";
170-
fclose(fp);
171134
return ALLOC_FAILURE;
172135
}
173-
fclose(fp);
174-
175-
if (0 > win32_utime(mmap_base_file, NULL)) {
176-
err = GetLastError();
177-
zend_win_error_message(ACCEL_LOG_WARNING, mmap_base_file, err);
178-
}
136+
wanted_mapping_base = ((void**)mapping_base)[0];
137+
execute_ex_base = ((void**)mapping_base)[1];
138+
UnmapViewOfFile(mapping_base);
179139

180140
execute_ex_moved = (void *)execute_ex != execute_ex_base;
181141

@@ -231,7 +191,7 @@ static int zend_shared_alloc_reattach(size_t requested_size, char **error_in)
231191
}
232192
return ALLOC_FAIL_MAPPING;
233193
}
234-
smm_shared_globals = (zend_smm_shared_globals *) mapping_base;
194+
smm_shared_globals = (zend_smm_shared_globals *) ((char*)mapping_base + ACCEL_BASE_POINTER_SIZE);
235195

236196
return SUCCESSFULLY_REATTACHED;
237197
}
@@ -349,19 +309,9 @@ static int create_segments(size_t requested_size, zend_shared_segment ***shared_
349309
*error_in = "MapViewOfFile";
350310
return ALLOC_FAILURE;
351311
} else {
352-
char *mmap_base_file = get_mmap_base_file();
353-
void *execute_ex_base = (void *)execute_ex;
354-
FILE *fp = fopen(mmap_base_file, "w");
355-
if (!fp) {
356-
err = GetLastError();
357-
zend_shared_alloc_unlock_win32();
358-
zend_win_error_message(ACCEL_LOG_WARNING, mmap_base_file, err);
359-
zend_win_error_message(ACCEL_LOG_FATAL, "Unable to write base address", err);
360-
return ALLOC_FAILURE;
361-
}
362-
fprintf(fp, "%p\n", mapping_base);
363-
fprintf(fp, "%p\n", execute_ex_base);
364-
fclose(fp);
312+
((void**)mapping_base)[0] = mapping_base;
313+
((void**)mapping_base)[1] = (void*)execute_ex;
314+
((char*)shared_segment->p) += ACCEL_BASE_POINTER_SIZE;
365315
}
366316

367317
shared_segment->pos = 0;

0 commit comments

Comments
 (0)