Commit 49c507dc authored by Leszek Swirski's avatar Leszek Swirski Committed by V8 LUCI CQ

[test] Make cctest run one test, with maybe custom platform

Remove cctest's ability to run multiple tests (which has long been
deprecated and mostly broken). We can then make platform & V8
initialisation be part of running the test's Run method.

In particular, this allows us to inject custom logic into the platform
initialisation, like setting up a platform wrapper. Add a
TEST_WITH_PLATFORM which exercises this by registering a platform
factory on the test, and wrapping the default platform using this
factory. This allows these tests to guarantee that the lifetime of the
platform is longer than the lifetime of the isolate.

As a result of this, we can also remove the complexity around draining
platform state in the TestPlatform (since it will now have a longer
lifetime than the Isolate using it), and as a drive-by clean up the
TestPlaform to use a CcTest-global "default platform" instead of trying
to scope over the "current" platform.

As another drive-by, change the linked-list of CcTests and the linear
search through it into an std::map of tests.

Change-Id: I610f6312fe042f29f45cc4dfba311e4184bc7759
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3569223Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Commit-Queue: Leszek Swirski <leszeks@chromium.org>
Cr-Commit-Position: refs/heads/main@{#79772}
parent 899f0af7
This diff is collapsed.
...@@ -69,23 +69,40 @@ class JSHeapBroker; ...@@ -69,23 +69,40 @@ class JSHeapBroker;
} // namespace v8 } // namespace v8
#ifndef TEST #ifndef TEST
#define TEST(Name) \ #define TEST(Name) \
static void Test##Name(); \ static void Test##Name(); \
CcTest register_test_##Name(Test##Name, __FILE__, #Name, true, true); \ CcTest register_test_##Name(Test##Name, __FILE__, #Name, true, true, \
nullptr); \
static void Test##Name() static void Test##Name()
#endif #endif
#ifndef UNINITIALIZED_TEST #ifndef UNINITIALIZED_TEST
#define UNINITIALIZED_TEST(Name) \ #define UNINITIALIZED_TEST(Name) \
static void Test##Name(); \ static void Test##Name(); \
CcTest register_test_##Name(Test##Name, __FILE__, #Name, true, false); \ CcTest register_test_##Name(Test##Name, __FILE__, #Name, true, false, \
nullptr); \
static void Test##Name() static void Test##Name()
#endif #endif
#ifndef TEST_WITH_PLATFORM
#define TEST_WITH_PLATFORM(Name, PlatformClass) \
static void Test##Name(PlatformClass& platform); \
static void TestWithoutPlatform##Name() { \
Test##Name(*static_cast<PlatformClass*>(i::V8::GetCurrentPlatform())); \
} \
CcTest register_test_##Name(TestWithoutPlatform##Name, __FILE__, #Name, \
true, true, \
[]() -> std::unique_ptr<TestPlatform> { \
return std::make_unique<PlatformClass>(); \
}); \
static void Test##Name(PlatformClass& platform)
#endif
#ifndef DISABLED_TEST #ifndef DISABLED_TEST
#define DISABLED_TEST(Name) \ #define DISABLED_TEST(Name) \
static void Test##Name(); \ static void Test##Name(); \
CcTest register_test_##Name(Test##Name, __FILE__, #Name, false, true); \ CcTest register_test_##Name(Test##Name, __FILE__, #Name, false, true, \
nullptr); \
static void Test##Name() static void Test##Name()
#endif #endif
...@@ -97,9 +114,9 @@ class JSHeapBroker; ...@@ -97,9 +114,9 @@ class JSHeapBroker;
// to correctly associate the tests with the test suite using them. // to correctly associate the tests with the test suite using them.
// 2. To actually execute the tests, create an instance of the class // 2. To actually execute the tests, create an instance of the class
// containing the MEMBER_TESTs. // containing the MEMBER_TESTs.
#define MEMBER_TEST(Name) \ #define MEMBER_TEST(Name) \
CcTest register_test_##Name = \ CcTest register_test_##Name = \
CcTest(Test##Name, kTestFileName, #Name, true, true); \ CcTest(Test##Name, kTestFileName, #Name, true, true, nullptr); \
static void Test##Name() static void Test##Name()
#define EXTENSION_LIST(V) \ #define EXTENSION_LIST(V) \
...@@ -119,18 +136,19 @@ static constexpr const char* kExtensionName[kMaxExtensions] = { ...@@ -119,18 +136,19 @@ static constexpr const char* kExtensionName[kMaxExtensions] = {
EXTENSION_LIST(DEFINE_EXTENSION_NAME)}; EXTENSION_LIST(DEFINE_EXTENSION_NAME)};
#undef DEFINE_EXTENSION_NAME #undef DEFINE_EXTENSION_NAME
class CcTest;
class TestPlatform;
using CcTestMapType = std::map<std::string, CcTest*>;
class CcTest { class CcTest {
public: public:
using TestFunction = void(); using TestFunction = void();
using TestPlatformFactory = std::unique_ptr<TestPlatform>();
CcTest(TestFunction* callback, const char* file, const char* name, CcTest(TestFunction* callback, const char* file, const char* name,
bool enabled, bool initialize); bool enabled, bool initialize,
~CcTest() { i::DeleteArray(file_); } TestPlatformFactory* platform_factory = nullptr);
void Run(); void Run(const char* argv0);
static CcTest* last() { return last_; }
CcTest* prev() { return prev_; }
const char* file() { return file_; }
const char* name() { return name_; }
bool enabled() { return enabled_; }
static v8::Isolate* isolate() { static v8::Isolate* isolate() {
CHECK_NOT_NULL(isolate_); CHECK_NOT_NULL(isolate_);
...@@ -150,6 +168,8 @@ class CcTest { ...@@ -150,6 +168,8 @@ class CcTest {
static i::Heap* heap(); static i::Heap* heap();
static i::ReadOnlyHeap* read_only_heap(); static i::ReadOnlyHeap* read_only_heap();
static v8::Platform* default_platform() { return default_platform_; }
static void AddGlobalFunction(v8::Local<v8::Context> env, const char* name, static void AddGlobalFunction(v8::Local<v8::Context> env, const char* name,
v8::FunctionCallback callback); v8::FunctionCallback callback);
static void CollectGarbage(i::AllocationSpace space, static void CollectGarbage(i::AllocationSpace space,
...@@ -178,9 +198,6 @@ class CcTest { ...@@ -178,9 +198,6 @@ class CcTest {
// This must be called first in a test. // This must be called first in a test.
static void InitializeVM(); static void InitializeVM();
// Only for UNINITIALIZED_TESTs
static void DisableAutomaticDispose();
// Helper function to configure a context. // Helper function to configure a context.
// Must be in a HandleScope. // Must be in a HandleScope.
static v8::Local<v8::Context> NewContext( static v8::Local<v8::Context> NewContext(
...@@ -196,21 +213,17 @@ class CcTest { ...@@ -196,21 +213,17 @@ class CcTest {
return NewContext(CcTestExtensionFlags{extensions}, isolate); return NewContext(CcTestExtensionFlags{extensions}, isolate);
} }
static void TearDown();
private: private:
static CcTest* last_; static std::unordered_map<std::string, CcTest*>* tests_;
static v8::ArrayBuffer::Allocator* allocator_; static v8::ArrayBuffer::Allocator* allocator_;
static v8::Isolate* isolate_; static v8::Isolate* isolate_;
static v8::Platform* default_platform_;
static bool initialize_called_; static bool initialize_called_;
static v8::base::Atomic32 isolate_used_; static v8::base::Atomic32 isolate_used_;
TestFunction* callback_; TestFunction* callback_;
const char* file_;
const char* name_;
bool enabled_;
bool initialize_; bool initialize_;
CcTest* prev_; TestPlatformFactory* test_platform_factory_;
friend int main(int argc, char** argv); friend int main(int argc, char** argv);
friend class ManualGCScope; friend class ManualGCScope;
...@@ -632,8 +645,7 @@ static inline void DisableDebugger(v8::Isolate* isolate) { ...@@ -632,8 +645,7 @@ static inline void DisableDebugger(v8::Isolate* isolate) {
static inline void EmptyMessageQueues(v8::Isolate* isolate) { static inline void EmptyMessageQueues(v8::Isolate* isolate) {
while (v8::platform::PumpMessageLoop(v8::internal::V8::GetCurrentPlatform(), while (v8::platform::PumpMessageLoop(CcTest::default_platform(), isolate)) {
isolate)) {
} }
} }
...@@ -701,19 +713,10 @@ class V8_NODISCARD ManualGCScope { ...@@ -701,19 +713,10 @@ class V8_NODISCARD ManualGCScope {
}; };
// This is a base class that can be overridden to implement a test platform. It // This is a base class that can be overridden to implement a test platform. It
// delegates all operations to a given platform at the time of construction. // delegates all operations to the default platform.
// Breaks if tasks cache the platform themselves.
class TestPlatform : public v8::Platform { class TestPlatform : public v8::Platform {
public: public:
// Users inheriting from `TestPlatform` need to invoke `NotifyPlatformReady()` ~TestPlatform() override = default;
// at the end of their constructor.
void NotifyPlatformReady();
// Eagerly removes the platform from being used by V8.
void RemovePlatform();
TestPlatform(const TestPlatform&) = delete;
TestPlatform& operator=(const TestPlatform&) = delete;
// v8::Platform implementation. // v8::Platform implementation.
v8::PageAllocator* GetPageAllocator() override; v8::PageAllocator* GetPageAllocator() override;
...@@ -734,14 +737,7 @@ class TestPlatform : public v8::Platform { ...@@ -734,14 +737,7 @@ class TestPlatform : public v8::Platform {
v8::TracingController* GetTracingController() override; v8::TracingController* GetTracingController() override;
protected: protected:
TestPlatform(); TestPlatform() = default;
~TestPlatform() override;
v8::Platform* old_platform() const { return old_platform_; }
private:
std::atomic<v8::Platform*> old_platform_;
bool active_ = false;
}; };
#if defined(USE_SIMULATOR) #if defined(USE_SIMULATOR)
......
...@@ -35,11 +35,10 @@ namespace heap { ...@@ -35,11 +35,10 @@ namespace heap {
class MockPlatform : public TestPlatform { class MockPlatform : public TestPlatform {
public: public:
MockPlatform() : taskrunner_(new MockTaskRunner()) { NotifyPlatformReady(); } MockPlatform() : taskrunner_(new MockTaskRunner()) {}
~MockPlatform() override { ~MockPlatform() override {
RemovePlatform();
for (auto& task : worker_tasks_) { for (auto& task : worker_tasks_) {
old_platform()->CallOnWorkerThread(std::move(task)); CcTest::default_platform()->CallOnWorkerThread(std::move(task));
} }
worker_tasks_.clear(); worker_tasks_.clear();
} }
...@@ -103,14 +102,11 @@ class MockPlatform : public TestPlatform { ...@@ -103,14 +102,11 @@ class MockPlatform : public TestPlatform {
std::vector<std::unique_ptr<Task>> worker_tasks_; std::vector<std::unique_ptr<Task>> worker_tasks_;
}; };
UNINITIALIZED_TEST(IncrementalMarkingUsingTasks) { TEST_WITH_PLATFORM(IncrementalMarkingUsingTasks, MockPlatform) {
if (!i::FLAG_incremental_marking) return; if (!i::FLAG_incremental_marking) return;
FLAG_stress_concurrent_allocation = false; // For SimulateFullSpace. FLAG_stress_concurrent_allocation = false; // For SimulateFullSpace.
FLAG_stress_incremental_marking = false; FLAG_stress_incremental_marking = false;
MockPlatform platform; v8::Isolate* isolate = CcTest::isolate();
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
v8::Isolate* isolate = v8::Isolate::New(create_params);
{ {
v8::HandleScope handle_scope(isolate); v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = CcTest::NewContext(isolate); v8::Local<v8::Context> context = CcTest::NewContext(isolate);
...@@ -134,7 +130,6 @@ UNINITIALIZED_TEST(IncrementalMarkingUsingTasks) { ...@@ -134,7 +130,6 @@ UNINITIALIZED_TEST(IncrementalMarkingUsingTasks) {
} }
CHECK(marking->IsStopped()); CHECK(marking->IsStopped());
} }
isolate->Dispose();
} }
} // namespace heap } // namespace heap
......
...@@ -132,10 +132,7 @@ namespace { ...@@ -132,10 +132,7 @@ namespace {
class MockPlatform : public TestPlatform { class MockPlatform : public TestPlatform {
public: public:
MockPlatform() : mock_task_runner_(new MockTaskRunner()) { MockPlatform() : mock_task_runner_(new MockTaskRunner()) {}
NotifyPlatformReady();
}
~MockPlatform() override { RemovePlatform(); }
std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner( std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
v8::Isolate*) override { v8::Isolate*) override {
...@@ -199,14 +196,10 @@ class MockMeasureMemoryDelegate : public v8::MeasureMemoryDelegate { ...@@ -199,14 +196,10 @@ class MockMeasureMemoryDelegate : public v8::MeasureMemoryDelegate {
} // namespace } // namespace
TEST(RandomizedTimeout) { TEST_WITH_PLATFORM(RandomizedTimeout, MockPlatform) {
MockPlatform platform;
v8::Isolate::CreateParams create_params; v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
// We have to create the isolate manually here. Using CcTest::isolate() would v8::Isolate* isolate = CcTest::isolate();
// lead to the situation when the isolate outlives MockPlatform which may lead
// to UAF on the background thread.
v8::Isolate* isolate = v8::Isolate::New(create_params);
std::vector<double> delays; std::vector<double> delays;
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
isolate->MeasureMemory(std::make_unique<MockMeasureMemoryDelegate>()); isolate->MeasureMemory(std::make_unique<MockMeasureMemoryDelegate>());
...@@ -214,7 +207,6 @@ TEST(RandomizedTimeout) { ...@@ -214,7 +207,6 @@ TEST(RandomizedTimeout) {
platform.PerformTask(); platform.PerformTask();
} }
std::sort(delays.begin(), delays.end()); std::sort(delays.begin(), delays.end());
isolate->Dispose();
CHECK_LT(delays[0], delays.back()); CHECK_LT(delays[0], delays.back());
} }
......
...@@ -20,11 +20,9 @@ namespace heap { ...@@ -20,11 +20,9 @@ namespace heap {
class MockPlatformForUnmapper : public TestPlatform { class MockPlatformForUnmapper : public TestPlatform {
public: public:
MockPlatformForUnmapper() { NotifyPlatformReady(); }
~MockPlatformForUnmapper() override { ~MockPlatformForUnmapper() override {
RemovePlatform();
for (auto& task : worker_tasks_) { for (auto& task : worker_tasks_) {
old_platform()->CallOnWorkerThread(std::move(task)); CcTest::default_platform()->CallOnWorkerThread(std::move(task));
} }
worker_tasks_.clear(); worker_tasks_.clear();
} }
......
...@@ -32,11 +32,7 @@ namespace { ...@@ -32,11 +32,7 @@ namespace {
// Implementation of v8::Platform that can register OOM callbacks. // Implementation of v8::Platform that can register OOM callbacks.
class AllocationPlatform : public TestPlatform { class AllocationPlatform : public TestPlatform {
public: public:
AllocationPlatform() { AllocationPlatform() { current_platform = this; }
current_platform = this;
NotifyPlatformReady();
}
~AllocationPlatform() override { RemovePlatform(); }
void OnCriticalMemoryPressure() override { oom_callback_called = true; } void OnCriticalMemoryPressure() override { oom_callback_called = true; }
...@@ -94,8 +90,7 @@ void OnAlignedAllocOOM(const char* location, const char* message) { ...@@ -94,8 +90,7 @@ void OnAlignedAllocOOM(const char* location, const char* message) {
} // namespace } // namespace
TEST(AccountingAllocatorOOM) { TEST_WITH_PLATFORM(AccountingAllocatorOOM, AllocationPlatform) {
AllocationPlatform platform;
v8::internal::AccountingAllocator allocator; v8::internal::AccountingAllocator allocator;
CHECK(!platform.oom_callback_called); CHECK(!platform.oom_callback_called);
const bool support_compression = false; const bool support_compression = false;
...@@ -105,8 +100,7 @@ TEST(AccountingAllocatorOOM) { ...@@ -105,8 +100,7 @@ TEST(AccountingAllocatorOOM) {
CHECK_EQ(result == nullptr, platform.oom_callback_called); CHECK_EQ(result == nullptr, platform.oom_callback_called);
} }
TEST(AccountingAllocatorCurrentAndMax) { TEST_WITH_PLATFORM(AccountingAllocatorCurrentAndMax, AllocationPlatform) {
AllocationPlatform platform;
v8::internal::AccountingAllocator allocator; v8::internal::AccountingAllocator allocator;
static constexpr size_t kAllocationSizes[] = {51, 231, 27}; static constexpr size_t kAllocationSizes[] = {51, 231, 27};
std::vector<v8::internal::Segment*> segments; std::vector<v8::internal::Segment*> segments;
...@@ -134,8 +128,7 @@ TEST(AccountingAllocatorCurrentAndMax) { ...@@ -134,8 +128,7 @@ TEST(AccountingAllocatorCurrentAndMax) {
CHECK(!platform.oom_callback_called); CHECK(!platform.oom_callback_called);
} }
TEST(MallocedOperatorNewOOM) { TEST_WITH_PLATFORM(MallocedOperatorNewOOM, AllocationPlatform) {
AllocationPlatform platform;
CHECK(!platform.oom_callback_called); CHECK(!platform.oom_callback_called);
CcTest::isolate()->SetFatalErrorHandler(OnMallocedOperatorNewOOM); CcTest::isolate()->SetFatalErrorHandler(OnMallocedOperatorNewOOM);
// On failure, this won't return, since a Malloced::New failure is fatal. // On failure, this won't return, since a Malloced::New failure is fatal.
...@@ -145,8 +138,7 @@ TEST(MallocedOperatorNewOOM) { ...@@ -145,8 +138,7 @@ TEST(MallocedOperatorNewOOM) {
CHECK_EQ(result == nullptr, platform.oom_callback_called); CHECK_EQ(result == nullptr, platform.oom_callback_called);
} }
TEST(NewArrayOOM) { TEST_WITH_PLATFORM(NewArrayOOM, AllocationPlatform) {
AllocationPlatform platform;
CHECK(!platform.oom_callback_called); CHECK(!platform.oom_callback_called);
CcTest::isolate()->SetFatalErrorHandler(OnNewArrayOOM); CcTest::isolate()->SetFatalErrorHandler(OnNewArrayOOM);
// On failure, this won't return, since a NewArray failure is fatal. // On failure, this won't return, since a NewArray failure is fatal.
...@@ -156,8 +148,7 @@ TEST(NewArrayOOM) { ...@@ -156,8 +148,7 @@ TEST(NewArrayOOM) {
CHECK_EQ(result == nullptr, platform.oom_callback_called); CHECK_EQ(result == nullptr, platform.oom_callback_called);
} }
TEST(AlignedAllocOOM) { TEST_WITH_PLATFORM(AlignedAllocOOM, AllocationPlatform) {
AllocationPlatform platform;
CHECK(!platform.oom_callback_called); CHECK(!platform.oom_callback_called);
CcTest::isolate()->SetFatalErrorHandler(OnAlignedAllocOOM); CcTest::isolate()->SetFatalErrorHandler(OnAlignedAllocOOM);
// On failure, this won't return, since an AlignedAlloc failure is fatal. // On failure, this won't return, since an AlignedAlloc failure is fatal.
...@@ -168,8 +159,7 @@ TEST(AlignedAllocOOM) { ...@@ -168,8 +159,7 @@ TEST(AlignedAllocOOM) {
CHECK_EQ(result == nullptr, platform.oom_callback_called); CHECK_EQ(result == nullptr, platform.oom_callback_called);
} }
TEST(AllocVirtualMemoryOOM) { TEST_WITH_PLATFORM(AllocVirtualMemoryOOM, AllocationPlatform) {
AllocationPlatform platform;
CHECK(!platform.oom_callback_called); CHECK(!platform.oom_callback_called);
v8::internal::VirtualMemory result(v8::internal::GetPlatformPageAllocator(), v8::internal::VirtualMemory result(v8::internal::GetPlatformPageAllocator(),
GetHugeMemoryAmount(), nullptr); GetHugeMemoryAmount(), nullptr);
...@@ -177,8 +167,7 @@ TEST(AllocVirtualMemoryOOM) { ...@@ -177,8 +167,7 @@ TEST(AllocVirtualMemoryOOM) {
CHECK_IMPLIES(!result.IsReserved(), platform.oom_callback_called); CHECK_IMPLIES(!result.IsReserved(), platform.oom_callback_called);
} }
TEST(AlignedAllocVirtualMemoryOOM) { TEST_WITH_PLATFORM(AlignedAllocVirtualMemoryOOM, AllocationPlatform) {
AllocationPlatform platform;
CHECK(!platform.oom_callback_called); CHECK(!platform.oom_callback_called);
v8::internal::VirtualMemory result(v8::internal::GetPlatformPageAllocator(), v8::internal::VirtualMemory result(v8::internal::GetPlatformPageAllocator(),
GetHugeMemoryAmount(), nullptr, GetHugeMemoryAmount(), nullptr,
......
...@@ -33,6 +33,8 @@ ...@@ -33,6 +33,8 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "test/cctest/cctest.h"
#if V8_OS_POSIX #if V8_OS_POSIX
#include <unistd.h> #include <unistd.h>
#endif #endif
...@@ -23104,9 +23106,6 @@ namespace { ...@@ -23104,9 +23106,6 @@ namespace {
class MockPlatform final : public TestPlatform { class MockPlatform final : public TestPlatform {
public: public:
MockPlatform() { NotifyPlatformReady(); }
~MockPlatform() final { RemovePlatform(); }
bool dump_without_crashing_called() const { bool dump_without_crashing_called() const {
return dump_without_crashing_called_; return dump_without_crashing_called_;
} }
...@@ -23119,9 +23118,7 @@ class MockPlatform final : public TestPlatform { ...@@ -23119,9 +23118,7 @@ class MockPlatform final : public TestPlatform {
} // namespace } // namespace
TEST(DumpOnJavascriptExecution) { TEST_WITH_PLATFORM(DumpOnJavascriptExecution, MockPlatform) {
MockPlatform platform;
LocalContext context; LocalContext context;
v8::Isolate* isolate = context->GetIsolate(); v8::Isolate* isolate = context->GetIsolate();
v8::HandleScope scope(isolate); v8::HandleScope scope(isolate);
...@@ -530,10 +530,7 @@ class DiscardedSamplesDelegateImpl : public v8::DiscardedSamplesDelegate { ...@@ -530,10 +530,7 @@ class DiscardedSamplesDelegateImpl : public v8::DiscardedSamplesDelegate {
class MockPlatform final : public TestPlatform { class MockPlatform final : public TestPlatform {
public: public:
MockPlatform() : mock_task_runner_(new MockTaskRunner()) { MockPlatform() : mock_task_runner_(new MockTaskRunner()) {}
NotifyPlatformReady();
}
~MockPlatform() override { RemovePlatform(); }
std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner( std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
v8::Isolate*) override { v8::Isolate*) override {
...@@ -561,6 +558,8 @@ class MockPlatform final : public TestPlatform { ...@@ -561,6 +558,8 @@ class MockPlatform final : public TestPlatform {
} }
bool IdleTasksEnabled() override { return false; } bool IdleTasksEnabled() override { return false; }
bool NonNestableTasksEnabled() const override { return true; }
bool NonNestableDelayedTasksEnabled() const override { return true; }
int posted_count() { return posted_count_; } int posted_count() { return posted_count_; }
...@@ -574,12 +573,11 @@ class MockPlatform final : public TestPlatform { ...@@ -574,12 +573,11 @@ class MockPlatform final : public TestPlatform {
}; };
} // namespace } // namespace
TEST(MaxSamplesCallback) { TEST_WITH_PLATFORM(MaxSamplesCallback, MockPlatform) {
i::Isolate* isolate = CcTest::i_isolate(); i::Isolate* isolate = CcTest::i_isolate();
CpuProfilesCollection profiles(isolate); CpuProfilesCollection profiles(isolate);
CpuProfiler profiler(isolate); CpuProfiler profiler(isolate);
profiles.set_cpu_profiler(&profiler); profiles.set_cpu_profiler(&profiler);
MockPlatform* mock_platform = new MockPlatform();
std::unique_ptr<DiscardedSamplesDelegateImpl> impl = std::unique_ptr<DiscardedSamplesDelegateImpl> impl =
std::make_unique<DiscardedSamplesDelegateImpl>( std::make_unique<DiscardedSamplesDelegateImpl>(
DiscardedSamplesDelegateImpl()); DiscardedSamplesDelegateImpl());
...@@ -600,7 +598,7 @@ TEST(MaxSamplesCallback) { ...@@ -600,7 +598,7 @@ TEST(MaxSamplesCallback) {
profiles.AddPathToCurrentProfiles( profiles.AddPathToCurrentProfiles(
sample1.timestamp, symbolized.stack_trace, symbolized.src_line, true, sample1.timestamp, symbolized.stack_trace, symbolized.src_line, true,
base::TimeDelta(), StateTag::JS, EmbedderStateTag::EMPTY); base::TimeDelta(), StateTag::JS, EmbedderStateTag::EMPTY);
CHECK_EQ(0, mock_platform->posted_count()); CHECK_EQ(0, platform.posted_count());
TickSample sample2; TickSample sample2;
sample2.timestamp = v8::base::TimeTicks::Now(); sample2.timestamp = v8::base::TimeTicks::Now();
sample2.pc = ToPointer(0x1925); sample2.pc = ToPointer(0x1925);
...@@ -610,7 +608,7 @@ TEST(MaxSamplesCallback) { ...@@ -610,7 +608,7 @@ TEST(MaxSamplesCallback) {
profiles.AddPathToCurrentProfiles( profiles.AddPathToCurrentProfiles(
sample2.timestamp, symbolized.stack_trace, symbolized.src_line, true, sample2.timestamp, symbolized.stack_trace, symbolized.src_line, true,
base::TimeDelta(), StateTag::JS, EmbedderStateTag::EMPTY); base::TimeDelta(), StateTag::JS, EmbedderStateTag::EMPTY);
CHECK_EQ(1, mock_platform->posted_count()); CHECK_EQ(1, platform.posted_count());
TickSample sample3; TickSample sample3;
sample3.timestamp = v8::base::TimeTicks::Now(); sample3.timestamp = v8::base::TimeTicks::Now();
sample3.pc = ToPointer(0x1510); sample3.pc = ToPointer(0x1510);
...@@ -619,11 +617,10 @@ TEST(MaxSamplesCallback) { ...@@ -619,11 +617,10 @@ TEST(MaxSamplesCallback) {
profiles.AddPathToCurrentProfiles( profiles.AddPathToCurrentProfiles(
sample3.timestamp, symbolized.stack_trace, symbolized.src_line, true, sample3.timestamp, symbolized.stack_trace, symbolized.src_line, true,
base::TimeDelta(), StateTag::JS, EmbedderStateTag::EMPTY); base::TimeDelta(), StateTag::JS, EmbedderStateTag::EMPTY);
CHECK_EQ(1, mock_platform->posted_count()); CHECK_EQ(1, platform.posted_count());
// Teardown // Teardown
profiles.StopProfiling(""); profiles.StopProfiling("");
delete mock_platform;
} }
TEST(NoSamples) { TEST(NoSamples) {
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <string.h> #include <string.h>
#include "include/v8-function.h" #include "include/v8-function.h"
#include "include/v8-platform.h"
#include "src/init/v8.h" #include "src/init/v8.h"
#include "src/tracing/trace-event.h" #include "src/tracing/trace-event.h"
#include "test/cctest/cctest.h" #include "test/cctest/cctest.h"
...@@ -86,9 +87,6 @@ class MockTracingController : public v8::TracingController { ...@@ -86,9 +87,6 @@ class MockTracingController : public v8::TracingController {
class MockTracingPlatform : public TestPlatform { class MockTracingPlatform : public TestPlatform {
public: public:
MockTracingPlatform() { NotifyPlatformReady(); }
~MockTracingPlatform() override { RemovePlatform(); }
v8::TracingController* GetTracingController() override { v8::TracingController* GetTracingController() override {
return &tracing_controller_; return &tracing_controller_;
} }
...@@ -107,18 +105,14 @@ class MockTracingPlatform : public TestPlatform { ...@@ -107,18 +105,14 @@ class MockTracingPlatform : public TestPlatform {
} // namespace } // namespace
TEST(TraceEventDisabledCategory) { TEST_WITH_PLATFORM(TraceEventDisabledCategory, MockTracingPlatform) {
MockTracingPlatform platform;
// Disabled category, will not add events. // Disabled category, will not add events.
TRACE_EVENT_BEGIN0("cat", "e1"); TRACE_EVENT_BEGIN0("cat", "e1");
TRACE_EVENT_END0("cat", "e1"); TRACE_EVENT_END0("cat", "e1");
CHECK_EQ(0, platform.NumberOfTraceObjects()); CHECK_EQ(0, platform.NumberOfTraceObjects());
} }
TEST(TraceEventNoArgs) { TEST_WITH_PLATFORM(TraceEventNoArgs, MockTracingPlatform) {
MockTracingPlatform platform;
// Enabled category will add 2 events. // Enabled category will add 2 events.
TRACE_EVENT_BEGIN0("v8-cat", "e1"); TRACE_EVENT_BEGIN0("v8-cat", "e1");
TRACE_EVENT_END0("v8-cat", "e1"); TRACE_EVENT_END0("v8-cat", "e1");
...@@ -133,9 +127,7 @@ TEST(TraceEventNoArgs) { ...@@ -133,9 +127,7 @@ TEST(TraceEventNoArgs) {
CHECK_EQ(0, platform.GetTraceObject(1)->num_args); CHECK_EQ(0, platform.GetTraceObject(1)->num_args);
} }
TEST(TraceEventWithOneArg) { TEST_WITH_PLATFORM(TraceEventWithOneArg, MockTracingPlatform) {
MockTracingPlatform platform;
TRACE_EVENT_BEGIN1("v8-cat", "e1", "arg1", 42); TRACE_EVENT_BEGIN1("v8-cat", "e1", "arg1", 42);
TRACE_EVENT_END1("v8-cat", "e1", "arg1", 42); TRACE_EVENT_END1("v8-cat", "e1", "arg1", 42);
TRACE_EVENT_BEGIN1("v8-cat", "e2", "arg1", "abc"); TRACE_EVENT_BEGIN1("v8-cat", "e2", "arg1", "abc");
...@@ -149,9 +141,7 @@ TEST(TraceEventWithOneArg) { ...@@ -149,9 +141,7 @@ TEST(TraceEventWithOneArg) {
CHECK_EQ(1, platform.GetTraceObject(3)->num_args); CHECK_EQ(1, platform.GetTraceObject(3)->num_args);
} }
TEST(TraceEventWithTwoArgs) { TEST_WITH_PLATFORM(TraceEventWithTwoArgs, MockTracingPlatform) {
MockTracingPlatform platform;
TRACE_EVENT_BEGIN2("v8-cat", "e1", "arg1", 42, "arg2", "abc"); TRACE_EVENT_BEGIN2("v8-cat", "e1", "arg1", 42, "arg2", "abc");
TRACE_EVENT_END2("v8-cat", "e1", "arg1", 42, "arg2", "abc"); TRACE_EVENT_END2("v8-cat", "e1", "arg1", 42, "arg2", "abc");
TRACE_EVENT_BEGIN2("v8-cat", "e2", "arg1", "abc", "arg2", 43); TRACE_EVENT_BEGIN2("v8-cat", "e2", "arg1", "abc", "arg2", 43);
...@@ -165,9 +155,7 @@ TEST(TraceEventWithTwoArgs) { ...@@ -165,9 +155,7 @@ TEST(TraceEventWithTwoArgs) {
CHECK_EQ(2, platform.GetTraceObject(3)->num_args); CHECK_EQ(2, platform.GetTraceObject(3)->num_args);
} }
TEST(ScopedTraceEvent) { TEST_WITH_PLATFORM(ScopedTraceEvent, MockTracingPlatform) {
MockTracingPlatform platform;
{ TRACE_EVENT0("v8-cat", "e"); } { TRACE_EVENT0("v8-cat", "e"); }
CHECK_EQ(1, platform.NumberOfTraceObjects()); CHECK_EQ(1, platform.NumberOfTraceObjects());
...@@ -184,9 +172,7 @@ TEST(ScopedTraceEvent) { ...@@ -184,9 +172,7 @@ TEST(ScopedTraceEvent) {
CHECK_EQ(2, platform.GetTraceObject(2)->num_args); CHECK_EQ(2, platform.GetTraceObject(2)->num_args);
} }
TEST(TestEventWithFlow) { TEST_WITH_PLATFORM(TestEventWithFlow, MockTracingPlatform) {
MockTracingPlatform platform;
static uint64_t bind_id = 21; static uint64_t bind_id = 21;
{ {
TRACE_EVENT_WITH_FLOW0("v8-cat", "f1", bind_id, TRACE_EVENT_FLAG_FLOW_OUT); TRACE_EVENT_WITH_FLOW0("v8-cat", "f1", bind_id, TRACE_EVENT_FLAG_FLOW_OUT);
...@@ -208,9 +194,7 @@ TEST(TestEventWithFlow) { ...@@ -208,9 +194,7 @@ TEST(TestEventWithFlow) {
CHECK_EQ(TRACE_EVENT_FLAG_FLOW_IN, platform.GetTraceObject(2)->flags); CHECK_EQ(TRACE_EVENT_FLAG_FLOW_IN, platform.GetTraceObject(2)->flags);
} }
TEST(TestEventWithId) { TEST_WITH_PLATFORM(TestEventWithId, MockTracingPlatform) {
MockTracingPlatform platform;
static uint64_t event_id = 21; static uint64_t event_id = 21;
TRACE_EVENT_ASYNC_BEGIN0("v8-cat", "a1", event_id); TRACE_EVENT_ASYNC_BEGIN0("v8-cat", "a1", event_id);
TRACE_EVENT_ASYNC_END0("v8-cat", "a1", event_id); TRACE_EVENT_ASYNC_END0("v8-cat", "a1", event_id);
...@@ -222,9 +206,7 @@ TEST(TestEventWithId) { ...@@ -222,9 +206,7 @@ TEST(TestEventWithId) {
CHECK_EQ(event_id, platform.GetTraceObject(1)->id); CHECK_EQ(event_id, platform.GetTraceObject(1)->id);
} }
TEST(TestEventWithTimestamp) { TEST_WITH_PLATFORM(TestEventWithTimestamp, MockTracingPlatform) {
MockTracingPlatform platform;
TRACE_EVENT_INSTANT_WITH_TIMESTAMP0("v8-cat", "0arg", TRACE_EVENT_INSTANT_WITH_TIMESTAMP0("v8-cat", "0arg",
TRACE_EVENT_SCOPE_GLOBAL, 1729); TRACE_EVENT_SCOPE_GLOBAL, 1729);
TRACE_EVENT_INSTANT_WITH_TIMESTAMP1("v8-cat", "1arg", TRACE_EVENT_INSTANT_WITH_TIMESTAMP1("v8-cat", "1arg",
...@@ -251,9 +233,8 @@ TEST(TestEventWithTimestamp) { ...@@ -251,9 +233,8 @@ TEST(TestEventWithTimestamp) {
CHECK_EQ(32832, platform.GetTraceObject(4)->timestamp); CHECK_EQ(32832, platform.GetTraceObject(4)->timestamp);
} }
TEST(BuiltinsIsTraceCategoryEnabled) { TEST_WITH_PLATFORM(BuiltinsIsTraceCategoryEnabled, MockTracingPlatform) {
CcTest::InitializeVM(); CcTest::InitializeVM();
MockTracingPlatform platform;
v8::Isolate* isolate = CcTest::isolate(); v8::Isolate* isolate = CcTest::isolate();
v8::HandleScope handle_scope(isolate); v8::HandleScope handle_scope(isolate);
...@@ -299,9 +280,8 @@ TEST(BuiltinsIsTraceCategoryEnabled) { ...@@ -299,9 +280,8 @@ TEST(BuiltinsIsTraceCategoryEnabled) {
} }
} }
TEST(BuiltinsTrace) { TEST_WITH_PLATFORM(BuiltinsTrace, MockTracingPlatform) {
CcTest::InitializeVM(); CcTest::InitializeVM();
MockTracingPlatform platform;
v8::Isolate* isolate = CcTest::isolate(); v8::Isolate* isolate = CcTest::isolate();
v8::HandleScope handle_scope(isolate); v8::HandleScope handle_scope(isolate);
......
...@@ -29,12 +29,9 @@ namespace wasm { ...@@ -29,12 +29,9 @@ namespace wasm {
class MockPlatform final : public TestPlatform { class MockPlatform final : public TestPlatform {
public: public:
MockPlatform() : task_runner_(std::make_shared<MockTaskRunner>()) { MockPlatform() : task_runner_(std::make_shared<MockTaskRunner>()) {}
NotifyPlatformReady();
}
~MockPlatform() { ~MockPlatform() {
RemovePlatform();
for (auto* job_handle : job_handles_) job_handle->ResetPlatform(); for (auto* job_handle : job_handles_) job_handle->ResetPlatform();
} }
...@@ -239,25 +236,18 @@ class StreamTester { ...@@ -239,25 +236,18 @@ class StreamTester {
}; };
} // namespace } // namespace
#define RUN_STREAM(name) \ #define RUN_STREAM(name) \
MockPlatform mock_platform; \ v8::Isolate* isolate = CcTest::isolate(); \
CHECK_EQ(V8::GetCurrentPlatform(), &mock_platform); \ v8::HandleScope handle_scope(isolate); \
v8::Isolate::CreateParams create_params; \ v8::Local<v8::Context> context = v8::Context::New(isolate); \
create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); \ v8::Context::Scope context_scope(context); \
v8::Isolate* isolate = v8::Isolate::New(create_params); \ RunStream_##name(&platform, isolate);
{ \
v8::HandleScope handle_scope(isolate); \
v8::Local<v8::Context> context = v8::Context::New(isolate); \
v8::Context::Scope context_scope(context); \
RunStream_##name(&mock_platform, isolate); \
} \
isolate->Dispose();
#define STREAM_TEST(name) \ #define STREAM_TEST(name) \
void RunStream_##name(MockPlatform*, v8::Isolate*); \ void RunStream_##name(MockPlatform*, v8::Isolate*); \
UNINITIALIZED_TEST(Async##name) { RUN_STREAM(name); } \ TEST_WITH_PLATFORM(Async##name, MockPlatform) { RUN_STREAM(name); } \
\ \
UNINITIALIZED_TEST(SingleThreaded##name) { \ TEST_WITH_PLATFORM(SingleThreaded##name, MockPlatform) { \
i::FlagScope<bool> single_threaded_scope(&i::FLAG_single_threaded, true); \ i::FlagScope<bool> single_threaded_scope(&i::FLAG_single_threaded, true); \
RUN_STREAM(name); \ RUN_STREAM(name); \
} \ } \
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "include/libplatform/libplatform.h" #include "include/libplatform/libplatform.h"
#include "include/v8-metrics.h" #include "include/v8-metrics.h"
#include "include/v8-platform.h"
#include "src/api/api-inl.h" #include "src/api/api-inl.h"
#include "src/base/platform/time.h" #include "src/base/platform/time.h"
#include "src/wasm/wasm-engine.h" #include "src/wasm/wasm-engine.h"
...@@ -24,12 +25,9 @@ namespace { ...@@ -24,12 +25,9 @@ namespace {
class MockPlatform final : public TestPlatform { class MockPlatform final : public TestPlatform {
public: public:
MockPlatform() : task_runner_(std::make_shared<MockTaskRunner>()) { MockPlatform() : task_runner_(std::make_shared<MockTaskRunner>()) {}
NotifyPlatformReady();
}
~MockPlatform() override { ~MockPlatform() override {
RemovePlatform();
for (auto* job_handle : job_handles_) job_handle->ResetPlatform(); for (auto* job_handle : job_handles_) job_handle->ResetPlatform();
} }
...@@ -208,32 +206,24 @@ class TestCompileResolver : public CompilationResultResolver { ...@@ -208,32 +206,24 @@ class TestCompileResolver : public CompilationResultResolver {
} // namespace } // namespace
#define RUN_COMPILE(name) \ #define RUN_COMPILE(name) \
MockPlatform mock_platform; \ v8::HandleScope handle_scope(CcTest::isolate()); \
CHECK_EQ(V8::GetCurrentPlatform(), &mock_platform); \ v8::Local<v8::Context> context = v8::Context::New(CcTest::isolate()); \
v8::Isolate::CreateParams create_params; \ v8::Context::Scope context_scope(context); \
create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); \ Isolate* i_isolate = CcTest::i_isolate(); \
v8::Isolate* isolate = v8::Isolate::New(create_params); \ testing::SetupIsolateForWasmModule(i_isolate); \
{ \ RunCompile_##name(&platform, i_isolate);
v8::HandleScope handle_scope(isolate); \
v8::Local<v8::Context> context = v8::Context::New(isolate); \
v8::Context::Scope context_scope(context); \
Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); \
testing::SetupIsolateForWasmModule(i_isolate); \
RunCompile_##name(&mock_platform, i_isolate); \
} \
isolate->Dispose();
#define COMPILE_TEST(name) \ #define COMPILE_TEST(name) \
void RunCompile_##name(MockPlatform*, i::Isolate*); \ void RunCompile_##name(MockPlatform*, i::Isolate*); \
UNINITIALIZED_TEST(Sync##name) { \ TEST_WITH_PLATFORM(Sync##name, MockPlatform) { \
i::FlagScope<bool> sync_scope(&i::FLAG_wasm_async_compilation, false); \ i::FlagScope<bool> sync_scope(&i::FLAG_wasm_async_compilation, false); \
RUN_COMPILE(name); \ RUN_COMPILE(name); \
} \ } \
\ \
UNINITIALIZED_TEST(Async##name) { RUN_COMPILE(name); } \ TEST_WITH_PLATFORM(Async##name, MockPlatform) { RUN_COMPILE(name); } \
\ \
UNINITIALIZED_TEST(Streaming##name) { \ TEST_WITH_PLATFORM(Streaming##name, MockPlatform) { \
i::FlagScope<bool> streaming_scope(&i::FLAG_wasm_test_streaming, true); \ i::FlagScope<bool> streaming_scope(&i::FLAG_wasm_test_streaming, true); \
RUN_COMPILE(name); \ RUN_COMPILE(name); \
} \ } \
......
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