Path: blob/main/contrib/llvm-project/compiler-rt/lib/asan/asan_activation.cpp
35233 views
//===-- asan_activation.cpp -------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of AddressSanitizer, an address sanity checker.9//10// ASan activation/deactivation logic.11//===----------------------------------------------------------------------===//1213#include "asan_activation.h"14#include "asan_allocator.h"15#include "asan_flags.h"16#include "asan_internal.h"17#include "asan_mapping.h"18#include "asan_poisoning.h"19#include "asan_stack.h"20#include "sanitizer_common/sanitizer_common.h"21#include "sanitizer_common/sanitizer_flags.h"2223namespace __asan {2425static struct AsanDeactivatedFlags {26AllocatorOptions allocator_options;27int malloc_context_size;28bool poison_heap;29bool coverage;30const char *coverage_dir;3132void RegisterActivationFlags(FlagParser *parser, Flags *f, CommonFlags *cf) {33#define ASAN_ACTIVATION_FLAG(Type, Name) \34RegisterFlag(parser, #Name, "", &f->Name);35#define COMMON_ACTIVATION_FLAG(Type, Name) \36RegisterFlag(parser, #Name, "", &cf->Name);37#include "asan_activation_flags.inc"38#undef ASAN_ACTIVATION_FLAG39#undef COMMON_ACTIVATION_FLAG4041RegisterIncludeFlags(parser, cf);42}4344void OverrideFromActivationFlags() {45Flags f;46CommonFlags cf;47FlagParser parser;48RegisterActivationFlags(&parser, &f, &cf);4950cf.SetDefaults();51// Copy the current activation flags.52allocator_options.CopyTo(&f, &cf);53cf.malloc_context_size = malloc_context_size;54f.poison_heap = poison_heap;55cf.coverage = coverage;56cf.coverage_dir = coverage_dir;57cf.verbosity = Verbosity();58cf.help = false; // this is activation-specific help5960// Check if activation flags need to be overriden.61if (const char *env = GetEnv("ASAN_ACTIVATION_OPTIONS")) {62parser.ParseString(env);63}6465InitializeCommonFlags(&cf);6667if (Verbosity()) ReportUnrecognizedFlags();6869if (cf.help) parser.PrintFlagDescriptions();7071allocator_options.SetFrom(&f, &cf);72malloc_context_size = cf.malloc_context_size;73poison_heap = f.poison_heap;74coverage = cf.coverage;75coverage_dir = cf.coverage_dir;76}7778void Print() {79Report(80"quarantine_size_mb %d, thread_local_quarantine_size_kb %d, "81"max_redzone %d, poison_heap %d, malloc_context_size %d, "82"alloc_dealloc_mismatch %d, allocator_may_return_null %d, coverage %d, "83"coverage_dir %s, allocator_release_to_os_interval_ms %d\n",84allocator_options.quarantine_size_mb,85allocator_options.thread_local_quarantine_size_kb,86allocator_options.max_redzone, poison_heap, malloc_context_size,87allocator_options.alloc_dealloc_mismatch,88allocator_options.may_return_null, coverage, coverage_dir,89allocator_options.release_to_os_interval_ms);90}91} asan_deactivated_flags;9293static bool asan_is_deactivated;9495void AsanDeactivate() {96CHECK(!asan_is_deactivated);97VReport(1, "Deactivating ASan\n");9899// Stash runtime state.100GetAllocatorOptions(&asan_deactivated_flags.allocator_options);101asan_deactivated_flags.malloc_context_size = GetMallocContextSize();102asan_deactivated_flags.poison_heap = CanPoisonMemory();103asan_deactivated_flags.coverage = common_flags()->coverage;104asan_deactivated_flags.coverage_dir = common_flags()->coverage_dir;105106// Deactivate the runtime.107SetCanPoisonMemory(false);108SetMallocContextSize(1);109110AllocatorOptions disabled = asan_deactivated_flags.allocator_options;111disabled.quarantine_size_mb = 0;112disabled.thread_local_quarantine_size_kb = 0;113// Redzone must be at least Max(16, granularity) bytes long.114disabled.min_redzone = Max(16, (int)ASAN_SHADOW_GRANULARITY);115disabled.max_redzone = disabled.min_redzone;116disabled.alloc_dealloc_mismatch = false;117disabled.may_return_null = true;118ReInitializeAllocator(disabled);119120asan_is_deactivated = true;121}122123void AsanActivate() {124if (!asan_is_deactivated) return;125VReport(1, "Activating ASan\n");126127UpdateProcessName();128129asan_deactivated_flags.OverrideFromActivationFlags();130131SetCanPoisonMemory(asan_deactivated_flags.poison_heap);132SetMallocContextSize(asan_deactivated_flags.malloc_context_size);133ReInitializeAllocator(asan_deactivated_flags.allocator_options);134135asan_is_deactivated = false;136if (Verbosity()) {137Report("Activated with flags:\n");138asan_deactivated_flags.Print();139}140}141142} // namespace __asan143144145