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

Undefined behavior in implement_buffer_content! #1949

Open
avl opened this issue Jul 17, 2021 · 2 comments
Open

Undefined behavior in implement_buffer_content! #1949

avl opened this issue Jul 17, 2021 · 2 comments
Labels

Comments

@avl
Copy link
Contributor

avl commented Jul 17, 2021

The macro implement_buffer_content! contains the following code:

#[inline]
fn is_size_suitable(size: usize) -> bool {
    use std::mem;

    let fake_ptr: &$struct_name = unsafe { mem::transmute((0usize, 0usize)) };
    let min_size = mem::size_of_val(fake_ptr);

    let fake_ptr: &$struct_name = unsafe { mem::transmute((0usize, 1usize)) };
    let step = mem::size_of_val(fake_ptr) - min_size;

    size > min_size && (size - min_size) % step == 0
}

This is, as I understand, undefined behavior. It sort of works by accident in some/many cases. But with recent (>2021-03-04) rustc it does not work in the app I'm developing.

I've tried to solicit help from reddit: https://www.reddit.com/r/rust/comments/om6xs6/help_needed_fixing_ub_in_glium_graphics_library/

I've also made a github repo with a minimal(ish) test case: https://github.com/avl/rust-llvm12-regression

I'm trying to come up with a PR to fix this. I'm very interested in if others have the same problem.

The problem is of course the cast from (0usize,0usize) to a reference to a DST.

@avl
Copy link
Contributor Author

avl commented Jul 17, 2021

Here is a PR: #1950
With this PR the code works, but the implement_buffer_content-macro is still UB. Just not as much UB :-) .

@est31
Copy link
Collaborator

est31 commented Jul 18, 2021

Alternative PR: #1951

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

No branches or pull requests

2 participants