aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang16-rt/lib/gwp_asan/optional/options_parser.cpp
blob: 60234124e8ede9306059cc4e4e008fa880e20f77 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//===-- options_parser.cpp --------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "gwp_asan/optional/options_parser.h"
#include "gwp_asan/optional/printf.h"
#include "gwp_asan/utilities.h"

#include <assert.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

namespace {
enum class OptionType : uint8_t {
  OT_bool,
  OT_int,
};

#define InvokeIfNonNull(Printf, ...)                                           \
  do {                                                                         \
    if (Printf)                                                                \
      Printf(__VA_ARGS__);                                                     \
  } while (0);

class OptionParser {
public:
  explicit OptionParser(gwp_asan::Printf_t PrintfForWarnings)
      : Printf(PrintfForWarnings) {}
  void registerOption(const char *Name, const char *Desc, OptionType Type,
                      void *Var);
  void parseString(const char *S);
  void printOptionDescriptions();

private:
  // Calculate at compile-time how many options are available.
#define GWP_ASAN_OPTION(...) +1
  static constexpr size_t MaxOptions = 0
#include "gwp_asan/options.inc"
      ;
#undef GWP_ASAN_OPTION

  struct Option {
    const char *Name;
    const char *Desc;
    OptionType Type;
    void *Var;
  } Options[MaxOptions];

  size_t NumberOfOptions = 0;
  const char *Buffer = nullptr;
  uintptr_t Pos = 0;
  gwp_asan::Printf_t Printf = nullptr;

