wasm3 / wasm3

🚀 A fast WebAssembly interpreter and the most universal WASM runtime
https://twitter.com/wasm3_engine
MIT License
7.32k stars 461 forks source link

Support multiple functions in same module sharing an implementation #198

Closed atdrendel closed 3 years ago

atdrendel commented 3 years ago

Currently, if multiple named exported functions share a single implementation, Wasm3 only exposes one of the exported functions to clients. This is because only the first name is saved to M3Function inside ParseSection_Export()

if (not io_module->functions [index].name)
{
  io_module->functions [index].name = utf8;
  utf8 = NULL; // ownership transfered to M3Function
}

A simple WebAssembly module illustrates this problem:

(module
  (type (;0;) (func (result i32)))
  (func (;0;) (type 0) (result i32)
    i32.const 42)
  (export "first" (func 0))
  (export "second" (func 0)))

If you try to use m3_FindFunction() to find first(), you will succeed. However, trying to find second() will fail.

We have run into this problem when using Wasm3 with Libsodium. Libsodium exposes a number of different functions that wrap private constants, some of which are equal. These functions are all compiled down into the same WebAssembly function implementation with different exported names.

atdrendel commented 3 years ago

I've opened this pull request with a possible fix.

vshymanskyy commented 3 years ago

merged