16
16
#include " ur/ur.hpp"
17
17
#include " ur_sanitizer_layer.hpp"
18
18
19
- #include < cstdint>
19
+ #include < algorithm>
20
+ #include < cstring>
21
+ #include < stdexcept>
20
22
21
23
namespace ur_sanitizer_layer {
22
24
@@ -30,9 +32,7 @@ struct AsanOptions {
30
32
return instance;
31
33
}
32
34
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 ;
36
36
uint64_t MinRZSize = 16 ;
37
37
uint64_t MaxRZSize = 2048 ;
38
38
uint32_t MaxQuarantineSizeMB = 0 ;
@@ -45,29 +45,69 @@ struct AsanOptions {
45
45
return ;
46
46
}
47
47
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
+ };
53
91
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" );
55
96
if (KV != OptionsEnvMap->end ()) {
56
97
auto Value = KV->second .front ();
57
98
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;
59
104
} catch (...) {
60
105
die (" <SANITIZER>[ERROR]: \" quarantine_size_mb\" should be "
61
- " an integer" );
106
+ " an positive integer that smaller than or equal to "
107
+ " 4294967295." );
62
108
}
63
109
}
64
110
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
-
71
111
KV = OptionsEnvMap->find (" redzone" );
72
112
if (KV != OptionsEnvMap->end ()) {
73
113
auto Value = KV->second .front ();
@@ -101,6 +141,6 @@ struct AsanOptions {
101
141
}
102
142
};
103
143
104
- inline AsanOptions &Options () { return AsanOptions::getInstance (); }
144
+ inline const AsanOptions &Options () { return AsanOptions::getInstance (); }
105
145
106
146
} // namespace ur_sanitizer_layer
0 commit comments