Граф коммитов

117 Коммитов

Автор SHA1 Сообщение Дата
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
David Chisnall b9b9055bbf
Try vm-actions instead of cross-platform-actions (#589)
This uses VirtualBox instead of xhyve.  It might be slower, but should
be more reliable.

Tests run on FreeBSD, NetBSD, and OpenBSD.  Only the FreeBSD ones are
passing at the moment, the others will keep running but aren't added as
dependencies for the action used to guard commits.
2023-02-20 12:05:53 +00:00
Nathaniel Wesley Filardo 704843d5ff Add BUILD_TESTING cmake option
As with CTest, but without the full machinery thereof.  This facilitates
package builders to use the usual build targets (all, install) without
needing to build the test programs if they're just going to get dropped
on the floor.
2022-12-16 16:43:47 +00:00
David CARLIER 5bb5701b45
LTO support proposal. (#573)
* LTO support proposal.

* giving choice
2022-12-09 20:30:56 +00:00
Matthias Wahl 6b0bda01c7
Support older linux systems (#545)
* Fix pal_linux.h for older linux systems

Where MADV_FREE is not defined - replaced with MADV_DONTNEED
Where GRND_NONBLOCK is not defined in <sys/random.h> but in <linux/random.h>

* Check for linux/random.h in CMake

as __has_include seems to not be reliable

* Use CMake module CheckIncludeFilesCXX

as C language isn't enabled by default everywhere

* Move madvise flag ifdefs into constexpr for cleaner code
2022-06-17 06:16:32 +01:00
Nathaniel Wesley Filardo 3e72ef63ad NFC: cmake: add SNMALLOC_LINK_ICF, default on
ICF currently breaks building on Morello, so allow cmake to notch it out.
2022-06-09 14:20:36 +01:00
Matthew Parkinson e17672d3c1
Missing PRIVATE in cmake. (#539) 2022-06-07 16:13:36 +01:00
Matthew Parkinson 563d5a5cee Make clang on Windows use /Debug 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
David Carlier bf54eeb7be New option to name reserved pages. 2022-04-18 21:01:40 +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
Matthew Parkinson a022a75b91
Fix Debug symbols for Windows CI. (#483) 2022-03-20 19:28:03 +00:00
David Carlier c27dd10c39 shared lib linkage build fix for OpenBSD.
disable purposely --no-undefined for this platform.
2022-02-28 09:16:16 +00:00
Matthew Parkinson 4f2d3ebf33 Enable sanitizers in CI. 2021-12-15 14:45:53 +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
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 20a114cb62 Add a timer to the PAL
This adds a way to periodically pool the PAL to see if any timers have
expired.  Timers can be used to periodically provide callbacks to the
rest of snmalloc.
2021-10-28 14:28:36 +01:00
Nathaniel Wesley Filardo eb0698fc09 CI: Add RISC-V 64 cross-build & qemu-user tests 2021-10-20 12:02:08 +01:00
David Chisnall 51e75bca89 Add memcpy with bounds checks.
The memcpy implementation is not completely stupid but is almost
certainly not as good as a carefully tuned and optimised one.

Building snmalloc with FreeBSD's libc memcpy + jemalloc and with this,
each 10 times, does not show a statistically significant performance
difference at 95% confidence.  The snmalloc version has very slightly
lower median and worst-case times.  This is in no way a sensible
benchmark, but it serves as a smoke test for significant performance
regressions.

The CI self-host job now uses the checked memcpy.

This also fixes an off-by-one error in the external bounds.  This is
triggered by ninja, so we will see breakage in CI if it is reintroduced.

In debug builds, we provide a verbose error containing the address of
the allocation, the base and bounds of the allocation, and a backtrace.

The backtrace was broken by the CI cleanup moving the BACKTRACE_HEADER
macro into the SNMALLOC_ namespace.  This is also fixed.

The test involves hijacking `abort`, which doesn't work everywhere.  It
also requires `backtrace` to work in configurations where stack traces
are enabled.  This is disabled in QEMU because `backtrace` appears to
crash reliably in QEMU user mode.

For now, in the -checks build configurations, we are hitting a slow path
in the pagemap on accesses so that the pages that are `PROT_NONE` don't
cause crashes.  These need to be made read-only, but this requires a PAL
change.
2021-09-16 13:53:13 +01:00
David Chisnall 6c5626fe5f Install test headers.
Verona uses these.
2021-09-07 08:45:20 +01:00
David Chisnall c70c23ad74
CMake cleanup. (#384)
Modernise and tidy the CMake a bit:

 - Use generator expressions for a lot of conditionals so that things
   are more reliable with multi-config generators (and less verbose).
 - Remove C as a needed language.  None of the code was C but we were
   using C to test if headers worked.  This was fragile because a build
   with `CMAKE_CXX_COMPILER` set might have checked things compiled with
   the system C compiler and then failed when the specified C++ compiler
   used different headers.
 - Rename the `BACKTRACE_HEADER` macro to `SNMALLOC_BACKTRACE_HEADER`.
   This is exposed into code that consumes snmalloc and so should be
   'namespaced' (to the degree that's possible with C macros).
 - Clean up the options and use dependent options to hide options 
   that are not always relevant.
 - Use functions instead of macros for better variable scoping.
 - Factor out some duplicated bits into functions.
 - Update to the latest way of telling CMake to use C++17 or C++20.
 - Migrate everything that's setting global properties to setting only
   per-target properties.
 - Link with -nostdlib++ if it's available.  If it isn't, fall back to
   enabling the C language and linking with the C compiler.
 - Make the per-test log messages verbose outputs.  These kept scrolling
   important messages off the top of the screen for me.
 - Make building as a header-only library a public option.
 - Add install targets that install all of the headers and provide a
   config option.  This works with the header-only configuration for
   integration with things like vcpkg.
 - Fix a missing `#endif` in the `malloc_useable_size` check.  This was
   failing co compile on all platforms because of the missing `#endif`.
 - Bump the minimum version to 3.14 so that we have access to
   target_link_options.  This is necessary to use generator expressions
   for linker flags.
 - Make the linker error if the shim libraries depend on symbols that
   are not defined in the explicitly-provided libraries.
 - Make the old-Ubuntu CI jobs use C++17 explicitly (previously CMake 
   was silently ignoring the fact that the compiler didn't support C++20)
 - Fix errors found by the more aggressive linking mode.

With these changes, it's now possible to install snmalloc and then, in
another project, do something like this:

```cmake
find_package(snmalloc CONFIG REQUIRED)
target_link_libraries(t1 snmalloc::snmalloc)
target_link_libraries(t2 snmalloc::snmallocshim-static)
```

In this example, `t1` gets all of the compile flags necessary to include
snmalloc headers for its build configuration.  `t2` is additionally
linked to the snmalloc static shim library.
2021-09-03 11:31:05 +01:00
Matthew Parkinson 0f70494d55 Enable passthrough to an underlying allocator
This passes though to an underlying allocator rather than using
snmalloc.  This is required for using ASAN in Verona.  Verona takes a
close coupling with snmalloc, but to use with ASAN would require a
more work, so we pass to the system allocator in this case.
2021-08-26 14:34:36 +01:00
Istvan Haller b84a7afb06
Merge pull request #381 from ihaller/ihaller/msvc17
Improved support for MSVC with C++17
2021-08-26 14:24:38 +01:00
Istvan Haller 935f3ccd29 Improved support for MSVC with C++17 2021-08-26 12:18:53 +01:00
Matthew Parkinson b52e2a6e27 Expose pthread feature flag
The code was able to use pthread destructors rather than C++ thread
local destructors.  This removes the dependence on a C++ .so on linux.
However, this is not stable on other platforms such as Apple. Where the
C++ thread local state can be cleared before the pthread destructor
runs.
2021-08-25 17:04:08 +01:00
Matthew Parkinson c1001ae7a4 Merge remote-tracking branch 'origin/master' into snmalloc2 2021-07-21 09:58:22 +01:00
Matthew Parkinson 9df0101dfd Enable guard pages in CHECK_CLIENT
Change the behaviour to use PROT_NONE for reservations in CHECK_CLIENT
mode.  This means that we only provide access once data is actually
being used.
2021-07-21 09:36:06 +01:00
Schrodinger ZHU Yifan 704bc423cc
refact(cmake): change mcx16 detection to facilitate cross compiling (#351)
* refact(cmake): change mcx16 detection to facilitate cross compiling

Signed-off-by: SchrodingerZhu <i@zhuyi.fan>
2021-07-20 08:59:44 +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
David Carlier 9e88691ce6 Fix still non getentropy platform builds 2021-06-01 20:35:52 +01:00
Istvan Haller aefd149f0a
Gated the mcx16 flag to C++ compilation (#332)
* Restricted mcx16 option to C++ files as only those can include the snmalloc header
2021-05-28 17:13:49 +01:00
Matthias Wahl 589ecfab02
Fix building on old libc systems without `getentropy` (#329)
Signed-off-by: Matthias Wahl <mwahl@wayfair.com>
2021-05-25 16:46:06 +01:00
Matthew Parkinson e77a5d9c58 Remove cache-friendly offset. 2021-05-18 14:58:15 +01:00
David Carlier 83e6208862 backtrace cmake vars build fix, got undetected if the system package include was already in the flags. 2021-05-06 11:54:51 +01:00
Nathaniel Filardo 71db09423f CMake: add SNMALLOC_USE_CXX20 option 2021-04-22 01:28:24 +01:00
Matthew Parkinson 578abd8db4
Randomise slab allocation pattern (#304)
The slab allocation pattern is randomised based on the deallocation
pattern.  This achieved by using two queues to enqueue free elements
onto.  We pick "randomly", which queue to add to, and then when we take
the free_queue to use, we splice the two queues together.
2021-03-24 16:12:22 +00:00
Matthew Parkinson d56a99a747 Remove USE_MEASURE
The initial performance monitoring for snmalloc used timing of small
operations to guide the design. This feature has not been maintained or
used for several years.

This commit removes the feature.
2021-03-21 19:00:54 +00:00
Matthew Parkinson 50f412157f Protect free list pointers stored in object space
Free list pointers can be exploited by attackers. This commit implements
a simple encoding scheme to detect corruption of the pointers.  This can
be used to detect UAF and double free.

This does not currently address anything for Medium or Large
allocations.  It also does not address cross thread deallocations.

Co-authored-by: Nathaniel Wesley Filardo <nfilardo@microsoft.com>
2021-03-19 11:28:39 +00:00
Matthew Parkinson afe53e71af Restrict clangformat search. 2021-03-19 11:28:39 +00:00
Nathaniel Filardo db0ca64ff3 NFC: Add an AAL Concept, too
While here, pull out some constants to their own header.  Eventually we'll
want to match on AalFeatures in the AAL Concept.
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 60cd538c28 Change behaviour of USE_POSIX_COMMIT_CHECKS
The previous setting applied USE_POSIX_COMMIT_CHECKS to snmalloc if it
was a non-release build.  This caused issues in CCF virtual mode, as it
was being built in RelWithDebInfo.

This commit changes the flag to be applied less, but for tests to always
apply the setting independent of build type.

This means that when snmalloc is being used as a library, it will be
off, unless explicitly requested.
2021-02-19 15:34:11 +00:00
David Chisnall c33f355736
Fix the sandbox use case and add a test. (#269)
Summary of changes:

- Add a new PAL that doesn't allocate memory, which can be used with a
  memory provider that is pre-initialised with a range of memory.
- Add a `NoAllocation` PAL property so that the methods on a PAL that 
  doesn't support dynamically reserving address space will never be
  called and therefore don't need to be implemented.
- Slightly refactor the memory provider class so that it has a narrower
  interface with LargeAlloc and is easier to proxy.
- Allow the address space manager and the memory provider to be
  initialised with a range of memory.

This may eventually also remove the need for (or, at least, simplify)
the Open Enclave PAL.

This commit also ends up with a few other cleanups:

 - The `malloc_useable_size` CMake test that checks whether the
   parameter is const qualified was failing on FreeBSD where this
   function is declared in `malloc_np.h` but where including
   `malloc.h` raises an error.  This should now be more robust.
 - The BSD aligned PAL inherited from the BSD PAL, which does not
   expose aligned allocation. This meant that it exposed both the
   aligned and non-aligned allocation interfaces and so happily
   accepted incorrect `constexpr` if blocks that expected one or 
   the other but accidentally required both to exist. The unaligned
   function is now deleted so the same failures that appear in CI should
   appear locally for anyone using this PAL.
2021-01-11 14:06:51 +00:00
David Chisnall c89f594b07 Add missing clang-format name. 2021-01-07 14:52:40 +00:00
David Carlier 08d0f42cc5 Solaris based system build fix.
malloc_usable_size or similar unsupported.
2020-11-27 15:26:37 +00:00
Robert Norton 0db4633ee7 Fix typo in macro definition for 16MiB shared library shim (#253).
This has been present since 4e1f5829a7 and resulted in the 16MiB shared library shim using the default 1MiB chunk size.
2020-10-12 15:27:59 +01:00
David Carlier 49b9856ed0 DragonFly support (userland).
- Close to OpenBSD as there is no malloc*size api nor arbritrary
 alignment support.
- Like FreeBSD, MAP_NORESERVE never had been implemented even tough
 still present in the header but not mentioned in the man page,
FreeBSD has reserved the value for another later usage seems
 DragonFly has just out of sync header.
2020-10-05 10:44:30 +01: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
David Carlier e615c33f7a Haiku debug build, rdynamic flag unsupported. 2020-09-14 09:43:18 +01:00
Nathaniel Filardo 3e21ea1f65 Add C++ concept for PAL
This will not be used unless the C++ standard version is raised to 20.  As
concepts and C++20 more generally are quite new, this does not do so.
Nevertheless, the use of concepts can improve the local development experience
as type mismatches are discovered earlier (at template invocation rather than
only during expansion).
2020-09-09 12:55:48 +01:00