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
2123namespace 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