Skip to content

Commit 3b7f35b

Browse files
authored
Merge pull request #1534 from zhaomaosu/config-red-zone-size
[DeviceSanitizer] Add options to allow user to customize red zone size
2 parents 65c39c8 + 3ec0dde commit 3b7f35b

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
@@ -153,6 +153,35 @@ SanitizerInterceptor::SanitizerInterceptor() {
153153
cl_Debug = Value == "1" || Value == "true" ? 1 : 0;
154154
}
155155

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

214-
uptr RZLog = ComputeRZLog(Size);
243+
uptr RZLog = ComputeRZLog(Size, cl_MinRZSize, cl_MaxRZSize);
215244
uptr RZSize = RZLog2Size(RZLog);
216245
uptr RoundedSize = RoundUpTo(Size, Alignment);
217246
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
@@ -251,6 +251,8 @@ class SanitizerInterceptor {
251251

252252
// We use "uint64_t" here because EnqueueWriteGlobal will fail when it's "uint32_t"
253253
uint64_t cl_Debug = 0;
254+
uint64_t cl_MinRZSize = 16;
255+
uint64_t cl_MaxRZSize = 2048;
254256
uint32_t cl_MaxQuarantineSizeMB = 0;
255257
bool cl_DetectLocals = true;
256258

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
/// Returns the next integer (mod 2**64) that is greater than or equal to

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)