Skip to content

Clarify the definition of undefined behavior #202

Open

Description

@gnzlbg

I think we should clarify in the definition of undefined behavior that:

  • While this document (and the reference) often call out the behavior that is undefined, the behavior that is not explicitly defined in these documents is also undefined.

  • Undefined behavior cannot be assumed to remain undefined.

To expand on this second point, we currently say that creating a &mut T with value 0 is undefined behavior, but we guarantee that this will always be undefined behavior (because the type has a niche), and therefore, this code is sound:

fn foo(x: &mut T) { unsafe {
   if transmute(x) == 0_usize {
      *std::ptr::null() // never invokes UB
   }
}}

Now follow me in this thought experiment, and imagine that, instead of guaranteeing that &mut T are never 0, we were just to say that creating a &mut T with value 0 is UB.

Would the example above be sound? I don't think so, because we could, in a future release of Rust, make this undefined behavior defined in such a way that would make that code unsound. For example, we could say that creating a &mut T with value 0 is ok, but dereferencing it panics, and that change in the language from undefined to defined behavior would break the code above.

That is, while the compiler can assume that undefined behavior never happens when optimizing Rust code, users cannot assume that undefined behavior will remain undefined when writing Rust code. The only thing users can assume are things that we guarantee.

If we want to say that some behavior is undefined, and that behavior will remain undefined forever, such that users can assume that it never happens, we need to word that as a guarantee, and breaking that guarantee would be a backward incompatible change.

So maybe we need to split our lists of undefined behavior into behaviors that are just undefined, and behaviors that are guaranteed to be undefined.

