LLVM loop optimization can make safe programs crash #28728

Open
RalfJung opened this Issue Sep 29, 2015 · 23 comments

Projects

None yet
@RalfJung
Contributor

The following snippet crashes when compiled in release mode on current stable, beta and nightly:

enum Null {}

fn foo() -> Null { loop { } }

fn create_null() -> Null {
    let n = foo();

    let mut i = 0;
    while i < 100 { i += 1; }
    return n;
}

fn use_null(n: Null) -> ! {
    match n { }
}


fn main() {
    use_null(create_null());
}

https://play.rust-lang.org/?gist=1f99432e4f2dccdf7d7e&version=stable

This is based on the following example of LLVM removing a loop that I was made aware of: https://github.com/simnalamburt/snippets/blob/master/rust/src/bin/infinite.rs.
What seems to happen is that since C allows LLVM to remove endless loops that have no side-effect, we end up executing a match that has to arms.

@steveklabnik steveklabnik added the A-llvm label Sep 29, 2015
@ranma42
Contributor
ranma42 commented Sep 29, 2015

The LLVM IR of the optimised code is

; Function Attrs: noreturn nounwind readnone uwtable
define internal void @_ZN4main20h5ec738167109b800UaaE() unnamed_addr #0 {
entry-block:
  unreachable
}

This kind of optimisation breaks the main assumption that should normally hold on uninhabited types: it should be impossible to have a value of that type.
rust-lang/rfcs#1216 proposes to explicitly handle such types in Rust. It might be effective in ensuring that LLVM never has to handle them and in injecting the appropriate code to ensure divergence when needed (IIUIC this could be achieved with appropriate attributes or intrinsic calls).
This topic has also been recently discussed in the LLVM mailing list: http://lists.llvm.org/pipermail/llvm-dev/2015-July/088095.html

@alexcrichton
Member

triage: I-nominated

Seems bad! If LLVM doesn't have a way to say "yes, this loop really is infinite" though then we may just have to sit-and-wait for the upstream discussion to settle.

@ranma42
Contributor
ranma42 commented Sep 29, 2015

A way to prevent infinite loops from being optimised away is to add unsafe {asm!("" :::: "volatile")} inside of them. This is similar to the llvm.noop.sideeffect intrinsic that has been proposed in the LLVM mailing list, but it might prevent some optimisations.
In order to avoid the performance loss and to still guarantee that diverging functions/loops are not optimised away, I believe that it should be sufficient to insert an empty non-optimisable loop (i.e. loop { unsafe { asm!("" :::: "volatile") } }) if uninhabited values are in scope.
If LLVM optimises the code which should diverge to the point that it does not diverge anymore, such loops will ensure that the control flow is still unable to proceed.
In "lucky" case in which LLVM is unable to optimise the diverging code, such loop will be removed by DCE.

@geofft
Contributor
geofft commented Sep 29, 2015

Is this related to #18785? That one's about infinite recursion to be UB, but it sounds like the fundamental cause might be similar: LLVM doesn't consider not halting to be a side effect, so if a function has no side effects other than not halting, it's happy to optimize it away.

@arielb1
Contributor
arielb1 commented Sep 29, 2015

@geofft

It's the same issue.

@RalfJung
Contributor

Yes, looks like it's the same. Further down that issue, they show how to get undef, from which I assume it's not hard to make a (seemingly safe) program crash.

@simnalamburt
Contributor

👍

@ranma42
Contributor
ranma42 commented Sep 29, 2015
@bluss bluss added the I-wrong label Sep 29, 2015
@nikomatsakis
Contributor

So I've been wondering how long until somebody reports this. :) In my opinion, the best solution would of course be if we could tell LLVM not to be so aggressive about potentially infinite loops. Otherwise, the only thing I think we can do is to do a conservative analysis in Rust itself that determines whether:

  1. the loop will terminate OR
  2. the loop will have side-effects (I/O operations etc, I forget precisely how this is defined in C)

Either of this should be enough to avoid undefined behavior.

@nikomatsakis
Contributor

triage: P-medium

We'd like to see what LLVM will do before we invest a lot of effort on our side, and this seems relatively unlikely to cause problems in practice (though I have personally hit this while developing the compiler as well). There are no backwards incomatibility issues to be concerned about.

