Stream: wasm

Topic: Setting the stack in clang


view this post on Zulip Dag Ågren (Oct 19 2020 at 15:59):

I am trying to run some code from two different native threads at the same time. WAVM supports this case fine, but it seems clang will use the same stack for both threads and thus crash the code horribly.

view this post on Zulip Dag Ågren (Oct 19 2020 at 15:59):

Is there a way to set a custom stack for one of the threads? I was pointed at --export __stack_pointer to get access to the stack pointer global and overwriting it, but it seems the symbol is not actually exported if you do this.

view this post on Zulip Dag Ågren (Oct 19 2020 at 16:00):

Any other trick I might use to set a new stack?

view this post on Zulip Yury Delendik (Oct 19 2020 at 16:05):

The https://bytecodealliance.zulipchat.com/#narrow/stream/206238-general/topic/Obtaining.20stack_pointer.3F might be helpful

view this post on Zulip Dag Ågren (Oct 19 2020 at 16:07):

Well, it's saying this doesn't work, that's all I got from it?

view this post on Zulip Yury Delendik (Oct 19 2020 at 16:08):

correct, you can export your own utility functions to mingle with mutable global though

view this post on Zulip Dag Ågren (Oct 19 2020 at 16:09):

I read elsewhere that you can't actually access globals in clang though, do I need to implement them directly in wat instead or something?

view this post on Zulip Yury Delendik (Oct 19 2020 at 16:09):

setting a new stack in LLVM compiled code at this moment is more like a hack

view this post on Zulip Dag Ågren (Oct 19 2020 at 16:11):

Well, I'm working on a longer-term project where I am hoping the tooling will catch up with me eventually, but I need the hack to be able to do the work for now.

view this post on Zulip Yury Delendik (Oct 19 2020 at 16:12):

so if the runtime will do wasm stack switching it can also do shadow stack as well

view this post on Zulip Yury Delendik (Oct 19 2020 at 16:13):

How do you plan to switch wasm call stack?

view this post on Zulip Dag Ågren (Oct 19 2020 at 16:14):

Just allocating a bit of memory and pointing the stack pointer for my extra thread there, and keeping the main stack for the main thread.

view this post on Zulip Dag Ågren (Oct 19 2020 at 16:14):

(I need to do audio processing, which happens in a background thread on the native side, the rest happens in a main thread.)

view this post on Zulip Dag Ågren (Oct 19 2020 at 16:21):

But the main question is, how do I actually do that in practice. If I had the global, I could use the API on the native side to set it, I guess. But it does not get exported. I could just assume it is global 0, but not sure if the WAVM API would let me do that... Doing it on the WASM side would be slightly easier since then I can actually allocate the memory too, but I don't know how to access the global then?

view this post on Zulip Dag Ågren (Oct 19 2020 at 23:03):

Finally managed to create a function in a WAT file that I got to link that sets __stack_pointer, and it works. Thanks!


Last updated: Nov 22 2024 at 16:03 UTC