Skip to content

Unsoundness in type checking of trait impls. Differences in implied lifetime bounds are not considered. #80176

@steffahn

Description

@steffahn
Member

Applies to current stable and nightly. See comment further down for a simplified example and one that works all the way since Rust 1.19.

type Ty = Box<&'static u8>;
trait Bad<'a> {
    fn f<'b>(x: &'static &'a Ty, y: &'b Ty) -> &'static Ty;
}

impl<'a> Bad<'a> for () {
    // NOTE that this signature does _not_ match the trait definition
    // (the first argument has different lifetimes)
    fn f<'b>(mut _x: &'static &'b Ty, y: &'b Ty) -> &'static Ty {
        let y = Box::new(y);
        let y = Box::leak(y);
        _x = y;
        foo(_x)
    }
}

fn foo<'b>(x: &'static &'b Ty) -> &'static Ty {
    x
}

fn main() {
    let v = Box::new(&42);
    let r = &v;
    let z: &_ = Box::leak(Box::new(Box::new(&0)));
    let z: &_ = Box::leak(Box::new(z));
    let r = <() as Bad<'static>>::f(z, r);
    drop(v);
    let _x = Box::new(0usize);
    println!("{}", r);
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
    Finished release [optimized] target(s) in 0.94s
     Running `target/release/playground`
timeout: the monitored command dumped core
/playground/tools/entrypoint.sh: line 11:     7 Segmentation fault      timeout --signal=KILL ${timeout} "$@"

@rustbot modify labels: T-compiler, C-bug, A-lifetimes, A-traits
@rustbot prioritize

Activity

added
A-lifetimesArea: Lifetimes / regions
C-bugCategory: This is a bug.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
I-prioritizeIssue: Indicates that prioritization has been requested for this issue.
and removed
A-lifetimesArea: Lifetimes / regions
C-bugCategory: This is a bug.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Dec 19, 2020
added
I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness
on Dec 19, 2020
jyn514

jyn514 commented on Dec 19, 2020

@jyn514
Member

This might be a duplicate of #57893.

steffahn

steffahn commented on Dec 19, 2020

@steffahn
MemberAuthor

@jyn514
Hm.. but isn’t that one about something with trait objects?

steffahn

steffahn commented on Dec 19, 2020

@steffahn
Author

39 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemA-type-systemArea: Type systemC-bugCategory: This is a bug.E-help-wantedCall for participation: Help is requested to fix this issue.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

Type

No type

Projects

Status

Completed

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @pnkfelix@oli-obk@thomcc@compiler-errors@steffahn

      Issue actions

        Unsoundness in type checking of trait impls. Differences in implied lifetime bounds are not considered. · Issue #80176 · rust-lang/rust