Harden sizeclass table

If a sizeclass in the metadata is corrupted, then this can be used to
force an index beyond the end of these tables. This extends the tables
to the next power of two, and uses a mask on the index, so they are
always either a valid piece of data, or zero.
This commit is contained in:
Matthew Parkinson
2019-01-21 17:44:09 +00:00
parent 94f8b886a0
commit bb5027b454
2 changed files with 27 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
#pragma once
#include "bits.h"
#include "flaglock.h"
namespace snmalloc
@@ -30,4 +31,22 @@ namespace snmalloc
return obj;
}
};
template <size_t length, typename T>
class ModArray
{
static constexpr size_t rlength = bits::next_pow2_const(length);
T array[rlength];
public:
constexpr const T &operator[] (const size_t i) const
{
return array[i & (rlength - 1)];
}
constexpr T &operator[] (const size_t i)
{
return array[i & (rlength - 1)];
}
};
}