Improve check_bounds init check.

This commit is contained in:
Matthew Parkinson
2022-01-10 10:34:28 +00:00
committed by Matthew Parkinson
parent 419347ba4a
commit ef64f6c31b
5 changed files with 26 additions and 2 deletions

View File

@@ -335,6 +335,12 @@ namespace snmalloc
UNUSED(local_state);
return concretePagemap.get_bounds();
}
static bool is_initialised(LocalState* ls)
{
UNUSED(ls);
return concretePagemap.is_initialised();
}
};
private:

View File

@@ -278,6 +278,14 @@ namespace snmalloc
return body[p >> SHIFT];
}
/**
* Check if the pagemap has been initialised.
*/
[[nodiscard]] bool is_initialised() const
{
return body_opt != nullptr;
}
/**
* Return the starting address corresponding to a given entry within the
* Pagemap. Also checks that the reference actually points to a valid entry.

View File

@@ -772,6 +772,16 @@ namespace snmalloc
#endif
}
bool check_bounds(const void* p, size_t s)
{
auto ls = core_alloc->backend_state_ptr();
if (SNMALLOC_LIKELY(SharedStateHandle::Pagemap::is_initialised(ls)))
{
return remaining_bytes(p) >= s;
}
return true;
}
/**
* Returns the byte offset into an object.
*

View File

@@ -137,7 +137,7 @@ namespace
auto& alloc = ThreadAlloc::get();
void* p = const_cast<void*>(ptr);
if (SNMALLOC_UNLIKELY(alloc.remaining_bytes(ptr) < len))
if (SNMALLOC_UNLIKELY(!alloc.check_bounds(ptr, len)))
{
if constexpr (FailFast)
{

View File

@@ -112,7 +112,7 @@ int main(int argc, char** argv)
};
std::vector<size_t> sizes;
for (size_t size = 1; size < 64; size++)
for (size_t size = 0; size < 64; size++)
{
sizes.push_back(size);
}