Hash :
5d01d538
Author :
Date :
2020-05-12T18:13:52
Resolve Bad Binary Link Failures When ANGLE_PROGRAM_BINARY_LOAD is enabled, Program::loadBinary unconditionally returns angle::Result::Continue to the caller. The caller, gl::Program::link, postpones the resolution of the link until resolveLinkImpl. Unfortunately, resolveLinkImpl is not able to tell whether the link failed because the shader from the developer is bad or because the loaded binary is not compatible with the backend. The former case should fail link. In the latter case, we should fallback to linking the program from the original shader sources. The loaded binary could be read from the on-disk shader cache and be corrupted or serialized with different revision and subsystem id than the currently loaded ANGLE backend. This fix adjusts Program::loadBinary and ProgramD3D::load so that angle::Result::Incomplete is returned to gl::Program::link when the binary is incompatible with the backend. gl::Program:link falls back to compilation from original shader sources. Since no code checks the return value of SizedMRUCache::eraseByKey, modified it to now return void. Bug: chromium:1079497 Change-Id: Id5271d7badad8627563e87859d1c9fdb81de5785 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2197944 Reviewed-by: Jamie Madill <jmadill@chromium.org> Reviewed-by: Tim Van Patten <timvp@google.com> Commit-Queue: Rafael Cintron <rafael.cintron@microsoft.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
//
// Copyright 2018 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// BlobCache: Stores keyed blobs in memory to support EGL_ANDROID_blob_cache.
// Can be used in conjunction with the platform layer to warm up the cache from
// disk. MemoryProgramCache uses this to handle caching of compiled programs.
#include "libANGLE/BlobCache.h"
#include "common/utilities.h"
#include "common/version.h"
#include "libANGLE/Context.h"
#include "libANGLE/Display.h"
#include "libANGLE/histogram_macros.h"
#include "platform/Platform.h"
namespace egl
{
namespace
{
enum CacheResult
{
kCacheMiss,
kCacheHitMemory,
kCacheHitDisk,
kCacheResultMax,
};
} // anonymous namespace
BlobCache::BlobCache(size_t maxCacheSizeBytes)
: mBlobCache(maxCacheSizeBytes), mSetBlobFunc(nullptr), mGetBlobFunc(nullptr)
{}
BlobCache::~BlobCache() {}
void BlobCache::put(const BlobCache::Key &key, angle::MemoryBuffer &&value)
{
if (areBlobCacheFuncsSet())
{
// Store the result in the application's cache
mSetBlobFunc(key.data(), key.size(), value.data(), value.size());
}
else
{
populate(key, std::move(value), CacheSource::Memory);
}
}
void BlobCache::putApplication(const BlobCache::Key &key, const angle::MemoryBuffer &value)
{
if (areBlobCacheFuncsSet())
{
mSetBlobFunc(key.data(), key.size(), value.data(), value.size());
}
}
void BlobCache::populate(const BlobCache::Key &key, angle::MemoryBuffer &&value, CacheSource source)
{
CacheEntry newEntry;
newEntry.first = std::move(value);
newEntry.second = source;
// Cache it inside blob cache only if caching inside the application is not possible.
mBlobCache.put(key, std::move(newEntry), newEntry.first.size());
}
bool BlobCache::get(angle::ScratchBuffer *scratchBuffer,
const BlobCache::Key &key,
BlobCache::Value *valueOut)
{
// Look into the application's cache, if there is such a cache
if (areBlobCacheFuncsSet())
{
EGLsizeiANDROID valueSize = mGetBlobFunc(key.data(), key.size(), nullptr, 0);
if (valueSize <= 0)
{
return false;
}
angle::MemoryBuffer *scratchMemory;
bool result = scratchBuffer->get(valueSize, &scratchMemory);
if (!result)
{
ERR() << "Failed to allocate memory for binary blob";
return false;
}
EGLsizeiANDROID originalValueSize = valueSize;
valueSize = mGetBlobFunc(key.data(), key.size(), scratchMemory->data(), valueSize);
// Make sure the key/value pair still exists/is unchanged after the second call
// (modifications to the application cache by another thread are a possibility)
if (valueSize != originalValueSize)
{
// This warning serves to find issues with the application cache, none of which are
// currently known to be thread-safe. If such a use ever arises, this WARN can be
// removed.
WARN() << "Binary blob no longer available in cache (removed by a thread?)";
return false;
}
*valueOut = BlobCache::Value(scratchMemory->data(), scratchMemory->size());
return true;
}
// Otherwise we are doing caching internally, so try to find it there
const CacheEntry *entry;
bool result = mBlobCache.get(key, &entry);
if (result)
{
if (entry->second == CacheSource::Memory)
{
ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.ProgramCache.CacheResult", kCacheHitMemory,
kCacheResultMax);
}
else
{
ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.ProgramCache.CacheResult", kCacheHitDisk,
kCacheResultMax);
}
*valueOut = BlobCache::Value(entry->first.data(), entry->first.size());
}
else
{
ANGLE_HISTOGRAM_ENUMERATION("GPU.ANGLE.ProgramCache.CacheResult", kCacheMiss,
kCacheResultMax);
}
return result;
}
bool BlobCache::getAt(size_t index, const BlobCache::Key **keyOut, BlobCache::Value *valueOut)
{
const CacheEntry *valueBuf;
bool result = mBlobCache.getAt(index, keyOut, &valueBuf);
if (result)
{
*valueOut = BlobCache::Value(valueBuf->first.data(), valueBuf->first.size());
}
return result;
}
void BlobCache::remove(const BlobCache::Key &key)
{
mBlobCache.eraseByKey(key);
}
void BlobCache::setBlobCacheFuncs(EGLSetBlobFuncANDROID set, EGLGetBlobFuncANDROID get)
{
mSetBlobFunc = set;
mGetBlobFunc = get;
}
bool BlobCache::areBlobCacheFuncsSet() const
{
// Either none or both of the callbacks should be set.
ASSERT((mSetBlobFunc != nullptr) == (mGetBlobFunc != nullptr));
return mSetBlobFunc != nullptr && mGetBlobFunc != nullptr;
}
} // namespace egl