Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
06eb84c
Move allocconfig.h from ds/ to ds_core/
mjp41 Mar 27, 2026
fad2d2a
Extract sizeclassstatic.h into ds_core/, move sizeclasstable.h to ds/
mjp41 Mar 27, 2026
07a0ee1
Make smallsizeclass_t a wrapper type
mjp41 Mar 27, 2026
4a9d5b2
Add alloc(smallsizeclass_t) fast path and libc entries
mjp41 Mar 27, 2026
d3a3914
Move pool.h/pooled.h from mem/ to ds/
mjp41 Mar 27, 2026
13ed7cd
Add external_pointer libc functions
mjp41 Mar 27, 2026
ac468b3
Create test library infrastructure
mjp41 Mar 27, 2026
d4ff893
Convert batch 9a tests to use testlib
mjp41 Mar 27, 2026
6d68c71
Convert perf tests to use testlib (batch 9d partial)
mjp41 Mar 27, 2026
2fda2eb
Convert remaining testlib-only tests (batch 9b/9c/9d)
mjp41 Mar 27, 2026
14ad0e6
Convert tests needing snmalloc_core.h to use testlib
mjp41 Mar 27, 2026
de83961
Move report_fatal_error/message definitions to ds_core/helpers.h
mjp41 Mar 27, 2026
2f65c2c
Remove unnecessary errno.h include from snmalloc_testlib.h
mjp41 Mar 28, 2026
01d650e
Convert protect_fork, multi_atexit, multi_threadatexit
mjp41 Mar 28, 2026
b1b1c13
Split allocconfig.h: extract sizeclassconfig.h
mjp41 Mar 28, 2026
f3448e6
Create mitigations/ layer between ds_aal/ and pal/
mjp41 Mar 28, 2026
2dba9af
Compile testlib-only tests once for both fast and check
mjp41 Mar 28, 2026
1a66f10
Document mitigations/ layer in include hierarchy README
mjp41 Mar 29, 2026
c4f359d
Use SNMALLOC_API macro for non-template API functions
mjp41 Mar 29, 2026
f26d146
Change remaining_bytes API to take const void* instead of address_t
mjp41 Mar 30, 2026
1e88f8c
Add libc::memcpy with Checked/ReadsChecked template parameters
mjp41 Mar 30, 2026
89b2361
Remove DefaultPal dependency from test/helpers.h
mjp41 Mar 30, 2026
6e453f9
Remove pal.h include from seqset test
mjp41 Mar 30, 2026
98d7950
Apply clangformat
mjp41 Mar 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ filegroup(
[
"src/snmalloc/**/*",
"src/test/*.h",
"src/test/*.cc",
"CMakeLists.txt",
],
),
Expand Down Expand Up @@ -39,6 +40,7 @@ CMAKE_FLAGS = {
"SNMALLOC_USE_SELF_VENDORED_STL": "OFF",
"SNMALLOC_IPO": "ON",
"USE_SNMALLOC_STATS": "ON",
"SNMALLOC_BUILD_TESTING": "OFF",
} | select({
":release_with_debug": {"CMAKE_BUILD_TYPE": "RelWithDebInfo"},
":release": {"CMAKE_BUILD_TYPE": "Release"},
Expand Down
61 changes: 60 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,16 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
set(TEST_CLEANUP ${SNMALLOC_CLEANUP})
endif()

# Tests that only include snmalloc_testlib.h (no snmalloc.h or snmalloc_core.h).
# These are mitigation-independent and can be compiled once, then linked
# against both fast and check testlib variants.
set(TESTLIB_ONLY_TESTS
bits first_operation memory memory_usage multi_atexit multi_threadatexit
redblack statistics teardown
contention external_pointer large_alloc lotsofthreads post_teardown
singlethread startup
)

function(make_tests TAG DEFINES)
foreach(TEST_CATEGORY ${TEST_CATEGORIES})
message(VERBOSE "Adding ${TAG}/${TEST_CATEGORY} tests")
Expand All @@ -488,7 +498,32 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
aux_source_directory(${TESTDIR}/${TEST_CATEGORY}/${TEST} SRC)
set(TESTNAME "${TEST_CATEGORY}-${TEST}-${TAG}")

add_executable(${TESTNAME} ${SRC})
# Check if this test is testlib-only (mitigation-independent).
# If so, compile the source once into an object library and reuse
# across flavours.
list(FIND TESTLIB_ONLY_TESTS ${TEST} _testlib_only_idx)
set(OBJLIBNAME "${TEST_CATEGORY}-${TEST}-obj")
if (NOT ${_testlib_only_idx} EQUAL -1 AND NOT TARGET ${OBJLIBNAME})
# First flavour to see this test creates the object library.
add_library(${OBJLIBNAME} OBJECT ${SRC})
target_link_libraries(${OBJLIBNAME} snmalloc)
target_compile_definitions(${OBJLIBNAME} PRIVATE "SNMALLOC_USE_${TEST_CLEANUP}")
add_warning_flags(${OBJLIBNAME})
if(SNMALLOC_SANITIZER)
target_compile_options(${OBJLIBNAME} PRIVATE -g -fsanitize=${SNMALLOC_SANITIZER} -fno-omit-frame-pointer)
if (${SNMALLOC_SANITIZER} MATCHES "thread")
target_compile_definitions(${OBJLIBNAME} PRIVATE SNMALLOC_THREAD_SANITIZER_ENABLED)
endif()
endif()
endif()

if (NOT ${_testlib_only_idx} EQUAL -1)
# Testlib-only: link the shared object library against this
# flavour's testlib.
add_executable(${TESTNAME} $<TARGET_OBJECTS:${OBJLIBNAME}>)
else()
add_executable(${TESTNAME} ${SRC})
endif()

if(SNMALLOC_SANITIZER)
target_compile_options(${TESTNAME} PRIVATE -g -fsanitize=${SNMALLOC_SANITIZER} -fno-omit-frame-pointer)
Expand All @@ -501,6 +536,10 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
add_warning_flags(${TESTNAME})

target_link_libraries(${TESTNAME} snmalloc)
# Link the pre-compiled test library into all tests. For tests
# that include snmalloc.h directly the linker will simply discard
# the duplicate inline definitions (ODR-safe).
target_link_libraries(${TESTNAME} snmalloc-testlib-${TAG})
target_compile_definitions(${TESTNAME} PRIVATE "SNMALLOC_USE_${TEST_CLEANUP}")

if (NOT DEFINES STREQUAL " ")
Expand Down Expand Up @@ -538,6 +577,24 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
endforeach()
endfunction()

function(build_test_library TAG DEFINES)
set(LIBNAME snmalloc-testlib-${TAG})
add_library(${LIBNAME} STATIC src/test/snmalloc_testlib.cc)
target_link_libraries(${LIBNAME} PRIVATE snmalloc)
set_target_properties(${LIBNAME} PROPERTIES INTERFACE_LINK_LIBRARIES "")
target_compile_definitions(${LIBNAME} PRIVATE "SNMALLOC_USE_${TEST_CLEANUP}")
if (NOT DEFINES STREQUAL " ")
target_compile_definitions(${LIBNAME} PRIVATE ${DEFINES})
endif()
if(SNMALLOC_SANITIZER)
target_compile_options(${LIBNAME} PRIVATE -g -fsanitize=${SNMALLOC_SANITIZER} -fno-omit-frame-pointer)
if (${SNMALLOC_SANITIZER} MATCHES "thread")
target_compile_definitions(${LIBNAME} PRIVATE SNMALLOC_THREAD_SANITIZER_ENABLED)
endif()
endif()
add_warning_flags(${LIBNAME})
endfunction()

if(NOT (DEFINED SNMALLOC_LINKER_FLAVOUR) OR ("${SNMALLOC_LINKER_FLAVOUR}" MATCHES "^$"))
# Linker not specified externally; probe to see if we can make lld work
set(CMAKE_REQUIRED_LINK_OPTIONS -fuse-ld=lld -Wl,--icf=all)
Expand Down Expand Up @@ -680,6 +737,7 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
set(DEFINES " ")
endif()

build_test_library(${FLAVOUR} ${DEFINES})
make_tests(${FLAVOUR} ${DEFINES})
endforeach()
endif()
Expand Down Expand Up @@ -743,6 +801,7 @@ install(DIRECTORY src/snmalloc/ds_aal DESTINATION include/snmalloc)
install(DIRECTORY src/snmalloc/ds_core DESTINATION include/snmalloc)
install(DIRECTORY src/snmalloc/global DESTINATION include/snmalloc)
install(DIRECTORY src/snmalloc/mem DESTINATION include/snmalloc)
install(DIRECTORY src/snmalloc/mitigations DESTINATION include/snmalloc)
install(DIRECTORY src/snmalloc/override DESTINATION include/snmalloc)
install(DIRECTORY src/snmalloc/pal DESTINATION include/snmalloc)
install(DIRECTORY src/snmalloc/stl DESTINATION include/snmalloc)
Expand Down
3 changes: 3 additions & 0 deletions src/snmalloc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ These are arranged in a hierarchy such that each of the directories may include
This layer provides abstractions over CPU-specific intrinsics and defines things such as the virtual address-space size.
There is a single AAL for an snmalloc instantiation.
- `ds_aal/` provides data structures that depend on the AAL.
- `mitigations/` provides compile-time configuration for security mitigations.
This includes the `mitigations()` function (controlled by `SNMALLOC_CHECK_CLIENT`), mitigation-dependent allocator constants, and CHERI capability checks.
Layers below this (`ds_core/`, `aal/`, `ds_aal/`) are mitigation-independent, which allows code that only includes those layers to be compiled once regardless of mitigation settings.
- `pal/` provides the platform abstraction layer (PAL).
This exposes OS- or environment-specific abstractions into the rest of the code.
An snmalloc instantiation may use more than one PAL, including ones provided by the user.
Expand Down
200 changes: 0 additions & 200 deletions src/snmalloc/ds/allocconfig.h

This file was deleted.

4 changes: 3 additions & 1 deletion src/snmalloc/ds/ds.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
#include "../ds_aal/ds_aal.h"
#include "../pal/pal.h"
#include "aba.h"
#include "allocconfig.h"
#include "combininglock.h"
#include "entropy.h"
#include "mpmcstack.h"
#include "pagemap.h"
#include "pool.h"
#include "pooled.h"
#include "sizeclasstable.h"
1 change: 0 additions & 1 deletion src/snmalloc/ds/mpmcstack.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "../ds_core/ds_core.h"
#include "aba.h"
#include "allocconfig.h"

namespace snmalloc
{
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/snmalloc/mem/pooled.h → src/snmalloc/ds/pooled.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "../ds/ds.h"
#include "backend_concept.h"
#include "../mem/backend_concept.h"

namespace snmalloc
{
Expand Down
Loading
Loading