Skip to content

Commit

Permalink
perf: add condidates for msan (#84)
Browse files Browse the repository at this point in the history
* perf: add condidates for msan

Signed-off-by: msclock <msclock@qq.com>

* style: refine verbose output for sanitizers

Signed-off-by: msclock <msclock@qq.com>

---------

Signed-off-by: msclock <msclock@qq.com>
  • Loading branch information
msclock authored Apr 3, 2024
1 parent 9f9f6c5 commit 3bb437a
Showing 1 changed file with 18 additions and 77 deletions.
95 changes: 18 additions & 77 deletions cmake/build/Sanitizer.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,82 +22,6 @@ include_guard(GLOBAL)

include(${CMAKE_CURRENT_LIST_DIR}/../Common.cmake)

message(
VERBOSE
"Sanitizer
---------------
Sanitizers are tools that perform checks during a program’s runtime and
returns issues, and as such, along with unit testing, code coverage and static
analysis, is another tool to add to the programmers toolbox. And of course,
like the previous tools, are tragically simple to add into any project using
CMake, allowing any project and developer to quickly and easily use.
A quick rundown of the tools available, and what they do:
LeakSanitizer detects memory leaks, or issues where memory is allocated and
never deallocated, causing programs to slowly consume more and more memory,
eventually leading to a crash.
AddressSanitizer
^^^^^^^^^^^^^^^^
AddressSanitizer is a fast memory error detector. It is useful for detecting
most issues dealing with memory, such as:
- Out of bounds accesses to heap, stack, global
- Use after free
- Use after return
- Use after scope
- Double-free, invalid free
- Memory leaks (using LeakSanitizer)
ThreadSanitizer
^^^^^^^^^^^^^^^
ThreadSanitizer detects data races for multi-threaded code.
UndefinedSanitinzer
^^^^^^^^^^^^^^^^^^^^^^^^^^^
UndefinedSanitinzer detects the use of various features of C/C++ that
are explicitly listed as resulting in undefined behaviour. Most notably:
- Using misaligned or null pointer.
- Signed integer overflow
- Conversion to, from, or between floating-point types which would overflow the destination
- Division by zero
- Unreachable code
MemorySanitizer
^^^^^^^^^^^^^^^
MemorySanitizer detects uninitialized reads.
CFI
^^^
Control Flow Integrity is designed to detect certain forms of undefined
behaviour that can potentially allow attackers to subvert the program's
control flow. These are used by declaring the USE_SANITIZER CMake variable as
string containing any of:
- Address
- Memory
- Undefined
- Thread
- Leak
- CFI
Multiple values are allowed, e.g. -DUSE_SANITIZER=Address,Leak but some
sanitizers cannot be combined together, e.g.-DUSE_SANITIZER=Address,Memory
will result in configuration error. The delimiter character is not required
and -DUSE_SANITIZER=AddressLeak would work as well.
Sanitizer provides the commands:
sanitize_target(target) - add sanitizer flags to a target including copy sanitizer runtime.
copy_sanitizer_runtime(target) - copy sanitizer runtime to target location.
")

set(USE_SANITIZER_ASAN_FLAGS
# MSVC
"/fsanitize=address /Zi"
Expand All @@ -108,8 +32,9 @@ set(USE_SANITIZER_MSAN_FLAGS
# MSVC
"/fsanitize=memory"
# GNU/Clang
"-g -fsanitize=memory -fsanitize-memory-track-origins"
"-g -fsanitize=memory -fno-omit-frame-pointer -fsanitize-memory-track-origins"
# Optional: -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2
"-g -fsanitize=memory -fno-omit-frame-pointer"
"-g -fsanitize=memory")

set(USE_SANITIZER_USAN_FLAGS # GNU/Clang
Expand Down Expand Up @@ -146,6 +71,22 @@ message(
- Thread can not work with Address and Leak sanitizers.
- Memory can not work with Address, Leak, and Thread sanitizers.")

message(
VERBOSE
"Multiple values are allowed with USE_SANITIZER, e.g. -DUSE_SANITIZER=Address,Leak but some
sanitizers cannot be combined together, e.g.-DUSE_SANITIZER=Address,Memory
will result in configuration error. The delimiter character is not required
and -DUSE_SANITIZER=AddressLeak would work as well.
You can add more flags to USE_SANITIZER_EXTRA_FLAGS referring to the sanitizer
documentation <https://clang.llvm.org/docs/index.html>.
Sanitizer provides the commands:
sanitize_target(target) - add sanitizer flags to a target including copy sanitizer runtime.
copy_sanitizer_runtime(target) - copy sanitizer runtime to target location.
")

string(TOLOWER "${USE_SANITIZER}" USE_SANITIZER)

if(NOT USE_SANITIZER)
Expand Down

0 comments on commit 3bb437a

Please sign in to comment.