Skip to content

Commit ac55488

Browse files
[CompilerRT] Add numerical sanitizer
1 parent dcb71c0 commit ac55488

23 files changed

+2400
-1
lines changed

compiler-rt/cmake/Modules/AllSupportedArchDefs.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ else()
6161
endif()
6262
set(ALL_MSAN_SUPPORTED_ARCH ${X86_64} ${MIPS64} ${ARM64} ${PPC64} ${S390X}
6363
${LOONGARCH64})
64+
set(ALL_NSAN_SUPPORTED_ARCH ${X86} ${X86_64})
6465
set(ALL_HWASAN_SUPPORTED_ARCH ${X86_64} ${ARM64} ${RISCV64})
6566
set(ALL_MEMPROF_SUPPORTED_ARCH ${X86_64})
6667
set(ALL_PROFILE_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${PPC32} ${PPC64}

compiler-rt/cmake/config-ix.cmake

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,9 @@ if(APPLE)
609609
list_intersect(MSAN_SUPPORTED_ARCH
610610
ALL_MSAN_SUPPORTED_ARCH
611611
SANITIZER_COMMON_SUPPORTED_ARCH)
612+
list_intersect(NSAN_SUPPORTED_ARCH
613+
ALL_NSAN_SUPPORTED_ARCH
614+
SANITIZER_COMMON_SUPPORTED_ARCH)
612615
list_intersect(HWASAN_SUPPORTED_ARCH
613616
ALL_HWASAN_SUPPORTED_ARCH
614617
SANITIZER_COMMON_SUPPORTED_ARCH)
@@ -678,6 +681,7 @@ else()
678681
filter_available_targets(SHADOWCALLSTACK_SUPPORTED_ARCH
679682
${ALL_SHADOWCALLSTACK_SUPPORTED_ARCH})
680683
filter_available_targets(GWP_ASAN_SUPPORTED_ARCH ${ALL_GWP_ASAN_SUPPORTED_ARCH})
684+
filter_available_targets(NSAN_SUPPORTED_ARCH ${ALL_NSAN_SUPPORTED_ARCH})
681685
filter_available_targets(ORC_SUPPORTED_ARCH ${ALL_ORC_SUPPORTED_ARCH})
682686
endif()
683687

@@ -712,7 +716,7 @@ if(COMPILER_RT_SUPPORTED_ARCH)
712716
endif()
713717
message(STATUS "Compiler-RT supported architectures: ${COMPILER_RT_SUPPORTED_ARCH}")
714718

715-
set(ALL_SANITIZERS asan;dfsan;msan;hwasan;tsan;safestack;cfi;scudo_standalone;ubsan_minimal;gwp_asan;asan_abi)
719+
set(ALL_SANITIZERS asan;dfsan;msan;hwasan;tsan;safestack;cfi;scudo_standalone;ubsan_minimal;gwp_asan;nsan;asan_abi)
716720
set(COMPILER_RT_SANITIZERS_TO_BUILD all CACHE STRING
717721
"sanitizers to build if supported on the target (all;${ALL_SANITIZERS})")
718722
list_replace(COMPILER_RT_SANITIZERS_TO_BUILD all "${ALL_SANITIZERS}")
@@ -897,4 +901,11 @@ if (GWP_ASAN_SUPPORTED_ARCH AND
897901
else()
898902
set(COMPILER_RT_HAS_GWP_ASAN FALSE)
899903
endif()
904+
905+
if (COMPILER_RT_HAS_SANITIZER_COMMON AND NSAN_SUPPORTED_ARCH AND
906+
OS_NAME MATCHES "Linux")
907+
set(COMPILER_RT_HAS_NSAN TRUE)
908+
else()
909+
set(COMPILER_RT_HAS_NSAN FALSE)
910+
endif()
900911
pythonize_bool(COMPILER_RT_HAS_GWP_ASAN)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//===-- sanitizer/nsan_interface.h ------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Public interface for nsan.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
#ifndef SANITIZER_NSAN_INTERFACE_H
13+
#define SANITIZER_NSAN_INTERFACE_H
14+
15+
#include <sanitizer/common_interface_defs.h>
16+
17+
#ifdef __cplusplus
18+
extern "C" {
19+
#endif
20+
21+
/// User-provided default option settings.
22+
///
23+
/// You can provide your own implementation of this function to return a string
24+
/// containing NSan runtime options (for example,
25+
/// <c>verbosity=1:halt_on_error=0</c>).
26+
///
27+
/// \returns Default options string.
28+
const char *__nsan_default_options(void);
29+
30+
// Dumps nsan shadow data for a block of `size_bytes` bytes of application
31+
// memory at location `addr`.
32+
//
33+
// Each line contains application address, shadow types, then values.
34+
// Unknown types are shown as `__`, while known values are shown as
35+
// `f`, `d`, `l` for float, double, and long double respectively. Position is
36+
// shown as a single hex digit. The shadow value itself appears on the line that
37+
// contains the first byte of the value.
38+
// FIXME: Show both shadow and application value.
39+
//
40+
// Example: `__nsan_dump_shadow_mem(addr, 32, 8, 0)` might print:
41+
//
42+
// 0x0add7359: __ f0 f1 f2 f3 __ __ __ (42.000)
43+
// 0x0add7361: __ d1 d2 d3 d4 d5 d6 d7
44+
// 0x0add7369: d8 f0 f1 f2 f3 __ __ f2 (-1.000) (12.5)
45+
// 0x0add7371: f3 __ __ __ __ __ __ __
46+
//
47+
// This means that there is:
48+
// - a shadow double for the float at address 0x0add7360, with value 42;
49+
// - a shadow float128 for the double at address 0x0add7362, with value -1;
50+
// - a shadow double for the float at address 0x0add736a, with value 12.5;
51+
// There was also a shadow double for the float at address 0x0add736e, but bytes
52+
// f0 and f1 were overwritten by one or several stores, so that the shadow value
53+
// is no longer valid.
54+
// The argument `reserved` can be any value. Its true value is provided by the
55+
// instrumentation.
56+
void __nsan_dump_shadow_mem(const char *addr, size_t size_bytes,
57+
size_t bytes_per_line, size_t reserved);
58+
59+
// Explicitly dumps a value.
60+
// FIXME: vector versions ?
61+
void __nsan_dump_float(float value);
62+
void __nsan_dump_double(double value);
63+
void __nsan_dump_longdouble(long double value);
64+
65+
// Explicitly checks a value.
66+
// FIXME: vector versions ?
67+
void __nsan_check_float(float value);
68+
void __nsan_check_double(double value);
69+
void __nsan_check_longdouble(long double value);
70+
71+
#ifdef __cplusplus
72+
} // extern "C"
73+
#endif
74+
75+
#endif // SANITIZER_NSAN_INTERFACE_H

compiler-rt/lib/nsan/CMakeLists.txt

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
add_compiler_rt_component(nsan)
2+
3+
include_directories(..)
4+
5+
set(NSAN_SOURCES
6+
nsan.cc
7+
nsan_flags.cc
8+
nsan_interceptors.cc
9+
nsan_stats.cc
10+
nsan_suppressions.cc
11+
)
12+
13+
set(NSAN_HEADERS
14+
nsan.h
15+
nsan_flags.h
16+
nsan_flags.inc
17+
nsan_platform.h
18+
nsan_stats.h
19+
nsan_suppressions.h
20+
)
21+
22+
append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC NSAN_CFLAGS)
23+
24+
set(NSAN_DYNAMIC_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
25+
26+
set(NSAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
27+
#-fno-rtti -fno-exceptions
28+
# -nostdinc++ -pthread -fno-omit-frame-pointer)
29+
30+
# Remove -stdlib= which is unused when passing -nostdinc++.
31+
# string(REGEX REPLACE "-stdlib=[a-zA-Z+]*" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
32+
33+
if (COMPILER_RT_HAS_NSAN)
34+
foreach(arch ${NSAN_SUPPORTED_ARCH})
35+
add_compiler_rt_runtime(
36+
clang_rt.nsan
37+
STATIC
38+
ARCHS ${arch}
39+
SOURCES ${NSAN_SOURCES}
40+
$<TARGET_OBJECTS:RTInterception.${arch}>
41+
$<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
42+
$<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
43+
$<TARGET_OBJECTS:RTSanitizerCommonCoverage.${arch}>
44+
$<TARGET_OBJECTS:RTSanitizerCommonSymbolizer.${arch}>
45+
$<TARGET_OBJECTS:RTUbsan.${arch}>
46+
ADDITIONAL_HEADERS ${NSAN_HEADERS}
47+
CFLAGS ${NSAN_CFLAGS}
48+
PARENT_TARGET nsan
49+
)
50+
endforeach()
51+
52+
add_compiler_rt_object_libraries(RTNsan
53+
ARCHS ${NSAN_SUPPORTED_ARCH}
54+
SOURCES ${NSAN_SOURCES}
55+
ADDITIONAL_HEADERS ${NSAN_HEADERS}
56+
CFLAGS ${NSAN_CFLAGS})
57+
endif()
58+
59+
if(COMPILER_RT_INCLUDE_TESTS)
60+
add_subdirectory(tests)
61+
endif()

0 commit comments

Comments
 (0)