[wasm] [jiterp] Fix jit calls and interp entry wrappers (#86120)
authorKatelyn Gadd <kg@luminance.org>
Thu, 11 May 2023 23:18:48 +0000 (16:18 -0700)
committerGitHub <noreply@github.com>
Thu, 11 May 2023 23:18:48 +0000 (16:18 -0700)
src/mono/wasm/runtime/jiterpreter-interp-entry.ts
src/mono/wasm/runtime/jiterpreter-jit-call.ts
src/mono/wasm/runtime/jiterpreter-support.ts

index 36c016b..190c42c 100644 (file)
@@ -292,6 +292,10 @@ function flush_wasm_entry_trampoline_jit_queue() {
             builder.defineImportedFunction("i", trampImports[i][0], trampImports[i][1], true, trampImports[i][2]);
         }
 
+        // Assign import indices so they get emitted in the import section
+        for (let i = 0; i < trampImports.length; i++)
+            builder.markImportAsUsed(trampImports[i][0]);
+
         builder._generateImportSection();
 
         // Function section
index ef349c6..db38070 100644 (file)
@@ -394,7 +394,12 @@ export function mono_interp_flush_jitcall_queue(): void {
 
         // Emit function imports
         for (let i = 0; i < trampImports.length; i++)
-            builder.defineImportedFunction("i", trampImports[i][0], trampImports[i][1], true, trampImports[i][2]);
+            builder.defineImportedFunction("i", trampImports[i][0], trampImports[i][1], false, trampImports[i][2]);
+
+        // Assign import indices so they get emitted in the import section
+        for (let i = 0; i < trampImports.length; i++)
+            builder.markImportAsUsed(trampImports[i][0]);
+
         builder._generateImportSection();
 
         // Function section
index dfecf49..a5f9eb3 100644 (file)
@@ -493,7 +493,7 @@ export class WasmBuilder {
         // console.log(`referenced ${importsToEmit.length} import(s)`);
         for (let i = 0; i < importsToEmit.length; i++) {
             const ifi = importsToEmit[i];
-            // console.log(`  #${ifi.index} ${ifi.module}.${ifi.name} = ${ifi.func}`);
+            // console.log(`  #${ifi.index} ${ifi.module}.${ifi.name} = ${ifi.func}. typeIndex=${ifi.typeIndex}`);
             this.appendName(ifi.module);
             this.appendName(this.getCompressedName(ifi));
             this.appendU8(0x0); // function
@@ -556,6 +556,14 @@ export class WasmBuilder {
         return result;
     }
 
+    markImportAsUsed(name: string) {
+        const func = this.importedFunctions[name];
+        if (!func)
+            throw new Error("No imported function named " + name);
+        if (typeof (func.index) !== "number")
+            func.index = this.importedFunctionCount++;
+    }
+
     defineFunction(
         options: {
             type: string,
@@ -642,8 +650,11 @@ export class WasmBuilder {
         const func = this.importedFunctions[name];
         if (!func)
             throw new Error("No imported function named " + name);
-        if (typeof (func.index) !== "number")
+        if (typeof (func.index) !== "number") {
+            if (this.lockImports)
+                throw new Error("Import section was emitted before assigning an index to import named " + name);
             func.index = this.importedFunctionCount++;
+        }
         this.appendU8(WasmOpcode.call);
         this.appendULeb(func.index);
     }