Skip to content
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

Fix path canonicalization in witx use statements #434

Merged
merged 6 commits into from
Jun 22, 2021

Conversation

cratelyn
Copy link
Contributor

πŸ¦‡ ✨ Hello!

This branch fixes a bug I have found in the witx crate, involving use statements.

πŸ› The Bug

Paths in ..transitive(?) use statements (e.g. document A imports document B, which imports document C) are evaluated relative to the original root document, rather than the import-er document B.

This is better understood with an example (see below), or consulting the test case added here, multi_use_with_layered_dirs. Note that unlike the existing multi_use test, these documents do not all live within the same directory in the file system.

πŸ“œ Example

;; /root.witx
(use "subdir/child.witx")
;; /subdir/child.witx"
(use "sibling.witx")
(typename $b_float f64)
;; /subdir/sibling.witx
(typename $c_int u32)

Today, when root.witx is loaded, we'll run into an error that /sibling.witx does not exist.

☭ The Fix

The fix for this turns out to be a single line of code! That can be found in 963c5cf.

It turns out that when we were recursing in witx::toplevel::parse_file upon the discovery of a use statement, we'd pass the original root along, rather than providing the directory of the given document.

@cratelyn cratelyn marked this pull request as draft June 18, 2021 20:36
@cratelyn
Copy link
Contributor Author

cratelyn commented Jun 18, 2021

Gah. I had an old main branch, I'll have to fix this up a bit.

@cratelyn
Copy link
Contributor Author

I have updated this branch so that it is based on #395, which I believe is the commit that corresponds with the 0.9 version of the witx crate on crates.io.

Unfortunately, this work conflicts with changes that introduced a new use syntax in #415. I spoke with @pchickey about this last week, and if I understand correctly, this work was experimental and is not likely to see public release.

So, in order for this work to make its way into a 0.9.1 release of witx, I believe I'd first need to submit a preliminary PR elsewhere that reverts #415. That PR was a substantive change however, so before I do so I'd like to confirm with other maintainers that this is the correct route forward. (cc: @alexcrichton)

I thank you in advance for your time and assistance πŸ’Œ

@alexcrichton
Copy link
Contributor

Thanks for the fix! I agree yeah that we should revert the main branch back to what it was at the time of the latest crates.io release. I had plans for more work but they got sidetracked and sidelined due to other priorities and other avenues, so yeah let's revert what's not published and then we can merge the bug fix.

Are you ok submitting the reverts? I can take a quick look and approve them/this.

@cratelyn
Copy link
Contributor Author

cratelyn commented Jun 21, 2021

I've just pushed #435, which should unblock this patch πŸ™‚

I'll note that I've not updated the base branch here, because I'm not sure how well that plays across forks.

@cratelyn cratelyn marked this pull request as ready for review June 21, 2021 21:06
@alexcrichton
Copy link
Contributor

I think this may need a rebase now with the recent merge, but tests may also need some updates?

katelyn martin added 4 commits June 21, 2021 17:36
This test case demonstrates a bug identified with the semantics of `use`
statements in witx documents.

No fix is provided in this commit, so `cargo test` will fail at this
commit.
Because who doesn't love println debugging?

Now, running the (failing) test case added in the previous commit, we'll
see:

```
---- toplevel::test::multi_use_with_layered_dirs stdout ----
parsing file at path='"root.witx"' root='"/"'
canonical-path='"/root.witx"'

found a use statement, parsing file at "subdir/child.witx"
parsing file at path='"subdir/child.witx"' root='"/"'
canonical-path='"/subdir/child.witx"'

found a use statement, parsing file at "sibling.witx"
parsing file at path='"sibling.witx"' root='"/"'
canonical-path='"/sibling.witx"'
thread 'toplevel::test::multi_use_with_layered_dirs' panicked at 'parse: Io("/sibling.witx", Custom { kind: Other, error: "mock fs: file not found" })', src/toplevel.rs:151:10
```

Note that root is always `/` even when we're evaluating the paths found
in a document that lives in a subdirectory! See how the canonical path
in the last block resolves to '/sibling.witx'?

We found you, Mr. Bug πŸ› πŸ”πŸ˜ƒ
This reverts commit 07c1ae9.
@cratelyn
Copy link
Contributor Author

I think this may need a rebase now with the recent merge, but tests may also need some updates?

Indeed! I've rebased this branch. Thank you for the quick response!

I'll fix tests up next πŸ™‚

@cratelyn
Copy link
Contributor Author

The tests have been satiated ☺️

Copy link
Contributor

@pchickey pchickey 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 great, suggest adding one more check to the tests (which it passes!)

diff --git a/tools/witx/src/toplevel.rs b/tools/witx/src/toplevel.rs
index e415065..94dbd13 100644
--- a/tools/witx/src/toplevel.rs
+++ b/tools/witx/src/toplevel.rs
@@ -150,6 +150,10 @@ mod test {
                     "(use \"sibling.witx\")\n(typename $b_float f64)",
                 ),
                 ("/subdir/sibling.witx", "(typename $c_int u32)"),
