Commit Graph

123 Commits

Author SHA1 Message Date
Matthew Parkinson
52a1a9cce1 Changes to OE Pal. 2021-04-06 14:09:18 +01:00
Matthew Parkinson
0377fd88d4 Improve BSDs support. 2021-04-06 14:09:18 +01:00
Matthew Parkinson
5419e58633 Add random to Open Enclave Pal 2021-04-06 14:09:18 +01:00
Matthew Parkinson
cc5f1cfe9f Expose entropy on POSIX platforms
This provides a common definition for many POSIX platforms. It
can be disabled.
2021-04-06 14:09:18 +01:00
Matthew Parkinson
6daa261161 Expose Entropy on Windows 2021-04-06 14:09:18 +01:00
Matthew Parkinson
f15dc6ee2e Expose Entropy
Define various parts of random that can be used to make the layout of
memory more random.  Thread this through the allocator.

Expose the concept as part of the Pal. Subsequent commits will expose
that on different platforms.
2021-04-06 14:09:18 +01:00
Nathaniel Filardo
960733099b POSIX PAL: reset errno on failing mmap()s for zeroing
We try, if the region to be zeroed is sufficiently aligned, to use mmap to swap
out pages for zeros.
2021-03-16 09:29:19 +00:00
David Carlier
35346e72c3 Making pal_noalloc self reliant and not depending on inclusion order anymore. 2021-03-03 18:07:40 +00:00
David Carlier
a1fc509a65 pal bsd aligned build fix due to the -Wconversion flag, it is expected
an integer.
2021-03-02 09:13:25 +00:00
Matthew Parkinson
8840b386bc Make LowMemoryNotification object allocated (#281)
* Make LowMemoryNotification object allocated

This makes a separate allocation for the callback object.  This makes
it easier for different callbacks to be used.

* Add reserve_with_leftover to address_space

The address_space now supports reserving for non-power of 2 allocations
and the space that is used for rounding up is retained by the
address_space.  This means that we can more tightly pack the allocators
internal objects.
2021-02-23 14:51:44 +00:00
David CARLIER
c082a331e2 POSIX_COMMIT_CHECKS adding for apple. (#284)
* POSIX_COMMIT_CHECKS adding for apple Intel
2021-02-23 14:51:02 +00:00
Matthew Parkinson
70e5f9653d Fix BSD usage of POSIX_COMMIT_CHECKS 2021-02-19 15:34:11 +00:00
David CARLIER
ee470c535e Second batch for Mac M1 (mainly) changes proposal to make the whole
work more realibly, in both mono and multi thread contexts.
2021-02-11 20:10:05 +00:00
David CARLIER
0a868484db Mac M1 fix (#278)
The actual code works fine on the usual mac Intel however, some failures
occurs with some unit tests on the ARM h/w when zero'ing page ranges.
2021-02-09 14:38:54 +00:00
Matthew Parkinson
a3660c4069 Update to use a more efficient power of 2 check. (#274) 2021-01-27 11:58:41 +00:00
David Chisnall
c33f355736 Fix the sandbox use case and add a test. (#269)
Summary of changes:

- Add a new PAL that doesn't allocate memory, which can be used with a
  memory provider that is pre-initialised with a range of memory.
- Add a `NoAllocation` PAL property so that the methods on a PAL that 
  doesn't support dynamically reserving address space will never be
  called and therefore don't need to be implemented.
- Slightly refactor the memory provider class so that it has a narrower
  interface with LargeAlloc and is easier to proxy.
- Allow the address space manager and the memory provider to be
  initialised with a range of memory.

This may eventually also remove the need for (or, at least, simplify)
the Open Enclave PAL.

This commit also ends up with a few other cleanups:

 - The `malloc_useable_size` CMake test that checks whether the
   parameter is const qualified was failing on FreeBSD where this
   function is declared in `malloc_np.h` but where including
   `malloc.h` raises an error.  This should now be more robust.
 - The BSD aligned PAL inherited from the BSD PAL, which does not
   expose aligned allocation. This meant that it exposed both the
   aligned and non-aligned allocation interfaces and so happily
   accepted incorrect `constexpr` if blocks that expected one or 
   the other but accidentally required both to exist. The unaligned
   function is now deleted so the same failures that appear in CI should
   appear locally for anyone using this PAL.
2021-01-11 14:06:51 +00:00
David CARLIER
2dd63606af Sparc support proposal. (#264)
* Sparc support proposal.

* Tweaks from feedback.
Shift to integer/pointers cast like other archs.
Assembly reworks.
Note apparently reading the register once is sufficient to provoke
a pause in the cpu adding a clobber tough.
2021-01-05 16:28:22 +00:00
Matthew Parkinson
3ca29d6f50 Make POSIX COMMIT CHECKS mode fill memory
This simulates the pages being switched off with unexpected content, and
thus, when turned on we don't assume they have been unchanged.
2020-11-10 09:57:38 +00:00
David Carlier
49b9856ed0 DragonFly support (userland).
- Close to OpenBSD as there is no malloc*size api nor arbritrary
 alignment support.
- Like FreeBSD, MAP_NORESERVE never had been implemented even tough
 still present in the header but not mentioned in the man page,
FreeBSD has reserved the value for another later usage seems
 DragonFly has just out of sync header.
2020-10-05 10:44:30 +01:00
Nathaniel Filardo
bf3c99d87f fully-static PALs 2020-09-09 12:55:48 +01:00
Nathaniel Filardo
cb1694f124 pal_windows: defer registration for low-mem until use
If we're going to explore fully-static PALs, then we shouldn't need a
constructor.
2020-09-09 12:55:48 +01:00
Nathaniel Filardo
3e21ea1f65 Add C++ concept for PAL
This will not be used unless the C++ standard version is raised to 20.  As
concepts and C++20 more generally are quite new, this does not do so.
Nevertheless, the use of concepts can improve the local development experience
as type mismatches are discovered earlier (at template invocation rather than
only during expansion).
2020-09-09 12:55:48 +01:00
Matthew Parkinson
591b049585 Implement PAL backoff (#227)
The POSIX PAL and in some configurations the Windows PAL overallocate
address space. If address space has become exhausted then this can lead
to issues, where the PAL fails, even though there is enough aligned
address space to satisfy the underlying request.

This commit tries increasingly smaller overallocation sizes in an
attempt to succeed in more cases.
2020-07-08 16:17:33 +01:00
David CARLIER
c6197cb5f2 Illumos systems/Solaris support. (#226)
MAP_NORESERVE flag usage for memory over commit permitted only
 in the mmap context.
2020-07-08 15:35:28 +01:00
David Chisnall
5199556263 Reduce code duplication. (#220)
* Reduce code duplication.

The Apple and Haiku PALs both had *almost* identical code to the POSIX
PAL, differing only in some small argument variables.  This is fragile
and easy to accidentally get out of sync.  For example, the changes to
`reserve_at_least` involved copying identical code into multiple PALs
and it's easy to accidentally miss one.

This change introduces two optional fields on POSIX-derived PALs:

 - `default_mmap_flags` allows a PAL to provide additional `MAP_*`
   flags to all `mmap` calls.
 - `anonymous_memory_fd` allows the PAL to override the default file
   descriptor used for memory mappings.

If a PAL does not provide these, default values are used.

Fixes #219
2020-06-30 13:33:50 +01:00
David CARLIER
4a3102fedb Haiku support proposal. (#218)
* Haiku support proposal.
Basic PAL implementation.
This platform does not support TLS modes.

* MAP_NORESERVE usage
2020-06-30 11:01:49 +01:00
Matthew Parkinson
94a2ba4eda Revert "fixes for mingw (#215)" (#216)
This reverts commit 8f6b8db4ed.
2020-06-23 09:40:06 +01:00
Schrodinger ZHU Yifan
8f6b8db4ed fixes for mingw (#215)
* fix mingw

* fix mingw malloc test
2020-06-23 07:05:06 +01:00
Matthew Parkinson
e16f2aff6f Add AddressSpaceManager (#214)
This change brings in a new approach to managing address space.
It wraps the Pal with a power of two reservation system, that
guarantees all returned blocks are naturally aligned to their size. It
either lets the Pal perform aligned requests, or over allocates and
splits into power of two blocks.
2020-06-22 12:36:40 +01:00
Matthew Parkinson
26949de089 New PAL for open enclave
This changes the implementation of the open enclacve pal to support
aligned allocations.  This reduces the amount of memory required for
the initial reservation as the large allocator doesn't have to
overallocate to get alignment.
2020-06-18 13:09:06 +01:00
Anand Krishnamoorthi
c7736a2def 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>
2020-05-28 19:04:33 +01:00
Matthew Parkinson
2c9ab3096d Fix includes for the OE Pal.
The Pal should include address.h, this was masked as other Pals included
it, but are only included for simulating OE scenarios, rather than
the actual build for OE.
2020-05-27 13:42:32 +01:00
Nathaniel Filardo
37766588de Linux PowerPC port 2020-05-23 15:42:10 +00:00
Nathaniel Filardo
44e9abe888 Move OS_PAGE_SIZE to PAL 2020-05-23 15:42:10 +00:00
Matthew Parkinson
87dfd41553 Improve code quality for alloc_size (#196)
* Improve code quality for alloc_size

* Made error noreturn.

* Update docs.

* Move annoation
2020-05-23 16:16:12 +01:00
Nathaniel Wesley Filardo
6dae830ea0 Use cmake to find backtrace() (#187)
On FreeBSD, this notably requires the use of -lexecinfo, as backtrace()
is not available in -lc.  Rather than testing in C, test in cmake.
2020-05-16 12:45:51 +01:00
Matthew Parkinson
a9cfc3a2b4 Various minor changes to aid compiling with std14 (#182)
These changes make the code compile in clang10 with -std14.
2020-05-07 15:02:31 +01:00
SchrodingerZhu
a43773c5b7 add android support (#171)
* adjust for android

* update docs

* add const qualifier to `alloc_size`

* check const qualifier in cmake
2020-04-18 07:58:13 +01:00
Matthew Parkinson
74657d9dbc Defensive code for alloc/dealloc during TLS teardown (#161)
* Defensive code for alloc/dealloc during TLS teardown

If an allocation or deallocation occurs during TLS teardown, then it is
possible for a new allocator to be created and then this is leaked. On
the mimalloc-bench mstressN benchmark this was observed leading to a
large memory leak.

This fix, detects if we are in the TLS teardown phase, and if so,
the calls to alloc or dealloc must return the allocator once they have
perform the specific operation.

Uses a separate variable to represent if a thread_local's destructor has
run already.  This is used to detect thread teardown to put the
allocator into a special slow path to avoid leaks.

* Added some printing first operation to track progress

* Improve error messages on posix

Flush errors, print assert details, and present stack traces.

* Detect incorrect use of pool.

* Clang format.

* Replace broken LL/SC implementation

LL/SC implementation was broken, this replaces it with
a locking implementation. Changes the API to support LL/SC
for future implementation on ARM.

* Improve TLS teardown.

* Make std::function fully inlined.

* Factor out PALLinux stack trace.

* Add checks for leaking allocators.

* Add release build of Windows Clang
2020-04-07 15:37:26 +01:00
Matthew Parkinson
06eef5c4c5 Small changes (#159)
* Use NoZero for fresh pages
* MADV_DONTNEED only use for greater than a whole slab
* Simplify free list threading code
2020-04-02 07:04:03 +01:00
Matthew Parkinson
d900e29424 Improve slow path performance for allocation (#143)
* Remote dealloc refactor.

* Improve remote dealloc

Change remote to count down to 0, so fast path does not need a constant.

Use signed value so that branch does not depend on addition.

* Inline remote_dealloc

The fast path of remote_dealloc is sufficiently compact that it can be
inlined.

* Improve fast path in Slab::alloc

Turn the internal structure into tail calls, to improve fast path.
Should be no algorithmic changes.

* Refactor initialisation to help fast path.

Break lazy initialisation into two functions, so it is easier to codegen
fast paths.

* Minor tidy to statically sized dealloc.

* Refactor semi-slow path for alloc

Make the backup path a bit faster.  Only algorithmic change is to delay
checking for first allocation. Otherwise, should be unchanged.

* Test initial operation of a thread

The first operation a new thread takes is special.  It results in
allocating an allocator, and swinging it into the TLS.  This makes
this a very special path, that is rarely tested.  This test generates
a lot of threads to cover the first alloc and dealloc operations.

* Correctly handle reusing get_noncachable

* Fix large alloc stats

Large alloc stats aren't necessarily balanced on a thread, this changes
to tracking individual pushs and pops, rather than the net effect
(with an unsigned value).

* Fix TLS init on large alloc path

* Add Bump ptrs to allocator

Each allocator has a bump ptr for each size class.  This is no longer
slab local.

Slabs that haven't been fully allocated no longer need to be in the DLL
for this sizeclass.

* Change to a cycle non-empty list

This change reduces the branching in the case of finding a new free
list. Using a non-empty cyclic list enables branch free add, and a
single branch in remove to detect the empty case.

* Update differences

* Rename first allocation

Use needs initialisation as makes more sense for other scenarios.

* Use a ptrdiff to help with zero init.

* Make GlobalPlaceholder zero init

The GlobalPlaceholder allocator is now a zero init block of memory.
This removes various issues for when things are initialised. It is made read-only
to we detect write to it on some platforms.
2020-03-31 09:17:53 +01:00
Matthew Parkinson
77c453600b OE fixes (#157)
* Only compile OE PAL if required.

* OE:reserve: Fix bug in loop.

* Handle out of memory by returning nullptr.
2020-03-25 08:10:39 +00:00
Matthew Parkinson
4246d9a065 Workaround for QEMU behaviour. (#147)
* Fixes for ARM

* Workaround for QEMU behaviour.
2020-03-19 12:37:44 +00:00
Matthew Parkinson
0b278747c7 Pal_linux mprotect bug fix
When simulating decommit on Linux enable pages
before zeroing.
2020-03-18 14:49:02 +00:00
David Carlier
55f1237df9 Few build tweaks. 2020-03-17 12:16:21 +00:00
David Carlier
2d4f2c3867 AAL, basic arm implementation proposal. 2020-03-13 08:09:14 +00:00
SchrodingerZhu
65de3c41bc fix mingw 2020-03-08 22:13:49 +08:00
Matthew Parkinson
9dc689762c Apply suggestions from code review 2020-03-04 17:44:54 +00:00
Amaury Chamayou
acbcbce597 replace assert with SNMALLOC_ASSERT 2020-03-04 16:57:44 +00:00
Matthew Parkinson
814f3ba289 Merge pull request #128 from microsoft/low-memory-async
Make Lazy Decomit asynchronous
2020-03-03 11:29:23 +00:00