Title: [275544] trunk
Revision
275544
Author
[email protected]
Date
2021-04-06 13:20:41 -0700 (Tue, 06 Apr 2021)

Log Message

Array's toString() is incorrect if join() is non-callable
https://bugs.webkit.org/show_bug.cgi?id=224215

Reviewed by Yusuke Suzuki.

JSTests:

* stress/array-toString-non-callable-join.js: Added.

Source/_javascript_Core:

This patch exposes objectPrototypeToString() to be used by Array.prototype.toString
if "join" lookup doesn't return a callable value [1].

Fixes Array's toString() to return the correct tag instead of internal `className`,
perform Symbol.toStringTag lookup, and throw for revoked Proxy objects.
Aligns JSC with V8 and SpiderMonkey.

Also, a few objectPrototypeToString() tweaks: a bit nicer `undefined` / `null`
checks and simpler toObject() exception handling.

[1]: https://tc39.es/ecma262/#sec-array.prototype.tostring (step 3)

* runtime/ArrayPrototype.cpp:
(JSC::JSC_DEFINE_HOST_FUNCTION):
* runtime/ObjectPrototype.cpp:
(JSC::objectPrototypeToString):
(JSC::JSC_DEFINE_HOST_FUNCTION):
* runtime/ObjectPrototype.h:

Modified Paths

Added Paths

Diff

Modified: trunk/JSTests/ChangeLog (275543 => 275544)


--- trunk/JSTests/ChangeLog	2021-04-06 19:51:49 UTC (rev 275543)
+++ trunk/JSTests/ChangeLog	2021-04-06 20:20:41 UTC (rev 275544)
@@ -1,3 +1,12 @@
+2021-04-06  Alexey Shvayka  <[email protected]>
+
+        Array's toString() is incorrect if join() is non-callable
+        https://bugs.webkit.org/show_bug.cgi?id=224215
+
+        Reviewed by Yusuke Suzuki.
+
+        * stress/array-toString-non-callable-join.js: Added.
+
 2021-04-05  Keith Miller  <[email protected]>
 
         DFG arity fixup nodes should exit to the caller's call opcode

Added: trunk/JSTests/stress/array-toString-non-callable-join.js (0 => 275544)


--- trunk/JSTests/stress/array-toString-non-callable-join.js	                        (rev 0)
+++ trunk/JSTests/stress/array-toString-non-callable-join.js	2021-04-06 20:20:41 UTC (rev 275544)
@@ -0,0 +1,73 @@
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error(`Bad value: ${actual}!`);
+}
+
+function shouldThrow(func, expectedError) {
+    let errorThrown = false;
+    try {
+        func();
+    } catch (error) {
+        errorThrown = true;
+        if (error.toString() !== expectedError)
+            throw new Error(`Bad error: ${error}!`);
+    }
+    if (!errorThrown)
+        throw new Error("Didn't throw!");
+}
+
+shouldBe(delete Object.prototype.toString, true);
+
+Array.prototype.join = {};
+shouldBe(Array.prototype.toString.call({}), "[object Object]");
+
+let revokeOnGet = false;
+const { proxy, revoke } = Proxy.revocable([], {
+    get: (target, key, receiver) => {
+        if (revokeOnGet)
+            revoke();
+        return Reflect.get(target, key, receiver);
+    },
+});
+
+shouldBe(Array.prototype.toString.call(proxy), "[object Array]");
+revokeOnGet = true;
+shouldThrow(() => { Array.prototype.toString.call(proxy); }, "TypeError: Array.isArray cannot be called on a Proxy that has been revoked");
+
+Array.prototype.join = 1n;
+shouldBe(Array.prototype.toString.call((function() { return arguments; })()), "[object Arguments]");
+shouldBe(Array.prototype.toString.call(new Error), "[object Error]");
+
+Array.prototype.join = Symbol();
+shouldBe(Array.prototype.toString.call(new Boolean), "[object Boolean]");
+shouldBe(Array.prototype.toString.call(new Number), "[object Number]");
+
+Array.prototype.join = "join";
+shouldBe(Array.prototype.toString.call(new String), "[object String]");
+shouldBe(Array.prototype.toString.call(new Date), "[object Date]");
+
+Array.prototype.join = 1;
+shouldBe(Array.prototype.toString.call(new RegExp), "[object RegExp]");
+shouldBe(Array.prototype.toString.call(new Proxy(() => {}, {})), "[object Function]");
+shouldBe(Array.prototype.toString.call(new Proxy(new Date, {})), "[object Object]");
+
+Array.prototype.join = true;
+shouldBe(Array.prototype.toString.call({ [Symbol.toStringTag]: "Foo" }), "[object Foo]");
+shouldBe(Array.prototype.toString.call(new Map), "[object Map]");
+
+Array.prototype.join = null;
+RegExp.prototype[Symbol.toStringTag] = "Foo";
+shouldBe(Array.prototype.toString.call(new RegExp), "[object Foo]");
+Number.prototype[Symbol.toStringTag] = Object("Foo"); // ignored
+shouldBe(Array.prototype.toString.call(new Number), "[object Number]");
+
+shouldBe(delete Array.prototype.join, true);
+
+Object.defineProperty(JSON, Symbol.toStringTag, { value: "Foo" });
+shouldBe(Array.prototype.toString.call(JSON), "[object Foo]");
+
+shouldBe(delete Set.prototype[Symbol.toStringTag], true);
+shouldBe(Array.prototype.toString.call(new Set), "[object Object]");
+
+Object.defineProperty(Object.prototype, Symbol.toStringTag, { get: () => { throw new Error("@@toStringTag"); } });
+shouldThrow(() => { Array.prototype.toString.call({}); }, "Error: @@toStringTag");

Modified: trunk/Source/_javascript_Core/ChangeLog (275543 => 275544)


--- trunk/Source/_javascript_Core/ChangeLog	2021-04-06 19:51:49 UTC (rev 275543)
+++ trunk/Source/_javascript_Core/ChangeLog	2021-04-06 20:20:41 UTC (rev 275544)
@@ -1,3 +1,29 @@
+2021-04-06  Alexey Shvayka  <[email protected]>
+
+        Array's toString() is incorrect if join() is non-callable
+        https://bugs.webkit.org/show_bug.cgi?id=224215
+
+        Reviewed by Yusuke Suzuki.
+
+        This patch exposes objectPrototypeToString() to be used by Array.prototype.toString
+        if "join" lookup doesn't return a callable value [1].
+
+        Fixes Array's toString() to return the correct tag instead of internal `className`,
+        perform Symbol.toStringTag lookup, and throw for revoked Proxy objects.
+        Aligns JSC with V8 and SpiderMonkey.
+
+        Also, a few objectPrototypeToString() tweaks: a bit nicer `undefined` / `null`
+        checks and simpler toObject() exception handling.
+
+        [1]: https://tc39.es/ecma262/#sec-array.prototype.tostring (step 3)
+
+        * runtime/ArrayPrototype.cpp:
+        (JSC::JSC_DEFINE_HOST_FUNCTION):
+        * runtime/ObjectPrototype.cpp:
+        (JSC::objectPrototypeToString):
+        (JSC::JSC_DEFINE_HOST_FUNCTION):
+        * runtime/ObjectPrototype.h:
+
 2021-04-06  Yusuke Suzuki  <[email protected]>
 
         [WTF] Introduce FixedVector and use it for FixedOperands

Modified: trunk/Source/_javascript_Core/runtime/ArrayPrototype.cpp (275543 => 275544)


--- trunk/Source/_javascript_Core/runtime/ArrayPrototype.cpp	2021-04-06 19:51:49 UTC (rev 275543)
+++ trunk/Source/_javascript_Core/runtime/ArrayPrototype.cpp	2021-04-06 20:20:41 UTC (rev 275544)
@@ -34,6 +34,7 @@
 #include "JSImmutableButterfly.h"
 #include "JSStringJoiner.h"
 #include "ObjectConstructor.h"
+#include "ObjectPrototype.h"
 #include "StringRecursionChecker.h"
 #include <algorithm>
 #include <wtf/Assertions.h>
@@ -612,20 +613,14 @@
     Integrity::auditStructureID(vm, thisObject->structureID());
     if (!canUseDefaultArrayJoinForToString(vm, thisObject)) {
         // 2. Let func be the result of calling the [[Get]] internal method of array with argument "join".
-        JSValue function = JSValue(thisObject).get(globalObject, vm.propertyNames->join);
+        JSValue function = thisObject->get(globalObject, vm.propertyNames->join);
         RETURN_IF_EXCEPTION(scope, encodedJSValue());
 
         // 3. If IsCallable(func) is false, then let func be the standard built-in method Object.prototype.toString (15.2.4.2).
-        bool customJoinCase = false;
-        if (!function.isCell())
-            customJoinCase = true;
         auto callData = getCallData(vm, function);
-        if (callData.type == CallData::Type::None)
-            customJoinCase = true;
+        if (UNLIKELY(callData.type == CallData::Type::None))
+            RELEASE_AND_RETURN(scope, JSValue::encode(objectPrototypeToString(globalObject, thisObject)));
 
-        if (UNLIKELY(customJoinCase))
-            RELEASE_AND_RETURN(scope, JSValue::encode(jsMakeNontrivialString(globalObject, "[object ", thisObject->methodTable(vm)->className(thisObject, vm), "]")));
-
         // 4. Return the result of calling the [[Call]] internal method of func providing array as the this value and an empty arguments list.
         if (!isJSArray(thisObject) || callData.type != CallData::Type::Native || callData.native.function != arrayProtoFuncJoin)
             RELEASE_AND_RETURN(scope, JSValue::encode(call(globalObject, function, callData, thisObject, *vm.emptyList)));

Modified: trunk/Source/_javascript_Core/runtime/ObjectPrototype.cpp (275543 => 275544)


--- trunk/Source/_javascript_Core/runtime/ObjectPrototype.cpp	2021-04-06 19:51:49 UTC (rev 275543)
+++ trunk/Source/_javascript_Core/runtime/ObjectPrototype.cpp	2021-04-06 20:20:41 UTC (rev 275544)
@@ -311,26 +311,25 @@
     RELEASE_AND_RETURN(scope, JSValue::encode(call(globalObject, toString, callData, thisValue, *vm.emptyList)));
 }
 
