-
Notifications
You must be signed in to change notification settings - Fork 49
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
ctor/dtor to be made always unsafe in 1.0 #159
Comments
I disagree. |
Can't it cause this, in some environments? My knowledge of libstd is rusty at best, but I think that there are at least some global variables that can't be accessed early like this. In addition, I don't think libstd should accommodate the use case where stuff happens before |
Maybe? If so that would be a good argument for being |
The big issue is that something as simple and fundamental as I've been pondering whether it is possible to allow a reduced subset of code that can run without |
So what would be the safety advice to the user? "Don't use anything from the standard library?" I notice that some of the examples in the README do access the standard library. |
@oscartbeaumont segmentation fault is not the same thing as UB. Programs do use segfault to avoid UB. So your demo could be the protection working as intended but I have no idea if it actually is. Anyway, I think assuming |
Would be really nice if we could have safety advice, I plan to replace |
I second the desire to add the requirement that functions annotated with The point is not that the function itself is inherently unsafe, but that a library may want to perform initialization within Hence the soundness invariant of any ctor function is at minimum that it doesn't rely on safe abstractions that require another ctor to have run. This is in line with the philosophy that "nothing happens before and after main()", in the sense that it would be nice to be able to say that anything that does happen before main() may be a prerequisite for the soundness of code inside main(). The standard library seems to be making at least somewhat similar assumptions. This invariant would be very, very useful in conjunction with crates such as linkme. Use caseMy use case is a string interning library, where interned string "literals" are frequently present in the code. I need to guarantee that all identical strings in the program are unified before the user sees them. Without the above invariant, this is not possible to achieve without some runtime check or indirection at the point-of-use. Ideally, I would like runtime use (i.e. within Example to illustrate the general idea, with many details omitted: #[linkme::distributed_slice]
static LOCATIONS: [UnsafeCell<&'static str>] = [..];
#[ctor]
unsafe fn unify() {
// MUST RUN BEFORE ANY CALL TO sym!() IS REACHED!
for location in LOCATIONS {
// Unify duplicate strings in-place.
}
}
macro_rules! sym {
($string:literal) => {
#[linkme::distributed_slice(LOCATIONS)]
static LOCATION: UnsafeCell<&'static str> = UnsafeCell::new($string);
unsafe {
// MUST RUN AFTER unify()!
*LOCATION.get()
}
};
} Currently I'm solving the problem without requiring a I realize that the |
This library requires you to know what you're doing, and making ctor/dtor unsafe is the right way to go. Most users are probably already using
unsafe
anyways, as this is often used to interface with C code.The text was updated successfully, but these errors were encountered: