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

View File

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

View File

@@ -1,10 +1,7 @@
#pragma once #pragma once
#include "ds/address.h" #include "pal_noalloc.h"
#include "ds/flaglock.h"
#include "pal_plain.h"
#include <array>
#ifdef OPEN_ENCLAVE #ifdef OPEN_ENCLAVE
extern "C" void* oe_memset_s(void* p, size_t p_size, int c, size_t size); 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); extern "C" int oe_random(void* data, size_t size);
@@ -12,55 +9,29 @@ extern "C" [[noreturn]] void oe_abort();
namespace snmalloc 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: public:
/** static void print_stack_trace() {}
* 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;
[[noreturn]] static void error(const char* const str) [[noreturn]] static void error(const char* const str)
{ {
UNUSED(str); UNUSED(str);
oe_abort(); oe_abort();
} }
};
static std::pair<void*, size_t> using OpenEnclaveBasePAL = PALNoAlloc<OpenEnclaveErrorHandler>;
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};
auto result = std::make_pair(heap_base, heap_size); class PALOpenEnclave : public OpenEnclaveBasePAL
heap_size = 0; {
return result; 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> template<bool page_aligned = false>
static void zero(void* p, size_t size) noexcept static void zero(void* p, size_t size) noexcept

View File

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

View File

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