r780 - trunk/code/qcommon

DONOTREPLY at icculus.org DONOTREPLY at icculus.org
Mon May 15 11:57:02 EDT 2006


Author: thilo
Date: 2006-05-15 11:57:02 -0400 (Mon, 15 May 2006)
New Revision: 780

Modified:
   trunk/code/qcommon/vm_x86.c
Log:
Fix JIT compiler code execution on NX-protected win32 OS

Modified: trunk/code/qcommon/vm_x86.c
===================================================================
--- trunk/code/qcommon/vm_x86.c	2006-05-13 12:18:43 UTC (rev 779)
+++ trunk/code/qcommon/vm_x86.c	2006-05-15 15:57:02 UTC (rev 780)
@@ -22,6 +22,9 @@
 // vm_x86.c -- load time compiler and execution environment for x86
 
 #include "vm_local.h"
+#ifdef _WIN32
+#include <windows.h>
+#endif
 
 #ifdef __FreeBSD__ // rb0101023
 #include <sys/types.h>
@@ -1081,6 +1084,11 @@
 	vm->codeBase = mmap(NULL, compiledOfs, PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
 	if(vm->codeBase == (void*)-1)
 		Com_Error(ERR_DROP, "VM_CompileX86: can't mmap memory");
+#elif _WIN32
+	// allocate memory with EXECUTE permissions under windows.
+	vm->codeBase = VirtualAlloc(NULL, compiledOfs, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
+	if(!vm->codeBase)
+		Com_Error(ERR_DROP, "VM_CompileX86: VirtualAlloc failed");
 #else
 	vm->codeBase = malloc(compiledOfs);
 #endif
@@ -1090,6 +1098,14 @@
 #ifdef VM_X86_MMAP
 	if(mprotect(vm->codeBase, compiledOfs, PROT_READ|PROT_EXEC))
 		Com_Error(ERR_DROP, "VM_CompileX86: mprotect failed");
+#elif _WIN32
+	{
+		DWORD oldProtect = 0;
+		
+		// remove write permissions.
+		if(!VirtualProtect(vm->codeBase, compiledOfs, PAGE_EXECUTE_READ, &oldProtect))
+			Com_Error(ERR_DROP, "VM_CompileX86: VirtualProtect failed");
+	}
 #endif
 
 	Z_Free( buf );
@@ -1108,6 +1124,8 @@
 {
 #ifdef VM_X86_MMAP
 	munmap(self->codeBase, self->codeLength);
+#elif _WIN32
+	VirtualFree(self->codeBase, self->codeLength, MEM_RELEASE);
 #else
 	free(self->codeBase);
 #endif




More information about the quake3-commits mailing list