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_libc.h"
17 #include "sanitizer_list.h"
18 #include "sanitizer_flag_parser.h"
20 namespace __sanitizer {
22 CommonFlags common_flags_dont_use;
24 void CommonFlags::SetDefaults() {
25 #define COMMON_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
26 #include "sanitizer_flags.inc"
30 void CommonFlags::CopyFrom(const CommonFlags &other) {
31 internal_memcpy(this, &other, sizeof(*this));
34 // Copy the string from "s" to "out", making the following substitutions:
35 // %b = binary basename
37 void SubstituteForFlagValue(const char *s, char *out, uptr out_size) {
38 char *out_end = out + out_size;
39 while (*s && out < out_end - 1) {
46 const char *base = GetProcessName();
48 while (*base && out < out_end - 1)
54 int pid = internal_getpid();
56 char *buf_pos = buf + 32;
58 *--buf_pos = (pid % 10) + '0';
61 while (buf_pos < buf + 32 && out < out_end - 1)
71 CHECK(out < out_end - 1);
75 class FlagHandlerInclude : public FlagHandlerBase {
78 const char *original_path_;
81 explicit FlagHandlerInclude(FlagParser *parser, bool ignore_missing)
82 : parser_(parser), ignore_missing_(ignore_missing), original_path_("") {}
83 bool Parse(const char *value) final {
84 original_path_ = value;
85 if (internal_strchr(value, '%')) {
86 char *buf = (char *)MmapOrDie(kMaxPathLength, "FlagHandlerInclude");
87 SubstituteForFlagValue(value, buf, kMaxPathLength);
88 bool res = parser_->ParseFile(buf, ignore_missing_);
89 UnmapOrDie(buf, kMaxPathLength);
92 return parser_->ParseFile(value, ignore_missing_);
94 bool Format(char *buffer, uptr size) {
95 // Note `original_path_` isn't actually what's parsed due to `%`
96 // substitutions. Printing the substituted path would require holding onto
98 return FormatString(buffer, size, original_path_);
102 void RegisterIncludeFlags(FlagParser *parser, CommonFlags *cf) {
103 FlagHandlerInclude *fh_include = new (FlagParser::Alloc)
104 FlagHandlerInclude(parser, /*ignore_missing*/ false);
105 parser->RegisterHandler("include", fh_include,
106 "read more options from the given file");
107 FlagHandlerInclude *fh_include_if_exists = new (FlagParser::Alloc)
108 FlagHandlerInclude(parser, /*ignore_missing*/ true);
109 parser->RegisterHandler(
110 "include_if_exists", fh_include_if_exists,
111 "read more options from the given file (if it exists)");
114 void RegisterCommonFlags(FlagParser *parser, CommonFlags *cf) {
115 #define COMMON_FLAG(Type, Name, DefaultValue, Description) \
116 RegisterFlag(parser, #Name, Description, &cf->Name);
117 #include "sanitizer_flags.inc"
120 RegisterIncludeFlags(parser, cf);
123 void InitializeCommonFlags(CommonFlags *cf) {
124 // need to record coverage to generate coverage report.
125 cf->coverage |= cf->html_cov_report;
126 SetVerbosity(cf->verbosity);
129 } // namespace __sanitizer