fixed a memory leak due to an incorrect munmap argument (credit: Cyrax)

This commit is contained in:
myT 2017-03-08 16:10:34 +01:00
parent 0bc9c75e49
commit d945904298
2 changed files with 13 additions and 13 deletions

View File

@ -182,6 +182,8 @@ struct vm_s {
vmFunc_t codeBase;
int codeLength;
int allocSize; // total allocation size, in bytes
int dataMask;
int dataLength; // exact data segment length
@ -191,7 +193,7 @@ struct vm_s {
int numSymbols;
vmSymbol_t *symbols;
int callLevel; // counts recursive VM_Call
int callLevel; // counts recursive VM_Call
int breakFunction; // increment breakCount on function entry to this
int breakCount;

View File

@ -2319,7 +2319,7 @@ __compile:
VM_FreeBuffers();
#ifdef VM_X86_MMAP
if ( mprotect( vm->codeBase.ptr, compiledOfs + n, PROT_READ|PROT_EXEC ) ) {
if ( mprotect( vm->codeBase.ptr, vm->allocSize, PROT_READ|PROT_EXEC ) ) {
VM_Destroy_Compiled( vm );
Com_Error( ERR_FATAL, "VM_CompileX86: mprotect failed" );
return qfalse;
@ -2329,7 +2329,7 @@ __compile:
DWORD oldProtect = 0;
// remove write permissions.
if ( !VirtualProtect( vm->codeBase.ptr, compiledOfs + n, PAGE_EXECUTE_READ, &oldProtect ) ) {
if ( !VirtualProtect( vm->codeBase.ptr, vm->allocSize, PAGE_EXECUTE_READ, &oldProtect ) ) {
VM_Destroy_Compiled( vm );
Com_Error( ERR_FATAL, "VM_CompileX86: VirtualProtect failed" );
return qfalse;
@ -2352,25 +2352,22 @@ VM_Alloc_Compiled
*/
static void *VM_Alloc_Compiled(vm_t *vm, int codeLength, int tableLength)
{
void *ptr;
int length;
length = codeLength + tableLength;
const int length = codeLength + tableLength;
#ifdef VM_X86_MMAP
ptr = mmap( NULL, length, PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0 );
void* const ptr = mmap( NULL, length, PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0 );
if ( ptr == MAP_FAILED ) {
Com_Error( ERR_FATAL, "VM_CompileX86: mmap failed" );
return NULL;
}
#elif _WIN32
// allocate memory with EXECUTE permissions under windows.
ptr = VirtualAlloc( NULL, length, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
void* const ptr = VirtualAlloc(NULL, length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if ( !ptr ) {
Com_Error( ERR_FATAL, "VM_CompileX86: VirtualAlloc failed" );
return NULL;
}
#else
ptr = malloc( length );
void* const ptr = malloc( length );
if ( !ptr ) {
Com_Error( ERR_FATAL, "VM_CompileX86: malloc failed" );
return NULL;
@ -2378,6 +2375,7 @@ static void *VM_Alloc_Compiled(vm_t *vm, int codeLength, int tableLength)
#endif
vm->codeBase.ptr = (byte*)ptr;
vm->codeLength = codeLength;
vm->allocSize = length;
return vm->codeBase.ptr;
}
@ -2390,7 +2388,7 @@ VM_Destroy_Compiled
static void VM_Destroy_Compiled(vm_t* vm)
{
#ifdef VM_X86_MMAP
munmap( vm->codeBase.ptr, vm->codeLength );
munmap( vm->codeBase.ptr, vm->allocSize );
#elif _WIN32
VirtualFree( vm->codeBase.ptr, 0, MEM_RELEASE );
#else