News for hackers. Stuff that matters.
Re:Getting Past C by sidlls on Tuesday January 03, 2017 @05:25PM (#13313757)
How do you suppose runtime bounds checks are done in Rust? They certainly also incur a performance penalty in not-trivial cases.

Also, "safe by grep audit" means "safe according to a human." The argument of course is that it lowers the surface area of what a human must be trusted to verify. I'm still not convinced by that argument, because human error is a thing. And for actual systems programming, "very rare" may not be true.

Re:Getting Past C by staticassertion on Tuesday January 03, 2017 @05:40PM (#13313878)
> How do you suppose runtime bounds checks are done in Rust? They certainly also incur a performance penalty in not-trivial cases.

Certainly. I didn't intend to imply otherwise.

> Also, "safe by grep audit" means "safe according to a human."

Again, totally correct here.

> The argument of course is that it lowers the surface area of what a human must be trusted to verify. I'm still not convinced by that argument, because human error is a thing. And for actual systems programming, "very rare" may not be true.

Well, given a codebase where both safe and unsafe code exists, the amount of unsafe code is strictly less than the amount of both safe and unsafe code. So it does reduce the amount of code needed to audit, even in a very atypical case where a ton of the code is unsafe.

It's true that a project may use egregious amounts of unsafe. That would be unfortunate. Rust is still safer than C in that case, since it just defines more behavior (like arithmetic overflow), but I certainly wouldn't pretend that the rust code should be trusted.

When writing rust one should certainly strive to write less unsafe code, and to always document the invariants required for unsafe code to be safe.

Rust is not 100% safe 100% of the time, I'm only arguing that safe defaults are critical, and that grep auditing is a powerful tool.

Re:Getting Past C by alexkehayias on Wednesday January 04, 2017 @12:24AM (#13315877)
I wrote a little tool to help check Rust crates on GitHub. It's been really interesting seeing how different libs use unsafety. https://github.com/alexkehayias/harbor
Re:Getting Past C by EugeneOZ on Tuesday January 03, 2017 @06:14PM (#13314187)
Rust is safe in terms of memory usage and race conditions, nobody claims that Rust compiler will catch all 100% of bugs human can invent.
Re:Getting Past C by sidlls on Tuesday January 03, 2017 @06:21PM (#13314238)
Rust is safe by default in terms of memory usage. It is not, however, strictly memory safe. It is trivial to overflow a buffer in Rust, for example. I haven't discovered a trivial way to hide it, though.
Re:Getting Past C by steveklabnik on Tuesday January 03, 2017 @06:23PM (#13314245)
If Rust is not memory safe in safe code, then you've found a bug. Please report it to https://www.rust-lang.org/security.html
Re:Getting Past C by derefr on Tuesday January 03, 2017 @06:44PM (#13314390)
If you're relying on any random third-party Rust crates you haven't audited yourself, don't you lose the safety guarantee? A given crate might turn out to have implemented operations on some data-structure using unsafe blocks, and then to have failed to mark its own API functions as unsafe in turn (like the Rust stdlib does, but without the "extensive manual auditing" that the stdlib gets).

AFAIK, cargo doesn't have any feature to point out when a crate contains unsafe code—so you pretty much need to grep the source of every crate you consume for "unsafe".

Re:Getting Past C by Animats on Tuesday January 03, 2017 @07:33PM (#13314647)
There's a lot more unsafe code in Rust crates than there should be. That's a fixable problem. Some stuff from the early days predates the optimizer getting smart enough that unsafe code isn't needed. I wrote on this a few days ago in a Rust topic.
Re:Getting Past C by Manishearth on Tuesday January 03, 2017 @09:02PM (#13315082)
While I now mostly agree with you that there is more unsafe code than there should be, I still maintain that the frequency of unsafe in a deptree is usually still small enough to be practically auditable, ignoring FFI. It could/should be much less, but it's not too bad. I've done such audits a few times and it's not been too hard and taken very little time.

Auditing FFI is a whole other challenge, however :(

Re:Getting Past C by pjmlp on Wednesday January 04, 2017 @03:04AM (#13316532)
> I still maintain that the frequency of unsafe in a deptree is usually still small enough to be practically auditable

Not in binary libraries, hence why it is important to have a culture to only use unsafe if it really must be used.

Re:Getting Past C by Manishearth on Wednesday January 04, 2017 @03:24PM (#13321735)
Well, yeah, but you don't really download Rust binary libraries yet :)

You do have C libraries which you access through FFI. This is inevitably unsafe. We should be auditing more there. Though IMO it's still manageable, for most crates.

Re:Getting Past C by staticassertion on Tuesday January 03, 2017 @09:51PM (#13315289)
Hmmmm. Note to self - actually try to audit a reasonably sized project's unsafe code to see how reasonable it is.
Re:Getting Past C by steveklabnik on Tuesday January 03, 2017 @06:49PM (#13314419)
That's why I said "safe code," I mean, not using any unsafe.

The issue you're talking about is related, but different.

Re:Getting Past C by derefr on Tuesday January 03, 2017 @07:07PM (#13314507)
Right, I was just trying to put a finer point on your use of "using any unsafe" here. It sounds like you mean "using" in the lexical sense (writing the token "unsafe" in your code), but you mean it in the dynamic sense (having an unsafe block in your control flow graph.)
Re:Getting Past C by steveklabnik on Tuesday January 03, 2017 @08:39PM (#13314991)
That's a good distinction to draw, thanks.
Re:Getting Past C by sidlls on Tuesday January 03, 2017 @06:37PM (#13314352)
Let me clarify: in Rust it is trivial to use memory unsafely. It is not, so far as I have found, trivial to hide that fact because it is required to use "unsafe" syntax decoration" to do so.
Re:Getting Past C by Manishearth on Tuesday January 03, 2017 @06:45PM (#13314396)
The issue here is that that definition of "safe" language basically excludes all practical languages, including languages like Python, because FFI is possible.

In general when talking about safety in a language it's about the level of explicitness required to trigger unsafety.

I like the distinction made in the nomicon (https://doc.rust-lang.org/stable/nomicon/meet-safe-and-unsaf...) -- Rust comprises of two distinct languages. You have everyday Rust, which is completely memory safe, and "unsafe Rust", which looks similar to everyday Rust but is not safe. `unsafe {}` blocks are your FFI between the two. Looking at unsafe blocks as FFI is IMO a very useful mental model especially for understanding the changes to invariants involved.

Re:Getting Past C by staticassertion on Tuesday January 03, 2017 @06:35PM (#13314338)
In safe rust it is definitely not trivial to overflow a buffer in a way that violates memory safety.
Re:Getting Past C by rtpg on Tuesday January 03, 2017 @09:22PM (#13315171)
"Actual systems programming" mostly does not involve unsafe code.

For example, most OS code is _not_ interrupt hooks or malloc but the rest of the OS. Most of Postgres is not reading data quickly, but higher-level abstractions.

Large-scale systems programming will always be mostly higher-level abstractions, because that's the only way to write large programs. Name any "systems programming" OSS project, choose a random C file, and you'll see that most code does not require pointer arithmetic except because that's how you do things in C.

You don't bit-twiddle for 200k lines of code, so being able to limit dangerous stuff like that to the 5k lines that actually need it makes work an order of magnitude easier.

Re:Getting Past C by anarazel on Wednesday January 04, 2017 @06:29AM (#13317334)
> "Actual systems programming" mostly does not involve unsafe code.

True. Or slightly rephrased: Most of the unsafe code is centralized to some core pieces.

From the POV of a PG dev: The big problem using something like rust for something like PostgreSQL is its it's portability, stability and uncertainty about where things are going. We do five years of back-branch releases (and for many that's not even enough!). Language and tooling around the new crop of languages simply aren't mature enough for that yet.