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

This lets us call deallocate_memory() with any address and it will not crash.
This commit is contained in:
apio 2023-01-22 14:25:51 +01:00
parent cb59a4e0e3
commit a7a38d3433
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 21 additions and 5 deletions

View File

@ -97,7 +97,7 @@ Result<void> UserVM::free_several_pages(u64 address, usize count)
const u64 index = (address - VM_BASE) / ARCH_PAGE_SIZE;
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);

View File

@ -50,6 +50,7 @@ class Bitmap
Option<usize> find_and_toggle_region(bool value, usize count, usize begin = 0);
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_region(usize start, usize bits, bool value);
@ -60,6 +61,8 @@ class Bitmap
return b ? 0xff : 0;
}
bool match_region_impl(usize start, usize bits, bool value);
u8* m_location = nullptr;
usize m_size_in_bytes = 0;
};

View File

@ -187,11 +187,8 @@ Option<usize> Bitmap::find_and_toggle_region(bool value, usize count, usize begi
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;
// 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;
}
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);
}