Skip to content

Commit 63b96a1

Browse files
committed
update impl
1 parent 9c877d9 commit 63b96a1

File tree

2 files changed

+65
-23
lines changed

2 files changed

+65
-23
lines changed

source/loader/layers/sanitizer/asan_interceptor.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ ur_result_t enqueueMemSetShadow(ur_context_handle_t Context,
145145
SanitizerInterceptor::SanitizerInterceptor() {
146146
if (Options().MaxQuarantineSizeMB) {
147147
m_Quarantine = std::make_unique<Quarantine>(
148-
Options().MaxQuarantineSizeMB * 1024 * 1024);
148+
static_cast<uint64_t>(Options().MaxQuarantineSizeMB) * 1024 * 1024);
149149
}
150150
}
151151

@@ -646,8 +646,9 @@ ur_result_t SanitizerInterceptor::prepareLaunch(
646646
};
647647

648648
// Write debug
649-
EnqueueWriteGlobal(kSPIR_AsanDebug, &(Options().Debug),
650-
sizeof(Options().Debug));
649+
// We use "uint64_t" here because EnqueueWriteGlobal will fail when it's "uint32_t"
650+
uint64_t Debug = Options().Debug ? 1 : 0;
651+
EnqueueWriteGlobal(kSPIR_AsanDebug, &Debug, sizeof(Debug));
651652

652653
// Write shadow memory offset for global memory
653654
EnqueueWriteGlobal(kSPIR_AsanShadowMemoryGlobalStart,
@@ -747,7 +748,8 @@ SanitizerInterceptor::findAllocInfoByAddress(uptr Address) {
747748
--It;
748749
// Make sure we got the right AllocInfo
749750
assert(Address >= It->second->AllocBegin &&
750-
Address < It->second->AllocBegin + It->second->AllocSize);
751+
Address < It->second->AllocBegin + It->second->AllocSize &&
752+
"Wrong AllocInfo for the address");
751753
return It;
752754
}
753755

source/loader/layers/sanitizer/asan_options.hpp

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
#include "ur/ur.hpp"
1717
#include "ur_sanitizer_layer.hpp"
1818

19-
#include <cstdint>
19+
#include <algorithm>
20+
#include <cstring>
21+
#include <stdexcept>
2022

2123
namespace ur_sanitizer_layer {
2224

@@ -30,9 +32,7 @@ struct AsanOptions {
3032
return instance;
3133
}
3234

33-
// We use "uint64_t" here because EnqueueWriteGlobal will fail when it's "uint32_t"
34-
uint64_t Debug = 0;
35-
35+
bool Debug = false;
3636
uint64_t MinRZSize = 16;
3737
uint64_t MaxRZSize = 2048;
3838
uint32_t MaxQuarantineSizeMB = 0;
@@ -45,29 +45,69 @@ struct AsanOptions {
4545
return;
4646
}
4747

48-
auto KV = OptionsEnvMap->find("debug");
49-
if (KV != OptionsEnvMap->end()) {
50-
auto Value = KV->second.front();
51-
Debug = Value == "1" || Value == "true" ? 1 : 0;
52-
}
48+
const char *TrueStrings[] = {"1", "true"};
49+
const char *FalseStrings[] = {"0", "false"};
50+
51+
auto InplaceToLower = [](std::string &S) {
52+
std::transform(S.begin(), S.end(), S.begin(),
53+
[](unsigned char C) { return std::tolower(C); });
54+
};
55+
auto IsTrue = [&](const std::string &S) {
56+
return std::any_of(std::begin(TrueStrings), std::end(TrueStrings),
57+
[&](const char *CS) { return S == CS; });
58+
};
59+
auto IsFalse = [&](const std::string &S) {
60+
return std::any_of(std::begin(FalseStrings), std::end(FalseStrings),
61+
[&](const char *CS) { return S == CS; });
62+
};
63+
64+
auto SetBoolOption = [&](const std::string &Name, bool &Opt) {
65+
auto KV = OptionsEnvMap->find(Name);
66+
if (KV != OptionsEnvMap->end()) {
67+
auto Value = KV->second.front();
68+
InplaceToLower(Value);
69+
if (IsTrue(Value)) {
70+
Opt = true;
71+
} else if (IsFalse(Value)) {
72+
Opt = false;
73+
} else {
74+
std::stringstream SS;
75+
SS << "<SANITIZER>[ERROR]: \"" << Name << "\" is set to \""
76+
<< Value << "\", which is not an valid setting. ";
77+
SS << "Acceptable input are: for enable, use:";
78+
for (auto &S : TrueStrings) {
79+
SS << " \"" << S << "\"";
80+
}
81+
SS << "; ";
82+
SS << "for disable, use:";
83+
for (auto &S : FalseStrings) {
84+
SS << " \"" << S << "\"";
85+
}
86+
SS << ".";
87+
die(SS.str().c_str());
88+
}
89+
}
90+
};
5391

54-
KV = OptionsEnvMap->find("quarantine_size_mb");
92+
SetBoolOption("debug", Debug);
93+
SetBoolOption("detect_locals", DetectLocals);
94+
95+
auto KV = OptionsEnvMap->find("quarantine_size_mb");
5596
if (KV != OptionsEnvMap->end()) {
5697
auto Value = KV->second.front();
5798
try {
58-
MaxQuarantineSizeMB = std::stoul(Value);
99+
auto temp_long = std::stoul(Value);
100+
if (temp_long > UINT32_MAX) {
101+
throw std::out_of_range("");
102+
}
103+
MaxQuarantineSizeMB = temp_long;
59104
} catch (...) {
60105
die("<SANITIZER>[ERROR]: \"quarantine_size_mb\" should be "
61-
"an integer");
106+
"an positive integer that smaller than or equal to "
107+
"4294967295.");
62108
}
63109
}
64110

65-
KV = OptionsEnvMap->find("detect_locals");
66-
if (KV != OptionsEnvMap->end()) {
67-
auto Value = KV->second.front();
68-
DetectLocals = Value == "1" || Value == "true" ? true : false;
69-
}
70-
71111
KV = OptionsEnvMap->find("redzone");
72112
if (KV != OptionsEnvMap->end()) {
73113
auto Value = KV->second.front();
@@ -101,6 +141,6 @@ struct AsanOptions {
101141
}
102142
};
103143

104-
inline AsanOptions &Options() { return AsanOptions::getInstance(); }
144+
inline const AsanOptions &Options() { return AsanOptions::getInstance(); }
105145

106146
} // namespace ur_sanitizer_layer

0 commit comments

Comments
 (0)