+                // This definition looks just like subdir/sibling.witx but
+                // defines c_int differently - this test shows it does Not get
+                // included by subdir/child.witx's use.
+                ("/sibling.witx", "(typename $c_int u64)"),
             ]),
         )
         .expect("parse");

Co-authored-by: Pat Hickey <[email protected]>
@cratelyn
Copy link
Contributor Author

This looks great, suggest adding one more check to the tests (which it passes!)

Great idea! I've added that in 5148a1b. ✨

@alexcrichton alexcrichton merged commit 931c62a into WebAssembly:main Jun 22, 2021
@cratelyn cratelyn deleted the katie/fix-relative-use-bug branch June 22, 2021 14:44
@alexcrichton
Copy link
Contributor

It looks like the crate was last published at 7ec4b1a (diff from main), and if we'd like to make an 0.9.1 release (ideally just a bugfix release), I think there's another breaking change we merged in the meantime, notably the deletion of tools/witx/src/phases.rs, which I think happened as #398. I think we may need to revert that as well before publishing 0.9.1?

cratelyn pushed a commit to cratelyn/WASI that referenced this pull request Jun 22, 2021
Per this discussion from WebAssembly#434:

> It looks like the crate was last published at
> [7ec4b1a](WebAssembly@7ec4b1a)
> ([diff from
> `main`](WebAssembly/WASI@ef8c1a5...main)),
> and if we'd like to make an 0.9.1 release (ideally just a bugfix
> release), I think there's another breaking change we merged in the
> meantime, notably the deletion of `tools/witx/src/phases.rs`, which I
> think happened as WebAssembly#398. I think we may need to revert that as well
> before publishing 0.9.1?

    - [@alexcrichton](WebAssembly#434 (comment))

This reintroduces 'witx::phases', so that we do not include any breaking
changes in what ought to be a pure bug release.
@cratelyn
Copy link
Contributor Author

It looks like the crate was last published at 7ec4b1a (diff from main), and if we'd like to make an 0.9.1 release (ideally just a bugfix release), I think there's another breaking change we merged in the meantime, notably the deletion of tools/witx/src/phases.rs, which I think happened as #398. I think we may need to revert that as well before publishing 0.9.1?

Great catch, thank you. I've just opened #436, which should address this point.

alexcrichton pushed a commit that referenced this pull request Jun 22, 2021
Per this discussion from #434:

> It looks like the crate was last published at
> [7ec4b1a](7ec4b1a)
> ([diff from
> `main`](ef8c1a5...main)),
> and if we'd like to make an 0.9.1 release (ideally just a bugfix
> release), I think there's another breaking change we merged in the
> meantime, notably the deletion of `tools/witx/src/phases.rs`, which I
> think happened as #398. I think we may need to revert that as well
> before publishing 0.9.1?

    - [@alexcrichton](#434 (comment))

This reintroduces 'witx::phases', so that we do not include any breaking
changes in what ought to be a pure bug release.
cratelyn pushed a commit to bytecodealliance/lucet that referenced this pull request Jun 23, 2021
The 0.9.1 release of witx most importantly includes
WebAssembly/WASI#434, which is a bug-fix affecting transitive imports in
witx documents.
cratelyn pushed a commit to cratelyn/wasmtime that referenced this pull request Jun 23, 2021
This updates the WASI submodule, pulling in changes to the witx crate,
now that there is a 0.9.1 version including some bug fixes. See
WebAssembly/WASI#434 for more information.
cratelyn pushed a commit to cratelyn/wasmtime that referenced this pull request Jun 23, 2021
This updates the WASI submodule, pulling in changes to the witx crate,
now that there is a 0.9.1 version including some bug fixes. See
WebAssembly/WASI#434 for more information.
alexcrichton added a commit to bytecodealliance/wasmtime that referenced this pull request Jun 24, 2021
* wasi-common: update wasi submodule

This updates the WASI submodule, pulling in changes to the witx crate,
now that there is a 0.9.1 version including some bug fixes. See
WebAssembly/WASI#434 for more information.

* wiggle: update witx dependencies

* publish: verify and vendor witx-cli

* adjust root workspace members

This commit removes some items from the root manifest's workspace
members array, and adds `witx-cli` to the root `workspace.exclude`
array.

The motivation for this stems from a cargo bug described in
rust-lang/cargo#6745: `workspace.exclude` does not work if it is nested
under a `workspace.members` path.

See WebAssembly/WASI#438 for the underlying change to the WASI submodule
which reorganized the `witx-cli` crate, and WebAssembly/WASI#398 for the
original PR introducing `witx-cli`.

See [this
comment](#3025 (comment))
for more details about the compilation errors, and failed alternative
approaches that necessitated this change.

N.B. This is not a functional change, these crates are still implicitly
workspace members as transitive dependencies, but this will allow us to
side-step the aforementioned cargo bug.

Co-Authored-By: Alex Crichton <[email protected]>

Co-authored-by: Alex Crichton <[email protected]>
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