| 1 |
/* |
| 2 |
* Copyright 2019 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/core/SkDescriptor.h" |
| 9 |
|
| 10 |
#include "include/core/SkTypes.h" |
| 11 |
#include "include/private/base/SkAlign.h" |
| 12 |
#include "include/private/base/SkTemplates.h" |
| 13 |
#include "include/private/base/SkTo.h" |
| 14 |
#include "src/core/SkChecksum.h" |
| 15 |
#include "src/core/SkReadBuffer.h" |
| 16 |
#include "src/core/SkWriteBuffer.h" |
| 17 |
|
| 18 |
#include <cstring> |
| 19 |
|
| 20 |
std::unique_ptr<SkDescriptor> SkDescriptor::Alloc(size_t length) { |
| 21 |
SkASSERT(length >= sizeof(SkDescriptor) && SkIsAlign4(length)); |
| 22 |
void* allocation = ::operator new(length); |
| 23 |
return std::unique_ptr<SkDescriptor>(new (allocation) SkDescriptor{}); |
| 24 |
} |
| 25 |
|
| 26 |
void SkDescriptor::operator delete(void* p) { ::operator delete(p); } |
| 27 |
void* SkDescriptor::operator new(size_t) { |
| 28 |
SK_ABORT("Descriptors are created with placement new."); |
| 29 |
} |
| 30 |
|
| 31 |
void SkDescriptor::flatten(SkWriteBuffer& buffer) const { |
| 32 |
buffer.writePad32(static_cast<const void*>(this), this->fLength); |
| 33 |
} |
| 34 |
|
| 35 |
void* SkDescriptor::addEntry(uint32_t tag, size_t length, const void* data) { |
| 36 |
SkASSERT(tag); |
| 37 |
SkASSERT(SkIsAlign4(length)); |
| 38 |
SkASSERT(this->findEntry(tag, nullptr) == nullptr); |
| 39 |
|
| 40 |
Entry* entry = (Entry*)((char*)this + fLength); |
| 41 |
entry->fTag = tag; |
| 42 |
entry->fLen = SkToU32(length); |
| 43 |
if (data) { |
| 44 |
memcpy(entry + 1, data, length); |
| 45 |
} |
| 46 |
|
| 47 |
fCount += 1; |
| 48 |
fLength = SkToU32(fLength + sizeof(Entry) + length); |
| 49 |
return (entry + 1); // return its data |
| 50 |
} |
| 51 |
|
| 52 |
void SkDescriptor::computeChecksum() { |
| 53 |
fChecksum = SkDescriptor::ComputeChecksum(this); |
| 54 |
} |
| 55 |
|
| 56 |
const void* SkDescriptor::findEntry(uint32_t tag, uint32_t* length) const { |
| 57 |
const Entry* entry = (const Entry*)(this + 1); |
| 58 |
int count = fCount; |
| 59 |
|
| 60 |
while (--count >= 0) { |
| 61 |
if (entry->fTag == tag) { |
| 62 |
if (length) { |
| 63 |
*length = entry->fLen; |
| 64 |
} |
| 65 |
return entry + 1; |
| 66 |
} |
| 67 |
entry = (const Entry*)((const char*)(entry + 1) + entry->fLen); |
| 68 |
} |
| 69 |
return nullptr; |
| 70 |
} |
| 71 |
|
| 72 |
std::unique_ptr<SkDescriptor> SkDescriptor::copy() const { |
| 73 |
std::unique_ptr<SkDescriptor> desc = SkDescriptor::Alloc(fLength); |
| 74 |
memcpy(desc.get(), this, fLength); |
| 75 |
return desc; |
| 76 |
} |
| 77 |
|
| 78 |
bool SkDescriptor::operator==(const SkDescriptor& other) const { |
| 79 |
// the first value we should look at is the checksum, so this loop |
| 80 |
// should terminate early if they descriptors are different. |
| 81 |
// NOTE: if we wrote a sentinel value at the end of each, we could |
| 82 |
// remove the aa < stop test in the loop... |
| 83 |
const uint32_t* aa = (const uint32_t*)this; |
| 84 |
const uint32_t* bb = (const uint32_t*)&other; |
| 85 |
SkASSERT(SkIsAlign4(fLength)); |
| 86 |
const uint32_t* stop = (const uint32_t*)((const char*)aa + fLength); |
| 87 |
do { |
| 88 |
if (*aa++ != *bb++) |
| 89 |
return false; |
| 90 |
} while (aa < stop); |
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
SkString SkDescriptor::dumpRec() const { |
| 95 |
const SkScalerContextRec* rec = static_cast<const SkScalerContextRec*>( |
| 96 |
this->findEntry(kRec_SkDescriptorTag, nullptr)); |
| 97 |
|
| 98 |
SkString result; |
| 99 |
result.appendf(" Checksum: %x\n", fChecksum); |
| 100 |
if (rec != nullptr) { |
| 101 |
result.append(rec->dump()); |
| 102 |
} |
| 103 |
return result; |
| 104 |
} |
| 105 |
|
| 106 |
uint32_t SkDescriptor::ComputeChecksum(const SkDescriptor* desc) { |
| 107 |
const uint32_t* ptr = (const uint32_t*)desc + 1; // skip the checksum field |
| 108 |
size_t len = desc->fLength - sizeof(uint32_t); |
| 109 |
return SkChecksum::Hash32(ptr, len); |
| 110 |
} |
| 111 |
|
| 112 |
bool SkDescriptor::isValid() const { |
| 113 |
uint32_t count = fCount; |
| 114 |
size_t lengthRemaining = this->fLength; |
| 115 |
if (lengthRemaining < sizeof(SkDescriptor) || !SkIsAlign4(lengthRemaining)) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
lengthRemaining -= sizeof(SkDescriptor); |
| 119 |
size_t offset = sizeof(SkDescriptor); |
| 120 |
|
| 121 |
while (lengthRemaining > 0 && count > 0) { |
| 122 |
if (lengthRemaining < sizeof(Entry)) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
lengthRemaining -= sizeof(Entry); |
| 126 |
|
| 127 |
const Entry* entry = (const Entry*)(reinterpret_cast<const char*>(this) + offset); |
| 128 |
|
| 129 |
if (lengthRemaining < entry->fLen || !SkIsAlign4(entry->fLen)) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
lengthRemaining -= entry->fLen; |
| 133 |
|
| 134 |
// rec tags are always a known size. |
| 135 |
if (entry->fTag == kRec_SkDescriptorTag && entry->fLen != sizeof(SkScalerContextRec)) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
offset += sizeof(Entry) + entry->fLen; |
| 140 |
count--; |
| 141 |
} |
| 142 |
return lengthRemaining == 0 && count == 0; |
| 143 |
} |
| 144 |
|
| 145 |
SkAutoDescriptor::SkAutoDescriptor() = default; |
| 146 |
SkAutoDescriptor::SkAutoDescriptor(size_t size) { this->reset(size); } |
| 147 |
SkAutoDescriptor::SkAutoDescriptor(const SkDescriptor& desc) { this->reset(desc); } |
| 148 |
SkAutoDescriptor::SkAutoDescriptor(const SkAutoDescriptor& that) { |
| 149 |
this->reset(*that.getDesc()); |
| 150 |
} |
| 151 |
SkAutoDescriptor& SkAutoDescriptor::operator=(const SkAutoDescriptor& that) { |
| 152 |
this->reset(*that.getDesc()); |
| 153 |
return *this; |
| 154 |
} |
| 155 |
SkAutoDescriptor::SkAutoDescriptor(SkAutoDescriptor&& that) { |
| 156 |
if (that.fDesc == (SkDescriptor*)&that.fStorage) { |
| 157 |
this->reset(*that.getDesc()); |
| 158 |
} else { |
| 159 |
fDesc = that.fDesc; |
| 160 |
that.fDesc = nullptr; |
| 161 |
} |
| 162 |
} |
| 163 |
SkAutoDescriptor& SkAutoDescriptor::operator=(SkAutoDescriptor&& that) { |
| 164 |
if (that.fDesc == (SkDescriptor*)&that.fStorage) { |
| 165 |
this->reset(*that.getDesc()); |
| 166 |
} else { |
| 167 |
this->free(); |
| 168 |
fDesc = that.fDesc; |
| 169 |
that.fDesc = nullptr; |
| 170 |
} |
| 171 |
return *this; |
| 172 |
} |
| 173 |
|
| 174 |
SkAutoDescriptor::~SkAutoDescriptor() { this->free(); } |
| 175 |
|
| 176 |
std::optional<SkAutoDescriptor> SkAutoDescriptor::MakeFromBuffer(SkReadBuffer& buffer) { |
| 177 |
SkDescriptor descriptorHeader; |
| 178 |
if (!buffer.readPad32(&descriptorHeader, sizeof(SkDescriptor))) { return {}; } |
| 179 |
|
| 180 |
// Basic bounds check on header length to make sure that bodyLength calculation does not |
| 181 |
// underflow. |
| 182 |
if (descriptorHeader.getLength() < sizeof(SkDescriptor)) { return {}; } |
| 183 |
uint32_t bodyLength = descriptorHeader.getLength() - sizeof(SkDescriptor); |
| 184 |
|
| 185 |
// Make sure the fLength makes sense with respect to the incoming data. |
| 186 |
if (bodyLength > buffer.available()) { |
| 187 |
return {}; |
| 188 |
} |
| 189 |
|
| 190 |
SkAutoDescriptor ad{descriptorHeader.getLength()}; |
| 191 |
memcpy(ad.fDesc, &descriptorHeader, sizeof(SkDescriptor)); |
| 192 |
if (!buffer.readPad32(SkTAddOffset<void>(ad.fDesc, sizeof(SkDescriptor)), bodyLength)) { |
| 193 |
return {}; |
| 194 |
} |
| 195 |
|
| 196 |
// If the fuzzer produces data but the checksum does not match, let it continue. This will boost |
| 197 |
// fuzzing speed. We leave the actual checksum computation in for fuzzing builds to make sure |
| 198 |
// the ComputeChecksum function is covered. |
| 199 |
#if defined(SK_BUILD_FOR_FUZZER) |
| 200 |
SkDescriptor::ComputeChecksum(ad.getDesc()); |
| 201 |
#else |
| 202 |
if (SkDescriptor::ComputeChecksum(ad.getDesc()) != ad.getDesc()->fChecksum) { return {}; } |
| 203 |
#endif |
| 204 |
if (!ad.getDesc()->isValid()) { return {}; } |
| 205 |
|
| 206 |
return {ad}; |
| 207 |
} |
| 208 |
|
| 209 |
void SkAutoDescriptor::reset(size_t size) { |
| 210 |
this->free(); |
| 211 |
if (size <= sizeof(fStorage)) { |
| 212 |
fDesc = new (&fStorage) SkDescriptor{}; |
| 213 |
} else { |
| 214 |
fDesc = SkDescriptor::Alloc(size).release(); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
void SkAutoDescriptor::reset(const SkDescriptor& desc) { |
| 219 |
size_t size = desc.getLength(); |
| 220 |
this->reset(size); |
| 221 |
memcpy(fDesc, &desc, size); |
| 222 |
} |
| 223 |
|
| 224 |
void SkAutoDescriptor::free() { |
| 225 |
if (fDesc == (SkDescriptor*)&fStorage) { |
| 226 |
fDesc->~SkDescriptor(); |
| 227 |
} else { |
| 228 |
delete fDesc; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
|