vm: Fix off-by-one mistakes in _InsertAreaSlot

This commit is contained in:
Pawel Dziepak
2013-04-24 14:05:27 +02:00
parent dd817ff93c
commit c36a000cb4
+14 -11
View File
@@ -397,11 +397,12 @@ VMUserAddressSpace::_RandomizeAddress(addr_t start, addr_t end,
size_t alignment, bool initial) size_t alignment, bool initial)
{ {
ASSERT((start & addr_t(alignment - 1)) == 0); ASSERT((start & addr_t(alignment - 1)) == 0);
ASSERT(start <= end);
if (start == end) if (start == end)
return start; return start;
addr_t range = end - start; addr_t range = end - start + 1;
if (initial) if (initial)
range = min_c(range, kMaxInitialRandomize); range = min_c(range, kMaxInitialRandomize);
else else
@@ -538,7 +539,7 @@ VMUserAddressSpace::_InsertAreaSlot(addr_t start, addr_t size, addr_t end,
if (addressSpec == B_RANDOMIZED_BASE_ADDRESS) { if (addressSpec == B_RANDOMIZED_BASE_ADDRESS) {
originalStart = start; originalStart = start;
start = _RandomizeAddress(start, end - size, alignment, true); start = _RandomizeAddress(start, end - size + 1, alignment, true);
} }
// walk up to the spot where we should start searching // walk up to the spot where we should start searching
@@ -568,10 +569,10 @@ second_chance:
if (last == NULL) { if (last == NULL) {
// see if we can build it at the beginning of the virtual map // see if we can build it at the beginning of the virtual map
addr_t alignedBase = ROUNDUP(start, alignment); addr_t alignedBase = ROUNDUP(start, alignment);
addr_t nextBase = next == NULL ? end : min_c(next->Base(), end); addr_t nextBase = next == NULL ? end : min_c(next->Base() - 1, end);
if (is_valid_spot(start, alignedBase, size, nextBase)) { if (is_valid_spot(start, alignedBase, size, nextBase)) {
addr_t rangeEnd = min_c(nextBase - size, end); addr_t rangeEnd = min_c(nextBase - size + 1, end);
if (is_randomized(addressSpec)) { if (is_randomized(addressSpec)) {
alignedBase = _RandomizeAddress(alignedBase, rangeEnd, alignedBase = _RandomizeAddress(alignedBase, rangeEnd,
alignment); alignment);
@@ -590,11 +591,11 @@ second_chance:
while (next != NULL && next->Base() + size - 1 <= end) { while (next != NULL && next->Base() + size - 1 <= end) {
addr_t alignedBase = ROUNDUP(last->Base() + last->Size(), addr_t alignedBase = ROUNDUP(last->Base() + last->Size(),
alignment); alignment);
addr_t nextBase = min_c(end, next->Base()); addr_t nextBase = min_c(end, next->Base() - 1);
if (is_valid_spot(last->Base() + (last->Size() - 1), if (is_valid_spot(last->Base() + (last->Size() - 1),
alignedBase, size, nextBase)) { alignedBase, size, nextBase)) {
addr_t rangeEnd = min_c(nextBase - size, end); addr_t rangeEnd = min_c(nextBase - size + 1, end);
if (is_randomized(addressSpec)) { if (is_randomized(addressSpec)) {
alignedBase = _RandomizeAddress(alignedBase, alignedBase = _RandomizeAddress(alignedBase,
rangeEnd, alignment); rangeEnd, alignment);
@@ -618,7 +619,7 @@ second_chance:
size, end)) { size, end)) {
if (is_randomized(addressSpec)) { if (is_randomized(addressSpec)) {
alignedBase = _RandomizeAddress(alignedBase, end - size, alignedBase = _RandomizeAddress(alignedBase, end - size + 1,
alignment); alignment);
} }
@@ -631,12 +632,13 @@ second_chance:
// we didn't find a free spot in the requested range, so we'll // we didn't find a free spot in the requested range, so we'll
// try again without any restrictions // try again without any restrictions
if (!is_randomized(addressSpec)) {
start = USER_BASE_ANY; start = USER_BASE_ANY;
if (!is_randomized(addressSpec))
addressSpec = B_ANY_ADDRESS; addressSpec = B_ANY_ADDRESS;
else if (start == originalStart) } else if (start == originalStart) {
start = USER_BASE_ANY;
addressSpec = B_RANDOMIZED_ANY_ADDRESS; addressSpec = B_RANDOMIZED_ANY_ADDRESS;
else { } else {
start = originalStart; start = originalStart;
addressSpec = B_RANDOMIZED_BASE_ADDRESS; addressSpec = B_RANDOMIZED_BASE_ADDRESS;
} }
@@ -675,7 +677,8 @@ second_chance:
&& alignedBase == next->Base() && alignedBase == next->Base()
&& next->Size() >= size) { && next->Size() >= size) {
addr_t rangeEnd = min_c(next->Size() - size, end); addr_t rangeEnd = min_c(
next->Base() + next->Size() - size, end);
if (is_randomized(addressSpec)) { if (is_randomized(addressSpec)) {
alignedBase = _RandomizeAddress(next->Base(), alignedBase = _RandomizeAddress(next->Base(),
rangeEnd, alignment); rangeEnd, alignment);