Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Ensure that Skia persistent cache filenames do not exceed the maximum allowed length #28315

Merged
merged 1 commit into from
Sep 2, 2021

Conversation

jason-simmons
Copy link
Member

The cache will now use a hash of the entry's key as the filename.
The full key will be stored inside the cache file.

Fixes flutter/flutter#88726


std::string_view view(reinterpret_cast<const char*>(sha_digest),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like asan is unhappy with this. Maybe there's an obo in the buffer size?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ASAN issue is due to a bug in the Base32 encoder - see #28328


uint32_t signature = kSignature;
uint32_t version = kVersion1;
uint32_t key_size;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How long are keys? Could version and size be uint16_t?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keys are arbitrarily sized SkData instances provided by Skia, and SkData::size() returns a size_t. In practice the keys are likely to be at most a few hundred bytes long, but I used a uint32_t for safety.

if (mapping->GetSize() == 0) {
return nullptr;
if (mapping->GetSize() < sizeof(CacheObjectHeader)) {
return result;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider some log messages here and below to flag when the file is in a bad state?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some logs. Corruption is unlikely because these files are written with fml::WriteAtomically, which completes the write and then renames the file if successful.

Copy link
Member

@chinmaygarde chinmaygarde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nits. But otherwise lgtm.


std::string_view view(reinterpret_cast<const char*>(sha_digest),
SHA_DIGEST_LENGTH);
auto encode_result = fml::Base32Encode(view);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Base32 encoding the key seems unnecessary now after the digest has been calculated. Can we just convert it to a hex string using standard utilities and get rid of fml::Base32Encode and Decode altogether? Will just simplify things.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a hex encode function to FML, which should be slightly faster than Base32. Base32 is still needed for reading and writing the precompiled shader JSON format.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. I forgot about the JSON for training. Sounds good.

@@ -291,17 +293,38 @@ bool PersistentCache::IsValid() const {
return cache_directory_ && cache_directory_->is_valid();
}

sk_sp<SkData> PersistentCache::LoadFile(const fml::UniqueFD& dir,
const std::string& file_name) {
PersistentCache::SkSLCache PersistentCache::LoadFile(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: Can we make PersistantCache::SkSLCache a struct with named members instead of a pair? It's hard to read now because both items are SkData and its not clear if the key is first or the value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

static const uint32_t kSignature = 0xA869593F;
static const uint32_t kVersion1 = 1;

CacheObjectHeader(uint32_t _key_size) : key_size(_key_size) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: The underscore position is not conventional. Perhaps p_key_size?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -1685,8 +1686,13 @@ bool Shell::OnServiceProtocolGetSkSLs(
SkBase64::Encode(sksl.second->data(), sksl.second->size(), b64_char);
b64_char[b64_size] = 0; // make it null terminated for printing
rapidjson::Value shader_value(b64_char, response->GetAllocator());
rapidjson::Value shader_key(PersistentCache::SkKeyToFilePath(*sksl.first),
response->GetAllocator());
std::string_view key_view(reinterpret_cast<const char*>(sksl.first->data()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the comment I made above about making SKSLCache a struct. I can't tell what sksl.first means here without some context. Just a minor nit.

@jason-simmons jason-simmons force-pushed the bug_88726 branch 2 times, most recently from 4d8582b to f9eb85b Compare September 2, 2021 14:59
… allowed length

The cache will now use a hash of the entry's key as the filename.
The full key will be stored inside the cache file.

Fixes flutter/flutter#88726
@jason-simmons jason-simmons added the waiting for tree to go green This PR is approved and tested, but waiting for the tree to be green to land. label Sep 2, 2021
@fluttergithubbot fluttergithubbot merged commit f8b4e8a into flutter:master Sep 2, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Sep 2, 2021
@zanderso
Copy link
Member

@jason-simmons SkiaPerf is showing that this change gave a very nice improvement in worst case frame rasterization time on our iOS sksl warmup benchmark. Thanks, Jason!

https://flutter-flutter-perf.skia.org/e/?begin=1630052164&end=1631621316&queries=test%3DMac_ios_flutter_gallery_ios__transition_perf%26test%3DMac_ios_flutter_gallery_ios_sksl_warmup__transition_perf&requestType=0

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes waiting for tree to go green This PR is approved and tested, but waiting for the tree to be green to land.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Skia persistent cache implementation should handle keys of any length
4 participants