#include "scudo_flags.h"
#include "scudo_interface_internal.h"
#include "scudo_utils.h"
#include "sanitizer_common/sanitizer_flags.h"
#include "sanitizer_common/sanitizer_flag_parser.h"
namespace __scudo {
static Flags ScudoFlags;
void Flags::setDefaults() {
#define SCUDO_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
#include "scudo_flags.inc"
#undef SCUDO_FLAG
}
static void RegisterScudoFlags(FlagParser *parser, Flags *f) {
#define SCUDO_FLAG(Type, Name, DefaultValue, Description) \
RegisterFlag(parser, #Name, Description, &f->Name);
#include "scudo_flags.inc"
#undef SCUDO_FLAG
}
static const char *getCompileDefinitionScudoDefaultOptions() {
#ifdef SCUDO_DEFAULT_OPTIONS
return SANITIZER_STRINGIFY(SCUDO_DEFAULT_OPTIONS);
#else
return "";
#endif
}
static const char *getScudoDefaultOptions() {
return (&__scudo_default_options) ? __scudo_default_options() : "";
}
void initFlags() {
SetCommonFlagsDefaults();
{
CommonFlags cf;
cf.CopyFrom(*common_flags());
cf.exitcode = 1;
OverrideCommonFlags(cf);
}
Flags *f = getFlags();
f->setDefaults();
FlagParser ScudoParser;
RegisterScudoFlags(&ScudoParser, f);
RegisterCommonFlags(&ScudoParser);
ScudoParser.ParseString(getCompileDefinitionScudoDefaultOptions());
ScudoParser.ParseString(getScudoDefaultOptions());
ScudoParser.ParseStringFromEnv("SCUDO_OPTIONS");
InitializeCommonFlags();
if (f->QuarantineSizeMb >= 0) {
if (f->QuarantineSizeKb >= 0) {
dieWithMessage("ERROR: please use either QuarantineSizeMb (deprecated) "
"or QuarantineSizeKb, but not both\n");
}
if (f->QuarantineChunksUpToSize >= 0) {
dieWithMessage("ERROR: QuarantineChunksUpToSize cannot be used in "
" conjunction with the deprecated QuarantineSizeMb option\n");
}
f->QuarantineSizeKb = f->QuarantineSizeMb * 1024;
} else {
if (f->QuarantineSizeKb < 0) {
const int DefaultQuarantineSizeKb = FIRST_32_SECOND_64(64, 256);
f->QuarantineSizeKb = DefaultQuarantineSizeKb;
}
if (f->QuarantineChunksUpToSize < 0) {
const int DefaultQuarantineChunksUpToSize = FIRST_32_SECOND_64(512, 2048);
f->QuarantineChunksUpToSize = DefaultQuarantineChunksUpToSize;
}
}
if (f->QuarantineChunksUpToSize > (4 * 1024 * 1024)) {
dieWithMessage("ERROR: the chunk quarantine threshold is too large\n");
}
if (f->QuarantineSizeKb > (32 * 1024)) {
dieWithMessage("ERROR: the quarantine size is too large\n");
}
if (f->ThreadLocalQuarantineSizeKb < 0) {
const int DefaultThreadLocalQuarantineSizeKb = FIRST_32_SECOND_64(16, 64);
f->ThreadLocalQuarantineSizeKb = DefaultThreadLocalQuarantineSizeKb;
}
if (f->ThreadLocalQuarantineSizeKb > (8 * 1024)) {
dieWithMessage("ERROR: the per thread quarantine cache size is too "
"large\n");
}
if (f->ThreadLocalQuarantineSizeKb == 0 && f->QuarantineSizeKb > 0) {
dieWithMessage("ERROR: ThreadLocalQuarantineSizeKb can be set to 0 only "
"when QuarantineSizeKb is set to 0\n");
}
}
Flags *getFlags() {
return &ScudoFlags;
}
}
#if !SANITIZER_SUPPORTS_WEAK_HOOKS
SANITIZER_INTERFACE_WEAK_DEF(const char*, __scudo_default_options, void) {
return "";
}
#endif