Skip to content

feat: Limit the range of randomly generated fd in the WASI component#4549

Open
uddhavphatak wants to merge 2 commits intoWasmEdge:masterfrom
uddhavphatak:master
Open

feat: Limit the range of randomly generated fd in the WASI component#4549
uddhavphatak wants to merge 2 commits intoWasmEdge:masterfrom
uddhavphatak:master

Conversation

@uddhavphatak
Copy link

@uddhavphatak uddhavphatak commented Jan 25, 2026

Motivation

Due to some applications using the old select API, our current implementation follows the standard of WASI which means we will have a map to handle the randomly generated fd number (<= 2**31) to the actual fd number. However, in some cases, the application only accepts the fd number which is less than FD_SETSIZE (usually 1024).

It would be nice to have an API to set the range of the randomly generated fd numbers.

Details

  • Implement an internal function to control the range
  • Export this function as a configuration option in C API
  • Export a CLI option to control this flag
  • Write a section in the document

solves issue #2102

@github-project-automation github-project-automation bot moved this to Triage-required in WasmEdge Roadmap Jan 25, 2026
@github-actions github-actions bot added c-CLI An issue related to WasmEdge CLI tools c-CAPI An issue related to the WasmEdge C API c-Test An issue/PR to enhance the test suite WASI https://github.com/WebAssembly/WASI labels Jan 25, 2026
@uddhavphatak
Copy link
Author

This is my attempt at trying to solve this issue, please review and tell if I can improve anything.

If everything is fine, I will write this into the documentation

.add_option("enable-component"sv, PropComponent)
.add_option("enable-all"sv, PropAll)
.add_option("time-limit"sv, TimeLim)
.add_option("max-fd"sv, MaxWasiFd)
Copy link
Collaborator

Choose a reason for hiding this comment

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

lint problem

Copy link
Author

Choose a reason for hiding this comment

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

@LFsWang Fixed here, also spotted this issue in other file declarations, mainly due to my habitual use of tab instead of spaces. Thus fixed at those places as well

@uddhavphatak uddhavphatak force-pushed the master branch 2 times, most recently from ee942ec to 8b069cb Compare January 26, 2026 01:15
@uddhavphatak uddhavphatak requested a review from LFsWang January 26, 2026 01:16
@uddhavphatak uddhavphatak force-pushed the master branch 2 times, most recently from 979abd0 to 6f14c99 Compare January 26, 2026 01:36
Copy link
Member

@hydai hydai left a comment

Choose a reason for hiding this comment

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

This PR doesn't contain any basic verifications and exhibits some unusual behaviors, including:

  1. What happens if users set the maximum WASI FD to 0?
  2. Hard-coding 0x7FFFFFF in the C API.
  3. I don't think we need to change the init series functions. The generateRandomFdToNode is not used in these functions. Instead, we should set the MaxFD in another phase.

@github-project-automation github-project-automation bot moved this from Triage-required to In progress in WasmEdge Roadmap Jan 26, 2026
@hydai hydai marked this pull request as draft January 26, 2026 01:47
@hydai
Copy link
Member

hydai commented Jan 26, 2026

Converted to a draft PR since the contributor is not running CI on their own forked repo. There are some very basic CI checks that must fail. It's not necessary to approve the CI to avoid wasting our resources on this PR.

@uddhavphatak uddhavphatak reopened this Jan 26, 2026
@github-project-automation github-project-automation bot moved this from Done to Todo in WasmEdge Roadmap Jan 26, 2026
@github-actions github-actions bot removed c-CAPI An issue related to the WasmEdge C API c-Test An issue/PR to enhance the test suite labels Jan 26, 2026
@uddhavphatak
Copy link
Author

uddhavphatak commented Jan 26, 2026

@hydai, It was mistake from my end, I had understood the issue wrong, thus thought that I had to make changes in the init API.

I also have setup the CI on my fork, and done the CI testing before reopening the issue. should I undraft this PR?

@uddhavphatak uddhavphatak requested a review from hydai January 26, 2026 14:19
@uddhavphatak uddhavphatak marked this pull request as ready for review January 26, 2026 19:09
std::atomic<bool> CoredumpWasmgdb = false;
std::atomic<bool> ForceInterpreter = false;
std::atomic<bool> AllowAFUNIX = false;
uint32_t MaxWasiFd = 0x7FFFFFFF;
Copy link
Member

Choose a reason for hiding this comment

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

This should be atomic.

return std::string(Buffer.data(), Buffer.size());
}

void setMaxWasiFd(uint32_t Limit) noexcept {
Copy link
Member

Choose a reason for hiding this comment

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

Your naming is inconsistent. Some of them use FD, while others use Limit.

}

void setMaxWasiFd(uint32_t Limit) noexcept {
MaxFd = std::clamp(Limit, 3U, 0x7FFFFFFFU);
Copy link
Member

Choose a reason for hiding this comment

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

You are silently modifying the user's given limit; however, this is not mentioned in the option description.

@github-project-automation github-project-automation bot moved this from Todo to In progress in WasmEdge Roadmap Jan 28, 2026
Signed-off-by: uddhav Phatak <[email protected]>
@uddhavphatak uddhavphatak requested a review from hydai January 29, 2026 16:48
WasiExpect<__wasi_fd_t> generateRandomFdToNode(std::shared_ptr<VINode> Node) {
std::uniform_int_distribution<__wasi_fd_t> Distribution(0, 0x7FFFFFFF);
std::uniform_int_distribution<__wasi_fd_t> Distribution(
0, static_cast<__wasi_fd_t>(MaxFd));
Copy link
Member

Choose a reason for hiding this comment

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

There are many possible cases that are not covered.
For example, what if MaxFd = 3, but there are more than one preopened folder?
What if MaxFd is fully used, but we are trying to open a new one? The original limitation is large enough to prevent such a case. However, your new approach may not work.

Copy link
Author

Choose a reason for hiding this comment

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

@hydai I see the issue, but I have a few doubts.
can the user set the MaxFd as 3?, as it is a CLI based command (sets max limit when option "max-fd" is used), but by default, the limitation is large enough.

and in the case if MaxFd is fully used, but we are trying to open a new one. should we return an error? or should I create an implementation that will create a queue to track the files which are open/closed? WDYT

Copy link
Member

Choose a reason for hiding this comment

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

That's why I am considering it.
The reason we want this feature is that the issue report uses select, which accepts fewer than 1024 file descriptors. I prefer to set the minimum to 1024; if the given MaxFd is less than this, then we should clamp it to 1024. If MaxFd is fully used, we should return an error directly. I don't think having a queue for this is a good idea, since execution may rely on this new FD to perform certain tasks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c-CLI An issue related to WasmEdge CLI tools WASI https://github.com/WebAssembly/WASI

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

3 participants