Line data Source code
1 : // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/flags.h"
6 :
7 : #include <cctype>
8 : #include <cerrno>
9 : #include <cstdlib>
10 : #include <sstream>
11 :
12 : #include "src/allocation.h"
13 : #include "src/base/functional.h"
14 : #include "src/base/platform/platform.h"
15 : #include "src/counters.h"
16 : #include "src/cpu-features.h"
17 : #include "src/memcopy.h"
18 : #include "src/ostreams.h"
19 : #include "src/utils.h"
20 : #include "src/wasm/wasm-limits.h"
21 :
22 : namespace v8 {
23 : namespace internal {
24 :
25 : // Define all of our flags.
26 : #define FLAG_MODE_DEFINE
27 : #include "src/flag-definitions.h" // NOLINT(build/include)
28 :
29 : // Define all of our flags default values.
30 : #define FLAG_MODE_DEFINE_DEFAULTS
31 : #include "src/flag-definitions.h" // NOLINT(build/include)
32 :
33 : namespace {
34 :
35 : // This structure represents a single entry in the flag system, with a pointer
36 : // to the actual flag, default value, comment, etc. This is designed to be POD
37 : // initialized as to avoid requiring static constructors.
38 : struct Flag {
39 : enum FlagType {
40 : TYPE_BOOL,
41 : TYPE_MAYBE_BOOL,
42 : TYPE_INT,
43 : TYPE_UINT,
44 : TYPE_UINT64,
45 : TYPE_FLOAT,
46 : TYPE_SIZE_T,
47 : TYPE_STRING,
48 : };
49 :
50 : FlagType type_; // What type of flag, bool, int, or string.
51 : const char* name_; // Name of the flag, ex "my_flag".
52 : void* valptr_; // Pointer to the global flag variable.
53 : const void* defptr_; // Pointer to the default value.
54 : const char* cmt_; // A comment about the flags purpose.
55 : bool owns_ptr_; // Does the flag own its string value?
56 :
57 : FlagType type() const { return type_; }
58 :
59 : const char* name() const { return name_; }
60 :
61 : const char* comment() const { return cmt_; }
62 :
63 : bool* bool_variable() const {
64 : DCHECK(type_ == TYPE_BOOL);
65 : return reinterpret_cast<bool*>(valptr_);
66 : }
67 :
68 : MaybeBoolFlag* maybe_bool_variable() const {
69 : DCHECK(type_ == TYPE_MAYBE_BOOL);
70 : return reinterpret_cast<MaybeBoolFlag*>(valptr_);
71 : }
72 :
73 : int* int_variable() const {
74 : DCHECK(type_ == TYPE_INT);
75 : return reinterpret_cast<int*>(valptr_);
76 : }
77 :
78 : unsigned int* uint_variable() const {
79 : DCHECK(type_ == TYPE_UINT);
80 : return reinterpret_cast<unsigned int*>(valptr_);
81 : }
82 :
83 : uint64_t* uint64_variable() const {
84 : DCHECK(type_ == TYPE_UINT64);
85 : return reinterpret_cast<uint64_t*>(valptr_);
86 : }
87 :
88 : double* float_variable() const {
89 : DCHECK(type_ == TYPE_FLOAT);
90 : return reinterpret_cast<double*>(valptr_);
91 : }
92 :
93 : size_t* size_t_variable() const {
94 : DCHECK(type_ == TYPE_SIZE_T);
95 : return reinterpret_cast<size_t*>(valptr_);
96 : }
97 :
98 : const char* string_value() const {
99 : DCHECK(type_ == TYPE_STRING);
100 4129936 : return *reinterpret_cast<const char**>(valptr_);
101 : }
102 :
103 1417953 : void set_string_value(const char* value, bool owns_ptr) {
104 : DCHECK(type_ == TYPE_STRING);
105 1417953 : const char** ptr = reinterpret_cast<const char**>(valptr_);
106 1417953 : if (owns_ptr_ && *ptr != nullptr) DeleteArray(*ptr);
107 1417953 : *ptr = value;
108 1417953 : owns_ptr_ = owns_ptr;
109 1417953 : }
110 :
111 : bool bool_default() const {
112 : DCHECK(type_ == TYPE_BOOL);
113 79258516 : return *reinterpret_cast<const bool*>(defptr_);
114 : }
115 :
116 : int int_default() const {
117 : DCHECK(type_ == TYPE_INT);
118 10130164 : return *reinterpret_cast<const int*>(defptr_);
119 : }
120 :
121 : unsigned int uint_default() const {
122 : DCHECK(type_ == TYPE_UINT);
123 920924 : return *reinterpret_cast<const unsigned int*>(defptr_);
124 : }
125 :
126 : uint64_t uint64_default() const {
127 : DCHECK(type_ == TYPE_UINT64);
128 230231 : return *reinterpret_cast<const uint64_t*>(defptr_);
129 : }
130 :
131 : double float_default() const {
132 : DCHECK(type_ == TYPE_FLOAT);
133 690693 : return *reinterpret_cast<const double*>(defptr_);
134 : }
135 :
136 : size_t size_t_default() const {
137 : DCHECK(type_ == TYPE_SIZE_T);
138 1151155 : return *reinterpret_cast<const size_t*>(defptr_);
139 : }
140 :
141 : const char* string_default() const {
142 : DCHECK(type_ == TYPE_STRING);
143 5295313 : return *reinterpret_cast<const char* const *>(defptr_);
144 : }
145 :
146 : // Compare this flag's current value against the default.
147 73778149 : bool IsDefault() const {
148 73778149 : switch (type_) {
149 : case TYPE_BOOL:
150 117771152 : return *bool_variable() == bool_default();
151 : case TYPE_MAYBE_BOOL:
152 1198253 : return maybe_bool_variable()->has_value == false;
153 : case TYPE_INT:
154 15063752 : return *int_variable() == int_default();
155 : case TYPE_UINT:
156 1369432 : return *uint_variable() == uint_default();
157 : case TYPE_UINT64:
158 342358 : return *uint64_variable() == uint64_default();
159 : case TYPE_FLOAT:
160 1027074 : return *float_variable() == float_default();
161 : case TYPE_SIZE_T:
162 1711790 : return *size_t_variable() == size_t_default();
163 : case TYPE_STRING: {
164 : const char* str1 = string_value();
165 : const char* str2 = string_default();
166 3937117 : if (str2 == nullptr) return str1 == nullptr;
167 1882969 : if (str1 == nullptr) return str2 == nullptr;
168 1882969 : return strcmp(str1, str2) == 0;
169 : }
170 : }
171 0 : UNREACHABLE();
172 : }
173 :
174 : // Set a flag back to it's default value.
175 25510464 : void Reset() {
176 25510464 : switch (type_) {
177 : case TYPE_BOOL:
178 20372940 : *bool_variable() = bool_default();
179 20372940 : break;
180 : case TYPE_MAYBE_BOOL:
181 413364 : *maybe_bool_variable() = MaybeBoolFlag::Create(false, false);
182 413364 : break;
183 : case TYPE_INT:
184 2598288 : *int_variable() = int_default();
185 2598288 : break;
186 : case TYPE_UINT:
187 236208 : *uint_variable() = uint_default();
188 236208 : break;
189 : case TYPE_UINT64:
190 59052 : *uint64_variable() = uint64_default();
191 59052 : break;
192 : case TYPE_FLOAT:
193 177156 : *float_variable() = float_default();
194 177156 : break;
195 : case TYPE_SIZE_T:
196 295260 : *size_t_variable() = size_t_default();
197 295260 : break;
198 : case TYPE_STRING:
199 1358196 : set_string_value(string_default(), false);
200 1358196 : break;
201 : }
202 25510464 : }
203 : };
204 :
205 : Flag flags[] = {
206 : #define FLAG_MODE_META
207 : #include "src/flag-definitions.h" // NOLINT(build/include)
208 : };
209 :
210 : const size_t num_flags = sizeof(flags) / sizeof(*flags);
211 :
212 : } // namespace
213 :
214 :
215 2185 : static const char* Type2String(Flag::FlagType type) {
216 2185 : switch (type) {
217 : case Flag::TYPE_BOOL: return "bool";
218 35 : case Flag::TYPE_MAYBE_BOOL: return "maybe_bool";
219 230 : case Flag::TYPE_INT: return "int";
220 : case Flag::TYPE_UINT:
221 20 : return "uint";
222 : case Flag::TYPE_UINT64:
223 5 : return "uint64";
224 25 : case Flag::TYPE_FLOAT: return "float";
225 : case Flag::TYPE_SIZE_T:
226 25 : return "size_t";
227 120 : case Flag::TYPE_STRING: return "string";
228 : }
229 0 : UNREACHABLE();
230 : }
231 :
232 :
233 1372373 : std::ostream& operator<<(std::ostream& os, const Flag& flag) { // NOLINT
234 1372373 : switch (flag.type()) {
235 : case Flag::TYPE_BOOL:
236 967504 : os << (*flag.bool_variable() ? "true" : "false");
237 967504 : break;
238 : case Flag::TYPE_MAYBE_BOOL:
239 59 : os << (flag.maybe_bool_variable()->has_value
240 24 : ? (flag.maybe_bool_variable()->value ? "true" : "false")
241 83 : : "unset");
242 59 : break;
243 : case Flag::TYPE_INT:
244 211085 : os << *flag.int_variable();
245 211085 : break;
246 : case Flag::TYPE_UINT:
247 140 : os << *flag.uint_variable();
248 : break;
249 : case Flag::TYPE_UINT64:
250 5 : os << *flag.uint64_variable();
251 : break;
252 : case Flag::TYPE_FLOAT:
253 183 : os << *flag.float_variable();
254 : break;
255 : case Flag::TYPE_SIZE_T:
256 578 : os << *flag.size_t_variable();
257 : break;
258 : case Flag::TYPE_STRING: {
259 : const char* str = flag.string_value();
260 192819 : os << (str ? str : "nullptr");
261 192819 : break;
262 : }
263 : }
264 1372373 : return os;
265 : }
266 :
267 :
268 : // static
269 0 : std::vector<const char*>* FlagList::argv() {
270 0 : std::vector<const char*>* args = new std::vector<const char*>(8);
271 0 : for (size_t i = 0; i < num_flags; ++i) {
272 0 : Flag* f = &flags[i];
273 0 : if (!f->IsDefault()) {
274 : {
275 0 : bool disabled = f->type() == Flag::TYPE_BOOL && !*f->bool_variable();
276 0 : std::ostringstream os;
277 0 : os << (disabled ? "--no" : "--") << f->name();
278 0 : args->push_back(StrDup(os.str().c_str()));
279 : }
280 0 : if (f->type() != Flag::TYPE_BOOL) {
281 0 : std::ostringstream os;
282 0 : os << *f;
283 0 : args->push_back(StrDup(os.str().c_str()));
284 : }
285 : }
286 : }
287 0 : return args;
288 : }
289 :
290 :
291 : inline char NormalizeChar(char ch) {
292 250003891 : return ch == '_' ? '-' : ch;
293 : }
294 :
295 : // Helper function to parse flags: Takes an argument arg and splits it into
296 : // a flag name and flag value (or nullptr if they are missing). negated is set
297 : // if the arg started with "-no" or "--no". The buffer may be used to NUL-
298 : // terminate the name, it must be large enough to hold any possible name.
299 478911 : static void SplitArgument(const char* arg, char* buffer, int buffer_size,
300 : const char** name, const char** value,
301 : bool* negated) {
302 478911 : *name = nullptr;
303 478911 : *value = nullptr;
304 478911 : *negated = false;
305 :
306 478911 : if (arg != nullptr && *arg == '-') {
307 : // find the begin of the flag name
308 343694 : arg++; // remove 1st '-'
309 343694 : if (*arg == '-') {
310 342168 : arg++; // remove 2nd '-'
311 342168 : if (arg[0] == '\0') {
312 : const char* kJSArgumentsFlagName = "js_arguments";
313 0 : *name = kJSArgumentsFlagName;
314 0 : return;
315 : }
316 : }
317 343694 : if (arg[0] == 'n' && arg[1] == 'o') {
318 106188 : arg += 2; // remove "no"
319 212376 : if (NormalizeChar(arg[0]) == '-') arg++; // remove dash after "no".
320 106188 : *negated = true;
321 : }
322 343694 : *name = arg;
323 :
324 : // find the end of the flag name
325 11617278 : while (*arg != '\0' && *arg != '=')
326 5636792 : arg++;
327 :
328 : // get the value if any
329 343694 : if (*arg == '=') {
330 : // make a copy so we can NUL-terminate flag name
331 151561 : size_t n = arg - *name;
332 151561 : CHECK(n < static_cast<size_t>(buffer_size)); // buffer is too small
333 : MemCopy(buffer, *name, n);
334 151561 : buffer[n] = '\0';
335 151561 : *name = buffer;
336 : // get the value
337 151561 : *value = arg + 1;
338 : }
339 : }
340 : }
341 :
342 :
343 : static bool EqualNames(const char* a, const char* b) {
344 395853483 : for (int i = 0; NormalizeChar(a[i]) == NormalizeChar(b[i]); i++) {
345 21398811 : if (a[i] == '\0') {
346 : return true;
347 : }
348 : }
349 : return false;
350 : }
351 :
352 :
353 343694 : static Flag* FindFlag(const char* name) {
354 207401890 : for (size_t i = 0; i < num_flags; ++i) {
355 207716306 : if (EqualNames(name, flags[i].name()))
356 : return &flags[i];
357 : }
358 : return nullptr;
359 : }
360 :
361 : template <typename T>
362 130 : bool TryParseUnsigned(Flag* flag, const char* arg, const char* value,
363 : char** endp, T* out_val) {
364 : // We do not use strtoul because it accepts negative numbers.
365 : // Rejects values >= 2**63 when T is 64 bits wide but that
366 : // seems like an acceptable trade-off.
367 : uint64_t max = static_cast<uint64_t>(std::numeric_limits<T>::max());
368 130 : errno = 0;
369 130 : int64_t val = static_cast<int64_t>(strtoll(value, endp, 10));
370 130 : if (val < 0 || static_cast<uint64_t>(val) > max || errno != 0) {
371 0 : PrintF(stderr,
372 : "Error: Value for flag %s of type %s is out of bounds "
373 : "[0-%" PRIu64 "]\n",
374 : arg, Type2String(flag->type()), max);
375 0 : return false;
376 : }
377 130 : *out_val = static_cast<T>(val);
378 130 : return true;
379 : }
380 :
381 : // static
382 171233 : int FlagList::SetFlagsFromCommandLine(int* argc,
383 : char** argv,
384 : bool remove_flags) {
385 : int return_code = 0;
386 : // parse arguments
387 650114 : for (int i = 1; i < *argc;) {
388 : int j = i; // j > 0
389 478911 : const char* arg = argv[i++];
390 :
391 : // split arg into flag components
392 : char buffer[1*KB];
393 : const char* name;
394 : const char* value;
395 : bool negated;
396 478911 : SplitArgument(arg, buffer, sizeof buffer, &name, &value, &negated);
397 :
398 478911 : if (name != nullptr) {
399 : // lookup the flag
400 343694 : Flag* flag = FindFlag(name);
401 343694 : if (flag == nullptr) {
402 14639 : if (remove_flags) {
403 : // We don't recognize this flag but since we're removing
404 : // the flags we recognize we assume that the remaining flags
405 : // will be processed somewhere else so this flag might make
406 : // sense there.
407 14634 : continue;
408 : } else {
409 5 : PrintF(stderr, "Error: unrecognized flag %s\n", arg);
410 : return_code = j;
411 5 : break;
412 : }
413 : }
414 :
415 : // if we still need a flag value, use the next argument if available
416 480685 : if (flag->type() != Flag::TYPE_BOOL &&
417 480660 : flag->type() != Flag::TYPE_MAYBE_BOOL && value == nullptr) {
418 59 : if (i < *argc) {
419 44 : value = argv[i++];
420 : }
421 59 : if (!value) {
422 15 : PrintF(stderr, "Error: missing value for flag %s of type %s\n", arg,
423 15 : Type2String(flag->type()));
424 : return_code = j;
425 15 : break;
426 : }
427 : }
428 :
429 : // set the flag
430 329040 : char* endp = const_cast<char*>(""); // *endp is only read
431 329040 : switch (flag->type()) {
432 : case Flag::TYPE_BOOL:
433 177425 : *flag->bool_variable() = !negated;
434 177425 : break;
435 : case Flag::TYPE_MAYBE_BOOL:
436 50 : *flag->maybe_bool_variable() = MaybeBoolFlag::Create(true, !negated);
437 25 : break;
438 : case Flag::TYPE_INT:
439 91683 : *flag->int_variable() = static_cast<int>(strtol(value, &endp, 10));
440 91683 : break;
441 : case Flag::TYPE_UINT:
442 24 : if (!TryParseUnsigned(flag, arg, value, &endp,
443 : flag->uint_variable())) {
444 : return_code = j;
445 : }
446 : break;
447 : case Flag::TYPE_UINT64:
448 0 : if (!TryParseUnsigned(flag, arg, value, &endp,
449 : flag->uint64_variable())) {
450 : return_code = j;
451 : }
452 : break;
453 : case Flag::TYPE_FLOAT:
454 20 : *flag->float_variable() = strtod(value, &endp);
455 20 : break;
456 : case Flag::TYPE_SIZE_T:
457 106 : if (!TryParseUnsigned(flag, arg, value, &endp,
458 : flag->size_t_variable())) {
459 : return_code = j;
460 : }
461 : break;
462 : case Flag::TYPE_STRING:
463 59757 : flag->set_string_value(value ? StrDup(value) : nullptr, true);
464 59757 : break;
465 : }
466 :
467 : // handle errors
468 329040 : bool is_bool_type = flag->type() == Flag::TYPE_BOOL ||
469 : flag->type() == Flag::TYPE_MAYBE_BOOL;
470 658080 : if ((is_bool_type && value != nullptr) || (!is_bool_type && negated) ||
471 329040 : *endp != '\0') {
472 : // TODO(neis): TryParseUnsigned may return with {*endp == '\0'} even in
473 : // an error case.
474 10 : PrintF(stderr, "Error: illegal value for flag %s of type %s\n", arg,
475 10 : Type2String(flag->type()));
476 10 : if (is_bool_type) {
477 : PrintF(stderr,
478 0 : "To set or unset a boolean flag, use --flag or --no-flag.\n");
479 : }
480 : return_code = j;
481 : break;
482 : }
483 :
484 : // remove the flag & value from the command
485 329030 : if (remove_flags) {
486 506933 : while (j < i) {
487 168984 : argv[j++] = nullptr;
488 : }
489 : }
490 : }
491 : }
492 :
493 171233 : if (FLAG_help) {
494 0 : PrintHelp();
495 0 : exit(0);
496 : }
497 :
498 171233 : if (remove_flags) {
499 : // shrink the argument list
500 : int j = 1;
501 697802 : for (int i = 1; i < *argc; i++) {
502 318835 : if (argv[i] != nullptr) argv[j++] = argv[i];
503 : }
504 60132 : *argc = j;
505 111101 : } else if (return_code != 0) {
506 15 : if (return_code + 1 < *argc) {
507 0 : PrintF(stderr, "The remaining arguments were ignored:");
508 0 : for (int i = return_code + 1; i < *argc; ++i) {
509 0 : PrintF(stderr, " %s", argv[i]);
510 : }
511 0 : PrintF(stderr, "\n");
512 : }
513 : }
514 171233 : if (return_code != 0) PrintF(stderr, "Try --help for options\n");
515 :
516 171233 : return return_code;
517 : }
518 :
519 :
520 : static char* SkipWhiteSpace(char* p) {
521 480570 : while (*p != '\0' && isspace(*p) != 0) p++;
522 : return p;
523 : }
524 :
525 :
526 : static char* SkipBlackSpace(char* p) {
527 9441472 : while (*p != '\0' && isspace(*p) == 0) p++;
528 : return p;
529 : }
530 :
531 :
532 : // static
533 111096 : int FlagList::SetFlagsFromString(const char* str, int len) {
534 : // make a 0-terminated copy of str
535 111096 : ScopedVector<char> copy0(len + 1);
536 111096 : MemCopy(copy0.start(), str, len);
537 111096 : copy0[len] = '\0';
538 :
539 : // strip leading white space
540 : char* copy = SkipWhiteSpace(copy0.start());
541 :
542 : // count the number of 'arguments'
543 111096 : int argc = 1; // be compatible with SetFlagsFromCommandLine()
544 431266 : for (char* p = copy; *p != '\0'; argc++) {
545 : p = SkipBlackSpace(p);
546 : p = SkipWhiteSpace(p);
547 : }
548 :
549 : // allocate argument array
550 111096 : ScopedVector<char*> argv(argc);
551 :
552 : // split the flags string into arguments
553 111096 : argc = 1; // be compatible with SetFlagsFromCommandLine()
554 431266 : for (char* p = copy; *p != '\0'; argc++) {
555 320170 : argv[argc] = p;
556 : p = SkipBlackSpace(p);
557 160085 : if (*p != '\0') *p++ = '\0'; // 0-terminate argument
558 : p = SkipWhiteSpace(p);
559 : }
560 :
561 222192 : return SetFlagsFromCommandLine(&argc, argv.start(), false);
562 : }
563 :
564 :
565 : // static
566 59052 : void FlagList::ResetAllFlags() {
567 51079980 : for (size_t i = 0; i < num_flags; ++i) {
568 25510464 : flags[i].Reset();
569 : }
570 59052 : }
571 :
572 :
573 : // static
574 5 : void FlagList::PrintHelp() {
575 : CpuFeatures::Probe(false);
576 5 : CpuFeatures::PrintTarget();
577 5 : CpuFeatures::PrintFeatures();
578 :
579 10 : StdoutStream os;
580 : os << "Synopsis:\n"
581 : " shell [options] [--shell] [<file>...]\n"
582 : " d8 [options] [-e <string>] [--shell] [[--module] <file>...]\n\n"
583 : " -e execute a string in V8\n"
584 : " --shell run an interactive JavaScript shell\n"
585 : " --module execute a file as a JavaScript module\n\n"
586 : "Note: the --module option is implicitly enabled for *.mjs files.\n\n"
587 5 : "Options:\n";
588 :
589 4325 : for (const Flag& f : flags) {
590 2160 : os << " --";
591 85930 : for (const char* c = f.name(); *c != '\0'; ++c) {
592 : os << NormalizeChar(*c);
593 : }
594 2160 : os << " (" << f.comment() << ")\n"
595 6480 : << " type: " << Type2String(f.type()) << " default: " << f
596 2160 : << "\n";
597 : }
598 5 : }
599 :
600 :
601 : static uint32_t flag_hash = 0;
602 :
603 :
604 171179 : void ComputeFlagListHash() {
605 342358 : std::ostringstream modified_args_as_string;
606 : #ifdef DEBUG
607 : modified_args_as_string << "debug";
608 : #endif // DEBUG
609 : if (FLAG_embedded_builtins) {
610 171179 : modified_args_as_string << "embedded";
611 : }
612 148069835 : for (size_t i = 0; i < num_flags; ++i) {
613 73949328 : Flag* current = &flags[i];
614 73949328 : if (current->type() == Flag::TYPE_BOOL &&
615 : current->bool_variable() == &FLAG_profile_deserialization) {
616 : // We want to be able to flip --profile-deserialization without
617 : // causing the code cache to get invalidated by this hash.
618 : continue;
619 : }
620 73778149 : if (!current->IsDefault()) {
621 : modified_args_as_string << i;
622 1370213 : modified_args_as_string << *current;
623 : }
624 : }
625 : std::string args(modified_args_as_string.str());
626 : flag_hash = static_cast<uint32_t>(
627 342358 : base::hash_range(args.c_str(), args.c_str() + args.length()));
628 171179 : }
629 :
630 :
631 : // static
632 171179 : void FlagList::EnforceFlagImplications() {
633 : #define FLAG_MODE_DEFINE_IMPLICATIONS
634 : #include "src/flag-definitions.h" // NOLINT(build/include)
635 : #undef FLAG_MODE_DEFINE_IMPLICATIONS
636 171179 : ComputeFlagListHash();
637 171179 : }
638 :
639 :
640 1002 : uint32_t FlagList::Hash() { return flag_hash; }
641 : } // namespace internal
642 120216 : } // namespace v8
|