Commit Graph

29 Commits

Author SHA1 Message Date
Matthew Parkinson
3d403aef7f Refactor use of sizeclasses (#415)
The primary aim for this refactor is to use a representation for
sizeclasses that uniformly covers both large and small.  This allows
certain operations such as alloc_size and external_pointer to be
uniformly implemented.

The additional types make clear which kind of sizeclass is in use.

This also tidies up the code for sizeclass based divisible by and
modulus.

It fixes a bug in rust_realloc that didn't correctly determine a realloc
was required for large classes.
2021-11-10 16:35:44 +00:00
Matthew Parkinson
5215bffa9e Change malloc test suite for errno
Errno is not required to be 0 on return from malloc,
so don't bother trying to make it 0. Leads to false test failures where
libc calls have not reset it after a failure.
2021-10-21 16:34:17 +01:00
Nathaniel Wesley Filardo
1de28eead7 test/func/malloc fixes
- `check_result()` `abort()` on `null` and non-`nullptr` result.  Otherwise it
  just prints and doesn't end the test

- Don't call `realloc(, 0)`; this has never been consistent and the current C2x
  draft (see §7.22.3.5 and N2464) finally just declares it to be undefined
  behavior.  POSIX (2017) tolerates our current behavior of freeing the
  passed-in pointer and returning a new object.
2021-10-19 08:42:44 +01:00
David Chisnall
cd70a7856b Fix fallout from the merge.
- CI merge issues:
   - The malloc shim libraries are renamed.
   - CMake gets very unhappy if you don't enable the C language and
     tries to link with the C compiler instead of the C++ compiler if
     you do enable it.
   - The Ubuntu packages for QEMU install a `binfmt_misc` activator for
     PowerPC64 little-endian, but set the page size to 4 KiB.  We then
     tried to run the tests (which expect 64 KiB pages) and became very
     confused when `mmap` returned 4 KiB-aligned memory.
 - Test failures:
   - Fix all of the issues UBsan found.
     - Underflow in `pointer_offset` when used to add negative offsets.
     - `CoreAlloc`'s `LocalState` accessed on a null `CoreAlloc` pointer.
     - Out of bounds access in the sizeclass list on attempts to access
       more memory than fits in the VA space.
     -
   - There was an integer overflow in `AddressSpace` that could cause it
     to try to allocate a zero-sized object, get a null pointer, and
     then try to do something with 0 - {size of the real allocation}.
   - The malloc tests weren't setting `errno` to 0 before doing
     calling `malloc`, which should set `errno` on failure, and then
     checking that `errno` was 0.
   - Don't call `PAL::error` on PAL allocation failure, return `nullptr`.
     The PALs were inconsistent about that and the new code expects to be
     able to report address-space exhaustion.
   - The malloc checks can behave differently with 0-sized allocations
     on different platforms but were very fragile about their
     expectations.
   - The malloc test didn't report failure for all of the ways that it
     could fail and so was spuriously passing on some platforms.
   - The perf test for external pointer is currently very slow on
     Windows.  The number of loops have been reduced and a timeout added
     for the Windows CI runs.
   - The logic to capture `errno` across calls was using
     `decltype(errno)`, which on some platforms where `errno` is a macro
     evaluated to `int&` and so they captured a reference rather than
     the value and failed to reset `errno`.
   - The Apple PAL can set `errno` on `notify_using` if it's called with
     memory that was not previously passed to `notify_not_using` but was
     not adequately protected against this and so would sometimes cause
     `malloc` to set `errno` to `EINVAL`.
2021-08-06 14:00:56 +01:00
David Chisnall
e8374479f4 Snmalloc2 API cleanups for sandbox use. (#359)
This is the set of changes required for snmalloc2 to be usable by the
process sandboxing code and incorporates some API changes that reduce
the amount of code required to embed snmalloc.  Highlights:

 - Merge the config and back-end classes.
 - Everything in config is now global (all methods are static)
 - The GlobalState class is gone (all global state is managed by global
   methods on the config class)
 - LocalState is now a member of the config class, all methods are
   instance methods.
 - Not every configuration needs to use the lazy initialisation hooks.
   They now need to be provided only if they are used.  If the
   configuration does not provide an `ensure_init` method, it is not
   called.  If it does not provide an `is_initialised` method then the
   global initialisation state is not checked.
 - There is now an `snmalloc::Options` class that default initialises
   itself to the default behaviour.  Every configuration must provide a
   `constexpr` instance of this class.  Each flag can be separately
   overridden and new flags can be added without breaking any existing
   API consumers.

The config classes are moved into the backend directory.
2021-08-05 15:08:12 +01:00
Matthew Parkinson
f0e2ab702a Major refactor of snmalloc (#343)
# Pagemap
 
The Pagemap now stores all the meta-data for the object allocation. The meta-data in the pagemap is effectively a triple of the sizeclass, the remote allocator, and a pointer to a 64 byte block of meta-data for this chunk of memory. By storing the pointer to a block, it allows the pagemap to handle multiple slab sizes without branching on the fast path. There is one entry in the pagemap per 16KiB of address space, but by using the same entry in the pagemap for 4 adjacent entries, then we can treat a 64KiB range can be treated as a single slab of allocations.

This change also means there is almost no capability amplification required by the implementation on CHERI for finding meta-data. The only amplification is required, when we change the way a chunk is used to a size of object allocation.


# Backend

There is a second major aspect of the refactor that there is now a narrow API that abstracts the Pagemap, PAL and address space management. This should better enable the compartmentalisation and makes it easier to produce alternative backends for various research directions. This is a template parameter that can be used to specialised by the front-end in different ways.

# Thread local state

The thread local state has been refactored into two components, one (called 'localalloc') that is stored directly in the TLS and is constant initialised, and one that is allocated in the address space (called 'coreallloc') which is lazily created and pooled.

# Difference

This removes Superslabs/Medium slabs as there meta-data is now part of the pagemap.
2021-07-12 15:53:36 +01:00
Nathaniel Filardo
8da12f5af8 NFC: tests: size_t and uintptr_t are not interchangable 2021-03-16 09:29:19 +00:00
Nathaniel Filardo
6259457790 Add -Wconversion to clang builds
MSVC has strong opinions on implicit conversions as used in CI, while Clang both
locally and in CI has weaker opinions.  In an effort to avoid subsequent
roundtrips through CI, make clang more strict.  Adding -Wconversion definitely
increases the strength of clang's opinions, apparently to include frowning on
some that even MSVC considers OK, so go make explicit the current implicit
behavior.
2021-03-01 20:18:01 +00:00
Matthew Parkinson
923705e514 Natural alignment for USE_MALLOC (#248)
* Add concept of natural alignment to tests.

snmalloc naturally aligns blocks very heavily, so that
the largest power-of-two in the rounded size is the alignment.
This checks that in the test, and provides a method for
finding the natural alignment of a block.

* Improve USE_MALLOC to provide alignment

snmalloc provides a lot of alginment guarantees. This ensures that when
we pass through to the system allocator we still get those alignment
guarantees.

The commit also fixes the tests to work with USE_MALLOC, and builds a
set of unit tests for ctest to check behaviour.
2020-09-28 10:08:19 +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
Theo Butler
61afa77898 Fix sizeclass rounding error 2020-06-08 07:55:32 +01:00
Matthew Parkinson
c899ee7ab2 Large alloc fix (#178)
* Improved malloc style tests

Added comprehensive testing of realloc, and other minor improvements
to reporting errors.

* Fix realloc resizing for large sizeclasses.

The rounding by sizeclass was incorrect for large allocation.  This fixes
that.

* Ensure alloc_size is committed

There is an awkward interaction between alloc_size and
committing only what is requested.  If the user assumes
everything up to alloc_size is available, then we need to
either store the more precise size for alloc_size to return
or commit the whole 2^n range, so that alloc_size stays simple.

This changes to just make the whole range committed.
In the future, we might want to store a more precise size, so
that the allocation can be sized more precisely.

* Reduce size of objects.
2020-05-07 06:31:37 +01:00
Matthew Parkinson
bad94e80d3 Clangformat 2020-02-04 10:24:57 +00:00
Matthew Parkinson
6e8edefc99 Make all large allocations naturally aligned
This makes any large allocation naturally aligned to its size. This
means all alignment requests can be handled without checks.
2020-02-04 10:19:22 +00:00
Nathaniel Filardo
c7d509e418 test/func/malloc: align check as size_t
If the test happens as uintptr_t on CHERI, then we attempt to construct
a capability and use a capability-based test rather than an
integer-based one, and things go south.
2019-11-26 15:50:22 +00:00
Nathaniel Filardo
0b47145526 test/func/malloc: posix_memalign vs. size_t
posix_memalign requires that the alignment parameter be a multiple of
sizeof(uintptr_t), but the test begins with alignments as small as
sizeof(size_t).  While those are very likely the same value out in the
wild right now, they're not on CHERI.

Begin the test loop at sizeof(uintptr_t) and add a test that a request
for a reasonable amount of memory but with an alignment of
sizeof(uintptr_t)/2 fails with EINVAL.
2019-11-26 15:50:22 +00:00
Matthew Parkinson
16b084f501 Changed abort behaviour for Windows CI. 2019-08-13 15:37:54 +01:00
Matthew Parkinson
6151b7a9b2 Make test_realloc not leak in failure case. 2019-08-13 13:03:06 +01:00
Matthew Parkinson
a32882cd55 Make malloc functional test check for leaks. 2019-08-13 13:03:06 +01:00
Matthew Parkinson
7a8eaec2cc Made a sizecass_t to wrap the sizeclass
This is useful as codegen is nicer if we use size_t, but the semantics
is uint8_t, and is stored as that in many places in the metadata.
Ultimately should introduce a wrapper to check this invariant.
2019-07-01 14:30:05 +01:00
Matthew Parkinson
5030fff9bd Missing include from test. 2019-04-29 11:02:47 +01:00
Matthew Parkinson
e7d90966f6 Made the malloc tests run on Windows 2019-02-15 14:36:47 +00:00
Theo Butler
a3fb2b9531 cleanup some tests 2019-02-14 22:31:45 -05:00
Theo Butler
057595a57e remove -1 check in calloc 2019-02-13 16:03:38 -05:00
Theo Butler
942313fec7 replace asserts with abort 2019-02-13 10:03:41 -05:00
Theo Butler
24ba068dce remove randomness from malloc tests 2019-02-13 08:42:51 -05:00
theodus
a9615bc6aa mark variables unused in release build 2019-02-12 20:22:10 -05:00
Theo Butler
f6497e6ee3 Add malloc tests 2019-02-12 19:50:15 -05:00