News for hackers. Stuff that matters.
Re:Rust means never having to close a socket by shadowmint on Tuesday October 07, 2014 @08:43PM (#8424775)
As someone who's been using (and really liking) rust for about 6 months, there are two things in this article which are, I'd say, 'common misconceptions' about rust:

    One of the coolest features of Rust is how it automatically manages 
    resources for you, while still guaranteeing both safety 
    (no segfaults) and high performance.
In rust there are two types of code. 'safe' and 'unsafe'. The above is only true if your entire code path contains only safe code.

NOT, if you only write safe code.

That is, if you write entirely 100% safe code, and use some library that uses 'unsafe' code at a low level (this is very common; for example, an C binding or memory optimization), then rust does NOT guarantee safety.

Furthermore, there is no way to know if your code path includes unsafe code.

Rust is relatively safe; it's just not totally safe. Using rust does a reasonable job of protecting you from segfaults most of the time; but it's not a silver bullet.

    As soon as the program stops using the resource, its cleanup logic gets 
    invoked.
    
    ...

    Because only one scope owns an object at a time, you can tell just by 
    looking at it which objects will be destroyed when it's done executing.
This isn't always necessarily true.

Rust uses 'drop flags', which are extra bytes at the end of struct instances in memroy, to track objects and keep meta information about them to determine if they should be dropped.

At the end of scope when the drop checker runs it checks against these drop flags to determine if an object should be dropped or not.

(For example, if a closure captures a variable it may no be dropped at the end of the scope it would normally have been dropped in).

There seems to be this myth that resource deallocation in rust is magically determined at compile time and the has 'zero cost'; it's simply not true.

There is a runtime cost to the drop checker; it's not particularly large, but it's certainly not just a sequence of drop operations.

Don't get me wrong; I really enjoy rust, and I like it a lot. ...but those two ideas pop up repeated in discussions about it, and they're not exactly correct.

Re:Rust means never having to close a socket by pcwalton on Tuesday October 07, 2014 @10:52PM (#8425105)
> That is, if you write entirely 100% safe code, and use some library that uses 'unsafe' code at a low level (this is very common; for example, an C binding or memory optimization), then rust does NOT guarantee safety.

What do you consider a "guarantee"? Do you consider pure JavaScript to "guarantee" safety, even though there have been exploitable bugs in the JIT and interpreter? Do you consider pure-computation, non-allocating Rust to be safe, even though LLVM may contain codegen bugs that allow for undefined behavior? Do you consider seccomp-isolated code to be safe, even though there have been, and will likely be in the future, kernel bugs that allow sandboxed process to escape?

The point is that instead of talking about "100% safe", which is never the case in the real world, I think it's better to talk about trusted computing bases. That is, instead of saying "X is safe" or "X is unsafe", we talk about what effect X has on the memory-unsafe surface area of a program. Rust's goal is to reduce the trusted computing base of an application written in it to the hardware, compiler, unsafe blocks, and type system. This gives us essentially the safety properties of "managed languages" (except I don't like that term because it tends to imply garbage collection and lack of fine-grained control over memory, which don't apply to Rust).

A specific example I like to give: Sure, we could hardwire (for instance) Vec into the language, and therefore reduce the number of lines of unsafe code in the libraries. But doing so would just be moving the unsafe code from the unsafe blocks in the libraries to the compiler itself. There would be no net gain in safety from it—compiler code can have bugs just as library code can—and there would be a decrease in maintainability and flexibility.

You can think of Rust's safety properties as establishing a type of sandbox if you'd like. All sandboxes have trusted computing bases, and Rust's compile-time sandbox is no exception. But "safety" as applied to a sandbox still has an important meaning. Chromium/Firefox OS's sandbox, for example, is a safe sandbox, even though its security depends on strong assumptions about the trustworthiness of the OS kernel and the IPC layer between the trusted and untrusted processes.

> There is a runtime cost to the drop checker; it's not particularly large, but it's certainly not just a sequence of drop operations.

After the drop optimizations are done, this will only be true if you conditionally move an object on one or more branches. It's already true today if the object gets SROA'd on the stack, as LLVM can then optimize out the drop flags. We've talked about adding a lint so the compiler can optionally warn and allow you to fix it if this performance cost is a concern to you. (It's a very minor cost—one test and branch on a stack byte, and again only if the object is conditionally moved.)

Note that C++ move semantics incurs essentially the same cost.

Re:Rust means never having to close a socket by jerf on Wednesday October 08, 2014 @09:45AM (#8426807)
"We've talked about adding a lint so the compiler can optionally warn and allow you to fix it if this performance cost is a concern to you. (It's a very minor cost—one test and branch on a stack byte, and again only if the object is conditionally moved.)"

Might I just make a meta comment that while that may not be a huge use case, the ability to assert that some particular optimization is firing and get warnings if I've done something to break it is something I've wanted out of a language for a long time. I know it's easier said than done, but I'd love it.

Re:Rust means never having to close a socket by wycats on Tuesday October 07, 2014 @08:54PM (#8424811)
> In rust there are two types of code. 'safe' and 'unsafe'. The above is only true if your entire code path contains only safe code.

I think this is actually a misconception. In every high-level language (Ruby, Python, etc.) there are APIs for interacting with low-level code. These APIs (C bindings and FFI) are unsafe, and are in the path of virtually all high-level code. It is the responsibility of somebody implementing a C binding for Ruby to ensure that the publicly exposed API is safe.

If you use `nokogiri` in Ruby and get a segfault, that is a bug in nokogiri. It is not an indication that Ruby is unsafe. Similarly, the "safe" dialect of Rust is Rust. Rust provides a low-level binding to unsafe code via the unsafe dialect that is philosophically equivalent to the FFI API or C bindings in Ruby. If somebody uses the low-level binding and exposes a Rust library that segfaults, there is a bug in that binding, full stop.

I think this is very important. If you use regular Rust code with the expectation that "anyone in your path may cause you to segfault", something has gone very, very awry in the ecosystem. This is not an expectation in other high-level languages when people write low-level libraries with unsafe code in them, and it should not be an expectation in Rust.

> Rust uses 'drop flags', which are extra bytes at the end of struct instances in memroy, to track objects and keep meta information about them to determine if they should be dropped

This is in the process of being eliminated (see https://github.com/rust-lang/rfcs/pull/320). I didn't mention it because it's runtime overhead that will not be present shortly, and certainly by the time 1.0 is reached later this year.

Re:Rust means never having to close a socket by shadowmint on Tuesday October 07, 2014 @09:00PM (#8424829)
> If you use `nokogiri` in Ruby and get a segfault, that is a bug in nokogiri. It is not an indication that Ruby is unsafe.

Rust has a strict definition of safety; not all rust code for fills that guarantee.

We're not talking fast and loose here. These are absolute definitions.

You can write safe code that crashes in rust. That absolutely violates the safety guarantee whatever the reason for it.

> This is in the process of being eliminated

No it's not.

    Keep dynamic drop semantics, by having each function maintain a (potentially empty) 
    set of auto-injected boolean flags for the drop obligations for the function that need 
    to be tracked dynamically (which we will call "dynamic drop obligations").
What's happening is some of the run time overhead is being reduced.
Re:Rust means never having to close a socket by dbaupp on Tuesday October 07, 2014 @10:35PM (#8425052)
> No it's not.

> What's happening is some of the run time overhead is being reduced.

I believe wycats was responding specifically to

> Rust uses 'drop flags', which are extra bytes at the end of struct instances in memroy, to track objects and keep meta information about them to determine if they should be dropped.

And these are being eliminated. That is the drop flag will be entirely separate to the struct instance, and will only be injected into stack frames where there is a conditional branch in which a value is moved out in one, but not the other, e.g.

  let x = some_value;
  if condition { drop(x); } else { /* do nothing */ }
then `x` will get a drop flag as a single bit (or byte) on the stack. If the code is changed to

  if condition { drop(x); } else { drop(x) }
there will be no drop flag, and no overhead.

Furthermore, placing the value on to the heap, e.g. `box some_value`, will not have an associated drop flag for `some_value`, that is, eliminating the byte that is currently stored with it.

Re:Rust means never having to close a socket by shadowmint on Tuesday October 07, 2014 @11:19PM (#8425177)
Whatever, there are still drop flags (on the stack instead, sure), and still dynamic runtime checks for drops.

The points I was making is that 'zero cost' memory management isn't 100% zero cost. The drop checker does incur some basic dynamic runtime costs.

To be 'zero cost' drop behaviour would be determined at compile time.

Re:Rust means never having to close a socket by dbaupp on Tuesday October 07, 2014 @11:28PM (#8425196)
The behaviour is determined at compile time, with runtime cost only introduced if it is absolutely needed (when one branch drops, and another doesn't), and, even then, it can be avoided, by manually forcing a drop of the value in every branch.
Re:Rust means never having to close a socket by wycats on Tuesday October 07, 2014 @09:30PM (#8424908)
> You can write safe code that crashes in rust. That absolutely violates the safety guarantee whatever the reason for it.

You can write Ruby code that calls into unsafe C code and SEGVs. This is a bug in the C code you called. It should have exposed a safe interface.

You can write Rust code that calls into clearly delineated unsafe code and SEGVs. This is a bug in the unsafe code you called. It should have exposed a safe interface.

Anything to the contrary is a misunderstanding of the purpose and role of unsafe code in Rust, and endangers the entire ecosystem, just as it would endanger the Ruby ecosystem if it were seen as normal for C extensions to SEGV the safe Ruby process.

Re:Rust means never having to close a socket by shadowmint on Tuesday October 07, 2014 @10:32PM (#8425043)
I'm not arguing any of these points.

I'm just saying there are these two misconceptions:

    - if you write safe rust code, your code can't segfault
This isn't true if your code path contains any unsafe code.

    - Rust code can't segfault
This isn't true at all.

Sure, if it does segfault its a bug in the c code / unsafe code.

You're absolutely correct.

...but bugs aren't deliberate. Obviously no one is going to deliberately expose a c api that has segfaults in it.

The misconception is that if you're using rust you're safe from the bugs that other programmers may have made... which is not true.

There's a trust chain involved; if you depend on a library you trust it's bug free and won't delete your harddisk and crash your program, but there's a difference between trusting a library is not malicious, and trusting a library is bug free.

If you have to trust the library is bug free, you're really no better off than using C++ are you?

...but people keep saying that if you use rust then there are certain types of safety that are guaranteed by the language: http://doc.rust-lang.org/reference.html#behavior-considered-..., regardless of programmer bugs.

It's just not true.

Once again, I'm not arguing that safety is good or bad or whatever. My assertion is very very specific:

Rust code written 100% safely can crash if it has a dependency that uses unsafe code.

The assertion that 100% safely written rust code with arbitrary dependencies can't crash is false.

Re:Rust means never having to close a socket by kibwen on Tuesday October 07, 2014 @10:49PM (#8425095)

  > If you have to trust the library is bug free, you're 
  > really no better off than using C++ are you?
This is a pretty far-out argument. For any programming language and environment, unless every dependency of your program including the compiler and runtime can be proven correct via formal verification, you will be susceptible to bugs. That doesn't imply that all code in the world is topologically as unsafe as C++. I can segfault Python if I try hard enough, and that's a bug in Python. Likewise I can segfault safe Rust code if I try hard enough, and that's a bug in Rust. I don't believe that anyone is really of the misconception that the mere use of Rust absolves you from the bugs of others, because that same person would have to believe that Rust somehow makes all bugs impossible, including bugs in C code that Rust calls into.

So let me give everyone a source to cite on Wikipedia:

"Rust does not keep C code from segfaulting." ~ kibwen

Re:Rust means never having to close a socket by dbaupp on Tuesday October 07, 2014 @10:40PM (#8425070)
> The misconception is that if you're using rust you're safe from the bugs that other programmers may have made... which is not true.

Of course it's not true, but the libraries you're using will (hopefully, for the most part) be written in Rust, which increases the level of trust.

> There's a trust chain involved; if you depend on a library you trust it's bug free and won't delete your harddisk and crash your program, but there's a difference between trusting a library is not malicious, and trusting a library is bug free.

> If you have to trust the library is bug free, you're really no better off than using C++ are you?

The libraries are written in Rust, so have all the benefits of that. It is nonsense to say that "libraries can have bugs so you might as well use C++".

Re:Rust means never having to close a socket by shadowmint on Tuesday October 07, 2014 @11:02PM (#8425131)
> It is nonsense to say that "libraries can have bugs so you might as well use C++".

My argument is that if you assert 'rust is 100% safe' then you must assert, all your dependencies are bug free.

If you can somehow magically assert 'all my code is bug free', that would be great... but if so, you wouldn't need the borrow checker at all would you?

(I'm certainly not endorsing using C++ :P I'm specifically talking to the assertion that rust code can be safe, as per the definition of safety in the guide, that certain operations are impossible; certainly, and absolutely, rust code is safer and more trust worthy (imo) than c/c++ code... but you've got to be reasonable about what you're arguing. 'rust is magical and can never crash' turns up in blog posts a lot)

Re:Rust means never having to close a socket by kibwen on Tuesday October 07, 2014 @11:21PM (#8425181)

  > 'rust is magical and can never crash' turns up in blog 
  > posts a lot
The next time that you see someone doing this, please correct them. Misinformation does none of us any favors.
Re:Rust means never having to close a socket by kbenson on Wednesday October 08, 2014 @12:19PM (#8427733)
I believe that's what started this entire thread.

...there are two things in this article which are, I'd say, 'common misconceptions' about rust.

Re:Rust means never having to close a socket by akavel on Wednesday October 08, 2014 @05:34AM (#8426055)
I don't understand -1s to the parent post; it's important to remember and be aware of limitations and dangerous areas in a language (or any other tool). And shadowmint tries to describe them in very civilized and calmly assertive way. I find his posts, as well as the counterarguments (like those from pcwalton and wycats), massively educative and informative. I absolutely can't understand why someone would like to see them fade out and disappear. I see the thread as exactly a kind of educated, professional argument that is so valuable on HN. And in such, being partially wrong or incorrect in some aspect is not something "bad" and "requiring censorship", but rather opportunity for straightening by others with a side effect of educating the larger community, isn't it?
Re:Rust means never having to close a socket by anonbanker on Wednesday October 08, 2014 @02:06PM (#8428405)
Upvoted grandparent based on this post.
Re:Rust means never having to close a socket by nwmcsween on Tuesday October 07, 2014 @10:06PM (#8424978)
'Unsafe' is a bad term, effect is much better and it is most definitely a rust issue. If the language doesn't have a way of dealing with effects it won't be able to handle them. Rust is honestly a more safe c++ and in my opinion not an alternative for c.
Re:Rust means never having to close a socket by wycats on Tuesday October 07, 2014 @10:16PM (#8424991)
I don't know what this means. I stand by my upstream comment.
Re:Rust means never having to close a socket by TheHydroImpulse on Wednesday October 08, 2014 @12:42PM (#8427860)
Algebraic effects system.