There NEEDS to be an os dev attribute!

Ever sense like nightly 2022 they changed stuff like no mangle:
Now you have to do this:

#[unsafe(no_mangle)]

there should be a global osdev attribute:

#[unsafe(os_dev)]

BOOM! your project would need way less unsafe and only needed for actually unsafe stuff.

It is still the same amount of unsafe. Just hidden.

#[unsafe(no_mangle)] is actually unsafe. Misusing is is genuinely UB.

2 Likes

Only if you use it incorrectly:

#[unsafe(no_mangle)

That looks unsafe but really you NEED it so your linker will be happy.

If you use unsafe no mangle it just makes it look unsafe when actually its only to make the linker happy.

It looks unsafe because it is unsafe. Unsafe means nothing more and nothing less than that there is a requirement you need to check to avoid UB that the compiler can't check for you. In the case of #[unsafe(no_mangle)] that would be that for well-known symbols (memcpy, on unix targets things like malloc, ...) you follow the specified behavior of this symbol. So for example the following thing is UB:

#[unsafe(no_mangle)]
fn malloc() -> *mut u8 {
    1 as *mut u8
}

This has absolutely nothing to do with making the linker happy. Linker reported conflicts are the least of your worry if you use #[unsafe(no_mangle)] the wrong way. It is precisely the cases where the linker doesn't report conflicts that you have to worry about UB.

8 Likes

That's the very definition of unsafe: something that can be easily used correctly or incorrectly.

4 Likes

I mean, yes? That's what all unsafe is. If there was no way to use it correctly it would just be an error, not unsafe.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.