Title: [209642] trunk
Revision
209642
Author
jfbast...@apple.com
Date
2016-12-09 18:34:02 -0800 (Fri, 09 Dec 2016)

Log Message

WebAssembly JS API: implement start function
https://bugs.webkit.org/show_bug.cgi?id=165150

Reviewed by Saam Barati.

JSTests:

* wasm/Builder.js: allow building a .Start()
* wasm/Builder_WebAssemblyBinary.js:
* wasm/js-api/test_Start.js: Added.
(const.emitters.Start): serialize a start section
* wasm/self-test/test_BuilderJSON.js: validate the start section's content

Source/_javascript_Core:

* wasm/WasmFormat.h: pass the start function around
* wasm/WasmModuleParser.cpp:
(JSC::Wasm::ModuleParser::parseTable): mark unreachable code
(JSC::Wasm::ModuleParser::parseGlobal): mark unreachable code
(JSC::Wasm::ModuleParser::parseStart): mark unreachable code
(JSC::Wasm::ModuleParser::parseElement): mark unreachable code
(JSC::Wasm::ModuleParser::parseData): mark unreachable code
* wasm/js/WebAssemblyFunction.cpp:
(JSC::callWebAssemblyFunction): NFC: call the new function below
(JSC::WebAssemblyFunction::call): separate this out so that the start function can use it
* wasm/js/WebAssemblyFunction.h:
* wasm/js/WebAssemblyModuleRecord.cpp:
(JSC::WebAssemblyModuleRecord::visitChildren): visit the start function
(JSC::WebAssemblyModuleRecord::link): handle start function
(JSC::WebAssemblyModuleRecord::evaluate): call the start function, if present
* wasm/js/WebAssemblyModuleRecord.h:

Modified Paths

Added Paths

Diff

Modified: trunk/JSTests/ChangeLog (209641 => 209642)


--- trunk/JSTests/ChangeLog	2016-12-10 02:03:45 UTC (rev 209641)
+++ trunk/JSTests/ChangeLog	2016-12-10 02:34:02 UTC (rev 209642)
@@ -1,3 +1,16 @@
+2016-12-09  JF Bastien  <jfbast...@apple.com>
+
+        WebAssembly JS API: implement start function
+        https://bugs.webkit.org/show_bug.cgi?id=165150
+
+        Reviewed by Saam Barati.
+
+        * wasm/Builder.js: allow building a .Start()
+        * wasm/Builder_WebAssemblyBinary.js:
+        * wasm/js-api/test_Start.js: Added.
+        (const.emitters.Start): serialize a start section
+        * wasm/self-test/test_BuilderJSON.js: validate the start section's content
+
 2016-12-09  Saam Barati  <sbar...@apple.com>
 
         WebAssembly JS API: implement importing and defining Memory

Modified: trunk/JSTests/wasm/Builder.js (209641 => 209642)


--- trunk/JSTests/wasm/Builder.js	2016-12-10 02:03:45 UTC (rev 209641)
+++ trunk/JSTests/wasm/Builder.js	2016-12-10 02:34:02 UTC (rev 209642)
@@ -437,8 +437,16 @@
                 break;
 
             case "Start":
-                // FIXME implement start https://bugs.webkit.org/show_bug.cgi?id=161709
-                this[section] = () => { throw new Error(`Unimplemented: section type "${section}"`); };
+                this[section] = function(functionIndexOrName) {
+                    const s = this._addSection(section);
+                    const startBuilder = {
+                        End: () => this,
+                    };
+                    if (typeof(functionIndexOrName) !== "number" && typeof(functionIndexOrName) !== "string")
+                        throw new Error(`Start section's function index  must either be a number or a string`);
+                    s.data.push(functionIndexOrName);
+                    return startBuilder;
+                };
                 break;
 
             case "Element":
@@ -456,6 +464,7 @@
                             const typeSection = builder._getSection("Type");
                             const importSection = builder._getSection("Import");
                             const exportSection = builder._getSection("Export");
+                            const startSection = builder._getSection("Start");
                             const codeSection = s;
                             if (exportSection) {
                                 for (const e of exportSection.data) {
@@ -488,6 +497,22 @@
                                     }
                                 }
                             }
