1 //===-- sanitizer_flags_test.cpp ------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
11 //===----------------------------------------------------------------------===//
12 #include "sanitizer_common/sanitizer_common.h"
13 #include "sanitizer_common/sanitizer_flags.h"
14 #include "sanitizer_common/sanitizer_flag_parser.h"
15 #include "sanitizer_common/sanitizer_libc.h"
16 #include "sanitizer_common/sanitizer_allocator_internal.h"
17 #include "gtest/gtest.h"
22 namespace __sanitizer
{
24 static const char kFlagName
[] = "flag_name";
25 static const char kFlagDesc
[] = "flag description";
28 static void TestFlag(T start_value
, const char *env
, T final_value
) {
32 RegisterFlag(&parser
, kFlagName
, kFlagDesc
, &flag
);
34 parser
.ParseString(env
);
36 EXPECT_EQ(final_value
, flag
);
38 // Reporting unrecognized flags is needed to reset them.
39 ReportUnrecognizedFlags();
43 void TestFlag(const char *start_value
, const char *env
,
44 const char *final_value
) {
45 const char *flag
= start_value
;
48 RegisterFlag(&parser
, kFlagName
, kFlagDesc
, &flag
);
50 parser
.ParseString(env
);
52 EXPECT_EQ(0, internal_strcmp(final_value
, flag
));
54 // Reporting unrecognized flags is needed to reset them.
55 ReportUnrecognizedFlags();
58 TEST(SanitizerCommon
, BooleanFlags
) {
59 TestFlag(false, "flag_name=1", true);
60 TestFlag(false, "flag_name=yes", true);
61 TestFlag(false, "flag_name=true", true);
62 TestFlag(true, "flag_name=0", false);
63 TestFlag(true, "flag_name=no", false);
64 TestFlag(true, "flag_name=false", false);
66 EXPECT_DEATH(TestFlag(false, "flag_name", true), "expected '='");
67 EXPECT_DEATH(TestFlag(false, "flag_name=", true),
68 "Invalid value for bool option: ''");
69 EXPECT_DEATH(TestFlag(false, "flag_name=2", true),
70 "Invalid value for bool option: '2'");
71 EXPECT_DEATH(TestFlag(false, "flag_name=-1", true),
72 "Invalid value for bool option: '-1'");
73 EXPECT_DEATH(TestFlag(false, "flag_name=on", true),
74 "Invalid value for bool option: 'on'");
77 TEST(SanitizerCommon
, HandleSignalMode
) {
78 TestFlag(kHandleSignalNo
, "flag_name=1", kHandleSignalYes
);
79 TestFlag(kHandleSignalNo
, "flag_name=yes", kHandleSignalYes
);
80 TestFlag(kHandleSignalNo
, "flag_name=true", kHandleSignalYes
);
81 TestFlag(kHandleSignalYes
, "flag_name=0", kHandleSignalNo
);
82 TestFlag(kHandleSignalYes
, "flag_name=no", kHandleSignalNo
);
83 TestFlag(kHandleSignalYes
, "flag_name=false", kHandleSignalNo
);
84 TestFlag(kHandleSignalNo
, "flag_name=2", kHandleSignalExclusive
);
85 TestFlag(kHandleSignalYes
, "flag_name=exclusive", kHandleSignalExclusive
);
87 EXPECT_DEATH(TestFlag(kHandleSignalNo
, "flag_name", kHandleSignalNo
),
89 EXPECT_DEATH(TestFlag(kHandleSignalNo
, "flag_name=", kHandleSignalNo
),
90 "Invalid value for signal handler option: ''");
91 EXPECT_DEATH(TestFlag(kHandleSignalNo
, "flag_name=3", kHandleSignalNo
),
92 "Invalid value for signal handler option: '3'");
93 EXPECT_DEATH(TestFlag(kHandleSignalNo
, "flag_name=-1", kHandleSignalNo
),
94 "Invalid value for signal handler option: '-1'");
95 EXPECT_DEATH(TestFlag(kHandleSignalNo
, "flag_name=on", kHandleSignalNo
),
96 "Invalid value for signal handler option: 'on'");
99 TEST(SanitizerCommon
, IntFlags
) {
100 TestFlag(-11, 0, -11);
101 TestFlag(-11, "flag_name=0", 0);
102 TestFlag(-11, "flag_name=42", 42);
103 TestFlag(-11, "flag_name=-42", -42);
105 // Unrecognized flags are ignored.
106 TestFlag(-11, "--flag_name=42", -11);
107 TestFlag(-11, "zzzzzzz=42", -11);
109 EXPECT_DEATH(TestFlag(-11, "flag_name", 0), "expected '='");
110 EXPECT_DEATH(TestFlag(-11, "flag_name=42U", 0),
111 "Invalid value for int option");
114 TEST(SanitizerCommon
, LongLongIntFlags
) {
116 s64 IntMin
= INT64_MIN
;
117 s64 IntMax
= INT64_MAX
;
118 TestFlag(InitValue
, "flag_name=0", 0ll);
119 TestFlag(InitValue
, "flag_name=42", 42ll);
120 TestFlag(InitValue
, "flag_name=-42", -42ll);
122 TestFlag(InitValue
, "flag_name=-9223372036854775808", IntMin
);
123 TestFlag(InitValue
, "flag_name=9223372036854775807", IntMax
);
125 TestFlag(InitValue
, "flag_name=-92233720368547758080000", IntMin
);
126 TestFlag(InitValue
, "flag_name=92233720368547758070000", IntMax
);
129 TEST(SanitizerCommon
, StrFlags
) {
130 TestFlag("zzz", 0, "zzz");
131 TestFlag("zzz", "flag_name=", "");
132 TestFlag("zzz", "flag_name=abc", "abc");
133 TestFlag("", "flag_name=abc", "abc");
134 TestFlag("", "flag_name='abc zxc'", "abc zxc");
135 // TestStrFlag("", "flag_name=\"abc qwe\" asd", "abc qwe");
138 static void TestTwoFlags(const char *env
, bool expected_flag1
,
139 const char *expected_flag2
,
140 const char *name1
= "flag1",
141 const char *name2
= "flag2") {
142 bool flag1
= !expected_flag1
;
143 const char *flag2
= "";
146 RegisterFlag(&parser
, name1
, kFlagDesc
, &flag1
);
147 RegisterFlag(&parser
, name2
, kFlagDesc
, &flag2
);
149 parser
.ParseString(env
);
151 EXPECT_EQ(expected_flag1
, flag1
);
152 EXPECT_EQ(0, internal_strcmp(flag2
, expected_flag2
));
154 // Reporting unrecognized flags is needed to reset them.
155 ReportUnrecognizedFlags();
158 TEST(SanitizerCommon
, MultipleFlags
) {
159 TestTwoFlags("flag1=1 flag2='zzz'", true, "zzz");
160 TestTwoFlags("flag2='qxx' flag1=0", false, "qxx");
161 TestTwoFlags("flag1=false:flag2='zzz'", false, "zzz");
162 TestTwoFlags("flag2=qxx:flag1=yes", true, "qxx");
163 TestTwoFlags("flag2=qxx\nflag1=yes", true, "qxx");
164 TestTwoFlags("flag2=qxx\r\nflag1=yes", true, "qxx");
165 TestTwoFlags("flag2=qxx\tflag1=yes", true, "qxx");
168 TEST(SanitizerCommon
, CommonSuffixFlags
) {
169 TestTwoFlags("flag=1 other_flag='zzz'", true, "zzz", "flag", "other_flag");
170 TestTwoFlags("other_flag='zzz' flag=1", true, "zzz", "flag", "other_flag");
171 TestTwoFlags("other_flag=' flag=0 ' flag=1", true, " flag=0 ", "flag",
173 TestTwoFlags("flag=1 other_flag=' flag=0 '", true, " flag=0 ", "flag",
177 TEST(SanitizerCommon
, CommonFlags
) {
180 RegisterCommonFlags(&parser
, &cf
);
183 EXPECT_TRUE(cf
.symbolize
);
184 EXPECT_STREQ(".", cf
.coverage_dir
);
186 cf
.symbolize
= false;
188 cf
.heap_profile
= true;
189 cf
.log_path
= "path/one";
191 parser
.ParseString("symbolize=1:heap_profile=false log_path='path/two'");
192 EXPECT_TRUE(cf
.symbolize
);
193 EXPECT_TRUE(cf
.coverage
);
194 EXPECT_FALSE(cf
.heap_profile
);
195 EXPECT_STREQ("path/two", cf
.log_path
);
198 } // namespace __sanitizer