2014-01-16 20:31:50 +08:00
|
|
|
//===-- asan_activation.cc --------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// 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 AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// ASan activation/deactivation logic.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "asan_activation.h"
|
2014-02-03 22:19:08 +08:00
|
|
|
#include "asan_allocator.h"
|
2014-01-16 20:31:50 +08:00
|
|
|
#include "asan_flags.h"
|
|
|
|
#include "asan_internal.h"
|
2014-12-17 08:01:02 +08:00
|
|
|
#include "asan_poisoning.h"
|
2014-12-16 09:23:03 +08:00
|
|
|
#include "asan_stack.h"
|
2014-01-16 20:31:50 +08:00
|
|
|
#include "sanitizer_common/sanitizer_flags.h"
|
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
|
|
|
static struct AsanDeactivatedFlags {
|
2014-12-20 03:35:11 +08:00
|
|
|
AllocatorOptions allocator_options;
|
2014-01-16 20:31:50 +08:00
|
|
|
int malloc_context_size;
|
2014-01-16 22:21:17 +08:00
|
|
|
bool poison_heap;
|
2014-12-26 20:32:32 +08:00
|
|
|
bool coverage;
|
|
|
|
const char *coverage_dir;
|
2014-12-20 03:16:07 +08:00
|
|
|
|
|
|
|
void OverrideFromActivationFlags() {
|
|
|
|
Flags f;
|
|
|
|
CommonFlags cf;
|
|
|
|
|
|
|
|
// Copy the current activation flags.
|
2014-12-20 04:35:53 +08:00
|
|
|
allocator_options.CopyTo(&f, &cf);
|
2014-12-20 03:16:07 +08:00
|
|
|
cf.malloc_context_size = malloc_context_size;
|
|
|
|
f.poison_heap = poison_heap;
|
|
|
|
|
|
|
|
// Check if activation flags need to be overriden.
|
|
|
|
// FIXME: Add diagnostic to check that activation flags string doesn't
|
|
|
|
// contain any other flags.
|
2014-12-25 00:58:50 +08:00
|
|
|
if (const char *env = GetEnv("ASAN_ACTIVATION_OPTIONS")) {
|
|
|
|
cf.ParseFromString(env);
|
|
|
|
ParseFlagsFromString(&f, env);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override from getprop asan.options.
|
2014-12-20 03:16:07 +08:00
|
|
|
char buf[100];
|
|
|
|
GetExtraActivationFlags(buf, sizeof(buf));
|
2014-12-20 05:40:04 +08:00
|
|
|
cf.ParseFromString(buf);
|
2014-12-20 03:16:07 +08:00
|
|
|
ParseFlagsFromString(&f, buf);
|
|
|
|
|
2014-12-20 04:35:53 +08:00
|
|
|
allocator_options.SetFrom(&f, &cf);
|
|
|
|
malloc_context_size = cf.malloc_context_size;
|
|
|
|
poison_heap = f.poison_heap;
|
2014-12-26 20:32:32 +08:00
|
|
|
coverage = cf.coverage;
|
|
|
|
coverage_dir = cf.coverage_dir;
|
2014-12-20 03:16:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Print() {
|
2014-12-26 20:32:32 +08:00
|
|
|
Report(
|
|
|
|
"quarantine_size_mb %d, max_redzone %d, poison_heap %d, "
|
|
|
|
"malloc_context_size %d, alloc_dealloc_mismatch %d, "
|
|
|
|
"allocator_may_return_null %d, coverage %d, coverage_dir %s\n",
|
|
|
|
allocator_options.quarantine_size_mb, allocator_options.max_redzone,
|
|
|
|
poison_heap, malloc_context_size,
|
|
|
|
allocator_options.alloc_dealloc_mismatch,
|
|
|
|
allocator_options.may_return_null, coverage, coverage_dir);
|
2014-12-20 03:16:07 +08:00
|
|
|
}
|
2014-01-16 20:31:50 +08:00
|
|
|
} asan_deactivated_flags;
|
|
|
|
|
|
|
|
static bool asan_is_deactivated;
|
|
|
|
|
2014-12-20 04:35:53 +08:00
|
|
|
void AsanDeactivate() {
|
|
|
|
CHECK(!asan_is_deactivated);
|
2014-01-16 21:17:13 +08:00
|
|
|
VReport(1, "Deactivating ASan\n");
|
2014-12-20 04:35:53 +08:00
|
|
|
|
|
|
|
// Stash runtime state.
|
|
|
|
GetAllocatorOptions(&asan_deactivated_flags.allocator_options);
|
|
|
|
asan_deactivated_flags.malloc_context_size = GetMallocContextSize();
|
|
|
|
asan_deactivated_flags.poison_heap = CanPoisonMemory();
|
2014-12-26 20:32:32 +08:00
|
|
|
asan_deactivated_flags.coverage = common_flags()->coverage;
|
|
|
|
asan_deactivated_flags.coverage_dir = common_flags()->coverage_dir;
|
2014-12-20 04:35:53 +08:00
|
|
|
|
|
|
|
// Deactivate the runtime.
|
|
|
|
SetCanPoisonMemory(false);
|
|
|
|
SetMallocContextSize(1);
|
2014-12-26 20:32:32 +08:00
|
|
|
ReInitializeCoverage(false, nullptr);
|
|
|
|
|
2014-12-20 04:35:53 +08:00
|
|
|
AllocatorOptions disabled = asan_deactivated_flags.allocator_options;
|
|
|
|
disabled.quarantine_size_mb = 0;
|
|
|
|
disabled.min_redzone = 16; // Redzone must be at least 16 bytes long.
|
|
|
|
disabled.max_redzone = 16;
|
|
|
|
disabled.alloc_dealloc_mismatch = false;
|
|
|
|
disabled.may_return_null = true;
|
|
|
|
ReInitializeAllocator(disabled);
|
2014-01-16 20:31:50 +08:00
|
|
|
|
|
|
|
asan_is_deactivated = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsanActivate() {
|
|
|
|
if (!asan_is_deactivated) return;
|
2014-01-16 21:17:13 +08:00
|
|
|
VReport(1, "Activating ASan\n");
|
2014-01-16 20:31:50 +08:00
|
|
|
|
2014-12-20 03:16:07 +08:00
|
|
|
asan_deactivated_flags.OverrideFromActivationFlags();
|
|
|
|
|
2014-12-17 08:01:02 +08:00
|
|
|
SetCanPoisonMemory(asan_deactivated_flags.poison_heap);
|
2014-12-16 09:23:03 +08:00
|
|
|
SetMallocContextSize(asan_deactivated_flags.malloc_context_size);
|
2014-12-26 20:32:32 +08:00
|
|
|
ReInitializeCoverage(asan_deactivated_flags.coverage,
|
|
|
|
asan_deactivated_flags.coverage_dir);
|
2014-12-20 03:35:11 +08:00
|
|
|
ReInitializeAllocator(asan_deactivated_flags.allocator_options);
|
2014-02-03 22:19:08 +08:00
|
|
|
|
2014-01-16 20:31:50 +08:00
|
|
|
asan_is_deactivated = false;
|
2014-12-20 03:16:07 +08:00
|
|
|
if (common_flags()->verbosity) {
|
|
|
|
Report("Activated with flags:\n");
|
|
|
|
asan_deactivated_flags.Print();
|
|
|
|
}
|
2014-01-16 20:31:50 +08:00
|
|
|
}
|
|
|
|
|
2014-01-16 21:17:13 +08:00
|
|
|
} // namespace __asan
|