+                            if (startSection) {
+                                const start = startSection.data[0];
+                                let mapped = builder._getFunctionFromIndexSpace(start);
+                                if (!builder._checked) {
+                                    if (typeof(mapped) === "undefined")
+                                        mapped = start; // In unchecked mode, simply use what was provided if it's nonsensical.
+                                    assert.isA(start, "number"); // It can't be too nonsensical, otherwise we can't create a binary.
+                                    startSection.data[0] = start;
+                                } else {
+                                    if (typeof(mapped) === "undefined")
+                                        throw new Error(`Start section refers to non-existant function '${start}'`);
+                                    if (typeof(start) === "string" || typeof(start) === "object")
+                                        startSection.data[0] = mapped;
+                                    // FIXME in checked mode, test that the type is acceptable for start function. We probably want _registerFunctionToIndexSpace to also register types per index. https://bugs.webkit.org/show_bug.cgi?id=165658
+                                }
+                            }
                             return builder;
                         },
 

Modified: trunk/JSTests/wasm/Builder_WebAssemblyBinary.js (209641 => 209642)


--- trunk/JSTests/wasm/Builder_WebAssemblyBinary.js	2016-12-10 02:03:45 UTC (rev 209641)
+++ trunk/JSTests/wasm/Builder_WebAssemblyBinary.js	2016-12-10 02:34:02 UTC (rev 209642)
@@ -115,7 +115,9 @@
             }
         }
     },
-    Start: (section, bin) => { throw new Error(`Not yet implemented`); },
+    Start: (section, bin) => {
+        put(bin, "varuint32", section.data[0]);
+    },
     Element: (section, bin) => { throw new Error(`Not yet implemented`); },
 
     Code: (section, bin) => {

Added: trunk/JSTests/wasm/js-api/test_Start.js (0 => 209642)


--- trunk/JSTests/wasm/js-api/test_Start.js	                        (rev 0)
+++ trunk/JSTests/wasm/js-api/test_Start.js	2016-12-10 02:34:02 UTC (rev 209642)
@@ -0,0 +1,35 @@
+import * as assert from '../assert.js';
+import Builder from '../Builder.js';
+
+(function StartNamedFunction() {
+    const b = (new Builder())
+        .Type().End()
+        .Import()
+            .Function("imp", "func", { params: ["i32"] })
+        .End()
+        .Function().End()
+        .Start("foo").End()
+        .Code()
+            .Function("foo", { params: [] })
+                .I32Const(42)
+                .Call(0) // Calls func(42).
+            .End()
+        .End();
+    const bin = b.WebAssembly().get();
+    const module = new WebAssembly.Module(bin);
+    let value = 0;
+    const setter = v => value = v;
+    const instance = new WebAssembly.Instance(module, { imp: { func: setter } });
+    assert.eq(value, 42);
+})();
+
+(function InvalidStartFunctionIndex() {
+    const b = (new Builder())
+        .setChecked(false)
+        .Type().End()
+        .Function().End()
+        .Start(0).End() // Invalid index.
+        .Code().End();
+    const bin = b.WebAssembly().get();
+    assert.throws(() => new WebAssembly.Module(bin), Error, `couldn't parse section Start: Start function declaration (evaluating 'new WebAssembly.Module(bin)')`);
+})();

Modified: trunk/JSTests/wasm/self-test/test_BuilderJSON.js (209641 => 209642)


--- trunk/JSTests/wasm/self-test/test_BuilderJSON.js	2016-12-10 02:03:45 UTC (rev 209641)
+++ trunk/JSTests/wasm/self-test/test_BuilderJSON.js	2016-12-10 02:34:02 UTC (rev 209642)
@@ -241,6 +241,57 @@
     assert.throws(() => e.Function("foo", 0, { params: ["i32"] }), Error, `Not the same: "1" and "0": Re-exporting import "bar" as "foo" has mismatching type`);
 })();
 
