[vm] Produce clearer error messages for malloc/realloc failures.

TEST=bots
Bug: https://github.com/dart-lang/sdk/issues/43642
Change-Id: I7bc2b3b5231413967f9b1f608c957326ff6c6aaa
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/171680
Commit-Queue: Ryan Macnak <rmacnak@google.com>
Reviewed-by: Alexander Aprelev <aam@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
This commit is contained in:
Ryan Macnak 2020-11-13 22:10:54 +00:00 committed by commit-bot@chromium.org
parent 00fe513296
commit b9c86f07dc
23 changed files with 52 additions and 41 deletions

View file

@ -294,9 +294,6 @@ void DartUtils::ReadFile(uint8_t** data, intptr_t* len, void* stream) {
}
*len = static_cast<intptr_t>(file_len);
*data = reinterpret_cast<uint8_t*>(malloc(*len));
if (*data == NULL) {
OUT_OF_MEMORY();
}
if (!file_stream->ReadFully(*data, *len)) {
free(*data);
*data = NULL;

View file

@ -312,9 +312,6 @@ class KernelIRNode {
}
*p_bytes = reinterpret_cast<uint8_t*>(malloc(size));
if (*p_bytes == nullptr) {
OUT_OF_MEMORY();
}
uint8_t* p = *p_bytes;
KernelIRNode* node = head;
while (node != nullptr) {

View file

@ -4,6 +4,7 @@
#include "bin/gzip.h"
#include "platform/allocation.h"
#include "platform/assert.h"
#include "platform/globals.h"
#include "zlib/zlib.h"

View file

@ -9,6 +9,8 @@
#include "platform/utils.h"
#include "platform/allocation.h"
#define MAX_LONG_PATH 32767
namespace dart {

View file

@ -720,7 +720,7 @@ DEFINE_NATIVE_ENTRY(TransferableTypedData_factory, 0, 2) {
}
}
uint8_t* data = reinterpret_cast<uint8_t*>(malloc(total_bytes));
uint8_t* data = reinterpret_cast<uint8_t*>(::malloc(total_bytes));
if (data == nullptr) {
const Instance& exception =
Instance::Handle(thread->isolate()->object_store()->out_of_memory());

View file

@ -0,0 +1,27 @@
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "platform/allocation.h"
#include "platform/assert.h"
namespace dart {
void* malloc(size_t size) {
void* result = ::malloc(size);
if (result == nullptr) {
OUT_OF_MEMORY();
}
return result;
}
void* realloc(void* ptr, size_t size) {
void* result = ::realloc(ptr, size);
if (result == nullptr) {
OUT_OF_MEMORY();
}
return result;
}
} // namespace dart

View file

@ -62,6 +62,9 @@ class MallocAllocated {
#endif
};
void* malloc(size_t size);
void* realloc(void* ptr, size_t size);
} // namespace dart
#endif // RUNTIME_PLATFORM_ALLOCATION_H_

View file

@ -240,12 +240,12 @@ class Malloc : public AllStatic {
public:
template <class T>
static inline T* Alloc(intptr_t len) {
return reinterpret_cast<T*>(malloc(len * sizeof(T)));
return reinterpret_cast<T*>(dart::malloc(len * sizeof(T)));
}
template <class T>
static inline T* Realloc(T* old_array, intptr_t old_len, intptr_t new_len) {
return reinterpret_cast<T*>(realloc(old_array, new_len * sizeof(T)));
return reinterpret_cast<T*>(dart::realloc(old_array, new_len * sizeof(T)));
}
template <class T>

View file

@ -6,6 +6,7 @@
# components.
platform_sources = [
"address_sanitizer.h",
"allocation.cc",
"allocation.h",
"assert.cc",
"assert.h",

View file

@ -114,9 +114,6 @@ void BaseTextBuffer::AddEscapedString(const char* s) {
TextBuffer::TextBuffer(intptr_t buf_size) {
ASSERT(buf_size > 0);
buffer_ = reinterpret_cast<char*>(malloc(buf_size));
if (buffer_ == nullptr) {
OUT_OF_MEMORY();
}
capacity_ = buf_size;
Clear();
}
@ -139,9 +136,6 @@ bool TextBuffer::EnsureCapacity(intptr_t len) {
if (remaining <= len) {
intptr_t new_size = capacity_ + Utils::Maximum(capacity_, len + 1);
char* new_buf = reinterpret_cast<char*>(realloc(buffer_, new_size));
if (new_buf == nullptr) {
OUT_OF_MEMORY();
}
buffer_ = new_buf;
capacity_ = new_size;
}

View file

@ -4,6 +4,8 @@
#include "platform/utils.h"
#include "platform/allocation.h"
namespace dart {
// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,

View file

@ -25,9 +25,6 @@ char* Utils::StrNDup(const char* s, intptr_t n) {
len = n;
}
char* result = reinterpret_cast<char*>(malloc(len + 1));
if (result == NULL) {
return NULL;
}
result[len] = '\0';
return reinterpret_cast<char*>(memmove(result, s, len));
#else // !defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || ...

View file

@ -6,6 +6,7 @@
#if defined(HOST_OS_WINDOWS)
#include <io.h> // NOLINT
#include "platform/allocation.h"
#include "platform/utils.h"
namespace dart {
@ -19,9 +20,6 @@ char* Utils::StrNDup(const char* s, intptr_t n) {
len = n;
}
char* result = reinterpret_cast<char*>(malloc(len + 1));
if (result == NULL) {
return NULL;
}
result[len] = '\0';
return reinterpret_cast<char*>(memmove(result, s, len));
}

View file

@ -4,6 +4,7 @@
#include "vm/base64.h"
#include "platform/allocation.h"
#include "vm/os.h"
namespace dart {

View file

@ -270,7 +270,6 @@ BENCHMARK(KernelServiceCompileAll) {
intptr_t kernel_buffer_size = file->Length();
uint8_t* kernel_buffer =
reinterpret_cast<uint8_t*>(malloc(kernel_buffer_size));
EXPECT(kernel_buffer != NULL);
bool read_fully = file->ReadFully(kernel_buffer, kernel_buffer_size);
EXPECT(read_fully);
Dart_Handle result =

View file

@ -7,6 +7,7 @@
#include <memory>
#include "platform/allocation.h"
#include "platform/assert.h"
#include "platform/atomic.h"
#include "platform/utils.h"

View file

@ -43,9 +43,6 @@ void CodeObservers::Register(CodeObserver* observer) {
observers_length_++;
observers_ = reinterpret_cast<CodeObserver**>(
realloc(observers_, sizeof(observer) * observers_length_));
if (observers_ == NULL) {
FATAL("failed to grow code observers array");
}
observers_[observers_length_ - 1] = observer;
}

View file

@ -842,11 +842,11 @@ void ApiMessageWriter::AddToForwardList(Dart_CObject* object) {
if (forward_list_length_ == 0) {
forward_list_length_ = 4;
intptr_t new_size = forward_list_length_ * sizeof(object);
new_list = ::malloc(new_size);
new_list = dart::malloc(new_size);
} else {
forward_list_length_ *= 2;
intptr_t new_size = (forward_list_length_ * sizeof(object));
new_list = ::realloc(forward_list_, new_size);
new_list = dart::realloc(forward_list_, new_size);
}
ASSERT(new_list != NULL);
forward_list_ = reinterpret_cast<Dart_CObject**>(new_list);
@ -1039,7 +1039,7 @@ bool ApiMessageWriter::WriteCObjectInlined(Dart_CObject* object,
WriteSmi(len);
if (type == Utf8::kLatin1) {
uint8_t* latin1_str =
reinterpret_cast<uint8_t*>(::malloc(len * sizeof(uint8_t)));
reinterpret_cast<uint8_t*>(dart::malloc(len * sizeof(uint8_t)));
bool success =
Utf8::DecodeToLatin1(utf8_str, utf8_len, latin1_str, len);
ASSERT(success);
@ -1049,7 +1049,7 @@ bool ApiMessageWriter::WriteCObjectInlined(Dart_CObject* object,
::free(latin1_str);
} else {
uint16_t* utf16_str =
reinterpret_cast<uint16_t*>(::malloc(len * sizeof(uint16_t)));
reinterpret_cast<uint16_t*>(dart::malloc(len * sizeof(uint16_t)));
bool success = Utf8::DecodeToUTF16(utf8_str, utf8_len, utf16_str, len);
ASSERT(success);
for (intptr_t i = 0; i < len; i++) {

View file

@ -10,7 +10,7 @@
namespace dart {
const intptr_t kSkipCount = 4;
const intptr_t kSkipCount = 5;
} // namespace dart

View file

@ -10,7 +10,7 @@
namespace dart {
const intptr_t kSkipCount = 4;
const intptr_t kSkipCount = 5;
} // namespace dart

View file

@ -11,9 +11,9 @@
namespace dart {
#if defined(DEBUG)
const intptr_t kSkipCount = 5;
const intptr_t kSkipCount = 6;
#elif !(defined(PRODUCT) || defined(DEBUG))
const intptr_t kSkipCount = 4;
const intptr_t kSkipCount = 5;
#endif
} // namespace dart

View file

@ -11,9 +11,9 @@
namespace dart {
#if defined(DEBUG)
const intptr_t kSkipCount = 5;
const intptr_t kSkipCount = 6;
#elif !(defined(PRODUCT) || defined(DEBUG))
const intptr_t kSkipCount = 4;
const intptr_t kSkipCount = 5;
#endif
} // namespace dart

View file

@ -1461,9 +1461,6 @@ void TypedDataLayout::WriteTo(SnapshotWriter* writer,
writer->Write<ObjectPtr>(length_);
uint8_t* data = reinterpret_cast<uint8_t*>(this->data());
void* passed_data = malloc(bytes);
if (passed_data == NULL) {
OUT_OF_MEMORY();
}
memmove(passed_data, data, bytes);
static_cast<MessageWriter*>(writer)->finalizable_data()->Put(
bytes,
@ -1546,9 +1543,6 @@ void ExternalTypedDataLayout::WriteTo(SnapshotWriter* writer,
writer->Write<ObjectPtr>(length_);
uint8_t* data = reinterpret_cast<uint8_t*>(data_);
void* passed_data = malloc(bytes);
if (passed_data == NULL) {
OUT_OF_MEMORY();
}
memmove(passed_data, data, bytes);
static_cast<MessageWriter*>(writer)->finalizable_data()->Put(
bytes,