Null backend: disallow more than 256M of Buffer allocations

Should prevent some uninteresting fuzzer failures.

Bug: dawn:37
Change-Id: I6b2bef01deb700239e02ef5afa9313023ec83899
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/7483
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Kai Ninomiya <kainino@chromium.org>
This commit is contained in:
Kai Ninomiya 2019-05-24 23:29:41 +00:00 committed by Commit Bot service account
parent 58ae08e132
commit 7777078fb9
2 changed files with 27 additions and 0 deletions

View File

@ -68,6 +68,10 @@ namespace dawn_native { namespace null {
Device::~Device() { Device::~Device() {
mDynamicUploader = nullptr; mDynamicUploader = nullptr;
// Ensure any in-flight maps have been cleaned up.
SubmitPendingOperations();
ASSERT(mMemoryUsage == 0);
} }
ResultOrError<BindGroupBase*> Device::CreateBindGroupImpl( ResultOrError<BindGroupBase*> Device::CreateBindGroupImpl(
@ -79,6 +83,7 @@ namespace dawn_native { namespace null {
return new BindGroupLayout(this, descriptor); return new BindGroupLayout(this, descriptor);
} }
ResultOrError<BufferBase*> Device::CreateBufferImpl(const BufferDescriptor* descriptor) { ResultOrError<BufferBase*> Device::CreateBufferImpl(const BufferDescriptor* descriptor) {
DAWN_TRY(IncrementMemoryUsage(descriptor->size));
return new Buffer(this, descriptor); return new Buffer(this, descriptor);
} }
CommandBufferBase* Device::CreateCommandBuffer(CommandEncoderBase* encoder) { CommandBufferBase* Device::CreateCommandBuffer(CommandEncoderBase* encoder) {
@ -138,6 +143,20 @@ namespace dawn_native { namespace null {
return DAWN_UNIMPLEMENTED_ERROR("Device unable to copy from staging buffer."); return DAWN_UNIMPLEMENTED_ERROR("Device unable to copy from staging buffer.");
} }
MaybeError Device::IncrementMemoryUsage(size_t bytes) {
static_assert(kMaxMemoryUsage <= std::numeric_limits<size_t>::max() / 2, "");
if (bytes > kMaxMemoryUsage || mMemoryUsage + bytes > kMaxMemoryUsage) {
return DAWN_CONTEXT_LOST_ERROR("Out of memory.");
}
mMemoryUsage += bytes;
return {};
}
void Device::DecrementMemoryUsage(size_t bytes) {
ASSERT(mMemoryUsage >= bytes);
mMemoryUsage -= bytes;
}
Serial Device::GetCompletedCommandSerial() const { Serial Device::GetCompletedCommandSerial() const {
return mCompletedSerial; return mCompletedSerial;
} }
@ -189,6 +208,8 @@ namespace dawn_native { namespace null {
} }
Buffer::~Buffer() { Buffer::~Buffer() {
DestroyInternal();
ToBackend(GetDevice())->DecrementMemoryUsage(GetSize());
} }
MaybeError Buffer::MapAtCreationImpl(uint8_t** mappedPointer) { MaybeError Buffer::MapAtCreationImpl(uint8_t** mappedPointer) {

View File

@ -102,6 +102,9 @@ namespace dawn_native { namespace null {
uint64_t destinationOffset, uint64_t destinationOffset,
uint64_t size) override; uint64_t size) override;
MaybeError IncrementMemoryUsage(size_t bytes);
void DecrementMemoryUsage(size_t bytes);
private: private:
ResultOrError<BindGroupBase*> CreateBindGroupImpl( ResultOrError<BindGroupBase*> CreateBindGroupImpl(
const BindGroupDescriptor* descriptor) override; const BindGroupDescriptor* descriptor) override;
@ -128,6 +131,9 @@ namespace dawn_native { namespace null {
Serial mCompletedSerial = 0; Serial mCompletedSerial = 0;
Serial mLastSubmittedSerial = 0; Serial mLastSubmittedSerial = 0;
std::vector<std::unique_ptr<PendingOperation>> mPendingOperations; std::vector<std::unique_ptr<PendingOperation>> mPendingOperations;
static constexpr size_t kMaxMemoryUsage = 256 * 1024 * 1024;
size_t mMemoryUsage = 0;
}; };
class Buffer : public BufferBase { class Buffer : public BufferBase {