blob: 64da1d9d8ec14ccf7233bee5c7565581653554dc [file] [log] [blame]
Kostya Serebryany712fc982016-06-07 01:20:26 +00001//===-- 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
Kostya Serebryany707894b2016-08-02 22:25:38 +000020extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
21const char* __scudo_default_options();
22
Kostya Serebryany712fc982016-06-07 01:20:26 +000023namespace __scudo {
24
Kostya Serebryany707894b2016-08-02 22:25:38 +000025Flags ScudoFlags; // Use via getFlags().
Kostya Serebryany712fc982016-06-07 01:20:26 +000026
27void Flags::setDefaults() {
28#define SCUDO_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
29#include "scudo_flags.inc"
30#undef SCUDO_FLAG
31}
32
33static 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"
37#undef SCUDO_FLAG
38}
39
Kostya Serebryany707894b2016-08-02 22:25:38 +000040static const char *callGetScudoDefaultOptions() {
41 return (&__scudo_default_options) ? __scudo_default_options() : "";
42}
43
Kostya Serebryany712fc982016-06-07 01:20:26 +000044void initFlags() {
45 SetCommonFlagsDefaults();
46 {
47 CommonFlags cf;
48 cf.CopyFrom(*common_flags());
49 cf.exitcode = 1;
50 OverrideCommonFlags(cf);
51 }
52 Flags *f = getFlags();
53 f->setDefaults();
54
Kostya Serebryany707894b2016-08-02 22:25:38 +000055 FlagParser ScudoParser;
56 RegisterScudoFlags(&ScudoParser, f);
57 RegisterCommonFlags(&ScudoParser);
Kostya Serebryany712fc982016-06-07 01:20:26 +000058
Kostya Serebryany707894b2016-08-02 22:25:38 +000059 // Override from user-specified string.
60 const char *ScudoDefaultOptions = callGetScudoDefaultOptions();
61 ScudoParser.ParseString(ScudoDefaultOptions);
62
63 // Override from environment.
64 ScudoParser.ParseString(GetEnv("SCUDO_OPTIONS"));
Kostya Serebryany712fc982016-06-07 01:20:26 +000065
66 InitializeCommonFlags();
67
68 // Sanity checks and default settings for the Quarantine parameters.
69
70 if (f->QuarantineSizeMb < 0) {
Kostya Kortchinsky8d6257b2017-02-03 20:49:42 +000071 const int DefaultQuarantineSizeMb = FIRST_32_SECOND_64(16, 64);
Kostya Serebryany712fc982016-06-07 01:20:26 +000072 f->QuarantineSizeMb = DefaultQuarantineSizeMb;
73 }
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");
77 }
78 if (f->ThreadLocalQuarantineSizeKb < 0) {
Kostya Kortchinsky8d6257b2017-02-03 20:49:42 +000079 const int DefaultThreadLocalQuarantineSizeKb =
80 FIRST_32_SECOND_64(256, 1024);
Kostya Serebryany712fc982016-06-07 01:20:26 +000081 f->ThreadLocalQuarantineSizeKb = DefaultThreadLocalQuarantineSizeKb;
82 }
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 "
86 "large\n");
87 }
Kostya Kortchinsky8d6257b2017-02-03 20:49:42 +000088 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");
91 }
Kostya Serebryany712fc982016-06-07 01:20:26 +000092}
93
94Flags *getFlags() {
Kostya Serebryany707894b2016-08-02 22:25:38 +000095 return &ScudoFlags;
Kostya Serebryany712fc982016-06-07 01:20:26 +000096}
97
Kostya Kortchinsky1148dc52016-11-30 17:32:20 +000098} // namespace __scudo