2018-08-30 06:23:34 +08:00
|
|
|
//===-- hwasan_allocator.h --------------------------------------*- C++ -*-===//
|
2017-12-09 09:31:51 +08:00
|
|
|
//
|
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
|
2017-12-09 09:31:51 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of HWAddressSanitizer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef HWASAN_ALLOCATOR_H
|
|
|
|
#define HWASAN_ALLOCATOR_H
|
|
|
|
|
2021-03-25 21:34:25 +08:00
|
|
|
#include "hwasan.h"
|
|
|
|
#include "hwasan_interface_internal.h"
|
2021-06-16 01:45:06 +08:00
|
|
|
#include "hwasan_mapping.h"
|
2021-03-24 02:57:12 +08:00
|
|
|
#include "hwasan_poisoning.h"
|
2018-08-31 14:08:48 +08:00
|
|
|
#include "sanitizer_common/sanitizer_allocator.h"
|
|
|
|
#include "sanitizer_common/sanitizer_allocator_checks.h"
|
|
|
|
#include "sanitizer_common/sanitizer_allocator_interface.h"
|
|
|
|
#include "sanitizer_common/sanitizer_allocator_report.h"
|
2017-12-09 09:31:51 +08:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
2018-08-30 05:07:07 +08:00
|
|
|
#include "sanitizer_common/sanitizer_ring_buffer.h"
|
2018-08-31 14:08:48 +08:00
|
|
|
|
|
|
|
#if !defined(__aarch64__) && !defined(__x86_64__)
|
|
|
|
#error Unsupported platform
|
|
|
|
#endif
|
2017-12-09 09:31:51 +08:00
|
|
|
|
|
|
|
namespace __hwasan {
|
|
|
|
|
2018-08-31 14:08:48 +08:00
|
|
|
struct Metadata {
|
2020-10-20 07:38:03 +08:00
|
|
|
u32 requested_size_low;
|
|
|
|
u32 requested_size_high : 31;
|
|
|
|
u32 right_aligned : 1;
|
2018-08-31 14:08:48 +08:00
|
|
|
u32 alloc_context_id;
|
2020-10-20 07:38:03 +08:00
|
|
|
u64 get_requested_size() {
|
|
|
|
return (static_cast<u64>(requested_size_high) << 32) + requested_size_low;
|
|
|
|
}
|
|
|
|
void set_requested_size(u64 size) {
|
|
|
|
requested_size_low = size & ((1ul << 32) - 1);
|
|
|
|
requested_size_high = size >> 32;
|
|
|
|
}
|
2018-08-31 14:08:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct HwasanMapUnmapCallback {
|
2018-09-08 08:11:12 +08:00
|
|
|
void OnMap(uptr p, uptr size) const { UpdateMemoryUsage(); }
|
2018-08-31 14:08:48 +08:00
|
|
|
void OnUnmap(uptr p, uptr size) const {
|
|
|
|
// We are about to unmap a chunk of user memory.
|
|
|
|
// It can return as user-requested mmap() or another thread stack.
|
|
|
|
// Make it accessible with zero-tagged pointer.
|
|
|
|
TagMemory(p, size, 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-20 07:38:03 +08:00
|
|
|
static const uptr kMaxAllowedMallocSize = 1UL << 40; // 1T
|
2018-08-31 14:08:48 +08:00
|
|
|
|
2019-01-04 07:19:02 +08:00
|
|
|
struct AP64 {
|
|
|
|
static const uptr kSpaceBeg = ~0ULL;
|
2021-03-25 21:34:25 +08:00
|
|
|
|
2021-05-15 00:52:47 +08:00
|
|
|
#if defined(HWASAN_ALIASING_MODE)
|
2021-03-25 21:34:25 +08:00
|
|
|
static const uptr kSpaceSize = 1ULL << kAddressTagShift;
|
|
|
|
#else
|
2019-01-04 07:19:02 +08:00
|
|
|
static const uptr kSpaceSize = 0x2000000000ULL;
|
2021-03-25 21:34:25 +08:00
|
|
|
#endif
|
2018-08-31 14:08:48 +08:00
|
|
|
static const uptr kMetadataSize = sizeof(Metadata);
|
2019-01-04 07:19:02 +08:00
|
|
|
typedef __sanitizer::VeryDenseSizeClassMap SizeClassMap;
|
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 = LocalAddressSpaceView;
|
2018-08-31 14:08:48 +08:00
|
|
|
typedef HwasanMapUnmapCallback MapUnmapCallback;
|
|
|
|
static const uptr kFlags = 0;
|
|
|
|
};
|
2019-01-04 07:19:02 +08:00
|
|
|
typedef SizeClassAllocator64<AP64> PrimaryAllocator;
|
2019-05-02 03:41:54 +08:00
|
|
|
typedef CombinedAllocator<PrimaryAllocator> Allocator;
|
2019-05-02 03:30:49 +08:00
|
|
|
typedef Allocator::AllocatorCache AllocatorCache;
|
2018-08-31 14:08:48 +08:00
|
|
|
|
2018-09-06 07:22:38 +08:00
|
|
|
void AllocatorSwallowThreadLocalCache(AllocatorCache *cache);
|
2017-12-09 09:31:51 +08:00
|
|
|
|
|
|
|
class HwasanChunkView {
|
|
|
|
public:
|
|
|
|
HwasanChunkView() : block_(0), metadata_(nullptr) {}
|
|
|
|
HwasanChunkView(uptr block, Metadata *metadata)
|
|
|
|
: block_(block), metadata_(metadata) {}
|
|
|
|
bool IsAllocated() const; // Checks if the memory is currently allocated
|
|
|
|
uptr Beg() const; // First byte of user memory
|
|
|
|
uptr End() const; // Last byte of user memory
|
|
|
|
uptr UsedSize() const; // Size requested by the user
|
2018-10-11 06:24:44 +08:00
|
|
|
uptr ActualSize() const; // Size allocated by the allocator.
|
2017-12-09 09:31:51 +08:00
|
|
|
u32 GetAllocStackId() const;
|
2018-10-11 06:24:44 +08:00
|
|
|
bool FromSmallHeap() const;
|
2017-12-09 09:31:51 +08:00
|
|
|
private:
|
|
|
|
uptr block_;
|
|
|
|
Metadata *const metadata_;
|
|
|
|
};
|
|
|
|
|
|
|
|
HwasanChunkView FindHeapChunkByAddress(uptr address);
|
|
|
|
|
2018-08-30 05:07:07 +08:00
|
|
|
// Information about one (de)allocation that happened in the past.
|
|
|
|
// These are recorded in a thread-local ring buffer.
|
2018-09-06 07:52:31 +08:00
|
|
|
// TODO: this is currently 24 bytes (20 bytes + alignment).
|
|
|
|
// Compress it to 16 bytes or extend it to be more useful.
|
2018-08-30 05:07:07 +08:00
|
|
|
struct HeapAllocationRecord {
|
|
|
|
uptr tagged_addr;
|
2018-08-31 06:11:56 +08:00
|
|
|
u32 alloc_context_id;
|
2018-08-30 05:07:07 +08:00
|
|
|
u32 free_context_id;
|
|
|
|
u32 requested_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef RingBuffer<HeapAllocationRecord> HeapAllocationsRingBuffer;
|
|
|
|
|
2018-09-07 06:08:41 +08:00
|
|
|
void GetAllocatorStats(AllocatorStatCounters s);
|
2018-08-30 05:07:07 +08:00
|
|
|
|
2021-03-24 02:57:12 +08:00
|
|
|
inline bool InTaggableRegion(uptr addr) {
|
2021-05-15 00:52:47 +08:00
|
|
|
#if defined(HWASAN_ALIASING_MODE)
|
2021-03-25 21:34:25 +08:00
|
|
|
// Aliases are mapped next to shadow so that the upper bits match the shadow
|
|
|
|
// base.
|
|
|
|
return (addr >> kTaggableRegionCheckShift) ==
|
2021-06-16 01:45:06 +08:00
|
|
|
(GetShadowOffset() >> kTaggableRegionCheckShift);
|
2021-03-25 21:34:25 +08:00
|
|
|
#endif
|
2021-03-24 02:57:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-09 09:31:51 +08:00
|
|
|
} // namespace __hwasan
|
|
|
|
|
|
|
|
#endif // HWASAN_ALLOCATOR_H
|