Commit 285e4d69 authored by Peter Marshall's avatar Peter Marshall Committed by Commit Bot

[unwinder] Add a vector-based code page mechanism for arm32

Add an API on Isolate that returns a sorted vector of code pages allocated
within V8. The implementation is designed to be signal-safe, so that the
user (the UMA sampling profiler) can access this information from a signal
handler, where allocation and taking locks is prohibited.

This CL adds the machinery for maintaining the list of allocated code
pages. Further CLs will modify the Unwinder API itself to accept the code
pages provided by this API.

The unwinder API currently uses the reserved virtual-memory range called
the CodeRange to identify where all V8 code objects live, but this doesn't
exist on arm32 or any 32-bit platform, so this approach adds a way to
expose the location of all valid V8 code objects in a signal-safe way for
use by the UMA sampling profiler.

On 64-bit, this API always gives the code_range and embedded_code_range, and
does not maintain a vector of code pages. This is so that we have a unified
API on 32 and 64-bit that can be used in exactly the same way by embedders.

Design doc:
https://docs.google.com/document/d/1VGwUult5AHLRk658VetwEHMOmDDxA2eDQs9lDFMZTE0

Bug: v8:8116
Change-Id: I732509a45121fc54853182481c24d1083275afce
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1564068
Commit-Queue: Peter Marshall <petermarshall@chromium.org>
Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#65469}
parent 532ca830
......@@ -9138,6 +9138,23 @@ class V8_EXPORT Isolate {
*/
UnwindState GetUnwindState();
static constexpr size_t kMinCodePagesBufferSize = 32;
/**
* Copies the code heap pages currently in use by V8 into |code_pages_out|.
* |code_pages_out| must have at least kMinCodePagesBufferSize capacity and
* must be empty.
*
* Signal-safe, does not allocate, does not access the V8 heap.
* No code on the stack can rely on pages that might be missing.
*
* Returns the number of pages available to be copied, which might be greater
* than |capacity|. In this case, only |capacity| pages will be copied into
* |code_pages_out|. The caller should provide a bigger buffer on the next
* call in order to get all available code pages, but this is not required.
*/
size_t CopyCodePages(size_t capacity, MemoryRange* code_pages_out);
/** Set the callback to invoke in case of fatal errors. */
void SetFatalErrorHandler(FatalErrorCallback that);
......
......@@ -8914,6 +8914,30 @@ UnwindState Isolate::GetUnwindState() {
return unwind_state;
}
size_t Isolate::CopyCodePages(size_t capacity, MemoryRange* code_pages_out) {
#if !defined(V8_TARGET_ARCH_X64) && !defined(V8_TARGET_ARCH_ARM64) && \
!defined(V8_TARGET_ARCH_ARM)
// Not implemented on all platforms.
UNREACHABLE();
#else
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
std::vector<MemoryRange>* code_pages = isolate->GetCodePages();
DCHECK_NOT_NULL(code_pages);
// Copy as many elements into the output vector as we can. If the
// caller-provided buffer is not big enough, we fill it, and the caller can
// provide a bigger one next time. We do it this way because allocation is not
// allowed in signal handlers.
size_t limit = std::min(capacity, code_pages->size());
for (size_t i = 0; i < limit; i++) {
code_pages_out[i] = code_pages->at(i);
}
return code_pages->size();
#endif
}
#define CALLBACK_SETTER(ExternalName, Type, InternalName) \
void Isolate::Set##ExternalName(Type callback) { \
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); \
......
......@@ -2236,6 +2236,14 @@ void Isolate::ReportPendingMessagesImpl(bool report_externally) {
}
}
std::vector<MemoryRange>* Isolate::GetCodePages() const {
return code_pages_.load(std::memory_order_acquire);
}
void Isolate::SetCodePages(std::vector<MemoryRange>* new_code_pages) {
code_pages_.store(new_code_pages, std::memory_order_release);
}
void Isolate::ReportPendingMessages() {
DCHECK(AllowExceptions::IsAllowed(this));
......@@ -2979,6 +2987,8 @@ void Isolate::Deinit() {
compiler_zone_ = nullptr;
compiler_cache_ = nullptr;
SetCodePages(nullptr);
ClearSerializerData();
{
......@@ -3268,6 +3278,14 @@ void Isolate::AddCrashKeysForIsolateAndHeapPointers() {
AddressToString(code_space_firstpage_address));
}
void Isolate::InitializeCodeRanges() {
DCHECK_NULL(GetCodePages());
MemoryRange embedded_range{reinterpret_cast<const void*>(embedded_blob()),
embedded_blob_size()};
code_pages_buffer1_.push_back(embedded_range);
SetCodePages(&code_pages_buffer1_);
}
bool Isolate::Init(ReadOnlyDeserializer* read_only_deserializer,
StartupDeserializer* startup_deserializer) {
TRACE_ISOLATE(init);
......@@ -3294,6 +3312,10 @@ bool Isolate::Init(ReadOnlyDeserializer* read_only_deserializer,
FOR_EACH_ISOLATE_ADDRESS_NAME(ASSIGN_ELEMENT)
#undef ASSIGN_ELEMENT
// We need to initialize code_pages_ before any on-heap code is allocated to
// make sure we record all code allocations.
InitializeCodeRanges();
compilation_cache_ = new CompilationCache(this);
descriptor_lookup_cache_ = new DescriptorLookupCache();
inner_pointer_to_code_cache_ = new InnerPointerToCodeCache(this);
......@@ -4382,6 +4404,85 @@ AssertNoContextChange::AssertNoContextChange(Isolate* isolate)
: isolate_(isolate), context_(isolate->context(), isolate) {}
#endif // DEBUG
void Isolate::AddCodeMemoryRange(MemoryRange range) {
std::vector<MemoryRange>* old_code_pages = GetCodePages();
DCHECK_NOT_NULL(old_code_pages);
std::vector<MemoryRange>* new_code_pages;
if (old_code_pages == &code_pages_buffer1_) {
new_code_pages = &code_pages_buffer2_;
} else {
new_code_pages = &code_pages_buffer1_;
}
// Copy all existing data from the old vector to the new vector and insert the
// new page.
new_code_pages->clear();
new_code_pages->reserve(old_code_pages->size() + 1);
std::merge(old_code_pages->begin(), old_code_pages->end(), &range, &range + 1,
std::back_inserter(*new_code_pages),
[](const MemoryRange& a, const MemoryRange& b) {
return a.start < b.start;
});
// Atomically switch out the pointer
SetCodePages(new_code_pages);
}
// |chunk| is either a Page or an executable LargePage.
void Isolate::AddCodeMemoryChunk(MemoryChunk* chunk) {
// We only keep track of individual code pages/allocations if we are on arm32,
// because on x64 and arm64 we have a code range which makes this unnecessary.
#if !defined(V8_TARGET_ARCH_ARM)
return;
#else
void* new_page_start = reinterpret_cast<void*>(chunk->area_start());
size_t new_page_size = chunk->area_size();
MemoryRange new_range{new_page_start, new_page_size};
AddCodeMemoryRange(new_range);
#endif // !defined(V8_TARGET_ARCH_ARM)
}
void Isolate::AddCodeRange(Address begin, size_t length_in_bytes) {
AddCodeMemoryRange(
MemoryRange{reinterpret_cast<void*>(begin), length_in_bytes});
}
// |chunk| is either a Page or an executable LargePage.
void Isolate::RemoveCodeMemoryChunk(MemoryChunk* chunk) {
// We only keep track of individual code pages/allocations if we are on arm32,
// because on x64 and arm64 we have a code range which makes this unnecessary.
#if !defined(V8_TARGET_ARCH_ARM)
return;
#else
void* removed_page_start = reinterpret_cast<void*>(chunk->area_start());
std::vector<MemoryRange>* old_code_pages = GetCodePages();
DCHECK_NOT_NULL(old_code_pages);
std::vector<MemoryRange>* new_code_pages;
if (old_code_pages == &code_pages_buffer1_) {
new_code_pages = &code_pages_buffer2_;
} else {
new_code_pages = &code_pages_buffer1_;
}
// Copy all existing data from the old vector to the new vector except the
// removed page.
new_code_pages->clear();
new_code_pages->reserve(old_code_pages->size() - 1);
std::remove_copy_if(old_code_pages->begin(), old_code_pages->end(),
std::back_inserter(*new_code_pages),
[removed_page_start](const MemoryRange& range) {
return range.start == removed_page_start;
});
// Atomically switch out the pointer
SetCodePages(new_code_pages);
#endif // !defined(V8_TARGET_ARCH_ARM)
}
#undef TRACE_ISOLATE
} // namespace internal
......
......@@ -676,6 +676,10 @@ class Isolate final : private HiddenFactory {
return &thread_local_top()->js_entry_sp_;
}
V8_EXPORT_PRIVATE std::vector<MemoryRange>* GetCodePages() const;
V8_EXPORT_PRIVATE void SetCodePages(std::vector<MemoryRange>* new_code_pages);
// Returns the global object of the current context. It could be
// a builtin object, or a JS global object.
inline Handle<JSGlobalObject> global_object();
......@@ -1334,8 +1338,8 @@ class Isolate final : private HiddenFactory {
// These always return the same result as static methods above, but don't
// access the global atomic variable (and thus *might be* slightly faster).
const uint8_t* embedded_blob() const;
uint32_t embedded_blob_size() const;
V8_EXPORT_PRIVATE const uint8_t* embedded_blob() const;
V8_EXPORT_PRIVATE uint32_t embedded_blob_size() const;
void set_array_buffer_allocator(v8::ArrayBuffer::Allocator* allocator) {
array_buffer_allocator_ = allocator;
......@@ -1484,6 +1488,10 @@ class Isolate final : private HiddenFactory {
// before such a mode change to ensure that this cannot happen.
V8_EXPORT_PRIVATE void CollectSourcePositionsForAllBytecodeArrays();
void AddCodeMemoryChunk(MemoryChunk* chunk);
void RemoveCodeMemoryChunk(MemoryChunk* chunk);
void AddCodeRange(Address begin, size_t length_in_bytes);
private:
explicit Isolate(std::unique_ptr<IsolateAllocator> isolate_allocator);
~Isolate();
......@@ -1493,6 +1501,9 @@ class Isolate final : private HiddenFactory {
void CheckIsolateLayout();
void InitializeCodeRanges();
void AddCodeMemoryRange(MemoryRange range);
class ThreadDataTable {
public:
ThreadDataTable() = default;
......@@ -1807,6 +1818,12 @@ class Isolate final : private HiddenFactory {
base::Mutex thread_data_table_mutex_;
ThreadDataTable thread_data_table_;
// A signal-safe vector of heap pages containing code. Used with the
// v8::Unwinder API.
std::atomic<std::vector<MemoryRange>*> code_pages_{nullptr};
std::vector<MemoryRange> code_pages_buffer1_;
std::vector<MemoryRange> code_pages_buffer2_;
// Enables the host application to provide a mechanism for recording a
// predefined set of data as crash keys to be used in postmortem debugging
// in case of a crash.
......
......@@ -1018,7 +1018,7 @@ class Heap {
// Slow methods that can be used for verification as they can also be used
// with off-heap Addresses.
bool InSpaceSlow(Address addr, AllocationSpace space);
V8_EXPORT_PRIVATE bool InSpaceSlow(Address addr, AllocationSpace space);
static inline Heap* FromWritableHeapObject(HeapObject obj);
......
......@@ -4,6 +4,7 @@
#include "src/heap/spaces.h"
#include <algorithm>
#include <cinttypes>
#include <utility>
......@@ -179,6 +180,7 @@ void MemoryAllocator::InitializeCodePageAllocator(
"CodeRange setup: allocate virtual memory");
}
code_range_ = reservation.region();
isolate_->AddCodeRange(code_range_.begin(), code_range_.size());
// We are sure that we have mapped a block of requested addresses.
DCHECK_GE(reservation.size(), requested);
......@@ -1723,6 +1725,8 @@ void PagedSpace::MergeLocalSpace(LocalSpace* other) {
// Relinking requires the category to be unlinked.
other->RemovePage(p);
AddPage(p);
// These code pages were allocated by the CompactionSpace.
if (identity() == CODE_SPACE) heap()->isolate()->AddCodeMemoryChunk(p);
DCHECK_IMPLIES(
!p->IsFlagSet(Page::NEVER_ALLOCATE_ON_PAGE),
p->AvailableInFreeList() == p->AvailableInFreeListFromAllocatedBytes());
......@@ -1843,6 +1847,10 @@ bool PagedSpace::Expand() {
// Pages created during bootstrapping may contain immortal immovable objects.
if (!heap()->deserialization_complete()) page->MarkNeverEvacuate();
AddPage(page);
// If this is a non-compaction code space, this is a previously unseen page.
if (identity() == CODE_SPACE && !is_compaction_space()) {
heap()->isolate()->AddCodeMemoryChunk(page);
}
Free(page->area_start(), page->area_size(),
SpaceAccountingMode::kSpaceAccounted);
heap()->NotifyOldGenerationExpansion();
......@@ -1987,6 +1995,8 @@ void PagedSpace::ReleasePage(Page* page) {
allocation_info_.Reset(kNullAddress, kNullAddress);
}
heap()->isolate()->RemoveCodeMemoryChunk(page);
AccountUncommitted(page->size());
accounting_stats_.DecreaseCapacity(page->area_size());
heap()->memory_allocator()->Free<MemoryAllocator::kPreFreeAndQueue>(page);
......@@ -4413,10 +4423,12 @@ AllocationResult CodeLargeObjectSpace::AllocateRaw(int object_size) {
void CodeLargeObjectSpace::AddPage(LargePage* page, size_t object_size) {
OldLargeObjectSpace::AddPage(page, object_size);
InsertChunkMapEntries(page);
heap()->isolate()->AddCodeMemoryChunk(page);
}
void CodeLargeObjectSpace::RemovePage(LargePage* page, size_t object_size) {
RemoveChunkMapEntries(page);
heap()->isolate()->RemoveCodeMemoryChunk(page);
OldLargeObjectSpace::RemovePage(page, object_size);
}
......
......@@ -193,6 +193,7 @@ v8_source_set("cctest_sources") {
"test-bit-vector.cc",
"test-circular-queue.cc",
"test-code-layout.cc",
"test-code-pages.cc",
"test-code-stub-assembler.cc",
"test-compiler.cc",
"test-constantpool.cc",
......
......@@ -486,6 +486,7 @@
'test-branch-combine/*': [SKIP],
'test-code-assembler/*': [SKIP],
'test-code-generator/*': [SKIP],
'test-code-pages/*': [SKIP],
'test-code-stub-assembler/*': [SKIP],
'test-js-context-specialization/*': [SKIP],
'test-multiple-return/*': [SKIP],
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment