llvm-project/clang/docs
Roman Lebedev 536b0ee40a [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

llvm-svn: 374293
2019-10-10 09:25:02 +00:00
..
CommandGuide [Test Commit] Fix typo in diagtool.rst 2019-08-16 20:26:48 +00:00
analyzer [analyzer] Add a checker option to detect nested dead stores 2019-09-03 15:22:43 +00:00
tools
AddressSanitizer.rst [Docs] Modernize references to macOS 2019-05-30 16:46:22 +00:00
AttributeReference.rst
AutomaticReferenceCounting.rst [Docs] Modernize references to macOS 2019-05-30 16:46:22 +00:00
Block-ABI-Apple.rst
Block-ABI-Apple.txt
BlockLanguageSpec.rst
CMakeLists.txt
ClangCheck.rst
ClangCommandLineReference.rst Decrease the verbosity of the -ftime-trace option 2019-10-01 07:52:42 +00:00
ClangFormat.rst
ClangFormatStyleOptions.rst [NFC] Reverting changes from test commit. 2019-10-09 15:12:22 +00:00
ClangPlugins.rst [PragmaHandler] Expose `#pragma` location 2019-05-21 23:51:38 +00:00
ClangStaticAnalyzer.rst [analyzer]Add user docs rst 2019-07-10 14:49:53 +00:00
ClangTools.rst
ConstantInterpreter.rst [Clang Interpreter] Initial patch for the constexpr interpreter 2019-09-13 09:46:16 +00:00
ControlFlowIntegrity.rst cfi-icall: Allow the jump table to be optionally made non-canonical. 2019-08-09 22:31:59 +00:00
ControlFlowIntegrityDesign.rst
CrossCompilation.rst
DataFlowSanitizer.rst
DataFlowSanitizerDesign.rst
DiagnosticsReference.rst
DriverArchitecture.png
DriverInternals.rst
ExternalClangExamples.rst
FAQ.rst
HardwareAssistedAddressSanitizerDesign.rst hwasan: Improve precision of checks using short granule tags. 2019-07-09 20:22:36 +00:00
HowToSetupToolingForLLVM.rst
InternalsManual.rst [ASTImporter][NFC] Update ASTImporter internals docs 2019-10-07 11:15:18 +00:00
IntroductionToTheClangAST.rst
ItaniumMangleAbiTags.rst
JSONCompilationDatabase.rst
LTOVisibility.rst
LanguageExtensions.rst Fix __is_signed builtin 2019-09-23 15:41:20 +00:00
LeakSanitizer.rst [Docs] Modernize references to macOS 2019-05-30 16:46:22 +00:00
LibASTImporter.rst [ASTImporter][NFC] Fix typo in user docs 2019-10-07 11:14:53 +00:00
LibASTMatchers.rst
LibASTMatchersReference.html [clang] Update isDerivedFrom to support Objective-C classes 🔍 2019-08-12 23:23:35 +00:00
LibASTMatchersTutorial.rst AST Matchers tutorial requests to enable clang-tools-extra. NFC 2019-06-07 16:31:06 +00:00
LibFormat.rst
LibTooling.rst [Tooling] Migrated APIs that take ownership of objects to unique_ptr 2019-08-30 09:29:34 +00:00
MSVCCompatibility.rst
Makefile.sphinx
MemorySanitizer.rst [ReleaseNotes] MemorySanitizer support of ASLR on FreeBSD 2019-08-27 10:04:03 +00:00
Modules.rst [Docs] Modernize references to macOS 2019-05-30 16:46:22 +00:00
ObjectiveCLiterals.rst
OpenMPSupport.rst [OPENMP]Updated status page, NFC. 2019-09-11 14:44:30 +00:00
PCHInternals.rst
PCHLayout.graffle
PCHLayout.png
RAVFrontendAction.rst [Tooling] Migrated APIs that take ownership of objects to unique_ptr 2019-08-30 09:29:34 +00:00
README.txt
RefactoringEngine.rst
ReleaseNotes.rst [UBSan][clang][compiler-rt] Applying non-zero offset to nullptr is undefined behaviour 2019-10-10 09:25:02 +00:00
SafeStack.rst [Docs] Modernize references to macOS 2019-05-30 16:46:22 +00:00
SanitizerCoverage.rst
SanitizerSpecialCaseList.rst
SanitizerStats.rst
ShadowCallStack.rst
SourceBasedCodeCoverage.rst
ThinLTO.rst ThinLTO: Document the option BOOTSTRAP_LLVM_ENABLE_LTO 2019-09-02 15:34:53 +00:00
ThreadSafetyAnalysis.rst
ThreadSanitizer.rst
Toolchain.rst
Tooling.rst
UndefinedBehaviorSanitizer.rst [UBSan][clang][compiler-rt] Applying non-zero offset to nullptr is undefined behaviour 2019-10-10 09:25:02 +00:00
UsersManual.rst Recommit -r372180 2019-09-18 15:09:49 +00:00
conf.py Bump the trunk version to 10.0.0svn 2019-07-18 11:51:05 +00:00
doxygen-mainpage.dox
doxygen.cfg.in [NFC] Remove LLVM_ALIGNAS 2019-07-31 03:22:08 +00:00
index.rst [Clang Interpreter] Initial patch for the constexpr interpreter 2019-09-13 09:46:16 +00:00
make.bat

README.txt

See llvm/docs/README.txt