Skip to content

fix(wasix): honour open(O_CREAT | O_EXCL) for symlinks + concurrent creates - #7205

Open
vardhan wants to merge 1 commit into
wasmerio:mainfrom
vardhan:fix/wasix-exclusive-create-symlink
Open

vardhan wants to merge 1 commit into
wasmerio:mainfrom
vardhan:fix/wasix-exclusive-create-symlink

Conversation

@vardhan

@vardhan vardhan commented Nov 21, 2026 •

Copy link
Copy Markdown

Description

While porting the Go toolchain to WASIX, I ran into failing filesystem tests in the Go repository. These fixes let the toolchain build on WASIX. The primary issue is that path_open2() doesn't fully adhere to posix semantics.

POSIX requires open(path, O_CREAT | O_EXCL, ...) to check and create the final directory entry atomically. If that entry exists, the call must fail with EEXIST, even when it is a dangling symlink. If two callers race to create the same absent file, only one can succeed. POSIX open()

The bug before this PR

path_open2 applied ordinary symlink lookup rules to an exclusive create, so a final symlink could yield ELOOP. If another caller won the create race, Wasmer translated the backing filesystem’s AlreadyExists into EPERM. The overlay filesystem also cleared the exclusive flag while checking its writable layer, which could open or truncate an existing file.

This PR fixes

The fix checks the final entry without following it, returns EEXIST when a competing create wins, and preserves the exclusive flag in the overlay. On backends that emulate symlinks, file creation and symlink registration share a lock so they cannot both claim the same name.

The overlay uses a deletion marker to hide a file from a lower layer. When a guest reuses that name for a symlink, the overlay now removes the marker only after the writable layer creates the link. If the backend needs an in-memory symlink instead, the marker stays in place. The guest can read through the link and later remove it without making the original file visible again.

Tests

  • [new] exclusive-create-symlink opens a dangling symlink with O_CREAT | O_EXCL. Adding O_NOFOLLOW used to yield ELOOP. Both opens must return EEXIST and leave the target absent.
  • [new] concurrent-exclusive-create races two threads to create each of 32 files. The losing thread could get EPERM. Each race must have one winner, and the loser must get EEXIST.
  • [new] exclusive-create-symlink-fallback makes a file appear between lookup and create on a backend that emulates symlinks. Wasmer used to translate that collision into EPERM. The guest must get EEXIST.
  • [new] create_new_preserves_existing_primary_file requests exclusive creation with truncation. The overlay used to open the existing file while checking its writable layer. The test checks both the error and the original contents.
  • [new] symlink-over-deleted-file deletes a file, reuses its name for a symlink, reads through the link, and removes it. On the fallback backend, attempting to create the link could expose the deleted file again. The original must stay hidden throughout.
  • [new] ephemeral_symlink_and_backing_create_cannot_both_claim_a_name pauses a file create while a second caller tries to register a symlink at the same name. The file must win and the link must get EEXIST. Releasing the lock before the backing create made this test fail.

With this PR applied, all 46 guest path tests, 19 WASIX filesystem unit tests, and 144 virtual filesystem tests passed, with two existing ignores.

While porting the Go toolchain to WASIX, I ran into failing filesystem tests in the Go repository. These fixes let the toolchain build on WASIX.

POSIX requires `open(path, O_CREAT | O_EXCL, ...)` to check and create the final directory entry atomically. If that entry exists, the call must fail with `EEXIST`, even when it is a dangling symlink. If two callers race to create the same absent file, only one can succeed. [POSIX `open()`](https://pubs.opengroup.org/onlinepubs/9799919799/functions/open.html)

*The bug before this PR*

`path_open2` applied ordinary symlink lookup rules to an exclusive create, so a final symlink could yield `ELOOP`. If another caller won the create race, Wasmer translated the backing filesystem’s `AlreadyExists` into `EPERM`. The overlay filesystem also cleared the exclusive flag while checking its writable layer, which could open or truncate an existing file.

*This PR fixes*

The fix checks the final entry without following it, returns `EEXIST` when a competing create wins, and preserves the exclusive flag in the overlay. On backends that emulate symlinks, file creation and symlink registration share a lock so they cannot both claim the same name.

The overlay uses a deletion marker to hide a file from a lower layer. When a guest reuses that name for a symlink, the overlay now removes the marker only after the writable layer creates the link. If the backend needs an in-memory symlink instead, the marker stays in place. The guest can read through the link and later remove it without making the original file visible again.

*Tests*

- [new] `exclusive-create-symlink` opens a dangling symlink with `O_CREAT | O_EXCL`. Adding `O_NOFOLLOW` used to yield `ELOOP`. Both opens must return `EEXIST` and leave the target absent.
- [new] `concurrent-exclusive-create` races two threads to create each of 32 files. The losing thread could get `EPERM`. Each race must have one winner, and the loser must get `EEXIST`.
- [new] `exclusive-create-symlink-fallback` makes a file appear between lookup and create on a backend that emulates symlinks. Wasmer used to translate that collision into `EPERM`. The guest must get `EEXIST`.
- [new] `create_new_preserves_existing_primary_file` requests exclusive creation with truncation. The overlay used to open the existing file while checking its writable layer. The test checks both the error and the original contents.
- [new] `symlink-over-deleted-file` deletes a file, reuses its name for a symlink, reads through the link, and removes it. On the fallback backend, attempting to create the link could expose the deleted file again. The original must stay hidden throughout.
- [new] `ephemeral_symlink_and_backing_create_cannot_both_claim_a_name` pauses a file create while a second caller tries to register a symlink at the same name. The file must win and the link must get `EEXIST`. Releasing the lock before the backing create made this test fail.

With this PR applied, all 46 guest path tests, 19 WASIX filesystem unit tests, and 144 virtual filesystem tests passed, with two existing ignores.
@vardhan
vardhan force-pushed the fix/wasix-exclusive-create-symlink branch from 207f91b to 61555fb Compare November 21, 2026 17:16
@vardhan

vardhan commented Nov 21, 2026

Copy link
Copy Markdown
Author

hi @theduke & @Arshia001 ; would you be able to look at this PR, or provide some guidance on the fixes? happy to address any feedback!

This branch has not been deployed

No deployments
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.

1 participant