scudo_flags.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //===-- scudo_flags.cpp -----------------------------------------*- C++ -*-===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// Hardened Allocator flag parsing logic.
  10. ///
  11. //===----------------------------------------------------------------------===//
  12. #include "scudo_flags.h"
  13. #include "scudo_interface_internal.h"
  14. #include "scudo_utils.h"
  15. #include "sanitizer_common/sanitizer_flags.h"
  16. #include "sanitizer_common/sanitizer_flag_parser.h"
  17. namespace __scudo {
  18. static Flags ScudoFlags; // Use via getFlags().
  19. void Flags::setDefaults() {
  20. #define SCUDO_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
  21. #include "scudo_flags.inc"
  22. #undef SCUDO_FLAG
  23. }
  24. static void RegisterScudoFlags(FlagParser *parser, Flags *f) {
  25. #define SCUDO_FLAG(Type, Name, DefaultValue, Description) \
  26. RegisterFlag(parser, #Name, Description, &f->Name);
  27. #include "scudo_flags.inc"
  28. #undef SCUDO_FLAG
  29. }
  30. static const char *getCompileDefinitionScudoDefaultOptions() {
  31. #ifdef SCUDO_DEFAULT_OPTIONS
  32. return SANITIZER_STRINGIFY(SCUDO_DEFAULT_OPTIONS);
  33. #else
  34. return "";
  35. #endif
  36. }
  37. static const char *getScudoDefaultOptions() {
  38. return (&__scudo_default_options) ? __scudo_default_options() : "";
  39. }
  40. void initFlags() {
  41. SetCommonFlagsDefaults();
  42. {
  43. CommonFlags cf;
  44. cf.CopyFrom(*common_flags());
  45. cf.exitcode = 1;
  46. OverrideCommonFlags(cf);
  47. }
  48. Flags *f = getFlags();
  49. f->setDefaults();
  50. FlagParser ScudoParser;
  51. RegisterScudoFlags(&ScudoParser, f);
  52. RegisterCommonFlags(&ScudoParser);
  53. // Override from compile definition.
  54. ScudoParser.ParseString(getCompileDefinitionScudoDefaultOptions());
  55. // Override from user-specified string.
  56. ScudoParser.ParseString(getScudoDefaultOptions());
  57. // Override from environment.
  58. ScudoParser.ParseStringFromEnv("SCUDO_OPTIONS");
  59. InitializeCommonFlags();
  60. // Sanity checks and default settings for the Quarantine parameters.
  61. if (f->QuarantineSizeMb >= 0) {
  62. // Backward compatible logic if QuarantineSizeMb is set.
  63. if (f->QuarantineSizeKb >= 0) {
  64. dieWithMessage("ERROR: please use either QuarantineSizeMb (deprecated) "
  65. "or QuarantineSizeKb, but not both\n");
  66. }
  67. if (f->QuarantineChunksUpToSize >= 0) {
  68. dieWithMessage("ERROR: QuarantineChunksUpToSize cannot be used in "
  69. " conjunction with the deprecated QuarantineSizeMb option\n");
  70. }
  71. // If everything is in order, update QuarantineSizeKb accordingly.
  72. f->QuarantineSizeKb = f->QuarantineSizeMb * 1024;
  73. } else {
  74. // Otherwise proceed with the new options.
  75. if (f->QuarantineSizeKb < 0) {
  76. const int DefaultQuarantineSizeKb = FIRST_32_SECOND_64(64, 256);
  77. f->QuarantineSizeKb = DefaultQuarantineSizeKb;
  78. }
  79. if (f->QuarantineChunksUpToSize < 0) {
  80. const int DefaultQuarantineChunksUpToSize = FIRST_32_SECOND_64(512, 2048);
  81. f->QuarantineChunksUpToSize = DefaultQuarantineChunksUpToSize;
  82. }
  83. }
  84. // We enforce an upper limit for the chunk quarantine threshold of 4Mb.
  85. if (f->QuarantineChunksUpToSize > (4 * 1024 * 1024)) {
  86. dieWithMessage("ERROR: the chunk quarantine threshold is too large\n");
  87. }
  88. // We enforce an upper limit for the quarantine size of 32Mb.
  89. if (f->QuarantineSizeKb > (32 * 1024)) {
  90. dieWithMessage("ERROR: the quarantine size is too large\n");
  91. }
  92. if (f->ThreadLocalQuarantineSizeKb < 0) {
  93. const int DefaultThreadLocalQuarantineSizeKb = FIRST_32_SECOND_64(16, 64);
  94. f->ThreadLocalQuarantineSizeKb = DefaultThreadLocalQuarantineSizeKb;
  95. }
  96. // And an upper limit of 8Mb for the thread quarantine cache.
  97. if (f->ThreadLocalQuarantineSizeKb > (8 * 1024)) {
  98. dieWithMessage("ERROR: the per thread quarantine cache size is too "
  99. "large\n");
  100. }
  101. if (f->ThreadLocalQuarantineSizeKb == 0 && f->QuarantineSizeKb > 0) {
  102. dieWithMessage("ERROR: ThreadLocalQuarantineSizeKb can be set to 0 only "
  103. "when QuarantineSizeKb is set to 0\n");
  104. }
  105. }
  106. Flags *getFlags() {
  107. return &ScudoFlags;
  108. }
  109. } // namespace __scudo
  110. #if !SANITIZER_SUPPORTS_WEAK_HOOKS
  111. SANITIZER_INTERFACE_WEAK_DEF(const char*, __scudo_default_options, void) {
  112. return "";
  113. }
  114. #endif