+(function StartInvalidNumberedFunction() {
+    const b = (new Builder())
+        .Type().End()
+        .Function().End()
+        .Start(0).End()
+    assert.throws(() => b.Code().End(), Error, `Start section refers to non-existant function '0'`);
+})();
+
+(function StartInvalidNamedFunction() {
+    const b = (new Builder())
+        .Type().End()
+        .Function().End()
+        .Start("foo").End();
+    assert.throws(() => b.Code().End(), Error, `Start section refers to non-existant function 'foo'`);
+})();
+
+(function StartNamedFunction() {
+    const b = (new Builder())
+        .Type().End()
+        .Function().End()
+        .Start("foo").End()
+        .Code()
+            .Function("foo", { params: [] }).End()
+        .End();
+    const j = JSON.parse(b.json());
+    assert.eq(j.section.length, 4);
+    assert.eq(j.section[2].name, "Start");
+    assert.eq(j.section[2].data.length, 1);
+    assert.eq(j.section[2].data[0], 0);
+})();
+
+/* FIXME implement checking of signature https://bugs.webkit.org/show_bug.cgi?id=165658
+(function StartInvalidTypeArg() {
+    const b = (new Builder())
+        .Type().End()
+        .Function().End()
+        .Start("foo").End()
+    assert.throws(() => b.Code().Function("foo", { params: ["i32"] }).End(), Error, `???`);
+})();
+
+(function StartInvalidTypeReturn() {
+    const b = (new Builder())
+        .Type().End()
+        .Function().End()
+        .Start("foo").End()
+    assert.throws(() => b.Code().Function("foo", { params: [], ret: "i32" }).I32Const(42).Ret().End(), Error, `???`);
+})();
+*/
+
+// FIXME test start of import or table. https://bugs.webkit.org/show_bug.cgi?id=165658
+
 (function EmptyCodeSection() {
     const b = new Builder();
     b.Code();

Modified: trunk/Source/_javascript_Core/ChangeLog (209641 => 209642)


--- trunk/Source/_javascript_Core/ChangeLog	2016-12-10 02:03:45 UTC (rev 209641)
+++ trunk/Source/_javascript_Core/ChangeLog	2016-12-10 02:34:02 UTC (rev 209642)
@@ -1,3 +1,27 @@
+2016-12-09  JF Bastien  <jfbast...@apple.com>
+
+        WebAssembly JS API: implement start function
+        https://bugs.webkit.org/show_bug.cgi?id=165150
+
+        Reviewed by Saam Barati.
+
+        * wasm/WasmFormat.h: pass the start function around
+        * wasm/WasmModuleParser.cpp:
+        (JSC::Wasm::ModuleParser::parseTable): mark unreachable code
+        (JSC::Wasm::ModuleParser::parseGlobal): mark unreachable code
+        (JSC::Wasm::ModuleParser::parseStart): mark unreachable code
+        (JSC::Wasm::ModuleParser::parseElement): mark unreachable code
+        (JSC::Wasm::ModuleParser::parseData): mark unreachable code
+        * wasm/js/WebAssemblyFunction.cpp:
+        (JSC::callWebAssemblyFunction): NFC: call the new function below
+        (JSC::WebAssemblyFunction::call): separate this out so that the start function can use it
+        * wasm/js/WebAssemblyFunction.h:
+        * wasm/js/WebAssemblyModuleRecord.cpp:
+        (JSC::WebAssemblyModuleRecord::visitChildren): visit the start function
+        (JSC::WebAssemblyModuleRecord::link): handle start function
+        (JSC::WebAssemblyModuleRecord::evaluate): call the start function, if present
+        * wasm/js/WebAssemblyModuleRecord.h:
+
 2016-12-09  Filip Pizlo  <fpi...@apple.com>
 
         GC might be forced to look at a nuked object due to ordering of AllocatePropertyStorage, MaterializeNewObject, and PutStructure

Modified: trunk/Source/_javascript_Core/wasm/WasmFormat.cpp (209641 => 209642)


--- trunk/Source/_javascript_Core/wasm/WasmFormat.cpp	2016-12-10 02:03:45 UTC (rev 209641)
+++ trunk/Source/_javascript_Core/wasm/WasmFormat.cpp	2016-12-10 02:34:02 UTC (rev 209642)
@@ -33,6 +33,8 @@
 
 namespace JSC { namespace Wasm {
 
+JS_EXPORT_PRIVATE ModuleInformation::~ModuleInformation() { }
+
 } } // namespace JSC::Wasm
 
 #endif // ENABLE(WEBASSEMBLY)

Modified: trunk/Source/_javascript_Core/wasm/WasmFormat.h (209641 => 209642)


--- trunk/Source/_javascript_Core/wasm/WasmFormat.h	2016-12-10 02:03:45 UTC (rev 209641)
+++ trunk/Source/_javascript_Core/wasm/WasmFormat.h	2016-12-10 02:34:02 UTC (rev 209642)
@@ -35,6 +35,7 @@
 #include "WasmMemoryInformation.h"
 #include "WasmOps.h"
 #include "WasmPageCount.h"
+#include <wtf/Optional.h>
 #include <wtf/Vector.h>
 
 namespace JSC {
@@ -123,6 +124,9 @@
     Vector<Signature*> internalFunctionSignatures;
     MemoryInformation memory;
     Vector<Export> exports;
+    std::optional<uint32_t> startFunctionIndexSpace;
+
+    ~ModuleInformation();
 };
 
 struct UnlinkedWasmToWasmCall {

Modified: trunk/Source/_javascript_Core/wasm/WasmModuleParser.cpp (209641 => 209642)


--- trunk/Source/_javascript_Core/wasm/WasmModuleParser.cpp	2016-12-10 02:03:45 UTC (rev 209641)
+++ trunk/Source/_javascript_Core/wasm/WasmModuleParser.cpp	2016-12-10 02:34:02 UTC (rev 209642)
@@ -303,7 +303,8 @@
 
 bool ModuleParser::parseTable()
 {
-    // FIXME
+    // FIXME implement table https://bugs.webkit.org/show_bug.cgi?id=164135
+    RELEASE_ASSERT_NOT_REACHED();
     return true;
 }
 
@@ -365,6 +366,7 @@
 bool ModuleParser::parseGlobal()
 {
     // FIXME https://bugs.webkit.org/show_bug.cgi?id=164133
+    RELEASE_ASSERT_NOT_REACHED();
     return true;
 }
 
@@ -415,7 +417,15 @@
 
 bool ModuleParser::parseStart()
 {
-    // FIXME https://bugs.webkit.org/show_bug.cgi?id=161709
+    uint32_t startFunctionIndex;
+    if (!parseVarUInt32(startFunctionIndex)
+        || startFunctionIndex >= m_functionIndexSpace.size())
+        return false;
+    Signature* signature = m_functionIndexSpace[startFunctionIndex].signature;
+    if (signature->arguments.size() != 0
+        || signature->returnType != Void)
+        return false;
+    m_module->startFunctionIndexSpace = startFunctionIndex;
     return true;
 }
 
@@ -422,6 +432,7 @@
 bool ModuleParser::parseElement()
 {
     // FIXME https://bugs.webkit.org/show_bug.cgi?id=161709
+    RELEASE_ASSERT_NOT_REACHED();
     return true;
 }
 
@@ -450,6 +461,7 @@
 bool ModuleParser::parseData()
 {
     // FIXME https://bugs.webkit.org/show_bug.cgi?id=161709
+    RELEASE_ASSERT_NOT_REACHED();
     return true;
 }
 

Modified: trunk/Source/_javascript_Core/wasm/js/WebAssemblyFunction.cpp (209641 => 209642)


--- trunk/Source/_javascript_Core/wasm/js/WebAssemblyFunction.cpp	2016-12-10 02:03:45 UTC (rev 209641)
+++ trunk/Source/_javascript_Core/wasm/js/WebAssemblyFunction.cpp	2016-12-10 02:34:02 UTC (rev 209642)
@@ -92,8 +92,20 @@
         argCount = boxedArgs.size();
     }
 
+    // Note: we specifically use the WebAssemblyFunction as the callee to begin with in the ProtoCallFrame.
+    // The reason for this is that calling into the llint may stack overflow, and the stack overflow
+    // handler might read the global object from the callee. The JSWebAssemblyCallee doesn't have a
+    // global object, but the WebAssemblyFunction does.
+    ProtoCallFrame protoCallFrame;
+    protoCallFrame.init(nullptr, wasmFunction, firstArgument, argCount, remainingArgs);
+
+    return wasmFunction->call(vm, &protoCallFrame);
+}
+
+EncodedJSValue WebAssemblyFunction::call(VM& vm, ProtoCallFrame* protoCallFrame)
+{
     // Setup the memory that the entrance loads.
-    if (JSWebAssemblyMemory* memory = wasmFunction->instance()->memory()) {
+    if (JSWebAssemblyMemory* memory = instance()->memory()) {
         Wasm::Memory* wasmMemory = memory->memory();
         vm.topWasmMemoryPointer = wasmMemory->memory();
         vm.topWasmMemorySize = wasmMemory->size();
@@ -102,20 +114,13 @@
         vm.topWasmMemorySize = 0;
     }
 
-    // Note: we specifically use the WebAsseblyFunction as the callee to begin with in the ProtoCallFrame.
-    // The reason for this is that calling into the llint may stack overflow, and the stack overflow
-    // handler might read the global object from the callee. The JSWebAssemblyCallee doesn't have a
-    // global object, but the WebAssemblyFunction does.
-    ProtoCallFrame protoCallFrame;
-    protoCallFrame.init(nullptr, wasmFunction, firstArgument, argCount, remainingArgs);
-    
     JSWebAssemblyInstance* prevJSWebAssemblyInstance = vm.topJSWebAssemblyInstance;
-    vm.topJSWebAssemblyInstance = wasmFunction->instance();
-    EncodedJSValue rawResult = vmEntryToWasm(wasmFunction->webAssemblyCallee()->jsToWasmEntryPoint(), &vm, &protoCallFrame);
+    vm.topJSWebAssemblyInstance = instance();
+    EncodedJSValue rawResult = vmEntryToWasm(webAssemblyCallee()->jsToWasmEntryPoint(), &vm, protoCallFrame);
     vm.topJSWebAssemblyInstance = prevJSWebAssemblyInstance;
 
     // FIXME is this correct? https://bugs.webkit.org/show_bug.cgi?id=164876
-    switch (signature->returnType) {
+    switch (signature()->returnType) {
     case Wasm::Void:
         return JSValue::encode(jsUndefined());
     case Wasm::I32:

Modified: trunk/Source/_javascript_Core/wasm/js/WebAssemblyFunction.h (209641 => 209642)


--- trunk/Source/_javascript_Core/wasm/js/WebAssemblyFunction.h	2016-12-10 02:03:45 UTC (rev 209641)
+++ trunk/Source/_javascript_Core/wasm/js/WebAssemblyFunction.h	2016-12-10 02:34:02 UTC (rev 209642)
@@ -34,6 +34,7 @@
 
 class JSGlobalObject;
 class JSWebAssemblyCallee;
+struct ProtoCallFrame;
 class WebAssemblyInstance;
 
 namespace B3 {
@@ -62,6 +63,7 @@
         ASSERT(m_signature);
         return m_signature;
     }
+    EncodedJSValue call(VM&, ProtoCallFrame*);
 
 protected:
     static void visitChildren(JSCell*, SlotVisitor&);

Modified: trunk/Source/_javascript_Core/wasm/js/WebAssemblyModuleRecord.cpp (209641 => 209642)


--- trunk/Source/_javascript_Core/wasm/js/WebAssemblyModuleRecord.cpp	2016-12-10 02:03:45 UTC (rev 209641)
+++ trunk/Source/_javascript_Core/wasm/js/WebAssemblyModuleRecord.cpp	2016-12-10 02:34:02 UTC (rev 209642)
@@ -34,8 +34,10 @@
 #include "JSModuleEnvironment.h"
 #include "JSWebAssemblyInstance.h"
 #include "JSWebAssemblyModule.h"
+#include "ProtoCallFrame.h"
 #include "WasmFormat.h"
 #include "WebAssemblyFunction.h"
+#include <limits>
 
 namespace JSC {
 
@@ -96,6 +98,7 @@
     WebAssemblyModuleRecord* thisObject = jsCast<WebAssemblyModuleRecord*>(cell);
     Base::visitChildren(thisObject, visitor);
     visitor.append(&thisObject->m_instance);
+    visitor.append(&thisObject->m_startFunction);
 }
 
 void WebAssemblyModuleRecord::link(ExecState* state, JSWebAssemblyInstance* instance)
@@ -107,6 +110,10 @@
 
     JSWebAssemblyModule* module = instance->module();
     const Wasm::ModuleInformation& moduleInformation = module->moduleInformation();
+
+    bool hasStart = !!moduleInformation.startFunctionIndexSpace;
+    auto startFunctionIndexSpace = moduleInformation.startFunctionIndexSpace.value_or(0);
+
     SymbolTable* exportSymbolTable = module->exportSymbolTable();
     unsigned importCount = module->importCount();
 
@@ -133,6 +140,8 @@
             Wasm::Signature* signature = module->signatureForFunctionIndexSpace(exp.functionIndex);
             WebAssemblyFunction* function = WebAssemblyFunction::create(vm, globalObject, signature->arguments.size(), exp.field.string(), instance, wasmCallee, signature);
             exportedValue = function;
+            if (hasStart && startFunctionIndexSpace == exp.functionIndex)
+                m_startFunction.set(vm, this, function);
             break;
         }
         case Wasm::External::Table: {
@@ -157,6 +166,20 @@
         RELEASE_ASSERT(putResult);
     }
 
+    if (hasStart) {
+        Wasm::Signature* signature = module->signatureForFunctionIndexSpace(startFunctionIndexSpace);
+        // The start function must not take any arguments or return anything. This is enforced by the parser.
+        ASSERT(!signature->arguments.size());
+        ASSERT(signature->returnType == Wasm::Void);
+        // FIXME can start call imports / tables? This assumes not. https://github.com/WebAssembly/design/issues/896
+        if (!m_startFunction.get()) {
+            // The start function wasn't added above. It must be a purely internal function.
+            JSWebAssemblyCallee* wasmCallee = module->calleeFromFunctionIndexSpace(startFunctionIndexSpace);
+            WebAssemblyFunction* function = WebAssemblyFunction::create(vm, globalObject, signature->arguments.size(), "start", instance, wasmCallee, signature);
+            m_startFunction.set(vm, this, function);
+        }
+    }
+
     RELEASE_ASSERT(!m_instance);
     m_instance.set(vm, this, instance);
     m_moduleEnvironment.set(vm, this, moduleEnvironment);
@@ -164,10 +187,14 @@
 
 JSValue WebAssemblyModuleRecord::evaluate(ExecState* state)
 {
-    // FIXME this should call the module's `start` function, if any. https://bugs.webkit.org/show_bug.cgi?id=165150
-    // https://github.com/WebAssembly/design/blob/master/Modules.md#module-start-function
-    // The start function must not take any arguments or return anything
-    UNUSED_PARAM(state);
+    if (WebAssemblyFunction* startFunction = m_startFunction.get()) {
+        VM& vm = state->vm();
+        auto scope = DECLARE_THROW_SCOPE(vm);
+        ProtoCallFrame protoCallFrame;
+        protoCallFrame.init(nullptr, startFunction, JSValue(), 1, nullptr);
+        startFunction->call(vm, &protoCallFrame);
+        RETURN_IF_EXCEPTION(scope, { });
+    }
     return jsUndefined();
 }
 

Modified: trunk/Source/_javascript_Core/wasm/js/WebAssemblyModuleRecord.h (209641 => 209642)


--- trunk/Source/_javascript_Core/wasm/js/WebAssemblyModuleRecord.h	2016-12-10 02:03:45 UTC (rev 209641)
+++ trunk/Source/_javascript_Core/wasm/js/WebAssemblyModuleRecord.h	2016-12-10 02:34:02 UTC (rev 209642)
@@ -33,6 +33,7 @@
 namespace JSC {
 
 class JSWebAssemblyInstance;
+class WebAssemblyFunction;
 
 // Based on the WebAssembly.Instance specification
 // https://github.com/WebAssembly/design/blob/master/JS.md#webassemblyinstance-constructor
@@ -58,6 +59,7 @@
     static void visitChildren(JSCell*, SlotVisitor&);
 
     WriteBarrier<JSWebAssemblyInstance> m_instance;
+    WriteBarrier<WebAssemblyFunction> m_startFunction;
 };
 
 } // namespace JSC
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to