IntlObject uses JSArray::tryCreateUninitialized in an unsafe way
authormsaboff@apple.com <msaboff@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 23 Jan 2017 18:45:17 +0000 (18:45 +0000)
committermsaboff@apple.com <msaboff@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 23 Jan 2017 18:45:17 +0000 (18:45 +0000)
https://bugs.webkit.org/show_bug.cgi?id=167288

Reviewed by Filip Pizlo.

Refactored the following "create" methods into a "tryCreate" method and a
"create" wrapper: JSArray::create(), Butterfly::create() and
createArrayButterfly().

Changed IntlObject.cpp to use JSArray::tryCreate() as it is simpler to use
by not requiring the caller to be GC savey.  The performance benefits of
tryCreateUninitialized() are not needed by the IntlObject c++ code.

Did not add a new test as the bug caused LayoutTests/js/intl.html to fail
reliably with the JSC option values scribbleFreeCells=true,
collectContinuously=true and JSC_useGenerationalGC=false.

* runtime/Butterfly.h:
* runtime/ButterflyInlines.h:
(JSC::Butterfly::tryCreate): Added.
(JSC::Butterfly::create):
* runtime/IntlObject.cpp:
(JSC::canonicalizeLocaleList):
(JSC::lookupSupportedLocales):
(JSC::intlObjectFuncGetCanonicalLocales):
* runtime/JSArray.h:
(JSC::createContiguousArrayButterfly): Deleted.
(JSC::tryCreateArrayButterfly): Added.
(JSC::createArrayButterfly):
(JSC::JSArray::tryCreate): Added.
(JSC::JSArray::create):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@211043 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/runtime/Butterfly.h
Source/JavaScriptCore/runtime/ButterflyInlines.h
Source/JavaScriptCore/runtime/IntlObject.cpp
Source/JavaScriptCore/runtime/JSArray.h

index 6eade96..99681c5 100644 (file)
@@ -1,3 +1,37 @@
+2017-01-23  Michael Saboff  <msaboff@apple.com>
+
+        IntlObject uses JSArray::tryCreateUninitialized in an unsafe way
+        https://bugs.webkit.org/show_bug.cgi?id=167288
+
+        Reviewed by Filip Pizlo.
+
+        Refactored the following "create" methods into a "tryCreate" method and a
+        "create" wrapper: JSArray::create(), Butterfly::create() and
+        createArrayButterfly().
+
+        Changed IntlObject.cpp to use JSArray::tryCreate() as it is simpler to use
+        by not requiring the caller to be GC savey.  The performance benefits of
+        tryCreateUninitialized() are not needed by the IntlObject c++ code.
+
+        Did not add a new test as the bug caused LayoutTests/js/intl.html to fail
+        reliably with the JSC option values scribbleFreeCells=true,
+        collectContinuously=true and JSC_useGenerationalGC=false.
+
+        * runtime/Butterfly.h:
+        * runtime/ButterflyInlines.h:
+        (JSC::Butterfly::tryCreate): Added.
+        (JSC::Butterfly::create):
+        * runtime/IntlObject.cpp:
+        (JSC::canonicalizeLocaleList):
+        (JSC::lookupSupportedLocales):
+        (JSC::intlObjectFuncGetCanonicalLocales):
+        * runtime/JSArray.h:
+        (JSC::createContiguousArrayButterfly): Deleted.
+        (JSC::tryCreateArrayButterfly): Added.
+        (JSC::createArrayButterfly):
+        (JSC::JSArray::tryCreate): Added.
+        (JSC::JSArray::create):
+
 2017-01-23  Joseph Pecoraro  <pecoraro@apple.com>
 
         JavaScriptCore has a weak external symbol in it
index 569fdc2..8ed39b0 100644 (file)
@@ -111,6 +111,7 @@ public:
     
     static Butterfly* createUninitialized(VM&, JSCell* intendedOwner, size_t preCapacity, size_t propertyCapacity, bool hasIndexingHeader, size_t indexingPayloadSizeInBytes);
 
+    static Butterfly* tryCreate(VM& vm, JSCell*, size_t preCapacity, size_t propertyCapacity, bool hasIndexingHeader, const IndexingHeader& indexingHeader, size_t indexingPayloadSizeInBytes);
     static Butterfly* create(VM&, JSCell* intendedOwner, size_t preCapacity, size_t propertyCapacity, bool hasIndexingHeader, const IndexingHeader&, size_t indexingPayloadSizeInBytes);
     static Butterfly* create(VM&, JSCell* intendedOwner, Structure*);
     
index db1b3cf..88c6f8a 100644 (file)
@@ -67,17 +67,27 @@ inline Butterfly* Butterfly::createUninitialized(VM& vm, JSCell*, size_t preCapa
     return result;
 }
 
-inline Butterfly* Butterfly::create(VM& vm, JSCell* intendedOwner, size_t preCapacity, size_t propertyCapacity, bool hasIndexingHeader, const IndexingHeader& indexingHeader, size_t indexingPayloadSizeInBytes)
+inline Butterfly* Butterfly::tryCreate(VM& vm, JSCell*, size_t preCapacity, size_t propertyCapacity, bool hasIndexingHeader, const IndexingHeader& indexingHeader, size_t indexingPayloadSizeInBytes)
 {
-    Butterfly* result = createUninitialized(
-        vm, intendedOwner, preCapacity, propertyCapacity, hasIndexingHeader,
-        indexingPayloadSizeInBytes);
+    size_t size = totalSize(preCapacity, propertyCapacity, hasIndexingHeader, indexingPayloadSizeInBytes);
+    void* base = vm.auxiliarySpace.tryAllocate(size);
+    if (!base)
+        return nullptr;
+    Butterfly* result = fromBase(base, preCapacity, propertyCapacity);
     if (hasIndexingHeader)
         *result->indexingHeader() = indexingHeader;
     memset(result->propertyStorage() - propertyCapacity, 0, propertyCapacity * sizeof(EncodedJSValue));
     return result;
 }
 
+inline Butterfly* Butterfly::create(VM& vm, JSCell* intendedOwner, size_t preCapacity, size_t propertyCapacity, bool hasIndexingHeader, const IndexingHeader& indexingHeader, size_t indexingPayloadSizeInBytes)
+{
+    Butterfly* result = tryCreate(vm, intendedOwner, preCapacity, propertyCapacity, hasIndexingHeader, indexingHeader, indexingPayloadSizeInBytes);
+
+    RELEASE_ASSERT(result);
+    return result;
+}
+
 inline Butterfly* Butterfly::create(VM& vm, JSCell* intendedOwner, Structure* structure)
 {
     return create(
index ba58c14..efd07dd 100644 (file)
@@ -548,7 +548,12 @@ Vector<String> canonicalizeLocaleList(ExecState& state, JSValue locales)
     JSObject* localesObject;
     if (locales.isString()) {
         //  a. Let aLocales be CreateArrayFromList(Ā«localesĀ»).
-        JSArray* localesArray = JSArray::tryCreateUninitialized(vm, globalObject->arrayStructureForIndexingTypeDuringAllocation(ArrayWithContiguous), 1);
+        JSArray* localesArray = JSArray::tryCreate(vm, globalObject->arrayStructureForIndexingTypeDuringAllocation(ArrayWithContiguous), 1);
+        if (!localesArray) {
+            throwOutOfMemoryError(&state, scope);
+            RETURN_IF_EXCEPTION(scope, Vector<String>());
+        }
+
         localesArray->initializeIndex(vm, 0, locales);
         // 4. Let O be ToObject(aLocales).
         localesObject = localesArray;
@@ -887,7 +892,7 @@ static JSArray* lookupSupportedLocales(ExecState& state, const HashSet<String>&
 
     // 3. Let subset be an empty List.
     JSGlobalObject* globalObject = state.jsCallee()->globalObject();
-    JSArray* subset = JSArray::tryCreateUninitialized(vm, globalObject->arrayStructureForIndexingTypeDuringAllocation(ArrayWithUndecided), 0);
+    JSArray* subset = JSArray::tryCreate(vm, globalObject->arrayStructureForIndexingTypeDuringAllocation(ArrayWithUndecided), 0);
     if (!subset) {
         throwOutOfMemoryError(&state, scope);
         return nullptr;
@@ -1031,7 +1036,7 @@ EncodedJSValue JSC_HOST_CALL intlObjectFuncGetCanonicalLocales(ExecState* state)
 
     // 2. Return CreateArrayFromList(ll).
     JSGlobalObject* globalObject = state->jsCallee()->globalObject();
-    JSArray* localeArray = JSArray::tryCreateUninitialized(vm, globalObject->arrayStructureForIndexingTypeDuringAllocation(ArrayWithContiguous), localeList.size());
+    JSArray* localeArray = JSArray::tryCreate(vm, globalObject->arrayStructureForIndexingTypeDuringAllocation(ArrayWithContiguous), localeList.size());
     if (!localeArray) {
         throwOutOfMemoryError(state, scope);
         return encodedJSValue();
index 1631dd2..06f1fc7 100644 (file)
@@ -52,13 +52,18 @@ protected:
     }
 
 public:
+    static JSArray* tryCreate(VM&, Structure*, unsigned initialLength = 0);
     static JSArray* create(VM&, Structure*, unsigned initialLength = 0);
     static JSArray* createWithButterfly(VM&, GCDeferralContext*, Structure*, Butterfly*);
 
     // tryCreateUninitialized is used for fast construction of arrays whose size and
-    // contents are known at time of creation. Clients of this interface must:
+    // contents are known at time of creation. This should be considered a private API.
+    // Clients of this interface must:
     //   - null-check the result (indicating out of memory, or otherwise unable to allocate vector).
     //   - call 'initializeIndex' for all properties in sequence, for 0 <= i < initialLength.
+    //   - Provide a valid GCDefferalContext* if they might garbage collect when initializing properties,
+    //     otherwise the caller can provide a null GCDefferalContext*.
+    //
     JS_EXPORT_PRIVATE static JSArray* tryCreateUninitialized(VM&, GCDeferralContext*, Structure*, unsigned initialLength);
     static JSArray* tryCreateUninitialized(VM& vm, Structure* structure, unsigned initialLength)
     {
@@ -183,23 +188,13 @@ private:
     void setLengthWritable(ExecState*, bool writable);
 };
 
-inline Butterfly* createContiguousArrayButterfly(VM& vm, JSCell* intendedOwner, unsigned length, unsigned& vectorLength)
+inline Butterfly* tryCreateArrayButterfly(VM& vm, JSCell* intendedOwner, unsigned initialLength)
 {
-    IndexingHeader header;
-    vectorLength = Butterfly::optimalContiguousVectorLength(
-        intendedOwner ? intendedOwner->structure(vm) : 0, length);
-    header.setVectorLength(vectorLength);
-    header.setPublicLength(length);
-    Butterfly* result = Butterfly::create(
-        vm, intendedOwner, 0, 0, true, header, vectorLength * sizeof(EncodedJSValue));
-    return result;
-}
-
-inline Butterfly* createArrayButterfly(VM& vm, JSCell* intendedOwner, unsigned initialLength)
-{
-    Butterfly* butterfly = Butterfly::create(
+    Butterfly* butterfly = Butterfly::tryCreate(
         vm, intendedOwner, 0, 0, true, baseIndexingHeaderForArrayStorage(initialLength),
         ArrayStorage::sizeFor(BASE_ARRAY_STORAGE_VECTOR_LEN));
+    if (!butterfly)
+        return nullptr;
     ArrayStorage* storage = butterfly->arrayStorage();
     storage->m_sparseMap.clear();
     storage->m_indexBias = 0;
@@ -207,29 +202,50 @@ inline Butterfly* createArrayButterfly(VM& vm, JSCell* intendedOwner, unsigned i
     return butterfly;
 }
 
+inline Butterfly* createArrayButterfly(VM& vm, JSCell* intendedOwner, unsigned initialLength)
+{
+    Butterfly* result = tryCreateArrayButterfly(vm, intendedOwner, initialLength);
+    RELEASE_ASSERT(result);
+    return result;
+}
+
 Butterfly* createArrayButterflyInDictionaryIndexingMode(
     VM&, JSCell* intendedOwner, unsigned initialLength);
 
-inline JSArray* JSArray::create(VM& vm, Structure* structure, unsigned initialLength)
+inline JSArray* JSArray::tryCreate(VM& vm, Structure* structure, unsigned initialLength)
 {
+    unsigned outOfLineStorage = structure->outOfLineCapacity();
+
     Butterfly* butterfly;
-    if (LIKELY(!hasAnyArrayStorage(structure->indexingType()))) {
+    IndexingType indexingType = structure->indexingType();
+    if (LIKELY(!hasAnyArrayStorage(indexingType))) {
         ASSERT(
-            hasUndecided(structure->indexingType())
-            || hasInt32(structure->indexingType())
-            || hasDouble(structure->indexingType())
-            || hasContiguous(structure->indexingType()));
-        unsigned vectorLength;
-        butterfly = createContiguousArrayButterfly(vm, 0, initialLength, vectorLength);
-        if (hasDouble(structure->indexingType()))
+            hasUndecided(indexingType)
+            || hasInt32(indexingType)
+            || hasDouble(indexingType)
+            || hasContiguous(indexingType));
+
+        if (initialLength > MAX_STORAGE_VECTOR_LENGTH)
+            return 0;
+
+        unsigned vectorLength = Butterfly::optimalContiguousVectorLength(structure, initialLength);
+        void* temp = vm.auxiliarySpace.tryAllocate(nullptr, Butterfly::totalSize(0, outOfLineStorage, true, vectorLength * sizeof(EncodedJSValue)));
+        if (!temp)
+            return nullptr;
+        butterfly = Butterfly::fromBase(temp, 0, outOfLineStorage);
+        butterfly->setVectorLength(vectorLength);
+        butterfly->setPublicLength(initialLength);
+        if (hasDouble(indexingType))
             clearArray(butterfly->contiguousDouble().data(), vectorLength);
         else
             clearArray(butterfly->contiguous().data(), vectorLength);
     } else {
         ASSERT(
-            structure->indexingType() == ArrayWithSlowPutArrayStorage
-            || structure->indexingType() == ArrayWithArrayStorage);
-        butterfly = createArrayButterfly(vm, 0, initialLength);
+            indexingType == ArrayWithSlowPutArrayStorage
+            || indexingType == ArrayWithArrayStorage);
+        butterfly = tryCreateArrayButterfly(vm, 0, initialLength);
+        if (!butterfly)
+            return nullptr;
         for (unsigned i = 0; i < BASE_ARRAY_STORAGE_VECTOR_LEN; ++i)
             butterfly->arrayStorage()->m_vector[i].clear();
     }
@@ -237,6 +253,14 @@ inline JSArray* JSArray::create(VM& vm, Structure* structure, unsigned initialLe
     return createWithButterfly(vm, nullptr, structure, butterfly);
 }
 
+inline JSArray* JSArray::create(VM& vm, Structure* structure, unsigned initialLength)
+{
+    JSArray* result = JSArray::tryCreate(vm, structure, initialLength);
+    RELEASE_ASSERT(result);
+
+    return result;
+}
+
 inline JSArray* JSArray::createWithButterfly(VM& vm, GCDeferralContext* deferralContext, Structure* structure, Butterfly* butterfly)
 {
     JSArray* array = new (NotNull, allocateCell<JSArray>(vm.heap, deferralContext)) JSArray(vm, structure, butterfly);