Commit Graph

719 Commits

Author SHA1 Message Date
Nathaniel Wesley Filardo
6f79ddee31 FreeBSD RISC platforms have fewer address bits 2021-10-20 12:02:08 +01:00
Nathaniel Wesley Filardo
97a8bd1758 Initial RISC-V AAL 2021-10-20 12:02:08 +01:00
Nathaniel Wesley Filardo
fe43f0bea8 Placate some compiler errors on CHERI 2021-10-20 12:02:08 +01:00
Nathaniel Wesley Filardo
fa4560801a freelist: check_prev expects an address_t
So make signed_prev return one.  This distinction only matters on CHERI, of
course; everywhere else address_t *is* a uintptr_t.
2021-10-20 12:02:08 +01:00
Nathaniel Wesley Filardo
8023a6c906 backend/address_space: drop spurious !StrictProvenance guard
This dates back to the much earlier design that required the use of an authority
map.  Since the current CHERI design does not, go ahead and ask the platform
whenever underlying allocation requests are sufficiently aligned.
2021-10-20 12:02:08 +01:00
Nathaniel Wesley Filardo
3cdb7be478 MetaEntry: split get_metaslab by intent 2021-10-20 12:02:08 +01:00
Nathaniel Wesley Filardo
599ae0e632 Metaslab: add MetaCommon field
This preserves the chunk pointer through the use of a chunk as a slab.  It does
grow the structure by one pointer, but on non-CHERI it is still padded to 64
bytes, even with CHECK_CLIENT guards in place:

 0: MetaCommon chunk pointer
 8: next pointer
16: builder head[0]
24: builder head[1]
32: builder tail[0]
40: builder tail[1]
48: builder length[0] (uint16_t)
50: builder length[1] (uint16_t)
52: padding (4 bytes)
56: needed (uint16_t)
58: sleeping (bool)

(Sadly, on CHERI, even without CHECK_CLIENT guards and with no padding, there
are now four pointers in the structure -- chunk, next, head, tail -- plus five
extra bytes.  We will likely wish to explore encoding the head and tail offsets
relative to the chunk pointer.)

This lets us remove the "subversive amplification" in dealloc() in favor of just
preserving the chunk pointer.  Speaking of, be sure to assign that in all the
right places, and ASSERT that we've got it right.
2021-10-20 12:02:08 +01:00
Nathaniel Wesley Filardo
6c115eec18 NFC: doc tweaks 2021-10-20 12:02:08 +01:00
Nathaniel Wesley Filardo
f0b7dc4b04 NFC: Extract MetaCommon from ChunkRecord 2021-10-20 12:02:08 +01:00
Nathaniel Wesley Filardo
5bb556cb68 Convert alloc paths to capptr::Alloc<void>
The use of void* had let an overzealous unsafe_ptr() leak a pointer with address
space control to the client (in LocalAllocator::alloc_not_small, specifically).
Correct this to call capptr_chunk_is_alloc() (to capture our intent) and
capptr_to_user_address_control() (to do the bounding) and defer the conversion
to void* until the very periphery of the allocator, using capptr_reveal()
(again, to capture intent).
2021-10-20 12:02:08 +01:00
Nathaniel Wesley Filardo
31b8d99ca6 test/func/memcpy: adjust test sizes 2021-10-20 12:02:08 +01:00
Nathaniel Wesley Filardo
7f6378f5bf test/func/domestication: tweak for robustness
- Grab a larger second allocation on the first allocator to dodge the sizeclass
  of the prior alloc on that allocator *and* any implicit, bootstrapping slabs
  that get opened (e.g., for remote queue message stubs).

- De-FAST_PATH the domestication function.  No need to always inline it, here.

- Document things a little better
2021-10-20 12:02:08 +01:00
Nathaniel Wesley Filardo
554db3997d localalloc::dealloc tweak size math for large objects
Avoid computing bits::next_pow2_bits(1 << n).  Even if the compiler can see
through enough of the algebra, it's surely more direct to just use n.

