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

Rust 1.84 sometimes allows overlapping impls in incremental re-builds #135514

Open
steffahn opened this issue Jan 15, 2025 · 8 comments
Open

Rust 1.84 sometimes allows overlapping impls in incremental re-builds #135514

steffahn opened this issue Jan 15, 2025 · 8 comments
Labels
A-coherence Area: Coherence A-incr-comp Area: Incremental compilation C-bug Category: This is a bug. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness P-low Low priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@steffahn
Copy link
Member

steffahn commented Jan 15, 2025

This repro uses a bit of lexical comments trickery, but only for convenience. (Quite useful while manually testing multiple rust versions), so the whole change you need to do between incremental compilations is turning

// /* // <- uncomment this line

into

/* // <- uncomment this line

The main relevant change that this entails is

impl Trait for W {}

turning into

impl Trait for S<W> {}

which makes the Other-impls overlapping. As an additional effect, the code turning the overlap into UB is also uncommented. (The const _ … stuff only “fixes” the * symbols left behind by the */* in the middle.)

trait Trait {}

struct S0<T>(T);

struct S<T>(T);
impl<T> Trait for S<T> where S0<T>: Trait {}

struct W;

trait Other {
    type Choose<L, R>;
}

struct A;
struct B;

// first impl
impl<T: Trait> Other for T {
    type Choose<L, R> = L;
}

// second impl
impl<T> Other for S<T> {
    type Choose<L, R> = R;
}

const _: u8 = 0

// /* // <- uncomment this line

*0;

impl Trait for W {}

pub fn transmute<L, R>(l: L) -> R {
    todo!();
}

const _: u8 = 0
*/*
0;

impl Trait for S<W> {}

fn use_first_impl<T: Trait, L, R>(l: L) -> <<T as TyEq>::To as Other>::Choose<L, R> {
    l
}

fn use_second_impl<T, L, R>(l: <S<T> as Other>::Choose<L, R>) -> R {
    l
}

trait TyEq {
    type To;
}
impl<T> TyEq for T {
    type To = T;
}

fn transmute_inner<W, T, L, R>(l: L) -> R
where
    T: Trait + TyEq<To = S<W>>,
{
    use_second_impl::<W, L, R>(use_first_impl::<T, L, R>(l))
}

pub fn transmute<L, R>(l: L) -> R {
    transmute_inner::<W, S<W>, L, R>(l)
}

const _: u8 =
// */
0;

fn main() {
    let v = vec![65_u8, 66, 67];
    let s: String = transmute(v);
    println!("{}", s);
}

Reproduce

cargo new repro
cd repro
…write above to src/main…
cargo run
…uncomment the line in question as described…
cargo run
   Compiling repro v0.1.0 (/home/frank/repro)
warning: struct `A` is never constructed
  --> src/main.rs:14:8
   |
14 | struct A;
   |        ^
   |
   = note: `#[warn(dead_code)]` on by default

warning: struct `B` is never constructed
  --> src/main.rs:15:8
   |
15 | struct B;
   |        ^

warning: `repro` (bin "repro") generated 2 warnings
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.12s
     Running `target/debug/repro`
ABC

Is safe code that compiles to UB, but only in incremental re-builds (compilation error otherwise), considered a soundness issue to be labelled I-unsound?

This was already fixed with #133828 (which also explains what was the underlying issue). That PR seems like a fairly small & straightforward fix to me… should it perhaps be considered for backporting to stable? cc @compiler-errors

@rustbot label regression-from-stable-to-stable, T-compiler, A-incr-comp, A-coherence

@steffahn steffahn added the C-bug Category: This is a bug. label Jan 15, 2025
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-coherence Area: Coherence A-incr-comp Area: Incremental compilation regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jan 15, 2025
@workingjubilee workingjubilee added the I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness label Jan 15, 2025
@BoxyUwU BoxyUwU added P-low Low priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jan 15, 2025
@steffahn
Copy link
Member Author

steffahn commented Jan 15, 2025

