Skip to content

Commit b51dec0

Browse files
authored
[UR][Layer] Add Sanitizer Layer (#1074)
1 parent c63ad9b commit b51dec0

16 files changed

+1564
-2
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,7 @@ out/
8686

8787
# External content
8888
*/**/external
89+
90+
# VS clangd
91+
/.cache
92+
/compile_commands.json

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ option(UR_USE_UBSAN "enable UndefinedBehaviorSanitizer" OFF)
3434
option(UR_USE_MSAN "enable MemorySanitizer" OFF)
3535
option(UR_USE_TSAN "enable ThreadSanitizer" OFF)
3636
option(UR_ENABLE_TRACING "enable api tracing through xpti" OFF)
37+
option(UR_ENABLE_SANITIZER "enable device sanitizer" ON)
3738
option(UMF_BUILD_SHARED_LIBRARY "Build UMF as shared library" OFF)
3839
option(UMF_ENABLE_POOL_TRACKING "Build UMF with pool tracking" ON)
3940
option(UR_BUILD_ADAPTER_L0 "Build the Level-Zero adapter" OFF)
@@ -121,6 +122,10 @@ if(UR_ENABLE_TRACING)
121122
endif()
122123
endif()
123124

125+
if(UR_ENABLE_SANITIZER)
126+
add_compile_definitions(UR_ENABLE_SANITIZER)
127+
endif()
128+
124129
if(UR_USE_ASAN)
125130
add_sanitizer_flag(address)
126131
endif()

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ List of options provided by CMake:
126126
| UR_USE_UBSAN | Enable UndefinedBehavior Sanitizer | ON/OFF | OFF |
127127
| UR_USE_MSAN | Enable MemorySanitizer (clang only) | ON/OFF | OFF |
128128
| UR_ENABLE_TRACING | Enable XPTI-based tracing layer | ON/OFF | OFF |
129+
| UR_ENABLE_SANITIZER | Enable device sanitizer layer | ON/OFF | ON |
129130
| UR_CONFORMANCE_TARGET_TRIPLES | SYCL triples to build CTS device binaries for | Comma-separated list | spir64 |
130131
| UR_BUILD_ADAPTER_L0 | Build the Level-Zero adapter | ON/OFF | OFF |
131132
| UR_BUILD_ADAPTER_OPENCL | Build the OpenCL adapter | ON/OFF | OFF |

scripts/core/INTRO.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,15 @@ Unified Runtime loader implements tracing support through the `XPTI framework <h
179179
| **user_data**: A pointer to `function_with_args_t` object, that includes function ID, name, arguments, and return value.
180180
- None
181181

182+
Sanitizers
183+
---------------------
184+
185+
Unified Runtime loader implements the runtime part of device-side sanitizers: AddressSanitizer (`UR_LAYER_ASAN`), MemorySanitizer (`UR_LAYER_MSAN`, planned), and ThreadSanitizer (`UR_LAYER_TSAN`, planned).
186+
187+
This layer shouldn't be enabled explicitly, for example, by the environment variable `UR_ENABLE_LAYERS`, but is enabled by program's runtime (e.g. SYCL/OpenMP Runtime) when the device code is compiled with flag `-fsanitize=address|memory|thread`.
188+
189+
Currently, AddressSanitizer only supports some of the devices on OpenCL and Level-Zero adapters, and this could be extended to support other devices and adapters if UR virtual memory APIs and shadow memory mapping in libdevice are supported.
190+
182191
Logging
183192
---------------------
184193

@@ -260,6 +269,8 @@ Layers currently included with the runtime are as follows:
260269
- Enables UR_LAYER_PARAMETER_VALIDATION and UR_LAYER_LEAK_CHECKING.
261270
* - UR_LAYER_TRACING
262271
- Enables the XPTI tracing layer, see Tracing_ for more detail.
272+
* - UR_LAYER_ASAN \| UR_LAYER_MSAN \| UR_LAYER_TSAN
273+
- Enables the device-side sanitizer layer, see Sanitizers_ for more detail.
263274

264275
Environment Variables
265276
---------------------
@@ -274,6 +285,10 @@ Specific environment variables can be set to control the behavior of unified run
274285

275286
Holds parameters for setting Unified Runtime null adapter logging. The syntax is described in the Logging_ section.
276287

288+
.. envvar:: UR_LOG_SANITIZER
289+
290+
Holds parameters for setting Unified Runtime sanitizer logging. The syntax is described in the Logging_ section.
291+
277292
.. envvar:: UR_LOG_VALIDATION
278293

279294
Holds parameters for setting Unified Runtime validation logging. The syntax is described in the Logging_ section.

source/common/logger/ur_logger.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ inline void error(const char *format, Args &&...args) {
4343
get_logger().log(logger::Level::ERR, format, std::forward<Args>(args)...);
4444
}
4545

46+
template <typename... Args>
47+
inline void always(const char *format, Args &&...args) {
48+
get_logger().always(format, std::forward<Args>(args)...);
49+
}
50+
4651
inline void setLevel(logger::Level level) { get_logger().setLevel(level); }
4752

4853
inline void setFlushLevel(logger::Level level) {

source/common/logger/ur_logger_details.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ class Logger {
5151
log(logger::Level::ERR, format, std::forward<Args>(args)...);
5252
}
5353

54+
template <typename... Args>
55+
void always(const char *format, Args &&...args) {
56+
if (sink) {
57+
sink->log(logger::Level::QUIET, format,
58+
std::forward<Args>(args)...);
59+
}
60+
}
61+
5462
template <typename... Args>
5563
void log(logger::Level level, const char *format, Args &&...args) {
5664
if (level < this->level) {

source/common/logger/ur_sinks.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Sink {
2222
template <typename... Args>
2323
void log(logger::Level level, const char *fmt, Args &&...args) {
2424
std::ostringstream buffer;
25-
if (!skip_prefix) {
25+
if (!skip_prefix && level != logger::Level::QUIET) {
2626
buffer << "<" << logger_name << ">"
2727
<< "[" << level_to_str(level) << "]: ";
2828
}

source/loader/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,23 @@ if(UR_ENABLE_TRACING)
101101
)
102102
endif()
103103

104+
if(UR_ENABLE_SANITIZER)
105+
target_sources(ur_loader
106+
PRIVATE
107+
${CMAKE_CURRENT_SOURCE_DIR}/../ur/ur.cpp
108+
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_interceptor.cpp
109+
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_interceptor.hpp
110+
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/common.hpp
111+
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/device_sanitizer_report.hpp
112+
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/ur_sanitizer_layer.cpp
113+
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/ur_sanitizer_layer.hpp
114+
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/ur_sanddi.cpp
115+
)
116+
target_include_directories(ur_loader PRIVATE
117+
"${CMAKE_CURRENT_SOURCE_DIR}/../"
118+
)
119+
endif()
120+
104121

105122
# link validation backtrace dependencies
106123
if(UNIX)

0 commit comments

Comments
 (0)