@rust-highfive rust-highfive added P-medium and removed I-nominated labels Oct 1, 2015
@dotdash
Contributor
dotdash commented Oct 1, 2015

Quoting from the LLVM mailing list discussion:

 The implementation may assume that any thread will eventually do one of the following:
   - terminate
   - make a call to a library I/O function
   - access or modify a volatile object, or
   - perform a synchronization operation or an atomic operation

 [Note: This is intended to allow compiler transformations such as removal of empty loops, even
  when termination cannot be proven. — end note ]
@ranma42
Contributor
ranma42 commented Oct 2, 2015

@dotdash The excerpt you are quoting comes from the C++ specification; it is basically the answer to "how it [having side effects] is defined in C" (also confirmed by the standard committee: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1528.htm ).

Regarding what is the expected behaviour of the LLVM IR there is some confusion. https://llvm.org/bugs/show_bug.cgi?id=24078 shows that there seems to be no accurate & explicit specification of the semantics of infinite loops in LLVM IR. It aligns with the semantics of C++, most likely for historical reasons and for convenience (I only managed to track down https://groups.google.com/forum/#!topic/llvm-dev/j2vlIECKkdE which apparently refers to a time when infinite loops were not optimised away, some time before the C/C++ specs were updated to allow it).

From the thread it is clear that there is the desire to optimise C++ code as effectively as possible (i.e. also taking into account the opportunity to remove infinite loops), but in the same thread several developers (including some that actively contribute to LLVM) have shown interest in the ability to preserve infinite loops, as they are needed for other languages.

@dotdash
Contributor
dotdash commented Oct 2, 2015

@ranma42 I'm aware of that, I just quoted that for reference, because one possibility to work-around this would be to detect such loops in rust and add one of the above to it to stop LLVM from performing this optimization.

@bstrie
Contributor
bstrie commented Nov 30, 2015

Is this a soundness issue? If so, we should tag it as such.

@bluss
Contributor
bluss commented Nov 30, 2015

Yes, following @ranma42's example, this way shows how it readily defeats array bounds checks. playground link

@bluss bluss added I-unsound and removed I-wrong labels Nov 30, 2015
@arielb1 arielb1 added I-wrong and removed I-unsound labels Dec 2, 2015
@arielb1
Contributor
arielb1 commented Dec 2, 2015

@bluss

The policy is that wrong-code issues that are also soundness issues (i.e. most of them) should be tagged I-wrong.

@nikomatsakis
Contributor

