Skip to content

Commit a7f92c3

Browse files
zhaomaosuKornevNikita
authored andcommitted
[DevSanitizer] Fix several misc issues in device memory sanitizer (#18026)
* Disable inserting unreachable instruction since we have another mechanism to exit current kernel execution. * Cleanup clean shadow after it's allocated. * Remove redzone maximum limitation.
1 parent 9c30174 commit a7f92c3

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2187,7 +2187,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
21872187
Value *Cmp = convertToBool(ConvertedShadow, IRB, "_mscmp");
21882188
Instruction *CheckTerm = SplitBlockAndInsertIfThen(
21892189
Cmp, &*IRB.GetInsertPoint(),
2190-
/* Unreachable */ !MS.Recover, MS.ColdCallWeights);
2190+
/* Unreachable */ SpirOrSpirv ? false : !MS.Recover,
2191+
MS.ColdCallWeights);
21912192

21922193
IRB.SetInsertPoint(CheckTerm);
21932194
insertWarningFn(IRB, Origin);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// REQUIRES: linux, cpu || (gpu && level_zero)
22
// RUN: %{build} %device_asan_flags -DUNSAFE -O0 -g -o %t1.out
3-
// RUN: env UR_LAYER_ASAN_OPTIONS=redzone:64 %{run} not %t1.out 2>&1 | FileCheck %s
3+
// RUN: env UR_LAYER_ASAN_OPTIONS=redzone:4000 %{run} not %t1.out 2>&1 | FileCheck %s
44
// RUN: %{build} %device_asan_flags -DSAFE -O0 -g -o %t2.out
55

66
// clang-format off
@@ -21,10 +21,11 @@ int main() {
2121
h.single_task<class Test>([=]() { ++array[0]; });
2222
#endif
2323
}).wait();
24+
// CHECK: <SANITIZER>[WARNING]: Increasing the redzone size may cause excessive memory overhead
2425
// CHECK: ERROR: DeviceSanitizer: out-of-bounds-access on Device USM
2526
// CHECK: {{READ of size 1 at kernel <.*Test> LID\(0, 0, 0\) GID\(0, 0, 0\)}}
26-
// CHECK: {{ #0 .* .*options-redzone.cpp:}}[[@LINE-7]]
27-
// CHECK-MIN: The valid range of "redzone" is [16, 2048]. Setting to the minimum value 16.
27+
// CHECK: {{ #0 .* .*options-redzone.cpp:}}[[@LINE-8]]
28+
// CHECK-MIN: The valid range of "redzone" is [16, 18446744073709551615]. Setting to the minimum value 16.
2829

2930
sycl::free(array, q);
3031
return 0;

unified-runtime/source/loader/layers/sanitizer/msan/msan_interceptor.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ ur_result_t MsanInterceptor::prepareLaunch(
479479
UR_CALL(getContext()->urDdiTable.USM.pfnDeviceAlloc(
480480
ContextInfo->Handle, DeviceInfo->Handle, nullptr, nullptr,
481481
ContextInfo->MaxAllocatedSize, (void **)&LaunchInfo.Data->CleanShadow));
482+
UR_CALL(EnqueueUSMBlockingSet(Queue, (void *)LaunchInfo.Data->CleanShadow, 0,
483+
ContextInfo->MaxAllocatedSize, 0, nullptr,
484+
nullptr));
482485

483486
if (LaunchInfo.LocalWorkSize.empty()) {
484487
LaunchInfo.LocalWorkSize.resize(LaunchInfo.WorkDim);

unified-runtime/source/loader/layers/sanitizer/sanitizer_common/sanitizer_common.hpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ inline constexpr bool IsPowerOfTwo(uptr x) {
3434
return (x & (x - 1)) == 0 && x != 0;
3535
}
3636

37+
inline constexpr uptr RoundUpToPowerOfTwo(uptr x) {
38+
if (x == 0)
39+
return 1;
40+
x--;
41+
x |= x >> 1;
42+
x |= x >> 2;
43+
x |= x >> 4;
44+
x |= x >> 8;
45+
x |= x >> 16;
46+
x |= x >> 32;
47+
x++;
48+
assert(IsPowerOfTwo(x));
49+
return x;
50+
}
51+
3752
inline constexpr uptr RoundUpTo(uptr Size, uptr boundary) {
3853
assert(IsPowerOfTwo(boundary));
3954
return (Size + boundary - 1) & ~(boundary - 1);
@@ -48,16 +63,12 @@ inline constexpr bool IsAligned(uptr a, uptr alignment) {
4863
return (a & (alignment - 1)) == 0;
4964
}
5065

51-
// Valid redzone sizes are 16, 32, 64, ... 2048, so we encode them in 3 bits.
66+
// Valid redzone sizes are 16, 32, 64 ..., so we encode them in 3 bits.
5267
// We use adaptive redzones: for larger allocation larger redzones are used.
53-
inline constexpr uptr RZLog2Size(uptr rz_log) {
54-
assert(rz_log < 8);
55-
return 16 << rz_log;
56-
}
68+
inline constexpr uptr RZLog2Size(uptr rz_log) { return 16 << rz_log; }
5769

5870
inline constexpr uptr RZSize2Log(uptr rz_size) {
5971
assert(rz_size >= 16);
60-
assert(rz_size <= 2048);
6172
assert(IsPowerOfTwo(rz_size));
6273
uptr res = log2(rz_size) - 4;
6374
assert(rz_size == RZLog2Size(res));

unified-runtime/source/loader/layers/sanitizer/sanitizer_common/sanitizer_options.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
#include "sanitizer_options.hpp"
15+
#include "sanitizer_common.hpp"
1516
#include "sanitizer_options_impl.hpp"
1617

1718
#include <cstring>
@@ -47,7 +48,13 @@ void SanitizerOptions::Init(const std::string &EnvName,
4748
Parser.ParseBool("halt_on_error", HaltOnError);
4849

4950
Parser.ParseUint64("quarantine_size_mb", MaxQuarantineSizeMB, 0, UINT32_MAX);
50-
Parser.ParseUint64("redzone", MinRZSize, 16, 2048);
51+
Parser.ParseUint64("redzone", MinRZSize, 16);
52+
MinRZSize =
53+
IsPowerOfTwo(MinRZSize) ? MinRZSize : RoundUpToPowerOfTwo(MinRZSize);
54+
if (MinRZSize > 16) {
55+
Logger.warning(
56+
"Increasing the redzone size may cause excessive memory overhead");
57+
}
5158
}
5259

5360
} // namespace ur_sanitizer_layer

0 commit comments

Comments
 (0)