8000 Fix dup/dup2 + Fix fcntl + New fdtables API by Yaxuan-w · Pull Request #117 · Lind-Project/lind-wasm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix dup/dup2 + Fix fcntl + New fdtables API #117

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

Merged
merged 14 commits into from
Apr 21, 2025
Merged

Fix dup/dup2 + Fix fcntl + New fdtables API #117

merged 14 commits into from
Apr 21, 2025

Conversation

Yaxuan-w
Copy link
Member
@Yaxuan-w Yaxuan-w commented Feb 22, 2025

This PR addresses issues related to dup/dup2 and fcntl. Related to bug reported in #112

Implementation details

dup/dup2:

Since RawPOSIX introduces the concept of virtual file descriptors through fdtables, duplicating a file descriptor using dup/dup2 in RawPOSIX does not require calling the underlying kernel syscall. Instead, it only needs to ensure that the two virtual fds point to the same underlying kernel fd.

One consideration is that the O_CLOEXEC flag is not automatically inherited when duplicating a file descriptor. Therefore, we need to explicitly set it to false for the duplicated fd.

fcntl:

Due to the design of fdtables, we need to handle F_DUPFD, F_DUPFD_CLOEXEC, F_GETFD, and F_SETFD separately. The operations F_DUPFD and F_DUPFD_CLOEXEC cannot directly use RawPOSIX dup/dup2 calls because fcntl allocates the lowest available fd starting from the given argument.

To accommodate this behavior, I added a new fdtables API specifically for handling these cases.

fdtables:

As mentioned above, I introduced a new API in all four algorithms in fdtables to support fcntl operations. I considered to change arguments list of original API, but this will cause modification spread to almost entire RawPOSXI codebase, since get_unused_virtual_fd is used in almost every fs syscalls. We could mark this as a TODO if we want future optimization

Test

Passed unit test:

  • tests/unit-tests/file_tests/deterministic/dupwrite.c
  • tests/unit-tests/file_tests/deterministic/dup2.c
  • tests/unit-tests/file_tests/non-deterministic/dup.c

Added new test file:

  • tests/unit-tests/file_tests/deterministic/fcntl.c

@rennergade
Copy link
Contributor

The changes look good and nice comment updates.

What I think would really be helpful here is a longerform comment explaining why we call to the kernel for dup here even though were virtually handling file descriptors. There definitely was a decision/reason for that made at some point but I forget all the details so was struggling to reason through this, seems like an issue that deserves some detail for people looking at this in the future.

@Yaxuan-w Yaxuan-w mentioned this pull request Mar 18, 2025
8 tasks
@Yaxuan-w Yaxuan-w changed the title Fix dup/dup2 error handling / Fix dup args list / Add comments Fix dup/dup2 + Fix fcntl + New fdtables API Mar 20, 2025
@Yaxuan-w Yaxuan-w requested a review from rennergade March 20, 2025 20:29
Copy link
Contributor
@rennergade rennergade left a comment

Choose a reason for hiding this comment

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

Looks great. I asked for a few additional comments but think this is in good shape.

@Yaxuan-w Yaxuan-w requested a review from rennergade March 21, 2025 15:01
rennergade
rennergade previously approved these changes Mar 27, 2025
Copy link
Contributor
@rennergade rennergade left a comment

Choose a reason for hiding this comment

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

This looks good now from my end.

@Yaxuan-w Yaxuan-w requested a review from JustinCappos March 31, 2025 00:05
@JustinCappos
8000
Copy link
Member

The way I implemented fd tracking didn't take fcntl's behavior into account. I'm fine with what you've done.

One question is what real world code uses fcntl like this and would break if we did dup2 / grabbed an available fd (but not the lowest) instead? We're likely to be much, much, much slower than native here.

@Yaxuan-w
Copy link
Member Author

One question is what real world code uses fcntl like this and would break if we did dup2 / grabbed an available fd (but not the lowest) instead? We're likely to be much, much, much slower than native here.

Theoretically, the new API shouldn't introduce additional performance overhead compared to dup2 or grabbing an available fd. The only difference is just offset by the different starting point in the FD search. The rest of the logic remains essentially the same.

Motivated by this, I started doing some benchmarks (#62) to better understand the performance in lind-wasm to help optimize implementation

@rennergade
Copy link
Contributor

This looks good to me now but we should be sure as to why this is failing the CI (I know its a WIP for now but still is useful). When it's failing early like this it usually means RawPOSIX isn't building correctly so lets confirm that.

@Yaxuan-w
Copy link
Member Author

When it's failing early like this it usually means RawPOSIX isn't building correctly so lets confirm that.

I can run unit-test on server. I'm going to check about CI.

@Yaxuan-w
Copy link
Member Author
Yaxuan-w commented Apr 21, 2025

but we should be sure as to why this is failing the CI (I know its a WIP for now but still is useful). When it's failing early like this it usually means RawPOSIX isn't building correctly so let's confirm that.

@rennergade, I talked with @m-hemmings about the failure. This PR actually passed all checks,
image (1)

but it took over an hour, so it timed out and reported an error.
image

This should stop happening once we merge #154.

@rennergade
Copy link
Contributor

Ok great I appreciate you looking into this.

@rennergade, I talked with @m-hemmings about the failure. This PR actually passed all checks, ![image
)

This should stop happening once we merge #154.

rennergade
rennergade previously approved these changes Apr 21, 2025
Copy link
Contributor
@rennergade rennergade left a comment

Choose a reason for hiding this comment

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

Approved. Should to be good to merge with approval from @JustinCappos

JustinCappos
JustinCappos previously approved these changes Apr 21, 2025
Copy link
Member
@JustinCappos JustinCappos left a comment

Choose a reason for hiding this comment

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

Looks good. One very minor change requested to a comment. Please fix and then squash and merge.

@Yaxuan-w Yaxuan-w dismissed stale reviews from JustinCappos and rennergade via 4a9d1c1 April 21, 2025 22:50
@Yaxuan-w Yaxuan-w merged commit fc86357 into main Apr 21, 2025
1 check failed
@Yaxuan-w Yaxuan-w deleted the fix-dup branch April 21, 2025 22:51
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.

3 participants
0