4 ms·
> A great strength of Rust is its safety — but something I also appreciate about it is the escape hatch offered via unsafe, whereby certain actions are permitte
by musicale 6y ago
> A great strength of Rust is its safety — but something I also appreciate about it is the escape hatch offered via unsafe, whereby certain actions are permitted that are otherwise disallowed.
I guess so, but I'd kind of prefer to be able to do everything I want in safe mode.
- timClicks 6y agoSometimes you need to work with pre-written libraries. The Rust compiler is unable to reason about anything on the other side of the FFI boundary.
- adwn 6y ago> I guess so, but I'd kind of prefer to be able to do everything I want in safe mode. Different people have different needs and want to do different things. It is impossible to satisfy everyone's needs – "do everything I want in safe mode" – both from a practical perspective (the language and the compiler would become incredibly complex and therefore buggy) as well as from a theoretical perspective (you would solve the halting problem, among other impossible feats).
- moonchild 6y ago> language and the compiler would become incredibly complex and therefore buggy Ats[1] isn't in such bad shape. Its type system is much more complex, sure; idiomatic ats proves many interesting programming attributes that rust doesn't. But if you write ats in the same style as rust, the only additional complexity you'll notice is you have to manually drop. > you would solve the halting problem, among other impossible feats ...what? I don't see what the halting problem has to do it. Safe rust programs aren't required to halt; nontermination is safe, even if not necessarily desirable (like memory leakage, which is also allowed for the same reason). 1. http://www.ats-lang.org/ http://www.ats-lang.org/
- adwn 6y ago> I don't see what the halting problem has to do it. Is the following code safe? function_xyz(); let p = 0x1234 as *mut u32; p.write(5); It is safe iff function_xyz() does not terminate. To determine whether the code is safe in the general case, the Rust compiler would have to solve the halting problem. Since that is impossible, the compiler uses a heuristic, which defaults to "not safe". It could use a better heuristic, like "Does function_xyz() contain an infinite loop? If yes, the code is safe." But that's of dubious value.
- moonchild 6y agoTreating otherwise unsafe code as safe because nontermination makes it dead doesn't strike me as particularly useful. Is there any other case where knowing the termination status of a function is helpful in proving safety?
- adwn 6y ago> Treating otherwise unsafe code as safe because nontermination makes it dead doesn't strike me as particularly useful. It doesn't matter whether it's useful or not. What matters is that the compiler can't determine whether it's safe or not.
- moonchild 6y agoTry ats[1]. It lets you prove safety for a lot of the places where you would need to resort to unsafe in rust. 1. http://www.ats-lang.org/ http://www.ats-lang.org/
- Const-me 6y agoI find it strange Rust marketing people are insisting the language is safe yet it can't dynamically allocate a byte of memory without some unsafe code. In languages like C# or Java the whole standard library is written in C# or Java. Somewhere deep inside implementation of new() there's a function call to C malloc() or some equivalent. The important difference, native API surface is much smaller this way. All else being equal, an API with a small surface has better chances to be secure than a large API.
- zozbot234 6y ago> I find it strange Rust marketing people are insisting the language is safe yet it can't dynamically allocate a byte of memory without some unsafe code. In languages like C# or Java the whole standard library is written in C# or Java. ... C# has an unsafe feature quite comparable to Rust. Java has its sun.misc.Unsafe module. These approaches are not meaningfully better than what Rust does.
- Const-me 6y ago> C# has an unsafe feature quite comparable to Rust. The standard library, and 95% of third-party libraries, don’t use these features. Rust standard library contains a huge amount of unsafe code, with a history of security bugs there: https://medium.com/@shnatsel/how-rusts-standard-library-was-vulnerable-for-years-and-nobody-noticed-aebf0503c3d6 https://medium.com/@shnatsel/how-rusts-standard-library-was-...
- kangalioo 6y agoRust tries to circumvent the _need_ for manual allocation entirely by providing plenty of high level constructs (Box, Vec, Rc, etc.) that you can do to safely and easily do what you might accomplish with manual memory allocation in C
- Const-me 6y agoAll these constructs are implemented in terms of unsafe code. This makes the unsafe API surface (which, in the context of security, becomes attack surface) very large. Compare to Java or C#. Vast majority of stuff, including these collections, are implemented on top of memory-safe primitives. Usually (unless explicitly writing unsafe code which most people don’t do), the only attack surface is the VM itself. The attack surface is much smaller in comparison, both JVM and CLR are very simple from the outside, 202 and 234 instructions, respectively. The attack surface is well-contained, third-party C# or Java libraries don’t patch the VMs, while unsafe rust is used in many third-party crates not just in the standard library. Also, these VMs are well-audited (most of the implementations are open source now) and widely used all over the industry, including very security sensitive applications like banks and military.