-JSC_DEFINE_HOST_FUNCTION(objectProtoFuncToString, (JSGlobalObject* globalObject, CallFrame* callFrame))
+JSString* objectPrototypeToString(JSGlobalObject* globalObject, JSValue thisValue)
 {
     VM& vm = globalObject->vm();
     auto scope = DECLARE_THROW_SCOPE(vm);
 
-    JSValue thisValue = callFrame->thisValue().toThis(globalObject, ECMAMode::strict());
-    if (thisValue.isUndefinedOrNull())
-        return JSValue::encode(thisValue.isUndefined() ? vm.smallStrings.undefinedObjectString() : vm.smallStrings.nullObjectString());
+    if (thisValue.isUndefined())
+        return vm.smallStrings.undefinedObjectString();
+    if (thisValue.isNull())
+        return vm.smallStrings.nullObjectString();
     JSObject* thisObject = thisValue.toObject(globalObject);
-    EXCEPTION_ASSERT(!!scope.exception() == !thisObject);
-    if (!thisObject)
-        return JSValue::encode(jsUndefined());
+    scope.assertNoException();
 
     Integrity::auditStructureID(vm, thisObject->structureID());
     auto result = thisObject->structure(vm)->cachedSpecialProperty(CachedSpecialPropertyKey::ToStringTag);
     if (result)
-        return JSValue::encode(result);
+        return asString(result);
 
     String tag = thisObject->methodTable(vm)->toStringName(thisObject, globalObject);
-    RETURN_IF_EXCEPTION(scope, { });
+    RETURN_IF_EXCEPTION(scope, nullptr);
     JSString* jsTag = nullptr;
 
     PropertySlot slot(thisObject, PropertySlot::InternalMethodType::Get);
@@ -338,7 +337,7 @@
     EXCEPTION_ASSERT(!scope.exception() || !hasProperty);
     if (hasProperty) {
         JSValue tagValue = slot.getValue(globalObject, vm.propertyNames->toStringTagSymbol);
-        RETURN_IF_EXCEPTION(scope, { });
+        RETURN_IF_EXCEPTION(scope, nullptr);
         if (tagValue.isString())
             jsTag = asString(tagValue);
     }
@@ -349,9 +348,15 @@
     }
 
     JSString* jsResult = jsString(globalObject, vm.smallStrings.objectStringStart(), jsTag, vm.smallStrings.singleCharacterString(']'));
-    RETURN_IF_EXCEPTION(scope, { });
+    RETURN_IF_EXCEPTION(scope, nullptr);
     thisObject->structure(vm)->cacheSpecialProperty(globalObject, vm, jsResult, CachedSpecialPropertyKey::ToStringTag, slot);
-    return JSValue::encode(jsResult);
+    return jsResult;
 }
 
+JSC_DEFINE_HOST_FUNCTION(objectProtoFuncToString, (JSGlobalObject* globalObject, CallFrame* callFrame))
+{
+    JSValue thisValue = callFrame->thisValue().toThis(globalObject, ECMAMode::strict());
+    return JSValue::encode(objectPrototypeToString(globalObject, thisValue));
+}
+
 } // namespace JSC

Modified: trunk/Source/_javascript_Core/runtime/ObjectPrototype.h (275543 => 275544)


--- trunk/Source/_javascript_Core/runtime/ObjectPrototype.h	2021-04-06 19:51:49 UTC (rev 275543)
+++ trunk/Source/_javascript_Core/runtime/ObjectPrototype.h	2021-04-06 20:20:41 UTC (rev 275544)
@@ -51,6 +51,7 @@
 };
 
 JS_EXPORT_PRIVATE JSC_DECLARE_HOST_FUNCTION(objectProtoFuncToString);
+JSString* objectPrototypeToString(JSGlobalObject*, JSValue thisValue);
 bool objectPrototypeHasOwnProperty(JSGlobalObject*, JSValue base, const Identifier& property);
 
 } // namespace JSC
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to