For context, I ran into this accidentally. (Without prior knowledge of #133828.)

Granted I was trying to explore the behavior of overlap checks; my first reaction this was “wait, why does this compile despite the overlap?” Only a while later (after making a bunch of changes, e.g. crafting the whole exploitation into transmute) it was even more surprising how it didn’t work on the playground. Only then I realized it might be an incremental compilation issue and confirmed via rebuilding after cargo clean.

I.e: when they don’t ICE, incr-comp issues can be very confusing to the user, especially if they result in illegal code being accepted rather than legal code erroring. My main concern wouldn’t be the “tricky safe code could be unsound” aspect of the issue as much as the “normal users might accidentally create overlapping trait impls and be really confused when it creates compilation errors potentially only much much later”, [assuming that such an overlap can realistically happen for normal users accidentally, that aren’t deliberately exploring the behavior of overlap checks].

@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 15, 2025
@compiler-errors
Copy link
Member

Yep, let's backport it at least to 1.85.

@steffahn
Copy link
Member Author

The fix should already be on track for 1.85 naturally (and beta doesn’t reproduce, confirming the milestones labelling).

@compiler-errors
Copy link
Member

oh, lemme un-beta-nominate it then

@compiler-errors
Copy link
Member

Also would be cool if this test was written with cfg's rather than commenting. Then we could probably add it to the suite.

@steffahn
Copy link
Member Author

Yes, that'd definitely be cleaner. TBH, I simply didn't know before that cfgs work for testing incremental compilation 😆

@zachs18
Copy link
Contributor

zachs18 commented Jan 15, 2025

Here's a cfg'd version (with separate cfg(before) and cfg(after) flags; could easily be rewritten to cfg(foo) and cfg(not(foo)) instead):

main.rs
trait Trait {}

struct S0<T>(T);

struct S<T>(T);
impl<T> Trait for S<T> where S0<T>: Trait {}

struct W;

trait Other {
    type Choose<L, R>;
}

struct A;
struct B;

// first impl
impl<T: Trait> Other for T {
    type Choose<L, R> = L;
}

// second impl
impl<T> Other for S<T> {
    type Choose<L, R> = R;
}



#[cfg(before)]
impl Trait for W {}

#[cfg(before)]
pub fn transmute<L, R>(l: L) -> R {
    todo!();
}


#[cfg(after)]
impl Trait for S<W> {}

#[cfg(after)]
fn use_first_impl<T: Trait, L, R>(l: L) -> <<T as TyEq>::To as Other>::Choose<L, R> {
    l
}

#[cfg(after)]
fn use_second_impl<T, L, R>(l: <S<T> as Other>::Choose<L, R>) -> R {
    l
}

#[cfg(after)]
trait TyEq {
    type To;
}
#[cfg(after)]
impl<T> TyEq for T {
    type To = T;
}

#[cfg(after)]
fn transmute_inner<W, T, L, R>(l: L) -> R
where
    T: Trait + TyEq<To = S<W>>,
{
    use_second_impl::<W, L, R>(use_first_impl::<T, L, R>(l))
}

#[cfg(after)]
pub fn transmute<L, R>(l: L) -> R {
    transmute_inner::<W, S<W>, L, R>(l)
}


fn main() {
    let v = vec![65_u8, 66, 67];
    let s: String = transmute(v);
    println!("{}", s);
}
$ RUSTFLAGS="--cfg before" cargo run
// cargo output
thread 'main' panicked at main.rs:34:5:
not yet implemented
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
$ RUSTFLAGS="--cfg after" cargo run
// cargo output
ABC

or, without cargo (edition doesn't appear to matter, as long as it's consistent)

$ rustc -Cincremental=incremental --cfg before main.rs && ./main
// rustc output
thread 'main' panicked at main.rs:34:5:
not yet implemented
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
$ rustc -Cincremental=incremental --cfg after main.rs && ./main
// rustc output
ABC

(all on 1.84.0 stable)

I couldn't get a version using cfg(feature = "...") working with cargo feature flags, so I assume cargo clears the incremental cache on feature changes? (but not RUSTFLAGS changes?)

@lqd
Copy link
Member

lqd commented Jan 15, 2025

Incremental tests require special cfgs to model fail/pass etc as well. I had already done this locally so I've opened PR #135522 for it. (I didn't end up including the actual safe transmute exploit into the test, just that the overlapping impl is rejected)

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 15, 2025
add incremental test for issue 135514

r? `@compiler-errors` as requested in rust-lang#135514 (comment)

This adds parts of `@steffahn's` repro as an incremental test for rust-lang#135514. I had initially added the actual exploitation of the issue into the safe transmute, but removed it because it's not exactly needed for such a test. I can add it back if you'd like.

I've verified that the test fails with rust-lang#133828 reverted.
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 16, 2025
Rollup merge of rust-lang#135522 - lqd:issue-135514, r=compiler-errors

add incremental test for issue 135514

r? `@compiler-errors` as requested in rust-lang#135514 (comment)

This adds parts of `@steffahn's` repro as an incremental test for rust-lang#135514. I had initially added the actual exploitation of the issue into the safe transmute, but removed it because it's not exactly needed for such a test. I can add it back if you'd like.

I've verified that the test fails with rust-lang#133828 reverted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coherence Area: Coherence A-incr-comp Area: Incremental compilation C-bug Category: This is a bug. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness P-low Low priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants