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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
/*
* Copyright (C) 2009, 2013, 2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "ArrayBuffer.h"
#include "ArrayBufferNeuteringWatchpoint.h"
#include "JSArrayBufferView.h"
#include "JSCInlines.h"
namespace JSC {
SharedArrayBufferContents::SharedArrayBufferContents(void* data, ArrayBufferDestructorFunction&& destructor)
: m_data(data)
, m_destructor(WTFMove(destructor))
{
}
SharedArrayBufferContents::~SharedArrayBufferContents()
{
m_destructor(m_data);
}
ArrayBufferContents::ArrayBufferContents()
{
reset();
}
ArrayBufferContents::ArrayBufferContents(ArrayBufferContents&& other)
{
reset();
other.transferTo(*this);
}
ArrayBufferContents::ArrayBufferContents(void* data, unsigned sizeInBytes, ArrayBufferDestructorFunction&& destructor)
: m_data(data)
, m_sizeInBytes(sizeInBytes)
{
m_destructor = WTFMove(destructor);
}
ArrayBufferContents& ArrayBufferContents::operator=(ArrayBufferContents&& other)
{
other.transferTo(*this);
return *this;
}
ArrayBufferContents::~ArrayBufferContents()
{
destroy();
}
void ArrayBufferContents::clear()
{
destroy();
reset();
}
void ArrayBufferContents::destroy()
{
m_destructor(m_data);
}
void ArrayBufferContents::reset()
{
m_destructor = [] (void*) { };
m_shared = nullptr;
m_data = nullptr;
m_sizeInBytes = 0;
}
void ArrayBufferContents::tryAllocate(unsigned numElements, unsigned elementByteSize, InitializationPolicy policy)
{
// Do not allow 31-bit overflow of the total size.
if (numElements) {
unsigned totalSize = numElements * elementByteSize;
if (totalSize / numElements != elementByteSize
|| totalSize > static_cast<unsigned>(std::numeric_limits<int32_t>::max())) {
reset();
return;
}
}
bool allocationSucceeded = false;
if (policy == ZeroInitialize)
allocationSucceeded = WTF::tryFastCalloc(numElements, elementByteSize).getValue(m_data);
else {
ASSERT(policy == DontInitialize);
allocationSucceeded = WTF::tryFastMalloc(numElements * elementByteSize).getValue(m_data);
}
if (allocationSucceeded) {
m_sizeInBytes = numElements * elementByteSize;
m_destructor = [] (void* p) { fastFree(p); };
return;
}
reset();
}
void ArrayBufferContents::makeShared()
{
m_shared = adoptRef(new SharedArrayBufferContents(m_data, WTFMove(m_destructor)));
m_destructor = [] (void*) { };
}
void ArrayBufferContents::transferTo(ArrayBufferContents& other)
{
other.clear();
other.m_data = m_data;
other.m_sizeInBytes = m_sizeInBytes;
other.m_destructor = WTFMove(m_destructor);
other.m_shared = m_shared;
clear();
}
void ArrayBufferContents::copyTo(ArrayBufferContents& other)
{
ASSERT(!other.m_data);
other.tryAllocate(m_sizeInBytes, sizeof(char), ArrayBufferContents::DontInitialize);
if (!other.m_data)
return;
memcpy(other.m_data, m_data, m_sizeInBytes);
other.m_sizeInBytes = m_sizeInBytes;
}
void ArrayBufferContents::shareWith(ArrayBufferContents& other)
{
ASSERT(!other.m_data);
ASSERT(m_shared);
other.m_destructor = [] (void*) { };
other.m_shared = m_shared;
other.m_data = m_data;
other.m_sizeInBytes = m_sizeInBytes;
}
Ref<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned elementByteSize)
{
auto buffer = tryCreate(numElements, elementByteSize);
if (!buffer)
CRASH();
return buffer.releaseNonNull();
}
Ref<ArrayBuffer> ArrayBuffer::create(ArrayBuffer& other)
{
return ArrayBuffer::create(other.data(), other.byteLength());
}
Ref<ArrayBuffer> ArrayBuffer::create(const void* source, unsigned byteLength)
{
auto buffer = tryCreate(source, byteLength);
if (!buffer)
CRASH();
return buffer.releaseNonNull();
}
Ref<ArrayBuffer> ArrayBuffer::create(ArrayBufferContents&& contents)
{
return adoptRef(*new ArrayBuffer(WTFMove(contents)));
}
Ref<ArrayBuffer> ArrayBuffer::createAdopted(const void* data, unsigned byteLength)
{
return createFromBytes(data, byteLength, [] (void* p) { fastFree(p); });
}
Ref<ArrayBuffer> ArrayBuffer::createFromBytes(const void* data, unsigned byteLength, ArrayBufferDestructorFunction&& destructor)
{
ArrayBufferContents contents(const_cast<void*>(data), byteLength, WTFMove(destructor));
return create(WTFMove(contents));
}
RefPtr<ArrayBuffer> ArrayBuffer::tryCreate(unsigned numElements, unsigned elementByteSize)
{
return tryCreate(numElements, elementByteSize, ArrayBufferContents::ZeroInitialize);
}
RefPtr<ArrayBuffer> ArrayBuffer::tryCreate(ArrayBuffer& other)
{
return tryCreate(other.data(), other.byteLength());
}
RefPtr<ArrayBuffer> ArrayBuffer::tryCreate(const void* source, unsigned byteLength)
{
ArrayBufferContents contents;
contents.tryAllocate(byteLength, 1, ArrayBufferContents::ZeroInitialize);
if (!contents.m_data)
return nullptr;
return createInternal(WTFMove(contents), source, byteLength);
}
Ref<ArrayBuffer> ArrayBuffer::createUninitialized(unsigned numElements, unsigned elementByteSize)
{
return create(numElements, elementByteSize, ArrayBufferContents::DontInitialize);
}
RefPtr<ArrayBuffer> ArrayBuffer::tryCreateUninitialized(unsigned numElements, unsigned elementByteSize)
{
return tryCreate(numElements, elementByteSize, ArrayBufferContents::DontInitialize);
}
Ref<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy)
{
auto buffer = tryCreate(numElements, elementByteSize, policy);
if (!buffer)
CRASH();
return buffer.releaseNonNull();
}
Ref<ArrayBuffer> ArrayBuffer::createInternal(ArrayBufferContents&& contents, const void* source, unsigned byteLength)
{
ASSERT(!byteLength || source);
auto buffer = adoptRef(*new ArrayBuffer(WTFMove(contents)));
memcpy(buffer->data(), source, byteLength);
return buffer;
}
RefPtr<ArrayBuffer> ArrayBuffer::tryCreate(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy)
{
ArrayBufferContents contents;
contents.tryAllocate(numElements, elementByteSize, policy);
if (!contents.m_data)
return nullptr;
return adoptRef(*new ArrayBuffer(WTFMove(contents)));
}
ArrayBuffer::ArrayBuffer(ArrayBufferContents&& contents)
: m_contents(WTFMove(contents))
, m_pinCount(0)
, m_locked(false)
{
}
RefPtr<ArrayBuffer> ArrayBuffer::slice(int begin, int end) const
{
return sliceImpl(clampIndex(begin), clampIndex(end));
}
RefPtr<ArrayBuffer> ArrayBuffer::slice(int begin) const
{
return sliceImpl(clampIndex(begin), byteLength());
}
RefPtr<ArrayBuffer> ArrayBuffer::sliceImpl(unsigned begin, unsigned end) const
{
unsigned size = begin <= end ? end - begin : 0;
RefPtr<ArrayBuffer> result = ArrayBuffer::create(static_cast<const char*>(data()) + begin, size);
result->setSharingMode(sharingMode());
return result;
}
void ArrayBuffer::makeShared()
{
m_contents.makeShared();
}
void ArrayBuffer::setSharingMode(ArrayBufferSharingMode newSharingMode)
{
if (newSharingMode == sharingMode())
return;
RELEASE_ASSERT(!isShared()); // Cannot revert sharing.
RELEASE_ASSERT(newSharingMode == ArrayBufferSharingMode::Shared);
makeShared();
}
bool ArrayBuffer::shareWith(ArrayBufferContents& result)
{
if (!m_contents.m_data || !isShared()) {
result.m_data = nullptr;
return false;
}
m_contents.shareWith(result);
return true;
}
bool ArrayBuffer::transferTo(VM& vm, ArrayBufferContents& result)
{
Ref<ArrayBuffer> protect(*this);
if (!m_contents.m_data) {
result.m_data = 0;
return false;
}
if (isShared()) {
m_contents.shareWith(result);
return true;
}
bool isNeuterable = !m_pinCount && !m_locked;
if (!isNeuterable) {
m_contents.copyTo(result);
if (!result.m_data)
return false;
return true;
}
m_contents.transferTo(result);
for (size_t i = numberOfIncomingReferences(); i--;) {
JSCell* cell = incomingReferenceAt(i);
if (JSArrayBufferView* view = jsDynamicCast<JSArrayBufferView*>(vm, cell))
view->neuter();
else if (ArrayBufferNeuteringWatchpoint* watchpoint = jsDynamicCast<ArrayBufferNeuteringWatchpoint*>(vm, cell))
watchpoint->fireAll();
}
return true;
}
} // namespace JSC
|