
How does dynamic dispatch work in WebAssembly? - mnemonik
http://fitzgeraldnick.com/2018/04/26/how-does-dynamic-dispatch-work-in-wasm.html
======
repsilat
Kinda annoying, but not surprising. VMs and bytecode formats make simplifying
assumptions based on the languages they expect to be hosted on top of them,
and that usually means "Something with control flow like C."

Creative uses of computed jumps, messing with the stack, dynamic codegen, all
sorts of weird things your new language might do to efficiently implement some
new control or data structure aren't likely to be possible.

At least in the short term nobody is going to be too upset. Today if something
needs to wring all the power available from your CPU it isn't reasonable to
put it on the web. That will continue to be true. WASM is the wise 80%
solution, not a toy for ASM hackers and people messing around with weird
prototype programming languages.

~~~
ajross
The reason behind the limited branching instructions in wasm is validation,
not compatibility with C. C compilers on real architectures emit all sorts of
crazy indirection mechanisms.

~~~
repsilat
Right -- the control flow allowed is basically the simplest thing that will
allow you to make a C-like language in a relatively efficient way. It isn't
aimed at any particular compiler, it's aimed at supporting some semantics.

Providing additional flexibility on top of those semantics is "expensive" in
terms of implementor-time and effort to get safety, portability and future-
proofing. There's no promise that the fun extensions they might want today go
"in the right direction", and the cost of going down a bad path is high for
standards.

------
amluto
Someone should make a CPU that does this. Intel CET is a bit of a start, but a
CALL instruction that takes a “type” operand and only calls functions of that
type would be a big improvement. So would a totally separate return address
stack.

~~~
haberman
There is also pointer authentication in ARM:
[https://lwn.net/Articles/718888/](https://lwn.net/Articles/718888/)

It seems pretty difficult to define function types at the ISA level, since
anything to do with typing is language/VM-specific. What types can arguments
have? Are varargs supported? Multiple parameter returns?

Maybe the type would just be an integer that the ABI would assign meaning to.
But if you did that, how would the function's type be determined? Some pseudo-
instruction at the CALL target? I guess looking at CET it does have some
things like this: the ENDBRANCH instructions notate valid indirect branch
targets.

~~~
amluto
> Maybe the type would just be an integer that the ABI would assign meaning
> to. But if you did that, how would the function's type be determined? Some
> pseudo-instruction at the CALL target?

Exactly.

------
merlish
How do you implement co-operative multitasking in WebAssembly?

Do you have to analyze the source program & know where every possible call to
a yield is, and store all resulting suffixes of a function as new functions?

------
foota
I'll be interested to read about the first WASM vulnerabilities that are
revealed.

------
gok
So how are function pointers in (say) C implemented?

~~~
geofft
From playing around with
[https://mbebenita.github.io/WasmExplorer/](https://mbebenita.github.io/WasmExplorer/)
: it seems like any function you ever take the address of gets stored in the
table, and wasm just passes the index in that table around. And if you do
things like cast an integer to a function pointer, you actually just cast it
to an index in the table.

C is high-level enough that IIRC it doesn't guarantee that arbitrary addresses
can successfully be used as function pointers; the only valid function
pointers are results of addresses of functions, or (possibly) values that have
been cast from and then to them. Which is enough for wasm.

Try compiling this (to wat, which is an S-expression format that otherwise
resembles the assembly in this article):
[https://wasdk.github.io/WasmFiddle/?5zbjb](https://wasdk.github.io/WasmFiddle/?5zbjb)

