Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compiler: ResolveImportedFunction should infer the typeID from the importing module #2314

Merged
merged 1 commit into from
Sep 16, 2024

Conversation

evacchi
Copy link
Contributor

@evacchi evacchi commented Sep 14, 2024

Fixes #2313.

This changes the signature for ResolveImportedFunction() so that it takes an additional index into the typeID section of the "current" module, i.e. the subject of the import.

The current behavior is to search for the import recursively into the chain of dependencies; however, this resolves to a typeID that is specific to that module. For instance:

  • consider the case of a module m declaring an import m2inst.sym.
  • let be m2inst.sym an import with type t with ID tid
  • let be m2inst be linked to an instance of module m2 with type t2 and ID t2id.

Now: we want t to be compatible with t2; this we verify at:

expectedType := &module.TypeSection[i.DescFunc]
src := importedModule.Source
actual := src.typeOfFunction(imported.Index)
if !actual.EqualsSignature(expectedType.Params, expectedType.Results) {
err = errorInvalidImport(i, fmt.Errorf("signature mismatch: %s != %s", expectedType, actual))
return
}

The current behavior is for ResolveImportedFunction() to resolve recursively a reference to a function so that we can then jump precisely to its address at runtime; however, we should not carry on its type ID, and instead use the type ID of the import.

In other words, even if the types are compatible, we should be still referring to type t with ID tid from m, and not type ID t2id, because, of course, the index t2id might be different from the local index tid.

The solution is to add another descFunc Index (from the name of the field Import.DescFunc) to the signature of ResolveImportedFunction(), so that the typeID is correctly resolved from moduleEngine.module.TypeIDs[descFunc] (i.e. the typeID defined in the module that is requiring the imported function).

The tests are adjusted accordingly; notably, this also means that, in order for m.ResolveImportedFunction() to work properly, m must be correctly initialized with an m.module being the module that is performing the import, so the initialization of moduleEngine is pushed down, after the declaration of the "imported" and "importing" modules.

Let me know if you think it is worth adding more tests!

@zshipko please double-check that this is fixing your issue (I have slightly changed my original approach)
@ncruces would you like to double-check this against your sqlite lib?
@anuraaga if you spare any time, would you take a look too? :P

…rrent module

Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
@zshipko
Copy link

zshipko commented Sep 14, 2024

Just tested this and verified it fixes the issue on my end! 🥳

Thank you!

@ncruces
Copy link
Collaborator

ncruces commented Sep 15, 2024

Everything seems to work fine.

@mathetake mathetake merged commit b468ada into tetratelabs:main Sep 16, 2024
44 checks passed
@evacchi evacchi deleted the fix-fnimport-typeids branch September 16, 2024 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Running into wasm error: indirect call type mismatch when using the compiler backend, but not the interpreter
4 participants