2012-07-09 21:21:39 +08:00
|
|
|
//===-- sanitizer_flags.cc ------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of ThreadSanitizer/AddressSanitizer runtime.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "sanitizer_flags.h"
|
|
|
|
|
|
|
|
#include "sanitizer_common.h"
|
|
|
|
#include "sanitizer_libc.h"
|
2014-03-20 21:49:21 +08:00
|
|
|
#include "sanitizer_list.h"
|
2012-07-09 21:21:39 +08:00
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2014-01-31 19:20:55 +08:00
|
|
|
CommonFlags common_flags_dont_use;
|
|
|
|
|
2014-03-20 21:49:21 +08:00
|
|
|
struct FlagDescription {
|
2014-03-20 20:52:52 +08:00
|
|
|
const char *name;
|
|
|
|
const char *description;
|
2014-03-20 21:49:21 +08:00
|
|
|
FlagDescription *next;
|
2014-03-20 20:52:52 +08:00
|
|
|
};
|
|
|
|
|
2014-03-20 21:49:21 +08:00
|
|
|
IntrusiveList<FlagDescription> flag_descriptions;
|
2014-03-20 20:52:52 +08:00
|
|
|
|
2013-11-27 17:54:10 +08:00
|
|
|
void SetCommonFlagsDefaults(CommonFlags *f) {
|
2013-11-12 21:59:08 +08:00
|
|
|
f->symbolize = true;
|
2013-11-27 00:24:53 +08:00
|
|
|
f->external_symbolizer_path = 0;
|
2014-02-12 16:29:42 +08:00
|
|
|
f->allow_addr2line = false;
|
2013-11-12 21:59:08 +08:00
|
|
|
f->strip_path_prefix = "";
|
|
|
|
f->fast_unwind_on_fatal = false;
|
|
|
|
f->fast_unwind_on_malloc = true;
|
|
|
|
f->handle_ioctl = false;
|
|
|
|
f->malloc_context_size = 1;
|
|
|
|
f->log_path = "stderr";
|
|
|
|
f->verbosity = 0;
|
2014-04-22 21:56:56 +08:00
|
|
|
f->detect_leaks = true;
|
2013-11-12 21:59:08 +08:00
|
|
|
f->leak_check_at_exit = true;
|
|
|
|
f->allocator_may_return_null = false;
|
2013-11-14 16:56:59 +08:00
|
|
|
f->print_summary = true;
|
2014-04-22 17:17:08 +08:00
|
|
|
f->check_printf = true;
|
2014-01-28 17:28:57 +08:00
|
|
|
// TODO(glider): tools may want to set different defaults for handle_segv.
|
|
|
|
f->handle_segv = SANITIZER_NEEDS_SEGV;
|
|
|
|
f->allow_user_segv_handler = false;
|
2014-04-24 19:55:17 +08:00
|
|
|
f->use_sigaltstack = true;
|
2014-02-14 20:08:23 +08:00
|
|
|
f->detect_deadlocks = false;
|
2014-02-14 19:41:26 +08:00
|
|
|
f->clear_shadow_mmap_threshold = 64 * 1024;
|
2014-02-26 17:06:59 +08:00
|
|
|
f->color = "auto";
|
2014-03-17 15:51:53 +08:00
|
|
|
f->legacy_pthread_cond = false;
|
2014-04-04 17:10:58 +08:00
|
|
|
f->intercept_tls_get_addr = false;
|
2014-05-12 22:27:36 +08:00
|
|
|
f->coverage = false;
|
2014-05-14 16:13:11 +08:00
|
|
|
f->full_address_space = false;
|
2013-11-12 21:59:08 +08:00
|
|
|
}
|
|
|
|
|
2013-11-27 17:54:10 +08:00
|
|
|
void ParseCommonFlagsFromString(CommonFlags *f, const char *str) {
|
2014-03-22 01:28:12 +08:00
|
|
|
ParseFlag(str, &f->symbolize, "symbolize",
|
|
|
|
"If set, use the online symbolizer from common sanitizer runtime to turn "
|
|
|
|
"virtual addresses to file/line locations.");
|
|
|
|
ParseFlag(str, &f->external_symbolizer_path, "external_symbolizer_path",
|
|
|
|
"Path to external symbolizer. If empty, the tool will search $PATH for "
|
|
|
|
"the symbolizer.");
|
|
|
|
ParseFlag(str, &f->allow_addr2line, "allow_addr2line",
|
|
|
|
"If set, allows online symbolizer to run addr2line binary to symbolize "
|
|
|
|
"stack traces (addr2line will only be used if llvm-symbolizer binary is "
|
|
|
|
"unavailable.");
|
|
|
|
ParseFlag(str, &f->strip_path_prefix, "strip_path_prefix",
|
|
|
|
"Strips this prefix from file paths in error reports.");
|
|
|
|
ParseFlag(str, &f->fast_unwind_on_fatal, "fast_unwind_on_fatal",
|
|
|
|
"If available, use the fast frame-pointer-based unwinder on fatal "
|
|
|
|
"errors.");
|
|
|
|
ParseFlag(str, &f->fast_unwind_on_malloc, "fast_unwind_on_malloc",
|
|
|
|
"If available, use the fast frame-pointer-based unwinder on "
|
|
|
|
"malloc/free.");
|
|
|
|
ParseFlag(str, &f->handle_ioctl, "handle_ioctl",
|
|
|
|
"Intercept and handle ioctl requests.");
|
|
|
|
ParseFlag(str, &f->malloc_context_size, "malloc_context_size",
|
|
|
|
"Max number of stack frames kept for each allocation/deallocation.");
|
|
|
|
ParseFlag(str, &f->log_path, "log_path",
|
|
|
|
"Write logs to \"log_path.pid\". The special values are \"stdout\" and "
|
|
|
|
"\"stderr\". The default is \"stderr\".");
|
|
|
|
ParseFlag(str, &f->verbosity, "verbosity",
|
|
|
|
"Verbosity level (0 - silent, 1 - a bit of output, 2+ - more output).");
|
|
|
|
ParseFlag(str, &f->detect_leaks, "detect_leaks",
|
|
|
|
"Enable memory leak detection.");
|
|
|
|
ParseFlag(str, &f->leak_check_at_exit, "leak_check_at_exit",
|
|
|
|
"Invoke leak checking in an atexit handler. Has no effect if "
|
|
|
|
"detect_leaks=false, or if __lsan_do_leak_check() is called before the "
|
|
|
|
"handler has a chance to run.");
|
2014-03-20 20:52:52 +08:00
|
|
|
ParseFlag(str, &f->allocator_may_return_null, "allocator_may_return_null",
|
2014-03-22 01:28:12 +08:00
|
|
|
"If false, the allocator will crash instead of returning 0 on "
|
|
|
|
"out-of-memory.");
|
|
|
|
ParseFlag(str, &f->print_summary, "print_summary",
|
|
|
|
"If false, disable printing error summaries in addition to error "
|
|
|
|
"reports.");
|
|
|
|
ParseFlag(str, &f->check_printf, "check_printf",
|
|
|
|
"Check printf arguments.");
|
|
|
|
ParseFlag(str, &f->handle_segv, "handle_segv",
|
|
|
|
"If set, registers the tool's custom SEGV handler (both SIGBUS and "
|
|
|
|
"SIGSEGV on OSX).");
|
|
|
|
ParseFlag(str, &f->allow_user_segv_handler, "allow_user_segv_handler",
|
|
|
|
"If set, allows user to register a SEGV handler even if the tool "
|
|
|
|
"registers one.");
|
|
|
|
ParseFlag(str, &f->use_sigaltstack, "use_sigaltstack",
|
|
|
|
"If set, uses alternate stack for signal handling.");
|
|
|
|
ParseFlag(str, &f->detect_deadlocks, "detect_deadlocks",
|
|
|
|
"If set, deadlock detection is enabled.");
|
2014-02-14 19:41:26 +08:00
|
|
|
ParseFlag(str, &f->clear_shadow_mmap_threshold,
|
2014-03-22 01:28:12 +08:00
|
|
|
"clear_shadow_mmap_threshold",
|
|
|
|
"Large shadow regions are zero-filled using mmap(NORESERVE) instead of "
|
|
|
|
"memset(). This is the threshold size in bytes.");
|
|
|
|
ParseFlag(str, &f->color, "color",
|
|
|
|
"Colorize reports: (always|never|auto).");
|
|
|
|
ParseFlag(str, &f->legacy_pthread_cond, "legacy_pthread_cond",
|
|
|
|
"Enables support for dynamic libraries linked with libpthread 2.2.5.");
|
2014-04-04 17:10:58 +08:00
|
|
|
ParseFlag(str, &f->intercept_tls_get_addr, "intercept_tls_get_addr",
|
|
|
|
"Intercept __tls_get_addr.");
|
2014-03-22 01:28:12 +08:00
|
|
|
ParseFlag(str, &f->help, "help", "Print the flag descriptions.");
|
2014-04-14 22:51:01 +08:00
|
|
|
ParseFlag(str, &f->mmap_limit_mb, "mmap_limit_mb",
|
|
|
|
"Limit the amount of mmap-ed memory (excluding shadow) in Mb; "
|
|
|
|
"not a user-facing flag, used mosly for testing the tools");
|
2014-05-12 22:27:36 +08:00
|
|
|
ParseFlag(str, &f->coverage, "coverage",
|
|
|
|
"If set, coverage information will be dumped at program shutdown (if the "
|
|
|
|
"coverage instrumentation was enabled at compile time).");
|
2014-05-14 16:13:11 +08:00
|
|
|
ParseFlag(str, &f->full_address_space, "full_address_space",
|
|
|
|
"Sanitize complete address space; "
|
|
|
|
"by default kernel area on 32-bit platforms will not be sanitized");
|
2013-10-11 17:58:30 +08:00
|
|
|
|
|
|
|
// Do a sanity check for certain flags.
|
|
|
|
if (f->malloc_context_size < 1)
|
|
|
|
f->malloc_context_size = 1;
|
2013-05-06 19:27:58 +08:00
|
|
|
}
|
|
|
|
|
2012-08-27 22:04:54 +08:00
|
|
|
static bool GetFlagValue(const char *env, const char *name,
|
|
|
|
const char **value, int *value_length) {
|
2012-07-09 21:21:39 +08:00
|
|
|
if (env == 0)
|
2012-08-27 22:04:54 +08:00
|
|
|
return false;
|
2013-03-28 01:59:13 +08:00
|
|
|
const char *pos = 0;
|
|
|
|
for (;;) {
|
|
|
|
pos = internal_strstr(env, name);
|
|
|
|
if (pos == 0)
|
|
|
|
return false;
|
|
|
|
if (pos != env && ((pos[-1] >= 'a' && pos[-1] <= 'z') || pos[-1] == '_')) {
|
|
|
|
// Seems to be middle of another flag name or value.
|
|
|
|
env = pos + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-07-09 21:21:39 +08:00
|
|
|
pos += internal_strlen(name);
|
2013-03-28 01:59:13 +08:00
|
|
|
const char *end;
|
2012-07-09 21:21:39 +08:00
|
|
|
if (pos[0] != '=') {
|
|
|
|
end = pos;
|
|
|
|
} else {
|
|
|
|
pos += 1;
|
|
|
|
if (pos[0] == '"') {
|
|
|
|
pos += 1;
|
|
|
|
end = internal_strchr(pos, '"');
|
|
|
|
} else if (pos[0] == '\'') {
|
|
|
|
pos += 1;
|
|
|
|
end = internal_strchr(pos, '\'');
|
|
|
|
} else {
|
2013-02-19 21:03:37 +08:00
|
|
|
// Read until the next space or colon.
|
|
|
|
end = pos + internal_strcspn(pos, " :");
|
2012-07-09 21:21:39 +08:00
|
|
|
}
|
|
|
|
if (end == 0)
|
|
|
|
end = pos + internal_strlen(pos);
|
|
|
|
}
|
2012-08-27 22:04:54 +08:00
|
|
|
*value = pos;
|
|
|
|
*value_length = end - pos;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool StartsWith(const char *flag, int flag_length, const char *value) {
|
|
|
|
if (!flag || !value)
|
|
|
|
return false;
|
|
|
|
int value_length = internal_strlen(value);
|
|
|
|
return (flag_length >= value_length) &&
|
|
|
|
(0 == internal_strncmp(flag, value, value_length));
|
2012-07-09 21:21:39 +08:00
|
|
|
}
|
|
|
|
|
2014-03-20 20:52:52 +08:00
|
|
|
static LowLevelAllocator allocator_for_flags;
|
|
|
|
|
|
|
|
// The linear scan is suboptimal, but the number of flags is relatively small.
|
|
|
|
bool FlagInDescriptionList(const char *name) {
|
2014-03-20 21:49:21 +08:00
|
|
|
IntrusiveList<FlagDescription>::Iterator it(&flag_descriptions);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
if (!internal_strcmp(it.next()->name, name)) return true;
|
2014-03-20 20:52:52 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddFlagDescription(const char *name, const char *description) {
|
|
|
|
if (FlagInDescriptionList(name)) return;
|
2014-03-24 18:47:37 +08:00
|
|
|
FlagDescription *new_description = new(allocator_for_flags) FlagDescription;
|
2014-03-20 20:52:52 +08:00
|
|
|
new_description->name = name;
|
|
|
|
new_description->description = description;
|
2014-03-20 21:49:21 +08:00
|
|
|
flag_descriptions.push_back(new_description);
|
2014-03-20 20:52:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(glider): put the descriptions inside CommonFlags.
|
|
|
|
void PrintFlagDescriptions() {
|
2014-03-20 21:49:21 +08:00
|
|
|
IntrusiveList<FlagDescription>::Iterator it(&flag_descriptions);
|
2014-03-20 20:52:52 +08:00
|
|
|
Printf("Available flags for %s:\n", SanitizerToolName);
|
2014-03-20 21:49:21 +08:00
|
|
|
while (it.hasNext()) {
|
|
|
|
FlagDescription *descr = it.next();
|
2014-03-22 01:28:12 +08:00
|
|
|
Printf("\t%s\n\t\t- %s\n", descr->name, descr->description);
|
2014-03-20 20:52:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParseFlag(const char *env, bool *flag,
|
|
|
|
const char *name, const char *descr) {
|
2012-08-27 22:04:54 +08:00
|
|
|
const char *value;
|
|
|
|
int value_length;
|
2014-03-20 20:52:52 +08:00
|
|
|
AddFlagDescription(name, descr);
|
2012-08-27 22:04:54 +08:00
|
|
|
if (!GetFlagValue(env, name, &value, &value_length))
|
2012-07-09 21:21:39 +08:00
|
|
|
return;
|
2012-08-27 22:04:54 +08:00
|
|
|
if (StartsWith(value, value_length, "0") ||
|
|
|
|
StartsWith(value, value_length, "no") ||
|
|
|
|
StartsWith(value, value_length, "false"))
|
2012-07-09 21:21:39 +08:00
|
|
|
*flag = false;
|
2012-08-27 22:04:54 +08:00
|
|
|
if (StartsWith(value, value_length, "1") ||
|
|
|
|
StartsWith(value, value_length, "yes") ||
|
|
|
|
StartsWith(value, value_length, "true"))
|
2012-07-09 21:21:39 +08:00
|
|
|
*flag = true;
|
|
|
|
}
|
|
|
|
|
2014-03-20 20:52:52 +08:00
|
|
|
void ParseFlag(const char *env, int *flag,
|
|
|
|
const char *name, const char *descr) {
|
2012-08-27 22:04:54 +08:00
|
|
|
const char *value;
|
|
|
|
int value_length;
|
2014-03-20 20:52:52 +08:00
|
|
|
AddFlagDescription(name, descr);
|
2012-08-27 22:04:54 +08:00
|
|
|
if (!GetFlagValue(env, name, &value, &value_length))
|
2012-07-09 21:21:39 +08:00
|
|
|
return;
|
2013-05-29 22:11:44 +08:00
|
|
|
*flag = static_cast<int>(internal_atoll(value));
|
2012-07-09 21:21:39 +08:00
|
|
|
}
|
|
|
|
|
2014-03-20 20:52:52 +08:00
|
|
|
void ParseFlag(const char *env, uptr *flag,
|
|
|
|
const char *name, const char *descr) {
|
2014-02-14 19:41:26 +08:00
|
|
|
const char *value;
|
|
|
|
int value_length;
|
2014-03-20 20:52:52 +08:00
|
|
|
AddFlagDescription(name, descr);
|
2014-02-14 19:41:26 +08:00
|
|
|
if (!GetFlagValue(env, name, &value, &value_length))
|
|
|
|
return;
|
|
|
|
*flag = static_cast<uptr>(internal_atoll(value));
|
|
|
|
}
|
|
|
|
|
2014-03-20 20:52:52 +08:00
|
|
|
void ParseFlag(const char *env, const char **flag,
|
|
|
|
const char *name, const char *descr) {
|
2012-08-27 22:04:54 +08:00
|
|
|
const char *value;
|
|
|
|
int value_length;
|
2014-03-20 20:52:52 +08:00
|
|
|
AddFlagDescription(name, descr);
|
2012-08-27 22:04:54 +08:00
|
|
|
if (!GetFlagValue(env, name, &value, &value_length))
|
2012-07-09 21:21:39 +08:00
|
|
|
return;
|
2012-08-27 22:51:36 +08:00
|
|
|
// Copy the flag value. Don't use locks here, as flags are parsed at
|
|
|
|
// tool startup.
|
2012-08-27 22:04:54 +08:00
|
|
|
char *value_copy = (char*)(allocator_for_flags.Allocate(value_length + 1));
|
|
|
|
internal_memcpy(value_copy, value, value_length);
|
|
|
|
value_copy[value_length] = '\0';
|
|
|
|
*flag = value_copy;
|
2012-07-09 21:21:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __sanitizer
|