Commit Graph

228 Commits

Author SHA1 Message Date
Schrodinger ZHU Yifan
16b96245f6 Enable a seconary allocator support (e.g. GWP-Asan) (#737) 2025-02-05 09:03:11 +00:00
Matthew Parkinson
32495fd42d Multiple Pals could return spuriously from wake on address (#739)
* Add stress test benchmark

Co-authored-by: Alexander Nadeau <wareya@gmail.com>

* Add defensive code against spurious wakeup

This commit checks that wait_on_address has not returned spuriously.

* pal: spurious wake up.

The code in the Pal for wake on address was incorrectly assuming the operation returning success meant it had actually changed.  The specification allows for spurious wake ups.

This change makes the Pals recheck for a change.

---------

Co-authored-by: Alexander Nadeau <wareya@gmail.com>
2025-02-04 18:44:02 +00:00
Schrodinger ZHU Yifan
0111a410a2 [proxy](7/n) self vendor array and algorithm headers (#722)
* [proxy](7/n) proxy array and algorithm headers

* [wip] address CRs

* undo comment

* format

* fix build

* simplify macro dispatching
2025-01-09 13:49:22 +00:00
Schrodinger ZHU Yifan
e874617d8e [proxy](5/n) use c headers instead of c++ headers (#720) 2025-01-09 09:45:24 +00:00
Neil Monday
6a3c5750bd Changing DEBUG constexpr to Debug to avoid collision with any #define DEBUG that may exist in other projects. (#729)
Co-authored-by: Neil Monday <neil.monday@amd.com>
2025-01-08 17:13:26 +00:00
Schrodinger ZHU Yifan
feff2e8151 [proxy](1/n) start self-vendored STL with atomic (#715)
* [proxy] start self-vendored STL

* address CR
2025-01-03 07:49:32 +00:00
David CARLIER
6952683692 memcpy test fix build warning on openbsd. (#714)
* memcpy test fix build warning on openbsd.

* enabling TEST_LIMITED for openbsd too.
2024-12-18 12:42:26 +00:00
Schrodinger ZHU Yifan
cbc3018e1a remove string_view STL dependency (#703)
* remove string_view STL dependency

* fix

* inline string literal handling directly

* simplify dispatch
2024-12-15 07:46:49 +00:00
Matthew Parkinson
69e280c331 Minor update to clangformat file. (#694) 2024-11-22 12:40:16 +00:00
David CARLIER
fd560d472b Revert "custom memmove implementation proposal. (#593)" (#692)
* Revert "custom memmove implementation proposal. (#593)"

This reverts commit 01885f5a04.

* disable memmove fuzzing
2024-11-20 10:36:42 +00:00
David CARLIER
01885f5a04 custom memmove implementation proposal. (#593)
mostly like memcpy with optional bound checking but
capable of handling overlapping cases thus using
reverse copy instead.
2024-11-16 07:39:38 +00:00
Matthew Parkinson
97b7675670 Remove some unneeded headers (#680)
* Removed unneeded headers

This removes some unneeded headers from the headers.

* Remove use of std::string

This stack allocates and copies a c-string to replace the calls to std::string.
2024-10-06 09:14:56 +01:00
Nathaniel Wesley Filardo
416fd39f6a gcc UAF warning in test/perf/singlethread -malloc
When building test/perf/singlethread to use the system allocator, gcc
(Debian 14.2.0-3) correctly sees that we were using the value of a
pointer after it had been passed to the privileged free(), which is UB.

Flip the check and dealloc, so that we query the set of pointers we're
tracking first, using the pointer while the allocation is still live.
2024-09-21 14:48:04 +00:00
Nathaniel Wesley Filardo
19259095c6 Further gcc -Werror=array-bounds fix
In test/perf/startup, gcc (Debian 14.2.0-3) seems to get confused about
the size of the counters vector as the code was written.  Rewrite the
code to pass the same value (`std::thread::hardware_concurrency()`, but
in a local) to both `counters.resize()` and the `ParallelTest` ctor.
2024-09-21 10:32:28 +00:00
Nathaniel Filardo
8b95b9a916 Bottom commits from BatchIt (#675)
* msvc: set __cplusplus to the actual value in use

* ds_core/bits: add mask_bits; convert one_at_bit-s

* remotecache: enable reserve_space multiple objects

* nits

* Small changes to tracing

- Trace "Handling remote" once per batch, rather than per element

- Remote queue events also log the associated metaslab; we'll use this
  to assess the efficacy of https://github.com/microsoft/snmalloc/issues/634

* freelist builder: allow forcibly tracking length

* Try forward declaring freelist::Builder to appease macos-14

* freelist: tweak intra-slab obfuscation keys by meta address

* NFC: freelist: allow `next` to be arbitrary value

* Switch to a central, tweaked key for all free lists

* allocconfig: introduce some properties of slabs

We'll use these to pack values in message queues.

- Maximum distance between two objects in a single slab
- Maximum number of objects in a slab

* NFC: Templatize LocalCache on Config

* NFC: split dealloc_local_object_slow

We'll use the _slower form when we're just stepping a slab through
multiple rounds of state transition (to come), which can't involve
the actual memory object in question.

* NFC: make freelist::Object::T-s by placement new

* NFC: CoreAlloc: split dealloc_local_object

The pattern of `if (!fast()) { slow() }` occurs in a few places, including in
contexts where we already know the entry and so don't need to look it up.
2024-09-12 17:06:53 -04:00
Nathaniel Filardo
835ab51863 msgpass benchmark and its refactoring dependencies (#659)
* NFC: split freelist_queue from remoteallocator

This lets us use freelists as message queues in contexts other than
the remoteallocator.  No functional change indended.

* freelist_queue: add and use destroy_and_iterate

* freelist: make backptr obfuscation key "tweakable"

* freelist: tweakable keys in forward direction, too

* test/perf/msgpass: ubench a producer-consumer app

Approximate a message-passing application as a set of producers, a set of
consumers, and a set of proxies that do both.  We'll use this for some initial
insight for https://github.com/microsoft/snmalloc/issues/634 but it seems worth
having in general.
2024-06-13 17:28:48 -04:00
Matthew Parkinson
2a7eabef6c Configurable client meta-data (#662)
This provide a way to configure snmalloc to provide per object meta-data that is out of band. This can be used to provide different mitigations on top of snmalloc, such as storing memory tags in a compressed form, or provide a miracle pointer like feature.

This also includes a couple of TSAN fixes as it wasn't fully on in CI.
2024-06-13 09:32:07 -04:00
Nathaniel Filardo
846a926155 NFC: sizeclass: differentiate minimum step size and minimum allocation sizes (#651)
* Move sizeclass debugging code to sizeclass test

The sizeclass was already testing most of this, so just add the missing bits.
Forgo some tests whose failure would have implied earlier failures.

This moves the last dynamic call of size_to_sizeclass_const into tests
(and so, too, to_exp_mant_const).  sizeclasstable.h still contains a static
call to compute NUM_SMALL_SIZECLASSES from MAX_SMALL_SIZECLASS_SIZE.

* Remove unused to_exp_mant

Only its _const sibling is used, and little at that, now that almost everything
to do with sizes and size classes is table-driven.

* test/memcpy: trap, if we can, before exiting

This just means I don't need to remember to set a breakpoint on exit

* test/memcpy: don't assume sizeclass 0 is allocable

* test/memory: don't assume sizeclass 0 is allocable

* test/sizeclass: handle nonzero minimum sizeclasses

* sizeclass: distinguish min alloc and step size

Add support for a minimum allocation size that isn't the minimum step of
the sizeclass table.

* Expose MIN_ALLOC_{,STEP}_SIZE through cmake

* test/sizeclass: report MIN_ALLOC_{STEP_,}SIZE
2024-05-24 18:49:39 +01:00
Matthew Parkinson
f38ee89e72 Template construction of Pool elements (#641)
* Template construction of Pool elements

The Pool class is used by verona-rt.  The recent changes made this
less nice to consume as an API.

This change makes the construction logic a template parameter to the
Pool. This enables standard allocation to be used from Verona.

* Drop parameter from acquire

Pool::acquire took a list of parameters to initialise the object that it
constructed.  But if this was serviced from the pool, the parameter
would be ignored.  This is not an ideal API.

This PR removes the ability to pass a parameter.
2023-10-03 13:59:23 +00:00
Matthew Parkinson
5543347543 Startup improvements (#639)
* Benchmark for testing startup performance.

* Make pool pass spare space to pooled item

The pool will result in power of 2 allocations as it doesn't have a
local state when it is initially set up.

This commit passes this extra space to the constructor of the pooled
type, so that it can be feed into the freshly created allocator.

Co-authored-by: Nathaniel Wesley Filardo <nfilardo@microsoft.com>
2023-09-28 14:53:39 +01:00
Matthew Parkinson
9d4466093a Move to clang-format 15 (#621)
The current version requires clang-format-9.  This now getting hard to get.
This commit moves it to the clang-format-15, which is the latest in 22.04.

Also, updates clang-tidy to 15 as well.
2023-07-18 11:24:07 +01:00
Matthew Parkinson
55376aa006 Move key_global into RemoteAllocator (#608)
There was a mis-compilation in a Verona configuration that lead to
two instances of key_global existing.  This change moves it inside
a struct that seems to fix the issue.

The rest of the changes are limiting the use of key_global as both
RemoteCache and RemoteAllocator must use the same configuration,
so there is no need to take the key_global as a parameter.
2023-04-26 17:24:16 +01:00
Matthew Parkinson
7b3a2b3fc1 Made pool reuse a queue (#612)
So allocator churn will cause remote queues to be visited.
2023-04-26 15:08:30 +01:00
Matthew Parkinson
ccca98a709 Factor checks under separate feature flags. (#587)
All the checks and mitigations have been placed under feature flags.
These can be controlled by defining

  SNMALLOC_CHECK_CLIENT_MITIGATIONS

This can take a term that represents the mitigations that should be enabled.
E.g.
  -DSNMALLOC_CHECK_CLIENT_MITIGATIONS=nochecks+random_pagemap

The CMake uses this to build numerous versions of the LD_PRELOAD library and
tests to allow individual features to be benchmarked.

Co-authored-by: Nathaniel Wesley Filardo <nfilardo@microsoft.com>
2023-03-23 13:41:02 +00:00
Matthew Parkinson
4e88b42621 Towards heap walk (#569)
* Implement tracking full slabs and large allocations

This adds an additional SeqSet that is used to track all the fully
used slabs and large allocations.  This gives more chances to
detect memory leaks, and additionally catch some more UAF failures
where the object is not recycled.

* Make slabmeta track a slab interior pointer

Use the head of the free list builder to track an interior pointer to
the slab. This is unused unless the list contains something.
Hence, we can use this to represent an interior pointer to the slab and
report more accurate leaks.

* clangformat

* clangtidy

* clangtidy

* Clang tidy again.

* Fixing provenance.

* Clangformat

* Clang tidy.

* Add assert for sanity

* Make reinterpret_cast more descriptive.

Add an operation to get a tag free pointer from an address_t, and use it

* Clangformat

* CR

* Fix calculation of number of allocations.

* Fix calculation of number of allocations.

* Fix test
2022-12-20 13:36:10 +00:00
Nathaniel Wesley Filardo
0cd36f4eb2 StrictProvenance: plumb Authmaps through backends
No use of them, yet, though.
2022-12-14 17:46:53 +00:00
Nathaniel Wesley Filardo
7f3b59eaf9 PagemapRegisterRange: don't presume Pagemap entry type
To date, we've had exactly one kind of Pagemap and it held exactly one
type of thing, a descendant of class MetaEntryBase.

PagemapRegisterRange tacitly assumed that the Pagemap (adapter) it
interacted would therefore store entries that could have .set_boundary()
called on them.  But in general there's no requirement that this be
true; Pagemaps are generic data structures.

To enable reuse of the PagemapRegisterRange machinery more generally,
change the type of Pagemap::register_range() to take a pointer (rather
than an address) and move the MetaEntryBase-specific functionality to
the backend_helpers/pagemap adapter.
2022-12-14 17:46:53 +00:00
Nathaniel Wesley Filardo
06873ac366 pagemap: don't depend on SNMALLOC_CHECK_CLIENT
Instead, take a template parameter for the no-args init() method, so
that randomization can be disabled on StrictProvenance architectures
(CHERI), where we don't expect it to be useful, even when snmalloc is
being built to be otherwise paranoid.

Catch callsites up.
2022-12-14 17:46:53 +00:00
Matthew Parkinson
ddc5703cc8 Expose pool sort. (#565) 2022-10-10 17:13:10 +01:00
Matthew Parkinson
2f8f376db8 Pagemap Rounding (#558)
* Extend pagemap test

Check for possible overlap between heap and pagemap, but writing and
reading the heap.

* Return unalign memory from the pagemap

This commit allows the pagemap to return unaligned range of memory. This
means that bump allocation of multiple pagemaps doesn't
waste as much space.
2022-09-17 14:30:13 +01:00
Matthew Parkinson
11c18082d6 Make smallbuddy handle larger requests correctly (#556)
* Fail more abruptly if the bounds are not exact.

* Move bounding from Pool into Backend.

This commit makes the rounding and the bounding occur in the same
function.

* Enable smallbuddyrange to handle larger requests

The smallbuddy can now pass the larger requests up the range chain if
it cannot satisfy it itself.

* Test larger requests for meta-data.
2022-09-14 12:12:06 +01:00
Nathaniel Wesley Filardo
0ea12d9725 test/func/cheri: verify representability of sizeclasses 2022-09-14 10:10:14 +01:00
Nathaniel Wesley Filardo
ad9967b5a4 test/perf/memcpy: also emit snmalloc-checked times 2022-09-07 13:05:49 +01:00
Nathaniel Wesley Filardo
c4311b4a41 func/cheri: probe memcpy correctness
Fling some misaligned cases at our memcpy and check the results
2022-09-07 13:05:49 +01:00
Nathaniel Wesley Filardo
09bc0c6be7 NFC: external_pointer address_cast earlier
Make it easier to justify our avoidance of capptr_from_client and
capptr_reveal in external_pointer by performing address_cast earlier.
In particular, with this change, we can see that the pointer (and so its
authority, in CHERI) is not passed to any called function other than
address_cast and pointer_offset, and so authority is merely propagated
and neither exercised nor amplified.

Remove the long-disused capptr_reveal_wild, which was added for earlier
versions of external_pointer.
2022-07-07 16:57:47 +01:00
Nathaniel Wesley Filardo
b2c75dffb7 NFC: rename ConceptBound to IsBound 2022-07-07 16:57:47 +01:00
Nathaniel Wesley Filardo
da19291d4e RFC: Add tests for some CHERI-specific behaviors 2022-06-09 01:05:04 +01:00
Nathaniel Wesley Filardo
095e8f13db func-malloc: expand CHERI tests to check no-VMEM 2022-06-09 01:05:04 +01:00
Nathaniel Wesley Filardo
f41bb321f7 RFC: Hide CapPtr constructor
Expose a static CapPtr<T,B>::unsafe_from() and use that everywhere instead
(though continue to allow implicit and explicit construction of CapPtr from
nullptr).
2022-06-09 01:05:04 +01:00
David CARLIER
e7e558badf exposes macOs malloc_good_size (#538) 2022-06-07 16:09:55 +01:00
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
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
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
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
Matthew Parkinson
943bae1b34 Minimal example of #506 2022-04-11 21:07:05 +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
Matthew Parkinson
821620133d Remove ChunkAllocator 2022-03-24 08:01:09 +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