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

@@ -11,20 +11,21 @@ namespace snmalloc
/**
* A single fixed address range allocator configuration
*/
template<SNMALLOC_CONCEPT(ConceptPAL) PAL>
class FixedGlobals : public CommonConfig
{
public:
using Backend = BackendAllocator<PALNoAlloc<Pal>, true>;
using Backend = BackendAllocator<PAL, true>;
private:
inline static Backend::GlobalState backend_state;
inline static typename Backend::GlobalState backend_state;
inline static ChunkAllocatorState slab_allocator_state;
inline static PoolState<CoreAllocator<FixedGlobals>> alloc_pool;
public:
static Backend::GlobalState& get_backend_state()
static typename Backend::GlobalState& get_backend_state()
{
return backend_state;
}

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

View File

@@ -11,7 +11,8 @@
using namespace snmalloc;
using FixedAlloc = LocalAllocator<FixedGlobals>;
using CustomGlobals = FixedGlobals<PALNoAlloc<DefaultPal>>;
using FixedAlloc = LocalAllocator<CustomGlobals>;
int main()
{
@@ -30,8 +31,8 @@ int main()
std::cout << "Allocated region " << oe_base.unsafe_ptr() << " - "
<< pointer_offset(oe_base, size).unsafe_ptr() << std::endl;
FixedGlobals fixed_handle;
FixedGlobals::init(oe_base, size);
CustomGlobals fixed_handle;
CustomGlobals::init(oe_base, size);
FixedAlloc a(fixed_handle);
size_t object_size = 128;

View File

@@ -10,7 +10,8 @@
#define SNMALLOC_PROVIDE_OWN_CONFIG
namespace snmalloc
{
using Alloc = LocalAllocator<FixedGlobals>;
using CustomGlobals = FixedGlobals<PALNoAlloc<DefaultPal>>;
using Alloc = LocalAllocator<CustomGlobals>;
}
#define SNMALLOC_NAME_MANGLE(a) enclave_##a
@@ -18,7 +19,7 @@ namespace snmalloc
extern "C" void oe_allocator_init(void* base, void* end)
{
snmalloc::FixedGlobals fixed_handle;
snmalloc::CustomGlobals fixed_handle;
fixed_handle.init(
CapPtr<void, CBChunk>(base), address_cast(end) - address_cast(base));
}