LargeMmapAllocator crashes the process on OOM even if allocator_may_return_null=1
| Bugzilla Link | 22026 |
| Resolution | FIXED |
| Resolved on | Feb 28, 2018 10:58 |
| Version | unspecified |
| OS | Windows NT |
| CC | @KernelAddress,@LebedevRI |
Extended Description
This hits us on Chromium on Win32 (32-bit): [ RUN ] DiscardableMemoryTests/DiscardableMemoryTest.AddressSpace/0 ==8508==ERROR: AddressSanitizer failed to allocate 0x408000 (4227072) bytes of LargeMmapAllocator (error code: 8) ==8508==AddressSanitizer CHECK failed: D:\src\chr_trunk\src\third_party\llvm\projects\compiler-rt\lib\sanitizer_common\sanitizer_win.cc:89 "(("unable to mmap" && 0)) != (0)" (0x0, 0x0)
Looking at the LargeMmapAllocator code, it unconditionally does MmapOrDie, so this is not OOM-friendly. It's likely we haven't seen this earlier because it's much harder to OOM on x64 :)
The broader allocator standards compliance was addressed by a series of patches, this particular secondary allocator issue was fixed in https://reviews.llvm.org/D34243.
This flag soft_rss_limit_mb=N however does not help if it's just one big allocation. E.g. in test case https://github.com/llvm-mirror/compiler-rt/blob/master/test/asan/TestCases/allocator_returns_null.cc#L28 an allocation of gigantic buffer is tested. However unfortunately, if i change volatile that volatile size_t size = std::numeric_limits<size_t>::max() - 10000;
to some lower value, in my case 32202304176, it does not work, it always crashes regardless. This problem somewhat complicates fuzzing of things where allocating big buffers is normal.
a flag that I recently implemented (but not documented on the wiki yet), soft_rss_limit_mb=N achieves something close to what's discussed here.
Implementing something more robust and general is hard, Dmitry explained it well.
It is not as simple as it may appear. Assuming you have a program that handles ENOMEM and continues to work in low memory conditions and you want it to continue to work under sanitizers. You need to handle ENOMEM through out sanitizer runtimes, not only in user memory allocator. For example, pthread_create interceptor allocates some internal sanitizer memory, ENOMEM must be handled in all internal allocations and propagated to user. Similarly, malloc puts the stack into stack depot, ENOMEM must be propagated on that path as well. I am not saying about tsan, where an atomic operation handler can fail due to ENOMEM, and there is no way to return ENOMEM from an atomic operation. I would say it is close to unfeasible (except for the toy synthetic test that you provided, of course).
yes, this is in my todo
OK, turns out this is not LargeMmapAllocator specific.
Here is a test that crashes SizeClassAllocator32:
#include <malloc.h>
#include <stdio.h>
int main() {
void *ptr;
do {
ptr = malloc(10);
} while (ptr);
}
and this one crashes LargeMmapAllocator:
#include <malloc.h>
#include <stdio.h>
int main() {
void *ptr;
do {
ptr = malloc(1 << 25);
} while (ptr);
}
// compile with $ clang -fsanitize=address -m32 test.cpp
The problem is that these crashes happen on a CHECK in MmapOrDie, that is: a) they are not controllable by ASAN_OPTIONS=allocator_may_return_null=1 b) they don't look user-friendly (too verbose output,includes procmaps etc) for comparison, replace "1 << 25" with "0xFFFFFF00".
Do we want to fix that? Do we want to handle OOMs better?
Kostya, can you please take a look? [if not, I can take over tomorrow]