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

Enable the native Windows subsystem #1955

Open
yasammez opened this issue Mar 22, 2017 · 4 comments
Open

Enable the native Windows subsystem #1955

yasammez opened this issue Mar 22, 2017 · 4 comments
Labels
T-compiler Relevant to the compiler team, which will review and decide on the RFC.

Comments

@yasammez
Copy link

yasammez commented Mar 22, 2017

Since this is my first opened issue and I am not entirely sure if I am doing this right, feel free to moderate this as necessary and/or move it to the correct queue. Since this is a feature request, the guidelines told me to open the issue here, so this is what I am doing, alas not with great confidence (this is by no means an RFC).

Right now, if I want

 extern "fastcall" fn unload(_ : &mut DRIVER_OBJECT) {
     unsafe {
         DbgPrint("Goodbye World\n\0".as_bytes().as_ptr());
     }
 }

 #[no_mangle]
 pub extern "fastcall" fn DriverEntry(d : &mut DRIVER_OBJECT, _ : *const u8) -> u32 {
     unsafe { 
        DbgPrint("Hello World\n\0".as_bytes().as_ptr()); 
    }
     d.DriverUnload = unload;
     0
 }

with the type bindings from eg. the winapi-kmd-rs crate to successfully run on my machine, I have to compile it with

rustc --emit obj driver.rs

and then link it manually

link.exe /subsystem:native,5.02 /entry:DriverEntry /out:driver.sys driver.o ntoskrnl.lib

It would be great to be able to build this with cargo and be it only because the rls only supports cargo projects at the moment. For this to happen, there'd need to be the possibility to write

#![windows_subsystem = "native"]

at the top of the crate which would prompt cargo to link it with the /subsystem:native switch and against the ntoskrnl.lib library instead of the standard userspace Windows libraries. A quick test showed that this will probably have to go in conjunction with somethingl ike #![no_std] or some additional work is required to define what happens should a panic! occur; this is actually not easy to do because a Windows Kernel Mode driver can only be successfully unloaded if it calls IoDeleteDevice on all its devices, which should be possible (all the devices are available as a linked list through the DRIVER_OBJECT variable passed to the unload function) but which itself can only be called once there are no pending interrupt/io requests on that device. For now I would be content with something as small as

#[lang = "panic_fmt"]
#[no_mangle]
pub extern fn rust_begin_panic(_msg: core::fmt::Arguments,
                               _file: &'static str,
                               _line: u32) -> ! {
    unsafe {
        DbgPrint("A panic occured!\n\0".as_bytes().as_ptr());
    }
   loop {}
}

Also, the aforementioned winapi-kmd-rs should probably be reviewed (some unions in MS-structs are implemented by picking a more or less random member — maybe these could be made into untagged unions once these land) and if deemed worthy, be elevated to something more „official“ like the winapi crate.

@mark-i-m
Copy link
Member

TBH, I know almost nothing about compiling on Windows. But I was wondering if this would be served by #1489. Are they related?

@yasammez
Copy link
Author

I don't really see a connection here: the RFC is about compiling and subsequently linking resource files into the program while this issue is about linking the program in a special way to make it loadable as a kernel mode driver. Maybe I am missing something?

@mark-i-m
Copy link
Member

Ah, I see. I guess the connection is very loose then... I would also like to see special linking abilities in stable rust. Currently, I think we only have the nightly #[link_args(...)], and IIRC, it only supports passing -l and -L...

@Centril Centril added the T-compiler Relevant to the compiler team, which will review and decide on the RFC. label Dec 6, 2017
@bjorn3
Copy link
Member

bjorn3 commented Aug 17, 2023

Windows kernel mode drivers should probably be a separate target rather than using #![windows_subsystem] given that libstd would either be entirely unavailable or use a different implementation based on the windows kernel driver api's.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-compiler Relevant to the compiler team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

4 participants