Skip to content

Better way of discussing memory safety than "segfaults" #30963

Closed
@lambda

Description

@lambda

As pointed out in a thread on users.rlo, language in the documentation and website about preventing segfaults or preventing segfaults outside of unsafe code can be misleading. The Rust compiler uses guard pages to protect against stack overflow, and will hopefully eventually use stack probes to make this sound (#16012). However, this means that it's possible to get a stack overflow using 100% safe code:

fn main() {
    let a = [0; 999999999];
}

This is not a theoretical concern; it has happened in practice using the musl target which has a small stack size, while using std::io::copy which copies a file using a 64k stack allocated buffer, in tailhook/vagga#116.

It seems that in a lot of discussion of Rust, "no segfaults" is used as a proxy for memory safety. However, it's not a very good proxy; there is both the possibility for segfaults without memory safety, like a stack overflow, and the bigger danger of memory unsafety is generally silent corruption of memory, not segfaults; segfaults are just an implementation specific and visible symptom of memory unsafety.

The book says that if you see a segfault "you can be sure the cause is related to something marked unsafe" (recently changed in #30819, but still not accurate, as demonstrated by the above code that segfaults without unsafe). The front page of the website says "prevents segfaults," which while that can be true as it doesn't say it prevents all segfaults, could mislead someone into thinking that it does.

I don't necessarily have a better phrasing for either the book or the website; explaining what is and is not considered memory safe is somewhat nuanced, and hard to do in a single sentence. But I think that whatever the phrasing is, "segfault" should probably not be used as it is misleading.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions