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

HANDLE pointers are incorrect in at least one union #452

Closed
kichristensen opened this issue May 30, 2017 · 7 comments
Closed

HANDLE pointers are incorrect in at least one union #452

kichristensen opened this issue May 30, 2017 · 7 comments

Comments

@kichristensen
Copy link
Contributor

The file handle returned by LOAD_DLL_DEBUG_INFO, obtain from DEBUG_EVENT, will never be valid in a 64-bit application.
This due to the fact that Microsoft for compability reasons has defined that HANDLEs defined in 64-bit applications, uses 32-bit handles. Because of the way unions are implement in winapi-rs, this will return an incorrect handle.

See https://msdn.microsoft.com/en-us/library/aa384203.aspx for more information about this.

@retep998
Copy link
Owner

I'm not exactly sure what you're trying to say. It doesn't really matter what values handles actually have, winapi simple binds the headers as they are. If there is a mistake with the bindings themselves, then point out the exact binding that is in error. Otherwise I'm not really sure what you want me to do here.

@kichristensen
Copy link
Contributor Author

Sorry I should have explained better. Here is an example:

let dll_info = unsafe { debug_event.LoadDll() };
println!("Handle: 0x{:x}", dll_info.hFile as usize);
println!("Correct handle: 0x{:x}", (dll_info.hFile as usize).rotate_right(32));

The output could be something like this:

Handle: 0x19c00000000
Correct handle: 0x19c

The reason this happens is because when compiling for a x64 platform the pointer itself is 64 bit, but it should be intrepreted as a 32 bit pointer when working with handles.

@retep998
Copy link
Owner

Due to how little endian works, interpreting a 32bit handle incorrectly as a 64bit handle would not shift it up by 32bits. That could only happen if you were reading from the wrong offset. The bindings in winapi seem to be correct according to the headers, so I'm failing to see what could be causing you to get invalid handles Is this an issue you're actually experiencing, and if so do you have an example program I could check out to see the problem for myself?

@kichristensen
Copy link
Contributor Author

Yes this is an issue I'm experiencing, and if verified the workaround in C (as good as possible as handles changes). I'll create a small sample in Rust and C tomorrow evening and post

@kichristensen
Copy link
Contributor Author

kichristensen commented May 31, 2017

@retep998 Here is some Rust code not working as I would expect without my workaround. Let me now if I should create the same in C.

https://gist.github.com/kichristensen/ddfa5b9e2982afbfe9e20e6a8f5b78f6

I'm always getting invalid handle without my workaround

@retep998
Copy link
Owner

retep998 commented May 31, 2017

Ah I see, you're using winapi 0.2. I rewrote that code using winapi 0.3 (which isn't done yet so I had to bind a few functions inside the example itself) and it appears to be working correctly. I'm guessing the old definition of that union was incorrect. All the more reason to finish up winapi 0.3 and get it published.

https://gist.github.com/retep998/da9586ce0c4ded14b33d3fe535c683ec

@kichristensen
Copy link
Contributor Author

It sure does, thanks

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

No branches or pull requests

2 participants