Fix munmap(2) shrink from right logic.

This fixes a rather nasty issue where gcc's garbage collection triggered a
munmap call with the effect that it began unmapping huge amounts of kernel
data until the system triple faulted.
This commit is contained in:
Jonas 'Sortie' Termansen 2015-09-28 01:21:34 +02:00
parent 40c2fd12dc
commit 4c2a93ea02
1 changed files with 10 additions and 5 deletions

View File

@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013. Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2015.
This file is part of Sortix. This file is part of Sortix.
@ -69,6 +69,11 @@ void UnmapMemory(Process* process, uintptr_t addr, size_t size)
assert(Page::IsAligned(size)); assert(Page::IsAligned(size));
assert(process == CurrentProcess()); assert(process == CurrentProcess());
if ( UINTPTR_MAX - addr < size )
size = Page::AlignDown(UINTPTR_MAX - addr);
if ( !size )
return;
struct segment unmap_segment; struct segment unmap_segment;
unmap_segment.addr = addr; unmap_segment.addr = addr;
unmap_segment.size = size; unmap_segment.size = size;
@ -77,7 +82,7 @@ void UnmapMemory(Process* process, uintptr_t addr, size_t size)
&unmap_segment) ) &unmap_segment) )
{ {
// Delete the segment if covered entirely by our request. // Delete the segment if covered entirely by our request.
if ( addr <= conflict->addr && conflict->addr + conflict->size - addr <= size ) if ( addr <= conflict->addr && conflict->addr + conflict->size <= addr + size )
{ {
uintptr_t conflict_offset = (uintptr_t) conflict - (uintptr_t) process->segments; uintptr_t conflict_offset = (uintptr_t) conflict - (uintptr_t) process->segments;
size_t conflict_index = conflict_offset / sizeof(struct segment); size_t conflict_index = conflict_offset / sizeof(struct segment);
@ -122,11 +127,11 @@ void UnmapMemory(Process* process, uintptr_t addr, size_t size)
} }
// Delete the part of the segment covered partially from the right. // Delete the part of the segment covered partially from the right.
if ( conflict->addr + size <= addr + size ) if ( conflict->addr <= addr + size )
{ {
Memory::UnmapRange(addr, addr + conflict->size + conflict->addr, PAGE_USAGE_USER_SPACE); Memory::UnmapRange(addr, conflict->addr + conflict->size - addr, PAGE_USAGE_USER_SPACE);
Memory::Flush(); Memory::Flush();
conflict->size -= conflict->size + conflict->addr; conflict->size -= conflict->addr + conflict->size - addr;
continue; continue;
} }
} }