1 //===-- sanitizer_flags.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 //===----------------------------------------------------------------------===//
13 #include "sanitizer_flags.h"
15 #include "sanitizer_common.h"
16 #include "sanitizer_flag_parser.h"
17 #include "sanitizer_libc.h"
18 #include "sanitizer_linux.h"
19 #include "sanitizer_list.h"
21 namespace __sanitizer
{
23 CommonFlags common_flags_dont_use
;
25 void CommonFlags::SetDefaults() {
26 #define COMMON_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
27 #include "sanitizer_flags.inc"
31 void CommonFlags::CopyFrom(const CommonFlags
&other
) {
32 internal_memcpy(this, &other
, sizeof(*this));
35 // Copy the string from "s" to "out", making the following substitutions:
36 // %b = binary basename
38 // %d = binary directory
39 void SubstituteForFlagValue(const char *s
, char *out
, uptr out_size
) {
40 char *out_end
= out
+ out_size
;
41 while (*s
&& out
< out_end
- 1) {
48 const char *base
= GetProcessName();
50 while (*base
&& out
< out_end
- 1)
56 int pid
= internal_getpid();
58 char *buf_pos
= buf
+ 32;
60 *--buf_pos
= (pid
% 10) + '0';
63 while (buf_pos
< buf
+ 32 && out
< out_end
- 1)
69 uptr len
= ReadBinaryDir(out
, out_end
- out
);
79 CHECK(out
< out_end
- 1);
83 class FlagHandlerInclude final
: public FlagHandlerBase
{
86 const char *original_path_
;
89 explicit FlagHandlerInclude(FlagParser
*parser
, bool ignore_missing
)
90 : parser_(parser
), ignore_missing_(ignore_missing
), original_path_("") {}
91 bool Parse(const char *value
) final
{
92 original_path_
= value
;
93 if (internal_strchr(value
, '%')) {
94 char *buf
= (char *)MmapOrDie(kMaxPathLength
, "FlagHandlerInclude");
95 SubstituteForFlagValue(value
, buf
, kMaxPathLength
);
96 bool res
= parser_
->ParseFile(buf
, ignore_missing_
);
97 UnmapOrDie(buf
, kMaxPathLength
);
100 return parser_
->ParseFile(value
, ignore_missing_
);
102 bool Format(char *buffer
, uptr size
) override
{
103 // Note `original_path_` isn't actually what's parsed due to `%`
104 // substitutions. Printing the substituted path would require holding onto
106 return FormatString(buffer
, size
, original_path_
);
110 void RegisterIncludeFlags(FlagParser
*parser
, CommonFlags
*cf
) {
111 FlagHandlerInclude
*fh_include
= new (FlagParser::Alloc
)
112 FlagHandlerInclude(parser
, /*ignore_missing*/ false);
113 parser
->RegisterHandler("include", fh_include
,
114 "read more options from the given file");
115 FlagHandlerInclude
*fh_include_if_exists
= new (FlagParser::Alloc
)
116 FlagHandlerInclude(parser
, /*ignore_missing*/ true);
117 parser
->RegisterHandler(
118 "include_if_exists", fh_include_if_exists
,
119 "read more options from the given file (if it exists)");
122 void RegisterCommonFlags(FlagParser
*parser
, CommonFlags
*cf
) {
123 #define COMMON_FLAG(Type, Name, DefaultValue, Description) \
124 RegisterFlag(parser, #Name, Description, &cf->Name);
125 #include "sanitizer_flags.inc"
128 RegisterIncludeFlags(parser
, cf
);
131 void InitializeCommonFlags(CommonFlags
*cf
) {
132 // need to record coverage to generate coverage report.
133 cf
->coverage
|= cf
->html_cov_report
;
134 SetVerbosity(cf
->verbosity
);
136 InitializePlatformCommonFlags(cf
);
139 } // namespace __sanitizer