php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login

Patch use_shared_memory_instead_memory_base_address_file for opcache Bug #73060

Patch version 2016-09-10 01:45 UTC

Return to Bug #73060 | Download this patch
Patch Revisions:

Developer: pvasilevich@plesk.com

--- ext/opcache/shared_alloc_win32.c	Thu Sep 08 01:35:23 2016
+++ ext/opcache/shared_alloc_win32.c	Fri Sep 09 13:45:51 2016
@@ -31,12 +31,11 @@
 
 #define ACCEL_FILEMAP_NAME "ZendOPcache.SharedMemoryArea"
 #define ACCEL_MUTEX_NAME "ZendOPcache.SharedMemoryMutex"
-#define ACCEL_FILEMAP_BASE_DEFAULT 0x01000000
 #define ACCEL_FILEMAP_BASE "ZendOPcache.MemoryBase"
 #define ACCEL_EVENT_SOURCE "Zend OPcache"
 
-static HANDLE memfile = NULL, memory_mutex = NULL;
-static void *mapping_base;
+static HANDLE memfile = NULL, membasefile = NULL, memory_mutex = NULL;
+static void *mapping_base = NULL, *mapping_base_address = NULL;
 
 #define MAX_MAP_RETRIES 25
 
@@ -127,20 +126,6 @@
 	return newname;
 }
 
-static char *get_mmap_base_file(void)
-{
-	static char windir[MAXPATHLEN+UNLEN + 3 + sizeof("\\\\@") + 1 + 32];
-	char uname[UNLEN + 1];
-	DWORD unsize = UNLEN;
-	int l;
-
-	GetTempPath(MAXPATHLEN, windir);
-	GetUserName(uname, &unsize);
-	l = strlen(windir);
-	snprintf(windir + l, sizeof(windir) - l - 1, "\\%s@%s@%.32s", ACCEL_FILEMAP_BASE, uname, accel_gen_system_id());
-	return windir;
-}
-
 void zend_shared_alloc_create_lock(void)
 {
 	memory_mutex = CreateMutex(NULL, FALSE, create_name_with_username(ACCEL_MUTEX_NAME));
@@ -169,25 +154,29 @@
 {
 	int err;
 	void *wanted_mapping_base;
-	char *mmap_base_file = get_mmap_base_file();
-	FILE *fp = fopen(mmap_base_file, "r");
 	MEMORY_BASIC_INFORMATION info;
 
-	err = GetLastError();
-	if (!fp) {
+	char *mmap_base_file = create_name_with_username(ACCEL_FILEMAP_BASE);
+
+	membasefile = OpenFileMapping(FILE_MAP_READ, 0, mmap_base_file);
+	if (membasefile == NULL) {
+		err = GetLastError();
 		zend_win_error_message(ACCEL_LOG_WARNING, mmap_base_file, err);
-		zend_win_error_message(ACCEL_LOG_FATAL, "Unable to open base address file", err);
-		*error_in="fopen";
+		zend_win_error_message(ACCEL_LOG_FATAL, "Unable to open base address file mapping", err);
+		*error_in="OpenFileMapping";
 		return ALLOC_FAILURE;
 	}
-	if (!fscanf(fp, "%p", &wanted_mapping_base)) {
+
+	mapping_base_address = MapViewOfFileEx(membasefile, FILE_MAP_READ, 0, 0, 0, 0);
+	if (mapping_base_address == NULL) {
 		err = GetLastError();
 		zend_win_error_message(ACCEL_LOG_FATAL, "Unable to read base address", err);
-		*error_in="read mapping base";
-		fclose(fp);
+		*error_in="MapViewOfFileEx";
+		CloseHandle(membasefile);
 		return ALLOC_FAILURE;
 	}
-	fclose(fp);
+
+	memcpy(&wanted_mapping_base, mapping_base_address, sizeof(void*));
 
 	/* Check if the requested address space is free */
 	if (VirtualQuery(wanted_mapping_base, &info, sizeof(info)) == 0 ||
@@ -324,17 +313,28 @@
 		*error_in = "MapViewOfFile";
 		return ALLOC_FAILURE;
 	} else {
-		char *mmap_base_file = get_mmap_base_file();
-		FILE *fp = fopen(mmap_base_file, "w");
+		char *mmap_base_file = create_name_with_username(ACCEL_FILEMAP_BASE);
+		membasefile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(mapping_base), mmap_base_file);
 		err = GetLastError();
-		if (!fp) {
+		if (membasefile == NULL) {
 			zend_shared_alloc_unlock_win32();
 			zend_win_error_message(ACCEL_LOG_WARNING, mmap_base_file, err);
 			zend_win_error_message(ACCEL_LOG_FATAL, "Unable to write base address", err);
+			*error_in = "CreateFileMapping";
+			return ALLOC_FAILURE;
+		}
+
+		mapping_base_address = MapViewOfFileEx(membasefile, FILE_MAP_ALL_ACCESS, 0, 0, 0, 0);
+		err = GetLastError();
+		if (mapping_base_address == NULL) {
+			CloseHandle(membasefile);
+			zend_shared_alloc_unlock_win32();
+			zend_win_error_message(ACCEL_LOG_FATAL, "Unable to map base address data", err);
+			*error_in = "MapViewOfFileEx";
 			return ALLOC_FAILURE;
 		}
-		fprintf(fp, "%p\n", mapping_base);
-		fclose(fp);
+
+		memcpy(mapping_base_address, &mapping_base, sizeof(void*));
 	}
 
 	shared_segment->pos = 0;
@@ -351,7 +351,13 @@
 	if (mapping_base) {
 		UnmapViewOfFile(mapping_base);
 	}
+	if (mapping_base_address) {
+		UnmapViewOfFile(mapping_base_address);
+	}
 	CloseHandle(memfile);
+	if (membasefile) {
+		CloseHandle(membasefile);
+	}
 	zend_shared_alloc_unlock_win32();
 	CloseHandle(memory_mutex);
 	return 0;
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 11:01:29 2024 UTC