Hacker News new | ask | show | jobs
by dijit 436 days ago
this seems like a good place to ask, I don’t write very much unsafe Rust code… but when I do, it’s because I’m calling the Win32 API.

Tools like valgrind do not work on windows, and I am nowhere near smart enough to know the entire layout of memory that should exist.

When using Windows and calling system system functions, there’s a lot of casting involved; to convert wide characters and DWORDS to rust primitives for example. And given that I don’t have a good debugging situation, I’m terrified that I’m corrupting or leaking memory.

does anyone know any good tools that work on windows to help me out here?

3 comments

The easy solution is, don't call system functions. Instead:

• Work out what you want to do, conceptually.

• Design a safe abstraction that would allow you to do that. (Consult the Win32 API documentation for concepts to use.)

• Implement that abstraction using the Win32 API.

That last step is way easier than trying to use the Win32 API throughout your program, you'll end up with significantly less unsafe code, and if anything does go wrong, it's much easier to fix.

that’s what I’m doing already, the issue is that unsafe code exists at all.

In order to call the win32 API one must create structs and pass pointers to them into the function call.

sending data is actually quite easy. But reading back data is quite difficult, in some cases you may have a list of something.

Regardless, Rust is not helping me anymore in those bits, and since all of the tools that find memory issues target primarily C++, and rust mangles certain things for C+ + toolchains - I find myself a little bit stuck, I’m not a genius and I’ll take all the help I can get.

The Win32 API's object model is (mostly) compatible with Rust's. Handles play well with OBRM. Does the winsafe crate provide the interfaces you need? https://docs.rs/winsafe/
if it does, I can’t find it.

I was looking for NetUserAdd and associated commands.

There are plenty of tools, but they are C and C++ specific.

Starts with Visual C++ analysers, SAL annotations, hardned runtime.

Then commercial tooling like PVS Studio, Parasoft for example.

> I don’t write very much unsafe Rust code… but when I do, it’s because I’m calling the Win32 API.

Check out windows-rs instead.

https://github.com/microsoft/windows-rs

I did look at that first.

In my case I am looking for NetUserAdd and associated functionality, which doesn’t exist in any wrapper crate I could find- since that would have been significantly easier than what I ended up needing to do.

But, how do they test their unsafe bits?