1 //===-- scudo_flags.cpp -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// Hardened Allocator flag parsing logic.
12 //===----------------------------------------------------------------------===//
14 #include "scudo_flags.h"
15 #include "scudo_utils.h"
17 #include "sanitizer_common/sanitizer_flags.h"
18 #include "sanitizer_common/sanitizer_flag_parser.h"
20 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
21 const char* __scudo_default_options();
25 Flags ScudoFlags; // Use via getFlags().
27 void Flags::setDefaults() {
28 #define SCUDO_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
29 #include "scudo_flags.inc"
33 static void RegisterScudoFlags(FlagParser *parser, Flags *f) {
34 #define SCUDO_FLAG(Type, Name, DefaultValue, Description) \
35 RegisterFlag(parser, #Name, Description, &f->Name);
36 #include "scudo_flags.inc"
40 static const char *callGetScudoDefaultOptions() {
41 return (&__scudo_default_options) ? __scudo_default_options() : "";
45 SetCommonFlagsDefaults();
48 cf.CopyFrom(*common_flags());
50 OverrideCommonFlags(cf);
52 Flags *f = getFlags();
55 FlagParser ScudoParser;
56 RegisterScudoFlags(&ScudoParser, f);
57 RegisterCommonFlags(&ScudoParser);
59 // Override from user-specified string.
60 const char *ScudoDefaultOptions = callGetScudoDefaultOptions();
61 ScudoParser.ParseString(ScudoDefaultOptions);
63 // Override from environment.
64 ScudoParser.ParseString(GetEnv("SCUDO_OPTIONS"));
66 InitializeCommonFlags();
68 // Sanity checks and default settings for the Quarantine parameters.
70 if (f->QuarantineSizeMb < 0) {
71 const int DefaultQuarantineSizeMb = FIRST_32_SECOND_64(4, 16);
72 f->QuarantineSizeMb = DefaultQuarantineSizeMb;
74 // We enforce an upper limit for the quarantine size of 4Gb.
75 if (f->QuarantineSizeMb > (4 * 1024)) {
76 dieWithMessage("ERROR: the quarantine size is too large\n");
78 if (f->ThreadLocalQuarantineSizeKb < 0) {
79 const int DefaultThreadLocalQuarantineSizeKb =
80 FIRST_32_SECOND_64(64, 256);
81 f->ThreadLocalQuarantineSizeKb = DefaultThreadLocalQuarantineSizeKb;
83 // And an upper limit of 128Mb for the thread quarantine cache.
84 if (f->ThreadLocalQuarantineSizeKb > (128 * 1024)) {
85 dieWithMessage("ERROR: the per thread quarantine cache size is too "
88 if (f->ThreadLocalQuarantineSizeKb == 0 && f->QuarantineSizeMb > 0) {
89 dieWithMessage("ERROR: ThreadLocalQuarantineSizeKb can be set to 0 only "
90 "when QuarantineSizeMb is set to 0\n");
98 } // namespace __scudo