Skip to content

Commit 1d05591

Browse files
rmacnak-googlecommit-bot@chromium.org
authored andcommitted
[vm] Produce clearer error messages when operator new fails.
TEST=bots Bug: #43642 Bug: #43862 Change-Id: I598a26a56762c141a1aef972f7d32f2c9594b244 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/171802 Commit-Queue: Ryan Macnak <rmacnak@google.com> Reviewed-by: Alexander Aprelev <aam@google.com> Reviewed-by: Martin Kustermann <kustermann@google.com>
1 parent 8263779 commit 1d05591

File tree

11 files changed

+49
-25
lines changed

11 files changed

+49
-25
lines changed

runtime/platform/allocation.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef RUNTIME_PLATFORM_ALLOCATION_H_
66
#define RUNTIME_PLATFORM_ALLOCATION_H_
77

8+
#include "platform/address_sanitizer.h"
89
#include "platform/assert.h"
910

1011
namespace dart {
@@ -31,6 +32,36 @@ class AllStatic {
3132
DISALLOW_IMPLICIT_CONSTRUCTORS(AllStatic);
3233
};
3334

35+
class MallocAllocated {
36+
public:
37+
MallocAllocated() {}
38+
39+
// Intercept operator new to produce clearer error messages when we run out
40+
// of memory. Don't do this when running under ASAN so it can continue to
41+
// check malloc/new/new[] are paired with free/delete/delete[] respectively.
42+
#if !defined(USING_ADDRESS_SANITIZER)
43+
void* operator new(size_t size) {
44+
void* result = ::malloc(size);
45+
if (result == nullptr) {
46+
OUT_OF_MEMORY();
47+
}
48+
return result;
49+
}
50+
51+
void* operator new[](size_t size) {
52+
void* result = ::malloc(size);
53+
if (result == nullptr) {
54+
OUT_OF_MEMORY();
55+
}
56+
return result;
57+
}
58+
59+
void operator delete(void* pointer) { ::free(pointer); }
60+
61+
void operator delete[](void* pointer) { ::free(pointer); }
62+
#endif
63+
};
64+
3465
} // namespace dart
3566

3667
#endif // RUNTIME_PLATFORM_ALLOCATION_H_

runtime/platform/growable_array.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,13 @@ class Malloc : public AllStatic {
254254
}
255255
};
256256

257-
class EmptyBase {};
258-
259257
template <typename T>
260-
class MallocGrowableArray : public BaseGrowableArray<T, EmptyBase, Malloc> {
258+
class MallocGrowableArray
259+
: public BaseGrowableArray<T, MallocAllocated, Malloc> {
261260
public:
262261
explicit MallocGrowableArray(intptr_t initial_capacity)
263-
: BaseGrowableArray<T, EmptyBase, Malloc>(initial_capacity, NULL) {}
264-
MallocGrowableArray() : BaseGrowableArray<T, EmptyBase, Malloc>(NULL) {}
262+
: BaseGrowableArray<T, MallocAllocated, Malloc>(initial_capacity, NULL) {}
263+
MallocGrowableArray() : BaseGrowableArray<T, MallocAllocated, Malloc>(NULL) {}
265264
};
266265

267266
} // namespace dart

