Improved OEPal integration with the new snmalloc architecture (#346)

* Improved OEPal integration with the new snmalloc architecture

* Applied PR feedback
This commit is contained in:
Istvan Haller
2021-07-15 15:06:47 +01:00
committed by GitHub
parent 39c2df0b56
commit d0ecba5280
5 changed files with 27 additions and 53 deletions

View File

@@ -16,11 +16,11 @@
# include "pal_haiku.h"
# include "pal_linux.h"
# include "pal_netbsd.h"
# include "pal_noalloc.h"
# include "pal_openbsd.h"
# include "pal_solaris.h"
# include "pal_windows.h"
#endif
#include "pal_noalloc.h"
#include "pal_plain.h"
namespace snmalloc
@@ -56,7 +56,7 @@ namespace snmalloc
#if defined(SNMALLOC_MEMORY_PROVIDER)
PALPlainMixin<SNMALLOC_MEMORY_PROVIDER>;
#elif defined(OPEN_ENCLAVE)
PALPlainMixin<PALOpenEnclave>;
PALOpenEnclave;
#else
DefaultPal;
#endif

View File

@@ -1,10 +1,7 @@
#pragma once
#include "ds/address.h"
#include "ds/flaglock.h"
#include "pal_plain.h"
#include "pal_noalloc.h"
#include <array>
#ifdef OPEN_ENCLAVE
extern "C" void* oe_memset_s(void* p, size_t p_size, int c, size_t size);
extern "C" int oe_random(void* data, size_t size);
@@ -12,55 +9,29 @@ extern "C" [[noreturn]] void oe_abort();
namespace snmalloc
{
class PALOpenEnclave
class OpenEnclaveErrorHandler
{
/// Base of OE heap
static inline void* heap_base = nullptr;
/// Size of OE heap
static inline size_t heap_size;
// This is infrequently used code, a spin lock simplifies the code
// considerably, and should never be on the fast path.
static inline std::atomic_flag spin_lock;
public:
/**
* This will be called by oe_allocator_init to set up enclave heap bounds.
*/
static void setup_initial_range(void* base, void* end)
{
heap_size = pointer_diff(base, end);
heap_base = base;
}
/**
* Bitmap of PalFeatures flags indicating the optional features that this
* PAL supports.
*/
static constexpr uint64_t pal_features = Entropy;
static constexpr size_t page_size = Aal::smallest_page_size;
static void print_stack_trace() {}
[[noreturn]] static void error(const char* const str)
{
UNUSED(str);
oe_abort();
}
};
static std::pair<void*, size_t>
reserve_at_least(size_t request_size) noexcept
{
// First call returns the entire address space
// subsequent calls return {nullptr, 0}
FlagLock lock(spin_lock);
if (request_size > heap_size)
return {nullptr, 0};
using OpenEnclaveBasePAL = PALNoAlloc<OpenEnclaveErrorHandler>;
auto result = std::make_pair(heap_base, heap_size);
heap_size = 0;
return result;
}
class PALOpenEnclave : public OpenEnclaveBasePAL
{
public:
/**
* Bitmap of PalFeatures flags indicating the optional features that this
* PAL supports.
*/
static constexpr uint64_t pal_features =
OpenEnclaveBasePAL::pal_features | Entropy;
template<bool page_aligned = false>
static void zero(void* p, size_t size) noexcept