Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upReview all safe code that takes raw pointers in stylo and other places #16572
Comments
|
Most |
Sorry, I disagree. We should not be calling these functions at all from Servo. It would be nice to have a tidy lint enforcing that. Firstly, most The problem with keeping them unsafe like this is that now basically everything is unsafe. We've diluted the utility of unsafe in pinpointing things that can go wrong. There's a larger issue of passing garbage data to the This has helped us catch some unsafe issues already, because it's very easy to root around unsafe code touching things related to a segfault you're seeing. There is the implicit understanding that glue.rs relies on various contracts with C++ that are not expressed in the Rust way, so ultimately it's still known that it's unsafe to some degree, which is enough -- rather than making the explicit cosmetic change that hobbles the safety wrappers, we use the implicit one. |
|
It's not a matter of tidy, or whatever unspoken invariant you wished everyone followed. The function exists, the function is exported from the crate, the function does unsafe things, therefore it should be unsafe.
That's because the functions do too much, instead of doing pointer to reference conversions and such things, and then calling very few safe functions, it does many unrelated things, and that's why you want to reduce the amount of unsafe code. I didn't know that the glue was even that unsafe, because all of the raw pointers are hidden behind type aliases. This code is dangerous, and this will bite us back in the future. |
|
Is there no way to export a function to C but not Rust? Or does |
An FFI crate that only contains extern C functions and is obviously to be called from C/++ code.
Yes, this is an improvement I've wanted to do, it's just low priority for me (because of the aforementioned "it's safe as long as you don't call it from Rust"). Go ahead and do it if you want. It enables a lot of improvements in the way we do safety glue, too, reducing dependence on bindgen replace.
There's precisely one accessible raw pointer hidden behind an alias, and that's the owned pres context. I'm aware of this, but there were some tricky issues around pres context ownership (this ties into the Device issue) so it's been on the backburner. (If there are more, that should be fixed) There is a bit of a safety hazard here in that you could accidentally use one of the owned/borrowed types without generating the right wrapper via build_gecko.rs. I have yet to figure out a way to deal with this; though we could somehow enforce that the type is not a pointer (there's already a unit test that enforces that the function types match). There are a bunch of glue functions which take Here are all the safety issues that have been put on the backburner:
|
Functions such as
style::gecko::media_queries::Device::newtake aRawGeckoPresContextOwnedthat is ultimately just a raw mutable pointer. This means one can writeDevice::new(0xdeadbeef as *mut _)and the code will happily compile and probably segfault at runtime, that is unsound, and this shouldn't be doable from safe code. There are probably many other occurrences of that problem in stylo in general, so we should review that.Cc @Manishearth