Skip to content

Commit b3ccfa1

Browse files
committed
[hwasan] Increase max allocation size to 1Tb.
2Gb is unreasonably low on devices with 12Gb RAM and more. Differential Revision: https://reviews.llvm.org/D89750
1 parent 2dc7e0c commit b3ccfa1

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

compiler-rt/lib/hwasan/hwasan_allocator.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ enum RightAlignMode {
4242
static ALIGNED(16) u8 tail_magic[kShadowAlignment - 1];
4343

4444
bool HwasanChunkView::IsAllocated() const {
45-
return metadata_ && metadata_->alloc_context_id && metadata_->requested_size;
45+
return metadata_ && metadata_->alloc_context_id &&
46+
metadata_->get_requested_size();
4647
}
4748

4849
// Aligns the 'addr' right to the granule boundary.
@@ -54,14 +55,14 @@ static uptr AlignRight(uptr addr, uptr requested_size) {
5455

5556
uptr HwasanChunkView::Beg() const {
5657
if (metadata_ && metadata_->right_aligned)
57-
return AlignRight(block_, metadata_->requested_size);
58+
return AlignRight(block_, metadata_->get_requested_size());
5859
return block_;
5960
}
6061
uptr HwasanChunkView::End() const {
6162
return Beg() + UsedSize();
6263
}
6364
uptr HwasanChunkView::UsedSize() const {
64-
return metadata_->requested_size;
65+
return metadata_->get_requested_size();
6566
}
6667
u32 HwasanChunkView::GetAllocStackId() const {
6768
return metadata_->alloc_context_id;
@@ -129,7 +130,7 @@ static void *HwasanAllocate(StackTrace *stack, uptr orig_size, uptr alignment,
129130
}
130131
Metadata *meta =
131132
reinterpret_cast<Metadata *>(allocator.GetMetaData(allocated));
132-
meta->requested_size = static_cast<u32>(orig_size);
133+
meta->set_requested_size(orig_size);
133134
meta->alloc_context_id = StackDepotPut(*stack);
134135
meta->right_aligned = false;
135136
if (zeroise) {
@@ -191,7 +192,7 @@ static void HwasanDeallocate(StackTrace *stack, void *tagged_ptr) {
191192
RoundDownTo(reinterpret_cast<uptr>(untagged_ptr), kShadowAlignment));
192193
Metadata *meta =
193194
reinterpret_cast<Metadata *>(allocator.GetMetaData(aligned_ptr));
194-
uptr orig_size = meta->requested_size;
195+
uptr orig_size = meta->get_requested_size();
195196
u32 free_context_id = StackDepotPut(*stack);
196197
u32 alloc_context_id = meta->alloc_context_id;
197198

@@ -208,7 +209,7 @@ static void HwasanDeallocate(StackTrace *stack, void *tagged_ptr) {
208209
orig_size, tail_magic);
209210
}
210211

211-
meta->requested_size = 0;
212+
meta->set_requested_size(0);
212213
meta->alloc_context_id = 0;
213214
// This memory will not be reused by anyone else, so we are free to keep it
214215
// poisoned.
@@ -245,8 +246,9 @@ static void *HwasanReallocate(StackTrace *stack, void *tagged_ptr_old,
245246
void *untagged_ptr_old = UntagPtr(tagged_ptr_old);
246247
Metadata *meta =
247248
reinterpret_cast<Metadata *>(allocator.GetMetaData(untagged_ptr_old));
248-
internal_memcpy(UntagPtr(tagged_ptr_new), untagged_ptr_old,
249-
Min(new_size, static_cast<uptr>(meta->requested_size)));
249+
internal_memcpy(
250+
UntagPtr(tagged_ptr_new), untagged_ptr_old,
251+
Min(new_size, static_cast<uptr>(meta->get_requested_size())));
250252
HwasanDeallocate(stack, tagged_ptr_old);
251253
}
252254
return tagged_ptr_new;
@@ -282,7 +284,7 @@ static uptr AllocationSize(const void *tagged_ptr) {
282284
} else {
283285
if (beg != untagged_ptr) return 0;
284286
}
285-
return b->requested_size;
287+
return b->get_requested_size();
286288
}
287289

288290
void *hwasan_malloc(uptr size, StackTrace *stack) {

compiler-rt/lib/hwasan/hwasan_allocator.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,17 @@
2828
namespace __hwasan {
2929

3030
struct Metadata {
31-
u32 requested_size : 31; // sizes are < 2G.
32-
u32 right_aligned : 1;
31+
u32 requested_size_low;
32+
u32 requested_size_high : 31;
33+
u32 right_aligned : 1;
3334
u32 alloc_context_id;
35+
u64 get_requested_size() {
36+
return (static_cast<u64>(requested_size_high) << 32) + requested_size_low;
37+
}
38+
void set_requested_size(u64 size) {
39+
requested_size_low = size & ((1ul << 32) - 1);
40+
requested_size_high = size >> 32;
41+
}
3442
};
3543

3644
struct HwasanMapUnmapCallback {
@@ -43,7 +51,7 @@ struct HwasanMapUnmapCallback {
4351
}
4452
};
4553

46-
static const uptr kMaxAllowedMallocSize = 2UL << 30; // 2G
54+
static const uptr kMaxAllowedMallocSize = 1UL << 40; // 1T
4755

4856
struct AP64 {
4957
static const uptr kSpaceBeg = ~0ULL;

compiler-rt/test/hwasan/TestCases/allocator_returns_null.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ int main(int argc, char **argv) {
5555
const char *action = argv[1];
5656
untag_fprintf(stderr, "%s:\n", action);
5757

58-
static const size_t kMaxAllowedMallocSizePlusOne = (2UL << 30) + 1;
58+
static const size_t kMaxAllowedMallocSizePlusOne = (1UL << 40) + 1;
5959

6060
void *x = nullptr;
6161
if (!untag_strcmp(action, "malloc")) {

compiler-rt/test/hwasan/TestCases/sizes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int main(int argc, char **argv) {
4444
static const size_t kChunkHeaderSize = 16;
4545

4646
size_t MallocSize = test_size_max ? std::numeric_limits<size_t>::max()
47-
: kMaxAllowedMallocSize;
47+
: (kMaxAllowedMallocSize + 1);
4848

4949
if (!untag_strcmp(argv[1], "malloc")) {
5050
void *p = malloc(MallocSize);

0 commit comments

Comments
 (0)