Commit Graph

1041 Commits

Author SHA1 Message Date
Matthew Parkinson
03c9da6aa4 Refactor interface between backend and frontend (#530)
* Rename to use Config, rather than StateHandle/Globals/Backend
* Make Backend a type on Config that contains the address space management implementation
* Make Ranges part of the Backend configuration, so we can reuse code for different ways of managing memory
* Pull the common chains of range definitions into separate files for reuse.
* Move PagemapEntry to CommonConfig
* Expose Pagemap through backend, so frontend doesn't see Pagemap directly
* Remove global Pal and use DefaultPal, where one is not pass explicitly.

Co-authored-by: David Chisnall <davidchisnall@users.noreply.github.com>
Co-authored-by: Nathaniel Filardo <105816689+nwf-msr@users.noreply.github.com>
2022-05-31 10:45:04 +01:00
David Carlier
1b8aa6bc0d Haiku build fix post refactoring. 2022-05-26 08:11:46 +01:00
Matthew Parkinson
7ff10c30f1 Enable accessing parent ranges. (#529)
This exposes a feature on Ranges to access ranges higher up the
stack of ranges.  This could be useful for applying operations in the
middle of a pipeline like

   object_range.ancestor<SpecialRange>().init(...);

This allows some initialisation to be added to the middle of pipeline
without breaking the current coding pattern.

It also allows for bypassing some ranges

   object_range.ancestor<LargeObjectsRange>().alloc_chunk(...);

Neither are done in this commit, but both will occur in future commits.

Co-authored-by: Nathaniel Wesley Filardo <nfilardo@microsoft.com>
2022-05-19 21:19:49 +01:00
Matthew Parkinson
c445de8eb4 Alter FailFast behaviour of memcpy (#526)
This commit changes the codegen for error messages for failed memcpys.
This no longer generates a stack frame and correctly tail calls the
error messages generator.

It also turns the error messages on in Release builds.  This will lead
to better adoption experience.
2022-05-19 14:20:45 +01:00
Matthew Parkinson
87d71cce21 Add a pipe operation to ranges (#527)
The ranges are naturally put together with pipes. This
commit does some template magic to make the code more
readable.  There should be now functional changes with
this change.
2022-05-17 16:34:00 +01:00
Matthew Parkinson
888d182bac Check size is correct if specified
Some secure allocators check that the C++ supplied size is correct
relative to the meta-data. This adds a check to the secure version of
snmalloc to do that.
2022-05-12 10:12:07 +01:00
Matthew Parkinson
967f1f2033 Allow check_client messages to have parameters
Make the check_client macro use the new "pretty" format.
2022-05-12 10:12:07 +01:00
Matthew Parkinson
22d7c04cb8 debug_check_empty (#521)
Currently a failing debug_check_empty does not provide any information.
This change allows it to print the size of the one of the allocations
that has not been freed.
2022-05-12 09:37:03 +01:00
Jakub Panek
c4f31bb279 Fix markdown (#522)
* Fix markdown link to ./docs/AddressSpace

* Fix footnotes in docs/StrictProvenance.md
2022-05-10 15:51:59 +01:00
Matthew Parkinson
d5c732f3c1 Preparation for 0.6.0 (#517)
Co-authored-by: David Chisnall <davidchisnall@users.noreply.github.com>
Co-authored-by: Robert Norton <1412774+rmn30@users.noreply.github.com>
Co-authored-by: Nathaniel Wesley Filardo <nfilardo@microsoft.com>
Co-authored-by: Istvan Haller <31476121+ihaller@users.noreply.github.com>
2022-05-09 13:38:12 +01:00
Matthew Parkinson
5906b14586 Out-of-memory can fail silently
If this test fails to allocate memory, that should not cause the test to
fail.  The 'abort' was added previously to confirm a infrequent failure
was caused by out-of-memory causing the test to assign to nullptr.

This was confirmed in a CI run, and now the test can be made to ignore
allocation failure.
2022-05-09 13:02:28 +01:00
Matthew Parkinson
2d44ae9db4 Check for allocation failure. 2022-05-08 20:55:29 +01:00
Matthew Parkinson
325c013e85 Add some tracing to the backend 2022-05-08 20:55:29 +01:00
Matthew Parkinson
9f9964239e Ensure logging doesn't affect errno 2022-05-08 20:55:29 +01:00
Matthew Parkinson
14b7b40a82 Add a Stats combiner to make code cleaner. 2022-05-05 17:27:31 +01:00
Matthew Parkinson
d47c44783d Remove redundant params. 2022-05-05 17:27:31 +01:00
Matthew Parkinson
563d5a5cee Make clang on Windows use /Debug 2022-05-03 15:59:31 +01:00
Matthew Parkinson
d927a9a179 Modified Metadata range to have separate pool
The Metadata range should not be shared with the object range.  This
change ensures that their are separate requests to the Pal for meta-data
and object data ranges.  The requests are never combined, and thus
memory cannot flow from being used in malloc to later be used in meta-
data.
2022-05-03 15:59:31 +01:00
Matthew Parkinson
56ccb5c794 Enable memcpy checks for check shim (#515)
Build three levels of checking
  - None
  - Checks memcpy only
  - Checks (full)

Currently you can build checks without enabling the memcpy protection.
This PR fixes that.
2022-05-03 14:26:25 +01:00
Robert Norton
3e08caaf32 Simplify asserts introduced in #512. (#513)
These are functionally equivalent but have the form A -> B (i.e. !A ||
B in C++).
2022-04-29 11:33:14 +01:00
Matthew Parkinson
70eba1e70b Refill heuristic (#511)
* Adding a refilling heuristic

The large buddy allocator requests memory from its parent range.  The
request size was a fixed large request.  This was sufficiently large, so
that contention was not a problem.

This change makes it initially smaller, and gradually growing so that
contention is still not a problem, but for small work loads it requests
less memory.

* Remove special case for OE as no longer required.
2022-04-28 17:08:27 +01:00
Robert Norton
f277cf2f00 Refactor capptr_domesticate SFINAE to make more statically safe.
This refactoring was provided by David.  Previously if a backend
provided a capptr_domesticate function with the wrong type it would be
silently ignored.  This change requires backends to explicitly opt in
to domestication via a new Backend::Option and ensures the compiler
will loudly complain if there is a mismatch.
2022-04-28 10:02:42 +01:00
David Carlier
bf54eeb7be New option to name reserved pages. 2022-04-18 21:01:40 +01:00
David Chisnall
848a7b1499 Check things in release builds with check-client.
Clang 15 doesn't build the release builds with CHECK_CLIENT enabled
because they are using `SNMALLOC_ASSERT` and so the values that we're
collecting to check are never actually checked.  This is probably a bug
- if we're turning on the checks, I imagine it's because we want them.
2022-04-14 10:35:32 +01:00
Matthew Parkinson
943bae1b34 Minimal example of #506 2022-04-11 21:07:05 +01:00
Matthew Parkinson
43f5f33913 Fix missing consolidation marker
During creation of large allocation the code was not setting the
consolidation bit. This meant that Windows would crash for certain patterns for large
allocations.
2022-04-11 21:07:05 +01:00
Robert Norton
f0361f8c01 Pagemap move (#504)
* Move PageMap interface into pagemap.h and rename to BasicPagemap.

Refactoring suggested by David. This allows custom backends to reuse
or extend the BasicPagemap. It has template parameters for the PAL,
concrete page map and page map entry types as well as the Backend (so
that it can be friends).  BackendAllocator provides an exmple page map
entry type.
2022-04-11 13:29:06 +01:00
David Chisnall
d4226a1ea2 Remove indirection of state in ranges. (#505)
This doesn't give any extra flexibility: the range itself can be either
a stateless class, a class with no per-instance state that stores all of
static fields, or a class with stateful instances.  It did add a
requirement that every range implementation added an indirection layer.
2022-04-11 13:28:03 +01:00
David Chisnall
f6e9796bbc Introduce header layering (#503)
See src/snmalloc/README.md for an explanation of the layers.

Some other cleanups on the way:

Fine-grained stats support is now gone.

It's been broken for two years, it depends on iostream (which then
causes linker failures with libstdc++) and it's collecting the wrong
stats for the new design.  After discussion with @mjp41, it's better to
remove it and introduce new stats support later, rather than keep broken
code in the main branch.

Tracing was controlled with a preprocessor macro, now there's also a
CMake option.
2022-04-06 09:59:33 +01:00
David Chisnall
65ee6b2a2f Refactor MetaSlab / MetaCommon. (#501)
MetaCommon is now gone.  The back end must provide a SlabMetadata,
which must be a subtype of MetaSlab (i.e. MetaSlab or a subclass of
MetaSlab).  It may add additional state here.

The MetaEntry is now templated on the concrete subclass of MetaSlab that
the back-end uses.  The MetaEntry still stores this as a `uintptr_t` to
allow easier toggling of the boundary bit but the interfaces are all in
terms of stable types now.

Also some tidying of names (SharedStateHandle is now called Backend).

In a follow-on PR, we can then remove the chunk field from the
BackendMetadata in the non-CHERI back end and allow back ends that don't
require extra state to use MetaSlab directly.

Other cleanups:

 - Remove backend/metatypes, define the types that the front end expects
   in mem/metaslab.  The back end may extend them but these types define
   part of the contract between the front and back ends.
 - Remove FrontendMetaEntry and fold its methods into MetaEntry.
 - For example purposes, the default back end now extends MetaEntry.
   This also ensures that nothing in the front end depends on the
   specific type of MetaEntry.
 - Some things now have more sensible names.

The meta entry now operates in one of three modes:

 - When owned by the front end, it stores a pointer to a remote, a
   pointer to some MetaSlab subclass, and a sizeclass.
 - When owned by the back end, it stores two back-end defined values
   that must fit in the bits of `uintptr_t` that are not reserved for
   the MetaEntry itself.
 - When not owned by either, it can be queried as if owned by the front
   end.

The red-black tree has been refactored to allow the holder to be a
wrapper type, removing all of the Holder* and Holder& uses and treating
it uniformly as a value type that can be used to access the contents.

The chunk field is fone from the slab medatada.
This will need to be added back in the CHERI back ends, but it's a
back-end policy.  The back end can choose to use it or not, depending on
whether it can safely convert between an Alloc-bounded pointer and a
Chunk-bounded pointer.

The term 'metaslab' originated in snmalloc 1 to mean a slab of slabs.
In the snmalloc2 branch it was repurposed to mean metadata about a
slab.  To make this clearer, all uses of metaslab are now gone and have
been renamed to slab metadata.  The frontend metadata classes are all
prefixed Frontend and some extra invariants are checked with
`static_assert`.
2022-04-01 17:32:53 +01:00
David CARLIER
ede7dbb3ef Following up using writev/fsync direct syscalls to avoid pthread interpositions (#499)
As these calls carries a pthread cancellation check and due to pthread allocation
 libc init timing, we directly access their syscalls instead.
2022-03-29 14:04:09 +01:00
David Chisnall
ee70f952d1 Allow POSIX PALs to overwrite writev and fsync. (#495)
On FreeBSD (possibly elsewhere) the normal versions of these go via an
indirection layer because they are pthread cancellation points.  This
indirection layer does not work correctly if pthreads can't allocate
memory and so we can't get debug output until malloc is working, at
least a little bit.

With this version, we can call the __sys_ variants, which skip any libc
/ libthr interposition.
2022-03-28 11:53:40 +01:00
David Chisnall
6386cd7eca Expose ranges in snmalloc_core. (#496)
Also add a missing #pragma once that cause multiple includes to error.
2022-03-28 11:11:31 +01:00
David Chisnall
dec65d1b4a Make MetaCommon state public.
C++ has no way of exposing it only to things that are backend
implementations.  Eventually this class should be made purely part of
the CHERI back end.
2022-03-28 09:52:49 +01:00
Nathaniel Wesley Filardo
deac29c576 CheriBSD renamed VMEM SW perm bit
CheriBSD 00d71bd4d11af448871d196f987c2ded474f3039 changes
"CHERI_PERM_CHERIABI_VMMAP" to be spelled "CHERI_PERM_SW_VMEM" and deprecated
the old form.  Follow along with fallback so we can use older CheriBSDs.
2022-03-25 15:55:57 +00:00
Nathaniel Wesley Filardo
6424edaeaa Stop playing OO games with MetaEntry
David points out that the downcasts I had introduced were UB.  Instead, go back
to passing MetaEntry-s around and make MetaslabMetaEntry just a namespace of
static methods.

This partially reverts 7940fee00c
2022-03-25 14:30:13 +00:00
Matthew Parkinson
e77b9e2851 Add ConcurrencySafe property
Ranges can be safe to call from multiple threads.  This adds a constexpr
field to signify if that is the case.
2022-03-24 08:01:09 +00:00
Matthew Parkinson
bdb3183989 Remove std::cout
Now we have an allocation free formatting routine, remove std::cout
from tracing.
2022-03-24 08:01:09 +00:00
Matthew Parkinson
bb82ac15e3 Add a thread id to messages
Automatically prepend messages with a thread id.  Makes debugging
easier.
2022-03-24 08:01:09 +00:00
Matthew Parkinson
821620133d Remove ChunkAllocator 2022-03-24 08:01:09 +00:00
Matthew Parkinson
73be8a3786 Fix StatsRange on OE 2022-03-24 08:01:09 +00:00
Nathaniel Wesley Filardo
453a7d57e9 backend: clobber MetaEntry-s in dealloc_chunk
Otherwise these won't get updated until the small buddy allocator hands them off
to the large buddy allocator (when they morph into being rbtree nodes) and so
the frontend might get confused in the interim (including risk of UAF on
double-free).
2022-03-21 23:21:24 +00:00
Matthew Parkinson
2ff2cdf8ff Add test for external pointer on stack
The external pointer function should work on any memory.  This checks
it works for the stack.
2022-03-21 23:21:24 +00:00
Matthew Parkinson
1e1104a11c Fix 32bit external pointer.
For 32bit external pointer it was performing a divide by size, and for
things not managed by snmalloc this was causing a crash.  This checks
for zero, and gives the start of the address range as the start of the
object.
2022-03-21 23:21:24 +00:00
Nathaniel Wesley Filardo
a0377f62eb Add docs/AddressSpace.md 2022-03-21 23:21:24 +00:00
Nathaniel Wesley Filardo
7940fee00c Refactor MetaEntry remote_and_sizeclass
Introduce a class that we can use to more completely separate the frontend
encoding details from the backend.
2022-03-21 23:21:24 +00:00
Nathaniel Wesley Filardo
772e46f878 Additional commentary and more verbose names 2022-03-21 23:21:24 +00:00
Nathaniel Wesley Filardo
b5a66131bd Move BACKEND_MARKER to backend/metatypes
And rename it to REMOTE_BACKEND_MARKER to scope it a bit.
2022-03-21 23:21:24 +00:00
Nathaniel Wesley Filardo
9d97a38806 Move MetaEntry and MetaCommon to backend
These are almost entirely backend concerns, so move their definitions over
there.  Use C++ friend classes to ensure that MetaCommon structures are opaque
to frontend code (at least, at compile time, and neglecting the rest of C++).
(These structures contain high-authority pointers and so should be as closely
guarded as we can make them.)

The bits that leak out are

- the encoding of RemoteAllocator* and sizeclass_t into the uinptr_t within a
  MetaEntry.  This, however, is almost entirely a frontend concern, so detach
  the method definitions from the class and leave those in mem/metaslab.h for
  the moment.

- the size of metadata structures pointed to by the MetaEntry meta field.
  Rather than use sizeof(Metaslab) (and assert that sizeof(ChunkRecord) is
  smaller), instead, define PAGEMAP_METADATA_STRUCT_SIZE once and assert that
  all records fit.  Additionally, add an assertion that Metaslab is exactly this
  size, not for semantic reasons, but because we expect it to be true.

The bits that leak in are

- the need to zero memory corresponding to a chunk.  Rather than having an
  escape hatch that reveals the MetaCommon.chunk, move the zeroing call into a
  small wrapper method within the MetaCommon class itself.

- the need to get the address of a chunk.  We want to assert that we've got the
  right chunk on occasion (well, at least once so far) and so add a class method
  to expose the address_t view of the chunk pointer without exposing the pointer
  itself.
2022-03-21 23:21:24 +00:00
Matthew Parkinson
6ad7f65d19 Remove unused code. 2022-03-20 19:28:35 +00:00