Skip to content

Usage of --sysroot may still be racy and/or allow false dependencies #49

Closed
rust-lang/cargo
#7699
@alexcrichton

Description

@alexcrichton

I'm realizing now that after we've implemented --sysroot that we may still be susceptible races where you can depend on a sysroot crate without actually declaring that you depend on it. Let's say that #5 has already been implemented, and you say you only depend on core, I think you could also depend on alloc via the following:

  • First, you build your project. Something else depends on alloc, but your crate fails to build.
    • The alloc crate, however, did actually end up finish building by the time your crate received its error.
  • Next, you run cargo build again.
  • This time, very quickly, alloc is linked into the sysroot
  • Your crate, which has extern crate alloc, now "magically works"

I think the problem here is that we're giving, via --sysroot, crates access to anything in the sysroot. Cargo doesn't actually have any control over what can be loaded from the sysroot. While this will probably only rarely come up in practice, I think we may want to strive to have Cargo have a more intrusive understanding of what crates are where in the sysroot.

Assuming that #5 is implemented I think we'll have some sort of distinction between crates that have sysroot dependencies or not. For example your crate will either declare its sysroot dependencies explicitly, or you won't say anything (like all crates do today) and we'll have some default behavior (like assuming you can use up to proc_macro). That puts us in one of two buckets:

  • If dependencies are listed, we don't use --sysroot. We instead use --extern always. This should be fine since the feature isn't implemented yet! If you're building in -Zbuild-std mode Cargo has a path to pass, otherwise Cargo uses --extern std to activate the name resolution logic to work the same as if --extern name=path was passed.
  • If dependencies aren't listed, we instead use a new and invented --extern-sysroot flag to point to the compiled crates. This has the same name resolution logic that sysroot crates have today (wrt shadowing via macros and stuff like that).

The invented --extern-sysroot flag here would take the form --extern-sysroot name=path. In all cases Cargo would pass -L dependency=... for the sysroot, and the --extern flags would configure which crates can be loaded from the sysroot. The only reason for --extern-sysroot to exist is to match the name resolution behavior of sysroot crates today since there are subtle differences. Alternatively we could fix rustc so there are no differences!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions