2013-05-20 18:54:00 +08:00
|
|
|
//=-- lsan_allocator.h ----------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-05-20 18:54:00 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of LeakSanitizer.
|
|
|
|
// Allocator for standalone LSan.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LSAN_ALLOCATOR_H
|
|
|
|
#define LSAN_ALLOCATOR_H
|
|
|
|
|
2017-03-27 22:07:50 +08:00
|
|
|
#include "sanitizer_common/sanitizer_allocator.h"
|
2013-05-20 18:54:00 +08:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
#include "sanitizer_common/sanitizer_internal_defs.h"
|
2017-03-27 22:07:50 +08:00
|
|
|
#include "lsan_common.h"
|
2013-05-20 18:54:00 +08:00
|
|
|
|
|
|
|
namespace __lsan {
|
|
|
|
|
|
|
|
void *Allocate(const StackTrace &stack, uptr size, uptr alignment,
|
|
|
|
bool cleared);
|
|
|
|
void Deallocate(void *p);
|
|
|
|
void *Reallocate(const StackTrace &stack, void *p, uptr new_size,
|
|
|
|
uptr alignment);
|
2014-08-26 22:28:28 +08:00
|
|
|
uptr GetMallocUsableSize(const void *p);
|
2013-05-20 18:54:00 +08:00
|
|
|
|
|
|
|
template<typename Callable>
|
|
|
|
void ForEachChunk(const Callable &callback);
|
|
|
|
|
|
|
|
void GetAllocatorCacheRange(uptr *begin, uptr *end);
|
|
|
|
void AllocatorThreadFinish();
|
|
|
|
void InitializeAllocator();
|
|
|
|
|
2017-04-12 04:05:02 +08:00
|
|
|
const bool kAlwaysClearMemory = true;
|
|
|
|
|
2017-03-27 22:07:50 +08:00
|
|
|
struct ChunkMetadata {
|
|
|
|
u8 allocated : 8; // Must be first.
|
|
|
|
ChunkTag tag : 2;
|
|
|
|
#if SANITIZER_WORDSIZE == 64
|
|
|
|
uptr requested_size : 54;
|
|
|
|
#else
|
|
|
|
uptr requested_size : 32;
|
|
|
|
uptr padding : 22;
|
|
|
|
#endif
|
|
|
|
u32 stack_trace_id;
|
|
|
|
};
|
|
|
|
|
2017-04-11 22:58:26 +08:00
|
|
|
#if defined(__mips64) || defined(__aarch64__) || defined(__i386__) || \
|
|
|
|
defined(__arm__)
|
2017-03-27 22:07:50 +08:00
|
|
|
static const uptr kRegionSizeLog = 20;
|
|
|
|
static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
|
Introduce `AddressSpaceView` template parameter to `SizeClassAllocator32`, `FlatByteMap`, and `TwoLevelByteMap`.
Summary:
This is a follow up patch to r346956 for the `SizeClassAllocator32`
allocator.
This patch makes `AddressSpaceView` a template parameter both to the
`ByteMap` implementations (but makes `LocalAddressSpaceView` the
default), some `AP32` implementations and is used in `SizeClassAllocator32`.
The actual changes to `ByteMap` implementations and
`SizeClassAllocator32` are very simple. However the patch is large
because it requires changing all the `AP32` definitions, and users of
those definitions.
For ASan and LSan we make `AP32` and `ByteMap` templateds type that take
a single `AddressSpaceView` argument. This has been done because we will
instantiate the allocator with a type that isn't `LocalAddressSpaceView`
in the future patches. For the allocators used in the other sanitizers
(i.e. HWAsan, MSan, Scudo, and TSan) use of `LocalAddressSpaceView` is
hard coded because we do not intend to instantiate the allocators with
any other type.
In the cases where untemplated types have become templated on a single
`AddressSpaceView` parameter (e.g. `PrimaryAllocator`) their name has
been changed to have a `ASVT` suffix (Address Space View Type) to
indicate they are templated. The only exception to this are the `AP32`
types due to the desire to keep the type name as short as possible.
In order to check that template is instantiated in the correct a way a
`static_assert(...)` has been added that checks that the
`AddressSpaceView` type used by `Params::ByteMap::AddressSpaceView` matches
the `Params::AddressSpaceView`. This uses the new `sanitizer_type_traits.h`
header.
rdar://problem/45284065
Reviewers: kcc, dvyukov, vitalybuka, cryptoad, eugenis, kubamracek, george.karpenkov
Subscribers: mgorny, llvm-commits, #sanitizers
Differential Revision: https://reviews.llvm.org/D54904
llvm-svn: 349138
2018-12-14 17:03:18 +08:00
|
|
|
template <typename AddressSpaceView>
|
|
|
|
using ByteMapASVT =
|
|
|
|
TwoLevelByteMap<(kNumRegions >> 12), 1 << 12, AddressSpaceView>;
|
2017-05-15 22:47:19 +08:00
|
|
|
|
Introduce `AddressSpaceView` template parameter to `SizeClassAllocator32`, `FlatByteMap`, and `TwoLevelByteMap`.
Summary:
This is a follow up patch to r346956 for the `SizeClassAllocator32`
allocator.
This patch makes `AddressSpaceView` a template parameter both to the
`ByteMap` implementations (but makes `LocalAddressSpaceView` the
default), some `AP32` implementations and is used in `SizeClassAllocator32`.
The actual changes to `ByteMap` implementations and
`SizeClassAllocator32` are very simple. However the patch is large
because it requires changing all the `AP32` definitions, and users of
those definitions.
For ASan and LSan we make `AP32` and `ByteMap` templateds type that take
a single `AddressSpaceView` argument. This has been done because we will
instantiate the allocator with a type that isn't `LocalAddressSpaceView`
in the future patches. For the allocators used in the other sanitizers
(i.e. HWAsan, MSan, Scudo, and TSan) use of `LocalAddressSpaceView` is
hard coded because we do not intend to instantiate the allocators with
any other type.
In the cases where untemplated types have become templated on a single
`AddressSpaceView` parameter (e.g. `PrimaryAllocator`) their name has
been changed to have a `ASVT` suffix (Address Space View Type) to
indicate they are templated. The only exception to this are the `AP32`
types due to the desire to keep the type name as short as possible.
In order to check that template is instantiated in the correct a way a
`static_assert(...)` has been added that checks that the
`AddressSpaceView` type used by `Params::ByteMap::AddressSpaceView` matches
the `Params::AddressSpaceView`. This uses the new `sanitizer_type_traits.h`
header.
rdar://problem/45284065
Reviewers: kcc, dvyukov, vitalybuka, cryptoad, eugenis, kubamracek, george.karpenkov
Subscribers: mgorny, llvm-commits, #sanitizers
Differential Revision: https://reviews.llvm.org/D54904
llvm-svn: 349138
2018-12-14 17:03:18 +08:00
|
|
|
template <typename AddressSpaceViewTy>
|
2017-05-15 22:47:19 +08:00
|
|
|
struct AP32 {
|
|
|
|
static const uptr kSpaceBeg = 0;
|
|
|
|
static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
|
|
|
|
static const uptr kMetadataSize = sizeof(ChunkMetadata);
|
|
|
|
typedef __sanitizer::CompactSizeClassMap SizeClassMap;
|
|
|
|
static const uptr kRegionSizeLog = __lsan::kRegionSizeLog;
|
Introduce `AddressSpaceView` template parameter to `SizeClassAllocator32`, `FlatByteMap`, and `TwoLevelByteMap`.
Summary:
This is a follow up patch to r346956 for the `SizeClassAllocator32`
allocator.
This patch makes `AddressSpaceView` a template parameter both to the
`ByteMap` implementations (but makes `LocalAddressSpaceView` the
default), some `AP32` implementations and is used in `SizeClassAllocator32`.
The actual changes to `ByteMap` implementations and
`SizeClassAllocator32` are very simple. However the patch is large
because it requires changing all the `AP32` definitions, and users of
those definitions.
For ASan and LSan we make `AP32` and `ByteMap` templateds type that take
a single `AddressSpaceView` argument. This has been done because we will
instantiate the allocator with a type that isn't `LocalAddressSpaceView`
in the future patches. For the allocators used in the other sanitizers
(i.e. HWAsan, MSan, Scudo, and TSan) use of `LocalAddressSpaceView` is
hard coded because we do not intend to instantiate the allocators with
any other type.
In the cases where untemplated types have become templated on a single
`AddressSpaceView` parameter (e.g. `PrimaryAllocator`) their name has
been changed to have a `ASVT` suffix (Address Space View Type) to
indicate they are templated. The only exception to this are the `AP32`
types due to the desire to keep the type name as short as possible.
In order to check that template is instantiated in the correct a way a
`static_assert(...)` has been added that checks that the
`AddressSpaceView` type used by `Params::ByteMap::AddressSpaceView` matches
the `Params::AddressSpaceView`. This uses the new `sanitizer_type_traits.h`
header.
rdar://problem/45284065
Reviewers: kcc, dvyukov, vitalybuka, cryptoad, eugenis, kubamracek, george.karpenkov
Subscribers: mgorny, llvm-commits, #sanitizers
Differential Revision: https://reviews.llvm.org/D54904
llvm-svn: 349138
2018-12-14 17:03:18 +08:00
|
|
|
using AddressSpaceView = AddressSpaceViewTy;
|
|
|
|
using ByteMap = __lsan::ByteMapASVT<AddressSpaceView>;
|
2017-05-15 22:47:19 +08:00
|
|
|
typedef NoOpMapUnmapCallback MapUnmapCallback;
|
|
|
|
static const uptr kFlags = 0;
|
|
|
|
};
|
Introduce `AddressSpaceView` template parameter to `SizeClassAllocator32`, `FlatByteMap`, and `TwoLevelByteMap`.
Summary:
This is a follow up patch to r346956 for the `SizeClassAllocator32`
allocator.
This patch makes `AddressSpaceView` a template parameter both to the
`ByteMap` implementations (but makes `LocalAddressSpaceView` the
default), some `AP32` implementations and is used in `SizeClassAllocator32`.
The actual changes to `ByteMap` implementations and
`SizeClassAllocator32` are very simple. However the patch is large
because it requires changing all the `AP32` definitions, and users of
those definitions.
For ASan and LSan we make `AP32` and `ByteMap` templateds type that take
a single `AddressSpaceView` argument. This has been done because we will
instantiate the allocator with a type that isn't `LocalAddressSpaceView`
in the future patches. For the allocators used in the other sanitizers
(i.e. HWAsan, MSan, Scudo, and TSan) use of `LocalAddressSpaceView` is
hard coded because we do not intend to instantiate the allocators with
any other type.
In the cases where untemplated types have become templated on a single
`AddressSpaceView` parameter (e.g. `PrimaryAllocator`) their name has
been changed to have a `ASVT` suffix (Address Space View Type) to
indicate they are templated. The only exception to this are the `AP32`
types due to the desire to keep the type name as short as possible.
In order to check that template is instantiated in the correct a way a
`static_assert(...)` has been added that checks that the
`AddressSpaceView` type used by `Params::ByteMap::AddressSpaceView` matches
the `Params::AddressSpaceView`. This uses the new `sanitizer_type_traits.h`
header.
rdar://problem/45284065
Reviewers: kcc, dvyukov, vitalybuka, cryptoad, eugenis, kubamracek, george.karpenkov
Subscribers: mgorny, llvm-commits, #sanitizers
Differential Revision: https://reviews.llvm.org/D54904
llvm-svn: 349138
2018-12-14 17:03:18 +08:00
|
|
|
template <typename AddressSpaceView>
|
|
|
|
using PrimaryAllocatorASVT = SizeClassAllocator32<AP32<AddressSpaceView>>;
|
|
|
|
using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;
|
2017-04-22 05:59:53 +08:00
|
|
|
#elif defined(__x86_64__) || defined(__powerpc64__)
|
2017-10-26 09:22:48 +08:00
|
|
|
# if defined(__powerpc64__)
|
|
|
|
const uptr kAllocatorSpace = 0xa0000000000ULL;
|
|
|
|
const uptr kAllocatorSize = 0x20000000000ULL; // 2T.
|
|
|
|
# else
|
|
|
|
const uptr kAllocatorSpace = 0x600000000000ULL;
|
|
|
|
const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
|
|
|
|
# endif
|
2018-12-22 05:09:31 +08:00
|
|
|
template <typename AddressSpaceViewTy>
|
2017-03-27 22:07:50 +08:00
|
|
|
struct AP64 { // Allocator64 parameters. Deliberately using a short name.
|
2017-10-26 09:22:48 +08:00
|
|
|
static const uptr kSpaceBeg = kAllocatorSpace;
|
|
|
|
static const uptr kSpaceSize = kAllocatorSize;
|
2017-03-27 22:07:50 +08:00
|
|
|
static const uptr kMetadataSize = sizeof(ChunkMetadata);
|
|
|
|
typedef DefaultSizeClassMap SizeClassMap;
|
|
|
|
typedef NoOpMapUnmapCallback MapUnmapCallback;
|
|
|
|
static const uptr kFlags = 0;
|
2018-12-22 05:09:31 +08:00
|
|
|
using AddressSpaceView = AddressSpaceViewTy;
|
2017-03-27 22:07:50 +08:00
|
|
|
};
|
|
|
|
|
2018-12-22 05:09:31 +08:00
|
|
|
template <typename AddressSpaceView>
|
|
|
|
using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>;
|
|
|
|
using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;
|
2017-03-27 22:07:50 +08:00
|
|
|
#endif
|
2018-12-22 05:22:27 +08:00
|
|
|
|
|
|
|
template <typename AddressSpaceView>
|
|
|
|
using AllocatorCacheASVT =
|
|
|
|
SizeClassAllocatorLocalCache<PrimaryAllocatorASVT<AddressSpaceView>>;
|
|
|
|
using AllocatorCache = AllocatorCacheASVT<LocalAddressSpaceView>;
|
|
|
|
|
|
|
|
template <typename AddressSpaceView>
|
|
|
|
using SecondaryAllocatorASVT =
|
|
|
|
LargeMmapAllocator<NoOpMapUnmapCallback, DefaultLargeMmapAllocatorPtrArray,
|
|
|
|
AddressSpaceView>;
|
|
|
|
|
|
|
|
template <typename AddressSpaceView>
|
|
|
|
using AllocatorASVT =
|
|
|
|
CombinedAllocator<PrimaryAllocatorASVT<AddressSpaceView>,
|
|
|
|
AllocatorCacheASVT<AddressSpaceView>,
|
2019-01-21 00:57:24 +08:00
|
|
|
SecondaryAllocatorASVT<AddressSpaceView>,
|
|
|
|
AddressSpaceView>;
|
2018-12-22 05:22:27 +08:00
|
|
|
using Allocator = AllocatorASVT<LocalAddressSpaceView>;
|
2017-03-27 22:07:50 +08:00
|
|
|
|
|
|
|
AllocatorCache *GetAllocatorCache();
|
2017-04-12 04:05:02 +08:00
|
|
|
|
2018-03-13 05:59:06 +08:00
|
|
|
int lsan_posix_memalign(void **memptr, uptr alignment, uptr size,
|
|
|
|
const StackTrace &stack);
|
2018-06-09 04:40:35 +08:00
|
|
|
void *lsan_aligned_alloc(uptr alignment, uptr size, const StackTrace &stack);
|
2017-04-12 04:05:02 +08:00
|
|
|
void *lsan_memalign(uptr alignment, uptr size, const StackTrace &stack);
|
|
|
|
void *lsan_malloc(uptr size, const StackTrace &stack);
|
|
|
|
void lsan_free(void *p);
|
|
|
|
void *lsan_realloc(void *p, uptr size, const StackTrace &stack);
|
|
|
|
void *lsan_calloc(uptr nmemb, uptr size, const StackTrace &stack);
|
|
|
|
void *lsan_valloc(uptr size, const StackTrace &stack);
|
2018-06-12 01:33:53 +08:00
|
|
|
void *lsan_pvalloc(uptr size, const StackTrace &stack);
|
2017-04-12 04:05:02 +08:00
|
|
|
uptr lsan_mz_size(const void *p);
|
|
|
|
|
2013-05-20 18:54:00 +08:00
|
|
|
} // namespace __lsan
|
|
|
|
|
|
|
|
#endif // LSAN_ALLOCATOR_H
|