So just to recap prior discussion, there are really two choices here that I can see:

  • Wait for LLVM to provide a solution.
  • Introduce no-op asm statements wherever there may be an infinite loop or infinite recursion (#18785).

The latter is kind of bad because it can inhibit optimization, so we'd want to do it somewhat sparingly -- basically wherever we can't prove termination ourselves. You could also imaging tying it a bit more to how LLVM optimizes -- i.e., introducing only if we can detect a scenario that LLVM might consider to be an infinite loop/recursion -- but that would (a) require tracking LLVM and (b) require deeper knowledge than I, at least, possess.

@gnzlbg
Contributor
gnzlbg commented Sep 1, 2016

Wait for LLVM to provide a solution.

What is the LLVM bug tracking this issue?

@oli-obk
Contributor
oli-obk commented Oct 11, 2016

side-note: while true {} exhibits this behaviour. Maybe the lint should be upgraded to error-by-default and get a note stating that this currently can exhibit undefined behaviour?

@ubsan
Contributor
ubsan commented Nov 30, 2016 edited

Also, note that this is invalid for C. LLVM making this argument means that there is a bug in clang.

void foo() { while (1) { } }

void create_null() {
        foo();

        int i = 0;
        while (i < 100) { i += 1; }
}

__attribute__((noreturn))
void use_null() {
        __builtin_unreachable();
}


int main() {
        create_null();
        use_null();
}

This crashes with optimizations; this is invalid behavior under the C11 standard:

An iteration statement whose controlling expression is not a constant
expression, [note 156] that performs no  input/output  operations,
does  not  access  volatile  objects,  and  performs  no synchronization or
atomic operations in its body, controlling expression, or (in the case of
a for statement) its expression-3, may be   assumed   by   the
implementation to terminate. [note 157]

156: An omitted controlling expression is replaced by a nonzero constant,
     which is a constant expression.
157: This  is  intended  to  allow  compiler  transformations  such  as
     removal  of  empty  loops  even  when termination cannot be proven. 

Note the "whose controlling expression is not a constant expression" - while (1) { }, 1 is a constant expression, and thus may not be removed.

@oli-obk
Contributor
oli-obk commented Nov 30, 2016

Is the loop removal an optimization pass that we could simply remove?

@gnzlbg
Contributor
gnzlbg commented Nov 30, 2016

@ubsan

Did you find a bug report for that in LLVM's bugzilla or filled one? It seems that in C++ infinite loops that can never terminate are undefined behavior, but in C they are defined behavior (either they can be safely removed in some cases, or they cannot in others).

@ubsan
Contributor
ubsan commented Nov 30, 2016 edited
@rkruppe rkruppe added a commit to rkruppe/rust that referenced this issue Dec 3, 2016
@rkruppe rkruppe book: use abort() over loop {} for panic
Due to #28728 loop {} is very risky and can lead to fun debugging experiences such as #38136. Besides, aborting is probably better behavior than an infinite loop.
9398d75
@rkruppe rkruppe added a commit to rkruppe/rust that referenced this issue Dec 3, 2016
@rkruppe rkruppe book: use abort() over loop {} for panic
Due to #28728 loop {} is very risky and can lead to fun debugging experiences like in #38136. Besides, aborting is probably better behavior than an infinite loop.
6687dcc
@rkruppe rkruppe added a commit to rkruppe/rust that referenced this issue Jan 4, 2017
@rkruppe rkruppe book: use abort() over loop {} for panic
Due to #28728 loop {} is very risky and can lead to fun debugging experiences like in #38136. Besides, aborting is probably better behavior than an infinite loop.
893f42a
@steveklabnik steveklabnik added a commit to steveklabnik/rust that referenced this issue Jan 4, 2017
@steveklabnik steveklabnik Rollup merge of #38138 - rkruppe:no_std-no_loop, r=steveklabnik
book: use abort() over loop {} for panic

Due to #28728 `loop {}` is very risky and can lead to fun debugging experiences such as #38136. Besides, aborting is probably better behavior than an infinite loop.

r? @steveklabnik
521d337
@bors bors added a commit that referenced this issue Jan 9, 2017
@bors bors Auto merge of #38138 - rkruppe:no_std-no_loop, r=steveklabnik
book: use abort() over loop {} for panic

Due to #28728 `loop {}` is very risky and can lead to fun debugging experiences such as #38136. Besides, aborting is probably better behavior than an infinite loop.

r? @steveklabnik
ec9ae8c
@frewsxcv frewsxcv added a commit to frewsxcv/rust that referenced this issue Jan 9, 2017
@frewsxcv frewsxcv Rollup merge of #38138 - rkruppe:no_std-no_loop, r=steveklabnik
book: use abort() over loop {} for panic

Due to #28728 `loop {}` is very risky and can lead to fun debugging experiences such as #38136. Besides, aborting is probably better behavior than an infinite loop.

r? @steveklabnik
06f45e6
@frewsxcv frewsxcv added a commit to frewsxcv/rust that referenced this issue Jan 9, 2017
@rkruppe rkruppe book: use abort() over loop {} for panic
Due to #28728 loop {} is very risky and can lead to fun debugging experiences like in #38136. Besides, aborting is probably better behavior than an infinite loop.
f7dbec3
@bors bors added a commit that referenced this issue Jan 9, 2017
@bors bors Auto merge of #38138 - rkruppe:no_std-no_loop, r=steveklabnik
book: use abort() over loop {} for panic

Due to #28728 `loop {}` is very risky and can lead to fun debugging experiences such as #38136. Besides, aborting is probably better behavior than an infinite loop.

r? @steveklabnik
aa3ae13
@bors bors added a commit that referenced this issue Jan 10, 2017
@bors bors Auto merge of #38138 - rkruppe:no_std-no_loop, r=steveklabnik
book: use abort() over loop {} for panic

Due to #28728 `loop {}` is very risky and can lead to fun debugging experiences such as #38136. Besides, aborting is probably better behavior than an infinite loop.

r? @steveklabnik
78c892d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment