[llvm] r374293 - [UBSan][clang][compiler-rt] Applying non-zero offset to nullptr is undefined behaviour
Roman Lebedev via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 10 02:25:03 PDT 2019
Author: lebedevri
Date: Thu Oct 10 02:25:02 2019
New Revision: 374293
URL: http://llvm.org/viewvc/llvm-project?rev=374293&view=rev
Log:
[UBSan][clang][compiler-rt] Applying non-zero offset to nullptr is undefined behaviour
Summary:
Quote from http://eel.is/c++draft/expr.add#4:
```
4 When an expression J that has integral type is added to or subtracted
from an expression P of pointer type, the result has the type of P.
(4.1) If P evaluates to a null pointer value and J evaluates to 0,
the result is a null pointer value.
(4.2) Otherwise, if P points to an array element i of an array object x with n
elements ([dcl.array]), the expressions P + J and J + P
(where J has the value j) point to the (possibly-hypothetical) array
element i+j of x if 0≤i+j≤n and the expression P - J points to the
(possibly-hypothetical) array element i−j of x if 0≤i−j≤n.
(4.3) Otherwise, the behavior is undefined.
```
Therefore, as per the standard, applying non-zero offset to `nullptr`
(or making non-`nullptr` a `nullptr`, by subtracting pointer's integral value
from the pointer itself) is undefined behavior. (*if* `nullptr` is not defined,
i.e. e.g. `-fno-delete-null-pointer-checks` was *not* specified.)
To make things more fun, in C (6.5.6p8), applying *any* offset to null pointer
is undefined, although Clang front-end pessimizes the code by not lowering
that info, so this UB is "harmless".
Since rL369789 (D66608 `[InstCombine] icmp eq/ne (gep inbounds P, Idx..), null -> icmp eq/ne P, null`)
LLVM middle-end uses those guarantees for transformations.
If the source contains such UB's, said code may now be miscompiled.
Such miscompilations were already observed:
* https://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20190826/687838.html
* https://github.com/google/filament/pull/1566
Surprisingly, UBSan does not catch those issues
... until now. This diff teaches UBSan about these UB's.
`getelementpointer inbounds` is a pretty frequent instruction,
so this does have a measurable impact on performance;
I've addressed most of the obvious missing folds (and thus decreased the performance impact by ~5%),
and then re-performed some performance measurements using my [[ https://github.com/darktable-org/rawspeed | RawSpeed ]] benchmark:
(all measurements done with LLVM ToT, the sanitizer never fired.)
* no sanitization vs. existing check: average `+21.62%` slowdown
* existing check vs. check after this patch: average `22.04%` slowdown
* no sanitization vs. this patch: average `48.42%` slowdown
Reviewers: vsk, filcab, rsmith, aaron.ballman, vitalybuka, rjmccall, #sanitizers
Reviewed By: rsmith
Subscribers: kristof.beyls, nickdesaulniers, nikic, ychen, dtzWill, xbolva00, dberris, arphaman, rupprecht, reames, regehr, llvm-commits, cfe-commits
Tags: #clang, #sanitizers, #llvm
Differential Revision: https://reviews.llvm.org/D67122
Modified:
llvm/trunk/docs/ReleaseNotes.rst
Modified: llvm/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/ReleaseNotes.rst?rev=374293&r1=374292&r2=374293&view=diff
==============================================================================
--- llvm/trunk/docs/ReleaseNotes.rst (original)
+++ llvm/trunk/docs/ReleaseNotes.rst Thu Oct 10 02:25:02 2019
@@ -53,6 +53,20 @@ Non-comprehensive list of changes in thi
Makes programs 10x faster by doing Special New Thing.
+* As per :ref:`LLVM Language Reference Manual <i_getelementptr>`,
+ ``getelementptr inbounds`` can not change the null status of a pointer,
+ meaning it can not produce non-null pointer given null base pointer, and
+ likewise given non-null base pointer it can not produce null pointer; if it
+ does, the result is a :ref:`poison value <poisonvalues>`.
+ Since `r369789 <https://reviews.llvm.org/rL369789>`_
+ (`D66608 <https://reviews.llvm.org/D66608>`_ ``[InstCombine] icmp eq/ne (gep
+ inbounds P, Idx..), null -> icmp eq/ne P, null``) LLVM uses that for
+ transformations. If the original source violates these requirements this
+ may result in code being miscompiled. If you are using Clang front-end,
+ Undefined Behaviour Sanitizer ``-fsanitize=pointer-overflow`` check
+ will now catch such cases.
+
+
Changes to the LLVM IR
----------------------
More information about the llvm-commits
mailing list