Commit Graph

74509 Commits

Author SHA1 Message Date
Roman Lebedev b69ba22773 [clang][ubsan] Implicit Conversion Sanitizer - integer truncation - clang part
Summary:
C and C++ are interesting languages. They are statically typed, but weakly.
The implicit conversions are allowed. This is nice, allows to write code
while balancing between getting drowned in everything being convertible,
and nothing being convertible. As usual, this comes with a price:

```
unsigned char store = 0;

bool consume(unsigned int val);

void test(unsigned long val) {
  if (consume(val)) {
    // the 'val' is `unsigned long`, but `consume()` takes `unsigned int`.
    // If their bit widths are different on this platform, the implicit
    // truncation happens. And if that `unsigned long` had a value bigger
    // than UINT_MAX, then you may or may not have a bug.

    // Similarly, integer addition happens on `int`s, so `store` will
    // be promoted to an `int`, the sum calculated (0+768=768),
    // and the result demoted to `unsigned char`, and stored to `store`.
    // In this case, the `store` will still be 0. Again, not always intended.
    store = store + 768; // before addition, 'store' was promoted to int.
  }

  // But yes, sometimes this is intentional.
  // You can either make the conversion explicit
  (void)consume((unsigned int)val);
  // or mask the value so no bits will be *implicitly* lost.
  (void)consume((~((unsigned int)0)) & val);
}
```

