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

[clang][WebAssembly] Support wasm32-wasi shared libraries #148

Conversation

dicej
Copy link

@dicej dicej commented Jun 26, 2023

This adds support for Emscripten-style shared libraries [1] to non-emscripten targets, such as wasm32-wasi. Previously, only static linking was supported, and the -shared and -fPIC flags were simply ignored. Now both flags are honored.

Since WASI runtimes do not necessarily include JavaScript support, we cannot rely on the JS-based Emscripten linker to link shared libraries. Instead, we link them using the Component Model proposal [2].

We have prototyped shared library support in wasi-sdk [3] and put together a demo [4] which uses a patched version of wit-component [5] to link libraries using the Component Model. We plan to submit the required changes upstream to the respective repositories in the next week or two.

[1] https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md
[2] https://github.com/WebAssembly/component-model/blob/main/design/mvp/examples/SharedEverythingDynamicLinking.md
[3] https://github.com/dicej/wasi-sdk/tree/dynamic-linking
[4] https://github.com/dicej/component-linking-demo
[5] https://github.com/bytecodealliance/wasm-tools/tree/main/crates/wit-component

Reviewed By: sbc100

Differential Revision: https://reviews.llvm.org/D153293

This adds support for Emscripten-style shared libraries [1] to
non-emscripten targets, such as `wasm32-wasi`.  Previously, only static
linking was supported, and the `-shared` and `-fPIC` flags were simply
ignored.  Now both flags are honored.

Since WASI runtimes do not necessarily include JavaScript support, we
cannot rely on the JS-based Emscripten linker to link shared libraries.
Instead, we link them using the Component Model proposal [2].

We have prototyped shared library support in `wasi-sdk` [3] and put
together a demo [4] which uses a patched version of `wit-component` [5]
to link libraries using the Component Model.  We plan to submit the
required changes upstream to the respective repositories in the next
week or two.

[1] https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md
[2] https://github.com/WebAssembly/component-model/blob/main/design/mvp/examples/SharedEverythingDynamicLinking.md
[3] https://github.com/dicej/wasi-sdk/tree/dynamic-linking
[4] https://github.com/dicej/component-linking-demo
[5] https://github.com/bytecodealliance/wasm-tools/tree/main/crates/wit-component

Signed-off-by: Joel Dice <[email protected]>

Reviewed By: sbc100

Differential Revision: https://reviews.llvm.org/D153293

Signed-off-by: Joel Dice <[email protected]>
@cuviper
Copy link
Member

cuviper commented Jul 10, 2023

What will it look like to be used in the rust compiler?
(Remember that we need to continue working with unpatched LLVM as well...)

@dicej
Copy link
Author

dicej commented Jul 11, 2023

This patch alone won't change the behavior of the rust compiler itself, but it will allow you to build the standard library and application code with RUSTFLAGS="-C relocation-model=pic". Then you can build your app as a static library and finally use clang -shared to turn that static library into a shared library.

Eventually, we'll want to make that whole process easier, but this at least makes it possible.

I'm also working with Dan Gohman on changes to wasi-sdk and wasi-libc to build libc, libc++, etc. as shared libraries, which will compliment this change.

@cuviper
Copy link
Member

cuviper commented Jul 11, 2023

OK, as long as we're not changing that relocation-model default yet, it seems fine.

@cuviper cuviper merged commit a7d11c4 into rust-lang:rustc/16.0-2023-06-05 Jul 11, 2023
bors pushed a commit to rust-lang-ci/rust that referenced this pull request Jul 27, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 27, 2023
trvswgnr added a commit to crablang/crab that referenced this pull request Jul 28, 2023
* Dynamic for smir

* Optimize format usage

Per #112156, using `&` in `format!` may cause a small perf delay, so I tried to clean up one module at a time format usage. This PR includes a few removals of the ref in format (they do compile locally without the ref), as well as a few format inlining for consistency.

