2022-04-23 02:53:43 +00:00
|
|
|
// Copyright 2022 the V8 project authors. All rights reserved.
|
2019-12-16 16:30:20 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2021-08-23 13:01:06 +00:00
|
|
|
#include "include/v8-function.h"
|
2019-12-16 16:30:20 +00:00
|
|
|
#include "src/api/api-inl.h"
|
|
|
|
#include "src/codegen/code-desc.h"
|
|
|
|
#include "src/common/globals.h"
|
|
|
|
#include "src/execution/isolate.h"
|
|
|
|
#include "src/handles/handles-inl.h"
|
|
|
|
#include "src/heap/factory.h"
|
|
|
|
#include "src/heap/spaces.h"
|
|
|
|
#include "src/libsampler/sampler.h"
|
2022-04-23 02:53:43 +00:00
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
2022-04-23 02:53:43 +00:00
|
|
|
|
|
|
|
using CodePagesTest = TestWithContext;
|
|
|
|
|
2019-12-16 16:30:20 +00:00
|
|
|
namespace internal {
|
|
|
|
namespace test_code_pages {
|
|
|
|
|
|
|
|
// We have three levels of support which have different behaviors to test.
|
|
|
|
// 1 - Have code range. ARM64 and x64
|
|
|
|
// 2 - Have code pages. ARM32 only
|
|
|
|
// 3 - Nothing - This feature does not work on other platforms.
|
|
|
|
#if defined(V8_TARGET_ARCH_ARM)
|
|
|
|
static const bool kHaveCodePages = true;
|
|
|
|
#else
|
|
|
|
static const bool kHaveCodePages = false;
|
|
|
|
#endif // defined(V8_TARGET_ARCH_ARM)
|
|
|
|
|
|
|
|
static const char* foo_source = R"(
|
|
|
|
function foo%d(a, b) {
|
|
|
|
let x = a * b;
|
|
|
|
let y = x ^ b;
|
|
|
|
let z = y / a;
|
|
|
|
return x + y - z;
|
|
|
|
};
|
|
|
|
%%PrepareFunctionForOptimization(foo%d);
|
|
|
|
foo%d(1, 2);
|
|
|
|
foo%d(1, 2);
|
|
|
|
%%OptimizeFunctionOnNextCall(foo%d);
|
|
|
|
foo%d(1, 2);
|
|
|
|
)";
|
|
|
|
|
|
|
|
std::string getFooCode(int n) {
|
|
|
|
constexpr size_t kMaxSize = 512;
|
|
|
|
char foo_replaced[kMaxSize];
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_LE(n, 999999);
|
2019-12-16 16:30:20 +00:00
|
|
|
snprintf(foo_replaced, kMaxSize, foo_source, n, n, n, n, n, n);
|
|
|
|
|
|
|
|
return std::string(foo_replaced);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool PagesHasExactPage(std::vector<MemoryRange>* pages, Address search_page) {
|
|
|
|
void* addr = reinterpret_cast<void*>(search_page);
|
2022-10-18 06:26:25 +00:00
|
|
|
auto it =
|
|
|
|
std::find_if(pages->begin(), pages->end(),
|
|
|
|
[addr](const MemoryRange& r) { return r.start == addr; });
|
2019-12-16 16:30:20 +00:00
|
|
|
return it != pages->end();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PagesHasExactPage(std::vector<MemoryRange>* pages, Address search_page,
|
|
|
|
size_t size) {
|
|
|
|
void* addr = reinterpret_cast<void*>(search_page);
|
2022-10-18 06:26:25 +00:00
|
|
|
auto it = std::find_if(pages->begin(), pages->end(),
|
|
|
|
[addr, size](const MemoryRange& r) {
|
|
|
|
return r.start == addr && r.length_in_bytes == size;
|
|
|
|
});
|
2019-12-16 16:30:20 +00:00
|
|
|
return it != pages->end();
|
|
|
|
}
|
|
|
|
|
2021-03-17 09:31:08 +00:00
|
|
|
bool PagesContainsRange(std::vector<MemoryRange>* pages, Address search_address,
|
|
|
|
size_t size) {
|
2019-12-16 16:30:20 +00:00
|
|
|
byte* addr = reinterpret_cast<byte*>(search_address);
|
|
|
|
auto it =
|
2022-10-18 06:26:25 +00:00
|
|
|
std::find_if(pages->begin(), pages->end(), [=](const MemoryRange& r) {
|
2019-12-16 16:30:20 +00:00
|
|
|
const byte* page_start = reinterpret_cast<const byte*>(r.start);
|
|
|
|
const byte* page_end = page_start + r.length_in_bytes;
|
2021-03-17 09:31:08 +00:00
|
|
|
return addr >= page_start && (addr + size) <= page_end;
|
2019-12-16 16:30:20 +00:00
|
|
|
});
|
|
|
|
return it != pages->end();
|
|
|
|
}
|
|
|
|
|
2021-03-17 09:31:08 +00:00
|
|
|
bool PagesContainsAddress(std::vector<MemoryRange>* pages,
|
|
|
|
Address search_address) {
|
|
|
|
return PagesContainsRange(pages, search_address, 0);
|
|
|
|
}
|
|
|
|
|
2019-12-16 16:30:20 +00:00
|
|
|
} // namespace
|
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
TEST_F(CodePagesTest, CodeRangeCorrectContents) {
|
|
|
|
if (!i_isolate()->RequiresCodeRange()) return;
|
2019-12-16 16:30:20 +00:00
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
std::vector<MemoryRange>* pages = i_isolate()->GetCodePages();
|
2019-12-16 16:30:20 +00:00
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
const base::AddressRegion& code_region = i_isolate()->heap()->code_region();
|
|
|
|
EXPECT_TRUE(!code_region.is_empty());
|
2019-12-16 16:30:20 +00:00
|
|
|
// We should only have the code range and the embedded code range.
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_EQ(2u, pages->size());
|
|
|
|
EXPECT_TRUE(
|
|
|
|
PagesHasExactPage(pages, code_region.begin(), code_region.size()));
|
|
|
|
EXPECT_TRUE(PagesHasExactPage(
|
|
|
|
pages, reinterpret_cast<Address>(i_isolate()->CurrentEmbeddedBlobCode()),
|
|
|
|
i_isolate()->CurrentEmbeddedBlobCodeSize()));
|
|
|
|
if (i_isolate()->is_short_builtin_calls_enabled()) {
|
2021-04-28 18:06:17 +00:00
|
|
|
// In this case embedded blob code must be included via code_region.
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_TRUE(PagesContainsRange(
|
|
|
|
pages, reinterpret_cast<Address>(i_isolate()->embedded_blob_code()),
|
|
|
|
i_isolate()->embedded_blob_code_size()));
|
2021-03-17 09:31:08 +00:00
|
|
|
} else {
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_TRUE(PagesHasExactPage(
|
|
|
|
pages, reinterpret_cast<Address>(i_isolate()->embedded_blob_code()),
|
|
|
|
i_isolate()->embedded_blob_code_size()));
|
2021-03-17 09:31:08 +00:00
|
|
|
}
|
2019-12-16 16:30:20 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
TEST_F(CodePagesTest, CodePagesCorrectContents) {
|
2019-12-16 16:30:20 +00:00
|
|
|
if (!kHaveCodePages) return;
|
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
std::vector<MemoryRange>* pages = i_isolate()->GetCodePages();
|
2019-12-16 16:30:20 +00:00
|
|
|
// There might be other pages already.
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_GE(pages->size(), 1u);
|
2019-12-16 16:30:20 +00:00
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
const base::AddressRegion& code_region = i_isolate()->heap()->code_region();
|
|
|
|
EXPECT_TRUE(code_region.is_empty());
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
// We should have the embedded code range even when there is no regular code
|
|
|
|
// range.
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_TRUE(PagesHasExactPage(
|
|
|
|
pages, reinterpret_cast<Address>(i_isolate()->embedded_blob_code()),
|
|
|
|
i_isolate()->embedded_blob_code_size()));
|
2019-12-16 16:30:20 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
TEST_F(CodePagesTest, OptimizedCodeWithCodeRange) {
|
2022-09-06 12:52:46 +00:00
|
|
|
v8_flags.allow_natives_syntax = true;
|
2022-04-23 02:53:43 +00:00
|
|
|
if (!i_isolate()->RequiresCodeRange()) return;
|
2020-02-20 18:02:57 +00:00
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
HandleScope scope(i_isolate());
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
std::string foo_str = getFooCode(1);
|
2022-04-23 02:53:43 +00:00
|
|
|
RunJS(foo_str.c_str());
|
2019-12-16 16:30:20 +00:00
|
|
|
v8::Local<v8::Function> local_foo = v8::Local<v8::Function>::Cast(
|
2022-04-23 02:53:43 +00:00
|
|
|
context()->Global()->Get(context(), NewString("foo1")).ToLocalChecked());
|
2019-12-16 16:30:20 +00:00
|
|
|
Handle<JSFunction> foo =
|
|
|
|
Handle<JSFunction>::cast(v8::Utils::OpenHandle(*local_foo));
|
|
|
|
|
2022-08-11 11:37:07 +00:00
|
|
|
CodeT codet = foo->code();
|
|
|
|
// We don't produce optimized code when run with --no-turbofan and
|
|
|
|
// --no-maglev.
|
|
|
|
if (!codet.is_optimized_code()) return;
|
|
|
|
Code foo_code = FromCodeT(codet);
|
2019-12-16 16:30:20 +00:00
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_TRUE(i_isolate()->heap()->InSpace(foo_code, CODE_SPACE));
|
2019-12-16 16:30:20 +00:00
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
std::vector<MemoryRange>* pages = i_isolate()->GetCodePages();
|
|
|
|
EXPECT_TRUE(PagesContainsAddress(pages, foo_code.address()));
|
2019-12-16 16:30:20 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
TEST_F(CodePagesTest, OptimizedCodeWithCodePages) {
|
2019-12-16 16:30:20 +00:00
|
|
|
if (!kHaveCodePages) return;
|
|
|
|
// We don't want incremental marking to start which could cause the code to
|
|
|
|
// not be collected on the CollectGarbage() call.
|
2022-04-23 02:53:43 +00:00
|
|
|
ManualGCScope manual_gc_scope(i_isolate());
|
2022-09-06 12:52:46 +00:00
|
|
|
v8_flags.allow_natives_syntax = true;
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
const void* created_page = nullptr;
|
|
|
|
int num_foos_created = 0;
|
|
|
|
|
|
|
|
{
|
2022-04-23 02:53:43 +00:00
|
|
|
HandleScope scope(i_isolate());
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
size_t num_code_pages = 0;
|
|
|
|
size_t initial_num_code_pages = 0;
|
|
|
|
|
|
|
|
// Keep generating new code until a new code page is added to the list.
|
|
|
|
for (int n = 0; n < 999999; n++) {
|
|
|
|
// Compile and optimize the code and get a reference to it.
|
|
|
|
std::string foo_str = getFooCode(n);
|
|
|
|
char foo_name[10];
|
|
|
|
snprintf(foo_name, sizeof(foo_name), "foo%d", n);
|
2022-04-23 02:53:43 +00:00
|
|
|
RunJS(foo_str.c_str());
|
|
|
|
v8::Local<v8::Function> local_foo = v8::Local<v8::Function>::Cast(
|
|
|
|
context()
|
|
|
|
->Global()
|
|
|
|
->Get(context(), NewString(foo_name))
|
|
|
|
.ToLocalChecked());
|
2019-12-16 16:30:20 +00:00
|
|
|
Handle<JSFunction> foo =
|
|
|
|
Handle<JSFunction>::cast(v8::Utils::OpenHandle(*local_foo));
|
|
|
|
|
2021-05-03 10:16:50 +00:00
|
|
|
// If there is baseline code, check that it's only due to
|
|
|
|
// --always-sparkplug (if this check fails, we'll have to re-think this
|
|
|
|
// test).
|
2021-09-06 11:07:41 +00:00
|
|
|
if (foo->shared().HasBaselineCode()) {
|
2022-09-06 12:52:46 +00:00
|
|
|
EXPECT_TRUE(v8_flags.always_sparkplug);
|
2021-05-03 10:16:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-08-11 11:37:07 +00:00
|
|
|
CodeT codet = foo->code();
|
|
|
|
// We don't produce optimized code when run with --no-turbofan and
|
|
|
|
// --no-maglev.
|
|
|
|
if (!codet.is_optimized_code()) return;
|
|
|
|
Code foo_code = FromCodeT(codet);
|
2019-12-16 16:30:20 +00:00
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_TRUE(i_isolate()->heap()->InSpace(foo_code, CODE_SPACE));
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
// Check that the generated code ended up in one of the code pages
|
|
|
|
// returned by GetCodePages().
|
|
|
|
byte* foo_code_ptr = reinterpret_cast<byte*>(foo_code.address());
|
2022-04-23 02:53:43 +00:00
|
|
|
std::vector<MemoryRange>* pages = i_isolate()->GetCodePages();
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
// Wait until after we have created the first function to take the initial
|
|
|
|
// number of pages so that this test isn't brittle to irrelevant
|
|
|
|
// implementation details.
|
|
|
|
if (n == 0) {
|
|
|
|
initial_num_code_pages = pages->size();
|
|
|
|
}
|
|
|
|
num_code_pages = pages->size();
|
|
|
|
|
|
|
|
// Check that the code object was allocation on any of the pages returned
|
|
|
|
// by GetCodePages().
|
|
|
|
auto it = std::find_if(
|
|
|
|
pages->begin(), pages->end(), [foo_code_ptr](const MemoryRange& r) {
|
|
|
|
const byte* page_start = reinterpret_cast<const byte*>(r.start);
|
|
|
|
const byte* page_end = page_start + r.length_in_bytes;
|
|
|
|
return foo_code_ptr >= page_start && foo_code_ptr < page_end;
|
|
|
|
});
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_NE(it, pages->end());
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
// Store the page that was created just for our functions - we expect it
|
|
|
|
// to be removed later.
|
|
|
|
if (num_code_pages > initial_num_code_pages) {
|
|
|
|
created_page = it->start;
|
|
|
|
num_foos_created = n + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK_NOT_NULL(created_page);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now delete all our foos and force a GC and check that the page is removed
|
|
|
|
// from the list.
|
|
|
|
{
|
2022-04-23 02:53:43 +00:00
|
|
|
HandleScope scope(i_isolate());
|
2019-12-16 16:30:20 +00:00
|
|
|
for (int n = 0; n < num_foos_created; n++) {
|
|
|
|
char foo_name[10];
|
|
|
|
snprintf(foo_name, sizeof(foo_name), "foo%d", n);
|
2022-04-23 02:53:43 +00:00
|
|
|
context()
|
2019-12-16 16:30:20 +00:00
|
|
|
->Global()
|
2022-04-23 02:53:43 +00:00
|
|
|
->Set(context(), NewString(foo_name), Undefined(isolate()))
|
2019-12-16 16:30:20 +00:00
|
|
|
.Check();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
CollectGarbage(CODE_SPACE);
|
2019-12-16 16:30:20 +00:00
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
std::vector<MemoryRange>* pages = i_isolate()->GetCodePages();
|
2019-12-16 16:30:20 +00:00
|
|
|
auto it = std::find_if(
|
|
|
|
pages->begin(), pages->end(),
|
|
|
|
[created_page](const MemoryRange& r) { return r.start == created_page; });
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_EQ(it, pages->end());
|
2019-12-16 16:30:20 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
TEST_F(CodePagesTest, LargeCodeObject) {
|
2019-12-16 16:30:20 +00:00
|
|
|
// We don't want incremental marking to start which could cause the code to
|
|
|
|
// not be collected on the CollectGarbage() call.
|
2022-04-23 02:53:43 +00:00
|
|
|
ManualGCScope manual_gc_scope(i_isolate());
|
2019-12-16 16:30:20 +00:00
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
if (!i_isolate()->RequiresCodeRange() && !kHaveCodePages) return;
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
// Create a big function that ends up in CODE_LO_SPACE.
|
|
|
|
const int instruction_size = Page::kPageSize + 1;
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_GT(instruction_size, MemoryChunkLayout::MaxRegularCodeObjectSize());
|
2019-12-16 16:30:20 +00:00
|
|
|
std::unique_ptr<byte[]> instructions(new byte[instruction_size]);
|
|
|
|
|
|
|
|
CodeDesc desc;
|
|
|
|
desc.buffer = instructions.get();
|
|
|
|
desc.buffer_size = instruction_size;
|
|
|
|
desc.instr_size = instruction_size;
|
|
|
|
desc.reloc_size = 0;
|
|
|
|
desc.constant_pool_size = 0;
|
|
|
|
desc.unwinding_info = nullptr;
|
|
|
|
desc.unwinding_info_size = 0;
|
|
|
|
desc.origin = nullptr;
|
|
|
|
|
|
|
|
Address stale_code_address;
|
|
|
|
|
|
|
|
{
|
2022-04-23 02:53:43 +00:00
|
|
|
HandleScope scope(i_isolate());
|
2019-12-16 16:30:20 +00:00
|
|
|
Handle<Code> foo_code =
|
2022-04-23 02:53:43 +00:00
|
|
|
Factory::CodeBuilder(i_isolate(), desc, CodeKind::WASM_FUNCTION)
|
|
|
|
.Build();
|
2019-12-16 16:30:20 +00:00
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_TRUE(i_isolate()->heap()->InSpace(*foo_code, CODE_LO_SPACE));
|
2019-12-16 16:30:20 +00:00
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
std::vector<MemoryRange>* pages = i_isolate()->GetCodePages();
|
2019-12-16 16:30:20 +00:00
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
if (i_isolate()->RequiresCodeRange()) {
|
|
|
|
EXPECT_TRUE(PagesContainsAddress(pages, foo_code->address()));
|
2019-12-16 16:30:20 +00:00
|
|
|
} else {
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_TRUE(PagesHasExactPage(pages, foo_code->address()));
|
2019-12-16 16:30:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stale_code_address = foo_code->address();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the large code object.
|
2022-04-23 02:53:43 +00:00
|
|
|
CollectGarbage(CODE_LO_SPACE);
|
|
|
|
EXPECT_TRUE(
|
|
|
|
!i_isolate()->heap()->InSpaceSlow(stale_code_address, CODE_LO_SPACE));
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
// Check that it was removed from CodePages.
|
2022-04-23 02:53:43 +00:00
|
|
|
std::vector<MemoryRange>* pages = i_isolate()->GetCodePages();
|
|
|
|
EXPECT_TRUE(!PagesHasExactPage(pages, stale_code_address));
|
2019-12-16 16:30:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr size_t kBufSize = v8::Isolate::kMinCodePagesBufferSize;
|
|
|
|
|
|
|
|
class SignalSender : public sampler::Sampler {
|
|
|
|
public:
|
|
|
|
explicit SignalSender(v8::Isolate* isolate) : sampler::Sampler(isolate) {}
|
|
|
|
|
|
|
|
// Called during the signal/thread suspension.
|
|
|
|
void SampleStack(const v8::RegisterState& regs) override {
|
|
|
|
MemoryRange* code_pages_copy = code_pages_copy_.load();
|
|
|
|
CHECK_NOT_NULL(code_pages_copy);
|
|
|
|
size_t num_pages = isolate_->CopyCodePages(kBufSize, code_pages_copy);
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_LE(num_pages, kBufSize);
|
2019-12-16 16:30:20 +00:00
|
|
|
sample_semaphore_.Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called on the sampling thread to trigger a sample. Blocks until the sample
|
|
|
|
// is finished.
|
|
|
|
void SampleIntoVector(MemoryRange output_buffer[]) {
|
|
|
|
code_pages_copy_.store(output_buffer);
|
|
|
|
DoSample();
|
|
|
|
sample_semaphore_.Wait();
|
|
|
|
code_pages_copy_.store(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
base::Semaphore sample_semaphore_{0};
|
|
|
|
std::atomic<MemoryRange*> code_pages_copy_{nullptr};
|
|
|
|
};
|
|
|
|
|
|
|
|
class SamplingThread : public base::Thread {
|
|
|
|
public:
|
|
|
|
explicit SamplingThread(SignalSender* signal_sender)
|
|
|
|
: base::Thread(base::Thread::Options("SamplingThread")),
|
|
|
|
signal_sender_(signal_sender) {}
|
|
|
|
|
|
|
|
// Blocks until a sample is taken.
|
|
|
|
void TriggerSample() { signal_sender_->SampleIntoVector(code_pages_copy_); }
|
|
|
|
|
|
|
|
void Run() override {
|
|
|
|
while (running_.load()) {
|
|
|
|
TriggerSample();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called from the main thread. Blocks until a sample is taken. Not
|
|
|
|
// thread-safe so do not call while this thread is running.
|
|
|
|
static std::vector<MemoryRange> DoSynchronousSample(v8::Isolate* isolate) {
|
|
|
|
MemoryRange code_pages_copy[kBufSize];
|
|
|
|
size_t num_pages = isolate->CopyCodePages(kBufSize, code_pages_copy);
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_LE(num_pages, kBufSize);
|
2019-12-16 16:30:20 +00:00
|
|
|
return std::vector<MemoryRange>{code_pages_copy,
|
|
|
|
&code_pages_copy[num_pages]};
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stop() { running_.store(false); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::atomic_bool running_{true};
|
|
|
|
SignalSender* signal_sender_;
|
|
|
|
MemoryRange code_pages_copy_[kBufSize];
|
|
|
|
};
|
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
TEST_F(CodePagesTest, LargeCodeObjectWithSignalHandler) {
|
2019-12-16 16:30:20 +00:00
|
|
|
// We don't want incremental marking to start which could cause the code to
|
|
|
|
// not be collected on the CollectGarbage() call.
|
2022-04-23 02:53:43 +00:00
|
|
|
ManualGCScope manual_gc_scope(i_isolate());
|
2019-12-16 16:30:20 +00:00
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
if (!i_isolate()->RequiresCodeRange() && !kHaveCodePages) return;
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
// Create a big function that ends up in CODE_LO_SPACE.
|
|
|
|
const int instruction_size = Page::kPageSize + 1;
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_GT(instruction_size, MemoryChunkLayout::MaxRegularCodeObjectSize());
|
2019-12-16 16:30:20 +00:00
|
|
|
std::unique_ptr<byte[]> instructions(new byte[instruction_size]);
|
|
|
|
|
|
|
|
CodeDesc desc;
|
|
|
|
desc.buffer = instructions.get();
|
|
|
|
desc.buffer_size = instruction_size;
|
|
|
|
desc.instr_size = instruction_size;
|
|
|
|
desc.reloc_size = 0;
|
|
|
|
desc.constant_pool_size = 0;
|
|
|
|
desc.unwinding_info = nullptr;
|
|
|
|
desc.unwinding_info_size = 0;
|
|
|
|
desc.origin = nullptr;
|
|
|
|
|
|
|
|
Address stale_code_address;
|
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
SignalSender signal_sender(isolate());
|
2019-12-16 16:30:20 +00:00
|
|
|
signal_sender.Start();
|
|
|
|
// Take an initial sample.
|
|
|
|
std::vector<MemoryRange> initial_pages =
|
2022-04-23 02:53:43 +00:00
|
|
|
SamplingThread::DoSynchronousSample(isolate());
|
2019-12-16 16:30:20 +00:00
|
|
|
SamplingThread sampling_thread(&signal_sender);
|
|
|
|
|
|
|
|
sampling_thread.StartSynchronously();
|
|
|
|
|
|
|
|
{
|
2022-04-23 02:53:43 +00:00
|
|
|
HandleScope scope(i_isolate());
|
2019-12-16 16:30:20 +00:00
|
|
|
Handle<Code> foo_code =
|
2022-04-23 02:53:43 +00:00
|
|
|
Factory::CodeBuilder(i_isolate(), desc, CodeKind::WASM_FUNCTION)
|
|
|
|
.Build();
|
2019-12-16 16:30:20 +00:00
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_TRUE(i_isolate()->heap()->InSpace(*foo_code, CODE_LO_SPACE));
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
// Do a synchronous sample to ensure that we capture the state with the
|
|
|
|
// extra code page.
|
|
|
|
sampling_thread.Stop();
|
|
|
|
sampling_thread.Join();
|
|
|
|
|
|
|
|
// Check that the page was added.
|
|
|
|
std::vector<MemoryRange> pages =
|
2022-04-23 02:53:43 +00:00
|
|
|
SamplingThread::DoSynchronousSample(isolate());
|
|
|
|
if (i_isolate()->RequiresCodeRange()) {
|
|
|
|
EXPECT_TRUE(PagesContainsAddress(&pages, foo_code->address()));
|
2019-12-16 16:30:20 +00:00
|
|
|
} else {
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_TRUE(PagesHasExactPage(&pages, foo_code->address()));
|
2019-12-16 16:30:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stale_code_address = foo_code->address();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start async sampling again to detect threading issues.
|
|
|
|
sampling_thread.StartSynchronously();
|
|
|
|
|
|
|
|
// Delete the large code object.
|
2022-04-23 02:53:43 +00:00
|
|
|
CollectGarbage(CODE_LO_SPACE);
|
|
|
|
EXPECT_TRUE(
|
|
|
|
!i_isolate()->heap()->InSpaceSlow(stale_code_address, CODE_LO_SPACE));
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
sampling_thread.Stop();
|
|
|
|
sampling_thread.Join();
|
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
std::vector<MemoryRange> pages =
|
|
|
|
SamplingThread::DoSynchronousSample(isolate());
|
|
|
|
EXPECT_TRUE(!PagesHasExactPage(&pages, stale_code_address));
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
signal_sender.Stop();
|
|
|
|
}
|
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
TEST_F(CodePagesTest, Sorted) {
|
2019-12-16 16:30:20 +00:00
|
|
|
// We don't want incremental marking to start which could cause the code to
|
|
|
|
// not be collected on the CollectGarbage() call.
|
2022-04-23 02:53:43 +00:00
|
|
|
ManualGCScope manual_gc_scope(i_isolate());
|
2019-12-16 16:30:20 +00:00
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
if (!i_isolate()->RequiresCodeRange() && !kHaveCodePages) return;
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
// Create a big function that ends up in CODE_LO_SPACE.
|
|
|
|
const int instruction_size = Page::kPageSize + 1;
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_GT(instruction_size, MemoryChunkLayout::MaxRegularCodeObjectSize());
|
2019-12-16 16:30:20 +00:00
|
|
|
std::unique_ptr<byte[]> instructions(new byte[instruction_size]);
|
|
|
|
|
|
|
|
CodeDesc desc;
|
|
|
|
desc.buffer = instructions.get();
|
|
|
|
desc.buffer_size = instruction_size;
|
|
|
|
desc.instr_size = instruction_size;
|
|
|
|
desc.reloc_size = 0;
|
|
|
|
desc.constant_pool_size = 0;
|
|
|
|
desc.unwinding_info = nullptr;
|
|
|
|
desc.unwinding_info_size = 0;
|
|
|
|
desc.origin = nullptr;
|
|
|
|
|
|
|
|
// Take an initial sample.
|
|
|
|
std::vector<MemoryRange> initial_pages =
|
2022-04-23 02:53:43 +00:00
|
|
|
SamplingThread::DoSynchronousSample(isolate());
|
2019-12-16 16:30:20 +00:00
|
|
|
size_t initial_num_pages = initial_pages.size();
|
|
|
|
|
|
|
|
auto compare = [](const MemoryRange& a, const MemoryRange& b) {
|
|
|
|
return a.start < b.start;
|
|
|
|
};
|
|
|
|
{
|
2022-04-23 02:53:43 +00:00
|
|
|
HandleScope outer_scope(i_isolate());
|
2019-12-16 16:30:20 +00:00
|
|
|
Handle<Code> code1, code3;
|
|
|
|
Address code2_address;
|
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
code1 = Factory::CodeBuilder(i_isolate(), desc, CodeKind::WASM_FUNCTION)
|
|
|
|
.Build();
|
|
|
|
EXPECT_TRUE(i_isolate()->heap()->InSpace(*code1, CODE_LO_SPACE));
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
{
|
2022-04-23 02:53:43 +00:00
|
|
|
HandleScope scope(i_isolate());
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
// Create three large code objects, we'll delete the middle one and check
|
|
|
|
// everything is still sorted.
|
|
|
|
Handle<Code> code2 =
|
2022-04-23 02:53:43 +00:00
|
|
|
Factory::CodeBuilder(i_isolate(), desc, CodeKind::WASM_FUNCTION)
|
2020-08-05 11:48:03 +00:00
|
|
|
.Build();
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_TRUE(i_isolate()->heap()->InSpace(*code2, CODE_LO_SPACE));
|
|
|
|
code3 = Factory::CodeBuilder(i_isolate(), desc, CodeKind::WASM_FUNCTION)
|
2020-08-05 11:48:03 +00:00
|
|
|
.Build();
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_TRUE(i_isolate()->heap()->InSpace(*code3, CODE_LO_SPACE));
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
code2_address = code2->address();
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_TRUE(
|
|
|
|
i_isolate()->heap()->InSpaceSlow(code1->address(), CODE_LO_SPACE));
|
|
|
|
EXPECT_TRUE(
|
|
|
|
i_isolate()->heap()->InSpaceSlow(code2->address(), CODE_LO_SPACE));
|
|
|
|
EXPECT_TRUE(
|
|
|
|
i_isolate()->heap()->InSpaceSlow(code3->address(), CODE_LO_SPACE));
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
// Check that the pages were added.
|
|
|
|
std::vector<MemoryRange> pages =
|
2022-04-23 02:53:43 +00:00
|
|
|
SamplingThread::DoSynchronousSample(isolate());
|
|
|
|
if (i_isolate()->RequiresCodeRange()) {
|
|
|
|
EXPECT_EQ(pages.size(), initial_num_pages);
|
2019-12-16 16:30:20 +00:00
|
|
|
} else {
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_EQ(pages.size(), initial_num_pages + 3);
|
2019-12-16 16:30:20 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_TRUE(std::is_sorted(pages.begin(), pages.end(), compare));
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
code3 = scope.CloseAndEscape(code3);
|
|
|
|
}
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_TRUE(
|
|
|
|
i_isolate()->heap()->InSpaceSlow(code1->address(), CODE_LO_SPACE));
|
|
|
|
EXPECT_TRUE(i_isolate()->heap()->InSpaceSlow(code2_address, CODE_LO_SPACE));
|
|
|
|
EXPECT_TRUE(
|
|
|
|
i_isolate()->heap()->InSpaceSlow(code3->address(), CODE_LO_SPACE));
|
2019-12-16 16:30:20 +00:00
|
|
|
// Delete code2.
|
2022-04-23 02:53:43 +00:00
|
|
|
CollectGarbage(CODE_LO_SPACE);
|
|
|
|
EXPECT_TRUE(
|
|
|
|
i_isolate()->heap()->InSpaceSlow(code1->address(), CODE_LO_SPACE));
|
|
|
|
EXPECT_TRUE(
|
|
|
|
!i_isolate()->heap()->InSpaceSlow(code2_address, CODE_LO_SPACE));
|
|
|
|
EXPECT_TRUE(
|
|
|
|
i_isolate()->heap()->InSpaceSlow(code3->address(), CODE_LO_SPACE));
|
2019-12-16 16:30:20 +00:00
|
|
|
|
|
|
|
std::vector<MemoryRange> pages =
|
2022-04-23 02:53:43 +00:00
|
|
|
SamplingThread::DoSynchronousSample(isolate());
|
|
|
|
if (i_isolate()->RequiresCodeRange()) {
|
|
|
|
EXPECT_EQ(pages.size(), initial_num_pages);
|
2019-12-16 16:30:20 +00:00
|
|
|
} else {
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_EQ(pages.size(), initial_num_pages + 2);
|
2019-12-16 16:30:20 +00:00
|
|
|
}
|
2022-04-23 02:53:43 +00:00
|
|
|
EXPECT_TRUE(std::is_sorted(pages.begin(), pages.end(), compare));
|
2019-12-16 16:30:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace test_code_pages
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|