News for hackers. Stuff that matters.
Re:Rust on AVR: Safer microcontrollers almost here by foota on Saturday February 11, 2017 @01:31AM (#13621399)
You don't get advantages over perfectly written C, but that doesn't exist. Over mortal written C, you gain safety against data races, use after free, and a couple other classes of errors. And without needing manual memory management, and with high level ergonomics.
[dead]
Re:Rust on AVR: Safer microcontrollers almost here by staticassertion on Saturday February 11, 2017 @10:17AM (#13622964)
Not really. We can simply expand it to "All code has bugs", and then we're simply at Rice's theorem. In C, the class of bugs that can not be proven to not exist are also quite dangerous - remote code execution, as an exemplar consequence. In rust, you're limited to a program that can crash, or that has other semantic faults.

So it's clearly not wrong, and is actually a totally well established idea well outside of the context of rust or C.

Re:Rust on AVR: Safer microcontrollers almost here by Thrillington on Sunday February 12, 2017 @12:36AM (#13626954)
An 8-bit AVR executes instructions from on board flash memory that requires twiddling hardware lines to erase and rewrite.

Remote code execution isn't a concern.

Re:Rust on AVR: Safer microcontrollers almost here by boznz on Monday February 13, 2017 @06:53PM (#13640016)
AVR ATMega is self programmable ie a bootloader can re-flash it if its configured to allow but you would need some code already resident on the flash to allow, it couldnt be done without this code.
Re:Rust on AVR: Safer microcontrollers almost here by pjmlp on Sunday February 12, 2017 @06:33AM (#13627897)
No, but memory corruption related to pointers misuse or unsigned arithmetic is.
Re:Rust on AVR: Safer microcontrollers almost here by dbaupp on Sunday February 12, 2017 @10:38PM (#13632960)
(I think you mean signed arithmetic here and in your almost-sibling comment.)
Re:Rust on AVR: Safer microcontrollers almost here by pjmlp on Monday February 13, 2017 @01:45AM (#13633552)
Probably, I always mix them up.
Re:Rust on AVR: Safer microcontrollers almost here by milesrout on Sunday February 12, 2017 @01:58AM (#13627179)
You can very easily have all the sorts of memory unsafety you have in C, in Rust.
Re:Rust on AVR: Safer microcontrollers almost here by pjmlp on Sunday February 12, 2017 @06:31AM (#13627893)
Only in sections marked as unsafe.

In C every single line of code that manipulates pointers, does string operations, uses arrays or unsigned arithmetic is a possible cause of memory corruption.

Re:Rust on AVR: Safer microcontrollers almost here by jononor on Saturday February 11, 2017 @07:24AM (#13622308)
Most code is less-than-perfect, independently of language. But the consequences of this depends on the language.
Re:Rust on AVR: Safer microcontrollers almost here by unrealhoang on Saturday February 11, 2017 @05:51AM (#13622092)
IMHO There's a big gap between perfectly written and poorly written
Re:Rust on AVR: Safer microcontrollers almost here by vertex-four on Saturday February 11, 2017 @05:24AM (#13622039)
Not poorly written, just it's very easy to forget things, and even very well-written, readable, documented C code usually has memory-related bugs, as long as it's written by fallible humans. Nearly every networked C program in existence has had a buffer overflow CVE at some point - that's simply not possible in Rust.
Re:Rust on AVR: Safer microcontrollers almost here by Someone on Saturday February 11, 2017 @09:37AM (#13622779)
Corrrections welcome, but I think there is one buffer that you can overflow in every Rust program: the stack.

If you have a MMU, the runtime will/should protect against that by mapping a page as 'trap on write' and panicking/rebooting/signaling if you do, but if you don't, I don't think rust has any functionality to prevent it.

Or can you declare that your stack is x bytes, and have the compiler verify that no call chain will need more? (That typically would prevent the use of recursion, but that's not too bad)

Re:Rust on AVR: Safer microcontrollers almost here by steveklabnik on Saturday February 11, 2017 @10:15AM (#13622947)
The answer is basically "stack probes", yes.
Re:Rust on AVR: Safer microcontrollers almost here by Someone on Saturday February 11, 2017 @02:45PM (#13624355)
I find very few relevant hits on that on Google (stack probes apparently also are hardware to measure various quantities such as moisture in stacks of materials), and none that explain to me what they are (you can enable them in gcc and Microsoft's compilers, and rust should use them instead of stack overflow checks, but that's about it), but from what I guesstimate, they are runtime instrumentation, not anything that a rust compiler could do to guarantee that a compiled and linked program never overflows the stack.

Can you tell me more about them?

Re:Rust on AVR: Safer microcontrollers almost here by steveklabnik on Saturday February 11, 2017 @03:44PM (#13624699)
They're runtime checks, but they're inserted by the compiler at compile time. So yeah, if you link in code that wasn't compiled with them, then that code isn't protected.

You're right that a summary seems hard to find; http://www.delorie.com/gnu/docs/gcc/gccint_124.html has some decent stuff in it, but given that it's random GCC docs, the details may not be 100% accurate today, I dunno, I'm on a flight layover right now, so that's all I've got at the moment :)

Re:Rust on AVR: Safer microcontrollers almost here by pcwalton on Saturday February 11, 2017 @03:27PM (#13624616)
They're dynamic checks.

Static checks for stack overflows are infeasible.

Re:Rust on AVR: Safer microcontrollers almost here by Someone on Saturday February 11, 2017 @04:37PM (#13624964)
They are not infeasible, if you are willing to limit what your language can do.

Doing so doesn’t remove all useful programs, as the first Fortran compilers showed (they used a fixed addresses for all variables, be they local or global (that they did not support recursion is not that surprising, given that many CPUs of the time didn’t have a return stack))

That’s why I asked whether Rust supported such a thing. It would be very helpful, for example, if your build system would tell you that that difficult to test error handler would, if called, overflow the stack before you spent time and, possibly, lots of money, on trying to run it.

Re:Rust on AVR: Safer microcontrollers almost here by pcwalton on Saturday February 11, 2017 @03:26PM (#13624613)
> That typically would prevent the use of recursion, but that's not too bad

It also prevents the use of indirect calls/virtual functions, because they can hide recursion. That's Bad.

Re:Rust on AVR: Safer microcontrollers almost here by sidlls on Saturday February 11, 2017 @10:15AM (#13622949)
It's fairly trivial to overflow a buffer in Rust. I don't consider having to write "unsafe { /* overflow buffer code goes here */}" non-trivial.
Re:Rust on AVR: Safer microcontrollers almost here by kbenson on Sunday February 12, 2017 @07:04PM (#13632018)
The difference is that when you are forced to declare where your program does not follow some safe semantics, while it's still trivial to write an overflow, it's also trivial (or at least not nearly as complex as some alternatives) to find and fix, compared to a language where the entire code base is a possible source for those problems.

For comparison, I would say C and C++ (but to a slightly lesser degree) are also trivial to write buffer overflows in, it is definitely non-trivial in many cases to find and fix those problems, as the surface area is so large.

To illustrate this, it's trivial to write a buffer overflow in almost all languages that provide any sort of raw memory access or hardware instructions (ASM), even if through a module. It's trivial for me to create a buffer overflow in Python or Perl my including a module that allows direct assembly code access. The difference when using those languages is that when I run into a buffer overflow problem, I can be reasonably sure it's in a place where I bypassed the assurances provided by those languages, or in some included module that did the same (including just using a shared library). When tracking down a problem like that, I'll focus my attention on those spots, which makes identifying and hopefully fixing those problems easier.

Rust's "unsafe" is really an in-language way to provide a similar set of levels of assurance about how likely the code is to have problems of specific types, with assurances provides by the language and compiler. This is a step above what C and C++[1] provide, and thus welcome.

1: C++ provides some similar assurances based on types, but by nature of how "safe" code becomes "unsafe" when used in conjunction with unsafe portions, is much harder to assess at a block level, since you can't even be sure a line is safe until you've verified all the types used within it.

Re:Rust on AVR: Safer microcontrollers almost here by jononor on Saturday February 11, 2017 @12:29PM (#13623590)
For embedded code, I'd recommend that all application code should be compiled with the `--deny unsafe-code` option. Then the compiler will stop you. A separate module for the I/O code providing is the only place where one should use `unsafe`. Extra care must be taken here, in code review and in testing.
Re:Rust on AVR: Safer microcontrollers almost here by milesrout on Sunday February 12, 2017 @02:01AM (#13627190)
Do you have any idea how impossible it is to do anything useful without unsafe in Rust on a microcontroller?
Re:Rust on AVR: Safer microcontrollers almost here by jononor on Sunday February 12, 2017 @07:29AM (#13628050)
About as hard as on desktop? I mean, many of the core datastructures commonly used there are implemented using unsafe. This does not mean that the application code must or should be riddled with unsafe code blocks... For a very simple example see https://github.com/hackndev/zinc/blob/master/examples/blink_...
Re:Rust on AVR: Safer microcontrollers almost here by pjmlp on Sunday February 12, 2017 @06:38AM (#13627909)
If Ada, Pascal and Basic can, so can Rust.

Every case where this is not true is a bug.

Re:Rust on AVR: Safer microcontrollers almost here by vertex-four on Sunday February 12, 2017 @09:58AM (#13628597)
You can also get a buffer overflow in Python and Java by abusing their equivalents of "unsafe".
Re:Rust on AVR: Safer microcontrollers almost here by sidlls on Sunday February 12, 2017 @01:04PM (#13629564)
"unsafe" in Rust isn't abuse. It's explicit syntax with compile time effects specifically for the purpose of facilitating unsafe constructs, like direct memory buffer manipulation, required for low level programming.

I've never tried the Python or Java equivalents and frankly don't know enough about them to even know whether your comment is accurate or not.

Re:Rust on AVR: Safer microcontrollers almost here by vertex-four on Sunday February 12, 2017 @06:50PM (#13631948)
Java has `sun.misc.Unsafe`, and Python has the FFI which is remarkably easy to misuse. The point is that you don't use these unless you need to, and you spend special effort when you do - same as `unsafe` in Rust. The rest of your code runs perfectly safely, and the vast majority of code has no need to use `unsafe`. Code which uses `unsafe` and isn't obviously either directly an FFI wrapper or a straightforward implementation of a generic data structure is generally considered bad form.
Re:Rust on AVR: Safer microcontrollers almost here by foota on Sunday February 12, 2017 @03:51PM (#13630688)
True that unsafe isn't considered too wrong in rust, but auditing the small amount of unsafe code is much easier than all the code.
Re:Rust on AVR: Safer microcontrollers almost here by jononor on Saturday February 11, 2017 @11:09AM (#13623172)
Is getting that past the person who is reviewing your code also trivial?
Re:Rust on AVR: Safer microcontrollers almost here by sidlls on Sunday February 12, 2017 @01:08PM (#13629587)
Possibly. Depends on the quality of the reviewer and the complexity of the code (as a whole). On average Rust programmers aren't necessarily better than programmers using any other language. If history is a guide then if Rust takes off there will be plenty of terrible, buggy, unsafe and very thoroughly reviewed Rust code out there used in popular, widely used applications or services.

I find the notion that "explicit unsafe makes bugs less likely" to be suspect. It seems reasonable, almost tautological at first glance. But theory and practice are different animals.

Re:Rust on AVR: Safer microcontrollers almost here by milesrout on Sunday February 12, 2017 @02:00AM (#13627187)
It certainly is possible in Rust. Rust is not memory safe.
Re:Rust on AVR: Safer microcontrollers almost here by pjmlp on Sunday February 12, 2017 @06:35AM (#13627906)
As it is in any language that has FFI to Assembly.

But that is a big difference in the amount of unsafety per line of code.