Skip to content

Opt-in busywait mode for futexes #562

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kateinoigakukun
Copy link
Contributor

@kateinoigakukun kateinoigakukun commented Jan 21, 2025

We are heavily using wasi-libc + wasip1-threads on browsers. Since wasi-libc uses memory.atomic.wait32 to implement futex, and the usage of memory.atomic.wait32 on the main thread is prohibited on browsers, we currently need to write code carefully not to reach the instruction.

Emscripten addresses this limitation by employing a busy-wait on the main thread as a workaround. Rust has always employs busywait regardless of the current thread is main. This approach, while effective for browsers, introduces unnecessary overhead in environments where memory.atomic.wait32 is permitted.

This change provides a similar solution by introducing an opt-in busy-wait mode for futexes. By making this an optional feature, we avoid imposing the overhead of busy-waiting in non-browser environments while ensuring compatibility with browser-based restrictions.

#include <wasi/libc-busywait.h>

/// Enable busywait in futex on current thread.
void __wasilibc_enable_futex_busywait_on_current_thread(void);

This change slightly adds some runtime overheads in futex to check if we should use busywait, but it can be optimized away as long as __wasilibc_enable_futex_busywait_on_current_thread is not used by user program and LTO is enabled.

@kateinoigakukun kateinoigakukun force-pushed the pr-98c3e8941de60bb9c8df94605fb1b211dc949a29 branch from 84a2ff9 to 57331a7 Compare January 21, 2025 05:03
abrown pushed a commit that referenced this pull request Feb 3, 2025
This commit adds a browser test harness to run the tests in the browser.

## Motivation

We are heavily using wasi-libc on browsers but we don't have any test
for the case in wasi-libc. In theory, there should be no behavioral
difference between wasmtime and browsers (as long as WASI
implementations are correct) but browsers have their own limitations in
their Wasm engines. For example, memory.atomic.wait32 is not permitted
on the main thread.

We are working on adding some mitigation for such browser-specific
issues (#562) and this test
harness will help to validate the fix.
@kateinoigakukun kateinoigakukun marked this pull request as ready for review February 4, 2025 14:37
@yamt
Copy link
Contributor

yamt commented Apr 16, 2025

We are heavily using wasi-libc + wasip1-threads on browsers. Since wasi-libc uses memory.atomic.wait32 to implement futex, and the usage of memory.atomic.wait32 on the main thread is prohibited on browsers, we currently need to write code carefully not to reach the instruction.

what happens when the instruction is executed on the main thread? does it trap?

@@ -64,6 +64,9 @@ int __wasilibc_rename_oldat(int olddirfd, const char *oldpath, const char *newpa
int __wasilibc_rename_newat(const char *oldpath, int newdirfd, const char *newpath)
__attribute__((__warn_unused_result__));

/// Enable busywait in futex on current thread.
void __wasilibc_enable_futex_busywait_on_current_thread(void);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while currently futex might be only the user of the blocking instruction, i guess it's better to name the api to make it clear it's about any blocking instructions, not necessarily for futex.
what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm also not a fan of the current name. Do you have any better idea?

@kateinoigakukun
Copy link
Contributor Author

what happens when the instruction is executed on the main thread? does it trap?

Yes, it traps. From the JS embedder's view, it throws a JS exception.

@yamt
Copy link
Contributor

yamt commented Apr 17, 2025

after skimming the relevant discussions, i have a question.
how is the new api supposed to be used?
in the test code in this PR, it's hardcoded in main().
however, in general, a thread, even if it's C main(), can't know if it's on the web "main" thread or not, right?
if it's fine to hardcode the knowledge in the user program, the program can just override __wasilibc_futex_wait or whatever, can't it? i guess we can even provide a small library to override relevant libc logic.

@kateinoigakukun
Copy link
Contributor Author

kateinoigakukun commented Apr 17, 2025

how is the new api supposed to be used?
in the test code in this PR, it's hardcoded in main().
however, in general, a thread, even if it's C main(), can't know if it's on the web "main" thread or not, right?

Yes, that's right. A user program needs to have a main entry point for each platform (web or not) and it's fine (at least for us) to hardcode the platform knowledge in the entry point of main and threads.

If we really don't want to hardcode the platform knowledge into the user program, it might be an option to export the enable function and ask embedders to call it.

if it's fine to hardcode the knowledge in the user program, the program can just override __wasilibc_futex_wait or whatever, can't it? i guess we can even provide a small library to override relevant libc logic.

Actually we are using the approach now, overriding the futex functions in the user program but we consider it as a workaround. We thought we just need to hook __wasilibc_futex_wait but we realized we also need to hook other functions calling __wasilibc_futex_wait in the same compile-unit (for now it's only __wait though).

So to make it overriding the default mode reliably, we have some options:

  1. Add a builtin opt-in mode for busywait like this PR
  2. Split out __wasilibc_futex_wait into a dedicated file to allow applications reliably overriding it
  3. On the top-of 2, we can provide a supplemental library that contains __wasilibc_busywait.c added in this PR.

1 or 3 are best for us and other on-browser p1-threads users but 2 is better than nothing for us.

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.

2 participants