While here, slightly expand documentation about what's going on with the
"sizeclass" encoded into MetaEntry-s.
2021-10-20 12:02:08 +01:00
Nathaniel Wesley Filardo
52a4b0c8d0 NFC: Make unsafe_capptr private, use unsafe_ptr() 2021-10-20 12:02:08 +01:00
Nathaniel Wesley Filardo
c54d2b527d NFC: CapPtr in external_pointer; drop capptr_rebound
capptr_rebound was only ever going to be used for external_pointer, which now
operates entirely using pointer_offset.  So instead, just make external_pointer
use capptr::AllocWild<void>, capptr_from_client, and a new capptr_reveal_wild.
2021-10-20 12:02:08 +01:00
Nathaniel Wesley Filardo
3462c53983 NFC: Remove spurious forward declarations
There is no such thing as "struct Slab" any more.

We use alignof(RemoteAllocator) below, so we already require the complete type
definition at this point.
2021-10-20 12:02:08 +01:00
Nathaniel Wesley Filardo
c2ce9c118d NFC: Racing stripes on Metaslab accessors
Even on debug builds, these little things should be inlined
2021-10-20 12:02:08 +01:00
Nathaniel Wesley Filardo
dba795ac6f NFC: move some capptr utility functions to that namespace
We'll want user_address_control_type in some particular PALs, so it can't live
in pal.h.

While here, make the spelling be capptr::is_spatial_refinement.
2021-10-20 12:02:08 +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
Matthew Parkinson
6db9a2f0e2 Add validate to freelist::Builder
The free list builder in a checked build will only validate entries when
they are removed.  This commit adds a validate method, so they can be
checked during teardown.  This means that programs that leak memory
will still fail if the free list has become corrupt.
2021-10-18 14:04:26 +01:00
Matthew Parkinson
93bb037c64 Fixed bounds calculation for domestication
Bug: bounds was base,size but code used it as base, end.
2021-10-18 14:04:26 +01:00
Nathaniel Wesley Filardo
ed10717dde RemoteAllocator: dequeue as destructive iterator
This avoids repeated double-tapping domestication of the same pointer in
!QueueHeadsAreTame builds, by keeping the current "front" pointer to the queue
in trusted locations (stack, register) rather than storing it back to possibly
client-accessible memory.
2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
2316a5c574 Add minimal domestication test
Instantiate two allocators and arrange for a message to get passed between them
by exploiting the existing slow-paths' handling of message queues.  Count and
CHECK the number of domestication calls during this message passing.  For a
little more excitement, pave over the forward pointer in the freelist::Object::T
that is the message and have the domestication callback patch the original value
back; should we somehow fail to invoke the domestication callback on that
address, this will induce a crash (WHP, on both CHECK_CLIENT and unchecked
builds).
2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
97950a9fca Optionally consider RemoteAllocator heads Tame 2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
96155db640 Collect freelist things in a namespace
Motivated by renaming `FreeObject::{Head,Queue,AtomicQueue}Ptr` to
`freelist::...Ptr`, in fact go further, moving `FreeObject` itself to
`freelist::Object` and `FreeListBuilder` to `freelist::Builder` and
`FreeListIter` to `freelist::Iter`
2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
bc365e0abb Default template args for FreeObject & friends
Now that explicit annotations have gotten us through the refactoring, it's time
for the scaffolding to disappear.  src/mem/freelist.h is left generic for any
future machinations, but `FreeObject::T<>`, the several `FreeObject::...Ptr<>`s,
`FreeListIter<>`, and `FreeListBuilder<>` are given default parameters and all
uses are shortened to use defaults where possible.
2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
7deb3b61da Fix builds on MSVC 2016
Without this it complains that LocalCache's constructor can't be constexpr
because small_fast_free_lists isn't initialized.  It's not clear to me why it
didn't mind before, and nobody else seems to mind now, but this shouldn't break
anyone else, either.
2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
501c14661f Remote queues hold Wild pointers 2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
94ab856ff5 NFC: remote queue domestication plumbing
These are, at present, just identity functions in the right places.
2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
d4c120dfe5 Free queues hold Wild pointers 2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
7750676598 NFC: FreeListIter domestication plumbing
Just an intermediate syntactic step to chase dependencies.  All these introduced
"domestication" callbacks are just the identity function, but they will let us
thread the LocalAlloc's handle to the Backend state down to where it's needed.
2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
a34b7a5973 Make capptr_from_client return a Wild CapPtr
Chase consequences in dealloc().
2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
1a608e6066 NFC: Introduce pointer domestication backend support
`capptr_domesticate<Backend>(Backend::LocalState*, CapPtr<T, B>)` is the
intended affordance for conversion to covert from a `CapPtr<T, B>` with
`B::wildness` `Wild` to a `CapPtr<>` with `B::with_wildness<Tame>` and thence
plumbed into the rest of the machinery.

