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

Implied bounds on nested references + variance = soundness hole #25860

Open
aturon opened this issue May 28, 2015 · 56 comments
Open

Implied bounds on nested references + variance = soundness hole #25860

aturon opened this issue May 28, 2015 · 56 comments
Labels
A-type-system Area: Type system A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) 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-medium Medium priority S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@aturon
Copy link
Member

aturon commented May 28, 2015

The combination of variance and implied bounds for nested references opens a hole in the current type system:

static UNIT: &'static &'static () = &&();

fn foo<'a, 'b, T>(_: &'a &'b (), v: &'b T) -> &'a T { v }

fn bad<'a, T>(x: &'a T) -> &'static T {
    let f: fn(&'static &'a (), &'a T) -> &'static T = foo;
    f(UNIT, x)
}

This hole has been fixed in #129021 for non-higher-ranked function pointers. The underlying issue still persists.

static UNIT: &'static &'static () = &&();

fn foo<'a, 'b, T>(_: &'a &'b (), v: &'b T, _: &()) -> &'a T { v }

fn bad<'a, T>(x: &'a T) -> &'static T {
    let f: fn(_, &'a T, &()) -> &'static T = foo;
    f(UNIT, x, &())
}

fn main() {}

Update from @pnkfelix :

While the test as written above is rejected by Rust today (with the error message for line 6 saying "in type &'static &'a (), reference has a longer lifetime than the data it references"), that is just an artifact of the original source code (with its explicit type signature) running up against one new WF-check.

The fundamental issue persists, since one can today write instead:

static UNIT: &'static &'static () = &&();

fn foo<'a, 'b, T>(_: &'a &'b (), v: &'b T) -> &'a T { v }

fn bad<'a, T>(x: &'a T) -> &'static T {
    let f: fn(_, &'a T) -> &'static T = foo;
    f(UNIT, x)
}

(and this way, still get the bad behaving fn bad, by just side-stepping one of the explicit type declarations.)

@aturon aturon added A-type-system Area: Type system I-nominated labels May 28, 2015
@aturon
Copy link
Member Author

aturon commented May 28, 2015

What's going on here is that foo gets to assume that 'b: 'a, but this isn't actually checked when producing f.

This assumption was thought to be valid because any nested reference type &'a &'b T has to guarantee it for well-formedness. But variance currently allows you to switch around the lifetimes before actually passing in the witnessing argument.

@nikomatsakis
Copy link
Contributor

One solution is to be more aggressive about checking WFedness, but there are other options to consider.

@nikomatsakis
Copy link
Contributor

triage: P-high T-lang

@rust-highfive rust-highfive added P-high High priority and removed I-nominated labels Jun 2, 2015
@nikomatsakis nikomatsakis self-assigned this Jun 2, 2015
@nikomatsakis nikomatsakis added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Jun 2, 2015
@nikomatsakis
Copy link
Contributor

I've been working on some proposal(s) that address this bug (among others), so assigning to me.

nikomatsakis added a commit to nikomatsakis/rust that referenced this issue Jun 9, 2015
invariance, this restores soundness to implied bounds (I think). :)

Fixes rust-lang#25860.
@nikomatsakis
Copy link
Contributor

There is a related problem that doesn't require variance on argument types. The current codebase doesn't check that the expected argument type is well-formed, only the provided one, which is a subtype of the expected one. This is insufficient (but easily rectified).

nikomatsakis added a commit to nikomatsakis/rust that referenced this issue Jul 6, 2015
invariance, this restores soundness to implied bounds (I think). :)

Fixes rust-lang#25860.
@Stebalien
Copy link
Contributor

@nikomatsakis I believe you meant to close this.

@nikomatsakis
Copy link
Contributor

The work needed to close this has not yet landed. It's in the queue though, once we finish up rust-lang/rfcs#1214.

@Stebalien
Copy link
Contributor

Sorry, I saw the commit but didn't notice that it hadn't been merged.

@arielb1 arielb1 added the I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness label Oct 1, 2015
@pnkfelix
Copy link
Member

One interesting thing to note, in light of the new WF checks that have landed with the preliminary implementation of rust-lang/rfcs#1214, is that if we change the definition of fn foo like this (where we rewrite the implied lifetime bounds to be explicitly stated in a where-clause :

