-
Notifications
You must be signed in to change notification settings - Fork 6k
[Impeller] Perform integrity checks for Vulkan pipeline caches. #54654
Changes from 1 commit
3bc5b1f
dfe8dc3
5cb6fd5
b2c750b
aafaa32
eddf424
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "impeller/renderer/backend/vulkan/pipeline_cache_data_vk.h" | ||
|
|
||
| #include "flutter/fml/file.h" | ||
| #include "impeller/base/allocation.h" | ||
| #include "impeller/base/validation.h" | ||
|
|
||
| namespace impeller { | ||
|
|
||
| static constexpr const char* kPipelineCacheFileName = | ||
| "flutter.impeller.vkcache"; | ||
|
|
||
| // Inspired by | ||
| // https://medium.com/@zeuxcg/creating-a-robust-pipeline-cache-with-vulkan-961d09416cda. | ||
| struct PipelineCacheHeader { | ||
| // This can be used by Impeller to manually invalidate all old caches. | ||
| uint32_t magic = 0xC0DEF00D; | ||
| // Notably, this field is missing from checks the Vulkan driver performs. For | ||
| // drivers that don't correctly check the UUID, explicitly disregarding caches | ||
| // generated by previous driver versions sidesteps some landmines. | ||
| uint32_t driver_version = 0; | ||
| uint32_t vendor_id = 0; | ||
| uint32_t device_id = 0; | ||
| // If applications are published as 32-bit and updated via the app store to be | ||
| // 64-bits, this check comes in handy to disregard previous caches. | ||
| uint32_t abi = sizeof(void*); | ||
| uint8_t uuid[VK_UUID_SIZE] = {}; | ||
| uint64_t data_size = 0; | ||
|
|
||
| PipelineCacheHeader() = default; | ||
|
|
||
| explicit PipelineCacheHeader(const VkPhysicalDeviceProperties& props, | ||
| uint64_t p_data_size) | ||
| : driver_version(props.driverVersion), | ||
| vendor_id(props.vendorID), | ||
| device_id(props.deviceID), | ||
| data_size(p_data_size) { | ||
| std::memcpy(uuid, props.pipelineCacheUUID, VK_UUID_SIZE); | ||
| } | ||
|
|
||
| bool IsCompatibleWith(const PipelineCacheHeader& o) const { | ||
| // Check for everything but the data size. | ||
| return magic == o.magic && // | ||
| driver_version == o.driver_version && // | ||
| vendor_id == o.vendor_id && // | ||
| device_id == o.device_id && // | ||
| abi == o.abi && // | ||
| std::memcmp(uuid, o.uuid, VK_UUID_SIZE) == 0; | ||
| } | ||
| }; | ||
|
|
||
| bool PipelineCacheDataPersist(const fml::UniqueFD& cache_directory, | ||
| const VkPhysicalDeviceProperties& props, | ||
| const vk::UniquePipelineCache& cache) { | ||
| if (!cache_directory.is_valid()) { | ||
| return false; | ||
| } | ||
| size_t data_size = 0u; | ||
| if (cache.getOwner().getPipelineCacheData(*cache, &data_size, nullptr) != | ||
| vk::Result::eSuccess) { | ||
| VALIDATION_LOG << "Could not fetch pipeline cache size."; | ||
| return false; | ||
| } | ||
| if (data_size == 0u) { | ||
| return true; | ||
| } | ||
| auto allocation = std::make_shared<Allocation>(); | ||
| if (!allocation->Truncate(Bytes{sizeof(PipelineCacheHeader) + data_size})) { | ||
| VALIDATION_LOG << "Could not allocate pipeline cache data staging buffer."; | ||
| return false; | ||
| } | ||
| const auto header = PipelineCacheHeader{props, data_size}; | ||
| std::memcpy(allocation->GetBuffer(), &header, sizeof(header)); | ||
| if (cache.getOwner().getPipelineCacheData( | ||
| *cache, &data_size, allocation->GetBuffer() + sizeof(header)) != | ||
| vk::Result::eSuccess) { | ||
| VALIDATION_LOG << "Could not copy pipeline cache data."; | ||
| return false; | ||
| } | ||
|
|
||
| auto allocation_mapping = CreateMappingFromAllocation(allocation); | ||
| if (!allocation_mapping) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice if we had a unique_ptr variant for when we don't need shared ownership like here. Not blocking.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once we get concepts, I'd like to apply one for any pointer like thing that can be moved. So it would work for both. |
||
| return false; | ||
| } | ||
| if (!fml::WriteAtomically(cache_directory, kPipelineCacheFileName, | ||
| *allocation_mapping)) { | ||
| VALIDATION_LOG << "Could not write cache file to disk."; | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| std::unique_ptr<fml::Mapping> PipelineCacheDataRetrieve( | ||
| const fml::UniqueFD& cache_directory, | ||
| const VkPhysicalDeviceProperties& props) { | ||
| if (!cache_directory.is_valid()) { | ||
| return nullptr; | ||
| } | ||
| std::shared_ptr<fml::FileMapping> on_disk_data = | ||
| fml::FileMapping::CreateReadOnly(cache_directory, kPipelineCacheFileName); | ||
| if (!on_disk_data) { | ||
| return nullptr; | ||
| } | ||
| if (on_disk_data->GetSize() < sizeof(PipelineCacheHeader)) { | ||
| VALIDATION_LOG << "Pipeline cache data size is too small."; | ||
| return nullptr; | ||
| } | ||
| auto on_disk_header = PipelineCacheHeader{}; | ||
| std::memcpy(&on_disk_header, // | ||
| on_disk_data->GetMapping(), // | ||
| sizeof(on_disk_header) // | ||
| ); | ||
| const auto current_header = PipelineCacheHeader{props, 0u}; | ||
| if (!on_disk_header.IsCompatibleWith(current_header)) { | ||
| FML_LOG(WARNING) | ||
| << "Persisted pipeline cache is not compatible with current " | ||
| "Vulkan context. Ignoring."; | ||
| return nullptr; | ||
| } | ||
| // Zero sized data is known to cause issues. | ||
| if (on_disk_header.data_size == 0u) { | ||
| return nullptr; | ||
| } | ||
| return std::make_unique<fml::NonOwnedMapping>( | ||
| on_disk_data->GetMapping() + sizeof(on_disk_header), | ||
| on_disk_header.data_size, [on_disk_data](auto, auto) {}); | ||
| } | ||
|
|
||
| } // namespace impeller | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_PIPELINE_CACHE_DATA_VK_H_ | ||
| #define FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_PIPELINE_CACHE_DATA_VK_H_ | ||
|
|
||
| #include "flutter/fml/mapping.h" | ||
| #include "flutter/fml/unique_fd.h" | ||
| #include "impeller/renderer/backend/vulkan/vk.h" | ||
|
|
||
| namespace impeller { | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| /// @brief Persist the pipeline cache to a file in the given cache | ||
| /// directory. This function performs integrity checks the Vulkan | ||
| /// driver may have missed. | ||
| /// | ||
| /// @warning The pipeline cache must be externally synchronized for most | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I left this in warning in there because it is theoretically possible if we are still in the middle of constructing pipelines. The failure mode is not fatal though and we only persist after a set number of frames. So I'm not too worried about this. |
||
| /// complete results. If additional pipelines are being created | ||
| /// while this function is executing, this function may fail to | ||
| /// persist data. | ||
| /// | ||
| /// @param[in] cache_directory The cache directory | ||
| /// @param[in] props The physical device properties | ||
| /// @param[in] cache The cache | ||
| /// | ||
| /// @return If the cache data could be persisted to disk. | ||
| /// | ||
| bool PipelineCacheDataPersist(const fml::UniqueFD& cache_directory, | ||
| const VkPhysicalDeviceProperties& props, | ||
| const vk::UniquePipelineCache& cache); | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| /// @brief Retrieve the previously persisted pipeline cache data. This | ||
| /// function provides integrity checks the Vulkan driver may have | ||
| /// missed. | ||
| /// | ||
| /// @param[in] cache_directory The cache directory | ||
| /// @param[in] props The properties | ||
| /// | ||
| /// @return The cache data if it was found and checked to have passed | ||
| /// additional integrity checks. | ||
| /// | ||
| std::unique_ptr<fml::Mapping> PipelineCacheDataRetrieve( | ||
| const fml::UniqueFD& cache_directory, | ||
| const VkPhysicalDeviceProperties& props); | ||
|
|
||
| } // namespace impeller | ||
|
|
||
| #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_PIPELINE_CACHE_DATA_VK_H_ | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jtmcdole this is almost you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol yeah! The other one I had in mind was
0xFA51F00D(fast-food for our pipelines) but theTdidn't read right.