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

Invaid prototype of PDRIVER_UNLOAD and other callbacks #47

Closed
HoShiMin opened this issue Aug 17, 2023 · 6 comments
Closed

Invaid prototype of PDRIVER_UNLOAD and other callbacks #47

HoShiMin opened this issue Aug 17, 2023 · 6 comments

Comments

@HoShiMin
Copy link

As earlier was stated here: microsoft/windows-rs#2612, there are no arguments in callack definitions like PDRIVER_UNLOAD in the Foundation module of WDK.

pub type PDRIVER_UNLOAD = ::core::option::Option<unsafe extern "system" fn() -> ()>;

But in C++ WDK we have this:

//
// Define driver unload routine type.
//
_Function_class_(DRIVER_UNLOAD)
_IRQL_requires_(PASSIVE_LEVEL)
_IRQL_requires_same_
typedef
VOID
DRIVER_UNLOAD (
    _In_ struct _DRIVER_OBJECT *DriverObject
    );

So, the Rust callback should be:

pub type PDRIVER_UNLOAD = ::core::option::Option<unsafe extern "system" fn(driver_object: *mut DRIVER_OBJECT) -> ()>;
@mikebattista
Copy link
Contributor

DRIVER_UNLOAD is in the metadata. Does that work as expected?

@kennykerr
Copy link

Although the currently published version of windows-sys 0.48 does not include this declaration, the latest version on GitHub has the expected type:

pub type DRIVER_UNLOAD = ::core::option::Option<unsafe extern "system" fn(driverobject: *const super::super::Foundation::DRIVER_OBJECT) -> ()>;

You can test by pointing your Cargo dependency to the git repo.

@HoShiMin
Copy link
Author

@kennykerr, I can't found this. I setted windows-sys using a git link:

[dependencies.windows-sys]
git = "https://github.com/microsoft/windows-rs.git"
features = [
    "Win32_Foundation",
    "Win32_Security",
    "Win32_System_IO",
    "Win32_System_Kernel",
    "Win32_System_Power",
    "Win32_System_WindowsProgramming",
    "Win32_System_Ioctl",
    "Wdk_Foundation",
    "Wdk_System",
    "Wdk_System_IO",
    "Wdk_System_SystemServices",
    "Wdk_System_Threading",
    "Wdk_Storage",
    "Wdk_Storage_FileSystem"
]

And we still have no arguments in the master branch: https://github.com/microsoft/windows-rs/blob/master/crates/libs/sys/src/Windows/Wdk/Foundation/mod.rs#L2208

@kennykerr
Copy link

Looks like a metadata bug. There's Windows.Wdk.System.SystemServices.DRIVER_UNLOAD which appears to be correct and then there's Windows.Wdk.Foundation.PDRIVER_UNLOAD which appears to be incorrect.

@vlabo
Copy link

vlabo commented Sep 14, 2023

Here is a workaround for anyone else who came across this issue until it is fixed:

let driver_unload_fn: windows_sys::Wdk::System::SystemServices::DRIVER_UNLOAD = Some(the_driver_unload_function);
(*driver_object).DriverUnload = core::mem::transmute(driver_unload_fn);

This also works for the Major functions array:

type WriteType = Option<unsafe extern "system" fn(&mut windows_sys::Wdk::Foundation::DEVICE_OBJECT, &mut windows_sys::Wdk::Foundation::IRP) -> windows_sys::Win32::Foundation::NTSTATUS>;
let driver_write_fn: WriteType = Some(the_read_function);
(*driver_object).MajorFunction[windows_sys::Wdk::System::SystemServices::IRP_MJ_WRITE as usize] = core::mem::transmute(driver_write_fn);

@mikebattista
Copy link
Contributor

//
// Define driver unload routine type.
//
_Function_class_(DRIVER_UNLOAD)
_IRQL_requires_(PASSIVE_LEVEL)
_IRQL_requires_same_
typedef
VOID
DRIVER_UNLOAD (
    _In_ struct _DRIVER_OBJECT *DriverObject
    );

typedef DRIVER_UNLOAD *PDRIVER_UNLOAD;

@tannergooding is there anything we can do to scrape these pointer-to-delegate typedefs in a better way? PDRIVER_UNLOAD and other typedefs like this result in an empty delegate, and then wherever these typedefs are referenced results in an empty delegate rather than a pointer to the real delegate.

We can manually exclude these typedefs and then remap all occurrences of them to use the original delegate, but there are a lot of instances of these so it would be better if we could handle this while scraping.

Is ClangSharp behaving as expected here? Are there any config options that could help us?

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

4 participants