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-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.
|
|
|
|
char buf[100];
|
|
|
|
GetExtraActivationFlags(buf, sizeof(buf));
|
|
|
|
ParseCommonFlagsFromString(&cf, buf);
|
|
|
|
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-20 03:16:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Print() {
|
2014-12-20 03:35:11 +08:00
|
|
|
Report("quarantine_size_mb %d, max_redzone %d, poison_heap %d, "
|
2014-12-20 03:16:07 +08:00
|
|
|
"malloc_context_size %d, alloc_dealloc_mismatch %d, "
|
|
|
|
"allocator_may_return_null %d\n",
|
2014-12-20 03:35:11 +08:00
|
|
|
allocator_options.quarantine_size_mb, allocator_options.max_redzone,
|
|
|
|
poison_heap, malloc_context_size,
|
|
|
|
allocator_options.alloc_dealloc_mismatch,
|
|
|
|
allocator_options.may_return_null);
|
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();
|
|
|
|
|
|
|
|
// Deactivate the runtime.
|
|
|
|
SetCanPoisonMemory(false);
|
|
|
|
SetMallocContextSize(1);
|
|
|
|
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-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
|