llvm-project/clang/test
Roman Lebedev 9872ea4ed1
[clang][CodeGen] Implicit Conversion Sanitizer: handle increment/decrement (PR44054)
Summary:
Implicit Conversion Sanitizer is *almost* feature complete.
There aren't *that* much unsanitized things left,
two major ones are increment/decrement (this patch) and bit fields.

As it was discussed in
[[ https://bugs.llvm.org/show_bug.cgi?id=39519 | PR39519 ]],
unlike `CompoundAssignOperator` (which is promoted internally),
or `BinaryOperator` (for which we always have promotion/demotion in AST)
or parts of `UnaryOperator` (we have promotion/demotion but only for
certain operations), for inc/dec, clang omits promotion/demotion
altogether, under as-if rule.

This is technically correct: https://rise4fun.com/Alive/zPgD
As it can be seen in `InstCombineCasts.cpp` `canEvaluateTruncated()`,
`add`/`sub`/`mul`/`and`/`or`/`xor` operators can all arbitrarily
be extended or truncated:
901cd3b3f6/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp (L1320-L1334)

But that has serious implications:
1. Since we no longer model implicit casts, do we pessimise
   their AST representation and everything that uses it?
2. There is no demotion, so lossy demotion sanitizer does not trigger :]

Now, i'm not going to argue about the first problem here,
but the second one **needs** to be addressed. As it was stated
in the report, this is done intentionally, so changing
this in all modes would be considered a penalization/regression.
Which means, the sanitization-less codegen must not be altered.

It was also suggested to not change the sanitized codegen
to the one with demotion, but i quite strongly believe
that will not be the wise choice here:
1. One will need to re-engineer the check that the inc/dec was lossy
   in terms of `@llvm.{u,s}{add,sub}.with.overflow` builtins
2. We will still need to compute the result we would lossily demote.
   (i.e. the result of wide `add`ition/`sub`traction)
3. I suspect it would need to be done right here, in sanitization.
   Which kinda defeats the point of
   using `@llvm.{u,s}{add,sub}.with.overflow` builtins:
   we'd have two `add`s with basically the same arguments,
   one of which is used for check+error-less codepath and other one
   for the error reporting. That seems worse than a single wide op+check.
4. OR, we would need to do that in the compiler-rt handler.
   Which means we'll need a whole new handler.
   But then what about the `CompoundAssignOperator`,
   it would also be applicable for it.
   So this also doesn't really seem like the right path to me.
5. At least X86 (but likely others) pessimizes all sub-`i32` operations
   (due to partial register stalls), so even if we avoid promotion+demotion,
   the computations will //likely// be performed in `i32` anyways.

So i'm not really seeing much benefit of
not doing the straight-forward thing.

While looking into this, i have noticed a few more LLVM middle-end
missed canonicalizations, and filed
[[ https://bugs.llvm.org/show_bug.cgi?id=44100 | PR44100 ]],
[[ https://bugs.llvm.org/show_bug.cgi?id=44102 | PR44102 ]].

Those are not specific to inc/dec, we also have them for
`CompoundAssignOperator`, and it can happen for normal arithmetics, too.
But if we take some other path in the patch, it will not be applicable
here, and we will have most likely played ourselves.

TLDR: front-end should emit canonical, easy-to-optimize yet
un-optimized code. It is middle-end's job to make it optimal.

I'm really hoping reviewers agree with my personal assessment
of the path this patch should take..

Fixes [[ https://bugs.llvm.org/show_bug.cgi?id=44054 | PR44054 ]].

Reviewers: rjmccall, erichkeane, rsmith, vsk

Reviewed By: erichkeane

Subscribers: mehdi_amini, dexonsmith, cfe-commits, #sanitizers, llvm-commits, aaron.ballman, t.p.northover, efriedma, regehr

Tags: #llvm, #clang, #sanitizers

Differential Revision: https://reviews.llvm.org/D70539
2019-11-27 15:39:55 +03:00
..
ARCMT
AST [Attr] Fix `-ast-print` for `asm` attribute 2019-11-18 11:55:25 -05:00
ASTMerge [clang] Make handling of unnamed template params similar to function params 2019-10-01 14:08:51 +00:00
Analysis [analyzer] Add custom filter functions for GenericTaintChecker 2019-11-23 20:12:15 +01:00
CXX [Diagnostics] Try to improve warning message for -Wreturn-type 2019-11-09 17:54:58 +01:00
ClangScanDeps [clang][ScanDeps] Fix issue with multiple commands with the same input. 2019-10-31 14:22:01 -07:00
CodeCompletion [clangd] Show lambda signature for lambda autocompletions 2019-11-22 12:48:06 +01:00
CodeGen [clang][CodeGen] Implicit Conversion Sanitizer: handle increment/decrement (PR44054) 2019-11-27 15:39:55 +03:00
CodeGenCUDA clang: Add -fconvergent-functions flag 2019-11-19 23:20:15 +05:30
CodeGenCXX Revert "Revert "As a follow-up to my initial mail to llvm-dev here's a first pass at the O1 described there."" 2019-11-26 20:28:52 -08:00
CodeGenCoroutines Remove unreachable blocks before splitting a coroutine. 2019-08-14 03:54:13 +00:00
CodeGenObjC Debug info: Emit objc_direct methods as members of their containing class 2019-11-21 11:01:10 -08:00
CodeGenObjCXX Revert "Revert "As a follow-up to my initial mail to llvm-dev here's a first pass at the O1 described there."" 2019-11-26 20:28:52 -08:00
CodeGenOpenCL [OpenCL] Add builtin function attribute handling 2019-11-05 10:26:47 +00:00
CodeGenOpenCLCXX [OpenCL] Fix address space for base method call (PR43145) 2019-11-21 10:39:33 +00:00
Coverage
CoverageMapping Revert "Use -fdebug-compilation-dir to form absolute paths in coverage mappings" 2019-10-28 14:40:17 -07:00
Driver Initial implementation of -fmacro-prefix-map and -ffile-prefix-map 2019-11-26 15:17:49 -08:00
FixIt [c++20] P1143R2: Add support for the C++20 'constinit' keyword. 2019-09-04 20:30:37 +00:00
Format [clang-format] Proposal for clang-format to give compiler style warnings 2019-10-13 14:51:45 +00:00
Frontend [Sema] Add MacroQualified case for FunctionTypeUnwrapper 2019-11-12 16:22:13 -08:00
Headers [X86] Fix the implementation of __readcr3/__writecr3 to work in 64-bit mode 2019-11-14 13:21:36 -08:00
Import [ASTImporter] Add support for BuiltinTemplateDecl 2019-10-30 14:53:35 +01:00
Index Revert "[Diagnostic] add a warning which warns about misleading indentation" 2019-11-25 13:19:57 -08:00
Integration
InterfaceStubs [clang][IFS] Driver Pipeline: generate stubs after standard pipeline (3) 2019-11-20 16:22:50 -05:00
Layout
Lexer Revert "Revert "As a follow-up to my initial mail to llvm-dev here's a first pass at the O1 described there."" 2019-11-26 20:28:52 -08:00
Misc Revert "[Diagnostic] add a warning which warns about misleading indentation" 2019-11-25 13:19:57 -08:00
Modules Remove +x permission on some files 2019-11-16 14:47:20 +01:00
OpenMP [OPENMP]Fix PR44133: crash on lambda reductions in templates. 2019-11-26 10:55:54 -05:00
PCH Revert "Revert "As a follow-up to my initial mail to llvm-dev here's a first pass at the O1 described there."" 2019-11-26 20:28:52 -08:00
Parser Fix parser bug that permitted 'private' as a (no-op) decl-specifier even outside OpenCL. 2019-11-20 11:59:58 -08:00
Preprocessor XFAIL a test on Windows 2019-11-26 19:55:50 -08:00
Profile Update clang tests for new LLVM IR backslash printing in r374415 2019-10-10 18:36:41 +00:00
Refactor [Tooling] Added DeclStmtClass to ExtractionSemicolonPolicy 2019-08-14 13:37:39 +00:00
Rewriter
Sema Workaround for EvalInfo ctor for MSVC 2017 2019-11-26 21:43:29 -05:00
SemaCUDA [CUDA][HIP} Add a test for constexpr default ctor 2019-10-11 02:43:28 +00:00
SemaCXX Partially reland "[Diagnostics] Put "deprecated copy" warnings into -Wdeprecated-copy"" 2019-11-26 14:41:34 +01:00
SemaObjC [Sema] Fix a -Wobjc-signed-char-bool false-positive 2019-11-18 12:15:20 -08:00
SemaObjCXX [ObjC] Diagnose implicit type coercion from ObjC 'Class' to object 2019-10-17 15:27:04 +00:00
SemaOpenCL [OpenCL] Add work-group and miscellaneous vector builtin functions 2019-11-26 10:44:49 +00:00
SemaOpenCLCXX Quote the token being diagnosed for C11 extensions. 2019-08-27 13:47:51 +00:00
SemaTemplate [Diagnostics] Try to improve warning message for -Wreturn-type 2019-11-09 17:54:58 +01:00
TableGen
Templight
Tooling Prefer 'env not' over 'not env' in tests. 2019-10-14 01:41:56 +00:00
Unit
VFS Improve VFS compatibility on Windows 2019-11-14 08:48:47 -08:00
clang-rename [clang-rename] Fix a crash when renaming a class without definition. 2019-10-04 14:09:31 +00:00
.clang-format
CMakeLists.txt [gen_ast_dump_json_test.py] Copy to binary directory to omit --clang argument 2019-11-15 12:52:56 +00:00
TestRunner.sh
cxx-sections.data
lit.cfg.py Revert "[analyzer] Add test directory for scan-build." 2019-11-05 14:03:36 -08:00
lit.site.cfg.py.in Improve behavior in the case of stack exhaustion. 2019-08-26 18:18:07 +00:00
make_test_dirs.pl