Skip to content

Commit aec8d07

Browse files
yingcong-wukbenzie
authored andcommitted
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 d7fa0f8 commit aec8d07

19 files changed

+385
-315
lines changed

source/loader/CMakeLists.txt

Lines changed: 2 additions & 4 deletions
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

source/loader/layers/sanitizer/asan/asan_ddi.cpp

Lines changed: 1 addition & 2 deletions
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()};

source/loader/layers/sanitizer/asan/asan_interceptor.cpp

Lines changed: 13 additions & 14 deletions
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);

source/loader/layers/sanitizer/asan/asan_interceptor.hpp

Lines changed: 1 addition & 5 deletions
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;

source/loader/layers/sanitizer/asan/asan_options.cpp

Lines changed: 0 additions & 148 deletions
This file was deleted.

source/loader/layers/sanitizer/asan/asan_report.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#include "asan_allocator.hpp"
1616
#include "asan_interceptor.hpp"
1717
#include "asan_libdevice.hpp"
18-
#include "asan_options.hpp"
1918
#include "asan_validator.hpp"
19+
#include "sanitizer_common/sanitizer_options.hpp"
2020
#include "sanitizer_common/sanitizer_utils.hpp"
2121
#include "ur_sanitizer_layer.hpp"
2222

@@ -135,7 +135,7 @@ void ReportUseAfterFree(const AsanErrorReport &Report,
135135
getContext()->logger.always(" #0 {} {}:{}", Func, File, Report.Line);
136136
getContext()->logger.always("");
137137

138-
if (getAsanInterceptor()->getOptions().MaxQuarantineSizeMB > 0) {
138+
if (getContext()->Options.MaxQuarantineSizeMB > 0) {
139139
auto AllocInfoItOp =
140140
getAsanInterceptor()->findAllocInfoByAddress(Report.Address);
141141

source/loader/layers/sanitizer/asan/asan_statistics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void AsanStats::UpdateUSMFreed(uptr FreedSize) {
6868
void AsanStats::UpdateUSMRealFreed(uptr FreedSize, uptr RedzoneSize) {
6969
UsmMalloced -= FreedSize;
7070
UsmMallocedRedzones -= RedzoneSize;
71-
if (getAsanInterceptor()->getOptions().MaxQuarantineSizeMB) {
71+
if (getContext()->Options.MaxQuarantineSizeMB) {
7272
UsmFreed -= FreedSize;
7373
}
7474
getContext()->logger.debug(
@@ -137,7 +137,7 @@ void AsanStatsWrapper::Print(ur_context_handle_t Context) {
137137
}
138138

139139
AsanStatsWrapper::AsanStatsWrapper() : Stat(nullptr) {
140-
if (getAsanInterceptor()->getOptions().PrintStats) {
140+
if (getContext()->Options.PrintStats) {
141141
Stat = new AsanStats;
142142
}
143143
}

source/loader/layers/sanitizer/msan/msan_interceptor.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#include "msan_interceptor.hpp"
1616
#include "msan_ddi.hpp"
17-
#include "msan_options.hpp"
1817
#include "msan_report.hpp"
1918
#include "msan_shadow.hpp"
2019
#include "sanitizer_common/sanitizer_stacktrace.hpp"
@@ -453,7 +452,7 @@ ur_result_t MsanInterceptor::prepareLaunch(
453452
LaunchInfo.Data->GlobalShadowOffset = DeviceInfo->Shadow->ShadowBegin;
454453
LaunchInfo.Data->GlobalShadowOffsetEnd = DeviceInfo->Shadow->ShadowEnd;
455454
LaunchInfo.Data->DeviceTy = DeviceInfo->Type;
456-
LaunchInfo.Data->Debug = getOptions().Debug ? 1 : 0;
455+
LaunchInfo.Data->Debug = getContext()->Options.Debug ? 1 : 0;
457456
UR_CALL(getContext()->urDdiTable.USM.pfnDeviceAlloc(
458457
ContextInfo->Handle, DeviceInfo->Handle, nullptr, nullptr,
459458
ContextInfo->MaxAllocatedSize, &LaunchInfo.Data->CleanShadow));

0 commit comments

Comments
 (0)