summaryrefslogtreecommitdiffstats
path: root/compiler-rt/lib/scudo/standalone/combined.h
Commit message (Collapse)AuthorAgeFilesLines
* [scudo][standalone] Fork supportKostya Kortchinsky2020-01-141-2/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: fork() wasn't well (or at all) supported in Scudo. This materialized in deadlocks in children. In order to properly support fork, we will lock the allocator pre-fork and unlock it post-fork in parent and child. This is done via a `pthread_atfork` call installing the necessary handlers. A couple of things suck here: this function allocates - so this has to be done post initialization as our init path is not reentrance, and it doesn't allow for an extra pointer - so we can't pass the allocator we are currently working with. In order to work around this, I added a post-init template parameter that gets executed once the allocator is initialized for the current thread. Its job for the C wrappers is to install the atfork handlers. I reorganized a bit the impacted area and added some tests, courtesy of cferris@ that were deadlocking prior to this fix. Subscribers: jfb, #sanitizers, llvm-commits Tags: #sanitizers, #llvm Differential Revision: https://reviews.llvm.org/D72470
* [scudo][standalone] Implement TSD registry disablingKostya Kortchinsky2019-12-201-6/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: In order to implement `malloc_{enable|disable}` we were just disabling (or really locking) the Primary and the Secondary. That meant that allocations could still be serviced from the TSD as long as the cache wouldn't have to be filled from the Primary. This wasn't working out for Android tests, so this change implements registry disabling (eg: locking) so that `getTSDAndLock` doesn't return a TSD if the allocator is disabled. This also means that the Primary doesn't have to be disabled in this situation. For the Shared Registry, we loop through all the TSDs and lock them. For the Exclusive Registry, we add a `Disabled` boolean to the Registry that forces `getTSDAndLock` to use the Fallback TSD instead of the thread local one. Disabling the Registry is then done by locking the Fallback TSD and setting the boolean in question (I don't think this needed an atomic variable but I might be wrong). I clang-formatted the whole thing as usual hence the couple of extra whiteline changes in this CL. Reviewers: cferris, pcc, hctim, morehouse, eugenis Subscribers: jfb, #sanitizers, llvm-commits Tags: #sanitizers, #llvm Differential Revision: https://reviews.llvm.org/D71719
* [Scudo] [GWP-ASan] Add GWP-ASan to Scudo Standalone.Mitch Phillips2019-12-131-0/+63
| | | | | | | | | | | | | | | | Summary: Adds GWP-ASan to Scudo standalone. Default parameters are pulled across from the GWP-ASan build. No backtrace support as of yet. Reviewers: cryptoad, eugenis, pcc Reviewed By: cryptoad Subscribers: merge_guards_bot, mgorny, #sanitizers, llvm-commits, cferris, vlad.tsyrklevich, pcc Tags: #sanitizers, #llvm Differential Revision: https://reviews.llvm.org/D71229
* scudo: Tweak how we align UserPtr. NFCI.Peter Collingbourne2019-12-101-5/+6
| | | | | | | | | | | | Instead of testing whether the pointer is aligned, just align it unconditionally and compare it to the original pointer. This moves the computation of UserPtr up to before we start preparing the header, so that the memory tagging code will be able to read the original header containing the bounds of the previous allocation before it gets potentially clobbered by the pointer realignment code. Differential Revision: https://reviews.llvm.org/D71292
* scudo: Move getChunkFromBlock() allocated check into caller. NFCI.Peter Collingbourne2019-12-101-17/+9
| | | | | | | | With tag-on-free we will need to get the chunk of a deallocated block. Change getChunkFromBlock() so that it doesn't check that the chunk is allocated, and move the check into the caller, so that it can be reused for this purpose. Differential Revision: https://reviews.llvm.org/D71291
* [scudo][standalone] Add chunk ownership functionKostya Kortchinsky2019-12-031-0/+15
| | | | | | | | | | | | | | | | | | | | | | | | Summary: In order to be compliant with tcmalloc's extension ownership determination function, we have to expose a function that will say if a chunk was allocated by us. As to whether or not this has security consequences: someone able to call this function repeatedly could use it to determine secrets (cookie) or craft a valid header. So this should not be exposed directly to untrusted user input. Add related tests. Additionally clang-format caught a few things to change. Reviewers: hctim, pcc, cferris, eugenis, vitalybuka Subscribers: JDevlieghere, jfb, #sanitizers, llvm-commits Tags: #sanitizers, #llvm Differential Revision: https://reviews.llvm.org/D70908
* scudo: Replace a couple of macros with their expansions.Peter Collingbourne2019-11-271-3/+3
| | | | | | | | | The macros INLINE and COMPILER_CHECK always expand to the same thing (inline and static_assert respectively). Both expansions are standards compliant C++ and are used consistently in the rest of LLVM, so let's improve consistency with the rest of LLVM by replacing them with the expansions. Differential Revision: https://reviews.llvm.org/D70793
* [scudo][standalone] Fix for releaseToOS prior to initKostya Kortchinsky2019-11-251-1/+4
| | | | | | | | | | | | | | | | | | | | Summary: cferris@ found an issue where calling `releaseToOS` prior to any other heap operation would lead to a crash, due to the allocator not being properly initialized (it was discovered via `mallopt`). The fix is to call `initThreadMaybe` prior to calling `releaseToOS` for the Primary. Add a test that crashes prior to fix. Reviewers: hctim, cferris, pcc, eugenis Subscribers: #sanitizers, llvm-commits Tags: #sanitizers, #llvm Differential Revision: https://reviews.llvm.org/D70552
* [scudo][standalone] Minor optimization & improvementsKostya Kortchinsky2019-11-211-5/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: A few small improvements and optimizations: - when refilling the free list, push back the last batch and return the front one: this allows to keep the allocations towards the front of the region; - instead of using 48 entries in the shuffle array, use a multiple of `MaxNumCached`; - make the maximum number of batches to create on refil a constant; ultimately it should be configurable, but that's for later; - `initCache` doesn't need to zero out the cache, it's already done. - it turns out that when using `||` or `&&`, the compiler is adamant on adding a short circuit for every part of the expression. Which ends up making somewhat annoying asm with lots of test and conditional jump. I am changing that to bitwise `|` or `&` in two place so that the generated code looks better. Added comments since it might feel weird to people. This yields to some small performance gains overall, nothing drastic though. Reviewers: hctim, morehouse, cferris, eugenis Subscribers: #sanitizers, llvm-commits Tags: #sanitizers, #llvm Differential Revision: https://reviews.llvm.org/D70452
* [scudo][standalone] Enabled SCUDO_DEBUG for tests + fixesKostya Kortchinsky2019-11-151-1/+1
| | | | | | | | | | | | | | | | | | | Summary: `SCUDO_DEBUG` was not enabled for unit tests, meaning the `DCHECK`s were never tripped. While turning this on, I discovered that a few of those not-exercised checks were actually wrong. This CL addresses those incorrect checks. Not that to work in tests `CHECK_IMPL` has to explicitely use the `scudo` namespace. Also changes a C cast to a C++ cast. Reviewers: hctim, pcc, cferris, eugenis, vitalybuka Subscribers: mgorny, #sanitizers, llvm-commits Tags: #sanitizers, #llvm Differential Revision: https://reviews.llvm.org/D70276
* [scudo][standalone] Fix Secondary bug w/ freelistKostya Kortchinsky2019-10-311-2/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: cferris@ found an issue due to the new Secondary free list behavior and unfortunately it's completely my fault. The issue is twofold: - I lost track of the (major) fact that the Combined assumes that all chunks returned by the Secondary are zero'd out apprioriately when dealing with `ZeroContents`. With the introduction of the freelist, it's no longer the case as there can be a small portion of memory between the header and the next page boundary that is left untouched (the rest is zero'd via release). So the next time that block is returned, it's not fully zero'd out. - There was no test that would exercise that behavior :( There are several ways to fix this, the one I chose makes the most sense to me: we pass `ZeroContents` to the Secondary's `allocate` and it zero's out the block if requested and it's coming from the freelist. The prevents an extraneous `memset` in case the block comes from `map`. Another possbility could have been to `memset` in `deallocate`, but it's probably overzealous as all secondary blocks don't need to be zero'd out. Add a test that would have found the issue prior to fix. Reviewers: morehouse, hctim, cferris, pcc, eugenis, vitalybuka Subscribers: #sanitizers, llvm-commits Tags: #sanitizers, #llvm Differential Revision: https://reviews.llvm.org/D69675
* [scudo][standalone] Add a free list to the SecondaryKostya Kortchinsky2019-10-301-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: The secondary allocator is slow, because we map and unmap each block on allocation and deallocation. While I really like the security benefits of such a behavior, this yields very disappointing performance numbers on Android for larger allocation benchmarks. So this change adds a free list to the secondary, that will hold recently deallocated chunks, and (currently) release the extraneous memory. This allows to save on some memory mapping operations on allocation and deallocation. I do not think that this lowers the security of the secondary, but can increase the memory footprint a little bit (RSS & VA). The maximum number of blocks the free list can hold is templatable, `0U` meaning that we fallback to the old behavior. The higher that number, the higher the extra memory footprint. I added default configurations for all our platforms, but they are likely to change in the near future based on needs and feedback. Reviewers: hctim, morehouse, cferris, pcc, eugenis, vitalybuka Subscribers: mgorny, #sanitizers, llvm-commits Tags: #sanitizers, #llvm Differential Revision: https://reviews.llvm.org/D69570
* [scudo][standalone] Get statistics in a char bufferKostya Kortchinsky2019-10-091-3/+29
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Following up on D68471, this CL introduces some `getStats` APIs to gather statistics in char buffers (`ScopedString` really) instead of printing them out right away. Ultimately `printStats` will just output the buffer, but that allows us to potentially do some work on the intermediate buffer, and can be used for a `mallocz` type of functionality. This allows us to pretty much get rid of all the `Printf` calls around, but I am keeping the function in for debugging purposes. This changes the existing tests to use the new APIs when required. I will add new tests as suggested in D68471 in another CL. Reviewers: morehouse, hctim, vitalybuka, eugenis, cferris Reviewed By: morehouse Subscribers: delcypher, #sanitizers, llvm-commits Tags: #llvm, #sanitizers Differential Revision: https://reviews.llvm.org/D68653 llvm-svn: 374173
* [scudo][standalone] Android related improvementsKostya Kortchinsky2019-09-111-10/+23
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: This changes a few things to improve memory footprint and performances on Android, and fixes a test compilation error: - add `stdlib.h` to `wrappers_c_test.cc` to address https://bugs.llvm.org/show_bug.cgi?id=42810 - change Android size class maps, based on benchmarks, to improve performances and lower the Svelte memory footprint. Also change the 32-bit region size for said configuration - change the `reallocate` logic to reallocate in place for sizes larger than the original chunk size, when they still fit in the same block. This addresses patterns from `memory_replay` dumps like the following: ``` 202: realloc 0xb48fd000 0xb4930650 12352 202: realloc 0xb48fd000 0xb48fd000 12420 202: realloc 0xb48fd000 0xb48fd000 12492 202: realloc 0xb48fd000 0xb48fd000 12564 202: realloc 0xb48fd000 0xb48fd000 12636 202: realloc 0xb48fd000 0xb48fd000 12708 202: realloc 0xb48fd000 0xb48fd000 12780 202: realloc 0xb48fd000 0xb48fd000 12852 202: realloc 0xb48fd000 0xb48fd000 12924 202: realloc 0xb48fd000 0xb48fd000 12996 202: realloc 0xb48fd000 0xb48fd000 13068 202: realloc 0xb48fd000 0xb48fd000 13140 202: realloc 0xb48fd000 0xb48fd000 13212 202: realloc 0xb48fd000 0xb48fd000 13284 202: realloc 0xb48fd000 0xb48fd000 13356 202: realloc 0xb48fd000 0xb48fd000 13428 202: realloc 0xb48fd000 0xb48fd000 13500 202: realloc 0xb48fd000 0xb48fd000 13572 202: realloc 0xb48fd000 0xb48fd000 13644 202: realloc 0xb48fd000 0xb48fd000 13716 202: realloc 0xb48fd000 0xb48fd000 13788 ... ``` In this situation we were deallocating the old chunk, and allocating a new one for every single one of those, but now we can keep the same chunk (we just updated the header), which saves some heap operations. Reviewers: hctim, morehouse, vitalybuka, eugenis, cferris, rengolin Reviewed By: morehouse Subscribers: srhines, delcypher, #sanitizers, llvm-commits Tags: #llvm, #sanitizers Differential Revision: https://reviews.llvm.org/D67293 llvm-svn: 371628
* [scudo][standalone] Fix malloc_iterateKostya Kortchinsky2019-08-201-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: cferris's Bionic tests found an issue in Scudo's `malloc_iterate`. We were inclusive of both boundaries, which resulted in a `Block` that was located on said boundary to be possibly accounted for twice, or just being accounted for while iterating on regions that are not ours (usually the unmapped ones in between Primary regions). The fix is to exclude the upper boundary in `iterateOverChunks`, and add a regression test. This additionally corrects a typo in a comment, and change the 64-bit Primary iteration function to not assume that `BatchClassId` is 0. Reviewers: cferris, morehouse, hctim, vitalybuka, eugenis Reviewed By: hctim Subscribers: delcypher, #sanitizers, llvm-commits Tags: #llvm, #sanitizers Differential Revision: https://reviews.llvm.org/D66231 llvm-svn: 369400
* [scudo][standalone] Optimization passKostya Kortchinsky2019-07-241-33/+33
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: This introduces a bunch of small optimizations with the purpose of making the fastpath tighter: - tag more conditions as `LIKELY`/`UNLIKELY`: as a rule of thumb we consider that every operation related to the secondary is unlikely - attempt to reduce the number of potentially extraneous instructions - reorganize the `Chunk` header to not straddle a word boundary and use more appropriate types Note that some `LIKELY`/`UNLIKELY` impact might be less obvious as they are in slow paths (for example in `secondary.cc`), but at this point I am throwing a pretty wide net, and it's consistant and doesn't hurt. This was mosly done for the benfit of Android, but other platforms benefit from it too. An aarch64 Android benchmark gives: - before: ``` BM_youtube/min_time:15.000/repeats:4/manual_time_mean 445244 us 659385 us 4 BM_youtube/min_time:15.000/repeats:4/manual_time_median 445007 us 658970 us 4 BM_youtube/min_time:15.000/repeats:4/manual_time_stddev 885 us 1332 us 4 ``` - after: ``` BM_youtube/min_time:15.000/repeats:4/manual_time_mean 415697 us 621925 us 4 BM_youtube/min_time:15.000/repeats:4/manual_time_median 415913 us 622061 us 4 BM_youtube/min_time:15.000/repeats:4/manual_time_stddev 990 us 1163 us 4 ``` Additional since `-Werror=conversion` is enabled on some platforms we are built on, enable it upstream to catch things early: a few sign conversions had slept through and needed additional casting. Reviewers: hctim, morehouse, eugenis, vitalybuka Reviewed By: vitalybuka Subscribers: srhines, mgorny, javed.absar, kristof.beyls, delcypher, #sanitizers, llvm-commits Tags: #llvm, #sanitizers Differential Revision: https://reviews.llvm.org/D64664 llvm-svn: 366918
* [scudo][standalone] NFC correctionsKostya Kortchinsky2019-07-111-3/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: A few corrections: - rename `TransferBatch::MaxCached` to `getMaxCached` to conform with the style guide; - move `getBlockBegin` from `Chunk::` to `Allocator::`: I believe it was a fallacy to have this be a `Chunk` method, as chunks' relationship to backend blocks are up to the frontend allocator. It makes more sense now, particularly with regard to the offset. Update the associated chunk test as the method isn't available there anymore; - add a forgotten `\n` to a log string; - for `releaseToOs`, instead of starting at `1`, start at `0` and `continue` on `BatchClassId`: in the end it's identical but doesn't assume a particular class id for batches; - change a `CHECK` to a `reportOutOfMemory`: it's a clearer message Reviewers: hctim, morehouse, eugenis, vitalybuka Reviewed By: hctim Subscribers: delcypher, #sanitizers, llvm-commits Tags: #llvm, #sanitizers Differential Revision: https://reviews.llvm.org/D64570 llvm-svn: 365816
* [scudo][standalone] Introduce the combined allocatorKostya Kortchinsky2019-06-171-0/+550
Summary: The Combined allocator hold together all the other components, and provides a memory allocator interface based on various template parameters. This will be in turn used by "wrappers" that will provide the standard C and C++ memory allocation functions, but can be used as is as well. This doesn't depart significantly from the current Scudo implementation except for a few details: - Quarantine batches are now protected by a header a well; - an Allocator instance has its own TSD registry, as opposed to a static one for everybody; - a function to iterate over busy chunks has been added, for Android purposes; This also adds the associated tests, and a few default configurations for several platforms, that will likely be further tuned later on. Reviewers: morehouse, hctim, eugenis, vitalybuka Reviewed By: morehouse Subscribers: srhines, mgorny, delcypher, jfb, #sanitizers, llvm-commits Tags: #llvm, #sanitizers Differential Revision: https://reviews.llvm.org/D63231 llvm-svn: 363569
OpenPOWER on IntegriCloud