]> git.proxmox.com Git - rustc.git/blob - src/compiler-rt/lib/sanitizer_common/sanitizer_flags.cc
New upstream version 1.19.0+dfsg3
[rustc.git] / src / compiler-rt / lib / sanitizer_common / sanitizer_flags.cc
1 //===-- sanitizer_flags.cc ------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "sanitizer_flags.h"
15
16 #include "sanitizer_common.h"
17 #include "sanitizer_libc.h"
18 #include "sanitizer_list.h"
19 #include "sanitizer_flag_parser.h"
20
21 namespace __sanitizer {
22
23 CommonFlags common_flags_dont_use;
24
25 struct FlagDescription {
26 const char *name;
27 const char *description;
28 FlagDescription *next;
29 };
30
31 IntrusiveList<FlagDescription> flag_descriptions;
32
33 void CommonFlags::SetDefaults() {
34 #define COMMON_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
35 #include "sanitizer_flags.inc"
36 #undef COMMON_FLAG
37 }
38
39 void CommonFlags::CopyFrom(const CommonFlags &other) {
40 internal_memcpy(this, &other, sizeof(*this));
41 }
42
43 // Copy the string from "s" to "out", making the following substitutions:
44 // %b = binary basename
45 // %p = pid
46 void SubstituteForFlagValue(const char *s, char *out, uptr out_size) {
47 char *out_end = out + out_size;
48 while (*s && out < out_end - 1) {
49 if (s[0] != '%') {
50 *out++ = *s++;
51 continue;
52 }
53 switch (s[1]) {
54 case 'b': {
55 const char *base = GetProcessName();
56 CHECK(base);
57 while (*base && out < out_end - 1)
58 *out++ = *base++;
59 s += 2; // skip "%b"
60 break;
61 }
62 case 'p': {
63 int pid = internal_getpid();
64 char buf[32];
65 char *buf_pos = buf + 32;
66 do {
67 *--buf_pos = (pid % 10) + '0';
68 pid /= 10;
69 } while (pid);
70 while (buf_pos < buf + 32 && out < out_end - 1)
71 *out++ = *buf_pos++;
72 s += 2; // skip "%p"
73 break;
74 }
75 default:
76 *out++ = *s++;
77 break;
78 }
79 }
80 CHECK(out < out_end - 1);
81 *out = '\0';
82 }
83
84 class FlagHandlerInclude : public FlagHandlerBase {
85 FlagParser *parser_;
86 bool ignore_missing_;
87
88 public:
89 explicit FlagHandlerInclude(FlagParser *parser, bool ignore_missing)
90 : parser_(parser), ignore_missing_(ignore_missing) {}
91 bool Parse(const char *value) final {
92 if (internal_strchr(value, '%')) {
93 char *buf = (char *)MmapOrDie(kMaxPathLength, "FlagHandlerInclude");
94 SubstituteForFlagValue(value, buf, kMaxPathLength);
95 bool res = parser_->ParseFile(buf, ignore_missing_);
96 UnmapOrDie(buf, kMaxPathLength);
97 return res;
98 }
99 return parser_->ParseFile(value, ignore_missing_);
100 }
101 };
102
103 void RegisterIncludeFlags(FlagParser *parser, CommonFlags *cf) {
104 FlagHandlerInclude *fh_include = new (FlagParser::Alloc) // NOLINT
105 FlagHandlerInclude(parser, /*ignore_missing*/ false);
106 parser->RegisterHandler("include", fh_include,
107 "read more options from the given file");
108 FlagHandlerInclude *fh_include_if_exists = new (FlagParser::Alloc) // NOLINT
109 FlagHandlerInclude(parser, /*ignore_missing*/ true);
110 parser->RegisterHandler(
111 "include_if_exists", fh_include_if_exists,
112 "read more options from the given file (if it exists)");
113 }
114
115 void RegisterCommonFlags(FlagParser *parser, CommonFlags *cf) {
116 #define COMMON_FLAG(Type, Name, DefaultValue, Description) \
117 RegisterFlag(parser, #Name, Description, &cf->Name);
118 #include "sanitizer_flags.inc"
119 #undef COMMON_FLAG
120
121 RegisterIncludeFlags(parser, cf);
122 }
123
124 void InitializeCommonFlags(CommonFlags *cf) {
125 // need to record coverage to generate coverage report.
126 cf->coverage |= cf->html_cov_report;
127 SetVerbosity(cf->verbosity);
128 }
129
130 } // namespace __sanitizer