fn foo<'a, 'b, T>(_: &'a &'b (), v: &'b T) -> &'a T where 'b: 'a { v }

then it seems like the code is rejected properly.

I am currently trying to puzzle through whether this kind of "switch from implicit to explicit", assuming it were done as a kind of desugaring by the compiler, if that would be effectively the same as "remove support for contravariance" from the language, or if it represents some other path...

@pnkfelix
Copy link
Member

Here's a variation on the original example that retains explicit types (rather than resorting to _ as a type like I did in the description):

fn foo<'a, 'b, T>(_false_witness: Option<&'a &'b ()>, v: &'b T) -> &'a T { v }

fn bad<'c, 'd, T>(x: &'c T) -> &'d T {
    // below is using contravariance to assign `foo` to `f`,
    // side-stepping the obligation to prove `'c: 'd`
    // implicit in the original `fn foo`.
    let f: fn(Option<&'d &'d ()>, &'c T) -> &'d T = foo;
    f(None, x)
}

fn main() {
    fn inner() -> &'static String {
        bad(&format!("hello"))
    }

    let x = inner();
    println!("x: {}", x);
}

@pnkfelix
Copy link
Member

I am currently trying to puzzle through whether this kind of "switch from implicit to explicit", assuming it were done as a kind of desugaring by the compiler, if that would be effectively the same as "remove support for contravariance" from the language

After some reflection, I think this does represent a (perhaps appropriately) weakened variation on "remove contravariance from the language"

In particular, if we did the desugaring right (where implied lifetime bounds from a fn-signature would get transformed into where clauses on the fn), then we could still have useful contravariance for fn's that have no such implied bounds.

@pnkfelix
Copy link
Member

as an aside, part of me does think that it would be better if we also added some way to write the proposed implied where-clauses explicitly as part of the fn-type. I.e. if you consider the following example:

fn bad<'a, 'b>(g: fn (&'a &'b i32) -> i32) {
    let _f: fn(x: &'b &'b i32) -> i32 = g;
}

under my imagined new system, the above assignment of g to _f would be illegal, due to the implied bounds attached to g (that are not part of the type of _f).

But it might be nice if we could actually write:

fn explicit_types<'a, 'b>(callback: fn (&'a i32, &'b i32) -> i32 where 'a: 'b) {
    ...
}

(note that the where clause there is part of the type of callback)

@RalfJung
Copy link
Member

I take it from your comments that removing or restricting the variance is actually considered as a solution to this?

That's surprising me. I cannot see variance at fault here. The observation underlying the issue is that implicit bounds are not properly preserved under variance. The most obvious solution (for me, anyways) would be to make the implicit bounds explicit: If "fn foo<'a, 'b, T>(&'a &'b (), &'b T) -> &'a T" would be considered mere syntactic sugar for "fn foo<'a, 'b, T>(&'a &'b (), &'b T) -> &'a T where 'b: 'a, 'a: 'fn, T: 'b" (using 'fn for the lifetime of the function), and from then on the checks are preserved and adapted appropriately when applying variance or specializing lifetimes, wouldn't that also catch the trouble? (Making implied bounds explicit was also discussed in rust-lang/rfcs#1327, since implied bounds are also a trouble for drop safety.)

@pnkfelix
Copy link
Member

@RalfJung

I cannot see variance at fault here.

I've been saying the similar things to @nikomatsakis

But the first step for me was to encode the test case in a way that made made it apparent (at least to me) that contravariance is (part of) why we are seeing this happen.

I think that #25860 (comment) is very much the same as what you are describing: 1. Ensure the implicit bounds are actually part of the fn-type itself (even if most users will not write the associated where clause explicitly) and are checked before allowing any calls to a value of a given fn-type, and then 2. fix the subtyping relation between fn-types to ensure that such where-clauses are preserved.

@RalfJung
Copy link
Member

I think that #25860 (comment) is very much the same as what you are describing: 1. Ensure the implicit bounds are actually part of the fn-type itself (even if most users will not write the associated where clause explicitly) and are checked before allowing any calls to a value of a given fn-type, and then 2. fix the subtyping relation between fn-types to ensure that such wshere-clauses are preserved.

Yes, that sounds like we're talking about the same idea.

@nikomatsakis
Copy link
Contributor

I do not consider the problem to be fundamentally about contravariance -- but I do consider removing contravariance from fn arguments to be a pragmatic way to solve the problem, at least in the short term. In one of the drafts for RFC rust-lang/rfcs#1214, I wrote the section pasted below, which I think is a good explanation of the problem as I understand it:

Appendix: for/where, an alternative view

The natural generalization of our current fn types is adding the ability to attach arbitrary where clauses to higher-ranked types. That is, a type like for<'a,'b> fn(&'a &'b T) might be written out more explicitly by adding the implied bounds as explicit where-clauses attached to the for:

for<'a,'b> where<'b:'a, T:'b> fn(&'a &'b T)

These where-clauses must be discharged before the fn can be called. They can also be discharged through subtyping, if no higher-ranked regions are involved: that is, there might be a typing rule that allows a where clause to be dropped from the type so long as it can be proven in the current environment (similarly, having fewer where clauses would be a subtype of having more.)

You can view the current notion of implied bounds as being a more limited form of this formalism where the where clauses are exactly the implied bounds of the argument types. However, making the where clauses explicit has some advantages, because it means that one can vary the types of the arguments (via contravariance) while leaving the where clauses intact.

For example, if you had a function:

fn foo<'a,'b,T>(&'a &'b T) { ... }

Under this RFC, the type of this function is:

for<'a,'b> fn(&'a &'b T)

Under the for/where scheme, the full type would be:

for<'a,'b> where<'b:'a, T:'b> fn(&'a &'b T)

Now, if we upcast this type to only accept static data as argument, the where clauses are unaffected:

for<'a,'b> where<'b:'a, T:'b> fn(&'static &'static T)

Viewed this way, we can see why the current fn types (in which one cannot write where clauses explicitly) are invariant: changing the argument types is fine, but it also changes the where clauses, and the new where clauses are not a superset of the old ones, so the subtyping relation does not hold. That is, if we write out the implicit where clauses that result implicitly, we can see why variance on fns causes problems:

for<'a,'b> where<'b:'a, T:'b> fn(&'a &'b T, &'b T) -> &'a T 
<:
for<'a,'b> fn(&'static &'static T, &'b T) -> &'a T
? (today yes, under this RFC no)

Clearly, this subtype relationship should not hold, because the where clauses in the subtype are not implied by the supertype.

@arielb1
Copy link
Contributor

arielb1 commented Jan 22, 2016

To make the point clearer, the principal part of the issue involves higher-ranked types. The rank 0 issue should not be that hard to solve (by requiring WF when instantiating a function - I am a bit surprised we don't do so already).

An example of the problem path (I am intentionally making the problem binder separate from the problem where-clause-container):

fn foo<'a, 'b, T>() -> fn(Option<&'a &'b ()>, &'b T) -> &'a T {
    fn foo_inner<'a, 'b, T>(_witness: Option<&'a &'b ()>, v: &'b T) -> &'a T {
        v
    }
    foo_inner
}

fn bad<'c, 'd, T>(x: &'c T) -> &'d T {
    // instantiate `foo`
    let foo1: for<'a, 'b> fn() -> fn(Option<&'a &'b ()>, &'b T) -> &'a T = foo;
    // subtyping: instantiate `'b <- 'c`
    let foo2: for<'a> fn() -> fn(Option<&'a &'c ()>, &'c T) -> &'a T = foo1;
    // subtyping: contravariantly 'c becomes 'static
    let foo3: for<'a> fn() -> fn(Option<&'a &'static ()>, &'c T) -> &'a T = foo2;
    // subtyping: instantiate `'a <- 'd`
    let foo4: fn() -> fn(Option<&'d &'static ()>, &'c T) -> &'d T = foo3;
    // boom!
    foo4()(None, x)
}

fn main() {
    fn inner() -> &'static String {
        bad(&format!("hello"))
    }

    let x = inner();
    println!("x: {}", x);
}

@jackh726 jackh726 added the S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. label Apr 25, 2023
@John-Nagle
Copy link

Publicized as a language design flaw at https://counterexamples.org/nearly-universal.html?highlight=Rust#nearly-universal-quantification

@ast-ral
Copy link
Contributor

ast-ral commented Aug 16, 2023

This code I've found also seems to exhibit a similar problem (on version 1.71.1):

use std::marker::PhantomData;

struct Bounded<'a, 'b: 'a, T>(&'a T, PhantomData<&'b ()>);

fn helper<'a, 'b, T>(
    input: &'a T,
    closure: impl FnOnce(&T) -> Bounded<'b, '_, T>,
) -> &'b T {
    closure(input).0
}

fn extend<'a, 'b, T>(input: &'a T) -> &'b T {
    helper(input, |x| Bounded(x, PhantomData))
}

I'm hesitant to file this as its own issue because I don't think it's different enough to justify doing so.

@Neutron3529
Copy link
Contributor

Neutron3529 commented Dec 11, 2023

This code I've found also seems to exhibit a similar problem (on version 1.71.1):

use std::marker::PhantomData;

struct Bounded<'a, 'b: 'a, T>(&'a T, PhantomData<&'b ()>);

fn helper<'a, 'b, T>(
    input: &'a T,
    closure: impl FnOnce(&T) -> Bounded<'b, '_, T>,
) -> &'b T {
    closure(input).0
}

fn extend<'a, 'b, T>(input: &'a T) -> &'b T {
    helper(input, |x| Bounded(x, PhantomData))
}

I'm hesitant to file this as its own issue because I don't think it's different enough to justify doing so.

I tried to simplify your code, got:

fn extend<'a, T>(input: &'a T) -> &'static T {
    struct Bounded<'a, 'b: 'static, T>(&'a T, [&'b ();0]);
    let n:Box<dyn FnOnce(&T) -> Bounded<'static, '_, T>>=Box::new(|x| Bounded(x, []));
    n(input).0
}
fn extend_mut<'a, T>(input: &'a mut T) -> &'static mut T {
    struct Bounded<'a, 'b: 'static, T>(&'a mut T, [&'b ();0]);
    let mut n:Box<dyn FnMut(&mut T) -> Bounded<'static, '_, T>>=Box::new(|x| Bounded(x, []));
    n(input).0
}

deleting the 'b would generate an compile error, but adding it is fine. I means, this code

fn extend<T>(input: &T) -> &'static T {
    struct Bounded<'a, T>(&'a T, [&'static ();0]);
    let n:Box<dyn FnOnce(&T) -> Bounded<'static, T>>=Box::new(|x| Bounded(x, []));
    n(input).0
}

yields

error: lifetime may not live long enough
 --> test2.rs:3:67
  |
3 |     let n:Box<dyn FnOnce(&T) -> Bounded<'static, T>>=Box::new(|x| Bounded(x, []));
  |                                                                -- ^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'2`
  |                                                                ||
  |                                                                |return type of closure is Bounded<'2, T>
  |                                                                has type `&'1 T`

error: aborting due to previous error

It seems the restriction is inverted here, we need 'a:'b to execute instructions like &'a T as &'b T, but we wrote 'b outlives 'a (actually here I wrote 'b outlives 'static)


What's more, the original implementation does not need an extra lifetime 'b since we could obtain 'b from 'static directly.

pub fn make_static<'a, T>(input: &'a T) -> &'static T {
    fn helper<'a, T>(_: [&'static&'a();0], v: &'a T) -> &'static T { v }
    let f: fn([&'static&();0], &T) -> &'static T = helper;
    f([], input) // we need not to create a &'a&'b unit directly.
}
pub fn make_static_mut<'a, T>(input: &'a mut T) -> &'static mut T {
    fn helper_mut<'a, T>(_: [&'static&'a();0], v: &'a mut T) -> &'static mut T { v }
    let f: fn([&'static&'static();0], &'a mut T) -> &'static mut T = helper_mut;
    // let f: fn([&'static&();0], &mut T) -> &'static mut T = helper_mut; // it also works
    f([], input)
}

@Jules-Bertholet
Copy link
Contributor

fn extend<'a, T>(input: &'a T) -> &'static T {
    struct Bounded<'a, 'b: 'static, T>(&'a T, [&'b ();0]);
    let n:Box<dyn FnOnce(&T) -> Bounded<'static, '_, T>>=Box::new(|x| Bounded(x, []));
    n(input).0
}
fn extend_mut<'a, T>(input: &'a mut T) -> &'static mut T {
    struct Bounded<'a, 'b: 'static, T>(&'a mut T, [&'b ();0]);
    let mut n:Box<dyn FnMut(&mut T) -> Bounded<'static, '_, T>>=Box::new(|x| Bounded(x, []));
    n(input).0
}

This is a new regression from 1.64 to 1.65: https://rust.godbolt.org/z/jGxrcrjTj

@bstrie

This comment was marked as outdated.

@slanterns

This comment was marked as outdated.

@Richardn2002

This comment was marked as off-topic.

@alexpyattaev

This comment was marked as off-topic.

@nyabinary

This comment was marked as off-topic.

@lcnr
Copy link
Contributor

lcnr commented Feb 20, 2024

we already had a crate published on crates.io before which used this bug to transmute in safe code, see #25860 (comment).

this issue is a priority to fix for the types team and has been so for years now. there is a reason for why it is not yet fixed. fixing it relies on where-bounds on binders which are blocked on the next-generation trait solver. we are actively working on this and cannot fix the unsoundness before it's done.

@rust-lang rust-lang locked as off-topic and limited conversation to collaborators Feb 21, 2024
@oli-obk
Copy link
Contributor

oli-obk commented Feb 21, 2024

I am locking this thread, since its 100+ comments have become impossible to navigate. Please open threads on zulip if you want to discuss this issue or related bugs

@workingjubilee workingjubilee added the A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) label Jun 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-type-system Area: Type system A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) 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-medium Medium priority S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
Status: new solver everywhere
Development

No branches or pull requests