Path: blob/main/contrib/llvm-project/compiler-rt/lib/gwp_asan/crash_handler.h
35236 views
//===-- crash_handler.h -----------------------------------------*- 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//===----------------------------------------------------------------------===//78// This file contains interface functions that can be called by an in-process or9// out-of-process crash handler after the process has terminated. Functions in10// this interface are never thread safe. For an in-process crash handler, the11// handler should call GuardedPoolAllocator::disable() to stop any other threads12// from retrieving new GWP-ASan allocations, which may corrupt the metadata.13#ifndef GWP_ASAN_INTERFACE_H_14#define GWP_ASAN_INTERFACE_H_1516#include "gwp_asan/common.h"1718#ifdef __cplusplus19extern "C" {20#endif2122// When a process crashes, there are three possible outcomes:23// 1. The crash is unrelated to GWP-ASan - in which case this function returns24// false.25// 2. The crash is internally detected within GWP-ASan itself (e.g. a26// double-free bug is caught in GuardedPoolAllocator::deallocate(), and27// GWP-ASan will terminate the process). In this case - this function28// returns true.29// 3. The crash is caused by a memory error at `AccessPtr` that's caught by the30// system, but GWP-ASan is responsible for the allocation. In this case -31// the function also returns true.32// This function takes an optional `AccessPtr` parameter. If the pointer that33// was attempted to be accessed is available, you should provide it here. In the34// case of some internally-detected errors, the crash may manifest as an abort35// or trap may or may not have an associated pointer. In these cases, the36// pointer can be obtained by a call to __gwp_asan_get_internal_crash_address.37bool __gwp_asan_error_is_mine(const gwp_asan::AllocatorState *State,38uintptr_t ErrorPtr = 0u);3940// Diagnose and return the type of error that occurred at `ErrorPtr`. If41// `ErrorPtr` is unrelated to GWP-ASan, or if the error type cannot be deduced,42// this function returns Error::UNKNOWN.43gwp_asan::Error44__gwp_asan_diagnose_error(const gwp_asan::AllocatorState *State,45const gwp_asan::AllocationMetadata *Metadata,46uintptr_t ErrorPtr);4748// This function, provided the fault address from the signal handler, returns49// the following values:50// 1. If the crash was caused by an internally-detected error (invalid free,51// double free), this function returns the pointer that was used for the52// internally-detected bad operation (i.e. the pointer given to free()).53// 2. For externally-detected crashes (use-after-free, buffer-overflow), this54// function returns zero.55// 3. If GWP-ASan wasn't responsible for the crash at all, this function also56// returns zero.57uintptr_t58__gwp_asan_get_internal_crash_address(const gwp_asan::AllocatorState *State,59uintptr_t ErrorPtr);6061// Returns a pointer to the metadata for the allocation that's responsible for62// the crash. This metadata should not be dereferenced directly due to API63// compatibility issues, but should be instead passed to functions below for64// information retrieval. Returns nullptr if there is no metadata available for65// this crash.66const gwp_asan::AllocationMetadata *67__gwp_asan_get_metadata(const gwp_asan::AllocatorState *State,68const gwp_asan::AllocationMetadata *Metadata,69uintptr_t ErrorPtr);7071// +---------------------------------------------------------------------------+72// | Error Information Functions |73// +---------------------------------------------------------------------------+74// Functions below return information about the type of error that was caught by75// GWP-ASan, or information about the allocation that caused the error. These76// functions generally take an `AllocationMeta` argument, which should be77// retrieved via. __gwp_asan_get_metadata.7879// Returns the start of the allocation whose metadata is in `AllocationMeta`.80uintptr_t __gwp_asan_get_allocation_address(81const gwp_asan::AllocationMetadata *AllocationMeta);8283// Returns the size of the allocation whose metadata is in `AllocationMeta`84size_t __gwp_asan_get_allocation_size(85const gwp_asan::AllocationMetadata *AllocationMeta);8687// Returns the Thread ID that allocated the memory that caused the error at88// `ErrorPtr`. This function may not be called if __gwp_asan_has_metadata()89// returns false.90uint64_t __gwp_asan_get_allocation_thread_id(91const gwp_asan::AllocationMetadata *AllocationMeta);9293// Retrieve the allocation trace for the allocation whose metadata is in94// `AllocationMeta`, and place it into the provided `Buffer` that has at least95// `BufferLen` elements. This function returns the number of frames that would96// have been written into `Buffer` if the space was available (i.e. however many97// frames were stored by GWP-ASan). A return value greater than `BufferLen`98// indicates that the trace was truncated when storing to `Buffer`.99size_t __gwp_asan_get_allocation_trace(100const gwp_asan::AllocationMetadata *AllocationMeta, uintptr_t *Buffer,101size_t BufferLen);102103// Returns whether the allocation whose metadata is in `AllocationMeta` has been104// deallocated. This function may not be called if __gwp_asan_has_metadata()105// returns false.106bool __gwp_asan_is_deallocated(107const gwp_asan::AllocationMetadata *AllocationMeta);108109// Returns the Thread ID that deallocated the memory whose metadata is in110// `AllocationMeta`. This function may not be called if111// __gwp_asan_is_deallocated() returns false.112uint64_t __gwp_asan_get_deallocation_thread_id(113const gwp_asan::AllocationMetadata *AllocationMeta);114115// Retrieve the deallocation trace for the allocation whose metadata is in116// `AllocationMeta`, and place it into the provided `Buffer` that has at least117// `BufferLen` elements. This function returns the number of frames that would118// have been written into `Buffer` if the space was available (i.e. however many119// frames were stored by GWP-ASan). A return value greater than `BufferLen`120// indicates that the trace was truncated when storing to `Buffer`. This121// function may not be called if __gwp_asan_is_deallocated() returns false.122size_t __gwp_asan_get_deallocation_trace(123const gwp_asan::AllocationMetadata *AllocationMeta, uintptr_t *Buffer,124size_t BufferLen);125126#ifdef __cplusplus127} // extern "C"128#endif129130#endif // GWP_ASAN_INTERFACE_H_131132133