Bitmap: Add a fallible variant of match_region() that does not crash if arguments are out of range
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This lets us call deallocate_memory() with any address and it will not crash.
This commit is contained in:
parent
cb59a4e0e3
commit
a7a38d3433
@ -97,7 +97,7 @@ Result<void> UserVM::free_several_pages(u64 address, usize count)
|
|||||||
const u64 index = (address - VM_BASE) / ARCH_PAGE_SIZE;
|
const u64 index = (address - VM_BASE) / ARCH_PAGE_SIZE;
|
||||||
if ((index + count) > (MAX_VM_SIZE * 8)) return err(EINVAL);
|
if ((index + count) > (MAX_VM_SIZE * 8)) return err(EINVAL);
|
||||||
|
|
||||||
if (!m_bitmap.match_region(index, count, true)) return err(EFAULT);
|
if (!TRY(m_bitmap.try_match_region(index, count, true))) return err(EFAULT);
|
||||||
|
|
||||||
m_bitmap.clear_region(index, count, false);
|
m_bitmap.clear_region(index, count, false);
|
||||||
|
|
||||||
|
@ -50,6 +50,7 @@ class Bitmap
|
|||||||
Option<usize> find_and_toggle_region(bool value, usize count, usize begin = 0);
|
Option<usize> find_and_toggle_region(bool value, usize count, usize begin = 0);
|
||||||
|
|
||||||
bool match_region(usize start, usize bits, bool value);
|
bool match_region(usize start, usize bits, bool value);
|
||||||
|
Result<bool> try_match_region(usize start, usize bits, bool value);
|
||||||
|
|
||||||
void clear(bool value);
|
void clear(bool value);
|
||||||
void clear_region(usize start, usize bits, bool value);
|
void clear_region(usize start, usize bits, bool value);
|
||||||
@ -60,6 +61,8 @@ class Bitmap
|
|||||||
return b ? 0xff : 0;
|
return b ? 0xff : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool match_region_impl(usize start, usize bits, bool value);
|
||||||
|
|
||||||
u8* m_location = nullptr;
|
u8* m_location = nullptr;
|
||||||
usize m_size_in_bytes = 0;
|
usize m_size_in_bytes = 0;
|
||||||
};
|
};
|
||||||
|
@ -187,11 +187,8 @@ Option<usize> Bitmap::find_and_toggle_region(bool value, usize count, usize begi
|
|||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Bitmap::match_region(usize start, usize bits, bool value)
|
bool Bitmap::match_region_impl(usize start, usize bits, bool value)
|
||||||
{
|
{
|
||||||
expect(initialized(), "Bitmap was never initialized");
|
|
||||||
expect((start + bits) <= size(), "Bitmap match out of range");
|
|
||||||
|
|
||||||
if (!bits) return true;
|
if (!bits) return true;
|
||||||
|
|
||||||
// Match individual bits while not on a byte boundary.
|
// Match individual bits while not on a byte boundary.
|
||||||
@ -223,3 +220,19 @@ bool Bitmap::match_region(usize start, usize bits, bool value)
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Bitmap::match_region(usize start, usize bits, bool value)
|
||||||
|
{
|
||||||
|
expect(initialized(), "Bitmap was never initialized");
|
||||||
|
expect((start + bits) <= size(), "Bitmap match out of range");
|
||||||
|
|
||||||
|
return match_region_impl(start, bits, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<bool> Bitmap::try_match_region(usize start, usize bits, bool value)
|
||||||
|
{
|
||||||
|
expect(initialized(), "Bitmap was never initialized");
|
||||||
|
if ((start + bits) > size()) return err(EINVAL);
|
||||||
|
|
||||||
|
return match_region_impl(start, bits, value);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user