1 //===-- scudo_flags.cpp -----------------------------------------*- C++ -*-===//
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 /// Hardened Allocator flag parsing logic.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "scudo_flags.h"
15 #include "scudo_utils.h"
16 
17 #include "sanitizer_common/sanitizer_flags.h"
18 #include "sanitizer_common/sanitizer_flag_parser.h"
19 
20 namespace __scudo {
21 
22 Flags scudo_flags_dont_use_directly;  // use via flags().
23 
setDefaults()24 void Flags::setDefaults() {
25 #define SCUDO_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
26 #include "scudo_flags.inc"
27 #undef SCUDO_FLAG
28 }
29 
RegisterScudoFlags(FlagParser * parser,Flags * f)30 static void RegisterScudoFlags(FlagParser *parser, Flags *f) {
31 #define SCUDO_FLAG(Type, Name, DefaultValue, Description) \
32   RegisterFlag(parser, #Name, Description, &f->Name);
33 #include "scudo_flags.inc"
34 #undef SCUDO_FLAG
35 }
36 
initFlags()37 void initFlags() {
38   SetCommonFlagsDefaults();
39   {
40     CommonFlags cf;
41     cf.CopyFrom(*common_flags());
42     cf.exitcode = 1;
43     OverrideCommonFlags(cf);
44   }
45   Flags *f = getFlags();
46   f->setDefaults();
47 
48   FlagParser scudo_parser;
49   RegisterScudoFlags(&scudo_parser, f);
50   RegisterCommonFlags(&scudo_parser);
51 
52   scudo_parser.ParseString(GetEnv("SCUDO_OPTIONS"));
53 
54   InitializeCommonFlags();
55 
56   // Sanity checks and default settings for the Quarantine parameters.
57 
58   if (f->QuarantineSizeMb < 0) {
59     const int DefaultQuarantineSizeMb = 64;
60     f->QuarantineSizeMb = DefaultQuarantineSizeMb;
61   }
62   // We enforce an upper limit for the quarantine size of 4Gb.
63   if (f->QuarantineSizeMb > (4 * 1024)) {
64     dieWithMessage("ERROR: the quarantine size is too large\n");
65   }
66   if (f->ThreadLocalQuarantineSizeKb < 0) {
67     const int DefaultThreadLocalQuarantineSizeKb = 1024;
68     f->ThreadLocalQuarantineSizeKb = DefaultThreadLocalQuarantineSizeKb;
69   }
70   // And an upper limit of 128Mb for the thread quarantine cache.
71   if (f->ThreadLocalQuarantineSizeKb > (128 * 1024)) {
72     dieWithMessage("ERROR: the per thread quarantine cache size is too "
73                    "large\n");
74   }
75 }
76 
getFlags()77 Flags *getFlags() {
78   return &scudo_flags_dont_use_directly;
79 }
80 
81 }
82