787 Commits

Author SHA1 Message Date
ChiaHungDuan
bab0507ff2
[scudo] Add EnableContiguousRegions mode (#85149)
This releases the requirement that we need to preserve the memory for
all regions at the beginning. It needs a huge amount of contiguous pages
and which may be a challenge in certain cases. Therefore, adding a new
flag, EnableContiguousRegions, to indicate whether we want to allocate
all the regions next to each other.

Note that once the EnableContiguousRegions is disabled,
EnableRandomOffset becomes irrelevant because the base of each region is
already random.
2024-04-09 09:30:11 -07:00
Christopher Ferris
3b43ae9a68
[scudo] Remove end of line checks. (#88022)
The regex to verify that there is nothing else at the end of the line
doesn't work in all cases, so remove it.
2024-04-08 13:08:35 -07:00
Christopher Ferris
0a39f1a7e5
[scudo] Add errno description to mmap failure. (#87713)
Added unit tests for all of the linux report error functions.
2024-04-05 14:25:39 -07:00
Christopher Ferris
ed6edf262d
[scudo] Change isPowerOfTwo macro to return false for zero. (#87120)
Clean-up all of the calls and remove the redundant == 0 checks.

There is only one small visible change. For non-Android, the memalign
function will now fail if alignment is zero. Before this would have
passed.
2024-04-01 13:35:29 -07:00
ChiaHungDuan
92d0d6f6cb
[scudo] Do a M_PURGE call before changing release interval on Android (#87110) 2024-04-01 10:09:40 -07:00
Christopher Ferris
c0a3c5c81f
[scudo] Change tests that use setrlimit to cause mmap to fail. (#87004)
It appears that qemu does not actually cause mmap to fail when calling
setrlimit to limit the address space size. In the two tests that use
setrlimit, detect if mmap still works and skip the tests in that case.

Since all Android targets should support setrlimit, compile out the mmap
check code for them.
2024-03-29 14:19:10 -07:00
Christopher Ferris
0dbd804a69
[scudo] Only init RingBuffer when needed. (#85994)
Only attempt to initialize the ring buffer when tracking is enabled.

Updated unit tests, and added a few new unit tests to verify the
RingBuffer is not initialized by default.

Verified that the two maps associated with the RingBuffer are not
created in processes by default.
2024-03-29 09:44:17 -07:00
Christopher Ferris
7a87902684
[scudo] Fix stack depot validation. (#87024)
In the StackDepot::isValid function, there is work to validate the
TabMask variable. Unfortunately, if TabMask is set to the maximum
allowed value, TabSize = TabMask + 1 becomes zero and validation passes.

Disallow that case to prevent invalid reads into the Tab structure.
2024-03-28 17:35:46 -07:00
ChiaHungDuan
6b149f70ab
[scudo] Dump ReleaseToOsIntervalMs (#86887)
This can be adjusted during runtime and it may impact the memory
footprint if it's set to a big value or is disabled.
2024-03-28 15:28:01 -07:00
Amy Kwan
685d7855ac Fix the -Wmissing-designated-field-initializers on the clang-ppc64le-rhel bot 2024-03-27 14:42:01 -05:00
ChiaHungDuan
f1ac559534
Revert "[scudo] Use getMonotonicTimeFast for tryLock." (#86590)
This reverts commit 36ca9a29025a2f678096e9545fa2ec44e8432592.

We were using the `time` as the seed while choosing a new TSD. To make
the access of TSDs evenly distributed, we require a higher precision in
`time`. Otherwise, many threads may result in having the same random
access pattern on TSDs because they share the same `time` in certain
period. On Linux, CLOCK_MONOTONIC_COARSE usually adopts 4 ms precision.
This is way higher than the average accessing time of TSD (which is
usually less than 1 us). As a result, when multiple threads try to
select a new TSD in a 4 ms interval, they share the same `time` seed and
end up choosing and congesting on the same TSD.
2024-03-27 11:30:08 -07:00
Simon Pilgrim
fca48312a8 Fix signed/unsigned comparison warning. NFC. 2024-03-27 16:36:07 +00:00
Christopher Ferris
1949f7d6c9
[scudo] Clean up string handling (#86364)
Do not abort if a vector cannot increase its own capacity. In that case,
push_back calls silently fail.

Modify the ScopedString implementation so that it no longer requires two
passes to do the format. Move the helper functions to be private member
functions so that they can use push_back directly. This allows the
capacity to be increased under the hood and/or silently discards data if
the capacity is exceeded and cannot be increased.

Add new tests for the Vector and ScopedString for capacity increase
failures.

Doing this so that if a map call fails, and we are attempting to write
an error string, we can still get some of the message dumped. This also
avoids crashing in Scudo code, and makes the caller handle any failures.
2024-03-26 14:47:48 -07:00
ChiaHungDuan
aa8cffb958
[scudo] Fix type mismatch on DefaultMaxEntrySize (#85897) 2024-03-20 08:15:47 -07:00
ChiaHungDuan
2dc9ec47fb
[scudo] Refactor allocator config to support optional flags (#81805)
Instead of explicitly disabling a feature by declaring the variable and
set it to false, this change supports the optional flags. I.e., you can
skip certain flags if you are not using it.

This optional feature supports both forms,
  1. Value: A parameter for a feature. E.g., EnableRandomOffset
  2. Type: A C++ type implementing a feature. E.g., ConditionVariableT

On the other hand, to access the flags will be through one of the
wrappers, BaseConfig/PrimaryConfig/SecondaryConfig/CacheConfig
(CacheConfig is embedded in SecondaryConfig). These wrappers have the
getters to access the value and the type. When adding a new feature, we
need to add it to `allocator_config.def` and mark the new variable with
either *_REQUIRED_* or *_OPTIONAL_* macro so that the accessor will be
generated properly.

In addition, also remove the need of `UseConditionVariable` to flip
on/off of condition variable. Now we only need to define the type of
condition variable.
2024-03-13 16:05:24 -07:00
Alexander Richardson
55b90b5140
[compiler-rt] Remove llvm_gtest dependency from unit tests
All these unit tests already include ${COMPILER_RT_GTEST_SOURCE} as an
input source file and the target llvm_gtest does not exist for
standalone builds. Currently the DEPS argument is ignored for standalone
builds so the missing target is not a problem, but as part of fixing a
build race for standalone builds I am planning to include those
dependencies in COMPILER_RT_TEST_STANDALONE_BUILD_LIBS configurations.

Reviewed By: vitalybuka

Pull Request: https://github.com/llvm/llvm-project/pull/83649
2024-03-13 11:28:44 -07:00
Florian Mayer
337a200715
[NFC] [scudo] Move static_assert to class it concerns (#84245) 2024-03-11 11:47:59 -07:00
Florian Mayer
b4e0890458
[NFC] [scudo] move static_assert closer to class it relates to (#84257)
delete other static_assert
2024-03-11 11:46:45 -07:00
Florian Mayer
1cf428a05a
Skip MemtagBasicDeathTest#Unsupported when running with HWASan (#84243)
Tested in AOSP.
2024-03-08 13:46:08 -08:00
Florian Mayer
8acef12030
[NFC] [scudo] remove DCHECK (#84255)
this gets checked in StackDepot::init anyway
2024-03-06 15:45:49 -08:00
WÁNG Xuěruì
597f9761c3
[scudo] Add support for LoongArch hardware CRC32 checksumming (#83113)
One has to probe for platform capability prior to use with HWCAP,
according to LoongArch documentation.
2024-03-02 13:58:18 +08:00
Andrei Homescu
bf0f874e48
[scudo] Avoid splitting aligned allocations on Trusty (#69281)
Don't use multiple tagged pages at the beginning of an allocation, since
it prevents using such allocations for memrefs, and mappings aren't
reused anyway since Trusty uses MapAllocatorNoCache.
Upstreamed from https://r.android.com/2537251.

Co-authored-by: Marco Nelissen <marcone@google.com>
2024-02-29 15:05:47 -08:00
ChiaHungDuan
f83f7128b3
[scudo][NFC] Explicit type casting to avoid compiler warning (#83355) 2024-02-28 16:15:31 -08:00
ChiaHungDuan
1a7776abe6
Reland "[scudo] Store more blocks in each TransferBatch" (#83078) (#83081)
This reverts commit 056d62be38c5db3d8332ac300c4ff29214126697.

Fixed the number of bytes copied in moveNToArray()
2024-02-28 12:09:49 -08:00
ChiaHungDuan
056d62be38
Revert "[scudo] Store more blocks in each TransferBatch" (#83078)
Reverts llvm/llvm-project#70390

There's a bug caught by
`ScudoCombinedTestReallocateInPlaceStress_DefaultConfig.ReallocateInPlaceStress`
with gwp asan. It's an easy fix but given that this is a major change, I
would like to revert it first
2024-02-26 15:21:32 -08:00
Fabio D'Urso
cda413087c
[scudo] Do not unmap the memory containing the this object in unmapRingBuffer (#83034) 2024-02-27 00:00:20 +01:00
ChiaHungDuan
1865c7ea85
[scudo] Store more blocks in each TransferBatch (#70390)
Instead of always storing the same number of blocks as cached, we prefer
increasing the utilization by saving more blocks in a single
TransferBatch. This may slightly impact the performance, but it will
save a lot of memory used by BatchClassId (especially for larger
blocks).
2024-02-26 11:04:08 -08:00
Florian Mayer
6dd6d487d0
[NFC] Make RingBuffer an atomic pointer (#82547)
This will allow us to atomically swap out RingBuffer and StackDepot.

Patched into AOSP and ran debuggerd_tests.
2024-02-23 11:28:20 -08:00
Florian Mayer
6ddb25ed9c
[scudo] increase frames per stack to 16 for stack depot (#82427)
8 was very low and it is likely that in real workloads we have more than
an average of 8 frames per stack given on Android we have 3 at the
bottom: __start_main, __libc_init, main, and three at the top: malloc,
scudo_malloc and Allocator::allocate. That leaves 2 frames for
application code, which is clearly unreasonable.
2024-02-22 11:19:02 -08:00
Florian Mayer
d17eade22a
Do not call disable / enable on null depot (#82542)
depot can be null if allocation_ring_buffer_size=0
2024-02-21 14:28:34 -08:00
Florian Mayer
b366643ca3 [NFC] fix typo 2024-02-16 23:26:49 -08:00
Florian Mayer
133b6a088e [NFC] fix type mismatch in test. 2024-02-16 23:22:19 -08:00
Florian Mayer
3da0166331 Reland^2 "[scudo] resize stack depot for allocation ring buffer"
Fix some warnings by matching types.

This reverts commit e1164d063558b1e89f20109d83c079caae1825d8.
2024-02-16 22:59:53 -08:00
Florian Mayer
e1164d0635
Revert "Reland "[scudo] resize stack depot for allocation ring buffer"" (#82088)
Reverts llvm/llvm-project#81028
2024-02-16 17:58:54 -08:00
Florian Mayer
aff6cb4957
Reland "[scudo] resize stack depot for allocation ring buffer" (#81028)
First commit of the stack is a clean reland, second is  the fix.

There was a typo in the `static_assert` that meant we were asserting the
size of the pointer, not the struct.

Also changed `alignas` to be more intuitive, but that is NFC.

Ran builds in Android here: https://r.android.com/2954411
2024-02-16 17:10:54 -08:00
Fabio D'Urso
cc673867d9
[scudo] Add ALWAYS_INLINE to ScopedTSD ctor (#81982)
Fix for performance regression introduced by #80061 that slowed
down Fuchsia's MallocFree microbenchmark by 3.5 - 8%
2024-02-16 19:48:07 +01:00
Florian Mayer
c3291253c3
Revert "[scudo] [MTE] resize stack depot for allocation ring buffer" (#80777)
Reverts llvm/llvm-project#74515

Broke build: https://lab.llvm.org/buildbot/#/builders/75/builds/42512
2024-02-05 16:56:39 -08:00
Florian Mayer
eff77d8456
[scudo] [MTE] resize stack depot for allocation ring buffer (#74515)
Co-authored-by: ChiaHungDuan <f103119@gmail.com>
2024-02-05 16:47:02 -08:00
ChiaHungDuan
8ce036d539
[scudo] Add ScopedTSD to avoid releasing TSD manually (#80061)
This makes the use of TSD be RAII style and avoid the exposing of the
type of TSDs.

Also move some thread safety analyses from static to runtime because of
its limitation. Even we mark some code path as NO_THREAD_SAFETY_ANALYSIS
but we still have the `assertLocked()` cover the correctness.
2024-02-05 14:36:04 -08:00
Evgenii Stepanov
c82f3caf56
[scudo] Add StackDepot lock to enable/disable. (#79670)
Scudo grabs all allocator locks in a pthread_atfork before the fork, and releases them after. This allows malloc to be used in a fork child of a multithreaded process, which is expressly forbidden by the standard, but very widely used. For example, Android's init uses std::string after fork when spawning services in android::init::EnterNamespaces and other places.

Any lock that is necessary to serve an allocator call must be handled this way. Otherwise there is a possibility that the lock is held during the call to fork, which results in it being held forever in the child process, and the next operation that needs it deadlocks.
2024-01-29 14:22:24 -08:00
ChiaHungDuan
e6c2952eb5
[scudo] Condition variable can be disabled by setting the flag to off (#77532)
To enable the condition variable, you have to define both
UseConditionVariable and the ConditionVariableT. Otherwise, it'll be
disabled. However, you may want to disable the condition variable by
setting UseConditionVariable=false, for example, while measuring the
performance and you want to turn it off temporarily. Instead of
requiring the removal of the variable, examining its value makes more
sense.
2024-01-10 11:03:28 -08:00
Florian Mayer
4f9ad0f856 [scudo] [NFC] remove unused method 2023-12-18 18:54:32 -08:00
Florian Mayer
fd8e854a86
[scudo] simplify flag parser out of bounds logic (#72371)
almost NFC, just that now we accept INT_MIN and INT_MAX

as discussed in https://r.android.com/2831100, but I didn't add the
*ValueEnd != Value check because I want to keep this change
behaviour-keeping.
2023-12-14 16:35:58 -08:00
Christopher Ferris
a8ef9c0969
[scudo] Add utilization percentages for stats. (#75101)
Refactor the percentage display in the secondary code. Re-use that to
display a utilization percentage when displaying fragmentation data.
2023-12-11 16:42:44 -08:00
Florian Mayer
a5bdc4a460
[scudo] do not store size inside ring buffer (#74541) 2023-12-08 17:16:56 -08:00
ChiaHungDuan
58c2a4e806
[scudo] Add hooks to mark the range of realloc (#74353)
`realloc` may involve both allocation and deallocation. Given that the
reporting the events is not atomic and which may lead the hook user to a
false case that the double-use pattern happens. In general, this can be
resolved on the hook side. To alleviate the task of handling it, we add
two new hooks to mark the range so that the hook user can combine those
calls together.
2023-12-07 13:49:06 -08:00
Florian Mayer
e68c265543
[scudo] Add parameters for ring buffer and stack depot sizes (#74539)
These will be used in follow-up CLs, committing this separately because
it needs a matching change in AOSP. This way we can avoid complicated
multi-repo rollbacks if something is wrong with the follow up CLs.
2023-12-05 16:12:16 -08:00
Evgenii Stepanov
54c30953b9
Do not initialize the allocator on free(nullptr). (#74366)
free(nullptr) is guaranteed by ISO and POSIX to be a no-op, we should not pay for the overhead of maybeInit() in this case.

Additionally, Bionic calls free(nullptr) before the allocator settings are finalized.
Scudo should not run allocator initialization at that time. Doing so
causes various bad things to happen, like mapping primary regions with
the wrong PROT_MTE setting.
2023-12-04 13:45:37 -08:00
ChiaHungDuan
75867f8e4a
[scudo] Fix realloc hooks behavior (#74149)
`realloc` may involve both allocation and deallocation. Given that the
reporting the events is not atomic and which may lead the hook user to a
false case that the double-use pattern happens, we always report the old
pointer is released and report the new allocation afterward (even it's
the same pointer).

This also fixes that we didn't report the new size when it doesn't need
to allocate a new space.
2023-12-01 14:41:43 -08:00
Florian Mayer
8aed91631d [NFC] remove static asserts 2023-11-14 15:27:40 -08:00