[Impeller] check allocation failures. (flutter/engine#54320)

Fixes https://github.com/flutter/flutter/issues/151084

If allocating a host buffer fails, log a validation error but don't cause an NPE by inserting a nullptr into the host buffer set.
This commit is contained in:
Jonah Williams
2024-08-02 12:49:40 -07:00
committed by GitHub
parent fb537e58de
commit a61c707fe0
3 changed files with 69 additions and 7 deletions

View File

@@ -7,6 +7,7 @@
#include <cstring>
#include <tuple>
#include "impeller/base/validation.h"
#include "impeller/core/allocator.h"
#include "impeller/core/buffer_view.h"
#include "impeller/core/device_buffer.h"
@@ -74,15 +75,21 @@ HostBuffer::TestStateQuery HostBuffer::GetStateForTest() {
};
}
void HostBuffer::MaybeCreateNewBuffer() {
bool HostBuffer::MaybeCreateNewBuffer() {
current_buffer_++;
if (current_buffer_ >= device_buffers_[frame_index_].size()) {
DeviceBufferDescriptor desc;
desc.size = kAllocatorBlockSize;
desc.storage_mode = StorageMode::kHostVisible;
device_buffers_[frame_index_].push_back(allocator_->CreateBuffer(desc));
std::shared_ptr<DeviceBuffer> buffer = allocator_->CreateBuffer(desc);
if (!buffer) {
VALIDATION_LOG << "Failed to allocate host buffer of size " << desc.size;
return false;
}
device_buffers_[frame_index_].push_back(std::move(buffer));
}
offset_ = 0;
return true;
}
std::tuple<Range, std::shared_ptr<DeviceBuffer>> HostBuffer::EmplaceInternal(
@@ -116,7 +123,9 @@ std::tuple<Range, std::shared_ptr<DeviceBuffer>> HostBuffer::EmplaceInternal(
padding = align - (offset_ % align);
}
if (offset_ + padding + length > kAllocatorBlockSize) {
MaybeCreateNewBuffer();
if (!MaybeCreateNewBuffer()) {
return {};
}
} else {
offset_ += padding;
}
@@ -156,7 +165,9 @@ std::tuple<Range, std::shared_ptr<DeviceBuffer>> HostBuffer::EmplaceInternal(
auto old_length = GetLength();
if (old_length + length > kAllocatorBlockSize) {
MaybeCreateNewBuffer();
if (!MaybeCreateNewBuffer()) {
return {};
}
}
old_length = GetLength();
@@ -180,8 +191,8 @@ HostBuffer::EmplaceInternal(const void* buffer, size_t length, size_t align) {
auto padding = align - (GetLength() % align);
if (offset_ + padding < kAllocatorBlockSize) {
offset_ += padding;
} else {
MaybeCreateNewBuffer();
} else if (!MaybeCreateNewBuffer()) {
return {};
}
}

View File

@@ -147,7 +147,11 @@ class HostBuffer {
size_t GetLength() const { return offset_; }
void MaybeCreateNewBuffer();
/// Attempt to create a new internal buffer if the existing capacity is not
/// sufficient.
///
/// A false return value indicates an unrecoverable allocation failure.
[[nodiscard]] bool MaybeCreateNewBuffer();
const std::shared_ptr<DeviceBuffer>& GetCurrentBuffer() const;

View File

@@ -2,7 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <limits>
#include <utility>
#include "flutter/testing/testing.h"
#include "impeller/base/validation.h"
#include "impeller/core/allocator.h"
#include "impeller/core/host_buffer.h"
#include "impeller/entity/entity_playground.h"
@@ -156,5 +160,48 @@ TEST_P(HostBufferTest, EmplaceWithProcIsAligned) {
EXPECT_EQ(view.range, Range(32, 64));
}
static constexpr const size_t kMagicFailingAllocation = 1024000 * 2;
class FailingAllocator : public Allocator {
public:
explicit FailingAllocator(std::shared_ptr<Allocator> delegate)
: Allocator(), delegate_(std::move(delegate)) {}
~FailingAllocator() = default;
std::shared_ptr<DeviceBuffer> OnCreateBuffer(
const DeviceBufferDescriptor& desc) {
// Magic number used in test below to trigger failure.
if (desc.size == kMagicFailingAllocation) {
return nullptr;
}
return delegate_->CreateBuffer(desc);
}
std::shared_ptr<Texture> OnCreateTexture(const TextureDescriptor& desc) {
return delegate_->CreateTexture(desc);
}
ISize GetMaxTextureSizeSupported() const override {
return delegate_->GetMaxTextureSizeSupported();
}
private:
std::shared_ptr<Allocator> delegate_;
};
TEST_P(HostBufferTest, EmplaceWithFailingAllocationDoesntCrash) {
ScopedValidationDisable disable;
std::shared_ptr<FailingAllocator> allocator =
std::make_shared<FailingAllocator>(GetContext()->GetResourceAllocator());
auto buffer = HostBuffer::Create(allocator);
auto view = buffer->Emplace(nullptr, kMagicFailingAllocation, 0);
EXPECT_EQ(view.buffer, nullptr);
EXPECT_EQ(view.range.offset, 0u);
EXPECT_EQ(view.range.length, 0u);
}
} // namespace testing
} // namespace impeller