Skip to content

Commit

Permalink
[UR][Layer] Add Sanitizer Layer (oneapi-src#1074)
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZyne authored Jan 17, 2024
1 parent c63ad9b commit b51dec0
Show file tree
Hide file tree
Showing 16 changed files with 1,564 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,7 @@ out/

# External content
*/**/external

# VS clangd
/.cache
/compile_commands.json
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ option(UR_USE_UBSAN "enable UndefinedBehaviorSanitizer" OFF)
option(UR_USE_MSAN "enable MemorySanitizer" OFF)
option(UR_USE_TSAN "enable ThreadSanitizer" OFF)
option(UR_ENABLE_TRACING "enable api tracing through xpti" OFF)
option(UR_ENABLE_SANITIZER "enable device sanitizer" ON)
option(UMF_BUILD_SHARED_LIBRARY "Build UMF as shared library" OFF)
option(UMF_ENABLE_POOL_TRACKING "Build UMF with pool tracking" ON)
option(UR_BUILD_ADAPTER_L0 "Build the Level-Zero adapter" OFF)
Expand Down Expand Up @@ -121,6 +122,10 @@ if(UR_ENABLE_TRACING)
endif()
endif()

if(UR_ENABLE_SANITIZER)
add_compile_definitions(UR_ENABLE_SANITIZER)
endif()

if(UR_USE_ASAN)
add_sanitizer_flag(address)
endif()
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ List of options provided by CMake:
| UR_USE_UBSAN | Enable UndefinedBehavior Sanitizer | ON/OFF | OFF |
| UR_USE_MSAN | Enable MemorySanitizer (clang only) | ON/OFF | OFF |
| UR_ENABLE_TRACING | Enable XPTI-based tracing layer | ON/OFF | OFF |
| UR_ENABLE_SANITIZER | Enable device sanitizer layer | ON/OFF | ON |
| UR_CONFORMANCE_TARGET_TRIPLES | SYCL triples to build CTS device binaries for | Comma-separated list | spir64 |
| UR_BUILD_ADAPTER_L0 | Build the Level-Zero adapter | ON/OFF | OFF |
| UR_BUILD_ADAPTER_OPENCL | Build the OpenCL adapter | ON/OFF | OFF |
Expand Down
15 changes: 15 additions & 0 deletions scripts/core/INTRO.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ Unified Runtime loader implements tracing support through the `XPTI framework <h
| **user_data**: A pointer to `function_with_args_t` object, that includes function ID, name, arguments, and return value.
- None

Sanitizers
---------------------

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).

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`.

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.

Logging
---------------------

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

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

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

.. envvar:: UR_LOG_SANITIZER

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

.. envvar:: UR_LOG_VALIDATION

Holds parameters for setting Unified Runtime validation logging. The syntax is described in the Logging_ section.
Expand Down
5 changes: 5 additions & 0 deletions source/common/logger/ur_logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ inline void error(const char *format, Args &&...args) {
get_logger().log(logger::Level::ERR, format, std::forward<Args>(args)...);
}

template <typename... Args>
inline void always(const char *format, Args &&...args) {
get_logger().always(format, std::forward<Args>(args)...);
}

inline void setLevel(logger::Level level) { get_logger().setLevel(level); }

inline void setFlushLevel(logger::Level level) {
Expand Down
8 changes: 8 additions & 0 deletions source/common/logger/ur_logger_details.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ class Logger {
log(logger::Level::ERR, format, std::forward<Args>(args)...);
}

template <typename... Args>
void always(const char *format, Args &&...args) {
if (sink) {
sink->log(logger::Level::QUIET, format,
std::forward<Args>(args)...);
}
}

template <typename... Args>
void log(logger::Level level, const char *format, Args &&...args) {
if (level < this->level) {
Expand Down
2 changes: 1 addition & 1 deletion source/common/logger/ur_sinks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Sink {
template <typename... Args>
void log(logger::Level level, const char *fmt, Args &&...args) {
std::ostringstream buffer;
if (!skip_prefix) {
if (!skip_prefix && level != logger::Level::QUIET) {
buffer << "<" << logger_name << ">"
<< "[" << level_to_str(level) << "]: ";
}
Expand Down
17 changes: 17 additions & 0 deletions source/loader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,23 @@ if(UR_ENABLE_TRACING)
)
endif()

if(UR_ENABLE_SANITIZER)
target_sources(ur_loader
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/../ur/ur.cpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_interceptor.cpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan_interceptor.hpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/common.hpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/device_sanitizer_report.hpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/ur_sanitizer_layer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/ur_sanitizer_layer.hpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/ur_sanddi.cpp
)
target_include_directories(ur_loader PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/../"
)
endif()


# link validation backtrace dependencies
if(UNIX)
Expand Down
Loading

0 comments on commit b51dec0

Please sign in to comment.