2012-12-04 15:54:41 +08:00
|
|
|
//===-- sanitizer_allocator.h -----------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Specialized memory allocator for ThreadSanitizer, MemorySanitizer, etc.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef SANITIZER_ALLOCATOR_H
|
|
|
|
#define SANITIZER_ALLOCATOR_H
|
|
|
|
|
|
|
|
#include "sanitizer_internal_defs.h"
|
|
|
|
#include "sanitizer_common.h"
|
|
|
|
#include "sanitizer_libc.h"
|
|
|
|
#include "sanitizer_list.h"
|
|
|
|
#include "sanitizer_mutex.h"
|
2013-01-14 16:23:34 +08:00
|
|
|
#include "sanitizer_lfstack.h"
|
2016-07-22 05:38:40 +08:00
|
|
|
#include "sanitizer_procmaps.h"
|
2012-12-04 15:54:41 +08:00
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2018-04-14 03:21:27 +08:00
|
|
|
// Allows the tools to name their allocations appropriately.
|
|
|
|
extern const char *PrimaryAllocatorName;
|
|
|
|
extern const char *SecondaryAllocatorName;
|
|
|
|
|
2017-06-21 05:23:02 +08:00
|
|
|
// Since flags are immutable and allocator behavior can be changed at runtime
|
|
|
|
// (unit tests or ASan on Android are some examples), allocator_may_return_null
|
|
|
|
// flag value is cached here and can be altered later.
|
|
|
|
bool AllocatorMayReturnNull();
|
|
|
|
void SetAllocatorMayReturnNull(bool may_return_null);
|
2016-09-30 07:00:54 +08:00
|
|
|
|
2017-06-21 05:23:02 +08:00
|
|
|
// Returns true if allocator detected OOM condition. Can be used to avoid memory
|
2018-06-21 01:10:33 +08:00
|
|
|
// hungry operations.
|
2017-06-21 05:23:02 +08:00
|
|
|
bool IsAllocatorOutOfMemory();
|
2018-06-21 01:10:33 +08:00
|
|
|
// Should be called by a particular allocator when OOM is detected.
|
2018-03-29 02:22:40 +08:00
|
|
|
void SetAllocatorOutOfMemory();
|
2016-09-30 07:00:54 +08:00
|
|
|
|
2018-06-21 01:10:33 +08:00
|
|
|
void PrintHintAllocatorCannotReturnNull();
|
|
|
|
|
2012-12-12 22:32:18 +08:00
|
|
|
// Allocators call these callbacks on mmap/munmap.
|
|
|
|
struct NoOpMapUnmapCallback {
|
|
|
|
void OnMap(uptr p, uptr size) const { }
|
|
|
|
void OnUnmap(uptr p, uptr size) const { }
|
|
|
|
};
|
|
|
|
|
2013-06-24 16:34:50 +08:00
|
|
|
// Callback type for iterating over chunks.
|
|
|
|
typedef void (*ForEachChunkCallback)(uptr chunk, void *arg);
|
|
|
|
|
[sanitizer] Random shuffling of chunks for the 32-bit Primary Allocator
Summary:
The 64-bit primary has had random shuffling of chunks for a while, this
implements it for the 32-bit primary. Scudo is currently the only user of
`kRandomShuffleChunks`.
This change consists of a few modifications:
- move the random shuffling functions out of the 64-bit primary to
`sanitizer_common.h`. Alternatively I could move them to
`sanitizer_allocator.h` as they are only used in the allocator, I don't feel
strongly either way;
- small change in the 64-bit primary to make the `rand_state` initialization
`UNLIKELY`;
- addition of a `rand_state` in the 32-bit primary's `SizeClassInfo` and
shuffling of chunks when populating the free list.
- enabling the `random_shuffle.cpp` test on platforms using the 32-bit primary
for Scudo.
Some comments on why the shuffling is done that way. Initially I just
implemented a `Shuffle` function in the `TransferBatch` which was simpler but I
came to realize this wasn't good enough: for chunks of 10000 bytes for example,
with a `CompactSizeClassMap`, a batch holds only 1 chunk, meaning shuffling the
batch has no effect, while a region is usually 1MB, eg: 104 chunks of that size.
So I decided to "stage" the newly gathered chunks in a temporary array that
would be shuffled prior to placing the chunks in batches.
The result is looping twice through n_chunks even if shuffling is not enabled,
but I didn't notice any significant significant performance impact.
Reviewers: alekseyshl
Reviewed By: alekseyshl
Subscribers: srhines, llvm-commits, kubamracek
Differential Revision: https://reviews.llvm.org/D39244
llvm-svn: 316596
2017-10-26 01:24:56 +08:00
|
|
|
INLINE u32 Rand(u32 *state) { // ANSI C linear congruential PRNG.
|
|
|
|
return (*state = *state * 1103515245 + 12345) >> 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
INLINE u32 RandN(u32 *state, u32 n) { return Rand(state) % n; } // [0, n)
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
INLINE void RandomShuffle(T *a, u32 n, u32 *rand_state) {
|
|
|
|
if (n <= 1) return;
|
2018-05-16 23:13:26 +08:00
|
|
|
u32 state = *rand_state;
|
[sanitizer] Random shuffling of chunks for the 32-bit Primary Allocator
Summary:
The 64-bit primary has had random shuffling of chunks for a while, this
implements it for the 32-bit primary. Scudo is currently the only user of
`kRandomShuffleChunks`.
This change consists of a few modifications:
- move the random shuffling functions out of the 64-bit primary to
`sanitizer_common.h`. Alternatively I could move them to
`sanitizer_allocator.h` as they are only used in the allocator, I don't feel
strongly either way;
- small change in the 64-bit primary to make the `rand_state` initialization
`UNLIKELY`;
- addition of a `rand_state` in the 32-bit primary's `SizeClassInfo` and
shuffling of chunks when populating the free list.
- enabling the `random_shuffle.cpp` test on platforms using the 32-bit primary
for Scudo.
Some comments on why the shuffling is done that way. Initially I just
implemented a `Shuffle` function in the `TransferBatch` which was simpler but I
came to realize this wasn't good enough: for chunks of 10000 bytes for example,
with a `CompactSizeClassMap`, a batch holds only 1 chunk, meaning shuffling the
batch has no effect, while a region is usually 1MB, eg: 104 chunks of that size.
So I decided to "stage" the newly gathered chunks in a temporary array that
would be shuffled prior to placing the chunks in batches.
The result is looping twice through n_chunks even if shuffling is not enabled,
but I didn't notice any significant significant performance impact.
Reviewers: alekseyshl
Reviewed By: alekseyshl
Subscribers: srhines, llvm-commits, kubamracek
Differential Revision: https://reviews.llvm.org/D39244
llvm-svn: 316596
2017-10-26 01:24:56 +08:00
|
|
|
for (u32 i = n - 1; i > 0; i--)
|
2018-05-16 23:13:26 +08:00
|
|
|
Swap(a[i], a[RandN(&state, i + 1)]);
|
|
|
|
*rand_state = state;
|
[sanitizer] Random shuffling of chunks for the 32-bit Primary Allocator
Summary:
The 64-bit primary has had random shuffling of chunks for a while, this
implements it for the 32-bit primary. Scudo is currently the only user of
`kRandomShuffleChunks`.
This change consists of a few modifications:
- move the random shuffling functions out of the 64-bit primary to
`sanitizer_common.h`. Alternatively I could move them to
`sanitizer_allocator.h` as they are only used in the allocator, I don't feel
strongly either way;
- small change in the 64-bit primary to make the `rand_state` initialization
`UNLIKELY`;
- addition of a `rand_state` in the 32-bit primary's `SizeClassInfo` and
shuffling of chunks when populating the free list.
- enabling the `random_shuffle.cpp` test on platforms using the 32-bit primary
for Scudo.
Some comments on why the shuffling is done that way. Initially I just
implemented a `Shuffle` function in the `TransferBatch` which was simpler but I
came to realize this wasn't good enough: for chunks of 10000 bytes for example,
with a `CompactSizeClassMap`, a batch holds only 1 chunk, meaning shuffling the
batch has no effect, while a region is usually 1MB, eg: 104 chunks of that size.
So I decided to "stage" the newly gathered chunks in a temporary array that
would be shuffled prior to placing the chunks in batches.
The result is looping twice through n_chunks even if shuffling is not enabled,
but I didn't notice any significant significant performance impact.
Reviewers: alekseyshl
Reviewed By: alekseyshl
Subscribers: srhines, llvm-commits, kubamracek
Differential Revision: https://reviews.llvm.org/D39244
llvm-svn: 316596
2017-10-26 01:24:56 +08:00
|
|
|
}
|
|
|
|
|
2016-07-21 06:06:41 +08:00
|
|
|
#include "sanitizer_allocator_size_class_map.h"
|
|
|
|
#include "sanitizer_allocator_stats.h"
|
|
|
|
#include "sanitizer_allocator_primary64.h"
|
|
|
|
#include "sanitizer_allocator_bytemap.h"
|
|
|
|
#include "sanitizer_allocator_primary32.h"
|
|
|
|
#include "sanitizer_allocator_local_cache.h"
|
|
|
|
#include "sanitizer_allocator_secondary.h"
|
|
|
|
#include "sanitizer_allocator_combined.h"
|
|
|
|
|
2015-09-30 02:23:36 +08:00
|
|
|
} // namespace __sanitizer
|
2012-12-04 15:54:41 +08:00
|
|
|
|
2015-09-30 02:23:36 +08:00
|
|
|
#endif // SANITIZER_ALLOCATOR_H
|