Stream: general

Topic: Host functions in start


view this post on Zulip Leonardo Yvens (Jun 12 2020 at 19:01):

I spent the last few weeks migrating from wasmi to wasmtime, and now seem to have hit a blocker. In AssemblyScript, the start function is used to initialize globals. The globals might call a host function, and that host function might then call an import from the module. But when I try it wasmtime time says the import is not found. What are the rules for what a host function called in start can do?

view this post on Zulip Yury Delendik (Jun 12 2020 at 19:05):

Do you have the example?

view this post on Zulip Leonardo Yvens (Jun 12 2020 at 19:11):

Hi, I don't have a minimal example. Do you need a minimal reproduction case?

view this post on Zulip Till Schneidereit (Jun 12 2020 at 19:58):

CC @Aaron Turner

view this post on Zulip Leonardo Yvens (Jun 12 2020 at 19:59):

I now understand my mistake. I tried to use Caller::get_export to get a function, but that only works for memories at the moment. Would it be straight forward to allow that to work for functions? I can write a patch for that.

view this post on Zulip Yury Delendik (Jun 12 2020 at 20:05):

Added in https://github.com/bytecodealliance/wasmtime/pull/1290 , only memory was needed for wasi use at that moment

This commit is a continuation of #1237 and updates the API of Func to allow defining host functions which have easy access to a caller's memory in particular. The new APIs look like so: The Fu...

view this post on Zulip Dan Gohman (Jun 12 2020 at 20:05):

The ability to obtain exports from the caller of a function is a temporary mechanism. We need it for WASI-like situations where it's necessary to grab the "memory" export of the caller in order to dereference pointers it passes, however once we have Interface Types and WASI calls, that won't be needed anymore

view this post on Zulip Dan Gohman (Jun 12 2020 at 20:07):

The current roadmap is that non-JS engines won't support cyclic dependencies, so if AssemblyScript supports cyclic dependencies, it'll need to introduce indirection, such as using call_indirect, to break the cycle.

view this post on Zulip Leonardo Yvens (Jun 12 2020 at 20:16):

I don't totally understand why this is considered a cyclic dependency, is it what happens with start -> host function -> module function nested calls? I really need this at the moment, is there no hope of that working because the plan is to not support this?

view this post on Zulip Dan Gohman (Jun 12 2020 at 20:20):

To see if I understand, it's start (in module A) -> host function -> module funciton (in module A)?

view this post on Zulip Dan Gohman (Jun 12 2020 at 20:20):

In which case, the module imports from the host, and the host imports from the module.

view this post on Zulip Leonardo Yvens (Jun 12 2020 at 20:23):

yes, is that generally wrong? I thought bi-directional communication was generally ok, we certainly use it all the time in our application.

view this post on Zulip Dan Gohman (Jun 12 2020 at 20:24):

Bidirectional communication per se is fine; it's bidirectional imports that are the issue

view this post on Zulip Leonardo Yvens (Jun 12 2020 at 20:24):

Specifically in this case, the memory allocator is implemented in WASM, so the host calls to it when it needs to allocate.

view this post on Zulip Leonardo Yvens (Jun 12 2020 at 20:25):

so there is a better way to communicate that's not imports? Or that will be interface types, when it exists?

view this post on Zulip Dan Gohman (Jun 12 2020 at 20:25):

In principle, AssemblyScript should factor out its runtime into a separate module, as described here: https://github.com/AssemblyScript/assemblyscript/issues/1301#issuecomment-635599053

I'm working on documenting the assumptions that runtimes make when running wasm modules, and I'm not very familiar with AssemblyScript and would like to learn more. Here are the exports in ...

view this post on Zulip Leonardo Yvens (Jun 12 2020 at 20:49):

I'll need to read up on the background. If this is in principle wrong, then my project is deep in wrong patterns, and I don't have a choice in the short term. I hacked in support for functions in Caller::get_exports, and it worked. Are you interested in a PR or should I keep this in a fork?

view this post on Zulip Dan Gohman (Jun 12 2020 at 20:59):

I think I'd prefer if you keep it on your side.

view this post on Zulip Dan Gohman (Jun 12 2020 at 21:00):

I can file a bug on the AssemblyScript repo to help start the conversation there though

view this post on Zulip Dan Gohman (Jun 12 2020 at 21:01):

I'll file an initial issue, and if you wanted to follow up with your use case as an example, that'd be great!

view this post on Zulip Leonardo Yvens (Jun 12 2020 at 21:03):

Alright we'll maintain a fork. If AS is the right place to discuss this, then lets take it there.

view this post on Zulip Dan Gohman (Jun 12 2020 at 21:37):

Ok, I've now filed https://github.com/AssemblyScript/assemblyscript/issues/1333

Several folks having been looking at running AssemblyScript programs in Wasmtime, and while single-module programs work great, multiple-module programs, and programs which call into host APIs that ...

view this post on Zulip Dan Gohman (Jun 12 2020 at 21:38):

If you wanted to describe your example there, that'd help!

view this post on Zulip Leonardo Yvens (Jun 12 2020 at 21:42):

Thanks! The AS maintainers are probably aware that this (anti)pattern is common, not sure I have much to add this point, but I'll subscribe to the issue and chime in if necessary.

view this post on Zulip Aaron Turner (Jun 15 2020 at 17:53):

Thanks for the mention @Till Schneidereit :) I had Friday off, so sorry for the delay, didn't see this come in.

And thank you @Dan Gohman for kicking off the discussion over in the AS repo! I'm not super familiar with the runtime internals, but it seems like dcodeIO is looking into it :)


Last updated: Dec 23 2024 at 12:05 UTC