Commit Graph

972 Commits

Author SHA1 Message Date
Robert Norton
af8ab2daf6 Clear freelist pointers on allocation for CHERI or CHECK_CLIENT builds.
This is especially important on CHERI to avoid leaking capabilities to
the freelist. In the CHERI case we also zero in clear_slab (see comment).
Also add a check in the malloc functional test that there are no valid
capabilities in the returned allocation.
2022-02-20 20:30:28 +00:00
David Chisnall
baf35cc80e Return a small allocation from realloc(ptr, 0). (#460)
An annoying amount of real-world code (e.g. mandoc, BSD sort) treats a
NULL return from `realloc` as a failure, even when requesting a size of
0.  This code is wrong (the standard explicitly permits a return of NULL
from realloc when given a size 0) but working around it in snmalloc is
easier than fixing it everywhere.
2022-02-11 15:11:15 +00:00
Matthew Parkinson
eb00f3184f Remove local state from Pagemap representation. 2022-02-11 13:21:46 +00:00
Matthew Parkinson
be98a27bf1 Improve RBTree
* Logging improvments
* No longer need for the representation to have a min and max.
2022-02-11 13:21:46 +00:00
Matthew Parkinson
69c824d54d Expose mutable reference to pagemap 2022-02-11 13:21:46 +00:00
Matthew Parkinson
79486b6331 Rearrange templates for buddy allocator. 2022-02-11 13:21:46 +00:00
David Chisnall
79ea08779b Add jemalloc compat functions. (#456)
This adds the full set of jemalloc functions that FreeBSD's libc
exposes, including some (the `*allocm` family) that are gone from newer
versions of jemalloc and the `*allocx` family that replaced them.  These
are not necessarily efficient implementations but they should allow
snmalloc to replace jemalloc without any ABI breakage (in the loosest
possible sense).

Jemalloc provides a very generic sysctl-like mechanism for setting and
getting some values.  These are all implemented to return the
not-supported error code.  This may break code that expects that they
will succeed.

In particular, these APIs are used to register custom backing-store
allocators and to manage caches and arenas.  These concepts don't map
directly onto snmalloc and attempting to do so would almost certainly
not provide the same performance characteristics and so it's better to
`LD_PRELOAD` jemalloc (or explicitly link to it) for programs that gain
a significant speedup from this.
2022-02-09 10:49:02 +00:00
Robert Norton
20ddf8a150 Fix bug setting CHERI bounds in address_space_core.h.
When splitting a power of two aligned chunk into two we need to set
bounds on both halves and not just the top half.
2022-02-08 14:17:43 +00:00
Robert Norton
539937336d Extend malloc test to check CHERI capability length. 2022-02-08 14:17:43 +00:00
Matthew Parkinson
63d3928687 Add a red-black tree implementation and testing. 2022-02-01 17:18:36 +00:00
Matthew Parkinson
3d1b973480 Add DEBUG constexpr
Enable checking use of a constexpr rather than ifdef for checking if in
DEBUG.
2022-02-01 17:18:36 +00:00
David Chisnall
f1be609cdb Small fixes for snmalloc used in FreeBSD libc. (#454)
- Mark the hook that we're exporting for the threading library to call
   to clean up per-thread malloc state as 'used'.  It was changed to
   `inline` to allow duplicate copies of it to be merged but this also
   means that it isn't emitted at all in compilation units that don't
   use it (and it isn't used internally at all).
 - Fix the `__je_bootstrap_*` functions, which are used to bootstrap TLS
   allocation, for the changes to `ScopedAllocator`.

The `__je_bootstrap*` functions weren't being built in CI.  They now are
for non-PIE targets with a smoke test.
2022-01-21 14:19:26 +00:00
Matthew Parkinson
ef64f6c31b Improve check_bounds init check. 2022-01-10 16:29:06 +00:00
Matthew Parkinson
419347ba4a Optimise guarded memcpy (#449)
* Improve testing of memcpy including adding perf test.

* Change remaining_bytes to be branch free.

Use reciprocal division followed by multiply to remove a branch.
2022-01-07 17:09:13 +00:00
David Chisnall
4ea978b946 Remove fake_large_remote
Since #441 was merged, pagemap entries are no longer ever set to
fake_large_remote.
2022-01-07 16:29:11 +00:00
Schrodinger ZHU Yifan
74a2da177f try enabling windows-2022 (#445)
* try enable windows-2022

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* only test MSVC on windows 2022

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
2022-01-07 11:24:56 +00:00
David CARLIER
a77890c6ee PAL netbsd (temporary until some time 2022) build fix. (#450)
std::random_device seems unimplemented on this platform
 thus falling back to the usual /dev/urandom device for
the time being.
2022-01-07 11:24:35 +00:00
Matthew Parkinson
61314f2260 Post large deallocations to original thread (#441)
* Post large deallocations to original thread

This change sets all large allocations to be owned by the originating
thread. This means they will be messaged back to the original thread
before they can be reused.

The following reason for making this change:
* This will improve producer/consumer apps involving large allocations.
* It enables the implementation of a more complex chunk allocator that
reassembles chunks.
* It addresses an issue with compartmentalisation where the handling of
large allocations can result in meta-data ownership changing.
2021-12-17 14:08:08 +00:00
Nathaniel Wesley Filardo
3fb7c98364 That's snmalloc_check_client to you.
c299826f58 landed as part of #428 while #416 was
still outstanding and not all conflicts are textual.  Sorry!
2021-12-17 13:45:42 +00:00
Matthew Parkinson
927575dc86 Increase barrier strength on ABA. 2021-12-17 13:16:01 +00:00
Matthew Parkinson
16875382c4 Make explicitly racy part of stack
The MPMCStack has a race that is necessary for implementing optimistic
non-blocking data-structures.  This commit makes TSAN ignore this race.
2021-12-17 13:16:01 +00:00
Matthew Parkinson
84ac360445 Make MPSCQ invariant thread-safe 2021-12-17 13:16:01 +00:00
Matthew Parkinson
91d500baf8 Fix flag lock race condition in Debug. 2021-12-17 13:16:01 +00:00
Matthew Parkinson
be85d53c20 Factor out sanitizer run.
Don't fail on sanitizer failure and provide separate CI jobs
for sanitizer builds.
2021-12-17 13:16:01 +00:00
Nathaniel Wesley Filardo
b777243981 Additional CHERI client checks 2021-12-16 19:25:09 +00:00
Nathaniel Wesley Filardo
22a05b4a3c CHERI: dealloc() use cap base, not address 2021-12-16 19:25:09 +00:00
Nathaniel Wesley Filardo
3a509d41f0 CHERI: Avoid traps on nullptr
The CHERI-RISC-V `CAndPerm` and `CSetBoundsExact` instructions trap on untagged
inputs, so avoid passing `nullptr` to primitives that become those instructions.
2021-12-16 19:25:09 +00:00
Nathaniel Wesley Filardo
13a4b0471e CHERI: address_space_core should bound more
When reserving with leftover space, bound both the reservation and the residual
pointer.  This may be excessive?
2021-12-16 19:25:09 +00:00
Nathaniel Wesley Filardo
9b60e8256d CHERI: update StrictProvenance.md for new world order 2021-12-16 19:25:09 +00:00
Matthew Parkinson
4f2d3ebf33 Enable sanitizers in CI. 2021-12-15 14:45:53 +00:00
Matthew Parkinson
0fd5c37563 Change MPSCQ to use acquire
TSAN complained that there was a race that after some thoughts appears
to be due to this exchange needing to be an `acquire`.

I still wonder if the data dependence that is threaded through the
exchange induces enough order.
2021-12-15 14:45:53 +00:00
Matthew Parkinson
7768765fe8 Fixed locking around notification
Deduplication locking had test_and_set incorrect direction.
2021-12-15 14:45:53 +00:00
Matthew Parkinson
f398545154 Fix memalign usage
- Alignment should be above sizeof(void*)
- Don't call with very large sizes
2021-12-15 14:45:53 +00:00
Schrodinger ZHU Yifan
37d2ac42af use ::write and ::fsync in error path (#443)
* use ::write and ::fsync in error path

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* mark return value unused

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* adjust fsync positions

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
2021-12-15 14:01:34 +00:00
Matthew Parkinson
d0df2d028a Fix CI branch name. (#442) 2021-12-13 14:36:12 +00:00
David CARLIER
360efa2123 export netbsd's reallocarr proposal. (#433)
* export netbsd's reallocarr proposal.

acts subtly differently from reallocarray, returns an error code
and first argument as receiver.

* not export by default

* ci tests

* apply suggestions

* doc addition

* Apply suggestions from code review

Co-authored-by: Matthew Parkinson <mjp41@users.noreply.github.com>
2021-12-02 14:49:32 +00:00
Matthew Parkinson
7f3642e05c Change external thread_alloc example. (#424)
On Open Enclave having the `local_alloc` directly in thread-local
storage was causing a crash.  This changes the `local_alloc` to be
indirected, and thus puts less pressure on the thread-local storage.

The test also has deals with how to allocate before a thread-local
storage has been established.
2021-12-02 10:39:44 +00:00
Schrodinger ZHU Yifan
71d5bb8756 add SYS_getrandom fallback for posix PAL (#431)
* add SYS_getrandom fallback for posix PAL

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* address CR

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* guard and comments

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* more fallback behavior

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* fix random device fd

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* special handling for EAGAIN

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
2021-12-02 10:38:50 +00:00
Matthew Parkinson
9329db102f Remove Win2016 as Github Action support ending. 2021-12-02 09:58:02 +00:00
Matthew Parkinson
894b0314c9 Add timeout to ci tests.
Windows can hang due to assert failures in CI.  Add a timeout to get
some information of what is happening.
2021-12-02 09:58:02 +00:00
Schrodinger ZHU Yifan
9b548ed3a2 Add ownership checkings for FlagLock under debug (#432)
* add ownership checkings for FlagLock under debug

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* fix owner reset ordering

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* addresss CR

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* fix include and constexpr problem

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* use thread_local variable as thread identity

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* change default form for initialization

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* address CR

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* fix typo and format

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* add more assertions

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* adjust flag lock and comments

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* address CR

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
2021-12-02 09:18:24 +00:00
David Carlier
b1da339b3e build fix proposal for GCC 11.x (spotted with 11.2.0).
build error due to access none attribute on the pthread_setspecific's
value argumenti (glibc).
2021-12-01 10:05:52 +00:00
Matthew Parkinson
5fd3288997 Modify heuristic for adding new slabs. (#429)
If there is only one slab remaining, then we probabalisticly allocator a
new one. If a slab is barely in use, then this could cause us to
effectively double the number of slabs in use.

This commit checks if the remaining slab has enough remaining elements
to provide randomisation.
2021-11-25 13:43:50 +00:00
Matthew Parkinson
c299826f58 Improve codegen for checks.
Use fail fast in release to avoid stack frame for error reporting.

Scope check_client macro.
2021-11-25 08:22:28 +00:00
Matthew Parkinson
a8ef963ed7 Small comment 2021-11-25 08:22:28 +00:00
Matthew Parkinson
d725beca90 Minor improvement to codegen for remaining_bytes 2021-11-25 08:22:28 +00:00
David CARLIER
9332557bb0 memory unit test android build fix. (#427) 2021-11-24 12:12:47 +00:00
David CARLIER
e24130137d solaris systems build fix (#425) 2021-11-22 09:47:30 +00:00
Schrodinger ZHU Yifan
8e5514bd5a clean up unused usages (#421)
* clean up unused usages

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* remove names for arg pack

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* fix namespace in setup.h

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* format

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>

* set UNUSED as fast path

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
2021-11-19 13:23:39 +00:00
David Carlier
f731bc169b build fix for some 3rd party oses. 2021-11-18 09:08:17 +00:00