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

Stable type_eq #2150

Merged
merged 6 commits into from
May 7, 2024
Merged

Stable type_eq #2150

merged 6 commits into from
May 7, 2024

Conversation

addisoncrump
Copy link
Collaborator

This is right up there with the evilest code I've ever written.

struct A;
struct B;

fn main() {
    println!("A is A: {}", type_eq::<A, A>()); // true
    println!("A is B: {}", type_eq::<A, B>()); // false
}

This performs a type equality comparison using Copy/Clone specialisation imported from core. In testing, I found that it is optimized out entirely, i.e.:

fn main() {
    if type_eq::<A, A>() {
        abort();
    }
}

Is compiled down to:

> disass copy_specialization::main
Dump of assembler code for function _ZN19copy_specialization4main17h45816c84b6311796E:
   0x000055555555c260 <+0>:     push   rax
=> 0x000055555555c261 <+1>:     call   QWORD PTR [rip+0x4a8f9]        # 0x5555555a6b60 (std::process::abort)
End of assembler dump.

@addisoncrump
Copy link
Collaborator Author

cc @langston-barrett: any immediate soundness concerns come to mind here?

@domenukk
Copy link
Member

domenukk commented May 7, 2024

It's insane, I love it.

@domenukk
Copy link
Member

domenukk commented May 7, 2024

I think all fns should be #[inline], and add a comment/ some documentation why this works

#[allow(clippy::mismatching_type_param_order)]
impl<'a, T: ?Sized> Copy for W<'a, T, T> {}

let detected = Cell::new(true);
// [].clone() is *specialized* in core.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any documentation on this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like, anything official/some blog post, forum entry, docstring, ...?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add those to the code?

Copy link
Member

@domenukk domenukk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to merge :)

@addisoncrump addisoncrump merged commit c1a5598 into main May 7, 2024
21 checks passed
@addisoncrump addisoncrump deleted the stable-type-eq branch May 7, 2024 15:09
riesentoaster pushed a commit to riesentoaster/LibAFL that referenced this pull request May 8, 2024
* stable type eq

* whoops, wrong section

* satiate clippy

* remove extraneous comment

* explain

* bonus inline
riesentoaster pushed a commit to riesentoaster/LibAFL that referenced this pull request May 8, 2024
* stable type eq

* whoops, wrong section

* satiate clippy

* remove extraneous comment

* explain

* bonus inline
riesentoaster pushed a commit to riesentoaster/LibAFL that referenced this pull request May 8, 2024
* stable type eq

* whoops, wrong section

* satiate clippy

* remove extraneous comment

* explain

* bonus inline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants