OpenEnclave PAL: Store enclave heap base/end in inline variables. (#201)

PALOpenEnclave object is lazily constructed. I couldn't
figure out a straight-forward way to pass the heap bounds to
the constructor of PALOpenEnclave object.
As an alternative, store the bounds in inline static variables of
the PALOpenEnclave class and set them via static setup_initial_range
function.

- two_alloc_types/alloc1.cc
  Define oe_allocator_init to forward base, end values to
  PALOpenEnclave::setup_inital_range
- two_alloc_types/main.cc
  Use oe_allocator_init function to set up heap range.

- fixed_region/fixed_region.cc
  Initialize heap range via call to PALOpenEnclave::setup_inital_range.

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
Anand Krishnamoorthi
2020-05-28 11:04:33 -07:00
committed by GitHub
parent 4c22c5b02f
commit c7736a2def
4 changed files with 24 additions and 40 deletions

View File

@@ -3,8 +3,6 @@
#include "ds/address.h"
#include "pal_plain.h"
#ifdef OPEN_ENCLAVE
extern "C" const void* __oe_get_heap_base();
extern "C" const void* __oe_get_heap_end();
extern "C" void* oe_memset_s(void* p, size_t p_size, int c, size_t size);
extern "C" [[noreturn]] void oe_abort();
@@ -12,9 +10,19 @@ namespace snmalloc
{
class PALOpenEnclave
{
std::atomic<void*> oe_base = nullptr;
static inline std::atomic<void*> oe_base;
static inline void* oe_end = nullptr;
public:
/**
* This will be called by oe_allocator_init to set up enclave heap bounds.
*/
static void setup_initial_range(void* base, void* end)
{
oe_base = base;
oe_end = end;
}
/**
* Bitmap of PalFeatures flags indicating the optional features that this
* PAL supports.
@@ -32,17 +40,9 @@ namespace snmalloc
template<bool committed>
void* reserve(size_t size) noexcept
{
if (oe_base == 0)
{
void* dummy = NULL;
// If this CAS fails then another thread has initialised this.
oe_base.compare_exchange_strong(
dummy, const_cast<void*>(__oe_get_heap_base()));
}
void* old_base = oe_base;
void* next_base;
auto end = __oe_get_heap_end();
auto end = oe_end;
do
{
auto new_base = old_base;