* compiler: Hermit targets: Remove pre-link args.

These pre-link args are remains from Hermit's old C version.
We don't need them and we have no reason to override the defaults here.
See ld [1] for details.

[1]: https://sourceware.org/binutils/docs/ld/Options.html

Signed-off-by: Martin Kröning <[email protected]>

* compiler: Hermit targets: Sort base fields by declaration

Signed-off-by: Martin Kröning <[email protected]>

* compiler: Hermit targets: Use functional update syntax

instead of mutating the base.

Signed-off-by: Martin Kröning <[email protected]>

* compiler: Add `riscv64gc-unknown-hermit` target

Co-authored-by: Martin Kröning <[email protected]>
Signed-off-by: Martin Kröning <[email protected]>

* compiler: Add `*-unknown-hermit` documentation

Signed-off-by: Martin Kröning <[email protected]>

* Remove redundant note.

This came from x86_64-unknown-none and doesn't make sense here.

* Add clarification about build-std and using newer instructions.

* new unstable option: -Zwrite-long-types-to-disk

This option guards the logic of writing long type names in files and
instead using short forms in error messages in rustc_middle/ty/error
behind a flag. The main motivation for this change is to disable this
behaviour when running ui tests.

This logic can be triggered by running tests in a directory that has a
long enough path, e.g. /my/very-long-path/where/rust-codebase/exists/

This means ui tests can fail depending on how long the path to their
file is.

Some ui tests actually rely on this behaviour for their assertions,
so for those we enable the flag manually.

* coverage: Obtain the `__llvm_covfun` section name outside a per-function loop

This section name is always constant for a given target, but obtaining it from
LLVM requires a few intermediate allocations. There's no need to do so
repeatedly from inside a per-function loop.

* interpret: support projecting into Place::Local without force_allocation

* Explain what the heck is going on with this lifetime remapping business

* builtin_macros: expect raw strings too

`expr_to_string` allows raw strings through so this code should be
expected to handle those.

Signed-off-by: David Wood <[email protected]>

* Some documentation nits

* Fix missing attribute merge on glob foreign re-exports

* Add regression test for #113982

* Hide `ToString` implementations that specialize the default ones

The status quo is highly confusing, since the overlap is not apparent,
and specialization is not a feature of Rust. This addresses #87545;
I'm not certain if it closes it, since that issue might also be trackign
a *general* solution for hiding specializing impls automatically.

* borrowck/errors: fix i18n error in delayed bug

During borrowck, the `MultiSpan` from a buffered diagnostic is cloned and
used to emit a delayed bug indicating a diagnostic was buffered - when
the buffered diagnostic is translated, then the cloned `MultiSpan` may
contain labels which can only render with the diagnostic's arguments, but
the delayed bug being emitted won't have those arguments. Adds a function
which clones `MultiSpan` without also cloning the contained labels, and
use this function when creating the buffered diagnostic delayed bug.

Signed-off-by: David Wood <[email protected]>

* Add missing documentation for `Session::time`

* If re-export is private, get the next item until a public one is found or expose the private item directly

* Add regression test for #81141

* Improve code readability

* Extend issue-81141-private-reexport-in-public-api test to cover more cases

* Rename `first_not_private` into `first_non_private`

* Correctly handle `super` and `::`

* Add test for private items

* Add support for `--document-hidden-items` in `first_non_private`

* Add test for `--document-hidden-items`

* Re-add missing generics in `first_non_private`

* Add regression test for generics reexport of private import

* Remove needs for transmute

* Revert "Remove needs for transmute"

This reverts commit ea9a17b9995b7a076283777b7d462a360fece2d6.

* Cache qpath first public result

* Fix span for punnycode

* Explain RPITs in the way they actually work

* lcnr's suggestions

Co-authored-by: lcnr <[email protected]>

* validate `doc(masked)`

* compiler: Add `x86_64-unikraft-linux-musl` target

Signed-off-by: Martin Kröning <[email protected]>

* compiler: Add `*-unikraft-linux-musl` documentation

Signed-off-by: Martin Kröning <[email protected]>

* bootstrap: Don't bundle musl on Unikraft

Signed-off-by: Martin Kröning <[email protected]>

* `unix::init`: Handle `ENOSYS` from `poll` on Unikraft.

Signed-off-by: Martin Kröning <[email protected]>

* `unix::init`: Don't use `signal` on Unikraft.

Signed-off-by: Martin Kröning <[email protected]>

* Update cargo

* fix(resolve): skip panic when resolution is dummy

* resolve: ensure compile failed when has dummy or ambiguous

* typos

* delete [allow(...)] from issue #74838

* remove additional [allow(unused_unsafe)]

* bootstrap: Define CMake platform if DragonFly.

CMAKE_SYSTEM_NAME is defined on a cross build if the target is
recognized.  Without this explicit definition cmake will assume that
we're building for the host platform which can bring in unwanted
compiler and linker flags.

Also, add a warning on cross builds with unknown target to aid in
cross builds for future platforms.

* docs(LazyLock): add example pass local LazyLock variable to struct

Signed-off-by: DragonBillow <[email protected]>

* factor out more stable impls

* Added recursive checking back up to see if a `Clone` suggestion would be helpful.

* extract common code

* builtin_macros: raw str in diagnostic output

If a raw string was used in the `env!` invocation, then it should also
be shown in the diagnostic messages as a raw string.

Signed-off-by: David Wood <[email protected]>

* Add regression test for invalid unused const in method

The warning can be reproduced with 1.63 but not with 1.64.

    $ rustc +1.63 tests/ui/lint/unused/const-local-var.rs
    warning: constant `F` is never used
      --> tests/ui/lint/unused/const-local-var.rs:14:9
       |
    14 |         const F: i32 = 2;
       |         ^^^^^^^^^^^^^^^^^
       |
       = note: `#[warn(dead_code)]` on by default
    $ rustc +1.64 tests/ui/lint/unused/const-local-var.rs

Add a regression test to prevent the problem from re-appearing.

* clippy: `env!` invocations can't be b"" literals

Signed-off-by: David Wood <[email protected]>

* write-long-types-to-disk: update tests

* interpret: refactor projection code to work on a common trait, and use that for visitors

* interpret: read_discriminant: only return VariantIdx

* bless more

* add some sanity checks in write_immediate_no_validate

* Suggest `{Option,Result}::as_ref()` instead of `cloned()` in some cases

* Remove some arguments that are always the same

* Inline a function that is only used once

* Remove a redundant field

* Improve performance of `first_non_private`

* Try explaining where `Inner` is in the signature better

* Use a builder instead of boolean/option arguments

* abi: unsized field in union - assert to delay bug

Unions cannot have unsized fields, and as such, layout computation for
unions asserts that each union field is sized (as this would normally
have halted compilation earlier).

However, if a generator ends up with an unsized local - a circumstance
in which an error will always have been emitted earlier, for example, if
attempting to dereference a `&str` - then the generator transform will
produce a union with an unsized field.

Since #110107, later passes will be run, such as constant propagation,
and can attempt layout computation on the generator, which will result
in layout computation of `str` in the context of it being a field of a
union - and so the aforementioned assertion would cause an ICE.

It didn't seem appropriate to try and detect this case in the MIR body
and skip this specific pass; tainting the MIR body or delaying a bug
from the generator transform (or elsewhere) wouldn't prevent this either
(as neither would prevent the later pass from running); and tainting when
the deref of `&str` is reported, if that's possible, would unnecessarily
prevent potential other errors from being reported later in compilation,
and is very tailored to this specific case of getting a unsized type in
a generator.

