Path: blob/main/contrib/llvm-project/compiler-rt/include/sanitizer/allocator_interface.h
35236 views
//===-- allocator_interface.h ---------------------------------------------===//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// Public interface header for allocator used in sanitizers (ASan/TSan/MSan).9//===----------------------------------------------------------------------===//10#ifndef SANITIZER_ALLOCATOR_INTERFACE_H11#define SANITIZER_ALLOCATOR_INTERFACE_H1213#include <sanitizer/common_interface_defs.h>14#include <stddef.h>1516#ifdef __cplusplus17extern "C" {18#endif19/* Returns the estimated number of bytes that will be reserved by allocator20for request of "size" bytes. If allocator can't allocate that much21memory, returns the maximal possible allocation size, otherwise returns22"size". */23size_t SANITIZER_CDECL __sanitizer_get_estimated_allocated_size(size_t size);2425/* Returns true if p was returned by the allocator and26is not yet freed. */27int SANITIZER_CDECL __sanitizer_get_ownership(const volatile void *p);2829/* If a pointer lies within an allocation, it will return the start address30of the allocation. Otherwise, it returns nullptr. */31const void *SANITIZER_CDECL __sanitizer_get_allocated_begin(const void *p);3233/* Returns the number of bytes reserved for the pointer p.34Requires (get_ownership(p) == true) or (p == 0). */35size_t SANITIZER_CDECL __sanitizer_get_allocated_size(const volatile void *p);3637/* Returns the number of bytes reserved for the pointer p.38Requires __sanitizer_get_allocated_begin(p) == p. */39size_t SANITIZER_CDECL40__sanitizer_get_allocated_size_fast(const volatile void *p);4142/* Number of bytes, allocated and not yet freed by the application. */43size_t SANITIZER_CDECL __sanitizer_get_current_allocated_bytes(void);4445/* Number of bytes, mmaped by the allocator to fulfill allocation requests.46Generally, for request of X bytes, allocator can reserve and add to free47lists a large number of chunks of size X to use them for future requests.48All these chunks count toward the heap size. Currently, allocator never49releases memory to OS (instead, it just puts freed chunks to free50lists). */51size_t SANITIZER_CDECL __sanitizer_get_heap_size(void);5253/* Number of bytes, mmaped by the allocator, which can be used to fulfill54allocation requests. When a user program frees memory chunk, it can first55fall into quarantine and will count toward __sanitizer_get_free_bytes()56later. */57size_t SANITIZER_CDECL __sanitizer_get_free_bytes(void);5859/* Number of bytes in unmapped pages, that are released to OS. Currently,60always returns 0. */61size_t SANITIZER_CDECL __sanitizer_get_unmapped_bytes(void);6263/* Malloc hooks that may be optionally provided by user.64- __sanitizer_malloc_hook(ptr, size) is called immediately after allocation65of "size" bytes, which returned "ptr".66- __sanitizer_free_hook(ptr) is called immediately before deallocation of67"ptr".68- __sanitizer_ignore_free_hook(ptr) is called immediately before deallocation69of "ptr", and if it returns a non-zero value, the deallocation of "ptr"70will not take place. This allows software to make free a no-op until it71calls free() again in the same pointer at a later time. Hint: read this as72"ignore the free" rather than "ignore the hook".73*/74void SANITIZER_CDECL __sanitizer_malloc_hook(const volatile void *ptr,75size_t size);76void SANITIZER_CDECL __sanitizer_free_hook(const volatile void *ptr);77int SANITIZER_CDECL __sanitizer_ignore_free_hook(const volatile void *ptr);7879/* Installs a pair of hooks for malloc/free.80Several (currently, 5) hook pairs may be installed, they are executed81in the order they were installed and after calling82__sanitizer_malloc_hook/__sanitizer_free_hook.83Unlike __sanitizer_malloc_hook/__sanitizer_free_hook these hooks can be84chained and do not rely on weak symbols working on the platform, but85require __sanitizer_install_malloc_and_free_hooks to be called at startup86and thus will not be called on malloc/free very early in the process.87Returns the number of hooks currently installed or 0 on failure.88Not thread-safe, should be called in the main thread before starting89other threads.90*/91int SANITIZER_CDECL __sanitizer_install_malloc_and_free_hooks(92void(SANITIZER_CDECL *malloc_hook)(const volatile void *, size_t),93void(SANITIZER_CDECL *free_hook)(const volatile void *));9495/* Drains allocator quarantines (calling thread's and global ones), returns96freed memory back to OS and releases other non-essential internal allocator97resources in attempt to reduce process RSS.98Currently available with ASan only.99*/100void SANITIZER_CDECL __sanitizer_purge_allocator(void);101#ifdef __cplusplus102} // extern "C"103#endif104105#endif106107108