Reorder member around PNextChainBuilder.

This stack allocated struct stores pointers to the stack. It should be
declared last to match the dependency.

This only matters when using:
```
use_asan_unowned_ptr=true
```
option, which check for dangling pointers on the stack.

Bug: chromium:336222179
Change-Id: Ibd877bcf610cf85b6ad84a5b18fa786d0e06c3ac
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/185760
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Auto-Submit: Arthur Sonzogni <arthursonzogni@chromium.org>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
diff --git a/src/dawn/native/vulkan/BackendVk.cpp b/src/dawn/native/vulkan/BackendVk.cpp
index 0015890..d8feeb7 100644
--- a/src/dawn/native/vulkan/BackendVk.cpp
+++ b/src/dawn/native/vulkan/BackendVk.cpp
@@ -477,11 +477,12 @@
     createInfo.enabledExtensionCount = static_cast<uint32_t>(extensionNames.size());
     createInfo.ppEnabledExtensionNames = extensionNames.data();
 
+    VkDebugUtilsMessengerCreateInfoEXT utilsMessengerCreateInfo;
+    VkValidationFeaturesEXT validationFeatures;
     PNextChainBuilder createInfoChain(&createInfo);
 
     // Register the debug callback for instance creation so we receive message for any errors
     // (validation or other).
-    VkDebugUtilsMessengerCreateInfoEXT utilsMessengerCreateInfo;
     if (usedKnobs.HasExt(InstanceExt::DebugUtils)) {
         utilsMessengerCreateInfo.flags = 0;
         utilsMessengerCreateInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT |
@@ -497,7 +498,6 @@
 
     // Try to turn on synchronization validation if the instance was created with backend
     // validation enabled.
-    VkValidationFeaturesEXT validationFeatures;
     VkValidationFeatureEnableEXT kEnableSynchronizationValidation =
         VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXT;
     if (instance->IsBackendValidationEnabled() &&
diff --git a/src/dawn/native/vulkan/ComputePipelineVk.cpp b/src/dawn/native/vulkan/ComputePipelineVk.cpp
index ec3954d..998ec53 100644
--- a/src/dawn/native/vulkan/ComputePipelineVk.cpp
+++ b/src/dawn/native/vulkan/ComputePipelineVk.cpp
@@ -99,9 +99,9 @@
 
     createInfo.stage.pSpecializationInfo = nullptr;
 
+    VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT subgroupSizeInfo = {};
     PNextChainBuilder stageExtChain(&createInfo.stage);
 
-    VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT subgroupSizeInfo = {};
     uint32_t computeSubgroupSize = device->GetComputeSubgroupSize();
     // If experimental full subgroups is required, pipeline is created with varying subgroup size
     // enabled, and thus do not use explicit subgroup size control.
diff --git a/src/dawn/native/vulkan/SharedTextureMemoryVk.cpp b/src/dawn/native/vulkan/SharedTextureMemoryVk.cpp
index 71cc744..cd2f959 100644
--- a/src/dawn/native/vulkan/SharedTextureMemoryVk.cpp
+++ b/src/dawn/native/vulkan/SharedTextureMemoryVk.cpp
@@ -134,10 +134,10 @@
     createInfo.pQueueFamilyIndices = nullptr;
     createInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
 
-    PNextChainBuilder createInfoChain(&createInfo);
-
     VkExternalMemoryImageCreateInfo externalMemoryImageCreateInfo = {};
     externalMemoryImageCreateInfo.handleTypes = externalMemoryHandleTypeFlagBits;
+
+    PNextChainBuilder createInfoChain(&createInfo);
     createInfoChain.Add(&externalMemoryImageCreateInfo,
                         VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO);
 
@@ -160,20 +160,20 @@
     VkPhysicalDeviceImageFormatInfo2* imageFormatInfo,
     VkExternalMemoryHandleTypeFlagBits externalMemoryHandleTypeFlagBits,
     AdditionalChains*... additionalChains) {
-    PNextChainBuilder imageFormatInfoChain(imageFormatInfo);
-
     VkPhysicalDeviceExternalImageFormatInfo externalImageFormatInfo = {};
     externalImageFormatInfo.handleType = externalMemoryHandleTypeFlagBits;
+
+    PNextChainBuilder imageFormatInfoChain(imageFormatInfo);
     imageFormatInfoChain.Add(&externalImageFormatInfo,
                              VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO);
-
     (imageFormatInfoChain.Add(additionalChains), ...);
 
     VkImageFormatProperties2 imageFormatProps = {};
     imageFormatProps.sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2;
-    PNextChainBuilder imageFormatPropsChain(&imageFormatProps);
 
     VkExternalImageFormatProperties externalImageFormatProps = {};
+
+    PNextChainBuilder imageFormatPropsChain(&imageFormatProps);
     imageFormatPropsChain.Add(&externalImageFormatProps,
                               VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES);
 
@@ -517,9 +517,8 @@
 
     // Query the properties to find the appropriate VkFormat and memory type.
     {
-        PNextChainBuilder bufferPropertiesChain(&bufferProperties);
-
         VkAndroidHardwareBufferFormatPropertiesANDROID bufferFormatProperties;
+        PNextChainBuilder bufferPropertiesChain(&bufferProperties);
         bufferPropertiesChain.Add(
             &bufferFormatProperties,
             VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID);
diff --git a/src/dawn/native/vulkan/TextureVk.cpp b/src/dawn/native/vulkan/TextureVk.cpp
index cf3de24..c484c2c 100644
--- a/src/dawn/native/vulkan/TextureVk.cpp
+++ b/src/dawn/native/vulkan/TextureVk.cpp
@@ -831,9 +831,6 @@
     // frontend already based on the minimum supported formats in the Vulkan spec
     VkImageCreateInfo createInfo = {};
     FillVulkanCreateInfoSizesAndType(*this, &createInfo);
-
-    PNextChainBuilder createInfoChain(&createInfo);
-
     createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
     createInfo.format = VulkanImageFormat(device, GetFormat().format);
     createInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
@@ -841,7 +838,6 @@
     createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
     createInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
 
-    VkImageFormatListCreateInfo imageFormatListInfo = {};
     std::vector<VkFormat> viewFormats;
     bool requiresViewFormatsList = GetViewFormats().any();
     // As current SPIR-V SPEC doesn't support 'bgra8' as a valid image format, to support the
@@ -866,6 +862,8 @@
     // Add the view format list only when the usage does not have storage. Otherwise, the VVL will
     // say creation of the texture is invalid.
     // See https://github.com/gpuweb/gpuweb/issues/4426.
+    VkImageFormatListCreateInfo imageFormatListInfo = {};
+    PNextChainBuilder createInfoChain(&createInfo);
     if (requiresViewFormatsList && device->GetDeviceInfo().HasExt(DeviceExt::ImageFormatList) &&
         !(createInfo.usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
         createInfoChain.Add(&imageFormatListInfo, VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO);
@@ -976,9 +974,6 @@
 
     VkImageCreateInfo baseCreateInfo = {};
     FillVulkanCreateInfoSizesAndType(*this, &baseCreateInfo);
-
-    PNextChainBuilder createInfoChain(&baseCreateInfo);
-
     baseCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
     baseCreateInfo.format = format;
     baseCreateInfo.usage = usage;
@@ -992,6 +987,7 @@
     baseCreateInfo.usage |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
 
     VkImageFormatListCreateInfo imageFormatListInfo = {};
+    PNextChainBuilder createInfoChain(&baseCreateInfo);
     std::vector<VkFormat> viewFormats;
     if (GetViewFormats().any()) {
         baseCreateInfo.flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
diff --git a/src/dawn/native/vulkan/UtilsVulkan.h b/src/dawn/native/vulkan/UtilsVulkan.h
index d7d94a6..7f08a79 100644
--- a/src/dawn/native/vulkan/UtilsVulkan.h
+++ b/src/dawn/native/vulkan/UtilsVulkan.h
@@ -68,6 +68,11 @@
 //     featuresChain.Add(&featuresExtensions.subgroupSizeControl,
 //                       VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT);
 //
+// Note:
+//   The build option `use_asan_unowned_ptr` checks the pointer to the current
+//   tail it is not dangling. So every structs in the chain must be declared
+//   before `PNextChainBuilder`.
+//
 struct PNextChainBuilder : public StackAllocated {
     // Constructor takes the address of a Vulkan structure instance, and
     // walks its pNext chain to record the current location of its tail.
diff --git a/src/dawn/native/vulkan/external_memory/MemoryServiceImplementationAHardwareBuffer.cpp b/src/dawn/native/vulkan/external_memory/MemoryServiceImplementationAHardwareBuffer.cpp
index de5f846..0ead481 100644
--- a/src/dawn/native/vulkan/external_memory/MemoryServiceImplementationAHardwareBuffer.cpp
+++ b/src/dawn/native/vulkan/external_memory/MemoryServiceImplementationAHardwareBuffer.cpp
@@ -67,11 +67,11 @@
             .flags = flags,
         };
 
-        PNextChainBuilder formatInfoChain(&formatInfo);
-
         VkPhysicalDeviceExternalImageFormatInfo externalFormatInfo = {
             .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID,
         };
+
+        PNextChainBuilder formatInfoChain(&formatInfo);
         formatInfoChain.Add(&externalFormatInfo,
                             VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO_KHR);
 
@@ -80,9 +80,8 @@
             .pNext = nullptr,
         };
 
-        PNextChainBuilder formatPropertiesChain(&formatProperties);
-
         VkExternalImageFormatProperties externalFormatProperties;
+        PNextChainBuilder formatPropertiesChain(&formatProperties);
         formatPropertiesChain.Add(&externalFormatProperties,
                                   VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES_KHR);
 
diff --git a/src/dawn/native/vulkan/external_memory/MemoryServiceImplementationDmaBuf.cpp b/src/dawn/native/vulkan/external_memory/MemoryServiceImplementationDmaBuf.cpp
index b8d2f63..9178027 100644
--- a/src/dawn/native/vulkan/external_memory/MemoryServiceImplementationDmaBuf.cpp
+++ b/src/dawn/native/vulkan/external_memory/MemoryServiceImplementationDmaBuf.cpp
@@ -49,11 +49,11 @@
     std::vector<VkDrmFormatModifierPropertiesEXT> formatModifierPropsVector;
     VkFormatProperties2 formatProps = {};
     formatProps.sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2;
-    PNextChainBuilder formatPropsChain(&formatProps);
-
     VkDrmFormatModifierPropertiesListEXT formatModifierPropsList = {};
     formatModifierPropsList.drmFormatModifierCount = 0;
     formatModifierPropsList.pDrmFormatModifierProperties = nullptr;
+
+    PNextChainBuilder formatPropsChain(&formatProps);
     formatPropsChain.Add(&formatModifierPropsList,
                          VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT);
 
diff --git a/src/dawn/native/vulkan/external_memory/MemoryServiceImplementationOpaqueFD.cpp b/src/dawn/native/vulkan/external_memory/MemoryServiceImplementationOpaqueFD.cpp
index bff749a..372c35a 100644
--- a/src/dawn/native/vulkan/external_memory/MemoryServiceImplementationOpaqueFD.cpp
+++ b/src/dawn/native/vulkan/external_memory/MemoryServiceImplementationOpaqueFD.cpp
@@ -135,17 +135,18 @@
         allocateInfo.pNext = nullptr;
         allocateInfo.allocationSize = importParams.allocationSize;
         allocateInfo.memoryTypeIndex = importParams.memoryTypeIndex;
-        PNextChainBuilder allocateInfoChain(&allocateInfo);
 
         VkImportMemoryFdInfoKHR importMemoryFdInfo;
         importMemoryFdInfo.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT;
         importMemoryFdInfo.fd = handle;
-        allocateInfoChain.Add(&importMemoryFdInfo, VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR);
 
         VkMemoryDedicatedAllocateInfo dedicatedAllocateInfo;
+        dedicatedAllocateInfo.image = image;
+        dedicatedAllocateInfo.buffer = VkBuffer{};
+
+        PNextChainBuilder allocateInfoChain(&allocateInfo);
+        allocateInfoChain.Add(&importMemoryFdInfo, VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR);
         if (importParams.dedicatedAllocation) {
-            dedicatedAllocateInfo.image = image;
-            dedicatedAllocateInfo.buffer = VkBuffer{};
             allocateInfoChain.Add(&dedicatedAllocateInfo,
                                   VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO);
         }
diff --git a/src/dawn/tests/white_box/VulkanImageWrappingTests_OpaqueFD.cpp b/src/dawn/tests/white_box/VulkanImageWrappingTests_OpaqueFD.cpp
index 67edf509..cd16976 100644
--- a/src/dawn/tests/white_box/VulkanImageWrappingTests_OpaqueFD.cpp
+++ b/src/dawn/tests/white_box/VulkanImageWrappingTests_OpaqueFD.cpp
@@ -209,10 +209,11 @@
         createInfo.queueFamilyIndexCount = 0;
         createInfo.pQueueFamilyIndices = nullptr;
         createInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
-        PNextChainBuilder createChain(&createInfo);
 
         VkExternalMemoryImageCreateInfoKHR externalInfo;
         externalInfo.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR;
+
+        PNextChainBuilder createChain(&createInfo);
         createChain.Add(&externalInfo, VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO_KHR);
 
         return deviceVk->fn.CreateImage(deviceVk->GetVkDevice(), &createInfo, nullptr, &**image);
@@ -236,11 +237,12 @@
         allocateInfo.pNext = nullptr;
         allocateInfo.allocationSize = requirements.size;
         allocateInfo.memoryTypeIndex = static_cast<uint32_t>(bestType);
-        PNextChainBuilder allocateChain(&allocateInfo);
 
         // Import memory from file descriptor
         VkExportMemoryAllocateInfoKHR externalInfo;
         externalInfo.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR;
+
+        PNextChainBuilder allocateChain(&allocateInfo);
         allocateChain.Add(&externalInfo, VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR);
 
         // Use a dedicated memory allocation if testing that path.