Given that this circumstance can only happen when an error should have
already been reported, the correct fix appears to be just changing the
assert to a delayed bug. This will still assert if there is some
circumstance where this occurs and no error has been reported, but it
won't crash the compiler in this instance.

Signed-off-by: David Wood <[email protected]>

* Normalize the RHS of an unsize goal

* Make sure to detect trait upcasting coercion even after normalization

* Restore tuple unsizing feature gate

* Consolidate trait upcasting and unsize into one normalization

* CI: fix CMake installation for 32 and 64bit `dist` Linux

* Add help for crate arg when crate name is invalid

* Make everything builtin!

* Remove credential providers from bootstrap
since they are now built-in to the Cargo binary

* Split nested GHA groups instead of panicking

* interpret: make write functions generic over the place type

* interpret: make read functions generic over operand type

* make MPlaceTy non-Copy

* Remove -Z diagnostic-width

* inline format!() args from rustc_codegen_llvm to the end (4)

r? @WaffleLapkin

* Optimize `AtomicBool` for target that don't support byte-sized atomics

`AtomicBool` is defined to have the same layout as `bool`, which means
that we guarantee that it has a size of 1 byte. However on certain
architectures such as RISC-V, LLVM will emulate byte atomics using a
masked CAS loop on an aligned word.

We can take advantage of the fact that `bool` only ever has a value of 0
or 1 to replace `swap` operations with `and`/`or` operations that LLVM
can lower to word-sized atomic `and`/`or` operations. This takes
advantage of the fact that the incoming value to a `swap` or
`compare_exchange` for `AtomicBool` is often a compile-time constant.

* Remove `Parser::desugar_doc_comments`.

It's currently stored twice: once in `Parser`, once in the `TokenStream`
within `Parser`. We only need the latter.

* Add `sym::iter_mut` + `sym::as_mut_ptr`

* rustdoc: stylistic changes

* rustdoc: fix cross-crate impl-Sized

* Don't treat negative trait predicates as always knowable

* Tweak `Parser::look_ahead`.

It doesn't really matter what the `desugar_doc_comments` argument is
here, because in practice we never look ahead through doc comments.
Changing it to `cursor.desugar_doc_comments` will allow some follow-up
simplifications.

* Remove `desugar_doc_comments` arguments from `TokenCursor::{inlined_,}next`.

Because it's now always `self.desugar_doc_comments`.

* Add a comment to `TokenCursor::desugar_doc_comments`.

Useful information that took me some time to discern.

* add tidy check that forbids issue ui test filenames

* add stable NullaryOp

* Make `x test src/tools/rustfmt --bless` format rustfmt with the freshly built in-tree version

* Bump syn now that it doesn't affect diagnostics anymore

* Bump syn dependency

* valtree: a bit of cleanup

* compiletest: remove ci-specific remap-path-prefix

Now that we have fixed the underlying cause of long type name
inconsistencies in #113893, we can remove the remap-path-prefix logic
from CI

* Regression test `println!()` panic message on `ErrorKind::BrokenPipe`

No existing test failed if the [`panic!()`][1] of the `println!()`
family of functions was removed, or if its message was changed.

So add such a test.

[1] https://github.com/rust-lang/rust/blob/104f4300cfddbd956e32820ef202a732f06ec848/library/std/src/io/stdio.rs#L1007-L1009

* docs: fmt::Debug*: Fix comments for finish method.

In the code sample for the `finish` method on `DebugList`,
`DebugMap`, and `DebugSet`, refer to finishing the list, map, or
set, rather than struct as it did.

* Add Param ty to SMIR

* Add Bound ty to SMIR

* Have a better file name than just the issue id

* Fix regression for private in public

* Replace in-tree `rustc_apfloat` with the new version of the crate

* Fix miri

* Remove unused NCSA license

* replace atty crate with std's isTerminal

* Squelch a noisy rustc_expand unittest

* Add test case for #109567

* Add tests for #102403 and #113407