Activity

  1. RalfJung commented on Sep 11, 2019

    @RalfJung
    Member

    While this document (and the reference) often call out the behavior that is undefined, the behavior that is not explicitly defined in these documents is also undefined.

    I think that is one of the worst possible definitions of UB.
    IMO, it is a spec bug if the spec does not cover a case. That's just like having a non-exhaustive match. A mathematical definition has to cover all cases, otherwise it is not a valid definition. The same goes for a proper spec.

  2. gnzlbg commented on Sep 11, 2019

    @gnzlbg
    ContributorAuthor

    So what do we call behavior not defined in the spec then ?

  3. gnzlbg commented on Sep 11, 2019

    @gnzlbg
    ContributorAuthor

    Or are you suggesting that the spec is not valid until it becomes complete ?

  4. RalfJung commented on Sep 11, 2019

    @RalfJung
    Member

    "Not yet specified behavior"? It's basically a white spot on the map. That's very different from UB, for which there should be a precise (and conscious) definition. UB is where we know there are dragons; a white spot is where there might be dragons or not.

  5. RalfJung commented on Sep 11, 2019

    @RalfJung
    Member

    Or are you suggesting that the spec is not valid until it becomes complete ?

    Not sure what you mean by "valid", but is certainly not complete until there are no more missing cases.

  6. gnzlbg commented on Sep 11, 2019

    @gnzlbg
    ContributorAuthor

    "Not yet specified behavior"?

    This sounds good to me.

    Not sure what you mean by "valid", but is certainly not complete until there are no more missing cases.

    You mentioned that:

    A mathematical definition has to cover all cases, otherwise it is not a valid definition. The same goes for a proper spec.

    So I asked whether this means that you consider any incomplete spec invalid. As in, if the spec is not complete, it is not a valid Rust spec, and nothing in it applies to Rust.

    I don't think doing that would be very useful, because AFAIK there might never be a complete spec for Rust (this is a practical / engineering problem). Even if at some point the spec would be believed to be complete, any tiny detail that would make it incomplete would render it invalid (e.g. new nightly feature is added to Rust without spec wording, now nothing in the spec applies to nightly Rust anymore).

    I think it would be more useful to say that the spec does not defines the behavior of all well-formed Rust programs because there is some behavior that has not yet been specified, but if you have a program for which the spec defines all of its behavior, then you can use the spec to reason about it.

  7. gnzlbg commented on Sep 11, 2019

    @gnzlbg
    ContributorAuthor

    As in, the spec can tell you what some well-formed programs do or whether some well-formed programs have UB, but at least right now there definitely are well-formed programs for which the spec can't tell you what they do or whether they have UB or not because the behavior of these programs is not specified.


    I worry a bit that "not-specified-behavior" is too similar to "unspecified behavior". There is a difference between unspecified behavior that's required not to be undefined, and unspecified behavior that could be undefined.

  8. Diggsey commented on Sep 11, 2019

    @Diggsey

    To expand on this second point, we currently say that creating a &mut T with value 0 is undefined behavior, but we guarantee that this will always be undefined behavior (because the type has a niche), and therefore, this code is sound:

    It's the other way around: the spec says that a &mut T must refer to a valid T and as a result creating one with value 0 is UB.

    It doesn't make sense for the spec to say "this is UB now but might not be UB in future" or for the spec to say "this is UB and will stay UB forever" - the spec describes rust at a point in time.

    We can then take two versions of the spec and ask whether a newer one is backwards compatible with an older one: if something is UB in a previous version then it can continue to be UB or its behaviour may be defined in the newer version.

    However, if the old spec says "&mut T must refer to a valid T" and the new spec relaxes that, then the new spec cannot be backwards compatible.

    TLDR. "UB" already means "we are saying nothing about the behaviour in this case", and there's no reason to introduce a term to say "we will never say anything about the behaviour in this case" because that's not a useful thing to say - it restricts what we can do for no reason.

  9. hanna-kruppe commented on Sep 11, 2019

    @hanna-kruppe

    @RalfJung

    I think that is one of the worst possible definitions of UB.
    IMO, it is a spec bug if the spec does not cover a case. That's just like having a non-exhaustive match. A mathematical definition has to cover all cases, otherwise it is not a valid definition. The same goes for a proper spec.

    I agree that it is generally better to explicitly state when something is UB than risk declaring something reasonable UB by mistakenly not covering it. However, a spec is only a model of reality, and neither can nor should exhaustively list everything that could possibly happen in reality that might be relevant to the operation of a Rust program. So there's always going to be vast stretches of things that people might reasonably want to do, but which the spec (and implementation documentation) doesn't give any guarantees or even guidance about. Since these things often interact with the parts that are covered by the spec in non-trivial ways, it's not hard get something indistingushable from de jure UB (assumed not to happen by implementations, non-local and time-travelling effects, has cascading and arbitrarily bad consequences, etc.).

    Clearly, it's overly conservative to declare everything not explicitly covered by the spec to be UB: many reasonable things that need to happen in real systems and work perfectly fine fall under that. But "it's probably UB" is a safer default answer and just calling these things "not specified" does not properly convey the distinction between "we'll get back to you" and "you're on your own and probably stuff will break". For example, if someone has a question about a tricky interplay of raw pointers and &mut's, we can say we don't yet have a finalized memory model so it's not settled yet. If someone does very funky stuff with page tables and linker scripts, I wouldn't call it a spec bug if the spec doesn't tell them if they're in the clear.

    I'd also like to note that "we don't specify what this does" is not really any more restricted than "it's UB" because the thing it might do is causing UB. So it's not like people can look at the spec, see that the thing they want is "not specified", and then can feel justified in doing it. It's really just UB by a less scary-sounding name.

  10. gnzlbg commented on Sep 11, 2019

    @gnzlbg
    ContributorAuthor

    We can then take two versions of the spec and ask whether a newer one is backwards compatible with an older one: if something is UB in a previous version then it can continue to be UB or its behaviour may be defined in the newer version.

    @Diggsey If undefined behavior becomes defined, the safe and sound Rust API of the thought experiment becomes unsound.

    There are dozens of places in the reference, nomicon, and the UCGs where we say that something is undefined.

    For a concrete example, we used to say that the behavior of unwinding out of an extern "C" functions is undefined. That is, the following safe and stable Rust API is sound (it never invokes UB):

    pub fn bar(x: extern "C" fn()) {
        if catch_unwind(|| x()).is_err() { unsafe { /* UB */ } }
    }

    If we were to "fix" that UB in the reference, by saying that "unwinding out of an extern "C" function is ok" (which feels like a perfectly reasonable thing that we should be able to do if we wanted), then that stable, safe, and sound Rust API becomes unsound. That would be a serious backward incompatible change.

    I think we should be able to remove "X is UB" from the reference, and for that, we need to forbid users from relying on things being UB to write code like the above.

  11. Diggsey commented on Sep 11, 2019

    @Diggsey

    If undefined behavior becomes defined, the safe and sound Rust API of the thought experiment becomes unsound.

    Undefined behaviour becoming defined is not what causes that example to become unsound. It becomes unsound because the spec previously said that &mut T referred to a valid T`, and the new version does not guarantee that, which is a breaking change.

  12. gnzlbg commented on Sep 11, 2019

    @gnzlbg
    ContributorAuthor

    It becomes unsound because the spec previously said that &mut T referred to a valid T`, and the new version does not guarantee that, which is a breaking change.

    I think you are focusing too much on the &mut T example instead of trying to understand the issue I'm trying to raise.

  13. Diggsey commented on Sep 11, 2019

    @Diggsey

    OK, to take your unwinding example, I don't believe that code is sound to begin with.

    pub fn bar(x: extern "C" fn()) {
        if catch_unwind(|| x()).is_err() { unsafe { /* UB */ } }
    }

    You can't negatively reason about UB in this way. Just because the spec doesn't define what the behaviour of unwinding is, a specific compiler implementation on specific hardware may define that behaviour to be something sensible, or a future version of the spec may define that behaviour.

    This is different from the &mut example, because in that case you are not relying on negative reasoning, you are relying on an explicit guarantee made by the spec.

    edit:
    If the spec explicitly said "extern C functions will never unwind" then that of course would be a different story.

  14. Lokathor commented on Sep 11, 2019

    @Lokathor
    Contributor

    It doesn't make sense for the spec to say "this is UB now but might not be UB in future" or for the spec to say "this is UB and will stay UB forever" - the spec describes rust at a point in time.

    Actually a lot of people doing unsafe code really want to know the difference between "this is UB now and we're resolving to keep it always UB (at least for this edition)" compared to "this us UB now because we haven't gotten there yet, but it might change later".

  15. gnzlbg commented on Sep 11, 2019

    @gnzlbg
    ContributorAuthor

    @Diggsey

    You can't negatively reason about UB in this way.

    Where do we say so?

  16. 19 remaining items

  17. hanna-kruppe commented on Oct 18, 2019

    @hanna-kruppe

    I hope this helps clarify why I see these two kinds of assumptions as being very different in nature.

    It does, thanks.

    I still maintain that, no matter what taxonomy of assumption we use for formalizations and specs, it would do users a disservice to choose terminology that casts any doubt on whether changing the rounding mode is "less bad" than e.g. transmuting 3 to bool.

    Edit: to be clear, this does not mean I'm opposed to trying to communicate the distinction you draw to users, but it is a somewhat subtle point and doesn't affect most users (the end result is the same, "don't do X or rustc breaks your code"), so overly stressing it doesn't seem like a good use of time nor worth the risk of confusion.

  18. gnzlbg commented on Oct 18, 2019

    @gnzlbg
    ContributorAuthor

    @RalfJung

    I hope this helps clarify why I see these two kinds of assumptions as being very different in nature.

    Thanks, that does clarify it, and it is related to what's discussed in rust-lang/miri#932 about UB due to incorrect usage of target-features: we make the assumption that the hardware the binary runs on is of a particular hardware architecture, has particular target features, the FP-environment configured in a particular way, etc. If the user then runs that binary on the wrong target, we make no guarantees, so that's UB, but is a different kind of UB than "an error in the abstract machine".

    @rkruppe

    I still maintain that, no matter what taxonomy of assumption we use for formalizations and specs, it would do users a disservice to choose terminology that casts any doubt on whether changing the rounding mode is "less bad" than e.g. transmuting 3 to bool.

    I think that we should actively communicate that this different kind of UB is actually much worse. UB of the form "error in the abstract machine" is not possible in safe Rust, can be detected under miri in unsafe Rust, etc. UB of this other form cannot really be detected nor prevented. For example, if one compiles a safe Rust program for x86 and runs it on ARM, then that's UB of this other form in safe Rust, and there is nothing that we can really do about it.

  19. RalfJung commented on Oct 18, 2019

    @RalfJung
    Member

    @rkruppe

    I still maintain that, no matter what taxonomy of assumption we use for formalizations and specs, it would do users a disservice to choose terminology that casts any doubt on whether changing the rounding mode is "less bad" than e.g. transmuting 3 to bool.

    Indeed I can see that from a user perspective, it is not always helpful to distinguish between the two. But I find it important that for the deeper discussions we have here in the UCG, we agree on being able to tell apart (at least) two separate kinds of assumptions the compiler is making.

    So maybe we should use "UB" as the overarching term for "assumptions the compiler is making" (one way or another), and then we can have a list of the kinds of assumptions it makes, and that list currently has two items:

    • The Rust Abstract Machine classifies certain behavior as erroneous, such as transmute::<u8, bool>(3). The Rust compiler assumes that the program it compiles will never trigger such an error. Hence reaching this error state in the R-AM is UB.
    • When generating target code, the Rust compiler makes some assumptions about its environment (e.g. on x86 it assumes the floating point environment to be set up in a certain way, it assumes the page tables not to "act up", things like that). Hence violating these assumptions is UB.

    @gnzlbg

    I think that we should actively communicate that this different kind of UB is actually much worse. UB of the form "error in the abstract machine" is not possible in safe Rust, can be detected under miri in unsafe Rust, etc. UB of this other form cannot really be detected nor prevented. For example, if one compiles a safe Rust program for x86 and runs it on ARM, then that's UB of this other form in safe Rust, and there is nothing that we can really do about it.

    OTOH it is much less likely that someone will try to run an x86 program on ARM, than that someone creates a NULL reference. Also the failure mode is typically more graceful (it'll just SIGILL instead of having a security vulnerability).

  20. Centril commented on Oct 18, 2019

    @Centril
    Contributor

    and that list currently has two items:

    Possibly with short-hands?

    • (abstract) machine UB
    • target UB
  21. Ixrec commented on Oct 18, 2019

    @Ixrec
    Contributor

    "machine UB" imo sounds too much like a hardware/target-specific thing.

    But "abstract (machine) UB" versus "target UB" seems like a very helpful and concise way to describe this distinction.

  22. Lokathor commented on Oct 19, 2019

    @Lokathor
    Contributor

    well x86 itself is comfortable setting the rounding mode, but LLVM is not. So fpenv is "llvm ub".

  23. RalfJung commented on Oct 19, 2019

    @RalfJung
    Member

    LLVM is not part of the Rust spec.

  24. Lokathor commented on Oct 19, 2019

    @Lokathor
    Contributor

    Nor are the particulars of x86 and ARM, but we must still live in the universe they give us.

    You yourself say that we can't possibly settle on any memory model that LLVM won't implement. This really isn't any different.

  25. hanna-kruppe commented on Oct 19, 2019

    @hanna-kruppe

    It is also not a matter specific to LLVM, as "supporting changing the rounding mode" (even more so, changing other aspects of the FP env) conflicts with desirable optimizations and is thus unappealing as only or default mode for optimizing compilers regardless of what components they are built from. Thus the "don't touch fpenv" rule is common to optimizing compilers built on LLVM and those not built on LLVM as well as some language standards that are independent of/predating LLVM (most prominently, C99 and its Annex F).

    Furthermore, there is ongoing work to support FP env access in LLVM, and for reasons discussed above rustc should IMO not use this capability by default but at most make it an option that some code sections can opt into.

    So this UB cannot be blamed on LLVM or its limitations in any way.

  26. RalfJung commented on Oct 19, 2019

    @RalfJung
    Member
  27. Lokathor commented on Oct 19, 2019

    @Lokathor
    Contributor

    @RalfJung Well, yes and no.

    The term "target" currently means the architecture and OS combination you're building for. So my point was that we should use "target UB" for UB problems that come from the architecture and OS and that pile of "platform stuff".

    However, LLVM's limits are also limits that we have to live by when we compile Rust via LLVM. I have no idea why you think we can just ignore talking about them.

    @rkruppe yeah I know it's not just LLVM, and that it's a good limit, useful, etc etc. I'm not saying LLVM is at fault and should change, I'm just trying to correctly attribute where each of our limitations comes from.

  28. Centril commented on Oct 19, 2019

    @Centril
    Contributor

    However, LLVM's limits are also limits that we have to live by when we compile Rust via LLVM. I have no idea why you think we can just ignore talking about them.

    Further to "it's not just LLVM", that goes for any backend -- we are bounded by the union of the set of things that each backend does not support. So for example, if Cranelift (🎉) does not support something then we are bounded by that as well. LLVM is not special.

  29. RalfJung commented on Oct 19, 2019

    @RalfJung
    Member
  30. added
    Triaged
    S-pending-documentation
    on Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-abstract-machineC-terminologyS-pending-documentationTriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions