Skip to content

Commit e2ab2b9

Browse files
authored
[DevSan][Refactor] Make Options an unified class shared by all sanitizers (#17157)
- Make `Options` shared by different sanitizers. - Also, remove the `max_redzone` option as we don't think it serves much purpose. - Move the testing in SYCL e2e test options-invalid-values.cpp to UR unit test. Reason: 1. reduce duplicated codes 2. if there are any options with same name in different sanitizers, then they should have the same type and same meaning, which is more friendly to users. So it should be okay to have one unified options class.
1 parent cd38f3f commit e2ab2b9

21 files changed

+386
-357
lines changed

sycl/test-e2e/AddressSanitizer/common/options-invalid-values.cpp

-39
This file was deleted.

sycl/test-e2e/AddressSanitizer/common/options-redzone.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
// clang-format off
77
// RUN: env UR_LOG_SANITIZER=level:debug UR_LAYER_ASAN_OPTIONS=redzone:8 %{run} %t2.out 2>&1 | FileCheck --check-prefixes CHECK-MIN %s
8-
// RUN: env UR_LOG_SANITIZER=level:debug UR_LAYER_ASAN_OPTIONS=max_redzone:4096 %{run} %t2.out 2>&1 | FileCheck --check-prefixes CHECK-MAX %s
98
// clang-format on
109

1110
#include <sycl/usm.hpp>
@@ -25,8 +24,7 @@ int main() {
2524
// CHECK: ERROR: DeviceSanitizer: out-of-bounds-access on Device USM
2625
// CHECK: {{READ of size 1 at kernel <.*Test> LID\(0, 0, 0\) GID\(0, 0, 0\)}}
2726
// CHECK: {{ #0 .* .*options-redzone.cpp:}}[[@LINE-7]]
28-
// CHECK-MIN: Trying to set redzone size to a value less than 16 is ignored
29-
// CHECK-MAX: Trying to set max redzone size to a value greater than 2048 is ignored
27+
// CHECK-MIN: The valid range of "redzone" is [16, 2048]. Setting to the minimum value 16.
3028

3129
sycl::free(array, q);
3230
return 0;

unified-runtime/source/loader/CMakeLists.txt

+2-4
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ if(UR_ENABLE_SANITIZER)
140140
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_interceptor.cpp
141141
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_interceptor.hpp
142142
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_libdevice.hpp
143-
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_options.cpp
144-
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_options.hpp
145143
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_quarantine.cpp
146144
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_quarantine.hpp
147145
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/asan/asan_report.cpp
@@ -161,8 +159,6 @@ if(UR_ENABLE_SANITIZER)
161159
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/msan/msan_interceptor.cpp
162160
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/msan/msan_interceptor.hpp
163161
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/msan/msan_libdevice.hpp
164-
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/msan/msan_options.cpp
165-
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/msan/msan_options.hpp
166162
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/msan/msan_report.cpp
167163
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/msan/msan_report.hpp
168164
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/msan/msan_shadow.cpp
@@ -176,6 +172,8 @@ if(UR_ENABLE_SANITIZER)
176172
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/sanitizer_common/sanitizer_stacktrace.hpp
177173
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/sanitizer_common/sanitizer_utils.cpp
178174
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/sanitizer_common/sanitizer_utils.hpp
175+
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/sanitizer_common/sanitizer_options.cpp
176+
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/sanitizer_common/sanitizer_options.hpp
179177
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/ur_sanddi.cpp
180178
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/ur_sanitizer_layer.cpp
181179
${CMAKE_CURRENT_SOURCE_DIR}/layers/sanitizer/ur_sanitizer_layer.hpp

unified-runtime/source/loader/layers/sanitizer/asan/asan_ddi.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
#include "asan_ddi.hpp"
1515
#include "asan_interceptor.hpp"
16-
#include "asan_options.hpp"
1716
#include "sanitizer_common/sanitizer_stacktrace.hpp"
1817
#include "sanitizer_common/sanitizer_utils.hpp"
1918
#include "ur_sanitizer_layer.hpp"
@@ -1559,7 +1558,7 @@ __urdlllocal ur_result_t UR_APICALL urKernelSetArgPointer(
15591558
pArgValue);
15601559

15611560
std::shared_ptr<KernelInfo> KI;
1562-
if (getAsanInterceptor()->getOptions().DetectKernelArguments) {
1561+
if (getContext()->Options.DetectKernelArguments) {
15631562
auto &KI = getAsanInterceptor()->getOrCreateKernelInfo(hKernel);
15641563
std::scoped_lock<ur_shared_mutex> Guard(KI.Mutex);
15651564
KI.PointerArgs[argIndex] = {pArgValue, GetCurrentBacktrace()};

unified-runtime/source/loader/layers/sanitizer/asan/asan_interceptor.cpp

+13-14
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@
1414

1515
#include "asan_interceptor.hpp"
1616
#include "asan_ddi.hpp"
17-
#include "asan_options.hpp"
1817
#include "asan_quarantine.hpp"
1918
#include "asan_report.hpp"
2019
#include "asan_shadow.hpp"
2120
#include "asan_validator.hpp"
21+
#include "sanitizer_common/sanitizer_options.hpp"
2222
#include "sanitizer_common/sanitizer_stacktrace.hpp"
2323
#include "sanitizer_common/sanitizer_utils.hpp"
2424

2525
namespace ur_sanitizer_layer {
2626
namespace asan {
2727

2828
AsanInterceptor::AsanInterceptor() {
29-
if (getOptions().MaxQuarantineSizeMB) {
29+
if (getContext()->Options.MaxQuarantineSizeMB) {
3030
m_Quarantine = std::make_unique<Quarantine>(
31-
static_cast<uint64_t>(getOptions().MaxQuarantineSizeMB) * 1024 * 1024);
31+
getContext()->Options.MaxQuarantineSizeMB * 1024 * 1024);
3232
}
3333
}
3434

@@ -90,8 +90,7 @@ ur_result_t AsanInterceptor::allocateMemory(ur_context_handle_t Context,
9090
Alignment = MinAlignment;
9191
}
9292

93-
uptr RZLog =
94-
ComputeRZLog(Size, getOptions().MinRZSize, getOptions().MaxRZSize);
93+
uptr RZLog = ComputeRZLog(Size, getContext()->Options.MinRZSize);
9594
uptr RZSize = RZLog2Size(RZLog);
9695
uptr RoundedSize = RoundUpTo(Size, Alignment);
9796
uptr NeededSize = RoundedSize + RZSize * 2;
@@ -175,7 +174,7 @@ ur_result_t AsanInterceptor::releaseMemory(ur_context_handle_t Context,
175174
if (!AllocInfoItOp) {
176175
// "Addr" might be a host pointer
177176
ReportBadFree(Addr, GetCurrentBacktrace(), nullptr);
178-
if (getOptions().HaltOnError) {
177+
if (getContext()->Options.HaltOnError) {
179178
exitWithErrors();
180179
}
181180
return UR_RESULT_SUCCESS;
@@ -193,23 +192,23 @@ ur_result_t AsanInterceptor::releaseMemory(ur_context_handle_t Context,
193192
// "Addr" might be a host pointer
194193
ReportBadFree(Addr, GetCurrentBacktrace(), nullptr);
195194
}
196-
if (getOptions().HaltOnError) {
195+
if (getContext()->Options.HaltOnError) {
197196
exitWithErrors();
198197
}
199198
return UR_RESULT_SUCCESS;
200199
}
201200

202201
if (Addr != AllocInfo->UserBegin) {
203202
ReportBadFree(Addr, GetCurrentBacktrace(), AllocInfo);
204-
if (getOptions().HaltOnError) {
203+
if (getContext()->Options.HaltOnError) {
205204
exitWithErrors();
206205
}
207206
return UR_RESULT_SUCCESS;
208207
}
209208

210209
if (AllocInfo->IsReleased) {
211210
ReportDoubleFree(Addr, GetCurrentBacktrace(), AllocInfo);
212-
if (getOptions().HaltOnError) {
211+
if (getContext()->Options.HaltOnError) {
213212
exitWithErrors();
214213
}
215214
return UR_RESULT_SUCCESS;
@@ -736,7 +735,7 @@ ur_result_t AsanInterceptor::prepareLaunch(
736735
LocalMemoryUsage, PrivateMemoryUsage);
737736

738737
// Validate pointer arguments
739-
if (getOptions().DetectKernelArguments) {
738+
if (getContext()->Options.DetectKernelArguments) {
740739
for (const auto &[ArgIndex, PtrPair] : KernelInfo.PointerArgs) {
741740
auto Ptr = PtrPair.first;
742741
if (Ptr == nullptr) {
@@ -813,10 +812,10 @@ ur_result_t AsanInterceptor::prepareLaunch(
813812
LaunchInfo.Data.Host.GlobalShadowOffset = DeviceInfo->Shadow->ShadowBegin;
814813
LaunchInfo.Data.Host.GlobalShadowOffsetEnd = DeviceInfo->Shadow->ShadowEnd;
815814
LaunchInfo.Data.Host.DeviceTy = DeviceInfo->Type;
816-
LaunchInfo.Data.Host.Debug = getOptions().Debug ? 1 : 0;
815+
LaunchInfo.Data.Host.Debug = getContext()->Options.Debug ? 1 : 0;
817816

818817
// Write shadow memory offset for local memory
819-
if (getOptions().DetectLocals) {
818+
if (getContext()->Options.DetectLocals) {
820819
if (DeviceInfo->Shadow->AllocLocalShadow(
821820
Queue, NumWG, LaunchInfo.Data.Host.LocalShadowOffset,
822821
LaunchInfo.Data.Host.LocalShadowOffsetEnd) != UR_RESULT_SUCCESS) {
@@ -836,7 +835,7 @@ ur_result_t AsanInterceptor::prepareLaunch(
836835
}
837836

838837
// Write shadow memory offset for private memory
839-
if (getOptions().DetectPrivates) {
838+
if (getContext()->Options.DetectPrivates) {
840839
if (DeviceInfo->Shadow->AllocPrivateShadow(
841840
Queue, NumWG, LaunchInfo.Data.Host.PrivateShadowOffset,
842841
LaunchInfo.Data.Host.PrivateShadowOffsetEnd) != UR_RESULT_SUCCESS) {
@@ -928,7 +927,7 @@ ContextInfo::~ContextInfo() {
928927
assert(URes == UR_RESULT_SUCCESS);
929928

930929
// check memory leaks
931-
if (getAsanInterceptor()->getOptions().DetectLeaks &&
930+
if (getContext()->Options.DetectLeaks &&
932931
getAsanInterceptor()->isNormalExit()) {
933932
std::vector<AllocationIterator> AllocInfos =
934933
getAsanInterceptor()->findAllocInfoByContext(Handle);

unified-runtime/source/loader/layers/sanitizer/asan/asan_interceptor.hpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
#include "asan_allocator.hpp"
1717
#include "asan_buffer.hpp"
1818
#include "asan_libdevice.hpp"
19-
#include "asan_options.hpp"
2019
#include "asan_shadow.hpp"
2120
#include "asan_statistics.hpp"
2221
#include "sanitizer_common/sanitizer_common.hpp"
22+
#include "sanitizer_common/sanitizer_options.hpp"
2323
#include "ur_sanitizer_layer.hpp"
2424

2525
#include <memory>
@@ -342,8 +342,6 @@ class AsanInterceptor {
342342
KernelInfo &getOrCreateKernelInfo(ur_kernel_handle_t Kernel);
343343
ur_result_t eraseKernelInfo(ur_kernel_handle_t Kernel);
344344

345-
const AsanOptions &getOptions() { return m_Options; }
346-
347345
void exitWithErrors() {
348346
m_NormalExit = false;
349347
exit(1);
@@ -375,8 +373,6 @@ class AsanInterceptor {
375373
ur_result_t registerSpirKernels(ur_program_handle_t Program);
376374

377375
private:
378-
// m_Options may be used in other places, place it at the top
379-
AsanOptions m_Options;
380376
std::unordered_map<ur_context_handle_t, std::shared_ptr<ContextInfo>>
381377
m_ContextMap;
382378
ur_shared_mutex m_ContextMapMutex;

unified-runtime/source/loader/layers/sanitizer/asan/asan_options.cpp

-148
This file was deleted.

0 commit comments

Comments
 (0)