  void skipWhitespace();
  void parseOptions();
  bool parseOption();
  bool setOptionToValue(const char *Name, const char *Value);
};

void OptionParser::printOptionDescriptions() {
  InvokeIfNonNull(Printf, "GWP-ASan: Available options:\n");
  for (size_t I = 0; I < NumberOfOptions; ++I)
    InvokeIfNonNull(Printf, "\t%s\n\t\t- %s\n", Options[I].Name,
                    Options[I].Desc);
}

bool isSeparator(char C) {
  return C == ' ' || C == ',' || C == ':' || C == '\n' || C == '\t' ||
         C == '\r';
}

bool isSeparatorOrNull(char C) { return !C || isSeparator(C); }

void OptionParser::skipWhitespace() {
  while (isSeparator(Buffer[Pos]))
    ++Pos;
}

bool OptionParser::parseOption() {
  const uintptr_t NameStart = Pos;
  while (Buffer[Pos] != '=' && !isSeparatorOrNull(Buffer[Pos]))
    ++Pos;

  const char *Name = Buffer + NameStart;
  if (Buffer[Pos] != '=') {
    InvokeIfNonNull(Printf, "GWP-ASan: Expected '=' when parsing option '%s'.",
                    Name);
    return false;
  }
  const uintptr_t ValueStart = ++Pos;
  const char *Value;
  if (Buffer[Pos] == '\'' || Buffer[Pos] == '"') {
    const char Quote = Buffer[Pos++];
    while (Buffer[Pos] != 0 && Buffer[Pos] != Quote)
      ++Pos;
    if (Buffer[Pos] == 0) {
      InvokeIfNonNull(Printf, "GWP-ASan: Unterminated string in option '%s'.",
                      Name);
      return false;
    }
    Value = Buffer + ValueStart + 1;
    ++Pos; // consume the closing quote
  } else {
    while (!isSeparatorOrNull(Buffer[Pos]))
      ++Pos;
    Value = Buffer + ValueStart;
  }

  return setOptionToValue(Name, Value);
}

void OptionParser::parseOptions() {
  while (true) {
    skipWhitespace();
    if (Buffer[Pos] == 0)
      break;
    if (!parseOption()) {
      InvokeIfNonNull(Printf, "GWP-ASan: Options parsing failed.\n");
      return;
    }
  }
}

void OptionParser::parseString(const char *S) {
  if (!S)
    return;
  Buffer = S;
  Pos = 0;
  parseOptions();
}

bool parseBool(const char *Value, bool *b) {
  if (strncmp(Value, "0", 1) == 0 || strncmp(Value, "no", 2) == 0 ||
      strncmp(Value, "false", 5) == 0) {
    *b = false;
    return true;
  }
  if (strncmp(Value, "1", 1) == 0 || strncmp(Value, "yes", 3) == 0 ||
      strncmp(Value, "true", 4) == 0) {
    *b = true;
    return true;
  }
  return false;
}

bool OptionParser::setOptionToValue(const char *Name, const char *Value) {
  for (size_t I = 0; I < NumberOfOptions; ++I) {
    const uintptr_t Len = strlen(Options[I].Name);
    if (strncmp(Name, Options[I].Name, Len) != 0 || Name[Len] != '=')
      continue;
    bool Ok = false;
    switch (Options[I].Type) {
    case OptionType::OT_bool:
      Ok = parseBool(Value, reinterpret_cast<bool *>(Options[I].Var));
      if (!Ok)
        InvokeIfNonNull(
            Printf, "GWP-ASan: Invalid boolean value '%s' for option '%s'.\n",
            Value, Options[I].Name);
      break;
    case OptionType::OT_int:
      char *ValueEnd;
      *reinterpret_cast<int *>(Options[I].Var) =
          static_cast<int>(strtol(Value, &ValueEnd, 10));
      Ok =
          *ValueEnd == '"' || *ValueEnd == '\'' || isSeparatorOrNull(*ValueEnd);
      if (!Ok)
        InvokeIfNonNull(
            Printf, "GWP-ASan: Invalid integer value '%s' for option '%s'.\n",
            Value, Options[I].Name);
      break;
    }
    return Ok;
  }

  InvokeIfNonNull(Printf, "GWP-ASan: Unknown option '%s'.", Name);
  return true;
}

void OptionParser::registerOption(const char *Name, const char *Desc,
                                  OptionType Type, void *Var) {
  assert(NumberOfOptions < MaxOptions &&
         "GWP-ASan Error: Ran out of space for options.\n");
  Options[NumberOfOptions].Name = Name;
  Options[NumberOfOptions].Desc = Desc;
  Options[NumberOfOptions].Type = Type;
  Options[NumberOfOptions].Var = Var;
  ++NumberOfOptions;
}

void registerGwpAsanOptions(OptionParser *parser,
                            gwp_asan::options::Options *o) {
#define GWP_ASAN_OPTION(Type, Name, DefaultValue, Description)                 \
  parser->registerOption(#Name, Description, OptionType::OT_##Type, &o->Name);
#include "gwp_asan/options.inc"
#undef GWP_ASAN_OPTION
}

const char *getGwpAsanDefaultOptions() {
  return (__gwp_asan_default_options) ? __gwp_asan_default_options() : "";
}

gwp_asan::options::Options *getOptionsInternal() {
  static gwp_asan::options::Options GwpAsanOptions;
  return &GwpAsanOptions;
}
} // anonymous namespace

namespace gwp_asan {
namespace options {

void initOptions(const char *OptionsStr, Printf_t PrintfForWarnings) {
  Options *o = getOptionsInternal();
  o->setDefaults();

  OptionParser Parser(PrintfForWarnings);
  registerGwpAsanOptions(&Parser, o);

  // Override from the weak function definition in this executable.
  Parser.parseString(getGwpAsanDefaultOptions());

  // Override from the provided options string.
  Parser.parseString(OptionsStr);

  if (o->help)
    Parser.printOptionDescriptions();

  if (!o->Enabled)
    return;

  if (o->MaxSimultaneousAllocations <= 0) {
    InvokeIfNonNull(
        PrintfForWarnings,
        "GWP-ASan ERROR: MaxSimultaneousAllocations must be > 0 when GWP-ASan "
        "is enabled.\n");
    o->Enabled = false;
  }
  if (o->SampleRate <= 0) {
    InvokeIfNonNull(
        PrintfForWarnings,
        "GWP-ASan ERROR: SampleRate must be > 0 when GWP-ASan is enabled.\n");
    o->Enabled = false;
  }
}

void initOptions(Printf_t PrintfForWarnings) {
  initOptions(getenv("GWP_ASAN_OPTIONS"), PrintfForWarnings);
}

Options &getOptions() { return *getOptionsInternal(); }

} // namespace options
} // namespace gwp_asan