In article <r7rfb2$bsu$
1...@dont-email.me>,
jacobnavia <
ja...@jacob.remcomp.fr> wrote:
>Le 22/04/2020 à 22:52, Dan Cross a écrit :
>> "Unsafe code is unsafe; film at 11."
>>
>> There's not much to see in this paper. It's
>> well-known that Rust gives you an unsafe superset
>> of the language that gives you access to dangerous
>> features; use of that superset is even preceded
>> by the `unsafe` keyword, so the notion of unsafety
>> is built into the language at the syntactic level.
>
>The problem with Rust is then, that to do anything like bluetooth or low
>level i/o you have to use "unsafe". Then, you are in your own and all
>the problems of C and C++ appear, eliminating any advantage of Rust over
>those languages, the point I wanted to make with the article where I
>started this thread.
That's simply not true, or rather, not true to the extent
the paper's authors make it out to be. I've written three
kernels in Rust now, including doing lots of "low-level i/o"
and you'd be really surprised how far you can push safe code.
>> "Unsafe" in Rust means two things: first, it allows
>> the compiler to relax its automated checking of
>> some of Rust's guarantees, under the assumption
>> that it's up to the programmer to enforce those
>> guarantees. Second, it's a note to future
>> programmers that, "there be dragons here."
>
>No checks, yes. I am used to it since I program in C. My point is,
>what's the point of learning Rust since I have to do the same thing as
>in C...
Seat belts don't save all lives, so what's the point of
wearing a seat belt? A blade guard on a table saw can't
stop you from _ever_ cutting off your fingers, so why
bother?
Rust can't prevent _all_ bugs; but that's axiomatic. But
like all sorts of safety apparatus, it can cut out a lot
of them.
You may have to do some, or even many things the same way
you do them in C. But many bugs that would be runtime
failures in C will be compile-time errors in Rust.
>> In all cases pointed out by this paper, the code
>> in question violated Rust's rules through misuse
>> of `unsafe`. In other words, the programmers
>> messed up.
>
>Of course the programmers messed up. Just like in C I mess up sometimes.
Obviously it won't prevent you from _ever_ messing up.
It just dramatically decreases the possible error surface.
>> While certainly problems, it's not clear how
>> this is the language's fault. If the programmers
>> could have restricted themselves to the safe
>> subset of the language, it's not clear that any
>> of these bugs would have been arisen.
>
>The point being here, is that they CAN'T RESTRICT THEMLSEVES TO THAT
>SAFE PART because they have to do things that are necessary and not
>possible within the safe part!
I find this argument specious at best. I've written a
lot of Rust code in the last two years and more often
than not, _most_ programmers _can_ restrict themselves
to that safe subset. Where they cannot, the interface
can be tightly controlled. I've written a lot _more_
C code over three decades. The things you can do in
Rust with respect to safetly are things you simply
cannot do in C or C++.
>> Can Rust
>> do better _in the unsafe superset_? Probably,
>> but I don't think anyone disputes that. Rust
>> helps, but doesn't absolve programmers from
>> knowing what they are doing.
>>
>
>I.E. the added value of Rust is zero.
I'm sorry, but that's ridiculous. Have you ever actually
written anything in Rust?
>> FWIW, I do see lots of beginning Rust programmers
>> misusing `unsafe`; in particular, it's often
>> over-used by those getting working with the
>> language for the first time.
>
>In the article they point out to bugs in the standard library of Rust,
>i.e. programmers that should be fairly advanced...
Certainly there are bugs. Now compare the number
of bugs in the Rust standard library to the number
of bugs in, e.g., glibc.
>I do not blame them. Programming is hard. I just wanted to point out
>that there is NO magic bullet, no language that will be of help, unless
>the language takes a performance hit by checking everything.
I don't think anyone suggest that Rust is a silver bullet.
Also, I think you probably haven't written much if anything
in Rust. Much of the burden of the types of checks you
describe are shifted to compile time in the safe subset.
The properties of the language allow the compiler to make
assertions that just cannot be made in, say, a C program.
In many ways, Rust is more restrictive, but that's a
tradeoff: one trades permissiveness for safety without
a runtime penalty.
>I wrote a container library for C. Should I check everything? Should all
>functions check for NULL values or wrong parameters? That slows down the
>library...
>
>And I decided against performance. We have now machines that run at
>incredible speeds with vast amounts of memory and ressources and we
>still hesitate to make that NULL check?
If I can create a situation where I _know_ that, say, a
pointer _cannot_ be NULL, then why would i check it for
NULL?
static const char *foo = "hi";
if (foo != NULL) {
if (strcmp(foo, "hi") == 0)
printf("Hi!\n");
Here, the check for `NULL` is obviously superflous; the
programmer could omit it without sacrificing correctness
or safety.
In Rust, the semantics of the language are such that the
compiler can often make this determination _at compile
time_ (note that even a C compiler can do this for this
particular, contribed example).
>This obsession with performance is what I question in Rust. They should
>never allow "unsafe". The whole point of a new language disappears then.
`unsafe` exists for situations where you need `unsafe`: that
sounds tautological, but the safe subset of the language is
_sometiems_ too restrictive. The seatbelt in my car prevents
me from flying through the windshield should I get into a
crash. That's good, but it also precents me from picking
something up from the floor at the other seat if I'm stopped.
I don't care to do it, but if I'm stopped at e.g. a traffic
light I have occasionally taken the car out of gear,
unbuckled the seatbelt, reached over, and retrieved
something that fell, then re-buckled my seatbelt and put
the car back into gear. I don't think anyone would argue
that that renders seatbelts "useless". That's the analogous
situation with Rust's `unsafe` superset.
- Dan C.