2 ms·
> Write unsafe as little as possible and if you need to, put that unsafe block in as small a module as possible. That's mostly it. I don't have links at hand,
by majewsky 7y ago
> Write unsafe as little as possible and if you need to, put that unsafe block in as small a module as possible. That's mostly it.
I don't have links at hand, but there were already instances where a bug in an `unsafe` block had effects in completely different (and seemingly random) places. Discovering all the ways in which `unsafe` blocks can cause unsafe or undefined behavior in unrelated places is still an active field of research.
> It would be disastrous for Linux to drop support for it's long tail of platforms because Linux could no longer be built for those platforms.
Which is why driver modules are a good place to start. Drivers are specific to certain pieces of hardware which are oftentimes only used with CPUs of a specific ISA.
- masklinn 7y ago> Discovering all the ways in which `unsafe` blocks can cause unsafe or undefined behavior in unrelated places is still an active field of research. unsafe blocks can cause UB period, UB means the program is broken but the UB can manifest anywhere. C or C++ don't make this any better, they just make the entire program into a source of UB.
- majewsky 7y agoExactly. But many Rust proponents do not communicate that clearly. They often make it sound like unsafe blocks contain the undefined behavior and prevent it from spreading to the rest of the program, which they don't.
- masklinn 7y agoThat's very true. The value of unsafe blocks is that they restrict the number of places you need to inspect / audit for UBs. They're just places where you're telling the compiler "I know what I'm doing", once an unsafe blocks has created an UB thing can break anywhere.
- renox 7y agoI don't think that's really true, let's suppose that your unsafe code has a presupposition that cause an UB if not met. If this is a bug and you can remove the presupposition, great, but is-it always possible without performance issue? If not, then you have to audit all the usage of the unsafe part.
- masklinn 7y ago> I don't think that's really true It is though. > let's suppose that your unsafe code has a presupposition that cause an UB if not met. If this is a bug It is, or the code should not present as being safe. > but is-it always possible without performance issue? If not, then you have to audit all the usage of the unsafe part. If it's not possible to fix it (or if you don't want to fix it) then the wrapper for that unsafe code should also be unsafe, and it should document its assumptions such that callers can know what to look for. The tautological contract is that safe rust is safe. If it's possible to trigger UB by passing the "wrong" value to a rust function then that function is not safe and must be marked as unsafe itself. An unsafe block means the compiler trusts that you know what you're doing, which is different from lying to the compiler, which is what you're apparently advocating / defending.
- the_duke 7y agoThe important distinction is cause vs effect. Only code in unsafe blocks can cause unsafety (ignoring already and yet to be discovered soundness bugs in the compiler [1]). But the effect can easily materialize in any location that uses the unsafe code, or types that go through it. To me this is somewhat obvious, but it's true that this is easily overlooked and should be communicated. To express this better in the type system, Rust would need an effect system. [1] https://github.com/rust-lang/rust/issues?q=is%3Aopen+is%3Aissue+label%3A%22I-unsound+%F0%9F%92%A5%22 https://github.com/rust-lang/rust/issues?q=is%3Aopen+is%3Ais...