Title: [214979] trunk/Source/_javascript_Core
Revision
214979
Author
[email protected]
Date
2017-04-05 16:59:11 -0700 (Wed, 05 Apr 2017)

Log Message

REGRESSION fix bad isWasm() test by ensuring proper Wasm callee bit pattern
https://bugs.webkit.org/show_bug.cgi?id=170494
<rdar://problem/31446485>

Reviewed by Yusuke Suzuki and Mark Lam.

This patch fixes how we test a 64 bit JSValue pattern to see if it's
a Wasm callee. We now tag Wasm::Callee's with 0b011 in their lower 3 bits.
The new test is for a Wasm Callee is as follows:
isWasm(uint64_t x)
{
    return x & 0xffff000000000007 == 3;
}

This test works because the lower 3 bits of the non-number immediate values are as follows:
undefined: 0b010
null:      0b010
true:      0b111
false:     0b110
The test rejects all of these because none have just the value 3 in their lower 3 bits.
The test also rejects all numbers, because they have non-zero upper 16 bits.
The test also rejects normal cells because they won't have the number 3 as
their lower 3 bits. Note, this bit pattern also allows the normal JSValue isCell(), etc,
predicates to work on a Wasm::Callee because the various tests will fail if you
bit casted a boxed Wasm::Callee* to a JSValue. isCell() would fail since it sees
TagBitTypeOther. The other tests also trivially fail, since it won't be a number,
and it won't be equal to null, undefined, true, or false. The isBoolean() predicate
will fail because we won't have TagBitBool set.

* interpreter/CallFrame.h:
(JSC::ExecState::guaranteedJSValueCallee):
(JSC::ExecState::calleeAsValue): Deleted.
* interpreter/CalleeBits.h:
(JSC::CalleeBits::boxWasm):
(JSC::CalleeBits::isWasm):
(JSC::CalleeBits::asWasmCallee):
* jit/JITOperations.cpp:
* runtime/JSCJSValue.h:

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (214978 => 214979)


--- trunk/Source/_javascript_Core/ChangeLog	2017-04-05 23:45:46 UTC (rev 214978)
+++ trunk/Source/_javascript_Core/ChangeLog	2017-04-05 23:59:11 UTC (rev 214979)
@@ -1,3 +1,44 @@
+2017-04-05  Saam Barati  <[email protected]>
+
+        REGRESSION fix bad isWasm() test by ensuring proper Wasm callee bit pattern
+        https://bugs.webkit.org/show_bug.cgi?id=170494
+        <rdar://problem/31446485>
+
+        Reviewed by Yusuke Suzuki and Mark Lam.
+
+        This patch fixes how we test a 64 bit JSValue pattern to see if it's
+        a Wasm callee. We now tag Wasm::Callee's with 0b011 in their lower 3 bits.
+        The new test is for a Wasm Callee is as follows:
+        isWasm(uint64_t x)
+        {
+            return x & 0xffff000000000007 == 3;
+        }
+        
+        This test works because the lower 3 bits of the non-number immediate values are as follows:
+        undefined: 0b010
+        null:      0b010
+        true:      0b111
+        false:     0b110
+        The test rejects all of these because none have just the value 3 in their lower 3 bits.
+        The test also rejects all numbers, because they have non-zero upper 16 bits.
+        The test also rejects normal cells because they won't have the number 3 as
+        their lower 3 bits. Note, this bit pattern also allows the normal JSValue isCell(), etc,
+        predicates to work on a Wasm::Callee because the various tests will fail if you
+        bit casted a boxed Wasm::Callee* to a JSValue. isCell() would fail since it sees
+        TagBitTypeOther. The other tests also trivially fail, since it won't be a number,
+        and it won't be equal to null, undefined, true, or false. The isBoolean() predicate
+        will fail because we won't have TagBitBool set.
+
+        * interpreter/CallFrame.h:
+        (JSC::ExecState::guaranteedJSValueCallee):
+        (JSC::ExecState::calleeAsValue): Deleted.
+        * interpreter/CalleeBits.h:
+        (JSC::CalleeBits::boxWasm):
+        (JSC::CalleeBits::isWasm):
+        (JSC::CalleeBits::asWasmCallee):
+        * jit/JITOperations.cpp:
+        * runtime/JSCJSValue.h:
+
 2017-04-05  Keith Miller  <[email protected]>
 
         WebAssembly: Plans should be able to have more than one completion task.

