diff options
author | Elliott Hughes <enh@google.com> | 2020-06-10 14:48:06 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2020-06-10 14:49:28 -0700 |
commit | 3205cddff06f0b59c1de05f594992d96f7194da3 (patch) | |
tree | 5bf325b5b1a0c0a69bcb2734ea55103e77d42b98 /linker/linker_block_allocator.cpp | |
parent | 79686ca145b5e54e3de4e409a82d72bdc7f22e88 (diff) |
linker: CHECK() or async_safe_fatal() rather than abort().
In particular, add the strerror() output if mprotect() fails.
Fix the CHECK macro so that you can make assertions involving operator%
without that being confused for a printf format specifier.
Bug: https://issuetracker.google.com/158645318
Test: treehugger
Change-Id: I6817f8ca5f094c52dc2c9067bfac90385a8743f5
Diffstat (limited to 'linker/linker_block_allocator.cpp')
-rw-r--r-- | linker/linker_block_allocator.cpp | 26 |
1 files changed, 9 insertions, 17 deletions
diff --git a/linker/linker_block_allocator.cpp b/linker/linker_block_allocator.cpp index 1e2f9a2a2..5b68b1d6c 100644 --- a/linker/linker_block_allocator.cpp +++ b/linker/linker_block_allocator.cpp @@ -27,12 +27,15 @@ */ #include "linker_block_allocator.h" + #include <inttypes.h> #include <string.h> #include <sys/mman.h> #include <sys/prctl.h> #include <unistd.h> +#include "linker_debug.h" + static constexpr size_t kAllocateSize = PAGE_SIZE * 100; static_assert(kAllocateSize % PAGE_SIZE == 0, "Invalid kAllocateSize."); @@ -88,16 +91,10 @@ void LinkerBlockAllocator::free(void* block) { } LinkerBlockAllocatorPage* page = find_page(block); - - if (page == nullptr) { - abort(); - } + CHECK(page != nullptr); ssize_t offset = reinterpret_cast<uint8_t*>(block) - page->bytes; - - if (offset % block_size_ != 0) { - abort(); - } + CHECK((offset % block_size_) == 0); memset(block, 0, block_size_); @@ -114,7 +111,7 @@ void LinkerBlockAllocator::free(void* block) { void LinkerBlockAllocator::protect_all(int prot) { for (LinkerBlockAllocatorPage* page = page_list_; page != nullptr; page = page->next) { if (mprotect(page, kAllocateSize, prot) == -1) { - abort(); + async_safe_fatal("mprotect(%p, %zu, %d) failed: %m", page, kAllocateSize, prot); } } } @@ -125,10 +122,7 @@ void LinkerBlockAllocator::create_new_page() { LinkerBlockAllocatorPage* page = reinterpret_cast<LinkerBlockAllocatorPage*>( mmap(nullptr, kAllocateSize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0)); - - if (page == MAP_FAILED) { - abort(); // oom - } + CHECK(page != MAP_FAILED); prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, page, kAllocateSize, "linker_alloc"); @@ -143,9 +137,7 @@ void LinkerBlockAllocator::create_new_page() { } LinkerBlockAllocatorPage* LinkerBlockAllocator::find_page(void* block) { - if (block == nullptr) { - abort(); - } + CHECK(block != nullptr); LinkerBlockAllocatorPage* page = page_list_; while (page != nullptr) { @@ -157,7 +149,7 @@ LinkerBlockAllocatorPage* LinkerBlockAllocator::find_page(void* block) { page = page->next; } - abort(); + async_safe_fatal("couldn't find page for %p", block); } void LinkerBlockAllocator::purge() { |