1 //===-- scudo_flags.cpp -----------------------------------------*- C++ -*-===//
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 /// Hardened Allocator flag parsing logic.
11 //===----------------------------------------------------------------------===//
13 #include "scudo_flags.h"
14 #include "scudo_interface_internal.h"
15 #include "scudo_utils.h"
17 #include "sanitizer_common/sanitizer_flags.h"
18 #include "sanitizer_common/sanitizer_flag_parser.h"
22 static Flags ScudoFlags; // Use via getFlags().
24 void Flags::setDefaults() {
25 #define SCUDO_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
26 #include "scudo_flags.inc"
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"
37 static const char *getCompileDefinitionScudoDefaultOptions() {
38 #ifdef SCUDO_DEFAULT_OPTIONS
39 return SANITIZER_STRINGIFY(SCUDO_DEFAULT_OPTIONS);
45 static const char *getScudoDefaultOptions() {
46 return (&__scudo_default_options) ? __scudo_default_options() : "";
50 SetCommonFlagsDefaults();
53 cf.CopyFrom(*common_flags());
55 OverrideCommonFlags(cf);
57 Flags *f = getFlags();
60 FlagParser ScudoParser;
61 RegisterScudoFlags(&ScudoParser, f);
62 RegisterCommonFlags(&ScudoParser);
64 // Override from compile definition.
65 ScudoParser.ParseString(getCompileDefinitionScudoDefaultOptions());
67 // Override from user-specified string.
68 ScudoParser.ParseString(getScudoDefaultOptions());
70 // Override from environment.
71 ScudoParser.ParseStringFromEnv("SCUDO_OPTIONS");
73 InitializeCommonFlags();
75 // Sanity checks and default settings for the Quarantine parameters.
77 if (f->QuarantineSizeMb >= 0) {
78 // Backward compatible logic if QuarantineSizeMb is set.
79 if (f->QuarantineSizeKb >= 0) {
80 dieWithMessage("ERROR: please use either QuarantineSizeMb (deprecated) "
81 "or QuarantineSizeKb, but not both\n");
83 if (f->QuarantineChunksUpToSize >= 0) {
84 dieWithMessage("ERROR: QuarantineChunksUpToSize cannot be used in "
85 " conjunction with the deprecated QuarantineSizeMb option\n");
87 // If everything is in order, update QuarantineSizeKb accordingly.
88 f->QuarantineSizeKb = f->QuarantineSizeMb * 1024;
90 // Otherwise proceed with the new options.
91 if (f->QuarantineSizeKb < 0) {
92 const int DefaultQuarantineSizeKb = FIRST_32_SECOND_64(64, 256);
93 f->QuarantineSizeKb = DefaultQuarantineSizeKb;
95 if (f->QuarantineChunksUpToSize < 0) {
96 const int DefaultQuarantineChunksUpToSize = FIRST_32_SECOND_64(512, 2048);
97 f->QuarantineChunksUpToSize = DefaultQuarantineChunksUpToSize;
101 // We enforce an upper limit for the chunk quarantine threshold of 4Mb.
102 if (f->QuarantineChunksUpToSize > (4 * 1024 * 1024)) {
103 dieWithMessage("ERROR: the chunk quarantine threshold is too large\n");
106 // We enforce an upper limit for the quarantine size of 32Mb.
107 if (f->QuarantineSizeKb > (32 * 1024)) {
108 dieWithMessage("ERROR: the quarantine size is too large\n");
111 if (f->ThreadLocalQuarantineSizeKb < 0) {
112 const int DefaultThreadLocalQuarantineSizeKb = FIRST_32_SECOND_64(16, 64);
113 f->ThreadLocalQuarantineSizeKb = DefaultThreadLocalQuarantineSizeKb;
115 // And an upper limit of 8Mb for the thread quarantine cache.
116 if (f->ThreadLocalQuarantineSizeKb > (8 * 1024)) {
117 dieWithMessage("ERROR: the per thread quarantine cache size is too "
120 if (f->ThreadLocalQuarantineSizeKb == 0 && f->QuarantineSizeKb > 0) {
121 dieWithMessage("ERROR: ThreadLocalQuarantineSizeKb can be set to 0 only "
122 "when QuarantineSizeKb is set to 0\n");
130 } // namespace __scudo
132 #if !SANITIZER_SUPPORTS_WEAK_HOOKS
133 SANITIZER_INTERFACE_WEAK_DEF(const char*, __scudo_default_options, void) {