Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef vm_ArrayBufferObject_h |
michael@0 | 8 | #define vm_ArrayBufferObject_h |
michael@0 | 9 | |
michael@0 | 10 | #include "jsobj.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "builtin/TypedObjectConstants.h" |
michael@0 | 13 | #include "vm/Runtime.h" |
michael@0 | 14 | |
michael@0 | 15 | typedef struct JSProperty JSProperty; |
michael@0 | 16 | |
michael@0 | 17 | namespace js { |
michael@0 | 18 | |
michael@0 | 19 | class ArrayBufferViewObject; |
michael@0 | 20 | |
michael@0 | 21 | // The inheritance hierarchy for the various classes relating to typed arrays |
michael@0 | 22 | // is as follows. |
michael@0 | 23 | // |
michael@0 | 24 | // - JSObject |
michael@0 | 25 | // - ArrayBufferObject |
michael@0 | 26 | // - SharedArrayBufferObject |
michael@0 | 27 | // - ArrayBufferViewObject |
michael@0 | 28 | // - DataViewObject |
michael@0 | 29 | // - TypedArrayObject (declared in vm/TypedArrayObject.h) |
michael@0 | 30 | // - TypedArrayObjectTemplate |
michael@0 | 31 | // - Int8ArrayObject |
michael@0 | 32 | // - Uint8ArrayObject |
michael@0 | 33 | // - ... |
michael@0 | 34 | // - TypedObject (declared in builtin/TypedObject.h) |
michael@0 | 35 | // |
michael@0 | 36 | // Note that |TypedArrayObjectTemplate| is just an implementation |
michael@0 | 37 | // detail that makes implementing its various subclasses easier. |
michael@0 | 38 | |
michael@0 | 39 | typedef Vector<ArrayBufferObject *, 0, SystemAllocPolicy> ArrayBufferVector; |
michael@0 | 40 | |
michael@0 | 41 | /* |
michael@0 | 42 | * ArrayBufferObject |
michael@0 | 43 | * |
michael@0 | 44 | * This class holds the underlying raw buffer that the various |
michael@0 | 45 | * ArrayBufferViewObject subclasses (DataViewObject and the TypedArrays) |
michael@0 | 46 | * access. It can be created explicitly and passed to an ArrayBufferViewObject |
michael@0 | 47 | * subclass, or can be created implicitly by constructing a TypedArrayObject |
michael@0 | 48 | * with a size. |
michael@0 | 49 | */ |
michael@0 | 50 | class ArrayBufferObject : public JSObject |
michael@0 | 51 | { |
michael@0 | 52 | static bool byteLengthGetterImpl(JSContext *cx, CallArgs args); |
michael@0 | 53 | static bool fun_slice_impl(JSContext *cx, CallArgs args); |
michael@0 | 54 | |
michael@0 | 55 | public: |
michael@0 | 56 | static const uint8_t DATA_SLOT = 0; |
michael@0 | 57 | static const uint8_t BYTE_LENGTH_SLOT = 1; |
michael@0 | 58 | static const uint8_t VIEW_LIST_SLOT = 2; |
michael@0 | 59 | static const uint8_t FLAGS_SLOT = 3; |
michael@0 | 60 | |
michael@0 | 61 | static const uint8_t RESERVED_SLOTS = 4; |
michael@0 | 62 | |
michael@0 | 63 | static const size_t ARRAY_BUFFER_ALIGNMENT = 8; |
michael@0 | 64 | |
michael@0 | 65 | static const Class class_; |
michael@0 | 66 | |
michael@0 | 67 | static const Class protoClass; |
michael@0 | 68 | static const JSFunctionSpec jsfuncs[]; |
michael@0 | 69 | static const JSFunctionSpec jsstaticfuncs[]; |
michael@0 | 70 | |
michael@0 | 71 | static bool byteLengthGetter(JSContext *cx, unsigned argc, Value *vp); |
michael@0 | 72 | |
michael@0 | 73 | static bool fun_slice(JSContext *cx, unsigned argc, Value *vp); |
michael@0 | 74 | |
michael@0 | 75 | static bool fun_isView(JSContext *cx, unsigned argc, Value *vp); |
michael@0 | 76 | |
michael@0 | 77 | static bool class_constructor(JSContext *cx, unsigned argc, Value *vp); |
michael@0 | 78 | |
michael@0 | 79 | static ArrayBufferObject *create(JSContext *cx, uint32_t nbytes, void *contents = nullptr, |
michael@0 | 80 | NewObjectKind newKind = GenericObject, bool mapped = false); |
michael@0 | 81 | |
michael@0 | 82 | static JSObject *createSlice(JSContext *cx, Handle<ArrayBufferObject*> arrayBuffer, |
michael@0 | 83 | uint32_t begin, uint32_t end); |
michael@0 | 84 | |
michael@0 | 85 | static bool createDataViewForThisImpl(JSContext *cx, CallArgs args); |
michael@0 | 86 | static bool createDataViewForThis(JSContext *cx, unsigned argc, Value *vp); |
michael@0 | 87 | |
michael@0 | 88 | template<typename T> |
michael@0 | 89 | static bool createTypedArrayFromBufferImpl(JSContext *cx, CallArgs args); |
michael@0 | 90 | |
michael@0 | 91 | template<typename T> |
michael@0 | 92 | static bool createTypedArrayFromBuffer(JSContext *cx, unsigned argc, Value *vp); |
michael@0 | 93 | |
michael@0 | 94 | static void obj_trace(JSTracer *trc, JSObject *obj); |
michael@0 | 95 | |
michael@0 | 96 | static void sweep(JSCompartment *rt); |
michael@0 | 97 | |
michael@0 | 98 | static void resetArrayBufferList(JSCompartment *rt); |
michael@0 | 99 | static bool saveArrayBufferList(JSCompartment *c, ArrayBufferVector &vector); |
michael@0 | 100 | static void restoreArrayBufferLists(ArrayBufferVector &vector); |
michael@0 | 101 | |
michael@0 | 102 | static void *stealContents(JSContext *cx, Handle<ArrayBufferObject*> buffer); |
michael@0 | 103 | |
michael@0 | 104 | bool hasStealableContents() const { |
michael@0 | 105 | // Inline elements strictly adhere to the corresponding buffer. |
michael@0 | 106 | if (!ownsData()) |
michael@0 | 107 | return false; |
michael@0 | 108 | |
michael@0 | 109 | // asm.js buffer contents are transferred by copying, just like inline |
michael@0 | 110 | // elements. |
michael@0 | 111 | if (isAsmJSArrayBuffer()) |
michael@0 | 112 | return false; |
michael@0 | 113 | |
michael@0 | 114 | // Neutered contents aren't transferrable because we want a neutered |
michael@0 | 115 | // array's contents to be backed by zeroed memory equal in length to |
michael@0 | 116 | // the original buffer contents. Transferring these contents would |
michael@0 | 117 | // allocate new ones based on the current byteLength, which is 0 for a |
michael@0 | 118 | // neutered array -- not the original byteLength. |
michael@0 | 119 | return !isNeutered(); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | static void addSizeOfExcludingThis(JSObject *obj, mozilla::MallocSizeOf mallocSizeOf, |
michael@0 | 123 | JS::ObjectsExtraSizes *sizes); |
michael@0 | 124 | |
michael@0 | 125 | void addView(ArrayBufferViewObject *view); |
michael@0 | 126 | |
michael@0 | 127 | void setNewOwnedData(FreeOp* fop, void *newData); |
michael@0 | 128 | void changeContents(JSContext *cx, void *newData); |
michael@0 | 129 | |
michael@0 | 130 | /* |
michael@0 | 131 | * Ensure data is not stored inline in the object. Used when handing back a |
michael@0 | 132 | * GC-safe pointer. |
michael@0 | 133 | */ |
michael@0 | 134 | static bool ensureNonInline(JSContext *cx, Handle<ArrayBufferObject*> buffer); |
michael@0 | 135 | |
michael@0 | 136 | bool canNeuter(JSContext *cx); |
michael@0 | 137 | |
michael@0 | 138 | /* Neuter this buffer and all its views. */ |
michael@0 | 139 | static void neuter(JSContext *cx, Handle<ArrayBufferObject*> buffer, void *newData); |
michael@0 | 140 | |
michael@0 | 141 | uint8_t *dataPointer() const; |
michael@0 | 142 | size_t byteLength() const; |
michael@0 | 143 | |
michael@0 | 144 | void releaseData(FreeOp *fop); |
michael@0 | 145 | |
michael@0 | 146 | /* |
michael@0 | 147 | * Check if the arrayBuffer contains any data. This will return false for |
michael@0 | 148 | * ArrayBuffer.prototype and neutered ArrayBuffers. |
michael@0 | 149 | */ |
michael@0 | 150 | bool hasData() const { |
michael@0 | 151 | return getClass() == &class_; |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | bool isAsmJSArrayBuffer() const { return flags() & ASMJS_BUFFER; } |
michael@0 | 155 | bool isSharedArrayBuffer() const { return flags() & SHARED_BUFFER; } |
michael@0 | 156 | bool isMappedArrayBuffer() const { return flags() & MAPPED_BUFFER; } |
michael@0 | 157 | bool isNeutered() const { return flags() & NEUTERED_BUFFER; } |
michael@0 | 158 | |
michael@0 | 159 | static bool prepareForAsmJS(JSContext *cx, Handle<ArrayBufferObject*> buffer); |
michael@0 | 160 | static bool canNeuterAsmJSArrayBuffer(JSContext *cx, ArrayBufferObject &buffer); |
michael@0 | 161 | |
michael@0 | 162 | static void finalize(FreeOp *fop, JSObject *obj); |
michael@0 | 163 | |
michael@0 | 164 | static void *createMappedContents(int fd, size_t offset, size_t length); |
michael@0 | 165 | |
michael@0 | 166 | static size_t flagsOffset() { |
michael@0 | 167 | return getFixedSlotOffset(FLAGS_SLOT); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | static uint32_t neuteredFlag() { return NEUTERED_BUFFER; } |
michael@0 | 171 | |
michael@0 | 172 | protected: |
michael@0 | 173 | enum OwnsState { |
michael@0 | 174 | DoesntOwnData = 0, |
michael@0 | 175 | OwnsData = 1, |
michael@0 | 176 | }; |
michael@0 | 177 | |
michael@0 | 178 | void setDataPointer(void *data, OwnsState ownsState); |
michael@0 | 179 | void setByteLength(size_t length); |
michael@0 | 180 | |
michael@0 | 181 | ArrayBufferViewObject *viewList() const; |
michael@0 | 182 | void setViewList(ArrayBufferViewObject *viewsHead); |
michael@0 | 183 | void setViewListNoBarrier(ArrayBufferViewObject *viewsHead); |
michael@0 | 184 | |
michael@0 | 185 | enum ArrayBufferFlags { |
michael@0 | 186 | // In the gcLiveArrayBuffers list. |
michael@0 | 187 | IN_LIVE_LIST = 0x1, |
michael@0 | 188 | |
michael@0 | 189 | // The dataPointer() is owned by this buffer and should be released |
michael@0 | 190 | // when no longer in use. Releasing the pointer may be done by either |
michael@0 | 191 | // freeing or unmapping it, and how to do this is determined by the |
michael@0 | 192 | // buffer's other flags. |
michael@0 | 193 | OWNS_DATA = 0x2, |
michael@0 | 194 | |
michael@0 | 195 | ASMJS_BUFFER = 0x4, |
michael@0 | 196 | SHARED_BUFFER = 0x8, |
michael@0 | 197 | MAPPED_BUFFER = 0x10, |
michael@0 | 198 | NEUTERED_BUFFER = 0x20 |
michael@0 | 199 | }; |
michael@0 | 200 | |
michael@0 | 201 | uint32_t flags() const; |
michael@0 | 202 | void setFlags(uint32_t flags); |
michael@0 | 203 | |
michael@0 | 204 | bool inLiveList() const { return flags() & IN_LIVE_LIST; } |
michael@0 | 205 | void setInLiveList(bool value) { |
michael@0 | 206 | setFlags(value ? (flags() | IN_LIVE_LIST) : (flags() & ~IN_LIVE_LIST)); |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | bool ownsData() const { return flags() & OWNS_DATA; } |
michael@0 | 210 | void setOwnsData(OwnsState owns) { |
michael@0 | 211 | setFlags(owns ? (flags() | OWNS_DATA) : (flags() & ~OWNS_DATA)); |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | void setIsAsmJSArrayBuffer() { setFlags(flags() | ASMJS_BUFFER); } |
michael@0 | 215 | void setIsSharedArrayBuffer() { setFlags(flags() | SHARED_BUFFER); } |
michael@0 | 216 | void setIsMappedArrayBuffer() { setFlags(flags() | MAPPED_BUFFER); } |
michael@0 | 217 | void setIsNeutered() { setFlags(flags() | NEUTERED_BUFFER); } |
michael@0 | 218 | |
michael@0 | 219 | void initialize(size_t byteLength, void *data, OwnsState ownsState) { |
michael@0 | 220 | setByteLength(byteLength); |
michael@0 | 221 | setFlags(0); |
michael@0 | 222 | setViewListNoBarrier(nullptr); |
michael@0 | 223 | setDataPointer(data, ownsState); |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | void releaseAsmJSArray(FreeOp *fop); |
michael@0 | 227 | void releaseMappedArray(); |
michael@0 | 228 | }; |
michael@0 | 229 | |
michael@0 | 230 | /* |
michael@0 | 231 | * ArrayBufferViewObject |
michael@0 | 232 | * |
michael@0 | 233 | * Common definitions shared by all ArrayBufferViews. |
michael@0 | 234 | */ |
michael@0 | 235 | |
michael@0 | 236 | class ArrayBufferViewObject : public JSObject |
michael@0 | 237 | { |
michael@0 | 238 | protected: |
michael@0 | 239 | /* Offset of view in underlying ArrayBufferObject */ |
michael@0 | 240 | static const size_t BYTEOFFSET_SLOT = JS_TYPEDOBJ_SLOT_BYTEOFFSET; |
michael@0 | 241 | |
michael@0 | 242 | /* Byte length of view */ |
michael@0 | 243 | static const size_t BYTELENGTH_SLOT = JS_TYPEDOBJ_SLOT_BYTELENGTH; |
michael@0 | 244 | |
michael@0 | 245 | /* Underlying ArrayBufferObject */ |
michael@0 | 246 | static const size_t BUFFER_SLOT = JS_TYPEDOBJ_SLOT_OWNER; |
michael@0 | 247 | |
michael@0 | 248 | /* ArrayBufferObjects point to a linked list of views, chained through this slot */ |
michael@0 | 249 | static const size_t NEXT_VIEW_SLOT = JS_TYPEDOBJ_SLOT_NEXT_VIEW; |
michael@0 | 250 | |
michael@0 | 251 | public: |
michael@0 | 252 | static ArrayBufferObject *bufferObject(JSContext *cx, Handle<ArrayBufferViewObject *> obj); |
michael@0 | 253 | |
michael@0 | 254 | ArrayBufferViewObject *nextView() const { |
michael@0 | 255 | return static_cast<ArrayBufferViewObject*>(getFixedSlot(NEXT_VIEW_SLOT).toPrivate()); |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | inline void setNextView(ArrayBufferViewObject *view); |
michael@0 | 259 | |
michael@0 | 260 | void neuter(void *newData); |
michael@0 | 261 | |
michael@0 | 262 | static void trace(JSTracer *trc, JSObject *obj); |
michael@0 | 263 | |
michael@0 | 264 | uint8_t *dataPointer() { |
michael@0 | 265 | return static_cast<uint8_t *>(getPrivate()); |
michael@0 | 266 | } |
michael@0 | 267 | }; |
michael@0 | 268 | |
michael@0 | 269 | bool |
michael@0 | 270 | ToClampedIndex(JSContext *cx, HandleValue v, uint32_t length, uint32_t *out); |
michael@0 | 271 | |
michael@0 | 272 | inline void |
michael@0 | 273 | PostBarrierTypedArrayObject(JSObject *obj) |
michael@0 | 274 | { |
michael@0 | 275 | #ifdef JSGC_GENERATIONAL |
michael@0 | 276 | JS_ASSERT(obj); |
michael@0 | 277 | JSRuntime *rt = obj->runtimeFromMainThread(); |
michael@0 | 278 | if (!rt->isHeapBusy() && !IsInsideNursery(rt, obj)) |
michael@0 | 279 | rt->gcStoreBuffer.putWholeCell(obj); |
michael@0 | 280 | #endif |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | inline void |
michael@0 | 284 | InitArrayBufferViewDataPointer(ArrayBufferViewObject *obj, ArrayBufferObject *buffer, size_t byteOffset) |
michael@0 | 285 | { |
michael@0 | 286 | /* |
michael@0 | 287 | * N.B. The base of the array's data is stored in the object's |
michael@0 | 288 | * private data rather than a slot to avoid alignment restrictions |
michael@0 | 289 | * on private Values. |
michael@0 | 290 | */ |
michael@0 | 291 | MOZ_ASSERT(buffer->dataPointer() != nullptr); |
michael@0 | 292 | obj->initPrivate(buffer->dataPointer() + byteOffset); |
michael@0 | 293 | |
michael@0 | 294 | PostBarrierTypedArrayObject(obj); |
michael@0 | 295 | } |
michael@0 | 296 | |
michael@0 | 297 | /* |
michael@0 | 298 | * Tests for either ArrayBufferObject or SharedArrayBufferObject. |
michael@0 | 299 | * For specific class testing, use e.g., obj->is<ArrayBufferObject>(). |
michael@0 | 300 | */ |
michael@0 | 301 | bool IsArrayBuffer(HandleValue v); |
michael@0 | 302 | bool IsArrayBuffer(HandleObject obj); |
michael@0 | 303 | bool IsArrayBuffer(JSObject *obj); |
michael@0 | 304 | ArrayBufferObject &AsArrayBuffer(HandleObject obj); |
michael@0 | 305 | ArrayBufferObject &AsArrayBuffer(JSObject *obj); |
michael@0 | 306 | |
michael@0 | 307 | inline void |
michael@0 | 308 | ArrayBufferViewObject::setNextView(ArrayBufferViewObject *view) |
michael@0 | 309 | { |
michael@0 | 310 | setFixedSlot(NEXT_VIEW_SLOT, PrivateValue(view)); |
michael@0 | 311 | PostBarrierTypedArrayObject(this); |
michael@0 | 312 | } |
michael@0 | 313 | |
michael@0 | 314 | extern uint32_t JS_FASTCALL |
michael@0 | 315 | ClampDoubleToUint8(const double x); |
michael@0 | 316 | |
michael@0 | 317 | struct uint8_clamped { |
michael@0 | 318 | uint8_t val; |
michael@0 | 319 | |
michael@0 | 320 | uint8_clamped() { } |
michael@0 | 321 | uint8_clamped(const uint8_clamped& other) : val(other.val) { } |
michael@0 | 322 | |
michael@0 | 323 | // invoke our assignment helpers for constructor conversion |
michael@0 | 324 | uint8_clamped(uint8_t x) { *this = x; } |
michael@0 | 325 | uint8_clamped(uint16_t x) { *this = x; } |
michael@0 | 326 | uint8_clamped(uint32_t x) { *this = x; } |
michael@0 | 327 | uint8_clamped(int8_t x) { *this = x; } |
michael@0 | 328 | uint8_clamped(int16_t x) { *this = x; } |
michael@0 | 329 | uint8_clamped(int32_t x) { *this = x; } |
michael@0 | 330 | uint8_clamped(double x) { *this = x; } |
michael@0 | 331 | |
michael@0 | 332 | uint8_clamped& operator=(const uint8_clamped& x) { |
michael@0 | 333 | val = x.val; |
michael@0 | 334 | return *this; |
michael@0 | 335 | } |
michael@0 | 336 | |
michael@0 | 337 | uint8_clamped& operator=(uint8_t x) { |
michael@0 | 338 | val = x; |
michael@0 | 339 | return *this; |
michael@0 | 340 | } |
michael@0 | 341 | |
michael@0 | 342 | uint8_clamped& operator=(uint16_t x) { |
michael@0 | 343 | val = (x > 255) ? 255 : uint8_t(x); |
michael@0 | 344 | return *this; |
michael@0 | 345 | } |
michael@0 | 346 | |
michael@0 | 347 | uint8_clamped& operator=(uint32_t x) { |
michael@0 | 348 | val = (x > 255) ? 255 : uint8_t(x); |
michael@0 | 349 | return *this; |
michael@0 | 350 | } |
michael@0 | 351 | |
michael@0 | 352 | uint8_clamped& operator=(int8_t x) { |
michael@0 | 353 | val = (x >= 0) ? uint8_t(x) : 0; |
michael@0 | 354 | return *this; |
michael@0 | 355 | } |
michael@0 | 356 | |
michael@0 | 357 | uint8_clamped& operator=(int16_t x) { |
michael@0 | 358 | val = (x >= 0) |
michael@0 | 359 | ? ((x < 255) |
michael@0 | 360 | ? uint8_t(x) |
michael@0 | 361 | : 255) |
michael@0 | 362 | : 0; |
michael@0 | 363 | return *this; |
michael@0 | 364 | } |
michael@0 | 365 | |
michael@0 | 366 | uint8_clamped& operator=(int32_t x) { |
michael@0 | 367 | val = (x >= 0) |
michael@0 | 368 | ? ((x < 255) |
michael@0 | 369 | ? uint8_t(x) |
michael@0 | 370 | : 255) |
michael@0 | 371 | : 0; |
michael@0 | 372 | return *this; |
michael@0 | 373 | } |
michael@0 | 374 | |
michael@0 | 375 | uint8_clamped& operator=(const double x) { |
michael@0 | 376 | val = uint8_t(ClampDoubleToUint8(x)); |
michael@0 | 377 | return *this; |
michael@0 | 378 | } |
michael@0 | 379 | |
michael@0 | 380 | operator uint8_t() const { |
michael@0 | 381 | return val; |
michael@0 | 382 | } |
michael@0 | 383 | |
michael@0 | 384 | void staticAsserts() { |
michael@0 | 385 | static_assert(sizeof(uint8_clamped) == 1, |
michael@0 | 386 | "uint8_clamped must be layout-compatible with uint8_t"); |
michael@0 | 387 | } |
michael@0 | 388 | }; |
michael@0 | 389 | |
michael@0 | 390 | /* Note that we can't use std::numeric_limits here due to uint8_clamped. */ |
michael@0 | 391 | template<typename T> inline const bool TypeIsFloatingPoint() { return false; } |
michael@0 | 392 | template<> inline const bool TypeIsFloatingPoint<float>() { return true; } |
michael@0 | 393 | template<> inline const bool TypeIsFloatingPoint<double>() { return true; } |
michael@0 | 394 | |
michael@0 | 395 | template<typename T> inline const bool TypeIsUnsigned() { return false; } |
michael@0 | 396 | template<> inline const bool TypeIsUnsigned<uint8_t>() { return true; } |
michael@0 | 397 | template<> inline const bool TypeIsUnsigned<uint16_t>() { return true; } |
michael@0 | 398 | template<> inline const bool TypeIsUnsigned<uint32_t>() { return true; } |
michael@0 | 399 | |
michael@0 | 400 | } // namespace js |
michael@0 | 401 | |
michael@0 | 402 | #endif // vm_ArrayBufferObject_h |