News for hackers. Stuff that matters.
Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by callumlocke on Friday October 30, 2015 @06:21AM (#10477014)
Can someone tell me what it means to have "no unsafe code"? (I'm a JavaScript developer with very little experience of languages like Rust and C++.)
Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by ekidd on Friday October 30, 2015 @06:41AM (#10477083)
In languages like C and C++, you have precise control over memory allocation and pointers. If you want to say, "My data structure should contain exactly these bytes, and be located here", then you can easily do so. This low-level control makes C and C++ very fast. Similarly, the language does very little "behind your back." It's why most kernels, interpreters, web browsers, compilers and AAA games are still written in a low-level language like C or C++.

But this control comes at a price: You need to be very careful. You need to manage memory manually, you need to avoid accidentally writing into memory you don't own, and so on. Overlooking small details results in security bugs.

Rust attempts to provide the same low-level control as C and C++, but it adds safeguards which keep you from shooting yourself in the foot. In "safe" mode, all of these safeguards are fully engaged. In "unsafe" mode, Rust allows you to do some sketchy things. The idea is to use as much safe code as you can, and only use a tiny amount of unsafe code if you need to do something Rust can't prove to be safe.

For example, there's a great series of blog posts about writing a toy OS kernel in Rust: http://blog.phil-opp.com/ The author uses some assembly to set things up, and a tiny amount of "unsafe" Rust to directly access VGA memory. But all the higher level code is safe, which rules out a whole class of bugs.

Personally, I've spent a lot time using both C++ and functional languages. And for me, Rust hits a sweet spot: I can get down to the metal when I want, and I can build nice abstractions. And I can write almost all my code in "safe" mode, and not worry about dumb pointer bugs. It's an interesting combination and it makes me want to hack on new things. :-)

Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by moron4hire on Friday October 30, 2015 @09:12AM (#10477549)
I wouldn't say the low level control is what makes C fast. Rather, it's the lack of higher level stuff like GC or array bounds checking.
Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by Retric on Friday October 30, 2015 @11:13AM (#10478290)
As far as I know there are not a lot of options, either you explicitly free memory and have low level control (malloc and free etc) or implicitly free memory and use GC.

So, I am not sure what you mean?

Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by zanny on Friday October 30, 2015 @11:35AM (#10478432)
Modern C++ programs should never write a new or delete anymore, except in very particular circumstance.

You should always be using make_unique and make_shared for heap allocations and you use shared whenever an object is being passed around and needs to track its owners, unique is for single-usage-instance use cases.

Those smart pointers have minimal overhead, deterministic runtime, and have no more overhead than manual implementations of the same mechanisms with raw pointers.

That is also how Rust does it - references are smart pointers, so you avoid having a GC.

Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by Retric on Friday October 30, 2015 @12:03PM (#10478608)
Being able to handle a large self-referential data structure automatically is never cheap. So, you need to either have complex GC, a manual approach, or leak memory.
Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by oldmanjay on Friday October 30, 2015 @12:28PM (#10478782)
Rust's lifetime tracking system is not implemented with smart pointers, although it does facilitate some very smart reference constructs indeed.
Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by steveklabnik on Friday October 30, 2015 @01:36PM (#10479235)
To elaborate a bit, references are "smart pointers" in the sense that they have extra semantics on top of regular pointers, but they also aren't like what most people refer to as smart pointers.

Rust's &T (references) are entirely checked at compile time, and generate the exact same code as a regular old C pointer would. No wrappers, extra allocations, reference counts, move/copy/whatever constructors (not that Rust even has those, but are important for C++ smart pointers).

Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by Jach on Friday October 30, 2015 @06:46AM (#10477096)
Rust has a particular meaning of 'unsafe': https://doc.rust-lang.org/book/unsafe.html There are just 3 things it allows you to do, but that's all it takes to get the necessary bits of potential-foot-shooting bits out of C/C++. One simple use case for dereferencing a raw pointer is if you're working with an embedded system. You know (through specs) that address 0x00b1 in memory corresponds to a physical button press on the development board, so you write a loop that continuously dereferences this address and does something based on whether the dereferenced value is 0 (pressed) or 1 (unpressed). As far as the source level is concerned, you're just dereferencing a magic address pointing at who knows what value over and over again and never changing that value either. "Unsafe" tells the compiler that you know what you're doing. (And sometimes even in C/C++ you'll need the 'volatile' keyword to keep their compilers from optimizing the dereference to only happen once out of the loop.)
Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by DasIch on Friday October 30, 2015 @06:30AM (#10477046)
Rust allows you define blocks of unsafe code in which certain checks aren't enforced by the compiler. In these sections you have pretty much as much freedom as you always do in C.
Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by e12e on Saturday October 31, 2015 @02:12AM (#10481975)
In addition to what's mentioned below (and you can read on the rust site) -- in a js frame of reference, consider == vs === and/or strict mode vs non-strict. Comparing Rust to C is a bit like comparing js code written in strict mode, using just === for comparison, versus non-strict, use only == - and perhaps always having a custom function that deals with arrays, using eval to marshal objects from strings stored in the array, rather than the object/type features that js provides (especially modern js).

It's not a perfect analogy, but perhaps gets a bit of the flavour difference.

Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by nbaksalyar on Friday October 30, 2015 @06:42AM (#10477086)
To understand unsafe code you need to understand what's safe code and pointers first.

Basically, safe code is automatically managed to get rid of memory leaks [0], dangling pointers [1], null pointer dereferencing [2], race conditions [3], and many other memory management bugs.

You don't encounter any of these in JavaScript because it's a managed language, meaning there's a garbage collector [4] that looks after all your memory allocations.

Contrary to the managed languages, C & C++ are unsafe and "native" (meaning that they're very close to the hardware level) - it's a wild land where you manage memory manually yourself. If you miss a single deallocation, you're left with a memory leak. If you deallocate a memory region twice, you have a double free situation [5]. So you have to be very cautious when writing code in C & C++. There are many techniques to make it easier (e.g. smart pointers [6]), but not all developers use them.

Rust takes the middle ground: it combines safety with unsafety by using a know-how affine type system with a borrow checker, which allows values/variables to have only one owner. This system has properties of both managed and unmanaged code. "Unsafe" code in the context of Rust means that you consciously turn off the borrow checker, leaving on your own and managing memory deallocations manually, as in C. That makes it possible to interface your Rust code with the native code, at the cost of making you prone to all errors and bugs that are listed above. So it's a good thing to minimize usage of unsafe code, to allow the Rust compiler to check your code for safety.

[0] https://en.wikipedia.org/wiki/Memory_leak

[1] https://en.wikipedia.org/wiki/Dangling_pointer

[2] https://en.wikipedia.org/wiki/Null_pointer#Dereferencing

[3] https://en.wikipedia.org/wiki/Race_condition

[4] https://en.wikipedia.org/wiki/Garbage_collection_(computer_s...

[5] http://stackoverflow.com/questions/21057393/what-does-double...

[6] https://en.wikipedia.org/wiki/Smart_pointer

Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by ant6n on Friday October 30, 2015 @09:58AM (#10477809)
How does it relate to writing C++11 without using raw pointers. I.e. using uniq_ptr (single owner), shared_ptr (multiple owners), references (not-an-owner). Are there benefits beyond that; and does it have the capabilities C++ has regarding making classes, operator overloading; templates?
Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by steveklabnik on Friday October 30, 2015 @10:50AM (#10478132)

  > How does it relate to writing C++11 without using raw pointers.
Rust's safety features are similar to those, but even stronger in a number of ways. There's two aspects to this: 1. more guarantees. You can write code with uniq_ptr that segfaults, but you cannot with Box<T>. 2. safety allows for patterns that would be dangerous in C++. For example, shared_ptr uses atomics for thread safety. Rust has two variants, Arc<T> with similar atomics, and Rc<T> without. If you're not using multiple threads, you can use Rc<T>, safe in knowing that Rust will give you a compile-time error if you introduce multithreading. In other words, there are ways that you need to be defensive in C++ that you do not in Rust.

  > does it have the capabilities C++ has regarding making classes, 
Rust does not really have OOP, exactly, we have structs and traits (concepts). One major difference is that we don't currently have inheritance, though proposals are being worked on. But since we already have the concept equivalent, we have extra power there. Tradeoffs.

  > operator overloading
Rust lets you overload certain pre-defined operators, but not create new ones.

  > templates?
AST based, hygenic macros, instead.
Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by cristicbz on Friday October 30, 2015 @11:00AM (#10478205)
> AST based, hygenic macros, instead.

Well and generics & traits. 99% of the places where C++ programmers use templates, they would use generics in Rust. `template std::vector<T>` <-> `Vec<T>` etc.

It's only the niche metaprogramming ability of C++'s templates that would be replaced with macros.

Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by steveklabnik on Friday October 30, 2015 @11:02AM (#10478219)
Yes, thanks, that's a more complete answer.
Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by lambda on Friday October 30, 2015 @11:00AM (#10478200)
Rust's model is similar to a cleaned-up and guaranteed safe use of modern C++ smart pointers and references.

A few of the improvements that Rust offers are compiler enforced move semantics by default, so assigning, returning, or passing a Box<T> (similar to unique_ptr<T> in C++) will move the ownership to the new location and statically check that you never try to access the old location. In C++, you have to explicitly call std::move(), and checks to make sure you don't reference the old one happen at runtime.

Rust also does safer checking of use of references. In C++, it's possible to return a reference to a stack variable which is no longer valid. In Rust, the lifetimes of references are tracked statically, so you can never have a dangling reference.

So, while the RAII pattern, use of smart pointers, and use of references, will feel fairly familiar to someone familiar with modern C++, they are actually generally more convenient and more safe in Rust.

Rust does not have classes. You define plain old data structures, and use traits to provide polymorphic behavior. Traits are somewhat like interfaces which can contain default implementations of methods. Traits can be used to constrain types. Traits can be used to provide either static (compile time) dispatch, or runtime dispatch, depending on whether the object is monomorphized at compile time or accessed through a fat pointer that includes a vtable to dispatch at runtime.

Rust provides generics which fill a similar niche to templates in C++. In some ways, they are more powerful as arguments can be constrained by traits; in some ways they are less powerful than C++ templates.

For an introduction to traits in Rust, take a look at the book: https://doc.rust-lang.org/book/traits.html

Rust doesn't provide operator overloading directly, but use of operators is equivalent to just calling a method defined on a particular trait in Rust. So, "a + b" is will work for any types that implement the Add trait (https://doc.rust-lang.org/std/ops/trait.Add.html), and is equivalent to calling a.add(b).

Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by ant6n on Friday October 30, 2015 @08:52PM (#10481388)
One thing I'd miss from C++ are the alternative operator representations for && and ||, and and or.
Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by cristicbz on Friday October 30, 2015 @10:58AM (#10478185)
You can still trigger plenty of undefined behaviour in C++11 with no raw pointers. Just one example--iterator invalidation:

    auto v = std::vector<int> {1, 2, 3}
    for (auto x : v) {
        if (x > 0) {
            v.push_back(-x);
        }
    }
The above may segfault, do what you expect it to, run forever etc. Unchecked indexing, branching on uninitialized data etc.

Rust frees you from _all_ of these things and offers a bunch of further advantages, most notably: data race freedom. You can actually share data between threads and keep your sanity.

Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by kibwen on Friday October 30, 2015 @02:59PM (#10479802)
Can someone explain why this was downvoted?
Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by Manishearth on Friday October 30, 2015 @04:23PM (#10480326)
> templates

The vast majority of C++ template usage is acheived by generics, and in a much more type safe and debugable manner.

Most of the remaining things can be achieved by Rust's hygenic macros (which are not like C++ macros, they're more like C++ templates in how they can be used and the guarantees they provide).

Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by easytiger on Friday October 30, 2015 @06:58AM (#10477133)
> . So you have to be very cautious when writing code in C & C++. There are many techniques to make it easier (e.g. smart pointers [6]), but not all developers use them.

Not everyone has security as a requirement. Some people jsut want to write something that is both fast, predicable. Most banks don't make any security demands of internal applications because they are not accessible to the internet. At which point safety is just a way to slow you do.

Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by smtddr on Friday October 30, 2015 @07:15AM (#10477171)
>>Most banks don't make any security demands of internal applications because they are not accessible to the internet. At which point safety is just a way to slow you do.

This is a bad way of doing things. That means as soon as someone finds a way to get inside the network, the bank is wrecked. You need to look at security from the point of assuming the attacker somehow got inside because it'll happen one day.

http://digg.com/2015/ashley-madison-hack

""" MOTHERBOARD: How did you hack Avid Life Media? Was it hard?

The Impact Team: We worked hard to make fully undetectable attack, then got in and found nothing to bypass.

MOTHERBOARD: What was their security like?

The Impact Team: Bad. Nobody was watching. No security. Only thing was segmented network. You could use Pass1234 from the internet to VPN to root on all servers. """

Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by lucozade on Friday October 30, 2015 @09:36AM (#10477663)
Let me know which banks you've worked at with this attitude. I just want to check I have no accounts with them.

This most definitely is not the view taken at the financial companies I've worked in/with including a number of banks.

They take internal security very seriously. There's the obvious threat of some form of break-in (no-one I know seriously believes that an unbreachable connected system is buildable). But there's also the issues of insiders taking stuff, Chinese walls and regulatory restrictions on access. It simply doesn't make economic sense to take security lightly any more.

Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by humanrebar on Friday October 30, 2015 @07:10AM (#10477152)
On the other hand, banks want numbers that actually add up properly, at least in the code that deals with money. If you use undefined behavior in C or C++, the languages do not guarantee that your bank account balances won't all be rounded down to the nearest prime number on Thursdays in odd numbered months in leap years.
Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by jerven on Friday October 30, 2015 @08:19AM (#10477349)
Safe, also means that the application does not crash in funny ways with segmentation faults. Which is something that banks like a lot.
Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by cjcole on Friday October 30, 2015 @08:50AM (#10477458)
"Some people jsut want to write something that is both fast, predicable."

And debuggable? Not crashing in random places due to stack or heap corruption helps there.

Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by aswanson on Friday October 30, 2015 @10:23AM (#10477957)
"Why do you rob banks?"

"Because that's where all the exploitable attack vectors in unsafe code are."

Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by kjdal2001 on Friday October 30, 2015 @10:58AM (#10478179)
Others have commented that security type safety can be important even in applications that are not externally facing, but thats even sort of besides the point. When people talk about 'safety' in Rust, often times they are talking about the predictability that you mention. Highly threaded code can seem to run perfectly for some time before obscure bugs pop up. Rust's abstractions around thread and memory safety can help in those situations.
Re:Rust-Doom: A Doom renderer in Rust, with no unsafe code by raesene4 on Friday October 30, 2015 @11:53AM (#10478550)
I'm going to have to disagree with your point about banking applications based on my experience (~15 years) in banking IT and security.

Whilst obviously Internet facing apps are treated with more caution, it's definitely not true in my experience to state that there are no security demands on internal applications. I would expect that most/all banks these days realise that there's no such thing as a fully trusted network...