runtime/platform/hashmap.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,6 @@ SimpleHashMap::Entry* SimpleHashMap::Probe(void* key, uint32_t hash) {
157157
void SimpleHashMap::Initialize(uint32_t capacity) {
158158
ASSERT(dart::Utils::IsPowerOfTwo(capacity));
159159
map_ = new Entry[capacity];
160-
if (map_ == NULL) {
161-
OUT_OF_MEMORY();
162-
}
163160
capacity_ = capacity;
164161
occupancy_ = 0;
165162
}

runtime/vm/allocation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ class ZoneAllocated {
5454
ZoneAllocated() {}
5555

5656
// Implicitly allocate the object in the current zone.
57-
void* operator new(uword size);
57+
void* operator new(size_t size);
5858

5959
// Allocate the object in the given zone, which must be the current zone.
60-
void* operator new(uword size, Zone* zone);
60+
void* operator new(size_t size, Zone* zone);
6161

6262
// Ideally, the delete operator should be protected instead of
6363
// public, but unfortunately the compiler sometimes synthesizes

runtime/vm/deopt_instructions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class TimelineEvent;
2727

2828
// Holds all data relevant for execution of deoptimization instructions.
2929
// Structure is allocated in C-heap.
30-
class DeoptContext {
30+
class DeoptContext : public MallocAllocated {
3131
public:
3232
enum DestFrameOptions {
3333
kDestIsOriginalFrame, // Replace the original frame with deopt frame.

runtime/vm/handles.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class Handles {
116116
// is allocated from the chunk until we run out space in the chunk,
117117
// at this point another chunk is allocated. These chunks are chained
118118
// together.
119-
class HandlesBlock {
119+
class HandlesBlock : public MallocAllocated {
120120
public:
121121
explicit HandlesBlock(HandlesBlock* next)
122122
: next_handle_slot_(0), next_block_(next) {}

runtime/vm/handles_impl.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,6 @@ void Handles<kHandleSizeInWords, kHandlesPerChunk, kOffsetOfRawPtr>::
157157
}
158158
if (scoped_blocks_->next_block() == NULL) {
159159
HandlesBlock* block = new HandlesBlock(NULL);
160-
if (block == NULL) {
161-
OUT_OF_MEMORY();
162-
}
163160
scoped_blocks_->set_next_block(block);
164161
}
165162
scoped_blocks_ = scoped_blocks_->next_block();
@@ -207,9 +204,6 @@ void Handles<kHandleSizeInWords, kHandlesPerChunk, kOffsetOfRawPtr>::
207204
CountScopedHandles());
208205
}
209206
zone_blocks_ = new HandlesBlock(zone_blocks_);
210-
if (zone_blocks_ == NULL) {
211-
OUT_OF_MEMORY();
212-
}
213207
}
214208

215209
#if defined(DEBUG)

runtime/vm/hash_map.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,15 +423,17 @@ class DirectChainedHashMap
423423

424424
template <typename KeyValueTrait>
425425
class MallocDirectChainedHashMap
426-
: public BaseDirectChainedHashMap<KeyValueTrait, EmptyBase, Malloc> {
426+
: public BaseDirectChainedHashMap<KeyValueTrait, MallocAllocated, Malloc> {
427427
public:
428428
MallocDirectChainedHashMap()
429-
: BaseDirectChainedHashMap<KeyValueTrait, EmptyBase, Malloc>(NULL) {}
429+
: BaseDirectChainedHashMap<KeyValueTrait, MallocAllocated, Malloc>(NULL) {
430+
}
430431

431432
// The only use of the copy constructor seems to be in hash_map_test.cc.
432433
// Not disallowing it for now just in case there are other users.
433434
MallocDirectChainedHashMap(const MallocDirectChainedHashMap& other)
434-
: BaseDirectChainedHashMap<KeyValueTrait, EmptyBase, Malloc>(other) {}
435+
: BaseDirectChainedHashMap<KeyValueTrait, MallocAllocated, Malloc>(
436+
other) {}
435437

436438
private:
437439
void operator=(const MallocDirectChainedHashMap& other) = delete;

runtime/vm/heap/pointer_block.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ObjectPointerVisitor;
1818

1919
// A set of ObjectPtr. Must be emptied before destruction (using Pop/Reset).
2020
template <int Size>
21-
class PointerBlock {
21+
class PointerBlock : public MallocAllocated {
2222
public:
2323
enum { kSize = Size };
2424

runtime/vm/port_set.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "include/dart_api.h"
99

10+
#include "platform/allocation.h"
1011
#include "platform/globals.h"
1112
#include "platform/utils.h"
1213

@@ -18,7 +19,7 @@ class PortSet {
1819
static constexpr Dart_Port kFreePort = static_cast<Dart_Port>(0);
1920
static constexpr Dart_Port kDeletedPort = static_cast<Dart_Port>(3);
2021

21-
struct Entry {
22+
struct Entry : public MallocAllocated {
2223
Entry() : port(kFreePort) {}
2324

2425
// Free entries have set this to 0.

0 commit comments

Comments
 (0)