llvm-project
143 строки · 4.6 Кб
1//===-- asan_activation.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// This file is a part of AddressSanitizer, an address sanity checker.
10//
11// ASan activation/deactivation logic.
12//===----------------------------------------------------------------------===//
13
14#include "asan_activation.h"15#include "asan_allocator.h"16#include "asan_flags.h"17#include "asan_internal.h"18#include "asan_mapping.h"19#include "asan_poisoning.h"20#include "asan_stack.h"21#include "sanitizer_common/sanitizer_common.h"22#include "sanitizer_common/sanitizer_flags.h"23
24namespace __asan {25
26static struct AsanDeactivatedFlags {27AllocatorOptions allocator_options;28int malloc_context_size;29bool poison_heap;30bool coverage;31const char *coverage_dir;32
33void RegisterActivationFlags(FlagParser *parser, Flags *f, CommonFlags *cf) {34#define ASAN_ACTIVATION_FLAG(Type, Name) \35RegisterFlag(parser, #Name, "", &f->Name);36#define COMMON_ACTIVATION_FLAG(Type, Name) \37RegisterFlag(parser, #Name, "", &cf->Name);38#include "asan_activation_flags.inc"39#undef ASAN_ACTIVATION_FLAG40#undef COMMON_ACTIVATION_FLAG41
42RegisterIncludeFlags(parser, cf);43}44
45void OverrideFromActivationFlags() {46Flags f;47CommonFlags cf;48FlagParser parser;49RegisterActivationFlags(&parser, &f, &cf);50
51cf.SetDefaults();52// Copy the current activation flags.53allocator_options.CopyTo(&f, &cf);54cf.malloc_context_size = malloc_context_size;55f.poison_heap = poison_heap;56cf.coverage = coverage;57cf.coverage_dir = coverage_dir;58cf.verbosity = Verbosity();59cf.help = false; // this is activation-specific help60
61// Check if activation flags need to be overriden.62if (const char *env = GetEnv("ASAN_ACTIVATION_OPTIONS")) {63parser.ParseString(env);64}65
66InitializeCommonFlags(&cf);67
68if (Verbosity()) ReportUnrecognizedFlags();69
70if (cf.help) parser.PrintFlagDescriptions();71
72allocator_options.SetFrom(&f, &cf);73malloc_context_size = cf.malloc_context_size;74poison_heap = f.poison_heap;75coverage = cf.coverage;76coverage_dir = cf.coverage_dir;77}78
79void Print() {80Report(81"quarantine_size_mb %d, thread_local_quarantine_size_kb %d, "82"max_redzone %d, poison_heap %d, malloc_context_size %d, "83"alloc_dealloc_mismatch %d, allocator_may_return_null %d, coverage %d, "84"coverage_dir %s, allocator_release_to_os_interval_ms %d\n",85allocator_options.quarantine_size_mb,86allocator_options.thread_local_quarantine_size_kb,87allocator_options.max_redzone, poison_heap, malloc_context_size,88allocator_options.alloc_dealloc_mismatch,89allocator_options.may_return_null, coverage, coverage_dir,90allocator_options.release_to_os_interval_ms);91}92} asan_deactivated_flags;93
94static bool asan_is_deactivated;95
96void AsanDeactivate() {97CHECK(!asan_is_deactivated);98VReport(1, "Deactivating ASan\n");99
100// Stash runtime state.101GetAllocatorOptions(&asan_deactivated_flags.allocator_options);102asan_deactivated_flags.malloc_context_size = GetMallocContextSize();103asan_deactivated_flags.poison_heap = CanPoisonMemory();104asan_deactivated_flags.coverage = common_flags()->coverage;105asan_deactivated_flags.coverage_dir = common_flags()->coverage_dir;106
107// Deactivate the runtime.108SetCanPoisonMemory(false);109SetMallocContextSize(1);110
111AllocatorOptions disabled = asan_deactivated_flags.allocator_options;112disabled.quarantine_size_mb = 0;113disabled.thread_local_quarantine_size_kb = 0;114// Redzone must be at least Max(16, granularity) bytes long.115disabled.min_redzone = Max(16, (int)ASAN_SHADOW_GRANULARITY);116disabled.max_redzone = disabled.min_redzone;117disabled.alloc_dealloc_mismatch = false;118disabled.may_return_null = true;119ReInitializeAllocator(disabled);120
121asan_is_deactivated = true;122}
123
124void AsanActivate() {125if (!asan_is_deactivated) return;126VReport(1, "Activating ASan\n");127
128UpdateProcessName();129
130asan_deactivated_flags.OverrideFromActivationFlags();131
132SetCanPoisonMemory(asan_deactivated_flags.poison_heap);133SetMallocContextSize(asan_deactivated_flags.malloc_context_size);134ReInitializeAllocator(asan_deactivated_flags.allocator_options);135
136asan_is_deactivated = false;137if (Verbosity()) {138Report("Activated with flags:\n");139asan_deactivated_flags.Print();140}141}
142
143} // namespace __asan144