unsafe { /* arbitrary code in here */ } // applied to a block
unsafe fn foo() { /* function body */ } // applied to a function
It's the second form that is infectious. When applied to a function, that function can only be called from an unsafe block or another unsafe function. The use of this form is to say, "hey, as the author of this function, I have no ability to determine whether or not this function is safe to use; let the caller beware."However if, as the author of a function, you can take steps to ensure that the function is used safely, then you can stop the infection with an unsafe block.
For illustration, let's do one of the things mentioned in the post. Here's a function that prints every element in a vector:
fn foo1(v: Vec<int>) {
let length = v.len();
let mut i = 0;
while i < length {
println!("{}", v[i]);
i += 1;
}
}
That `v[i]` in there is potentially doing bounds checking (in practice a simple case like this would get optimized away by LLVM, but let's ignore that), but note that this is superfluous because we've written this code such that `i` will always be a valid index. So let's turn off bounds checking by using the `unsafe_get` method mentioned in the OP: fn foo2(v: Vec<int>) {
let length = v.len();
let mut i = 0;
while i < length {
println!("{}", unsafe { v.unsafe_get(i) });
i += 1;
}
}
Like I was saying above, `unsafe_get` is a function that is itself marked unsafe, because the author of `unsafe_get` knows that you can cause memory unsafety by passing in an invalid index. But because we've written this program carefully, we know that both foo1 and foo2 are exactly as safe, so we've fenced off the unsafety here with the `unsafe` block. Both foo1 and foo2 can be called from safe Rust code with no special consideration.Now, what happens when, years down the road, someone who isn't paying attention accidentally modifies this function?
fn foo3(v: Vec<int>) {
let length = v.len();
let mut i = 0;
while i <= length { // look carefully
println!("{}", unsafe { v.unsafe_get(i) });
i += 1;
}
}
This version of the function contains an off-by-one error. Anyone calling foo3 will try to index beyond the end of the array, and all hell will break loose. What do you do then? Well, you grep for the word "unsafe" and you start auditing unsafe blocks, looking at all the inputs that you send into them. This is generally the same approach you'd take in any other systems language, except that by limiting where unsafe code can appear you reduce the code that can potentially go wrong by 10-100 times, and speed up your auditing commeasurably. And the promise made in the OP stands: if you don't yourself use `unsafe` code and memory safety happens, it's someone else's fault, not yours. :)And how often do you need to resort to unsafe code in Rust? It depends on your domain. Efficient data structures often require all sorts of custom pointer tricks, so maybe a tenth to a third of their code is unsafe in Rust. For application-level programming, I'd expect no more than 1-3% unsafe code (maybe up to 5-10% if you're trying to push performance to the absolute limit, like Servo is doing).
That's not a very good solution. "Kill problems, don't just wound then" - Kelly Johnson, Lockheed Skunk Works, designer of the U-2 and SR-71.
I used to work on this problem, about a decade ago. I was trying to figure out how to make C++ safe. I finally gave up, since memory safety wasn't a priority with the C++ committee. I'd proposed scope-based "borrowing" (I used the term "keeping", as the inverse of borrowing) and reference counts.
Complex data structures remained a problem. The best idea I could come up with was to use weak pointers heavily. When you have something like a GUI widget library, with pointers going upwards, downwards, and sideways, who owns what is a big issue. One solution is that all the graphics widgets are owned by some top level object, like the "window" object, all the inter-widget links are weak, and all the weak pointers are in objects that can't outlive the top level object. This creates a sort of allocation pool. Objects pointed to by weak pointers can't fully go away until the parent object of all the weak pointers goes away. They only get marked as invalid for weak pointer referencing.
This is memory-safe, but you don't get the space used by widgets back until the top-level page object is deleted. That can be a problem for long-lived graph structures with a lot of churn, like the scene graph in a game or the active collision structures of a collision detection system. For most applications, though, it's not a big deal; browsers build a DOM, use a DOM, and discard a DOM when the page closes. Might be a problem if you left Facebook open for a month.
Go for safety. We do not need yet another unsafe language. We have plenty of those. The day there's a CERT advisory for a buffer overflow in a Rust program, Rust has failed.
> We do not need yet another unsafe language. We have plenty of those.
I sadly must disagree. If safety were truly paramount, people would have dropped C++ entirely in favor of Java in the 90s. The people still using C++ aren't idiots, they just have requirements with respect to efficiency, control, determinism, and expressiveness that other languages don't match. Rust exists to provide an alternative to these people, so that we can at last bring some semblance of safety to domains with the requirements intimated above. > The day there's a CERT advisory for a buffer overflow in a Rust program,
> Rust has failed.
I dread that day, and yet I know that it's inevitable. In the meantime, that same buffer-overflowing Rust program may well have produced ten times the CERT advisories had it been written in C. I'm not an absolutist; I'll take that improvement as a win. > I used to work on this problem, about a decade ago.
> I was trying to figure out how to make C++ safe.
Honestly, the Rust developers would love your perspective if you'd like to take a harder look at Rust.Normal compiler hoisting operations aren't allowed to do that. It violates normal execution order semantics. If you don't do that, you can't optimize checks fully. This is why generating assert statements in macros or templates and then trying to optimize them doesn't work. Asserts may need to be hoisted through statements with side effects. Compilers have to handle that at the flow graph level.
This works best if you're not allowed to capture such errors. If you're headed for an unavoidable crash, it's best to have it sooner, rather than later. Go takes a hard line on this - no exception mechanism. (Admittedly some people are abusing Go's "panic/recover".) Google's position is that in their kind of operation, it's better to fail the whole program. They can recover from a failed server, after all. Erlang also take a "fail early" position.
As for the need to eliminate errors, the recent history of computer language development has been in the direction of increased safety. All the major scripting languages are memory-safe. Newer hard-compiled languages such as D and Go are memory-safe, although they have to use GC to do it. A whole generation of programmers have never had to look for a buffer overflow or a dangling pointer.
Meanwhile, in the security world, the buffer overflow threat has become worse. It's not script kiddies any more. It's national intelligence agencies (ours, theirs, who knows who else?) with big budgets and competent people looking for exploits. There's a black market in zero-day exploits now. Rust has a fighting chance to fix that. Not just trim it back a little. Fix it. Please don't blow this chance.
Latest buffer overflow reported by CERT: November 14, 2014:
https://www.us-cert.gov/ncas/alerts/TA14-318B
The Microsoft Windows OLE OleAut32.dll library provides the SafeArrayRedim function that allows resizing of SAFEARRAY objects in memory.... In certain circumstances, this library does not properly check sizes of arrays when an error occurs. The improper size allows an attacker to manipulate memory in a way that can bypass the Internet Explorer Enhanced Protected Mode (EPM) sandbox as well as the Enhanced Mitigation Experience Toolkit (EMET).
This vulnerability can be exploited using a specially-crafted web page utilizing VBscript in Internet Explorer. However, it may impact other software that makes use of OleAut32.dll and VBscript. Exploit code is publicly available for this vulnerability. Arbitrary code can be run on the computer with user privileges.
That's why "carefully checked" "unsafe" code doesn't work. "SafeArrayRedim" - wasn't.
They all have the ability to write unsafe code in the same manner as Rust; but only Rust has it really built-in to the language so the compiler can reason about it. E.g.
- http://golang.org/pkg/unsafe/
- https://docs.python.org/3.4/library/ctypes.html
It just doesn't make sense to presuppose that it is any worse in Rust. In fact, quite the opposite, the type system is designed for ensuring memory safety without a GC, and in multithreaded environments. I believe this is not true for Go (it is vulnerable to data races, and requires a runtime tool to detect some subset of the ones can occur in any given program), but I don't know about D.
Rust cannot remove `unsafe` without being useless for its target, and useless as a language (it wouldn't even have FFI).
The low-level code has to exist somewhere, vectors need to be allocated, strings need to be printed, and people will want the performance. This code is either going to be in external libs called via FFI, as raw codegen in the compiler or in Rust. The last sounds like the one that is safest since the FFI code would have to be written in one of the unsafe systems languages, while writing in Rust gives all the type system advantages and high-level features (references, pattern matching), not to mention avoiding the (mental) overhead of correctly interacting between languages.
It seems to me that taking away the low-level power of opt-in `unsafe` is just myopically punting the problem along, rather than seeing it as giving Rust the ability to avoid having to write really unsafe C plugins (like Python etc.).
I like to think of Haskell as a parallel: It guarantees side effect-free code. But you still need an escape hatch in the form of unsafePerformIO in order to use the FFI. The language's semantics can easily be broken with that functionality, but it seems to be a necessary price to pay if you want to have any kind of FFI. (<- Well, I guess something like Proof Carrying Code could be an alternative..)
> All the major scripting languages are memory-safe.
> Newer hard-compiled languages such as D and Go are memory-safe
Rust's definition of safety actually surpasses most of these languages, as it extends to safety in the face of concurrent operations. Rust statically prevents data races, which AFAIK is a property that until now has only been exhibited by thoroughly immutable languages such as Erlang. That it manages all this without a GC is very exciting. > Meanwhile, in the security world,
> the buffer overflow threat has become worse.
The Rust developers agree with you. Rust is motivated by the fact that Mozilla is tired of Firefox getting pwned by dint of C++'s looseness, and despairing at the fact that further improvements to Firefox will entail tacking concurrency onto a multi-million-line C++ codebase dating back to 1997, which would be notoriously difficult even if security weren't a concern. They're writing a brand-new browser engine in Servo with a focus on massive concurrency and user safety.I guarantee you that Rust cares a great deal about safety. It goes to lengths that almost no other language is willing to go in pursuit of this goal. However, it is also a thoroughly pragmatic language. We won't make the world a better place by blindly preaching dogma. Rust also won't be the last language ever made. If nothing else, even baby steps toward a safer world are a measure of progress. :)
The low-level code has to be written somewhere (Rust needs to have a vector type, needs to have an efficient hashmap), and frankly, writing it in Rust is better than writing it in C and using FFI to import it.
Rust is going for safety.
Do we? The only ones I hear about people using are C/++. On the other hand, safe systems languages seem to end up being forgotten, as people opt for C/++ instead for systems/performance domains. Presumably in part because they use things like garbage collection, giving up some of the more raw control and probably also speed that C/++ provides.
It really seems that any language that wishes to challenge C/++ on their own turf needs to offer the power of C/++ as far as it is possible. In Rust's case, that is possible as long as you're being explicit about where the safe interfaces end and the unsafe ones begin.
I might point out to the grandparent post that even the basic vector collection in Rust is full of unsafe {} blocks.
1) > unsafe code is infectious like Java's exceptions. If you call an unsafe function, you need to mark yourself as unsafe, or explicitly state the boundaries where the unsafety is handled.
2) > Unsafe is for the lowest levels of abstraction to deal with
So according to 1) unsafe code is infectious but according to 2) you should only use it at the lowest level in your code. By this I understand things like mmap-ing a file, get a value out of shared memory, interacting with C code and so on. Presumably if your high level code depends on low level code, wouldn't the lower level "usafety" bubble all the way to the top. And next thing your main do_top_level_business_logic() function has to be wrapped in an unsafe block?
Now while typing this I am thinking of maybe one way to handle is and that is to create a separate task and shuffle data via messages between another unsafe task. If task crashes just restart it...