* Dont pass -Zwrite-long-types-to-disk=no for ui-fulldeps --stage=1

* Unite bless environment variables under `RUSTC_BLESS`

Currently, Clippy, Miri, Rustfmt, and rustc all use an environment variable to
indicate that output should be blessed, but they use different variable names.
In order to improve consistency, this patch applies the following changes:

- Emit `RUSTC_BLESS` within `prepare_cargo_test` so it is always
  available
- Change usage of `MIRI_BLESS` in the Miri subtree to use `RUSTC_BLESS`
- Change usage of `BLESS` in the Clippy subtree to `RUSTC_BLESS`
- Change usage of `BLESS` in the Rustfmt subtree to `RUSTC_BLESS`
- Adjust the blessable test in `rustc_errors` to use this same
  convention
- Update documentation where applicable

Any tools that uses `RUSTC_BLESS` should check that it is set to any value
other than `"0"`.

* Update LLVM submodule

This adds rust-lang/llvm-project#148.

* Fix URL for `rmatches`

* Simplify the `ttdelim_span` test.

The existing code is a very complex and inefficient way to the get the
span of the last token.

* Replace `into_trees` with `trees` in a test.

There's no need for token tree cloning here.

* Use `TokenStream::trees` instead of `into_trees` for attributes.

This avoids cloning some token trees. A couple of `clone` calls were
inserted, but only on some paths, and the next commit will remove them.

* Make `TokenTree::uninterpolate` take `&self` and return a `Cow`.

Making it similar to `Token::uninterpolate`. This avoids some more token
tree cloning.

* Avoid some token cloning in `filter_tokens_from_list`.

Now the cloning only happens on some paths, instead of all paths.

* Avoid some token tree cloning in decl macro parsing.

By changing `into_trees` into `trees`. Some of the subsequent paths
require explicit clones, but not all.

* Avoid `into_trees` usage in rustfmt.

Token tree cloning is only needed in one place.

* Remove `Iterator` impl for `TokenTreeCursor`.

This is surprising, but the new comment explains why. It's a logical
conclusion in the drive to avoid `TokenTree` clones.

`TokenTreeCursor` is now only used within `Parser`. It's still needed
due to `replace_prev_and_rewind`.

* Revert "don't uniquify regions when canonicalizing"

This reverts commit 171f541.

* Consider a goal as NOT changed if its response is identity modulo regions

* Optimize `TokenKind::clone`.

`TokenKind` would impl `Copy` if it weren't for
`TokenKind::Interpolated`. This commit makes `clone` reflect that.

* tests/ui/hello_world/main.rs: Remove FIXME

The purpose of the test is to make sure that compiling hello world
produces no compiler output. To properly test that, we need to run the
entire compiler pipeline. We don't want the test to pass if codegen
accidentally starts writing to stdout. So keep it as build-pass.

* Turns out opaque types can have hidden types registered during mir validation

* Remove transmute calls and caching for use paths

* Revert "add tidy check that forbids issue ui test filenames"

This reverts commit 13e2abf.

Reverting because an MCP was requested and it turned out there
was a lack of a consensus on what to do in this area.

* Remove `constness` from `ParamEnv`

* update tests, adding known-bug

* Make `--print KIND=PATH` unstable

rust-lang/rust#113780 should have gone through
an MCP+FCP but wasn't, but instead of reverting the original PR, this PR
just make that new option unstable.

* bless clippy

* docs(style-guide): don't flatten match arms with macro call

* Change the description of `SUSPICIOUS_AUTO_TRAIT_IMPLS`

* Introduce the `#[diagnostic]` attribute namespace

Co-authored-by: est31 <[email protected]>

Co-authored-by: Esteban Kuber <[email protected]>

Co-authored-by: Vadim Petrochenkov <[email protected]>

* imported src/doc/nomicon into main repository

* imported src/tools/cargo into main repository

* imported src/doc/reference into main repository

* imported src/doc/book into main repository

* imported src/doc/rust-by-example into main repository

* imported library/stdarch into main repository

* imported src/doc/rustc-dev-guide into main repository

* imported src/doc/edition-guide into main repository

* imported src/doc/embedded-book into main repository

* imported library/backtrace into main repository

* use crablang readme

* imported src/tools/cargo into main repository

* abandon submodules

added a bash script that converts a project using gitsubmodules (ew) to a monorepo

* use crab contrib

---------

Signed-off-by: Martin Kröning <[email protected]>
Signed-off-by: David Wood <[email protected]>
Signed-off-by: DragonBillow <[email protected]>
Co-authored-by: Eric Mark Martin <[email protected]>
Co-authored-by: Yuri Astrakhan <[email protected]>
Co-authored-by: bors <[email protected]>
Co-authored-by: Martin Kröning <[email protected]>
Co-authored-by: Simon Schöning <[email protected]>
Co-authored-by: Laurențiu Nicola <[email protected]>
Co-authored-by: Jonathan Pallant (Ferrous Systems) <[email protected]>
Co-authored-by: Mahdi Dibaiee <[email protected]>
Co-authored-by: Zalathar <[email protected]>
Co-authored-by: Ralf Jung <[email protected]>
Co-authored-by: Oli Scherer <[email protected]>
Co-authored-by: David Wood <[email protected]>
Co-authored-by: Guillaume Gomez <[email protected]>
Co-authored-by: Frank Steffahn <[email protected]>
Co-authored-by: Eric Huss <[email protected]>
Co-authored-by: Michael Goulet <[email protected]>
Co-authored-by: lcnr <[email protected]>
Co-authored-by: Matthias Krüger <[email protected]>
Co-authored-by: Lukas Markeffsky <@>
Co-authored-by: Martin Kröning <[email protected]>
Co-authored-by: Arlo Siemsen <[email protected]>
Co-authored-by: bohan <[email protected]>
Co-authored-by: Tshepang Mbambo <[email protected]>
Co-authored-by: James Dietz <[email protected]>
Co-authored-by: Alex Zepeda <[email protected]>
Co-authored-by: DragonBillow <[email protected]>
Co-authored-by: Steven Trotter <[email protected]>
Co-authored-by: Deadbeef <[email protected]>
Co-authored-by: Martin Nordholts <[email protected]>
Co-authored-by: clubby789 <[email protected]>
Co-authored-by: Jakub Beránek <[email protected]>
Co-authored-by: yukang <[email protected]>
Co-authored-by: Amanieu d'Antras <[email protected]>
Co-authored-by: Nicholas Nethercote <[email protected]>
Co-authored-by: blyxyas <[email protected]>
Co-authored-by: León Orell Valerian Liehr <[email protected]>
Co-authored-by: asquared31415 <[email protected]>
Co-authored-by: Bruce Mitchener <[email protected]>
Co-authored-by: Santiago Pastorino <[email protected]>
Co-authored-by: MoskalykA <[email protected]>
Co-authored-by: Wesley Wiser <[email protected]>
Co-authored-by: klensy <[email protected]>
Co-authored-by: Trevor Gross <[email protected]>
Co-authored-by: Joel Dice <[email protected]>
Co-authored-by: Veera <[email protected]>
Co-authored-by: Guillaume Gomez <[email protected]>
Co-authored-by: Jubilee Young <[email protected]>
Co-authored-by: Urgau <[email protected]>
Co-authored-by: Jubilee <[email protected]>
Co-authored-by: Caleb Cartwright <[email protected]>
Co-authored-by: Oleksandr Babak <[email protected]>
Co-authored-by: Georg Semmler <[email protected]>
Co-authored-by: Vadim Petrochenkov <[email protected]>
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Jul 30, 2023
vext01 pushed a commit to vext01/llvm-project that referenced this pull request May 1, 2024
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