Make persistent cache serialization safer

Add a version tag at the start, reject any cache blobs without it

Bug: chromium:1062018
Change-Id: I18bb668e6b836e1247640e9286a69a3e745babfd
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/277376
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
diff --git a/src/gpu/GrPersistentCacheUtils.h b/src/gpu/GrPersistentCacheUtils.h
index f38d78b..fe932c2 100644
--- a/src/gpu/GrPersistentCacheUtils.h
+++ b/src/gpu/GrPersistentCacheUtils.h
@@ -27,6 +27,9 @@
     bool fHasSecondaryColorOutput = false;
 };
 
+// Increment this whenever the serialization format of cached shaders changes
+static constexpr int kCurrentVersion = 1;
+
 static inline sk_sp<SkData> PackCachedShaders(SkFourByteTag shaderType,
                                               const SkSL::String shaders[],
                                               const SkSL::Program::Inputs inputs[],
@@ -37,6 +40,7 @@
     SkASSERT(numInputs >= 1 && numInputs <= kGrShaderTypeCount);
 
     SkWriter32 writer;
+    writer.write32(kCurrentVersion);
     writer.write32(shaderType);
     for (int i = 0; i < kGrShaderTypeCount; ++i) {
         writer.writeString(shaders[i].c_str(), shaders[i].size());
@@ -62,6 +66,17 @@
     return writer.snapshotAsData();
 }
 
+static SkFourByteTag GetType(SkReader32* reader) {
+    constexpr SkFourByteTag kInvalidTag = ~0;
+    if (!reader->isAvailable(2 * sizeof(int))) {
+        return kInvalidTag;
+    }
+    if (reader->readInt() != kCurrentVersion) {
+        return kInvalidTag;
+    }
+    return reader->readU32();
+}
+
 static inline void UnpackCachedShaders(SkReader32* reader,
                                        SkSL::String shaders[],
                                        SkSL::Program::Inputs inputs[],