2018-08-30 06:23:34 +08:00
|
|
|
//===-- hwasan_allocator.h --------------------------------------*- C++ -*-===//
|
2017-12-09 09:31:51 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of HWAddressSanitizer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef HWASAN_ALLOCATOR_H
|
|
|
|
#define HWASAN_ALLOCATOR_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
|
|
|
#include "hwasan_poisoning.h"
|
|
|
|
|
|
|
|
#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 {
|
|
|
|
u32 requested_size; // sizes are < 4G.
|
|
|
|
u32 alloc_context_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct HwasanMapUnmapCallback {
|
|
|
|
void OnMap(uptr p, uptr size) const {}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uptr kMaxAllowedMallocSize = 2UL << 30; // 2G
|
|
|
|
static const uptr kRegionSizeLog = 20;
|
|
|
|
static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
|
|
|
|
typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
|
|
|
|
|
|
|
|
struct AP32 {
|
|
|
|
static const uptr kSpaceBeg = 0;
|
|
|
|
static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
|
|
|
|
static const uptr kMetadataSize = sizeof(Metadata);
|
|
|
|
typedef __sanitizer::CompactSizeClassMap SizeClassMap;
|
|
|
|
static const uptr kRegionSizeLog = __hwasan::kRegionSizeLog;
|
|
|
|
typedef __hwasan::ByteMap ByteMap;
|
|
|
|
typedef HwasanMapUnmapCallback MapUnmapCallback;
|
|
|
|
static const uptr kFlags = 0;
|
|
|
|
};
|
|
|
|
typedef SizeClassAllocator32<AP32> PrimaryAllocator;
|
|
|
|
typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
|
|
|
|
typedef LargeMmapAllocator<HwasanMapUnmapCallback> SecondaryAllocator;
|
|
|
|
typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
|
|
|
|
SecondaryAllocator> Allocator;
|
|
|
|
|
2017-12-09 09:31:51 +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
|
|
|
|
u32 GetAllocStackId() const;
|
|
|
|
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
|
|
|
|
2017-12-09 09:31:51 +08:00
|
|
|
} // namespace __hwasan
|
|
|
|
|
|
|
|
#endif // HWASAN_ALLOCATOR_H
|