News for hackers. Stuff that matters.
Re:Rust via its Core Values by bogomipz on Sunday April 03, 2016 @04:35PM (#11417538)
Could someone explain this - "Ruby also protects you from segmentation fault errors. But to do so it uses a garbage collector."?

How does GC protect you from a segfault?

Surely trying that doesn't help you when you attempt to access the 5th element of an array with only two elements. Maybe its just confusing that the author used that as an example. I'm guessing aside form a dangling pointer GC doesn't offer much protection form a segfault?

Re:Rust via its Core Values by catnaroek on Sunday April 03, 2016 @04:49PM (#11417609)
Of course, garbage collection per se doesn't protect you from segfaults. For example, there exist garbage collectors for C. They reclaim unused memory, but you can still cause segfaults if you want to (or are careless).

Automatic memory management (whether done at runtime with a garbage collector, or at compile time as in Rust) frees you from logical memory errors by making them inexpressible. But automatic memory management doesn't guarantee that segfaults won't happen - it only guarantees that they won't matter to the programmer. For example, a garbage collector could deliberately trigger segfaults (by `mprotect()`ing all managed memory) to forcibly pause the mutator during a garbage collection cycle. Every managed thread must install a `SIGSEGV` handler that waits until the collection process is done.

Re:Rust via its Core Values by bogomipz on Monday April 04, 2016 @06:46AM (#11420781)
"For example, a garbage collector could deliberately trigger segfaults (by `mprotect()`ing all managed memory) to forcibly pause the mutator during a garbage collection cycle. Every managed thread must install a `SIGSEGV` handler that waits until the collection process is done."

Interesting, is this actually a common implementation pattern for languages that have a GC built into their run time?

Re:Rust via its Core Values by samth on Tuesday April 05, 2016 @10:16AM (#11430523)
I don't think the exact thing you describe is common, but plenty of garbage collectors use memory protection to implement the write barrier. This is particularly useful when integrating with arbitrary code you don't control, since it will also be affected by memory protection.
Re:Rust via its Core Values by catnaroek on Monday April 04, 2016 @01:35PM (#11423722)
No idea. I haven't read the source code of any serious, production GC. I've only written toy ones.
Re:Rust via its Core Values by barrkel on Sunday April 03, 2016 @06:20PM (#11418054)
A segfault implies a pointer to an unmapped page in memory. An array out of bounds error isn't the same class of error. The former implies that what pointers can point at isn't constrained; instead of pointing at unmapped page, it could point at the inside of some sensitive data structure (think: web response buffer pointing at database credentials). The latter implies that indirections are checked; this is a very different guarantee.
Re:Rust via its Core Values by bogomipz on Monday April 04, 2016 @06:24AM (#11420696)
Thanks, could you explain the "indirections" in this context? I haven't heard this before.
Re:Rust via its Core Values by barrkel on Monday April 04, 2016 @02:09PM (#11423977)
Array accesses are implicit indirections. The base, index and element size are used to compute the address of a location which is then indirected to store or load. Without range checking, array accesses can be used to read or write arbitrary locations in memory, just like pointers.
Re:Rust via its Core Values by bogomipz on Monday April 04, 2016 @04:10PM (#11425117)
Ah ok right, that makes perfect sense. Thanks.
Re:Rust via its Core Values by rntz on Monday April 04, 2016 @03:55AM (#11420119)
> I'm guessing aside form a dangling pointer GC doesn't offer much protection form a segfault?

Correct. By using GC and banning free() you avoid use-after-free bugs, but not out-of-bounds accesses.

Out of bounds accesses have to be forbidden in some other fashion; most languages do bounds-checking and throw an exception rather than trying to access the memory and getting a segfault (or, worse, reading some random other object's memory).

Re:Rust via its Core Values by bogomipz on Monday April 04, 2016 @06:23AM (#11420691)
Thanks