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

Incorrect PROC typedef #438

Closed
msiglreith opened this issue Nov 22, 2017 · 12 comments
Closed

Incorrect PROC typedef #438

msiglreith opened this issue Nov 22, 2017 · 12 comments
Labels

Comments

@msiglreith
Copy link
Contributor

The definition of PROC causes issues on the latest nightly. The functions aren't allow to be zero, which rust makes use by annotating them with non-null. Unfortunately, the returned functions by wglGetProcAddress can be zero (e.g GL1.1 functions). Glutin currently checks if the functions are zero else it queries the addresses via kernel32::GetProcAddress. The rust compiler optimizes this call away as it assumes we don't have null functions.

The proposed solution is to replace extern "system" fn() with *const (), which also fixes the issue for me locally.

@tomaka
Copy link
Collaborator

tomaka commented Nov 22, 2017

The fix makes sense to me.

@brendanzab
Copy link
Owner

brendanzab commented Nov 22, 2017

Yeah, as you can see from the comment I had no idea what I was doing when I was translating those type defs! What does PROC look like in use? Would people have to transmute/cast to *const ()?

I'm also guessing we would have to do a major release seeing as its an API change.

@retep998
Copy link

retep998 commented Nov 23, 2017

You can use what winapi does as an example of how it should be done. Raw pointer to an opaque type (can be replaced with extern type when that is stable enough).

https://github.com/retep998/winapi-rs/blob/b6eed13c28a8808bfdc54cdf50c4498132ce9176/src/shared/minwindef.rs#L76-L82

@brendanzab
Copy link
Owner

@retep998: That's still using a void enum - is it still UB to use that?

pub enum __some_function {}
/// Pointer to a function with unknown type signature.
pub type FARPROC = *mut __some_function;

cc. @eddyb

@brendanzab
Copy link
Owner

Wondering if we should just depend on winapi somehow for these types rather than having to maintain our own, but that's a separate issue 🤔

@eddyb
Copy link

eddyb commented Nov 25, 2017

The canonical equivalent for C function pointers into Rust is Option<extern fn()> (explicitly defined and whitelisted to do the right thing), and that is precisely what bindgen generates for them.

@retep998
Copy link

retep998 commented Nov 25, 2017

Raw pointers to uninhabited types are legal and well defined (because they can be created in safe code). You just can never ever deref it. All you have to do is transmute the raw pointer to Option<unsafe extern fn()> with the correct signature. The reason GetProcAddress returns a raw pointer to an uninhabited type instead of an Option<unsafe extern fn()> directly is because there is no fixed signature that the function pointers it returns will use. Otherwise someone could accidentally call the function pointer without first casting it to the correct type (which is bad).

@McGittyHub
Copy link

McGittyHub commented Nov 27, 2017

* friendly bump *

Any ETA on this?

@McGittyHub
Copy link

Is this going to be fixed any time soon?

Currently you cannot run release builds of programs using gl-rs on windows using current nightly rustc.

* yet another friendly bump *

@brendanzab
Copy link
Owner

Sorry for delays, gl_generator v0.7.0 and gl v0.7.0 should be published now with the fixes!

kvark pushed a commit to gfx-rs/gfx_gl that referenced this issue Dec 2, 2017
* Update gl_generate to 0.7

This is required for projects using gfx_gl to be run when compiled with release flag using nightly version of rustc starting with 19. november 2k17.

brendanzab/gl-rs#438

* Update Cargo.toml
mitchmindtree added a commit to mitchmindtree/glium that referenced this issue Dec 11, 2017
@albel727
Copy link

I've just encountered the very same typing bug resulting in segmentation fault due to __GLXextFuncPtr type in glx template, which is used as a return type for GetProcAddress().

pub type __GLXextFuncPtr = extern "system" fn();

Fixing the type fixes the crash for me.

@albel727
Copy link

albel727 commented Jan 1, 2018

So, it's been two weeks. Can we fix it for GLX too? Should I open a new issue?

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

8 participants