WebGPU error handling 1: Return error objects on errors.

Dawn used to return "nullptr" when an error happened while creating an
object. To match WebGPU we want to return valid pointers but to "error"
objects.

This commit implements WebGPU error handling for all "descriptorized"
objects and changes the nullptr error checks into "ValidateObject"
checks. This method is used both to check that the object isn't an
error, but also that all objects in a function call are from the same
device.

New validation is added to objects with methods (apart from Device) so
they check they aren't error objects.

A large number of ASSERTs were added to check that frontend objects
aren't accessed when they are errors, so that missing validation would
hit the asserts instead of crashing randomly.

The bind group validation tests were modified to test the behavior with
both nullptrs and error objects.

Future commits will change the CommandBufferBuilder to not be a builder
anymore but an "Encoder" instead with special-cased error handling. Then
once all objects are descriptorized, the notion of builders will be
removed from the code.

BUG=dawn:8

Change-Id: I8647712d5de3deb0e99e3bc58f34496f67710edd
Reviewed-on: https://dawn-review.googlesource.com/c/4360
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
diff --git a/src/dawn_native/Buffer.cpp b/src/dawn_native/Buffer.cpp
index 543d62e..97ba85a 100644
--- a/src/dawn_native/Buffer.cpp
+++ b/src/dawn_native/Buffer.cpp
@@ -23,6 +23,33 @@
 
 namespace dawn_native {
 
+    namespace {
+
+        class ErrorBuffer : public BufferBase {
+          public:
+            ErrorBuffer(DeviceBase* device) : BufferBase(device, ObjectBase::kError) {
+            }
+
+          private:
+            MaybeError SetSubDataImpl(uint32_t start,
+                                      uint32_t count,
+                                      const uint8_t* data) override {
+                UNREACHABLE();
+                return {};
+            }
+            void MapReadAsyncImpl(uint32_t serial, uint32_t start, uint32_t size) override {
+                UNREACHABLE();
+            }
+            void MapWriteAsyncImpl(uint32_t serial, uint32_t start, uint32_t size) override {
+                UNREACHABLE();
+            }
+            void UnmapImpl() override {
+                UNREACHABLE();
+            }
+        };
+
+    }  // anonymous namespace
+
     MaybeError ValidateBufferDescriptor(DeviceBase*, const BufferDescriptor* descriptor) {
         if (descriptor->nextInChain != nullptr) {
             return DAWN_VALIDATION_ERROR("nextInChain must be nullptr");
@@ -53,22 +80,35 @@
         : ObjectBase(device), mSize(descriptor->size), mUsage(descriptor->usage) {
     }
 
+    BufferBase::BufferBase(DeviceBase* device, ObjectBase::ErrorTag tag) : ObjectBase(device, tag) {
+    }
+
     BufferBase::~BufferBase() {
         if (mIsMapped) {
+            ASSERT(!IsError());
             CallMapReadCallback(mMapSerial, DAWN_BUFFER_MAP_ASYNC_STATUS_UNKNOWN, nullptr);
             CallMapWriteCallback(mMapSerial, DAWN_BUFFER_MAP_ASYNC_STATUS_UNKNOWN, nullptr);
         }
     }
 
+    // static
+    BufferBase* BufferBase::MakeError(DeviceBase* device) {
+        return new ErrorBuffer(device);
+    }
+
     uint32_t BufferBase::GetSize() const {
+        ASSERT(!IsError());
         return mSize;
     }
 
     dawn::BufferUsageBit BufferBase::GetUsage() const {
+        ASSERT(!IsError());
         return mUsage;
     }
 
     MaybeError BufferBase::ValidateCanUseInSubmitNow() const {
+        ASSERT(!IsError());
+
         if (mIsMapped) {
             return DAWN_VALIDATION_ERROR("Buffer used in a submit while mapped");
         }
@@ -78,6 +118,8 @@
     void BufferBase::CallMapReadCallback(uint32_t serial,
                                          dawnBufferMapAsyncStatus status,
                                          const void* pointer) {
+        ASSERT(!IsError());
+
         if (mMapReadCallback != nullptr && serial == mMapSerial) {
             ASSERT(mMapWriteCallback == nullptr);
             // Tag the callback as fired before firing it, otherwise it could fire a second time if
@@ -91,6 +133,8 @@
     void BufferBase::CallMapWriteCallback(uint32_t serial,
                                           dawnBufferMapAsyncStatus status,
                                           void* pointer) {
+        ASSERT(!IsError());
+
         if (mMapWriteCallback != nullptr && serial == mMapSerial) {
             ASSERT(mMapReadCallback == nullptr);
             // Tag the callback as fired before firing it, otherwise it could fire a second time if
@@ -105,6 +149,7 @@
         if (GetDevice()->ConsumedError(ValidateSetSubData(start, count))) {
             return;
         }
+        ASSERT(!IsError());
 
         if (GetDevice()->ConsumedError(SetSubDataImpl(start, count, data))) {
             return;
@@ -119,6 +164,7 @@
             callback(DAWN_BUFFER_MAP_ASYNC_STATUS_ERROR, nullptr, userdata);
             return;
         }
+        ASSERT(!IsError());
 
         ASSERT(mMapWriteCallback == nullptr);
 
@@ -139,6 +185,7 @@
             callback(DAWN_BUFFER_MAP_ASYNC_STATUS_ERROR, nullptr, userdata);
             return;
         }
+        ASSERT(!IsError());
 
         ASSERT(mMapReadCallback == nullptr);
 
@@ -155,6 +202,7 @@
         if (GetDevice()->ConsumedError(ValidateUnmap())) {
             return;
         }
+        ASSERT(!IsError());
 
         // A map request can only be called once, so this will fire only if the request wasn't
         // completed before the Unmap
@@ -168,6 +216,8 @@
     }
 
     MaybeError BufferBase::ValidateSetSubData(uint32_t start, uint32_t count) const {
+        DAWN_TRY(GetDevice()->ValidateObject(this));
+
         if (count > GetSize()) {
             return DAWN_VALIDATION_ERROR("Buffer subdata with too much data");
         }
@@ -187,6 +237,8 @@
     MaybeError BufferBase::ValidateMap(uint32_t start,
                                        uint32_t size,
                                        dawn::BufferUsageBit requiredUsage) const {
+        DAWN_TRY(GetDevice()->ValidateObject(this));
+
         if (size > GetSize()) {
             return DAWN_VALIDATION_ERROR("Buffer mapping with too big a region");
         }
@@ -208,6 +260,8 @@
     }
 
     MaybeError BufferBase::ValidateUnmap() const {
+        DAWN_TRY(GetDevice()->ValidateObject(this));
+
         if (!mIsMapped) {
             return DAWN_VALIDATION_ERROR("Buffer wasn't mapped");
         }