1
- cmake_minimum_required (VERSION 3.14 )
1
+ cmake_minimum_required (VERSION 3.21 )
2
2
project (snmalloc CXX )
3
3
4
4
if (NOT CMAKE_BUILD_TYPE )
@@ -32,22 +32,29 @@ option(SNMALLOC_ENABLE_FUZZING "Enable fuzzing instrumentation tests" OFF)
32
32
option (SNMALLOC_USE_SELF_VENDORED_STL "Avoid using system STL" OFF )
33
33
# Options that apply only if we're not building the header-only library
34
34
cmake_dependent_option (SNMALLOC_RUST_SUPPORT "Build static library for rust" OFF "NOT SNMALLOC_HEADER_ONLY_LIBRARY" OFF )
35
+ cmake_dependent_option (SNMALLOC_RUST_LIBC_API "Include libc API in the rust library" OFF "SNMALLOC_RUST_SUPPORT" OFF )
35
36
cmake_dependent_option (SNMALLOC_STATIC_LIBRARY "Build static libraries" ON "NOT SNMALLOC_HEADER_ONLY_LIBRARY" OFF )
36
37
cmake_dependent_option (SNMALLOC_CHECK_LOADS "Perform bounds checks on the source argument to memcpy with heap objects" OFF "NOT SNMALLOC_HEADER_ONLY_LIBRARY" OFF )
37
38
cmake_dependent_option (SNMALLOC_OPTIMISE_FOR_CURRENT_MACHINE "Compile for current machine architecture" Off "NOT SNMALLOC_HEADER_ONLY_LIBRARY" OFF )
38
39
cmake_dependent_option (SNMALLOC_PAGEID "Set an id to memory regions" OFF "NOT SNMALLOC_PAGEID" OFF )
40
+
41
+ # GwpAsan secondary allocator
42
+ option (SNMALLOC_ENABLE_GWP_ASAN_INTEGRATION "Enable GwpAsan as a secondary allocator" OFF )
43
+ set (SNMALLOC_GWP_ASAN_INCLUDE_PATH "" CACHE PATH "GwpAsan header directory" )
44
+ set (SNMALLOC_GWP_ASAN_LIBRARY_PATH "" CACHE PATH "GwpAsan library directory" )
45
+
39
46
if (NOT SNMALLOC_HEADER_ONLY_LIBRARY )
40
47
# Pick a sensible default for the thread cleanup mechanism
41
48
if (${CMAKE_SYSTEM_NAME} STREQUAL FreeBSD )
42
49
set (SNMALLOC_CLEANUP_DEFAULT THREAD_CLEANUP )
43
50
elseif (UNIX AND NOT APPLE )
44
- set (SNMALLOC_CLEANUP_DEFAULT PTHREAD_DESTRUCTORS )
51
+ set (SNMALLOC_CLEANUP_DEFAULT CXX11_THREAD_ATEXIT_DIRECT )
45
52
else ()
46
53
set (SNMALLOC_CLEANUP_DEFAULT CXX11_DESTRUCTORS )
47
54
endif ()
48
55
# Specify the thread cleanup mechanism to use.
49
- set (SNMALLOC_CLEANUP ${SNMALLOC_CLEANUP_DEFAULT} CACHE STRING "The mechanism that snmalloc will use for thread destructors. Valid options are: CXX11_DESTRUCTORS (use C++11 destructors, may depend on the C++ runtime library), PTHREAD_DESTRUCTORS (use pthreads, may interact badly with C++ on some platforms, such as macOS) THREAD_CLEANUP (depend on an explicit call to _malloc_thread_cleanup on thread exit, supported by FreeBSD's threading implementation and possibly elsewhere)" )
50
- set_property (CACHE SNMALLOC_CLEANUP PROPERTY STRINGS THREAD_CLEANUP PTHREAD_DESTRUCTORS CXX11_DESTRUCTORS )
56
+ set (SNMALLOC_CLEANUP ${SNMALLOC_CLEANUP_DEFAULT} CACHE STRING "The mechanism that snmalloc will use for thread destructors. Valid options are: CXX11_DESTRUCTORS (use C++11 destructors, may depend on the C++ runtime library), CXX11_THREAD_ATEXIT_DIRECT (use the glibc call for libstdc++ directly), PTHREAD_DESTRUCTORS (use pthreads, may interact badly with C++ on some platforms, such as macOS) THREAD_CLEANUP (depend on an explicit call to _malloc_thread_cleanup on thread exit, supported by FreeBSD's threading implementation and possibly elsewhere)" )
57
+ set_property (CACHE SNMALLOC_CLEANUP PROPERTY STRINGS THREAD_CLEANUP PTHREAD_DESTRUCTORS CXX11_THREAD_ATEXIT_DIRECT CXX11_DESTRUCTORS )
51
58
52
59
set (SNMALLOC_STATIC_LIBRARY_PREFIX "sn_" CACHE STRING "Static library function prefix" )
53
60
set (SNMALLOC_COMPILER_SUPPORT_IPO FALSE )
@@ -114,6 +121,19 @@ int main()
114
121
SNMALLOC_LINKER_SUPPORT_NOSTDLIBXX )
115
122
set (CMAKE_REQUIRED_LINK_OPTIONS "" )
116
123
124
+ # Detect if pthread_atfork works
125
+ CHECK_CXX_SOURCE_COMPILES ("
126
+ #include <pthread.h>
127
+ void prepare() {}
128
+ void parent() {}
129
+ void child() {}
130
+ int main() {
131
+ pthread_atfork(prepare, parent, child);
132
+ return 0;
133
+ }
134
+ " SNMALLOC_PTHREAD_ATFORK_WORKS )
135
+
136
+
117
137
if (NOT MSVC AND NOT (SNMALLOC_CLEANUP STREQUAL CXX11_DESTRUCTORS ))
118
138
# If the target compiler doesn't support -nostdlib++ then we must enable C at
119
139
# the global scope for the fallbacks to work.
@@ -241,6 +261,20 @@ if(SNMALLOC_USE_SELF_VENDORED_STL)
241
261
target_compile_definitions (snmalloc INTERFACE SNMALLOC_USE_SELF_VENDORED_STL )
242
262
endif ()
243
263
264
+ if (SNMALLOC_ENABLE_GWP_ASAN_INTEGRATION )
265
+ if (NOT EXISTS ${SNMALLOC_GWP_ASAN_INCLUDE_PATH} )
266
+ message (FATAL_ERROR "GwpAsan cannot be enabled without setting SNMALLOC_GWP_ASAN_INCLUDE_PATH" )
267
+ endif ()
268
+ if (NOT EXISTS ${SNMALLOC_GWP_ASAN_LIBRARY_PATH} )
269
+ message (FATAL_ERROR "GwpAsan cannot be enabled without setting SNMALLOC_GWP_ASAN_LIBRARY_PATH" )
270
+ endif ()
271
+ message (STATUS "GwpAsan is enabled: ${SNMALLOC_GWP_ASAN_LIBRARY_PATH} /libclang_rt.gwp_asan-${CMAKE_SYSTEM_PROCESSOR} .a" )
272
+ target_include_directories (snmalloc INTERFACE ${SNMALLOC_GWP_ASAN_INCLUDE_PATH} )
273
+ target_link_directories (snmalloc INTERFACE ${SNMALLOC_GWP_ASAN_LIBRARY_PATH} )
274
+ target_compile_definitions (snmalloc INTERFACE -DSNMALLOC_ENABLE_GWP_ASAN_INTEGRATION )
275
+ target_link_libraries (snmalloc INTERFACE clang_rt.gwp_asan-${CMAKE_SYSTEM_PROCESSOR} )
276
+ endif ()
277
+
244
278
# https://learn.microsoft.com/en-us/cpp/build/reference/zc-cplusplus
245
279
if (MSVC )
246
280
target_compile_options (snmalloc INTERFACE "/Zc:__cplusplus" )
@@ -300,6 +334,7 @@ add_as_define(SNMALLOC_QEMU_WORKAROUND)
300
334
add_as_define (SNMALLOC_TRACING )
301
335
add_as_define (SNMALLOC_CI_BUILD )
302
336
add_as_define (SNMALLOC_PLATFORM_HAS_GETENTROPY )
337
+ add_as_define (SNMALLOC_PTHREAD_ATFORK_WORKS )
303
338
add_as_define (SNMALLOC_HAS_LINUX_RANDOM_H )
304
339
add_as_define (SNMALLOC_HAS_LINUX_FUTEX_H )
305
340
if (SNMALLOC_NO_REALLOCARRAY )
@@ -344,7 +379,7 @@ endif()
344
379
function (add_warning_flags name )
345
380
target_compile_options (${name} PRIVATE
346
381
$< $< CXX_COMPILER_ID:MSVC> :/Zi /W4 /WX /wd4127 /wd4324 /wd4201>
347
- $< $< NOT:$< OR:$< CXX_COMPILER_ID:MSVC> ,$< STREQUAL:${CMAKE_CXX_SIMULATE_ID} ,MSVC> > > :-fno-exceptions -fno- rtti -Wall -Wextra -Werror -Wundef>
382
+ $< $< NOT:$< OR:$< CXX_COMPILER_ID:MSVC> ,$< STREQUAL:${CMAKE_CXX_SIMULATE_ID} ,MSVC> > > :-fno-rtti -Wall -Wextra -Werror -Wundef>
348
383
$< $< CXX_COMPILER_ID:Clang> :-Wsign-conversion -Wconversion> )
349
384
target_link_options (${name} PRIVATE
350
385
$< $< BOOL:${SNMALLOC_LINKER_SUPPORT_NO_ALLOW_SHLIB_UNDEF} > :-Wl,--no-undefined>
@@ -465,32 +500,32 @@ if(NOT SNMALLOC_HEADER_ONLY_LIBRARY)
465
500
if (SNMALLOC_OPTIMISE_FOR_CURRENT_MACHINE )
466
501
check_cxx_compiler_flag (-march=native SUPPORT_MARCH_NATIVE )
467
502
if (NOT SUPPORT_MARCH_NATIVE )
468
- message_once (WARNING "Compiler does not support `-march=native` required by SNMALLOC_OPTIMISE_FOR_CURRENT_MACHINE" )
503
+ message (WARNING "Compiler does not support `-march=native` required by SNMALLOC_OPTIMISE_FOR_CURRENT_MACHINE" )
469
504
set (SNMALLOC_OPTIMISE_FOR_CURRENT_MACHINE FALSE )
470
505
endif ()
471
- endif ()
506
+ endif ()
472
507
473
508
474
- function (add_shim name type )
475
- add_library (${name} ${type } ${ARGN} )
509
+ function (compile name TYPE ${ARGN} )
510
+ add_library (${name} ${TYPE } ${ARGN} )
476
511
target_link_libraries (${name} snmalloc )
477
- set_target_properties (${name} PROPERTIES CXX_VISIBILITY_PRESET hidden INTERPROCEDURAL_OPTIMIZATION ${SNMALLOC_COMPILER_SUPPORT_IPO} )
478
512
target_compile_definitions (${name} PRIVATE "SNMALLOC_USE_${SNMALLOC_CLEANUP} " )
479
513
480
- if (MSVC )
481
- target_compile_definitions (${name} INTERFACE -D_HAS_EXCEPTIONS=0 )
482
- endif ()
483
-
484
514
add_warning_flags (${name} )
485
515
if (NOT MSVC )
486
516
target_compile_definitions (${name} PRIVATE "SNMALLOC_EXPORT=__attribute__((visibility(\" default\" )))" )
487
- target_compile_options (${name} PRIVATE
488
- -fomit-frame-pointer -ffunction-sections )
517
+ set_target_properties (${name} PROPERTIES CXX_VISIBILITY_PRESET hidden INTERPROCEDURAL_OPTIMIZATION ${SNMALLOC_COMPILER_SUPPORT_IPO} )
518
+ # Make debugging harder, and code faster.
519
+ target_compile_options (${name} PRIVATE -fomit-frame-pointer )
520
+
521
+ set_property (TARGET ${name} PROPERTY POSITION_INDEPENDENT_CODE ON )
489
522
523
+ # Check for prefetch write support.
490
524
check_cxx_compiler_flag ("-Werror -Wextra -Wall -mprfchw" SUPPORT_PREFETCH_WRITE )
491
525
if (SUPPORT_PREFETCH_WRITE )
492
526
target_compile_options (${name} PRIVATE -mprfchw )
493
527
endif ()
528
+
494
529
# Static TLS model is unsupported on Haiku.
495
530
if (SNMALLOC_STATIC_MODE_TLS )
496
531
target_compile_options (${name} PRIVATE -ftls-model=initial-exec )
@@ -501,18 +536,8 @@ endif()
501
536
target_compile_options (${name} PRIVATE -march=native )
502
537
endif ()
503
538
504
- # Ensure that we do not link against C++ stdlib when compiling shims.
505
- # If the compiler supports excluding the C++ stdlib implementation, use
506
- # it. Otherwise, fall back to linking the library as if it were C, which
507
- # has roughly the same effect.
508
- if (NOT ${SNMALLOC_CLEANUP} STREQUAL CXX11_DESTRUCTORS )
509
- if (SNMALLOC_LINKER_SUPPORT_NOSTDLIBXX )
510
- target_link_options (${name} PRIVATE -nostdlib++ )
511
- else ()
512
- set_target_properties (${name} PROPERTIES LINKER_LANGUAGE C )
513
- endif ()
514
- endif ()
515
539
# Remove all the duplicate new/malloc and free/delete definitions
540
+ target_compile_options (${name} PRIVATE -ffunction-sections )
516
541
target_link_options (${name} PRIVATE $< $< BOOL:${LLD_WORKS} > :$< $< BOOL:${SNMALLOC_LINK_ICF} > :-Wl,--icf=all> -fuse-ld=lld> )
517
542
endif ()
518
543
@@ -521,55 +546,65 @@ endif()
521
546
target_compile_definitions (${name} PRIVATE
522
547
SNMALLOC_PAGEID=$<IF:$<BOOL:${SNMALLOC_PAGEID}>,true,false> )
523
548
524
- install (TARGETS ${name} EXPORT snmallocConfig )
549
+ if (MSVC )
550
+ else ()
551
+ # We can't do --nostdlib++ as we are using exceptions, but we
552
+ # can make it a static dependency, which we aren't really using
553
+ # as the interesting stuff for exceptions is in libgcc_s
554
+ target_link_options (${name} PRIVATE -static-libstdc++ )
555
+ endif ()
525
556
557
+ if (SNMALLOC_RUST_LIBC_API )
558
+ target_compile_definitions (${name} PRIVATE SNMALLOC_RUST_LIBC_API )
559
+ endif ()
560
+ install (TARGETS ${name} EXPORT snmallocConfig )
526
561
endfunction ()
527
562
528
- set (SHIM_FILES src/snmalloc/override/malloc.cc src/snmalloc/override/new.cc )
529
- set (SHIM_FILES_MEMCPY src/snmalloc/override/memcpy.cc )
563
+ # Various files for overriding libc/rust behaviours.
564
+ set (MALLOC src/snmalloc/override/malloc.cc )
565
+ set (NEW src/snmalloc/override/new.cc )
566
+ set (MEMCPY src/snmalloc/override/memcpy.cc )
567
+ set (RUST src/snmalloc/override/rust.cc )
530
568
531
- add_shim (snmalloc-new-override STATIC src/snmalloc/override/new.cc )
569
+ set (ALLOC ${MALLOC} ${NEW} )
570
+ set (ALL ${ALLOC} ${MEMCPY} )
532
571
533
572
if (SNMALLOC_STATIC_LIBRARY )
534
- add_shim (snmallocshim-static STATIC ${SHIM_FILES } )
573
+ compile (snmallocshim-static STATIC ${ALLOC } )
535
574
target_compile_definitions (snmallocshim-static PRIVATE
536
575
SNMALLOC_STATIC_LIBRARY_PREFIX=${SNMALLOC_STATIC_LIBRARY_PREFIX} )
537
576
endif ()
538
577
578
+ compile (snmalloc-new-override STATIC ${NEW} )
579
+
539
580
if (NOT WIN32 )
540
- add_shim (snmallocshim SHARED ${SHIM_FILES} )
581
+ compile (snmallocshim SHARED ${ALLOC} )
582
+
541
583
if (SNMALLOC_MEMCPY_OVERRIDE )
542
- add_shim (snmallocshim-checks-memcpy-only SHARED ${SHIM_FILES} ${SHIM_FILES_MEMCPY } )
543
- add_shim (snmallocshim-checks SHARED ${SHIM_FILES} ${SHIM_FILES_MEMCPY } )
584
+ compile (snmallocshim-checks-memcpy-only SHARED ${ALL } )
585
+ compile (snmallocshim-checks SHARED ${ALL } )
544
586
else ()
545
- add_shim (snmallocshim-checks SHARED ${SHIM_FILES } )
587
+ compile (snmallocshim-checks SHARED ${ALLOC } )
546
588
endif ()
547
589
target_compile_definitions (snmallocshim-checks PRIVATE SNMALLOC_CHECK_CLIENT )
590
+
591
+ compile (snmalloc-minimal SHARED ${MALLOC} )
592
+ target_compile_options (snmalloc-minimal PRIVATE -fno-exceptions )
593
+ if (SNMALLOC_LINKER_SUPPORT_NOSTDLIBXX AND NOT ${SNMALLOC_CLEANUP} STREQUAL CXX11_DESTRUCTORS )
594
+ target_compile_options (snmalloc-minimal PRIVATE -fno-exceptions -nostdlib++ )
595
+ endif ()
548
596
endif ()
549
597
550
598
if (SNMALLOC_RUST_SUPPORT )
551
- add_shim (snmallocshim-rust STATIC src/snmalloc/override/rust.cc )
552
- add_shim (snmallocshim-checks-rust STATIC src/snmalloc/override/rust.cc )
599
+ compile (snmallocshim-rust STATIC ${RUST} )
600
+ compile (snmallocshim-checks-rust STATIC ${RUST} )
553
601
target_compile_definitions (snmallocshim-checks-rust PRIVATE SNMALLOC_CHECK_CLIENT )
554
602
endif ()
555
603
556
604
if (SNMALLOC_BUILD_TESTING )
557
- if (WIN32
558
- OR (CMAKE_SYSTEM_NAME STREQUAL NetBSD )
559
- OR (CMAKE_SYSTEM_NAME STREQUAL OpenBSD )
560
- OR (CMAKE_SYSTEM_NAME STREQUAL SunOS ))
561
- # Windows does not support aligned allocation well enough
562
- # for pass through.
563
- # NetBSD, OpenBSD and DragonFlyBSD do not support malloc*size calls.
564
- set (FLAVOURS fast;check )
565
- else ()
566
- set (FLAVOURS fast;check;malloc )
567
- endif ()
605
+ set (FLAVOURS fast;check )
568
606
569
607
foreach (FLAVOUR ${FLAVOURS} )
570
- if (${FLAVOUR} STREQUAL "malloc" )
571
- set (DEFINES SNMALLOC_PASS_THROUGH )
572
- endif ()
573
608
if (${FLAVOUR} STREQUAL "check" )
574
609
set (DEFINES SNMALLOC_CHECK_CLIENT )
575
610
endif ()
@@ -599,7 +634,7 @@ endif()
599
634
600
635
foreach (MITIGATION ${MITIGATIONS} )
601
636
set (DEFINES "SNMALLOC_CHECK_CLIENT_MITIGATIONS=${MITIGATION} " )
602
- add_shim (snmallocshim-${MITIGATION} SHARED ${SHIM_FILES } )
637
+ compile (snmallocshim-${MITIGATION} SHARED ${ALLOC } )
603
638
target_compile_definitions (snmallocshim-${MITIGATION} PRIVATE ${DEFINES} )
604
639
if (SNMALLOC_BUILD_TESTING )
605
640
make_tests (${MITIGATION} ${DEFINES} )
@@ -614,7 +649,7 @@ endif()
614
649
set (MITIGATIONSET "${MITIGATIONSET} +${MITIGATION} " )
615
650
message (STATUS "MITIGATIONSET: ${COUNT} -> ${MITIGATIONSET} " )
616
651
set (DEFINES "-DSNMALLOC_CHECK_CLIENT_MITIGATIONS=${MITIGATIONSET} " )
617
- add_shim (snmallocshim-${MITIGATIONNAME} SHARED ${SHIM_FILES } )
652
+ compile (snmallocshim-${MITIGATIONNAME} SHARED ${ALLOC } )
618
653
target_compile_definitions (snmallocshim-${MITIGATIONNAME} PRIVATE ${DEFINES} )
619
654
if (SNMALLOC_BUILD_TESTING )
620
655
make_tests (${MITIGATIONNAME} ${DEFINES} )
@@ -633,19 +668,24 @@ install(TARGETS EXPORT snmallocConfig DESTINATION ${CMAKE_INSTALL_LIBDIR}
633
668
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} /snmalloc )
634
669
635
670
install (DIRECTORY src/snmalloc/aal DESTINATION include /snmalloc )
636
- install (DIRECTORY src/snmalloc/ds DESTINATION include /snmalloc )
637
- install (DIRECTORY src/snmalloc/override DESTINATION include /snmalloc )
638
671
install (DIRECTORY src/snmalloc/backend DESTINATION include /snmalloc )
672
+ install (DIRECTORY src/snmalloc/backend_helpers DESTINATION include /snmalloc )
673
+ install (DIRECTORY src/snmalloc/ds DESTINATION include /snmalloc )
674
+ install (DIRECTORY src/snmalloc/ds_aal DESTINATION include /snmalloc )
675
+ install (DIRECTORY src/snmalloc/ds_core DESTINATION include /snmalloc )
676
+ install (DIRECTORY src/snmalloc/global DESTINATION include /snmalloc )
639
677
install (DIRECTORY src/snmalloc/mem DESTINATION include /snmalloc )
678
+ install (DIRECTORY src/snmalloc/override DESTINATION include /snmalloc )
640
679
install (DIRECTORY src/snmalloc/pal DESTINATION include /snmalloc )
680
+ install (DIRECTORY src/snmalloc/stl DESTINATION include /snmalloc )
641
681
install (FILES
642
682
src/test/measuretime.h
643
683
src/test/opt.h
644
684
src/test/setup.h
645
685
src/test/usage.h
646
686
src/test/xoroshiro.h
647
687
DESTINATION include /snmalloc/test
648
- )
688
+ )
649
689
install (FILES src/snmalloc/snmalloc.h;src/snmalloc/snmalloc_core.h;src/snmalloc/snmalloc_front.h DESTINATION include /snmalloc )
650
690
651
691
install (EXPORT snmallocConfig
0 commit comments