Skip to content

Commit 9f963f8

Browse files
committed
[DeviceSanitizer] Add options to allow user to customize red zone size
1 parent 717791b commit 9f963f8

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

source/loader/layers/sanitizer/asan_interceptor.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,35 @@ SanitizerInterceptor::SanitizerInterceptor() {
155155
cl_Debug = Value == "1" || Value == "true" ? 1 : 0;
156156
}
157157

158+
KV = Options->find("redzone");
159+
if (KV != Options->end()) {
160+
auto Value = KV->second.front();
161+
try {
162+
cl_MinRZSize = std::stoul(Value);
163+
if (cl_MinRZSize < 16) {
164+
cl_MinRZSize = 16;
165+
context.logger.warning("Trying to set redzone size to a value "
166+
"less than 16 is ignored");
167+
}
168+
} catch (...) {
169+
die("<SANITIZER>[ERROR]: \"redzone\" should be an integer");
170+
}
171+
}
172+
KV = Options->find("max_redzone");
173+
if (KV != Options->end()) {
174+
auto Value = KV->second.front();
175+
try {
176+
cl_MaxRZSize = std::stoul(Value);
177+
if (cl_MaxRZSize > 2048) {
178+
cl_MaxRZSize = 2048;
179+
context.logger.warning("Trying to set max redzone size to a "
180+
"value greater than 2048 is ignored");
181+
}
182+
} catch (...) {
183+
die("<SANITIZER>[ERROR]: \"max_redzone\" should be an integer");
184+
}
185+
}
186+
158187
KV = Options->find("quarantine_size_mb");
159188
if (KV != Options->end()) {
160189
auto Value = KV->second.front();
@@ -213,7 +242,7 @@ ur_result_t SanitizerInterceptor::allocateMemory(
213242
Alignment = MinAlignment;
214243
}
215244

216-
uptr RZLog = ComputeRZLog(Size);
245+
uptr RZLog = ComputeRZLog(Size, cl_MinRZSize, cl_MaxRZSize);
217246
uptr RZSize = RZLog2Size(RZLog);
218247
uptr RoundedSize = RoundUpTo(Size, Alignment);
219248
uptr NeededSize = RoundedSize + RZSize * 2;

source/loader/layers/sanitizer/asan_interceptor.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ class SanitizerInterceptor {
221221

222222
// We use "uint64_t" here because EnqueueWriteGlobal will fail when it's "uint32_t"
223223
uint64_t cl_Debug = 0;
224+
uint64_t cl_MinRZSize = 16;
225+
uint64_t cl_MaxRZSize = 2048;
224226
uint32_t cl_MaxQuarantineSizeMB = 0;
225227
bool cl_DetectLocals = true;
226228

source/loader/layers/sanitizer/common.hpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "ur_ddi.h"
1717

1818
#include <cassert>
19+
#include <cmath>
1920
#include <cstdint>
2021
#include <string>
2122

@@ -53,7 +54,17 @@ inline constexpr uptr RZLog2Size(uptr rz_log) {
5354
return 16 << rz_log;
5455
}
5556

56-
inline constexpr uptr ComputeRZLog(uptr user_requested_size) {
57+
inline constexpr uptr RZSize2Log(uptr rz_size) {
58+
assert(rz_size >= 16);
59+
assert(rz_size <= 2048);
60+
assert(IsPowerOfTwo(rz_size));
61+
uptr res = log2(rz_size) - 4;
62+
assert(rz_size == RZLog2Size(res));
63+
return res;
64+
}
65+
66+
inline constexpr uptr ComputeRZLog(uptr user_requested_size, uptr min_size,
67+
uptr max_size) {
5768
uptr rz_log = user_requested_size <= 64 - 16 ? 0
5869
: user_requested_size <= 128 - 32 ? 1
5970
: user_requested_size <= 512 - 64 ? 2
@@ -62,7 +73,9 @@ inline constexpr uptr ComputeRZLog(uptr user_requested_size) {
6273
: user_requested_size <= (1 << 15) - 512 ? 5
6374
: user_requested_size <= (1 << 16) - 1024 ? 6
6475
: 7;
65-
return rz_log;
76+
uptr min_log = RZSize2Log(min_size);
77+
uptr max_log = RZSize2Log(max_size);
78+
return std::min(std::max(rz_log, min_log), max_log);
6679
}
6780

6881
// ================================================================

source/loader/layers/sanitizer/ur_sanitizer_layer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ context_t context;
1818

1919
///////////////////////////////////////////////////////////////////////////////
2020
context_t::context_t()
21-
: interceptor(std::make_unique<SanitizerInterceptor>()),
22-
logger(logger::create_logger("sanitizer")) {}
21+
: logger(logger::create_logger("sanitizer")),
22+
interceptor(std::make_unique<SanitizerInterceptor>()) {}
2323

2424
bool context_t::isAvailable() const { return true; }
2525

source/loader/layers/sanitizer/ur_sanitizer_layer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ enum class SanitizerType {
3232
class __urdlllocal context_t : public proxy_layer_context_t {
3333
public:
3434
ur_dditable_t urDdiTable = {};
35-
std::unique_ptr<SanitizerInterceptor> interceptor;
3635
logger::Logger logger;
36+
std::unique_ptr<SanitizerInterceptor> interceptor;
3737
SanitizerType enabledType = SanitizerType::None;
3838

3939
context_t();

0 commit comments

Comments
 (0)