Yes, there is a `-Wconversion`` diagnostic group, but first, it is kinda
noisy, since it warns on everything (unlike sanitizers, warning on an
actual issues), and second, there are cases where it does **not** warn.
So a Sanitizer is needed. I don't have any motivational numbers, but i know
i had this kind of problem 10-20 times, and it was never easy to track down.

The logic to detect whether an truncation has happened is pretty simple
if you think about it - https://godbolt.org/g/NEzXbb - basically, just
extend (using the new, not original!, signedness) the 'truncated' value
back to it's original width, and equality-compare it with the original value.

The most non-trivial thing here is the logic to detect whether this
`ImplicitCastExpr` AST node is **actually** an implicit conversion, //or//
part of an explicit cast. Because the explicit casts are modeled as an outer
`ExplicitCastExpr` with some `ImplicitCastExpr`'s as **direct** children.
https://godbolt.org/g/eE1GkJ

Nowadays, we can just use the new `part_of_explicit_cast` flag, which is set
on all the implicitly-added `ImplicitCastExpr`'s of an `ExplicitCastExpr`.
So if that flag is **not** set, then it is an actual implicit conversion.

As you may have noted, this isn't just named `-fsanitize=implicit-integer-truncation`.
There are potentially some more implicit conversions to be warned about.
Namely, implicit conversions that result in sign change; implicit conversion
between different floating point types, or between fp and an integer,
when again, that conversion is lossy.

One thing i know isn't handled is bitfields.

This is a clang part.
The compiler-rt part is D48959.

Fixes [[ https://bugs.llvm.org/show_bug.cgi?id=21530 | PR21530 ]], [[ https://bugs.llvm.org/show_bug.cgi?id=37552 | PR37552 ]], [[ https://bugs.llvm.org/show_bug.cgi?id=35409 | PR35409 ]].
Partially fixes [[ https://bugs.llvm.org/show_bug.cgi?id=9821 | PR9821 ]].
Fixes https://github.com/google/sanitizers/issues/940. (other than sign-changing implicit conversions)

Reviewers: rjmccall, rsmith, samsonov, pcc, vsk, eugenis, efriedma, kcc, erichkeane

Reviewed By: rsmith, vsk, erichkeane

Subscribers: erichkeane, klimek, #sanitizers, aaron.ballman, RKSimon, dtzWill, filcab, danielaustin, ygribov, dvyukov, milianw, mclow.lists, cfe-commits, regehr

Tags: #sanitizers

Differential Revision: https://reviews.llvm.org/D48958

llvm-svn: 338288
2018-07-30 18:58:30 +00:00
George Karpenkov 676b3f0157 [analyzer] Store ValueDecl in DeclRegion
All use cases of DeclRegion actually have ValueDecl there,
and getting the name from declaration comes in very handy.

Differential Revision: https://reviews.llvm.org/D49998

llvm-svn: 338286
2018-07-30 18:57:13 +00:00
Richard Smith 91c89c5e2c Delete some unreachable AST printing code.
llvm-svn: 338282
2018-07-30 18:05:19 +00:00
Momchil Velikov 20208cc046 [ARM, AArch64]: Use unadjusted alignment when passing composites as arguments
The "Procedure Call Procedure Call Standard for the ARM® Architecture"
(https://static.docs.arm.com/ihi0042/f/IHI0042F_aapcs.pdf), specifies that
composite types are passed according to their "natural alignment", i.e. the
alignment before alignment adjustment on the entire composite is applied.

The same applies for AArch64 ABI.

Clang, however, used the adjusted alignment.

GCC already implements the ABI correctly. With this patch Clang becomes
compatible with GCC and passes such arguments in accordance with AAPCS.

Differential Revision: https://reviews.llvm.org/D46013

llvm-svn: 338279
2018-07-30 17:48:23 +00:00
Reka Kovacs e48ea894c6 [analyzer] Add missing state transition in IteratorChecker.
After cleaning up program state maps in `checkDeadSymbols()`,
a transition should be added to generate the new state.

Differential Revision: https://reviews.llvm.org/D47417

llvm-svn: 338263
2018-07-30 16:14:59 +00:00
Reka Kovacs c74cfc4215 [analyzer] Add support for more invalidating functions in InnerPointerChecker.
According to the standard, pointers referring to the elements of a
`basic_string` may be invalidated if they are used as an argument to
any standard library function taking a reference to non-const
`basic_string` as an argument. This patch makes InnerPointerChecker warn
for these cases.

Differential Revision: https://reviews.llvm.org/D49656

llvm-svn: 338259
2018-07-30 15:43:45 +00:00
Ilya Biryukov 55b1b157b6 [CodeComplete] Fix the crash in code completion on access checking
Started crashing in r337453. See the added test case for the crash repro.

The fix reverts part of r337453 that causes the crash and does
not actually break anything when reverted.

llvm-svn: 338255
2018-07-30 15:19:05 +00:00
Alexey Bataev cdbe44c95c [OPENMP] Modify the info about OpenMP support in UsersManual, NFC.
llvm-svn: 338252
2018-07-30 14:44:29 +00:00
Stefan Maksimovic 6e50be1e97 [mips64][clang] Adjust tests to account for changes in r338239
llvm-svn: 338246
2018-07-30 12:27:40 +00:00
Krasimir Georgiev e3424bf477 [clang-format] Silence -Wdocumentation warnings
introduced in r338232

llvm-svn: 338245
2018-07-30 12:22:41 +00:00
Stefan Maksimovic b9da8a5dff [mips64][clang] Provide the signext attribute for i32 return values
Additional info: see r338019.

Differential Revision: https://reviews.llvm.org/D49289

llvm-svn: 338239
2018-07-30 10:44:46 +00:00
Simon Pilgrim 0d466c41ff Fix -Wdocumentation warning. NFCI.
llvm-svn: 338238
2018-07-30 10:07:47 +00:00
Adam Balogh a692120cb7 [Analyzer] Iterator Checker Hotfix: Defer deletion of container data until its last iterator is cleaned up
The analyzer may consider a container region as dead while it still has live
iterators. We must defer deletion of the data belonging to such containers
until all its iterators are dead as well to be able to compare the iterator
to the begin and the end of the container which is stored in the container
data.

Differential Revision: https://reviews.llvm.org/D48427

llvm-svn: 338234
2018-07-30 08:52:21 +00:00
Krasimir Georgiev 6a5c95bd66 [clang-format] Indent after breaking Javadoc annotated line
Summary:
This patch makes clang-format indent the subsequent lines created by breaking a
long javadoc annotated line.

Reviewers: mprobst

Reviewed By: mprobst

Subscribers: acoomans, cfe-commits

Differential Revision: https://reviews.llvm.org/D49797

llvm-svn: 338232
2018-07-30 08:45:45 +00:00
Richard Smith 5f2a349280 PR38355 Prevent infinite recursion when checking initializer lifetime if
an initializer is self-referential.

llvm-svn: 338230
2018-07-30 07:19:54 +00:00
Chandler Carruth 1f82d9ba6e Revert r337456: [CodeGen] Disable aggressive structor optimizations at -O0, take 3
This commit increases the number of sections and overall output size of
.o files by 10% and sometimes a bit more. This alone is challenging for
some users, but it also appears to trigger an as-yet unexplained
behavior in the Gold linker where the memory usage increases
considerably more than 10% (we think).

The increase is also frustrating because in many (if not all) cases we
end up with almost all of the growth coming from the ELF overhead of
-ffunction-sections and such, not from actual extra code being emitted.

Richard Smith and Eric Christopher are both going to investigate this
and try to get to the bottom of what is triggering this and whether the
kinds of increases here are sustainable or what options we might have to
minimize the impact they have. However, this is currently breaking
a pretty large number of our users' builds so reverting it while we sort
out how to make progress here. I've seen a longer and more detailed
update to the commit thread.

llvm-svn: 338209
2018-07-29 03:05:07 +00:00
Serge Pavlov 376051820d [UBSan] Strengthen pointer checks in 'new' expressions
With this change compiler generates alignment checks for wider range
of types. Previously such checks were generated only for the record types
with non-trivial default constructor. So the types like:

    struct alignas(32) S2 { int x; };
    typedef __attribute__((ext_vector_type(2), aligned(32))) float float32x2_t;

did not get checks when allocated by 'new' expression.

This change also optimizes the checks generated for the arrays created
in 'new' expressions. Previously the check was generated for each
invocation of type constructor. Now the check is generated only once
for entire array.

Differential Revision: https://reviews.llvm.org/D49589

llvm-svn: 338199
2018-07-28 15:33:03 +00:00
Akira Hatanaka a6b5e00361 [Sema][ObjC] Warn when a method declared in a protocol takes a
non-escaping parameter but the implementation's method takes an escaping
parameter.

rdar://problem/39548196

Differential Revision: https://reviews.llvm.org/D49119

llvm-svn: 338189
2018-07-28 04:06:13 +00:00
Yaxun Liu a4005e13f7 [CUDA][HIP] Allow function-scope static const variable
CUDA 8.0 E.3.9.4 says: Within the body of a __device__ or __global__
function, only __shared__ variables or variables without any device
memory qualifiers may be declared with static storage class.

It is unclear how a function-scope non-const static variable
without device memory qualifier is implemented, therefore only static
const variable without device memory qualifier is allowed, which
can be emitted as a global variable in constant address space.

Currently clang only allows function-scope static variable with
__shared__ qualifier.

This patch also allows function-scope static const variable without
device memory qualifier and emits it as a global variable in constant
address space.

Differential Revision: https://reviews.llvm.org/D49931

llvm-svn: 338188
2018-07-28 03:05:25 +00:00
George Karpenkov 39e5137f43 [AST] Add a convenient getter from QualType to RecordDecl
Differential Revision: https://reviews.llvm.org/D49951

llvm-svn: 338187
2018-07-28 02:16:13 +00:00
Erik Pilkington 38aecc6381 Compile SemaTemplate.cpp with /bigobj on MSVC
This should fix some bot failures introduced by r338165.

llvm-svn: 338186
2018-07-28 01:29:31 +00:00
Fangrui Song 32fa871547 [CFG] Remove duplicate function/class names at the beginning of comments
Some functions/classes have renamed while the comments still use the old names. Delete them per coding style.

Also some whitespace cleanup.

llvm-svn: 338183
2018-07-28 00:48:05 +00:00
Nicolas Lesser 05141f1f2d Parse a possible trailing postfix expression suffix after a fold expression
Summary:
This patch allows the parsing of a postfix expression involving a fold expression, which is legal as a fold-expression is a primary-expression.

See also https://llvm.org/pr38282

Reviewers: rsmith

Reviewed By: rsmith

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D49848

llvm-svn: 338170
2018-07-27 21:55:12 +00:00
Erik Pilkington 69770d3d7a [Sema] Use a TreeTransform to extract deduction guide parameter types
Previously, we just canonicalized the type, but this lead to crashes with
parameter types that referred to ParmVarDecls of the constructor. There may be
more cases that this TreeTransform needs to handle though, such as a constructor
parameter type referring to a member in an unevaluated context. Canonicalization
doesn't address these cases either though, so we can address them as-needed in
follow-up commits.

rdar://41330135

Differential revision: https://reviews.llvm.org/D49439

llvm-svn: 338165
2018-07-27 21:23:48 +00:00
Alexey Bataev e1c9feb6e5 [DEBUG_INFO] Fix tests, NFC.
llvm-svn: 338158
2018-07-27 20:16:44 +00:00
Alexey Bataev b83b4e40fe [DEBUGINFO] Disable unsupported debug info options for NVPTX target.
Summary:
Some targets support only default set of the debug options and do not
support additional debug options, like NVPTX target. Patch introduced
virtual function supportsDebugInfoOptions() that can be overloaded
by the toolchain, checks if the target supports some debug
options and emits warning when an unsupported debug option is
found.

Reviewers: echristo

Subscribers: aprantl, JDevlieghere, cfe-commits

Differential Revision: https://reviews.llvm.org/D49148

llvm-svn: 338155
2018-07-27 19:45:14 +00:00
George Karpenkov b293c6bb54 [analyzer] Extend NoStoreFuncVisitor to insert a note on IVars
The note is added in the following situation:

 - We are throwing a nullability-related warning on an IVar
 - The path goes through a method which *could have* (syntactically
   determined) written into that IVar, but did not

rdar://42444460

Differential Revision: https://reviews.llvm.org/D49689

llvm-svn: 338149
2018-07-27 18:26:40 +00:00
Richard Smith 032a0b9f37 Fix typos in comment.
llvm-svn: 338141
2018-07-27 18:06:54 +00:00
George Karpenkov 42b76e8232 [ASTMatchers] Introduce a matcher for `ObjCIvarExpr`, support getting it's declaration
ObjCIvarExpr is *not* a subclass of MemberExpr, and a separate matcher
is required to support it.
Adding a hasDeclaration support as well, as it's not very useful without
it.

Differential Revision: https://reviews.llvm.org/D49701

llvm-svn: 338140
2018-07-27 17:40:59 +00:00
Alexey Bataev 2f5b2671da [OPENMP] Static variables on device must be externally visible.
Do not mark static variable as internal on the device as they must be
visible from the host to be mapped correctly.

llvm-svn: 338139
2018-07-27 17:37:32 +00:00
George Karpenkov 079275b4dc [ASTMatchers] Introduce a matcher for `ObjCIvarExpr`, support getting it's declaration.
ObjCIvarExpr is *not* a subclass of MemberExpr, and a separate matcher
is required to support it.
Adding a hasDeclaration support as well, as it's not very useful without
it.

Differential Revision: https://reviews.llvm.org/D49701

llvm-svn: 338137
2018-07-27 17:26:11 +00:00
Richard Smith 7ed5fb2d22 Add missing temporary materialization conversion on left-hand side of .
in some member function calls.

Specifically, when calling a conversion function, we would fail to
create the AST node representing materialization of the class object.

llvm-svn: 338135
2018-07-27 17:13:18 +00:00
Roman Lebedev 12216f1d4a [AST] Sink 'part of explicit cast' down into ImplicitCastExpr
Summary:
As discussed in IRC with @rsmith, it is slightly not good to keep that in the `CastExpr` itself:
Given the explicit cast, which is represented in AST as an `ExplicitCastExpr` + `ImplicitCastExpr`'s,
only the  `ImplicitCastExpr`'s will be marked as `PartOfExplicitCast`, but not the `ExplicitCastExpr` itself.
Thus, it is only ever `true` for `ImplicitCastExpr`'s, so we don't need to write/read/dump it for `ExplicitCastExpr`'s.

We don't need to worry that we write the `PartOfExplicitCast` in PCH after `CastExpr::path_iterator`,
since the `ExprImplicitCastAbbrev` is only used when the `NumBaseSpecs == 0`, i.e. there is no 'path'.

Reviewers: rsmith, rjmccall, erichkeane, aaron.ballman

Reviewed By: rsmith, erichkeane

Subscribers: vsk, cfe-commits, rsmith

Tags: #clang

Differential Revision: https://reviews.llvm.org/D49838

llvm-svn: 338108
2018-07-27 07:27:14 +00:00
Mike Edwards 1cb062b041 [WWW] Fixing file permissions for the .html pages.
llvm-svn: 338098
2018-07-27 04:41:37 +00:00
Emmett Neyman be97297545 added shared library to fix buildbot
Summary: added shared library to fix buildbot

Subscribers: mgorny, cfe-commits

Differential Revision: https://reviews.llvm.org/D49895

llvm-svn: 338091
2018-07-27 00:43:26 +00:00
Erik Pilkington dd0b344339 [Sema] Fix a crash by completing a type before using it
Only apply this exception on a type that we're able to check.

rdar://41903969

Differential revision: https://reviews.llvm.org/D49868

llvm-svn: 338089
2018-07-26 23:40:42 +00:00
Mike Edwards 2eb7a7a9a3 [WWW] Removing my test file as the auto-deployment script has been fixed.
llvm-svn: 338087
2018-07-26 23:29:54 +00:00
Mike Edwards f5d614c901 [WWW] Adding a test page to work out an auto-deployment issue.
llvm-svn: 338086
2018-07-26 23:23:40 +00:00
Reid Kleckner 4d23f45a11 Revert r338057 "[VirtualFileSystem] InMemoryFileSystem::status: Return a Status with the requested name"
This broke clang/test/PCH/case-insensitive-include.c on Windows.

llvm-svn: 338084
2018-07-26 23:21:51 +00:00
Reid Kleckner 4a83f0abd8 [MS] Add L__FUNCSIG__ for compatibility
Clang already has L__FUNCTION__ as a workaround for dealing with
pre-processor code that expects to be able to do L##__FUNCTION__ in a
macro. This patch implements the same logic for __FUNCSIG__.

Fixes PR38295.

llvm-svn: 338083
2018-07-26 23:18:44 +00:00
Emmett Neyman e5581242a0 Updated llvm-proto-fuzzer to execute the compiled code
Summary:
Made changes to the llvm-proto-fuzzer
- Added loop vectorizer optimization pass in order to have two IR versions
- Updated old fuzz target to handle two different IR versions
- Wrote code to execute both versions in memory

Reviewers: morehouse, kcc, alexshap

Reviewed By: morehouse

Subscribers: pcc, mgorny, cfe-commits, llvm-commits

Differential Revision: https://reviews.llvm.org/D49526

llvm-svn: 338077
2018-07-26 22:23:25 +00:00
Sanjin Sijaric 56391d6f84 [ARM64] [Windows] Follow MS X86_64 C++ ABI when passing structs
Summary: Microsoft's C++ object model for ARM64 is the same as that for X86_64.
For example, small structs with non-trivial copy constructors or virtual
function tables are passed indirectly.  Currently, they are passed in registers
when compiled with clang.

Reviewers: rnk, mstorsjo, TomTan, haripul, javed.absar

Reviewed By: rnk, mstorsjo

Subscribers: kristof.beyls, chrib, llvm-commits, cfe-commits

Differential Revision: https://reviews.llvm.org/D49770

llvm-svn: 338076
2018-07-26 22:18:28 +00:00
Simon Marchi 9980c261df [VirtualFileSystem] InMemoryFileSystem::status: Return a Status with the requested name
Summary:

InMemoryFileSystem::status behaves differently than
RealFileSystem::status.  The Name contained in the Status returned by
RealFileSystem::status will be the path as requested by the caller,
whereas InMemoryFileSystem::status returns the normalized path.

For example, when requested the status for "../src/first.h",
RealFileSystem returns a Status with "../src/first.h" as the Name.
InMemoryFileSystem returns "/absolute/path/to/src/first.h".

The reason for this change is that I want to make a unit test in the
clangd testsuite (where we use an InMemoryFileSystem) to reproduce a
bug I get with the clangd program (where a RealFileSystem is used).
This difference in behavior "hides" the bug in the unit test version.

Reviewers: malaperle, ilya-biryukov, bkramer

Subscribers: cfe-commits, ioeric, ilya-biryukov, bkramer, hokein, omtcyfz

Differential Revision: https://reviews.llvm.org/D48903

llvm-svn: 338057
2018-07-26 18:55:02 +00:00
Richard Smith ef6c43dc0c Refactor checking of switch conditions and case values.
Check each case value in turn while parsing it, performing the
conversion to the switch type within the context of the expression
itself. This will become necessary in order to properly handle cleanups
for temporaries created as part of the case label (in an upcoming
patch). For now it's just good hygiene.

This necessitates moving the checking for the switch condition itself to
earlier, so that the destination type is available when checking the
case labels.

As a nice side-effect, we get slightly improved diagnostic quality and
error recovery by separating the case expression checking from the case
statement checking and from tracking whether there are discarded case
labels.

llvm-svn: 338056
2018-07-26 18:41:30 +00:00
Alexey Bataev c5982fb634 [OPENMP, DOCS] Fixed typo, NFC.
llvm-svn: 338055
2018-07-26 18:40:41 +00:00
Mandeep Singh Grang 2a153101bf [COFF, ARM64] Decide when to mark struct returns as SRet
Summary:
Refer the MS ARM64 ABI Convention for the behavior for struct returns:
https://docs.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions#return-values

Reviewers: mstorsjo, compnerd, rnk, javed.absar, yinma, efriedma

Reviewed By: rnk, efriedma

Subscribers: haripul, TomTan, yinma, efriedma, kristof.beyls, chrib, llvm-commits

Differential Revision: https://reviews.llvm.org/D49464

llvm-svn: 338050
2018-07-26 18:07:59 +00:00
Alexey Bataev 3bdd60095f [OPENMP] What's new for OpenMP in clang.
Updated ReleaseNotes + Status of the OpenMP support in clang.

llvm-svn: 338049
2018-07-26 17:53:45 +00:00
Akira Hatanaka 66d405d31f [Sema][ObjC] Do not propagate the nullability specifier on the receiver
to the result type of a message send if the result type cannot have a
nullability specifier.

Previously, clang would print the following message when the code in
nullability.m was compiled:

"incompatible integer to pointer conversion initializing 'int *' with
an expression of type 'int _Nullable'"

This is wrong as 'int' isn't supposed to have any nullability
specifiers.

rdar://problem/40830514

llvm-svn: 338048
2018-07-26 17:51:13 +00:00
Ana Pazos 1eee1b771f [RISCV] Add support for interrupt attribute
Summary:
Clang supports the GNU style ``__attribute__((interrupt))`` attribute  on RISCV targets.
Permissible values for this parameter are user, supervisor, and machine.
If there is no parameter, then it defaults to machine.
Reference: https://gcc.gnu.org/onlinedocs/gcc/RISC-V-Function-Attributes.html
Based on initial patch by Zhaoshi Zheng.

Reviewers: asb, aaron.ballman

Reviewed By: asb, aaron.ballman

Subscribers: rkruppe, the_o, aaron.ballman, MartinMosbeck, brucehoult, rbar, johnrusso, simoncook, sabuasal, niosHD, kito-cheng, shiva0217, zzheng, edward-jones, mgrang, rogfer01, cfe-commits

Differential Revision: https://reviews.llvm.org/D48412

llvm-svn: 338045
2018-07-26 17:37:45 +00:00
Akira Hatanaka cb6a933c9b [CodeGen][ObjC] Make block copy/dispose helper functions exception-safe.
When an exception is thrown in a block copy helper function, captured
objects that have previously been copied should be destructed or
released. Similarly, captured objects that are yet to be released should
be released when an exception is thrown in a dispose helper function.

rdar://problem/42410255

Differential Revision: https://reviews.llvm.org/D49718

llvm-svn: 338041
2018-07-26 16:51:21 +00:00