xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_allocator.h (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===-- sanitizer_allocator.h -----------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Specialized memory allocator for ThreadSanitizer, MemorySanitizer, etc.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #ifndef SANITIZER_ALLOCATOR_H
140b57cec5SDimitry Andric #define SANITIZER_ALLOCATOR_H
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "sanitizer_common.h"
17349cc55cSDimitry Andric #include "sanitizer_flat_map.h"
180b57cec5SDimitry Andric #include "sanitizer_internal_defs.h"
190b57cec5SDimitry Andric #include "sanitizer_lfstack.h"
200b57cec5SDimitry Andric #include "sanitizer_libc.h"
210b57cec5SDimitry Andric #include "sanitizer_list.h"
220b57cec5SDimitry Andric #include "sanitizer_local_address_space_view.h"
230b57cec5SDimitry Andric #include "sanitizer_mutex.h"
240b57cec5SDimitry Andric #include "sanitizer_procmaps.h"
250b57cec5SDimitry Andric #include "sanitizer_type_traits.h"
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric namespace __sanitizer {
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric // Allows the tools to name their allocations appropriately.
300b57cec5SDimitry Andric extern const char *PrimaryAllocatorName;
310b57cec5SDimitry Andric extern const char *SecondaryAllocatorName;
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric // Since flags are immutable and allocator behavior can be changed at runtime
340b57cec5SDimitry Andric // (unit tests or ASan on Android are some examples), allocator_may_return_null
350b57cec5SDimitry Andric // flag value is cached here and can be altered later.
360b57cec5SDimitry Andric bool AllocatorMayReturnNull();
370b57cec5SDimitry Andric void SetAllocatorMayReturnNull(bool may_return_null);
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric // Returns true if allocator detected OOM condition. Can be used to avoid memory
400b57cec5SDimitry Andric // hungry operations.
410b57cec5SDimitry Andric bool IsAllocatorOutOfMemory();
420b57cec5SDimitry Andric // Should be called by a particular allocator when OOM is detected.
430b57cec5SDimitry Andric void SetAllocatorOutOfMemory();
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric void PrintHintAllocatorCannotReturnNull();
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric // Callback type for iterating over chunks.
480b57cec5SDimitry Andric typedef void (*ForEachChunkCallback)(uptr chunk, void *arg);
490b57cec5SDimitry Andric 
Rand(u32 * state)50e8d8bef9SDimitry Andric inline u32 Rand(u32 *state) {  // ANSI C linear congruential PRNG.
510b57cec5SDimitry Andric   return (*state = *state * 1103515245 + 12345) >> 16;
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric 
RandN(u32 * state,u32 n)54e8d8bef9SDimitry Andric inline u32 RandN(u32 *state, u32 n) { return Rand(state) % n; }  // [0, n)
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric template<typename T>
RandomShuffle(T * a,u32 n,u32 * rand_state)57e8d8bef9SDimitry Andric inline void RandomShuffle(T *a, u32 n, u32 *rand_state) {
580b57cec5SDimitry Andric   if (n <= 1) return;
590b57cec5SDimitry Andric   u32 state = *rand_state;
600b57cec5SDimitry Andric   for (u32 i = n - 1; i > 0; i--)
610b57cec5SDimitry Andric     Swap(a[i], a[RandN(&state, i + 1)]);
620b57cec5SDimitry Andric   *rand_state = state;
630b57cec5SDimitry Andric }
640b57cec5SDimitry Andric 
65*06c3fb27SDimitry Andric struct NoOpMapUnmapCallback {
OnMapNoOpMapUnmapCallback66*06c3fb27SDimitry Andric   void OnMap(uptr p, uptr size) const {}
OnMapSecondaryNoOpMapUnmapCallback67*06c3fb27SDimitry Andric   void OnMapSecondary(uptr p, uptr size, uptr user_begin,
68*06c3fb27SDimitry Andric                       uptr user_size) const {}
OnUnmapNoOpMapUnmapCallback69*06c3fb27SDimitry Andric   void OnUnmap(uptr p, uptr size) const {}
70*06c3fb27SDimitry Andric };
71*06c3fb27SDimitry Andric 
720b57cec5SDimitry Andric #include "sanitizer_allocator_size_class_map.h"
730b57cec5SDimitry Andric #include "sanitizer_allocator_stats.h"
740b57cec5SDimitry Andric #include "sanitizer_allocator_primary64.h"
750b57cec5SDimitry Andric #include "sanitizer_allocator_primary32.h"
760b57cec5SDimitry Andric #include "sanitizer_allocator_local_cache.h"
770b57cec5SDimitry Andric #include "sanitizer_allocator_secondary.h"
780b57cec5SDimitry Andric #include "sanitizer_allocator_combined.h"
790b57cec5SDimitry Andric 
800eae32dcSDimitry Andric bool IsRssLimitExceeded();
810eae32dcSDimitry Andric void SetRssLimitExceeded(bool limit_exceeded);
820eae32dcSDimitry Andric 
830b57cec5SDimitry Andric } // namespace __sanitizer
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric #endif // SANITIZER_ALLOCATOR_H
86