16504 Commits

Author SHA1 Message Date
Dmitry Vyukov
99c7664bd4 asan: fix crash in strdup on malloc failure
There are some programs that try to handle all malloc failures.
Let's allow testing of such programs.

Reviewed By: melver

Differential Revision: https://reviews.llvm.org/D144374
2023-02-20 15:34:33 +01:00
Erik Desjardins
87d02e0dfd Recommit "[Support] change StringMap hash function from djbHash to xxHash"
This reverts commit 37eb9d13f891f7656f811516e765b929b169afe0.

Test failures have been fixed:

- ubsan failure fixed by 72eac42f21c0f45a27f3eaaff9364cbb5189b9e4
- warn-unsafe-buffer-usage-fixits-local-var-span.cpp fixed by
  03cc52dfd1dbb4a59b479da55e87838fb93d2067 (wasn't related)
- test-output-format.ll failure was spurious, build failed at
  https://lab.llvm.org/buildbot/#/builders/54/builds/3545 (b4431b2d945b6fc19b1a55ac6ce969a8e06e1e93)
  but passed at
  https://lab.llvm.org/buildbot/#/builders/54/builds/3546 (5ae99be0377248c74346096dc475af254a3fc799)
  which is before my revert
  b4431b2d94...5ae99be037

Original commit message:

    Depends on https://reviews.llvm.org/D142861.

    Alternative to https://reviews.llvm.org/D137601.

    xxHash is much faster than djbHash. This makes a simple Rust test case with a large constant string 10% faster to compile.

    Previous attempts at changing this hash function (e.g. https://reviews.llvm.org/D97396) had to be reverted due to breaking tests that depended on iteration order.
    No additional tests fail with this patch compared to `main` when running `check-all` with `-DLLVM_ENABLE_PROJECTS="all"` (on a Linux host), so I hope I found everything that needs to be changed.

    Differential Revision: https://reviews.llvm.org/D142862
2023-02-19 16:52:26 -05:00
Vitaly Buka
3a3ce59123 [sanitizers] Update global_symbols.txt 2023-02-18 23:40:11 -08:00
Lang Hames
2ec1aba2b6 [llvm-jitlink] Discard allocation actions in -noexec mode.
Allocation actions may run JIT'd code, which isn't permitted in -noexec mode.

Testcases that depend on actions running should be moved to the ORC runtime.
2023-02-17 20:18:23 -08:00
Kirill Stoimenov
b4b5006be5 [LSAN] Mask out tags from pointers on ARM in MaybeUserPointer heuristic
This caused false positives because the existing logic was not taking into account that pointers could have a tag in them.

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D144305
2023-02-18 03:37:56 +00:00
Daniel Thornburgh
8007bcc13f [llvm-cov] Create syntax to pass source w/o binary.
Since binary ID lookup makes CLI object arguments optional, it should be
possible to pass a list of source files without a binary. Unfortunately,
the current syntax will always interpret the first source file as a
binary. This change adds a `-sources` option to cause all later
positional arguments to be considered sources.

Reviewed By: gulfem

Differential Revision: https://reviews.llvm.org/D144207
2023-02-17 13:11:54 -08:00
Leonard Chan
22b7685401 Reland "[hwasan] Add definitions for missing operator delete functions"
Looks like builders were failing for the sized variants of operator
delete. These are hidden behind the __cpp_sized_deallocation macro
which can be defined with -fsized-deallocation.
2023-02-15 23:47:01 +00:00
Chia-hung Duan
a9269773eb [scudo] Improve the uses of roundUpTo/roundDownTo/isAligned
The implementations of those functions require the rounding target to be
power-of-two. It's better to add a debugging check to avoid misuse.
Besides, add a general verion of those three to accommadate non
power-of-two cases.

Also change the name to roundUp/roundDown/isAligned

Reviewed By: cferris, cryptoad

Differential Revision: https://reviews.llvm.org/D142658
2023-02-15 23:44:44 +00:00
Chia-hung Duan
94a391b949 [scudo] Calling iterateOverChunks requires holding lock
Ensure the allocator is disabled before visiting all chunks.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D142157
2023-02-15 23:44:44 +00:00
Chia-hung Duan
ae1bd3adf0 [scudo] Add thread-safety annotations on TSD data members
Ideally, we want to assert that all the operations on
Cache/QuarantineCache always have the `Mutex` acquired. However,
the current architecture of accessing TSD is not easy to cooperate
with the thread-safety analysis because of pointer aliasing. In
alternative, we add the getters for accessing TSD member and attach
proper thread-safety annotations on them.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D142151
2023-02-15 23:44:44 +00:00
Leonard Chan
6313ece5ce Revert "[hwasan] Add definitions for missing operator delete functions"
This reverts commit d6ff0808618cd421d7ee82daec951956ec27a837.

This broke a bunch of builders:
http://45.33.8.238/linux/99657/step_10.txt
https://lab.llvm.org/buildbot/#/builders/247/builds/1627
2023-02-15 21:51:49 +00:00
Leonard Chan
d6ff080861 [hwasan] Add definitions for missing operator delete functions
Differential Revision: https://reviews.llvm.org/D144030
2023-02-15 19:41:04 +00:00
Chia-hung Duan
72584d9199 [scudo] Fix inconsistent signed/unsigned comparison
Fix broken test on PPC

Differential Revision: https://reviews.llvm.org/D144121
2023-02-15 19:14:17 +00:00
Chia-hung Duan
dfacba5af0 [scudo] Update ring buffer test to make it accept zero size
allocation ring buffer is allowed to be zero. Update the logic in the
test so that on the platform that disables it won't fail this case.

Reviewed By: fmayer

Differential Revision: https://reviews.llvm.org/D144055
2023-02-15 01:23:39 +00:00
Chia-hung Duan
6a4c39596d [scudo] Add the thread-safety annotations
This CL adds the proper thread-safety annotations for most of the
functions and variables. However, given the restriction of the current
architecture, in some cases, we may not be able to use the annotations
easily. The followings are two exceptions,

1. enable()/disable(): Many structures in scudo are enabled/disabled by
   acquiring the lock in each instance. This makes those structure act
   like a `lock`. We can't mark those functions with ACQUIRE()/RELEASE()
   because that makes the entire allocator become another `lock`. In the
   end, that implies we need to *acquire* the `allocator` before each
   malloc et al. request. Therefore, adding a variable to tell the
   status of those structures may be a better way to cooperate with
   thread-safety annotation.

2. TSD/TSD shared/TSD exclusive: These three have simiar restrictions as
   mentioned above. In addition, they don't always need to be released
   if it's a thread local instance. However, thread-safety analysis
   doesn't support conditional branch. Which means we can't mark the
   proper annotations around the uses of TSDs. We may consider to make
   it consistent and which makes the code structure simpler.

This CL is supposed to introduce the annotations with the least code
refactoring. So only trivial thread safety issues will be addressed
here. For example, lacking of acquiring certain lock before accessing
certain variables will have the ScopedLock inserted. Other than that,
they are supposed to be done in the later changes.

Reviewed By: cferris

Differential Revision: https://reviews.llvm.org/D140706
2023-02-15 01:19:51 +00:00
Leonard Chan
913b4aa608 Reland "[compiler-rt][hwasan] Add unused attribute to GetRegisters"
This was reverted bc of breakage on Fuchsia, but we landed a local fix
for this.
2023-02-14 21:13:52 +00:00
Guillaume Chelfi
4e3dac6f0a [scudo] Call __scudo_deallocate_hook on reallocations.
Scudo is expected to call __scudo_allocate_hook on allocations, and
__scudo_deallocate_hook on deallocations, but it's behavior is not
clear on reallocations. Currently, non-trivial reallocations call
__scudo_allocate_hook but never __scudo_deallocate_hook. We should
prefer either calling both, none, or a dedicated
hook (__scudo_reallocate_hook, for instance).

This patch implements the former, and adds a unit test to enforce
those expectations.

Reviewed By: Chia-hungDuan

Differential Revision: https://reviews.llvm.org/D141407
2023-02-14 18:44:38 +00:00
Chia-hung Duan
f7016f8a8e [scudo] Call getStats when the region is exhausted
Because of lock contention, we temporarily disabled the printing of
regions' status when it's exhausted. Given that it's useful when the
Region OOM happens, this CL brings it back without lock contention.

Differential Revision: https://reviews.llvm.org/D141955
2023-02-13 23:12:15 +00:00
Chia-hung Duan
70758b801d [scudo] Calling getStats requires holding lock
We didn't acquire the mutex while accessing those lock protected data,
this CL fixes it and now we don't need to disable the allocator while
reading its states.

Differential Revision: https://reviews.llvm.org/D142149
2023-02-13 23:09:47 +00:00
Fangrui Song
e7ade78a8c [dfsan] Fix some build-libc-list.py lint issues 2023-02-10 16:48:34 -08:00
Fangrui Song
35188e02d1 [dfsan] Fix build-libc-list.py for Python 3 after D143701 2023-02-10 10:55:44 -08:00
Tobias Hieta
8470430107
[NFC][compiler-rt] Shorten comment 2023-02-10 14:42:48 +01:00
Tobias Hieta
8ccde9368f
[CMake][compiler-rt] Support clang-cl in CompilerRTMockLLVMCMakeConfig
clang-cl doesn't support -dumpmachine directly, so we need to
preface it with /clang: in order to get this probing function
to work.

This is needed in order to run cmake directly on the runtimes
directory.

Reviewed By: hans

Differential Revision: https://reviews.llvm.org/D143557
2023-02-10 14:32:46 +01:00
Tobias Hieta
57edc61093
[NFC] convert compiler-rt/lib/dfsan/scripts/build-libc-list.py to python3 syntax
I found a few Python files not using Python 3 syntax in the tree
when trying to apply reformatting to them. This was converted with
2to3 and the changes seemed sane.

Reviewed By: MaskRay, browneee

Differential Revision: https://reviews.llvm.org/D143701
2023-02-10 10:20:51 +01:00
Kirill Stoimenov
d87468e56c [HWASAN] Add support for max_allocation_size_mb option
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D143667
2023-02-09 20:24:47 +00:00
Kirill Stoimenov
c9258ab7f2 [LSAN] Fix pthread_create interceptor to ignore leaks in real pthread_create.
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D143209
2023-02-08 22:55:19 +00:00
Kirill Stoimenov
550cb763da Revert "[LSAN] Fix pthread_create interceptor to ignore leaks in real pthread_create."
This reverts commit a7db3cb257ff6396481f44427bccd0ca5abf4d63.
2023-02-08 21:55:55 +00:00
Kirill Stoimenov
a7db3cb257 [LSAN] Fix pthread_create interceptor to ignore leaks in real pthread_create.
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D143209
2023-02-08 20:36:35 +00:00
Erik Desjardins
37eb9d13f8 Revert "[Support] change StringMap hash function from djbHash to xxHash"
This reverts commit d768b97424f9e1a0aae45440a18b99f21c4027ce.

Causes sanitizer failure: https://lab.llvm.org/buildbot/#/builders/238/builds/1114

```
/b/sanitizer-aarch64-linux-bootstrap-ubsan/build/llvm-project/llvm/lib/Support/xxhash.cpp:107:12: runtime error: applying non-zero offset 8 to null pointer
    #0 0xaaaab28ec6c8 in llvm::xxHash64(llvm::StringRef) /b/sanitizer-aarch64-linux-bootstrap-ubsan/build/llvm-project/llvm/lib/Support/xxhash.cpp:107:12
    #1 0xaaaab28cbd38 in llvm::StringMapImpl::LookupBucketFor(llvm::StringRef) /b/sanitizer-aarch64-linux-bootstrap-ubsan/build/llvm-project/llvm/lib/Support/StringMap.cpp:87:28
```

Probably causes test failure in `warn-unsafe-buffer-usage-fixits-local-var-span.cpp`: https://lab.llvm.org/buildbot/#/builders/60/builds/10619

Probably causes reverse-iteration test failure in `test-output-format.ll`: https://lab.llvm.org/buildbot/#/builders/54/builds/3545
2023-02-08 10:41:51 -05:00
Marco Elver
bf9814b705 [SanitizerBinaryMetadata] Emit constants as ULEB128
Emit all constant integers produced by SanitizerBinaryMetadata as
ULEB128 to further reduce binary space used. Increasing the version is
not necessary given this change depends on (and will land) along with
the bump to v2.

To support this, the !pcsections metadata format is extended to allow
for per-section options, encoded in the first MD operator which must
always be a string and contain the section: "<section>!<options>".

Reviewed By: dvyukov

Differential Revision: https://reviews.llvm.org/D143484
2023-02-08 13:12:34 +01:00
Marco Elver
3d53b52730 [SanitizerBinaryMetadata] Optimize used space for features and UAR stack args
Optimize the encoding of "covered" metadata by:

 1. Reducing feature mask from 4 bytes to 1 byte (needs increase once we
    reach more than 8 features).

 2. Only emitting UAR stack args size if it is non-zero, saving 4 bytes
    in the common case.

One caveat is that the emitted metadata for function PC (offset), size,
and UAR size (if enabled) are no longer aligned to 4 bytes.

SanitizerBinaryMetadata version base is increased to 2, since the change
is backwards incompatible.

Reviewed By: dvyukov

Differential Revision: https://reviews.llvm.org/D143482
2023-02-08 13:12:33 +01:00
Tobias Hieta
78fb02107a
[compiler-rt][macOS]: Disable iOS support if iOS SDK is not found
If you are missing the iOS SDK on your macOS (for example you don't have
full Xcode but just CommandLineTools) then CMake currently errors
out without a helpful message. This patch disables iOS support in
compiler-rt if the iOS SDK is not found. This can be overriden by
passing -DCOMPILER_RT_ENABLE_IOS=ON.

Reviewed By: delcypher, thetruestblue

Differential Revision: https://reviews.llvm.org/D133273
2023-02-08 08:12:34 +01:00
Erik Desjardins
d768b97424 [Support] change StringMap hash function from djbHash to xxHash
Depends on https://reviews.llvm.org/D142861.

Alternative to https://reviews.llvm.org/D137601.

xxHash is much faster than djbHash. This makes a simple Rust test case with a large constant string 10% faster to compile.

Previous attempts at changing this hash function (e.g. https://reviews.llvm.org/D97396) had to be reverted due to breaking tests that depended on iteration order.
No additional tests fail with this patch compared to `main` when running `check-all` with `-DLLVM_ENABLE_PROJECTS="all"` (on a Linux host), so I hope I found everything that needs to be changed.

Differential Revision: https://reviews.llvm.org/D142862
2023-02-07 22:55:41 -05:00
Mariusz Borsa
82d852c69f [Sanitizers] Fix read buffer overrun in scanning loader commands
The fix only affects Darwin, but to write the test I had to modify
the MemoryMappingLayout class which is used by all OSes,
to allow for mocking of image header (this change should be NFC). Hence no [Darwin] in the subject
so I can get more eyes on it.

While looking for a memory gap to put the shadow area into, the sanitizer code
scans through the loaded images, and for each image it scans through its
loader command to determine the occupied memory ranges.

While doing so, if the 'segment load' (kLCSegment) loader comand is encountered, the command scanning function
returns success (true), but does not decrement the command list iterator counter.
The result is that the function is called again and again, with the iterator counter
now being too high. The command scanner keeps updating the loader command pointer,
by using the command size field.

If the loop counter is too high, the command pointer
lands into unintended area ( beyond
+sizeof(mac_header64)+header->sizeofcmds ),
and result depends on the random content found there.

The random content interpreted as loader command might contain a large integer value in the
cmdsize field - this value is added to the current loader command pointer,
which might now point to an inaccessible memory address. It can occasionally result
in a crash if it happens to run beyond the mapped memory segment.

Note that when the area after the loader command list
contains zeros or small integers only, the loop will end normally and the problem
will go unnoticed. So it happened until now since having a some big value
after the header area, falling into command size field is a pretty rare situation.

The fix makes sure that the iterator counter gets updated when the segment load (kLCSegment)
loader command is found too, and in the same code location so the updates will always go together.

Undo the changes in the sanitizer_procmaps_mac.cpp to see the test failing.

rdar://101161047
rdar://102819707

Differential Revision: https://reviews.llvm.org/D142164
2023-02-07 15:21:15 -08:00
Kirill Stoimenov
9de144eee5 [HWASAN] Remove GetRequestedSize check from Metadata::IsAllocated
After D143438 there is no point in this check as the size will never 0. Also this keeps it consistent with ASAN where there is not size check in LsanMetadata::allocated.

Reviewed By: MaskRay, vitalybuka

Differential Revision: https://reviews.llvm.org/D143442
2023-02-07 15:51:56 +00:00
Kirill Stoimenov
e403fd8705 [HWASAN] Add unlikely to if in HwasanAllocate. 2023-02-07 00:39:17 +00:00
Kirill Stoimenov
01176191d2 [HWASAN] Modify HwasanAllocate to set the size to 1 if requested size is 0
This should keep it consistent with LSAN and ASAN,

Reviewed By: vitalybuka, MaskRay

Differential Revision: https://reviews.llvm.org/D143438
2023-02-07 00:35:56 +00:00
Kirill Stoimenov
c6ea5b0cd1 [HWASAN] Modify HwasanAllocate to set the size to 1 if requested size is 0
This should keep it consistent with LSAN and ASAN,

Reviewed By: vitalybuka, MaskRay

Differential Revision: https://reviews.llvm.org/D143438
2023-02-07 00:01:20 +00:00
Kirill Stoimenov
914f86949a [HWASAN] Fix Metadata::IsAllocatedMetadata::IsAllocated to return true even if the requested size is 0.
Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D143438
2023-02-06 23:47:23 +00:00
Fangrui Song
39b8a27132 [sanitizer] Simplify with GET_CALLER_PC_BP. NFC 2023-02-04 11:30:14 -08:00
Douglas Yung
056769cdbc Revert "[Sanitizers] Fix read buffer overrun in scanning loader commands"
This reverts commit abbd4da2043856f443e3d1c8d2c7627cac93a6ac.

This change is breaking many bots including:
- http://45.33.8.238/linux/98629/step_10.txt
- https://buildkite.com/llvm-project/llvm-main/builds/6461#01861c4f-9d9c-4781-88f7-d6ccddcb4b06/919-8848
- https://lab.llvm.org/buildbot/#/builders/94/builds/13196
- https://lab.llvm.org/buildbot/#/builders/45/builds/10633
- https://lab.llvm.org/buildbot/#/builders/247/builds/1238
- https://lab.llvm.org/buildbot/#/builders/70/builds/33424
- https://lab.llvm.org/buildbot/#/builders/168/builds/11693
- https://lab.llvm.org/buildbot/#/builders/74/builds/17006
- https://lab.llvm.org/buildbot/#/builders/85/builds/14120
2023-02-04 10:19:57 -08:00
Sam James
ca50897a76
[compiler-rt] Fix FORTIFY_SOURCE -> _FORTIFY_SOURCE reference (NFC)
As pointed out by maskray.

Fixes: 8ab762557fb057af1a3015211ee116a975027e78
2023-02-04 18:05:26 +00:00
Sam James
8ab762557f [compiler-rt] Disable default config files for tests
Without this, if hardening measures like FORTIFY_SOURCE are are in
/etc/clang/*.cfg, many sanitizer tests will die before the sanitizer
can trap the problem being tested, because e.g. the _chk variants
of common functions will abort first.

This gets the number of failing tests down from 42->3 for me (and the
remaining 3 are unrelated).

See: 52ce6776cf98e993c6ec04ae54b52e1354fff917
See: 136f77805fd89cd30e69b3d1204fbf7efedd9a12
Closes: https://github.com/llvm/llvm-project/issues/60394

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D143322
2023-02-04 18:04:23 +00:00
Mariusz Borsa
abbd4da204 [Sanitizers] Fix read buffer overrun in scanning loader commands
The fix only affects Darwin, but to write the test I had to modify
the MemoryMappingLayout class which is used by all OSes,
to allow for mocking of image header (this change should be NFC). Hence no [Darwin] in the subject
so I can get more eyes on it.

While looking for a memory gap to put the shadow area into, the sanitizer code
scans through the loaded images, and for each image it scans through its
loader command to determine the occupied memory ranges.

While doing so, if the 'segment load' (kLCSegment) loader comand is encountered, the command scanning function
returns success (true), but does not decrement the command list iterator counter.
The result is that the function is called again and again, with the iterator counter
now being too high. The command scanner keeps updating the loader command pointer,
by using the command size field.

If the loop counter is too high, the command pointer
lands into unintended area ( beyond <header addr>+sizeof(mac_header64)+header->sizeofcmds ),
and result depends on the random content found there.

The random content interpreted as loader command might contain a large integer value in the
cmdsize field - this value is added to the current loader command pointer,
which might now point to an inaccessible memory address. It can occasionally result
in a crash if it happens to run beyond the mapped memory segment.

Note that when the area after the loader command list
contains zeros or small integers only, the loop will end normally and the problem
will go unnoticed. So it happened until now since having a some big value
after the header area, falling into command size field is a pretty rare situation.

The fix makes sure that the iterator counter gets updated when the segment load (kLCSegment)
loader command is found too, and in the same code location so the updates will always go together.

Undo the changes in the sanitizer_procmaps_mac.cpp to see the test failing.

rdar://101161047
rdar://102819707

Differential Revision: https://reviews.llvm.org/D142164
2023-02-03 18:43:33 -08:00
Khem Raj
26800a2c7e [sanitizer] Undef _TIME_BITS along with _FILE_OFFSET_BITS on Linux
On 32-bit glibc>=2.34 systems using 64bit time_t build fails because
_FILE_OFFSET_BITS is undefined here but _TIME_BITS is still set to 64

Fixes

```
/usr/include/features-time64.h:26:5: error: "_TIME_BITS=64 is allowed
 only with _FILE_OFFSET_BITS=64"
| #   error "_TIME_BITS=64 is allowed only with _FILE_OFFSET_BITS=64"
|     ^
| 1 error generated.
```

Reviewed By: thesamesam, MaskRay

Differential Revision: https://reviews.llvm.org/D140812
2023-02-03 11:48:35 -08:00
Marco Elver
960b4c3b5d [SanitizerBinaryMetadata] Treat constant globals and non-escaping addresses specially
For atomics metadata, we can make data race analysis more efficient by
entirely ignoring functions that include memory accesses but which only
access non-escaping (non-shared) and/or non-mutable memory. Such
functions will not be considered to be covered by "atomics" metadata,
resulting in the following benefits:

  1. reduces "covered" metadata; and
  2. allows data race analysis to skip such functions.

Reviewed By: dvyukov

Differential Revision: https://reviews.llvm.org/D143159
2023-02-03 15:35:24 +01:00
Mitch Phillips
10939d1d58 [GWP-ASan] Remove thread clamping in tests.
It's better and easier for us to just have threads contend against each
other in the tests if it's more than the maximum supported number of
hardware threads available.

Specifically, the recoverable test fails on Android because the
GTEST_SKIP in a called function, and it only properly works from the
TEST_* harness function. Android tests run on cuttlefish, which can be a
single core with two hyperthreads.

Reviewed By: fmayer

Differential Revision: https://reviews.llvm.org/D143221
2023-02-02 15:40:07 -08:00
Kirill Stoimenov
b2aa0a4650 [LSAN] Enable more tests which are passing as is in HWASAN.
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D143126
2023-02-02 18:06:35 +00:00
Kirill Stoimenov
042f01b289 Revert "[LSAN] Enable more tests which are passing as is in HWASAN."
This reverts commit b4abbf17572dce3993402f2e00e72678518ef6e1.
2023-02-02 17:12:31 +00:00
Kirill Stoimenov
b4abbf1757 [LSAN] Enable more tests which are passing as is in HWASAN.
Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D143126
2023-02-02 16:39:26 +00:00