Skip to content

GAT's + associated type defaults may cause UB #74824

Closed
@DutchGhost

Description

@DutchGhost
Contributor

I tried this code:

#![feature(generic_associated_types)]
#![feature(associated_type_defaults)]

use core::ops::Deref;

trait UnsafeCopy {
    type Copy<T>: Copy = Box<T>;
    
    fn copy<T>(x: &Self::Copy<T>) -> Self::Copy<T> {
        *x
    }
}

impl <T> UnsafeCopy for T {}

fn main() {
    let b = Box::new(42usize);
    let copy = <()>::copy(&b);
    
    let raw_b = Box::deref(&b) as *const _;
    let raw_copy = Box::deref(&copy) as *const _;
    
    // assert the addresses.
    assert_eq!(raw_b, raw_copy);
}

I expected to see this happen: Getting some errors that Box<usize> does not satisfy the Copy bound.

Instead, this happened: The program compiled and resulted in a double free, b and copy both are of type Box<usize> and point to the same memory...

Meta

rustc --version --verbose:

1.47.0-nightly (2020-07-26 6c8927b0cf80ceee1938)
Backtrace

warning: 1 warning emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.80s
     Running `target/debug/playground`
free(): double free detected in tcache 2
timeout: the monitored command dumped core
/playground/tools/entrypoint.sh: line 11:     7 Aborted                 timeout --signal=KILL ${timeout} "$@"

This seems like some variation of #62211 and #68641

Activity

added
I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness
requires-nightlyThis issue requires a nightly compiler in some way.
on Jul 27, 2020
steffahn

steffahn commented on Jul 27, 2020

@steffahn
Member

I guess, this duplicates #74816.
(Especially with the comment that I added pretty much at the exact same time you created this issue).

DutchGhost

DutchGhost commented on Jul 27, 2020

@DutchGhost
ContributorAuthor

I guess, this duplicates #74816.
(Especially with the comment that I added pretty much at the exact same time you created this issue).

Yes, I noticed it haha. Perhaps splitting your original issue up in several issues is a good idea, though

aDotInTheVoid

aDotInTheVoid commented on Dec 17, 2020

@aDotInTheVoid
Member

Triage: this given an error, on nightly.

@rustbot modify labels +E-needs-test

@rustbot claim

added
E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
on Dec 17, 2020
added a commit that references this issue on Dec 22, 2020

Rollup merge of rust-lang#80136 - aDotInTheVoid:74824-test, r=Mark-Si…

9b40dd8
added a commit that references this issue on Dec 22, 2020

Rollup merge of rust-lang#80136 - aDotInTheVoid:74824-test, r=Mark-Si…

26f2d8e
added a commit that references this issue on Dec 23, 2020

Auto merge of rust-lang#80314 - GuillaumeGomez:rollup-9rc48vx, r=Guil…

added
A-GATsArea: Generic associated types (GATs)
on Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-GATsArea: Generic associated types (GATs)C-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.F-associated_type_defaults`#![feature(associated_type_defaults)]`F-generic_associated_types`#![feature(generic_associated_types)]` a.k.a. GATsI-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundnessrequires-nightlyThis issue requires a nightly compiler in some way.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @jonas-schievink@steffahn@fmease@DutchGhost@aDotInTheVoid

    Issue actions

      GAT's + associated type defaults may cause UB · Issue #74824 · rust-lang/rust