David added the SFINAE wrapper so that `Backend`-s now don't need to implement a
domestication callback; instead, if the `Backend` does not provide a
`capptr_domesticate` function, a default, which just does an explicit type cast,
will be used instead.

This is not yet hooked into the rest of the tree.

Co-authored-by: David Chisnall <David.Chisnall@microsoft.com>
2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
7e53a2e82a CapPtr: shift free lists to Alloc bounds
This is incomplete, yet still more reflective of what's going on: we take the
exported pointers back from userspace and thread them directly into the free
lists.

So: move capptr_to_user_address_control to list construction time rather than
list consumption time.
2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
55cd5e87c0 NFC: Add FreeObject::from_next_ptr
Rather than open-code the conversion from &f->next_object to f, add a static
method to FreeObject and take the opportunity to add a static_assert that our
reinterpret_cast is sound.
2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
06e333a3a9 NFC: FreeObject: shift readers to take domestication callbacks
If we're going to check next's prev in atomic_read_next, we will need to
domesticate the next pointer first.  We could push the check up, but that opens
boxes, so it's simpler to plumb domestication this far down.  For symmetry, we
also plumb to (non-atomic) read_next.
2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
e25db7b832 Move to FreeObject::T<capptr::bound<>>
FreeObject itself is now just a namespace (but `friend`-ly); the actual free
list nodes are FreeObject::T-s that are templatized on the (perceived)
`capptr::bound<>` of the pointer they contain.  (These may differ across an
instantiated snmalloc; for example, in the sandboxing design, the in-sandbox
allocators may perceive all remotes to be full of `AllocUser` while the
privileged allocator of sandbox memory should perceive its remote queue as
holding `AllocUserWild` pointers in need of domestication.)

The interfaces to `FreeObject::T`-s now let us distinguish between the base and
inductive cases of the queues:

* in the inductive case, the pointer we hold to a `FreeObject::T` and its
  next_object have the same bounds

* in the base case, the pointer we hold has different bounds (typically,
  domesticated by contrast to the wild pointers in the queues).

To keep the clutter down a bit, we occasionally use raw pointers when we can be
reasonably certain that domestication is assured.  Moreover, we define some type
aliases, `FreeObject::{HeadPtr, QueuePtr, AtomicQueuePtr}`, that are slightly
more convenient labels than, e.g., `CapPtr<FreeObject::T<BQueue>, BView>`.
Because we are using template parameters for the `capptr::bound<>`s themselves,
we cannot use the aliases for `CapPtr<>s` provided within `capptr::`.

The two primary interfaces around free objects (`FreeListIter` AND
`FreeListBuilder`) are adjusted appropriately and their `BView` and `BQueue`
template paramters are plumbed explicitly around the tree.  This makes for quite
a bit of noise at the moment, but means that we'll be able to evolve parts of
the tree separately and can consider putting defaults in once that's done.
2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
d76f5fdd28 NFC: CapPtr: Introduce "Tame"/"Wild" split
This is just the changes to the taxonomy, no use of it in the tree yet
2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
ca70856dc1 CapPtr: remove a stale static assert
In the new world order, we will actually operate on AllocUser-bound pointers,
such as slabs' free lists.
2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
9065893181 Overhaul CapPtr
* Switch to a multidimensional taxonomy.

  Rather than encoding the abstract bound states in a single enum, move to a
  more algebraic treatment.  The dimensions themselves are within the
  snmalloc::capptr_bounds namespace so that their fairly generic names do not
  conflict with consumer code.  Aliases for many points in the space are
  established outside that namespace for ease of use elsewhere.

* Introduce several new namespaces:

    * snmalloc::capptr::dimension holds each of the dimension enums

    * snmalloc::capptr holds the bound<> type itself and a ConceptBound

    * snmalloc::capptr::bounds gives convenient specializations of bound<>

    * snmalloc::capptr also has aliases for CapPtr<> itself

  All told, rather than `CapPtr<T, CBChunk>`, we now expect client code to read
  `capptr::Chunk<T>` in almost all cases (and this is just an alias for the
  appropriate `CapPtr<T, bounds<...>>` type).  When the bound<>s themselves are
  necessary, as when calling capptr_bound, we expect that they will almost
  always be pronounced using an alias (e.g., `capptr::bounds::Alloc`).

* Chase consequences.

* Prune old taxa and aliases that are no longer in use in snmalloc2.
2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
b57390663e address_cast SNMALLOC_FAST_PATH 2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
3109ae9f72 NFC: Accumulated nits in comments
Mostly, promote some inline commentary to doc comments.  A typo and some stale
text can go, too.
2021-10-13 16:30:41 +01:00
Nathaniel Wesley Filardo
73ebb69955 localcache: drop some useless snmalloc:: namespace qualifiers 2021-10-13 16:30:41 +01:00
Matthew Parkinson
6470d62635 Fix teardown for main thread when using pthread destructors.
Co-authored with David Chisnall <david.chisnall@microsoft.com>
2021-10-07 19:28:58 +01:00
Matthew Parkinson
bba66e4f7e Randomise slab filling (#397)
# Free List builder track length

This commit makes the free list builder track the length of the lists in
the Random case.

# Refactor free list creation.

Minor refactoring to share code between the new free list and existing
path.

# Randomise slab filling

Knowing when a slab is going to become full makes it easier to by pass
the free list entries as protection for OOB writes.  This commit
randomises when a slab will become full.

This commit changes two things

* the free list builder can return some fraction of the deallocations
  on a slab.
* when there is a single free slab, we can with some probability
  allocate an additional slab.

These two combine to make it difficult to predict when a slab will be
free.

# Apply suggestions from code review

Co-authored-by: Nathaniel Wesley Filardo <nfilardo@microsoft.com>
2021-10-07 15:51:18 +01:00
Matthew Parkinson
8ac2adc4e5 Added a sequential queue
This changes the slab lists to use a sequential queue.
They were previously stored in a stack.

This commit also tidies up some incomplete refactoring from the
initial snmalloc2 work.
2021-09-29 14:26:09 +01:00
Matthew Parkinson
dbb7965507 Minor. 2021-09-29 14:26:09 +01:00
Matthew Parkinson
55a7ad2d58 Introduce PalEnforceAccess
The various Pals were given different meanings in CHECK_CLIENT and
non-CHECK_CLIENT builds.  This was because it is essential
that in the CHECK_CLIENT builds access is prevented, when not requested.

This PR separates the CHECK_CLIENT concept from how the Pal should be
implemented.
2021-09-28 09:23:52 +01:00
Matthew Parkinson
b4efc40aa6 Expose notify_using_readonly
This exposes a readonly notify using, so that the underlying platform
can map the range of pages readonly into the application.  This improves
performance of external pointer on platforms that support lazy commit
of pages as it can access anything in the range.
2021-09-28 09:23:52 +01:00
Matthew Parkinson
0af1ee3bef Tidy TODOs from Free List
* Add extra key to freelist.  This follows the encoding Cedric suggested
  for a signature of two things. Free list key now has a pair of keys
  for encoding previous pointer. This makes it harder to extract the
  underlying keys out of the multiplication.

* Apply SFINAE to the extract_segment.
2021-09-27 10:25:43 +01:00