Hardening access to meta data

Introduce a wrapper that ensure various fields of meta data, represent
valid indexes into the the address space, and not beyond the current
slab/superslab.
This commit is contained in:
Matthew Parkinson
2019-01-24 10:48:16 +00:00
parent 2083b29c9b
commit 294efe8e03
3 changed files with 40 additions and 9 deletions

View File

@@ -32,6 +32,34 @@ namespace snmalloc
}
};
/**
* Wrapper for wrapping values.
*
* Wraps on read. This allows code to trust the value is in range, even when
* there is a memory corruption.
**/
template<size_t length, typename T>
class Mod
{
static_assert(length == bits::next_pow2_const(length),
"Must be a power of two.");
private:
T value;
public:
operator T()
{
return (T)(value & (length - 1));
}
T& operator=(const T v)
{
value = v;
return value;
}
};
template<size_t length, typename T>
class ModArray
{