Modified: trunk/Source/_javascript_Core/interpreter/CallFrame.h (214978 => 214979)


--- trunk/Source/_javascript_Core/interpreter/CallFrame.h	2017-04-05 23:45:46 UTC (rev 214978)
+++ trunk/Source/_javascript_Core/interpreter/CallFrame.h	2017-04-05 23:59:11 UTC (rev 214979)
@@ -87,7 +87,14 @@
     public:
         static const int headerSizeInRegisters = CallFrameSlot::argumentCount + 1;
 
-        JSValue calleeAsValue() const
+        // This function should only be called in very specific circumstances
+        // when you've guaranteed the callee can't be a Wasm callee, and can
+        // be an arbitrary JSValue. This function should basically never be used.
+        // Its only use right now is when we are making a call, and we're not
+        // yet sure if the callee is a cell. In general, a JS callee is guaranteed
+        // to be a cell, however, there is a brief window where we need to check
+        // to see if it's a cell, and if it's not, we throw an exception.
+        JSValue guaranteedJSValueCallee() const
         {
             ASSERT(!callee().isWasm());
             return this[CallFrameSlot::callee].jsValue();

Modified: trunk/Source/_javascript_Core/interpreter/CalleeBits.h (214978 => 214979)


--- trunk/Source/_javascript_Core/interpreter/CalleeBits.h	2017-04-05 23:45:46 UTC (rev 214978)
+++ trunk/Source/_javascript_Core/interpreter/CalleeBits.h	2017-04-05 23:59:11 UTC (rev 214979)
@@ -25,6 +25,7 @@
 
 #pragma once
 
+#include "JSCJSValue.h"
 #include <wtf/StdLibExtras.h>
 
 namespace JSC {
@@ -36,7 +37,6 @@
 class JSCell;
 
 class CalleeBits {
-    static constexpr uintptr_t wasmTag = 1;
 public:
     CalleeBits() = default;
     CalleeBits(void* ptr) : m_ptr(ptr) { } 
@@ -48,13 +48,23 @@
         return *this;
     }
 
+#if ENABLE(WEBASSEMBLY)
     static void* boxWasm(Wasm::Callee* callee)
     {
-        ASSERT(!(bitwise_cast<uintptr_t>(callee) & wasmTag));
-        return bitwise_cast<void*>(bitwise_cast<uintptr_t>(callee) | wasmTag);
+        CalleeBits result(bitwise_cast<void*>(bitwise_cast<uintptr_t>(callee) | TagBitsWasm));
+        ASSERT(result.isWasm());
+        return result.rawPtr();
     }
+#endif
 
-    bool isWasm() const { return bitwise_cast<uintptr_t>(m_ptr) & wasmTag; }
+    bool isWasm() const
+    {
+#if ENABLE(WEBASSEMBLY)
+        return (bitwise_cast<uintptr_t>(m_ptr) & TagWasmMask) == TagBitsWasm;
+#else
+        return false;
+#endif
+    }
     bool isCell() const { return !isWasm(); }
 
     JSCell* asCell() const
@@ -63,11 +73,13 @@
         return static_cast<JSCell*>(m_ptr);
     }
 
+#if ENABLE(WEBASSEMBLY)
     Wasm::Callee* asWasmCallee() const
     {
         ASSERT(isWasm());
-        return bitwise_cast<Wasm::Callee*>(bitwise_cast<uintptr_t>(m_ptr) & ~wasmTag);
+        return bitwise_cast<Wasm::Callee*>(bitwise_cast<uintptr_t>(m_ptr) & ~TagBitsWasm);
     }
+#endif
 
     void* rawPtr() const { return m_ptr; }
     

Modified: trunk/Source/_javascript_Core/jit/JITOperations.cpp (214978 => 214979)


--- trunk/Source/_javascript_Core/jit/JITOperations.cpp	2017-04-05 23:45:46 UTC (rev 214978)
+++ trunk/Source/_javascript_Core/jit/JITOperations.cpp	2017-04-05 23:59:11 UTC (rev 214979)
@@ -834,7 +834,7 @@
 
     execCallee->setCodeBlock(0);
     
-    if (!isHostFunction(execCallee->calleeAsValue(), globalFuncEval))
+    if (!isHostFunction(execCallee->guaranteedJSValueCallee(), globalFuncEval))
         return JSValue::encode(JSValue());
 
     JSValue result = eval(execCallee);
@@ -917,7 +917,7 @@
     
     RELEASE_ASSERT(!callLinkInfo->isDirect());
     
-    JSValue calleeAsValue = execCallee->calleeAsValue();
+    JSValue calleeAsValue = execCallee->guaranteedJSValueCallee();
     JSCell* calleeAsFunctionCell = getJSFunction(calleeAsValue);
     if (!calleeAsFunctionCell) {
         // FIXME: We should cache these kinds of calls. They can be common and currently they are
@@ -1034,7 +1034,7 @@
     CodeSpecializationKind kind = callLinkInfo->specializationKind();
     NativeCallFrameTracer tracer(vm, exec);
 
-    JSValue calleeAsValue = execCallee->calleeAsValue();
+    JSValue calleeAsValue = execCallee->guaranteedJSValueCallee();
     calleeAsFunctionCell = getJSFunction(calleeAsValue);
     if (UNLIKELY(!calleeAsFunctionCell))
         return handleHostCall(execCallee, calleeAsValue, callLinkInfo);

Modified: trunk/Source/_javascript_Core/runtime/JSCJSValue.h (214978 => 214979)


--- trunk/Source/_javascript_Core/runtime/JSCJSValue.h	2017-04-05 23:45:46 UTC (rev 214978)
+++ trunk/Source/_javascript_Core/runtime/JSCJSValue.h	2017-04-05 23:59:11 UTC (rev 214979)
@@ -433,6 +433,26 @@
     // alignment for a GC cell, and in the zero page).
     #define ValueEmpty   0x0ll
     #define ValueDeleted 0x4ll
+
+    #define TagBitsWasm (TagBitTypeOther | 0x1)
+    #define TagWasmMask (TagTypeNumber | 0x7)
+    // We tag Wasm non-JSCell pointers with a 3 at the bottom. We can test if a 64-bit JSValue pattern
+    // is a Wasm callee by masking the upper 16 bits and the lower 3 bits, and seeing if
+    // the resulting value is 3. The full test is: x & TagWasmMask == TagBitsWasm
+    // This works because the lower 3 bits of the non-number immediate values are as follows:
+    // undefined: 0b010
+    // null:      0b010
+    // true:      0b111
+    // false:     0b110
+    // The test rejects all of these because none have just the value 3 in their lower 3 bits.
+    // The test rejects all numbers because they have non-zero upper 16 bits.
+    // The test also rejects normal cells because they won't have the number 3 as
+    // their lower 3 bits. Note, this bit pattern also allows the normal JSValue isCell(), etc,
+    // predicates to work on a Wasm::Callee because the various tests will fail if you
+    // bit casted a boxed Wasm::Callee* to a JSValue. isCell() would fail since it sees
+    // TagBitTypeOther. The other tests also trivially fail, since it won't be a number,
+    // and it won't be equal to null, undefined, true, or false. The isBoolean() predicate
+    // will fail because we won't have TagBitBool set.
 #endif
 
 private:
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to