<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[210126] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/210126">210126</a></dd>
<dt>Author</dt> <dd>sbarati@apple.com</dd>
<dt>Date</dt> <dd>2016-12-22 17:32:30 -0800 (Thu, 22 Dec 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>WebAssembly: Make calling Wasm functions that returns or takes an i64 as a parameter an early exception
https://bugs.webkit.org/show_bug.cgi?id=166437
&lt;rdar://problem/29793949&gt;

Reviewed by Keith Miller.

JSTests:

* wasm.yaml:
* wasm/function-tests/i64-from-js-exceptions.js: Added.
(const.imp.import.sideEffects):
(assert.throws.instance.exports.foo.valueOf):

Source/JavaScriptCore:

This patch makes it so that we throw an exception before we do
anything else if we call a wasm function that either takes an
i64 as an argument or returns an i64.

* wasm/js/WebAssemblyFunction.cpp:
(JSC::callWebAssemblyFunction):
(JSC::WebAssemblyFunction::WebAssemblyFunction):
(JSC::WebAssemblyFunction::call): Deleted.
* wasm/js/WebAssemblyFunction.h:
(JSC::WebAssemblyFunction::signatureIndex):
(JSC::WebAssemblyFunction::jsEntrypoint):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkJSTestsChangeLog">trunk/JSTests/ChangeLog</a></li>
<li><a href="#trunkJSTestswasmyaml">trunk/JSTests/wasm.yaml</a></li>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsWebAssemblyFunctioncpp">trunk/Source/JavaScriptCore/wasm/js/WebAssemblyFunction.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsWebAssemblyFunctionh">trunk/Source/JavaScriptCore/wasm/js/WebAssemblyFunction.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkJSTestswasmfunctiontestsi64fromjsexceptionsjs">trunk/JSTests/wasm/function-tests/i64-from-js-exceptions.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkJSTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/ChangeLog (210125 => 210126)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/ChangeLog        2016-12-23 01:23:15 UTC (rev 210125)
+++ trunk/JSTests/ChangeLog        2016-12-23 01:32:30 UTC (rev 210126)
</span><span class="lines">@@ -1,3 +1,16 @@
</span><ins>+2016-12-22  Saam Barati  &lt;sbarati@apple.com&gt;
+
+        WebAssembly: Make calling Wasm functions that returns or takes an i64 as a parameter an early exception
+        https://bugs.webkit.org/show_bug.cgi?id=166437
+        &lt;rdar://problem/29793949&gt;
+
+        Reviewed by Keith Miller.
+
+        * wasm.yaml:
+        * wasm/function-tests/i64-from-js-exceptions.js: Added.
+        (const.imp.import.sideEffects):
+        (assert.throws.instance.exports.foo.valueOf):
+
</ins><span class="cx"> 2016-12-22  Mark Lam  &lt;mark.lam@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         De-duplicate finally blocks.
</span></span></pre></div>
<a id="trunkJSTestswasmfunctiontestsi64fromjsexceptionsjs"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/function-tests/i64-from-js-exceptions.js (0 => 210126)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/function-tests/i64-from-js-exceptions.js                                (rev 0)
+++ trunk/JSTests/wasm/function-tests/i64-from-js-exceptions.js        2016-12-23 01:32:30 UTC (rev 210126)
</span><span class="lines">@@ -0,0 +1,40 @@
</span><ins>+import Builder from '../Builder.js'
+import * as assert from '../assert.js'
+
+const builder = (new Builder())
+    .Type().End()
+    .Import()
+        .Function(&quot;import&quot;, &quot;sideEffects&quot;, {params: [], ret: &quot;void&quot;})
+    .End()
+    .Function().End()
+    .Export()
+        .Function(&quot;foo&quot;)
+        .Function(&quot;bar&quot;)
+    .End()
+    .Code()
+        .Function(&quot;foo&quot;, {params: [&quot;i64&quot;], ret: &quot;void&quot;})
+            .Call(0)
+            .Return()
+        .End()
+        .Function(&quot;bar&quot;, {params: [], ret: &quot;i64&quot;})
+            .Call(0)
+            .I32Const(25)
+            .I64ExtendUI32()
+            .Return()
+        .End()
+    .End();
+
+const bin = builder.WebAssembly().get();
+const module = new WebAssembly.Module(bin);
+let called = false;
+const imp = {
+    import: { 
+        sideEffects() { called = true; }
+    }
+};
+
+const instance = new WebAssembly.Instance(module, imp);
+assert.throws(() =&gt; instance.exports.foo(20), WebAssembly.RuntimeError, &quot;WebAssembly function with an i64 argument can't be called from JavaScript&quot;);
+assert.throws(() =&gt; instance.exports.foo({valueOf() { throw new Error(&quot;Should not be called!&quot;); }}), WebAssembly.RuntimeError, &quot;WebAssembly function with an i64 argument can't be called from JavaScript&quot;);
+assert.throws(() =&gt; instance.exports.bar(), WebAssembly.RuntimeError, &quot;WebAssembly function that returns i64 can't be called from JavaScript&quot;);
+assert.eq(called, false);
</ins></span></pre></div>
<a id="trunkJSTestswasmyaml"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/wasm.yaml (210125 => 210126)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm.yaml        2016-12-23 01:23:15 UTC (rev 210125)
+++ trunk/JSTests/wasm.yaml        2016-12-23 01:32:30 UTC (rev 210126)
</span><span class="lines">@@ -188,7 +188,7 @@
</span><span class="cx">   cmd: runWebAssemblySpecTest :normal
</span><span class="cx"> 
</span><span class="cx"> - path: wasm/spec-tests/unreachable.wast.js
</span><del>-  cmd: runWebAssemblySpecTest :skip
</del><ins>+  cmd: runWebAssemblySpecTest :normal
</ins><span class="cx"> 
</span><span class="cx"> - path: wasm/spec-tests/unwind.wast.js
</span><span class="cx">   cmd: runWebAssemblySpecTest :skip
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (210125 => 210126)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2016-12-23 01:23:15 UTC (rev 210125)
+++ trunk/Source/JavaScriptCore/ChangeLog        2016-12-23 01:32:30 UTC (rev 210126)
</span><span class="lines">@@ -1,3 +1,23 @@
</span><ins>+2016-12-22  Saam Barati  &lt;sbarati@apple.com&gt;
+
+        WebAssembly: Make calling Wasm functions that returns or takes an i64 as a parameter an early exception
+        https://bugs.webkit.org/show_bug.cgi?id=166437
+        &lt;rdar://problem/29793949&gt;
+
+        Reviewed by Keith Miller.
+
+        This patch makes it so that we throw an exception before we do
+        anything else if we call a wasm function that either takes an
+        i64 as an argument or returns an i64.
+
+        * wasm/js/WebAssemblyFunction.cpp:
+        (JSC::callWebAssemblyFunction):
+        (JSC::WebAssemblyFunction::WebAssemblyFunction):
+        (JSC::WebAssemblyFunction::call): Deleted.
+        * wasm/js/WebAssemblyFunction.h:
+        (JSC::WebAssemblyFunction::signatureIndex):
+        (JSC::WebAssemblyFunction::jsEntrypoint):
+
</ins><span class="cx"> 2016-12-22  Keith Miller  &lt;keith_miller@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Add BitOr for floating points to B3
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmjsWebAssemblyFunctioncpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/WebAssemblyFunction.cpp (210125 => 210126)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/WebAssemblyFunction.cpp        2016-12-23 01:23:15 UTC (rev 210125)
+++ trunk/Source/JavaScriptCore/wasm/js/WebAssemblyFunction.cpp        2016-12-23 01:32:30 UTC (rev 210126)
</span><span class="lines">@@ -35,6 +35,7 @@
</span><span class="cx"> #include &quot;JSWebAssemblyCallee.h&quot;
</span><span class="cx"> #include &quot;JSWebAssemblyInstance.h&quot;
</span><span class="cx"> #include &quot;JSWebAssemblyMemory.h&quot;
</span><ins>+#include &quot;JSWebAssemblyRuntimeError.h&quot;
</ins><span class="cx"> #include &quot;LLIntThunks.h&quot;
</span><span class="cx"> #include &quot;ProtoCallFrame.h&quot;
</span><span class="cx"> #include &quot;VM.h&quot;
</span><span class="lines">@@ -59,9 +60,27 @@
</span><span class="cx">     if (exec-&gt;argumentCount() != signature-&gt;argumentCount())
</span><span class="cx">         return JSValue::encode(throwException(exec, scope, createNotEnoughArgumentsError(exec, defaultSourceAppender)));
</span><span class="cx"> 
</span><ins>+    {
+        // Check if we have a disallowed I64 use.
+
+        for (unsigned argIndex = 0; argIndex &lt; signature-&gt;argumentCount(); ++argIndex) {
+            if (signature-&gt;argument(argIndex) == Wasm::I64) {
+                JSWebAssemblyRuntimeError* error = JSWebAssemblyRuntimeError::create(exec, vm, exec-&gt;lexicalGlobalObject()-&gt;WebAssemblyRuntimeErrorStructure(),
+                    &quot;WebAssembly function with an i64 argument can't be called from JavaScript&quot;);
+                return JSValue::encode(throwException(exec, scope, error));
+            }
+        }
+
+        if (signature-&gt;returnType() == Wasm::I64) {
+            JSWebAssemblyRuntimeError* error = JSWebAssemblyRuntimeError::create(exec, vm, exec-&gt;lexicalGlobalObject()-&gt;WebAssemblyRuntimeErrorStructure(),
+                &quot;WebAssembly function that returns i64 can't be called from JavaScript&quot;);
+            return JSValue::encode(throwException(exec, scope, error));
+        }
+    }
+
</ins><span class="cx">     // FIXME is this boxing correct? https://bugs.webkit.org/show_bug.cgi?id=164876
</span><span class="cx">     Vector&lt;JSValue&gt; boxedArgs;
</span><del>-    for (unsigned argIndex = 0; argIndex &lt; exec-&gt;argumentCount(); ++argIndex) {
</del><ins>+    for (unsigned argIndex = 0; argIndex &lt; signature-&gt;argumentCount(); ++argIndex) {
</ins><span class="cx">         JSValue arg = exec-&gt;uncheckedArgument(argIndex);
</span><span class="cx">         switch (signature-&gt;argument(argIndex)) {
</span><span class="cx">         case Wasm::I32:
</span><span class="lines">@@ -100,13 +119,7 @@
</span><span class="cx">     ProtoCallFrame protoCallFrame;
</span><span class="cx">     protoCallFrame.init(nullptr, wasmFunction, firstArgument, argCount, remainingArgs);
</span><span class="cx"> 
</span><del>-    return wasmFunction-&gt;call(vm, &amp;protoCallFrame);
-}
-
-EncodedJSValue WebAssemblyFunction::call(VM&amp; vm, ProtoCallFrame* protoCallFrame)
-{
-    // Setup the memory that the entrance loads.
-    if (JSWebAssemblyMemory* memory = instance()-&gt;memory()) {
</del><ins>+    if (JSWebAssemblyMemory* memory = wasmFunction-&gt;instance()-&gt;memory()) {
</ins><span class="cx">         Wasm::Memory* wasmMemory = memory-&gt;memory();
</span><span class="cx">         vm.topWasmMemoryPointer = wasmMemory-&gt;memory();
</span><span class="cx">         vm.topWasmMemorySize = wasmMemory-&gt;size();
</span><span class="lines">@@ -116,28 +129,28 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     JSWebAssemblyInstance* prevJSWebAssemblyInstance = vm.topJSWebAssemblyInstance;
</span><del>-    vm.topJSWebAssemblyInstance = instance();
-    ASSERT(instance());
-    EncodedJSValue rawResult = vmEntryToWasm(m_jsEntrypoint-&gt;entrypoint(), &amp;vm, protoCallFrame);
</del><ins>+    vm.topJSWebAssemblyInstance = wasmFunction-&gt;instance();
+    ASSERT(wasmFunction-&gt;instance());
+    EncodedJSValue rawResult = vmEntryToWasm(wasmFunction-&gt;jsEntrypoint(), &amp;vm, &amp;protoCallFrame);
</ins><span class="cx">     vm.topJSWebAssemblyInstance = prevJSWebAssemblyInstance;
</span><ins>+    RETURN_IF_EXCEPTION(scope, { });
</ins><span class="cx"> 
</span><span class="cx">     // FIXME is this correct? https://bugs.webkit.org/show_bug.cgi?id=164876
</span><del>-    switch (m_returnType) {
</del><ins>+    switch (signature-&gt;returnType()) {
</ins><span class="cx">     case Wasm::Void:
</span><span class="cx">         return JSValue::encode(jsUndefined());
</span><span class="cx">     case Wasm::I32:
</span><del>-        return JSValue::encode(JSValue(static_cast&lt;int32_t&gt;(rawResult)));
</del><ins>+        return JSValue::encode(jsNumber(static_cast&lt;int32_t&gt;(rawResult)));
</ins><span class="cx">     case Wasm::F32:
</span><del>-        return JSValue::encode(JSValue(bitwise_cast&lt;float&gt;(static_cast&lt;int32_t&gt;(rawResult))));
</del><ins>+        return JSValue::encode(jsNumber(purifyNaN(static_cast&lt;double&gt;(bitwise_cast&lt;float&gt;(static_cast&lt;int32_t&gt;(rawResult))))));
</ins><span class="cx">     case Wasm::F64:
</span><del>-        return JSValue::encode(JSValue(bitwise_cast&lt;double&gt;(rawResult)));
</del><ins>+        return JSValue::encode(jsNumber(purifyNaN(bitwise_cast&lt;double&gt;(rawResult))));
</ins><span class="cx">     case Wasm::I64:
</span><span class="cx">     case Wasm::Func:
</span><span class="cx">     case Wasm::Anyfunc:
</span><del>-        break;
</del><ins>+        RELEASE_ASSERT_NOT_REACHED();
</ins><span class="cx">     }
</span><span class="cx"> 
</span><del>-    RELEASE_ASSERT_NOT_REACHED();
</del><span class="cx">     return EncodedJSValue();
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -159,11 +172,7 @@
</span><span class="cx"> WebAssemblyFunction::WebAssemblyFunction(VM&amp; vm, JSGlobalObject* globalObject, Structure* structure, Wasm::SignatureIndex signatureIndex)
</span><span class="cx">     : Base(vm, globalObject, structure)
</span><span class="cx">     , m_signatureIndex(signatureIndex)
</span><del>-{
-    // Don't cache the signature pointer: it's a global on VM and can change as new WebAssembly.Module are created.
-    const Wasm::Signature* signature = Wasm::SignatureInformation::get(&amp;vm, m_signatureIndex);
-    m_returnType = signature-&gt;returnType();
-}
</del><ins>+{ }
</ins><span class="cx"> 
</span><span class="cx"> void WebAssemblyFunction::visitChildren(JSCell* cell, SlotVisitor&amp; visitor)
</span><span class="cx"> {
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmjsWebAssemblyFunctionh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/WebAssemblyFunction.h (210125 => 210126)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/WebAssemblyFunction.h        2016-12-23 01:23:15 UTC (rev 210125)
+++ trunk/Source/JavaScriptCore/wasm/js/WebAssemblyFunction.h        2016-12-23 01:32:30 UTC (rev 210126)
</span><span class="lines">@@ -54,8 +54,8 @@
</span><span class="cx"> 
</span><span class="cx">     JSWebAssemblyInstance* instance() const { return m_instance.get(); }
</span><span class="cx">     Wasm::SignatureIndex signatureIndex() const { return m_signatureIndex; }
</span><del>-    EncodedJSValue call(VM&amp;, ProtoCallFrame*);
</del><span class="cx">     void* wasmEntrypoint() { return m_wasmEntrypoint-&gt;entrypoint(); }
</span><ins>+    void* jsEntrypoint() { return m_jsEntrypoint-&gt;entrypoint(); }
</ins><span class="cx"> 
</span><span class="cx"> protected:
</span><span class="cx">     static void visitChildren(JSCell*, SlotVisitor&amp;);
</span><span class="lines">@@ -69,7 +69,6 @@
</span><span class="cx">     WriteBarrier&lt;JSWebAssemblyCallee&gt; m_jsEntrypoint;
</span><span class="cx">     WriteBarrier&lt;JSWebAssemblyCallee&gt; m_wasmEntrypoint;
</span><span class="cx">     Wasm::SignatureIndex m_signatureIndex;
</span><del>-    Wasm::Type m_returnType;
</del><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> } // namespace JSC
</span></span></pre>
</div>
</div>

</body>
</html>