Skip to content

Commit f81814b

Browse files
Treehugger RobotGerrit Code Review
authored andcommitted
Merge "EGL Multifile Blobcache: Make use of crc32_z algorithm instead of crc32c" into main
2 parents bbe31e3 + 88a1b76 commit f81814b

6 files changed

Lines changed: 28 additions & 29 deletions

File tree

opengl/libs/Android.bp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ cc_library_static {
135135
"EGL/MultifileBlobCache.cpp",
136136
],
137137
export_include_dirs: ["EGL"],
138+
shared_libs: [
139+
"libz",
140+
],
138141
}
139142

140143
cc_library_shared {
@@ -169,6 +172,7 @@ cc_library_shared {
169172
"libutils",
170173
"libSurfaceFlingerProp",
171174
"libunwindstack",
175+
"libz",
172176
],
173177
static_libs: [
174178
"libEGL_getProcAddress",
@@ -199,6 +203,7 @@ cc_test {
199203
],
200204
shared_libs: [
201205
"libutils",
206+
"libz",
202207
],
203208
}
204209

opengl/libs/EGL/FileBlobCache.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,18 @@
2727

2828
#include <log/log.h>
2929
#include <utils/Trace.h>
30+
#include <zlib.h>
3031

3132
// Cache file header
3233
static const char* cacheFileMagic = "EGL$";
3334
static const size_t cacheFileHeaderSize = 8;
3435

3536
namespace android {
3637

37-
uint32_t crc32c(const uint8_t* buf, size_t len) {
38-
const uint32_t polyBits = 0x82F63B78;
39-
uint32_t r = 0;
40-
for (size_t i = 0; i < len; i++) {
41-
r ^= buf[i];
42-
for (int j = 0; j < 8; j++) {
43-
if (r & 1) {
44-
r = (r >> 1) ^ polyBits;
45-
} else {
46-
r >>= 1;
47-
}
48-
}
49-
}
50-
return r;
38+
uint32_t GenerateCRC32(const uint8_t *data, size_t size)
39+
{
40+
const unsigned long initialValue = crc32_z(0u, nullptr, 0u);
41+
return static_cast<uint32_t>(crc32_z(initialValue, data, size));
5142
}
5243

5344
FileBlobCache::FileBlobCache(size_t maxKeySize, size_t maxValueSize, size_t maxTotalSize,
@@ -101,7 +92,7 @@ FileBlobCache::FileBlobCache(size_t maxKeySize, size_t maxValueSize, size_t maxT
10192
return;
10293
}
10394
uint32_t* crc = reinterpret_cast<uint32_t*>(buf + 4);
104-
if (crc32c(buf + headerSize, cacheSize) != *crc) {
95+
if (GenerateCRC32(buf + headerSize, cacheSize) != *crc) {
10596
ALOGE("cache file failed CRC check");
10697
close(fd);
10798
return;
@@ -175,7 +166,7 @@ void FileBlobCache::writeToFile() {
175166
// Write the file magic and CRC
176167
memcpy(buf, cacheFileMagic, 4);
177168
uint32_t* crc = reinterpret_cast<uint32_t*>(buf + 4);
178-
*crc = crc32c(buf + headerSize, cacheSize);
169+
*crc = GenerateCRC32(buf + headerSize, cacheSize);
179170

180171
if (write(fd, buf, fileSize) == -1) {
181172
ALOGE("error writing cache file: %s (%d)", strerror(errno),

opengl/libs/EGL/FileBlobCache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
namespace android {
2424

25-
uint32_t crc32c(const uint8_t* buf, size_t len);
25+
uint32_t GenerateCRC32(const uint8_t *data, size_t size);
2626

2727
class FileBlobCache : public BlobCache {
2828
public:

opengl/libs/EGL/MultifileBlobCache.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,8 @@ MultifileBlobCache::MultifileBlobCache(size_t maxKeySize, size_t maxValueSize, s
214214
}
215215

216216
// Ensure we have a good CRC
217-
if (header.crc !=
218-
crc32c(mappedEntry + sizeof(MultifileHeader),
219-
fileSize - sizeof(MultifileHeader))) {
217+
if (header.crc != GenerateCRC32(mappedEntry + sizeof(MultifileHeader),
218+
fileSize - sizeof(MultifileHeader))) {
220219
ALOGV("INIT: Entry %u failed CRC check! Removing.", entryHash);
221220
if (remove(fullPath.c_str()) != 0) {
222221
ALOGE("Error removing %s: %s", fullPath.c_str(), std::strerror(errno));
@@ -532,9 +531,9 @@ bool MultifileBlobCache::createStatus(const std::string& baseDir) {
532531
mBuildId.length() > PROP_VALUE_MAX ? PROP_VALUE_MAX : mBuildId.length());
533532

534533
// Finally update the crc, using cacheVersion and everything the follows
535-
status.crc =
536-
crc32c(reinterpret_cast<uint8_t*>(&status) + offsetof(MultifileStatus, cacheVersion),
537-
sizeof(status) - offsetof(MultifileStatus, cacheVersion));
534+
status.crc = GenerateCRC32(
535+
reinterpret_cast<uint8_t *>(&status) + offsetof(MultifileStatus, cacheVersion),
536+
sizeof(status) - offsetof(MultifileStatus, cacheVersion));
538537

539538
// Create the status file
540539
std::string cacheStatus = baseDir + "/" + kMultifileBlobCacheStatusFile;
@@ -599,9 +598,9 @@ bool MultifileBlobCache::checkStatus(const std::string& baseDir) {
599598
}
600599

601600
// Ensure we have a good CRC
602-
if (status.crc !=
603-
crc32c(reinterpret_cast<uint8_t*>(&status) + offsetof(MultifileStatus, cacheVersion),
604-
sizeof(status) - offsetof(MultifileStatus, cacheVersion))) {
601+
if (status.crc != GenerateCRC32(reinterpret_cast<uint8_t *>(&status) +
602+
offsetof(MultifileStatus, cacheVersion),
603+
sizeof(status) - offsetof(MultifileStatus, cacheVersion))) {
605604
ALOGE("STATUS(CHECK): Cache status failed CRC check!");
606605
return false;
607606
}
@@ -840,8 +839,8 @@ void MultifileBlobCache::processTask(DeferredTask& task) {
840839

841840
// Add CRC check to the header (always do this last!)
842841
MultifileHeader* header = reinterpret_cast<MultifileHeader*>(buffer);
843-
header->crc =
844-
crc32c(buffer + sizeof(MultifileHeader), bufferSize - sizeof(MultifileHeader));
842+
header->crc = GenerateCRC32(buffer + sizeof(MultifileHeader),
843+
bufferSize - sizeof(MultifileHeader));
845844

846845
ssize_t result = write(fd, buffer, bufferSize);
847846
if (result != bufferSize) {

opengl/libs/EGL/MultifileBlobCache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
namespace android {
3636

37-
constexpr uint32_t kMultifileBlobCacheVersion = 1;
37+
constexpr uint32_t kMultifileBlobCacheVersion = 2;
3838
constexpr char kMultifileBlobCacheStatusFile[] = "cache.status";
3939

4040
struct MultifileHeader {

opengl/libs/EGL/fuzzer/Android.bp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ cc_fuzz {
3636
"libutils",
3737
],
3838

39+
shared_libs: [
40+
"libz",
41+
],
42+
3943
srcs: [
4044
"MultifileBlobCache_fuzzer.cpp",
4145
],

0 commit comments

Comments
 (0)