2008-09-09 20:08:45 +00:00
|
|
|
// Copyright 2008 the V8 project authors. All rights reserved.
|
2008-08-22 13:33:59 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#ifndef CCTEST_H_
|
|
|
|
#define CCTEST_H_
|
|
|
|
|
2016-09-01 12:01:33 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2015-07-15 12:26:06 +00:00
|
|
|
#include "include/libplatform/libplatform.h"
|
2017-08-01 14:16:30 +00:00
|
|
|
#include "include/v8-platform.h"
|
2019-01-14 16:52:32 +00:00
|
|
|
#include "src/base/enum-set.h"
|
2019-05-21 09:30:15 +00:00
|
|
|
#include "src/codegen/register-configuration.h"
|
2017-03-10 07:06:25 +00:00
|
|
|
#include "src/debug/debug-interface.h"
|
2022-07-18 10:54:58 +00:00
|
|
|
#include "src/execution/isolate-inl.h"
|
2020-02-12 11:45:31 +00:00
|
|
|
#include "src/execution/simulator.h"
|
2018-04-09 19:11:22 +00:00
|
|
|
#include "src/heap/factory.h"
|
2020-07-23 10:17:18 +00:00
|
|
|
#include "src/objects/js-function.h"
|
2019-05-23 08:51:46 +00:00
|
|
|
#include "src/objects/objects.h"
|
2016-09-20 16:07:25 +00:00
|
|
|
#include "src/zone/accounting-allocator.h"
|
2009-11-04 08:51:48 +00:00
|
|
|
|
2016-09-01 12:01:33 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace base {
|
|
|
|
|
|
|
|
class RandomNumberGenerator;
|
|
|
|
|
|
|
|
} // namespace base
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
2018-10-02 14:24:19 +00:00
|
|
|
const auto GetRegConfig = RegisterConfiguration::Default;
|
|
|
|
|
2016-09-01 12:01:33 +00:00
|
|
|
class HandleScope;
|
|
|
|
class Zone;
|
|
|
|
|
2019-01-29 12:50:53 +00:00
|
|
|
namespace compiler {
|
|
|
|
|
|
|
|
class JSHeapBroker;
|
|
|
|
|
|
|
|
} // namespace compiler
|
|
|
|
|
2016-09-01 12:01:33 +00:00
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
} // namespace v8
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
#ifndef TEST
|
2022-04-05 08:16:57 +00:00
|
|
|
#define TEST(Name) \
|
|
|
|
static void Test##Name(); \
|
|
|
|
CcTest register_test_##Name(Test##Name, __FILE__, #Name, true, true, \
|
|
|
|
nullptr); \
|
2013-09-19 07:33:45 +00:00
|
|
|
static void Test##Name()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef UNINITIALIZED_TEST
|
2022-04-05 08:16:57 +00:00
|
|
|
#define UNINITIALIZED_TEST(Name) \
|
|
|
|
static void Test##Name(); \
|
|
|
|
CcTest register_test_##Name(Test##Name, __FILE__, #Name, true, false, \
|
|
|
|
nullptr); \
|
2014-09-19 08:01:35 +00:00
|
|
|
static void Test##Name()
|
|
|
|
#endif
|
|
|
|
|
2022-04-05 08:16:57 +00:00
|
|
|
#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
|
|
|
|
|
2008-09-05 00:04:37 +00:00
|
|
|
#ifndef DISABLED_TEST
|
2022-04-05 08:16:57 +00:00
|
|
|
#define DISABLED_TEST(Name) \
|
|
|
|
static void Test##Name(); \
|
|
|
|
CcTest register_test_##Name(Test##Name, __FILE__, #Name, false, true, \
|
|
|
|
nullptr); \
|
2008-08-22 13:33:59 +00:00
|
|
|
static void Test##Name()
|
|
|
|
#endif
|
|
|
|
|
2021-03-24 16:22:36 +00:00
|
|
|
// Similar to TEST, but used when test definitions appear as members of a
|
|
|
|
// (probably parameterized) class. This allows re-using the given tests multiple
|
|
|
|
// times. For this to work, the following conditions must hold:
|
|
|
|
// 1. The class has a template parameter named kTestFileName of type char
|
|
|
|
// const*, which is instantiated with __FILE__ at the *use site*, in order
|
|
|
|
// to correctly associate the tests with the test suite using them.
|
|
|
|
// 2. To actually execute the tests, create an instance of the class
|
|
|
|
// containing the MEMBER_TESTs.
|
2022-04-05 08:16:57 +00:00
|
|
|
#define MEMBER_TEST(Name) \
|
|
|
|
CcTest register_test_##Name = \
|
|
|
|
CcTest(Test##Name, kTestFileName, #Name, true, true, nullptr); \
|
2021-03-24 16:22:36 +00:00
|
|
|
static void Test##Name()
|
|
|
|
|
2014-01-17 10:52:00 +00:00
|
|
|
#define EXTENSION_LIST(V) \
|
|
|
|
V(GC_EXTENSION, "v8/gc") \
|
|
|
|
V(PRINT_EXTENSION, "v8/print") \
|
|
|
|
V(PROFILER_EXTENSION, "v8/profiler") \
|
|
|
|
V(TRACE_EXTENSION, "v8/trace")
|
2013-04-10 08:29:39 +00:00
|
|
|
|
|
|
|
#define DEFINE_EXTENSION_ID(Name, Ident) Name##_ID,
|
2019-01-16 17:31:37 +00:00
|
|
|
enum CcTestExtensionId { EXTENSION_LIST(DEFINE_EXTENSION_ID) kMaxExtensions };
|
2013-04-10 08:29:39 +00:00
|
|
|
#undef DEFINE_EXTENSION_ID
|
|
|
|
|
2019-01-16 17:31:37 +00:00
|
|
|
using CcTestExtensionFlags = v8::base::EnumSet<CcTestExtensionId>;
|
2013-04-10 08:29:39 +00:00
|
|
|
|
2019-01-16 17:31:37 +00:00
|
|
|
#define DEFINE_EXTENSION_NAME(Name, Ident) Ident,
|
|
|
|
static constexpr const char* kExtensionName[kMaxExtensions] = {
|
|
|
|
EXTENSION_LIST(DEFINE_EXTENSION_NAME)};
|
|
|
|
#undef DEFINE_EXTENSION_NAME
|
2013-09-11 07:14:41 +00:00
|
|
|
|
2022-04-05 08:16:57 +00:00
|
|
|
class CcTest;
|
|
|
|
class TestPlatform;
|
|
|
|
|
|
|
|
using CcTestMapType = std::map<std::string, CcTest*>;
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
class CcTest {
|
|
|
|
public:
|
2019-05-28 08:28:04 +00:00
|
|
|
using TestFunction = void();
|
2022-04-05 08:16:57 +00:00
|
|
|
using TestPlatformFactory = std::unique_ptr<TestPlatform>();
|
2008-09-05 00:04:37 +00:00
|
|
|
CcTest(TestFunction* callback, const char* file, const char* name,
|
2022-04-05 08:16:57 +00:00
|
|
|
bool enabled, bool initialize,
|
|
|
|
TestPlatformFactory* platform_factory = nullptr);
|
|
|
|
void Run(const char* argv0);
|
2013-05-06 13:01:03 +00:00
|
|
|
|
2013-09-19 07:33:45 +00:00
|
|
|
static v8::Isolate* isolate() {
|
2017-10-18 09:06:55 +00:00
|
|
|
CHECK_NOT_NULL(isolate_);
|
2017-05-30 07:44:37 +00:00
|
|
|
v8::base::Relaxed_Store(&isolate_used_, 1);
|
2013-09-19 13:30:47 +00:00
|
|
|
return isolate_;
|
2013-09-19 07:33:45 +00:00
|
|
|
}
|
2013-04-10 08:29:39 +00:00
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
static i::Isolate* InitIsolateOnce() {
|
|
|
|
if (!initialize_called_) InitializeVM();
|
|
|
|
return i_isolate();
|
|
|
|
}
|
|
|
|
|
2013-09-05 08:48:34 +00:00
|
|
|
static i::Isolate* i_isolate() {
|
2013-09-19 07:33:45 +00:00
|
|
|
return reinterpret_cast<i::Isolate*>(isolate());
|
2013-09-05 08:48:34 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 12:01:33 +00:00
|
|
|
static i::Heap* heap();
|
2019-05-02 15:35:51 +00:00
|
|
|
static i::ReadOnlyHeap* read_only_heap();
|
2013-09-19 09:46:15 +00:00
|
|
|
|
2022-04-05 08:16:57 +00:00
|
|
|
static v8::Platform* default_platform() { return default_platform_; }
|
|
|
|
|
[cpu-profiler] Ensure sampled thread has Isolate lock under Windows
While the sampler checked if the sampled thread had the Isolate locked
(if locks are being used) under Linux, the check was not done under
Windows (or Fuchsia) which meant that in a multi-threading application
under Windows, thread locking was not checked making it prone to seg
faults and the like as the profiler would be using isolate->js_entry_sp
to determine the stack to walk but isolate->js_entry_sp is the stack
pointer for the thread that currently has the Isolate lock so, if the
sampled thread does not have the lock, the sampler woud be iterating
over the wrong stack, one that might actually be actively changing on
another thread. The fix was to move the lock check into CpuSampler
and Ticker (--prof) so all OSes would do the correct check.
The basic concept is that on all operating systems a CpuProfiler, and
so its corresponding CpuCampler, the profiler is tied to a thread.
This is not based on first principles or anything, it's simply the
way it works in V8, though it is a useful conceit as it makes
visualization and interpretation of profile data much easier.
To collect a sample on a thread associated with a profiler the thread
must be stopped for obvious reasons -- walking the stack of a running
thread is a formula for disaster. The mechanism for stopping a thread
is OS-specific and is done in sample.cc. There are currently three
basic approaches, one for Linux/Unix variants, one for Windows and one
for Fuchsia. The approaches vary as to which thread actually collects
the sample -- under Linux the sample is actually collected on the
(interrupted) sampled thread whereas under Fuchsia/Windows it's on
a separate thread.
However, in a multi-threaded environment (where Locker is used), it's
not sufficient for the sampled thread to be stopped. Because the stack
walk involves looking in the Isolate heap, no other thread can be
messing with the heap while the sample is collected. The only ways to
ensure this would be to either stop all threads whenever collecting a
sample, or to ensure that the thread being sampled holds the Isolate
lock so prevents other threads from messing with the heap. While there
might be something to be said for the "stop all threads" approach, the
current approach in V8 is to only stop the sampled thread so, if in a
multi-threaded environment, the profiler must check if the thread being
sampled holds the Isolate lock.
Since this check must be done, independent of which thread the sample
is being collected on (since it varies from OS to OS), the approach is
to save the thread id of the thread to be profiled/sampled when the
CpuSampler is instantiated (on all OSes it is instantiated on the
sampled thread) and then check that thread id against the Isolate lock
holder thread id before collecting a sample. If it matches, we know
sample.cc has stop the sampled thread, one way or another, and we know
that no other thread can mess with the heap (since the stopped thread
holds the Isolate lock) so it's safe to walk the stack and collect data
from the heap so the sample can be taken. It it doesn't match, we can't
safely collect the sample so we don't.
Bug: v8:10850
Change-Id: Iba6cabcd3e11a19c261c004103e37e806934dc6f
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2411343
Reviewed-by: Peter Marshall <petermarshall@chromium.org>
Commit-Queue: Peter Marshall <petermarshall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#69952}
2020-09-16 12:00:30 +00:00
|
|
|
static void AddGlobalFunction(v8::Local<v8::Context> env, const char* name,
|
|
|
|
v8::FunctionCallback callback);
|
2020-08-25 15:33:11 +00:00
|
|
|
static void CollectGarbage(i::AllocationSpace space,
|
|
|
|
i::Isolate* isolate = nullptr);
|
2018-09-19 08:19:40 +00:00
|
|
|
static void CollectAllGarbage(i::Isolate* isolate = nullptr);
|
|
|
|
static void CollectAllAvailableGarbage(i::Isolate* isolate = nullptr);
|
|
|
|
static void PreciseCollectAllGarbage(i::Isolate* isolate = nullptr);
|
2016-09-07 10:02:58 +00:00
|
|
|
|
2020-05-26 11:17:38 +00:00
|
|
|
static i::Handle<i::String> MakeString(const char* str);
|
|
|
|
static i::Handle<i::String> MakeName(const char* str, int suffix);
|
|
|
|
|
2016-09-01 12:01:33 +00:00
|
|
|
static v8::base::RandomNumberGenerator* random_number_generator();
|
2014-07-30 13:54:45 +00:00
|
|
|
|
2016-09-01 12:01:33 +00:00
|
|
|
static v8::Local<v8::Object> global();
|
2013-09-23 11:25:52 +00:00
|
|
|
|
2015-04-29 09:54:34 +00:00
|
|
|
static v8::ArrayBuffer::Allocator* array_buffer_allocator() {
|
|
|
|
return allocator_;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_array_buffer_allocator(
|
|
|
|
v8::ArrayBuffer::Allocator* allocator) {
|
|
|
|
allocator_ = allocator;
|
|
|
|
}
|
|
|
|
|
2013-09-19 13:30:47 +00:00
|
|
|
// TODO(dcarney): Remove.
|
|
|
|
// This must be called first in a test.
|
2016-09-01 12:01:33 +00:00
|
|
|
static void InitializeVM();
|
2013-09-19 13:30:47 +00:00
|
|
|
|
|
|
|
// Helper function to configure a context.
|
|
|
|
// Must be in a HandleScope.
|
|
|
|
static v8::Local<v8::Context> NewContext(
|
2019-01-16 17:31:37 +00:00
|
|
|
v8::Isolate* isolate = CcTest::isolate()) {
|
|
|
|
return NewContext({}, isolate);
|
|
|
|
}
|
|
|
|
static v8::Local<v8::Context> NewContext(
|
|
|
|
CcTestExtensionFlags extension_flags,
|
2013-09-19 13:30:47 +00:00
|
|
|
v8::Isolate* isolate = CcTest::isolate());
|
2019-11-26 16:25:14 +00:00
|
|
|
static v8::Local<v8::Context> NewContext(
|
|
|
|
std::initializer_list<CcTestExtensionId> extensions,
|
|
|
|
v8::Isolate* isolate = CcTest::isolate()) {
|
|
|
|
return NewContext(CcTestExtensionFlags{extensions}, isolate);
|
|
|
|
}
|
2013-01-18 07:20:17 +00:00
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
private:
|
2022-04-05 08:16:57 +00:00
|
|
|
static std::unordered_map<std::string, CcTest*>* tests_;
|
2022-01-14 09:09:43 +00:00
|
|
|
static v8::ArrayBuffer::Allocator* allocator_;
|
|
|
|
static v8::Isolate* isolate_;
|
2022-04-05 08:16:57 +00:00
|
|
|
static v8::Platform* default_platform_;
|
2022-01-14 09:09:43 +00:00
|
|
|
static bool initialize_called_;
|
|
|
|
static v8::base::Atomic32 isolate_used_;
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
TestFunction* callback_;
|
2013-09-19 07:33:45 +00:00
|
|
|
bool initialize_;
|
2022-04-05 08:16:57 +00:00
|
|
|
TestPlatformFactory* test_platform_factory_;
|
2022-01-14 09:09:43 +00:00
|
|
|
|
|
|
|
friend int main(int argc, char** argv);
|
|
|
|
friend class ManualGCScope;
|
2008-08-22 13:33:59 +00:00
|
|
|
};
|
|
|
|
|
2009-11-04 08:51:48 +00:00
|
|
|
// Switches between all the Api tests using the threading support.
|
|
|
|
// In order to get a surprising but repeatable pattern of thread
|
|
|
|
// switching it has extra semaphores to control the order in which
|
|
|
|
// the tests alternate, not relying solely on the big V8 lock.
|
|
|
|
//
|
|
|
|
// A test is augmented with calls to ApiTestFuzzer::Fuzz() in its
|
|
|
|
// callbacks. This will have no effect when we are not running the
|
|
|
|
// thread fuzzing test. In the thread fuzzing test it will
|
|
|
|
// pseudorandomly select a successor thread and switch execution
|
|
|
|
// to that thread, suspending the current test.
|
2014-06-30 13:25:46 +00:00
|
|
|
class ApiTestFuzzer: public v8::base::Thread {
|
2009-11-04 08:51:48 +00:00
|
|
|
public:
|
|
|
|
void CallTest();
|
|
|
|
|
|
|
|
// The ApiTestFuzzer is also a Thread, so it has a Run method.
|
2018-09-14 15:34:02 +00:00
|
|
|
void Run() override;
|
2009-11-04 08:51:48 +00:00
|
|
|
|
2016-11-04 09:04:11 +00:00
|
|
|
enum PartOfTest {
|
|
|
|
FIRST_PART,
|
|
|
|
SECOND_PART,
|
|
|
|
THIRD_PART,
|
|
|
|
FOURTH_PART,
|
|
|
|
FIFTH_PART,
|
|
|
|
SIXTH_PART,
|
|
|
|
SEVENTH_PART,
|
|
|
|
EIGHTH_PART,
|
|
|
|
LAST_PART = EIGHTH_PART
|
|
|
|
};
|
2009-11-04 08:51:48 +00:00
|
|
|
|
2012-01-13 13:09:52 +00:00
|
|
|
static void SetUp(PartOfTest part);
|
2009-11-04 08:51:48 +00:00
|
|
|
static void RunAllTests();
|
|
|
|
static void TearDown();
|
|
|
|
// This method switches threads if we are running the Threading test.
|
|
|
|
// Otherwise it does nothing.
|
|
|
|
static void Fuzz();
|
2011-09-08 19:57:14 +00:00
|
|
|
|
2009-11-04 08:51:48 +00:00
|
|
|
private:
|
2013-01-18 07:20:17 +00:00
|
|
|
explicit ApiTestFuzzer(int num)
|
2014-08-12 13:33:35 +00:00
|
|
|
: Thread(Options("ApiTestFuzzer")),
|
2013-01-18 07:20:17 +00:00
|
|
|
test_number_(num),
|
2013-09-02 12:26:06 +00:00
|
|
|
gate_(0),
|
2014-08-12 13:33:35 +00:00
|
|
|
active_(true) {}
|
2018-09-14 15:34:02 +00:00
|
|
|
~ApiTestFuzzer() override = default;
|
2013-01-18 07:20:17 +00:00
|
|
|
|
2009-11-04 08:51:48 +00:00
|
|
|
static bool fuzzing_;
|
|
|
|
static int tests_being_run_;
|
|
|
|
static int current_;
|
|
|
|
static int active_tests_;
|
|
|
|
static bool NextThread();
|
|
|
|
int test_number_;
|
2014-06-30 13:25:46 +00:00
|
|
|
v8::base::Semaphore gate_;
|
2009-11-04 08:51:48 +00:00
|
|
|
bool active_;
|
|
|
|
void ContextSwitch();
|
|
|
|
static int GetNextTestNumber();
|
2014-06-30 13:25:46 +00:00
|
|
|
static v8::base::Semaphore all_tests_done_;
|
2009-11-04 08:51:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#define THREADED_TEST(Name) \
|
|
|
|
static void Test##Name(); \
|
|
|
|
RegisterThreadedTest register_##Name(Test##Name, #Name); \
|
|
|
|
/* */ TEST(Name)
|
|
|
|
|
|
|
|
class RegisterThreadedTest {
|
|
|
|
public:
|
|
|
|
explicit RegisterThreadedTest(CcTest::TestFunction* callback,
|
|
|
|
const char* name)
|
2017-10-13 16:33:03 +00:00
|
|
|
: fuzzer_(nullptr), callback_(callback), name_(name) {
|
2009-11-04 08:51:48 +00:00
|
|
|
prev_ = first_;
|
|
|
|
first_ = this;
|
|
|
|
count_++;
|
|
|
|
}
|
|
|
|
static int count() { return count_; }
|
|
|
|
static RegisterThreadedTest* nth(int i) {
|
|
|
|
CHECK(i < count());
|
|
|
|
RegisterThreadedTest* current = first_;
|
|
|
|
while (i > 0) {
|
|
|
|
i--;
|
|
|
|
current = current->prev_;
|
|
|
|
}
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
CcTest::TestFunction* callback() { return callback_; }
|
|
|
|
ApiTestFuzzer* fuzzer_;
|
|
|
|
const char* name() { return name_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
static RegisterThreadedTest* first_;
|
|
|
|
static int count_;
|
|
|
|
CcTest::TestFunction* callback_;
|
|
|
|
RegisterThreadedTest* prev_;
|
|
|
|
const char* name_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A LocalContext holds a reference to a v8::Context.
|
|
|
|
class LocalContext {
|
|
|
|
public:
|
2018-09-13 14:55:18 +00:00
|
|
|
LocalContext(v8::Isolate* isolate,
|
|
|
|
v8::ExtensionConfiguration* extensions = nullptr,
|
2015-09-07 14:44:46 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> global_template =
|
|
|
|
v8::Local<v8::ObjectTemplate>(),
|
|
|
|
v8::Local<v8::Value> global_object = v8::Local<v8::Value>()) {
|
2013-09-19 10:31:04 +00:00
|
|
|
Initialize(isolate, extensions, global_template, global_object);
|
|
|
|
}
|
|
|
|
|
2018-09-13 14:55:18 +00:00
|
|
|
LocalContext(v8::ExtensionConfiguration* extensions = nullptr,
|
2015-09-07 14:44:46 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> global_template =
|
|
|
|
v8::Local<v8::ObjectTemplate>(),
|
|
|
|
v8::Local<v8::Value> global_object = v8::Local<v8::Value>()) {
|
2013-09-19 10:31:04 +00:00
|
|
|
Initialize(CcTest::isolate(), extensions, global_template, global_object);
|
2009-11-04 08:51:48 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 12:01:33 +00:00
|
|
|
virtual ~LocalContext();
|
2009-11-04 08:51:48 +00:00
|
|
|
|
2013-06-13 09:27:09 +00:00
|
|
|
v8::Context* operator->() {
|
|
|
|
return *reinterpret_cast<v8::Context**>(&context_);
|
|
|
|
}
|
|
|
|
v8::Context* operator*() { return operator->(); }
|
2009-11-04 08:51:48 +00:00
|
|
|
bool IsReady() { return !context_.IsEmpty(); }
|
|
|
|
|
[cpu-profiler] Ensure sampled thread has Isolate lock under Windows
While the sampler checked if the sampled thread had the Isolate locked
(if locks are being used) under Linux, the check was not done under
Windows (or Fuchsia) which meant that in a multi-threading application
under Windows, thread locking was not checked making it prone to seg
faults and the like as the profiler would be using isolate->js_entry_sp
to determine the stack to walk but isolate->js_entry_sp is the stack
pointer for the thread that currently has the Isolate lock so, if the
sampled thread does not have the lock, the sampler woud be iterating
over the wrong stack, one that might actually be actively changing on
another thread. The fix was to move the lock check into CpuSampler
and Ticker (--prof) so all OSes would do the correct check.
The basic concept is that on all operating systems a CpuProfiler, and
so its corresponding CpuCampler, the profiler is tied to a thread.
This is not based on first principles or anything, it's simply the
way it works in V8, though it is a useful conceit as it makes
visualization and interpretation of profile data much easier.
To collect a sample on a thread associated with a profiler the thread
must be stopped for obvious reasons -- walking the stack of a running
thread is a formula for disaster. The mechanism for stopping a thread
is OS-specific and is done in sample.cc. There are currently three
basic approaches, one for Linux/Unix variants, one for Windows and one
for Fuchsia. The approaches vary as to which thread actually collects
the sample -- under Linux the sample is actually collected on the
(interrupted) sampled thread whereas under Fuchsia/Windows it's on
a separate thread.
However, in a multi-threaded environment (where Locker is used), it's
not sufficient for the sampled thread to be stopped. Because the stack
walk involves looking in the Isolate heap, no other thread can be
messing with the heap while the sample is collected. The only ways to
ensure this would be to either stop all threads whenever collecting a
sample, or to ensure that the thread being sampled holds the Isolate
lock so prevents other threads from messing with the heap. While there
might be something to be said for the "stop all threads" approach, the
current approach in V8 is to only stop the sampled thread so, if in a
multi-threaded environment, the profiler must check if the thread being
sampled holds the Isolate lock.
Since this check must be done, independent of which thread the sample
is being collected on (since it varies from OS to OS), the approach is
to save the thread id of the thread to be profiled/sampled when the
CpuSampler is instantiated (on all OSes it is instantiated on the
sampled thread) and then check that thread id against the Isolate lock
holder thread id before collecting a sample. If it matches, we know
sample.cc has stop the sampled thread, one way or another, and we know
that no other thread can mess with the heap (since the stopped thread
holds the Isolate lock) so it's safe to walk the stack and collect data
from the heap so the sample can be taken. It it doesn't match, we can't
safely collect the sample so we don't.
Bug: v8:10850
Change-Id: Iba6cabcd3e11a19c261c004103e37e806934dc6f
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2411343
Reviewed-by: Peter Marshall <petermarshall@chromium.org>
Commit-Queue: Peter Marshall <petermarshall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#69952}
2020-09-16 12:00:30 +00:00
|
|
|
v8::Local<v8::Context> local() const {
|
2013-05-02 20:18:42 +00:00
|
|
|
return v8::Local<v8::Context>::New(isolate_, context_);
|
2009-11-04 08:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-09-07 14:44:46 +00:00
|
|
|
void Initialize(v8::Isolate* isolate, v8::ExtensionConfiguration* extensions,
|
|
|
|
v8::Local<v8::ObjectTemplate> global_template,
|
2016-09-01 12:01:33 +00:00
|
|
|
v8::Local<v8::Value> global_object);
|
2013-09-19 10:31:04 +00:00
|
|
|
|
2009-11-04 08:51:48 +00:00
|
|
|
v8::Persistent<v8::Context> context_;
|
2013-01-25 08:31:46 +00:00
|
|
|
v8::Isolate* isolate_;
|
2009-11-04 08:51:48 +00:00
|
|
|
};
|
|
|
|
|
2014-06-03 15:02:16 +00:00
|
|
|
|
|
|
|
static inline uint16_t* AsciiToTwoByteString(const char* source) {
|
2019-04-29 14:56:08 +00:00
|
|
|
size_t array_length = strlen(source) + 1;
|
2014-06-03 15:02:16 +00:00
|
|
|
uint16_t* converted = i::NewArray<uint16_t>(array_length);
|
2019-04-29 14:56:08 +00:00
|
|
|
for (size_t i = 0; i < array_length; i++) converted[i] = source[i];
|
2014-06-03 15:02:16 +00:00
|
|
|
return converted;
|
|
|
|
}
|
|
|
|
|
2017-08-21 01:58:40 +00:00
|
|
|
template <typename T>
|
|
|
|
static inline i::Handle<T> GetGlobal(const char* name) {
|
|
|
|
i::Isolate* isolate = CcTest::i_isolate();
|
|
|
|
i::Handle<i::String> str_name =
|
|
|
|
isolate->factory()->InternalizeUtf8String(name);
|
|
|
|
|
|
|
|
i::Handle<i::Object> value =
|
2018-07-17 08:49:20 +00:00
|
|
|
i::Object::GetProperty(isolate, isolate->global_object(), str_name)
|
2017-08-21 01:58:40 +00:00
|
|
|
.ToHandleChecked();
|
|
|
|
return i::Handle<T>::cast(value);
|
|
|
|
}
|
2014-06-03 15:02:16 +00:00
|
|
|
|
2019-03-07 18:54:37 +00:00
|
|
|
static inline v8::Local<v8::Boolean> v8_bool(bool val) {
|
|
|
|
return v8::Boolean::New(v8::Isolate::GetCurrent(), val);
|
|
|
|
}
|
|
|
|
|
2009-11-04 08:51:48 +00:00
|
|
|
static inline v8::Local<v8::Value> v8_num(double x) {
|
2014-01-03 14:31:17 +00:00
|
|
|
return v8::Number::New(v8::Isolate::GetCurrent(), x);
|
2009-11-04 08:51:48 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 12:48:26 +00:00
|
|
|
static inline v8::Local<v8::Integer> v8_int(int32_t x) {
|
|
|
|
return v8::Integer::New(v8::Isolate::GetCurrent(), x);
|
|
|
|
}
|
2009-11-04 08:51:48 +00:00
|
|
|
|
[compiler] Re-reland "Faster JS-to-Wasm calls"
This is a reland of 6ada6a90ee387d8de183208b0ef8b786f1768665
- Fixed a GC issue
https://bugs.chromium.org/p/v8/issues/detail?id=11335:
GC expected all arguments on the stack from code with
CodeKind::TURBOFAN to be tagged objects. This is not the case now with
inlined Wasm calls, and this information can be passed in
SafepointEntry for each call site.
- Disabled JS-to-Wasm inlining for calls inside try/catch.
For more details, see updated doc:
https://docs.google.com/document/d/1mXxYnYN77tK-R1JOVo6tFG3jNpMzfueQN1Zp5h3r9aM/edit#
Bug: v8:11092
Original change's description:
> Reland "Faster JS-to-Wasm calls"
>
> This is a reland of 860fcb1bd2bd6447e08f3636874ac7abcd77b781
>
> - Disabled the tests for this feature in V8-lite mode (the original
> change broke V8-lite tests).
> - Also modified test console-profile-wasm.js that was brittle with this
> change because it assumed that there was always a JS-to-Wasm wrapper
> but this is not the case when the TurboFan compilation completes before
> the Liftoff-compiled code starts to run.
>
> More changes in Patchset 8:
>
> - Moved inlining of the "JSToWasm Wrapper" away from simplified-lowering,
> into a new phase, wasm-inlining that reuses the JSInliner reducer.
> The doc
> https://docs.google.com/document/d/1mXxYnYN77tK-R1JOVo6tFG3jNpMzfueQN1Zp5h3r9aM/edit#
> describes the new logic.
>
> - Fixed a couple of small issues in wasm_compiler.cc to make sure that
> the graph "JSToWasm Wrapper" subgraph has a valid Control chain;
> this should solve the problem we had inlining the calls in functions
> that can throw exception.
Original change's description:
> Faster JS-to-Wasm calls
>
> This replaces https://chromium-review.googlesource.com/c/v8/v8/+/2376165/.
>
> Currently JS-to-Wasm calls go through a wrapper/trampoline, built on
> the basis of the signature of a Wasm function to call, and whose task
> is to:
> - set "thread_in_wasm_flag" to true
> - convert the arguments from tagged types into Wasm native types
> - calculate the address of the Wasm function to call and call it
> - convert back the result from Wasm native types into tagged types
> - reset "thread_in_wasm_flag" to false.
>
> This CL tries to improve the performance of JS-to-Wasm calls by
> inlining the code of the JS-to-Wasm wrappers in the call site.
>
> It introduces a new IR operand, JSWasmCall, which replaces JSCall for
> this kind of calls. A 'JSWasmCall' node is associated to
> WasmCallParameters, which contain information about the signature of
> the Wasm function to call.
>
> WasmWrapperGraphBuilder::BuildJSToWasmWrapper is modified to avoid
> generating code to convert the types for the arguments
> of the Wasm function, when the conversion is not necessary.
> The actual inlining of the graph generated for this wrapper happens in
> the simplified-lowering phase.
>
> A new builtin, JSToWasmLazyDeoptContinuation, is introduced to manage
> lazy deoptimizations that can happen if the Wasm function callee calls
> back some JS code that invalidates the compiled JS caller function.
>
Bug: v8:11092
Cq-Include-Trybots: luci.v8.try:v8_linux_arm_lite_rel_ng
Change-Id: Ie052634598754feab4ff36d10fd04e008b5227a5
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2649777
Commit-Queue: Paolo Severini <paolosev@microsoft.com>
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Reviewed-by: Andreas Haas <ahaas@chromium.org>
Reviewed-by: Georg Neis <neis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#72541}
2021-02-04 15:46:10 +00:00
|
|
|
static inline v8::Local<v8::BigInt> v8_bigint(int64_t x) {
|
|
|
|
return v8::BigInt::New(v8::Isolate::GetCurrent(), x);
|
|
|
|
}
|
|
|
|
|
2009-11-04 08:51:48 +00:00
|
|
|
static inline v8::Local<v8::String> v8_str(const char* x) {
|
2020-03-09 10:41:45 +00:00
|
|
|
return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), x).ToLocalChecked();
|
2009-11-04 08:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-19 13:45:42 +00:00
|
|
|
static inline v8::Local<v8::String> v8_str(v8::Isolate* isolate,
|
|
|
|
const char* x) {
|
2020-03-09 10:41:45 +00:00
|
|
|
return v8::String::NewFromUtf8(isolate, x).ToLocalChecked();
|
2015-11-19 13:45:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-03 21:05:04 +00:00
|
|
|
static inline v8::Local<v8::Symbol> v8_symbol(const char* name) {
|
|
|
|
return v8::Symbol::New(v8::Isolate::GetCurrent(), v8_str(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-07 14:44:46 +00:00
|
|
|
static inline v8::Local<v8::Script> v8_compile(v8::Local<v8::String> x) {
|
|
|
|
v8::Local<v8::Script> result;
|
2019-11-29 10:57:29 +00:00
|
|
|
CHECK(v8::Script::Compile(v8::Isolate::GetCurrent()->GetCurrentContext(), x)
|
|
|
|
.ToLocal(&result));
|
|
|
|
return result;
|
2009-11-04 08:51:48 +00:00
|
|
|
}
|
|
|
|
|
2015-09-07 14:44:46 +00:00
|
|
|
static inline v8::Local<v8::Script> v8_compile(const char* x) {
|
|
|
|
return v8_compile(v8_str(x));
|
2014-03-10 11:58:56 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 10:57:29 +00:00
|
|
|
static inline v8::MaybeLocal<v8::Script> v8_try_compile(
|
|
|
|
v8::Local<v8::String> x) {
|
|
|
|
return v8::Script::Compile(v8::Isolate::GetCurrent()->GetCurrentContext(), x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline v8::MaybeLocal<v8::Script> v8_try_compile(const char* x) {
|
|
|
|
return v8_try_compile(v8_str(x));
|
|
|
|
}
|
2014-03-10 11:58:56 +00:00
|
|
|
|
2015-11-26 12:44:11 +00:00
|
|
|
static inline int32_t v8_run_int32value(v8::Local<v8::Script> script) {
|
|
|
|
v8::Local<v8::Context> context = CcTest::isolate()->GetCurrentContext();
|
|
|
|
return script->Run(context).ToLocalChecked()->Int32Value(context).FromJust();
|
|
|
|
}
|
|
|
|
|
2014-03-14 10:20:33 +00:00
|
|
|
static inline v8::Local<v8::Script> CompileWithOrigin(
|
2019-03-07 18:54:37 +00:00
|
|
|
v8::Local<v8::String> source, v8::Local<v8::String> origin_url,
|
2020-11-24 14:07:00 +00:00
|
|
|
bool is_shared_cross_origin) {
|
2021-01-12 11:54:50 +00:00
|
|
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
|
|
|
v8::ScriptOrigin origin(isolate, origin_url, 0, 0, is_shared_cross_origin);
|
2014-03-19 13:24:13 +00:00
|
|
|
v8::ScriptCompiler::Source script_source(source, origin);
|
2021-01-12 11:54:50 +00:00
|
|
|
return v8::ScriptCompiler::Compile(isolate->GetCurrentContext(),
|
|
|
|
&script_source)
|
2015-09-07 14:44:46 +00:00
|
|
|
.ToLocalChecked();
|
2014-03-10 11:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline v8::Local<v8::Script> CompileWithOrigin(
|
2019-03-07 18:54:37 +00:00
|
|
|
v8::Local<v8::String> source, const char* origin_url,
|
|
|
|
bool is_shared_cross_origin) {
|
2020-11-24 14:07:00 +00:00
|
|
|
return CompileWithOrigin(source, v8_str(origin_url), is_shared_cross_origin);
|
2014-03-14 10:20:33 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 18:54:37 +00:00
|
|
|
static inline v8::Local<v8::Script> CompileWithOrigin(
|
|
|
|
const char* source, const char* origin_url, bool is_shared_cross_origin) {
|
|
|
|
return CompileWithOrigin(v8_str(source), v8_str(origin_url),
|
2020-11-24 14:07:00 +00:00
|
|
|
is_shared_cross_origin);
|
2014-03-10 11:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper functions that compile and run the source.
|
2015-09-07 14:44:46 +00:00
|
|
|
static inline v8::MaybeLocal<v8::Value> CompileRun(
|
|
|
|
v8::Local<v8::Context> context, const char* source) {
|
|
|
|
return v8::Script::Compile(context, v8_str(source))
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Run(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-19 13:45:42 +00:00
|
|
|
static inline v8::Local<v8::Value> CompileRunChecked(v8::Isolate* isolate,
|
|
|
|
const char* source) {
|
|
|
|
v8::Local<v8::String> source_string =
|
2020-03-09 10:41:45 +00:00
|
|
|
v8::String::NewFromUtf8(isolate, source).ToLocalChecked();
|
2015-11-19 13:45:42 +00:00
|
|
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
|
|
|
v8::Local<v8::Script> script =
|
|
|
|
v8::Script::Compile(context, source_string).ToLocalChecked();
|
|
|
|
return script->Run(context).ToLocalChecked();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-07 14:44:46 +00:00
|
|
|
static inline v8::Local<v8::Value> CompileRun(v8::Local<v8::String> source) {
|
|
|
|
v8::Local<v8::Value> result;
|
|
|
|
if (v8_compile(source)
|
|
|
|
->Run(v8::Isolate::GetCurrent()->GetCurrentContext())
|
|
|
|
.ToLocal(&result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return v8::Local<v8::Value>();
|
2014-03-10 11:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-26 14:58:51 +00:00
|
|
|
// Helper functions that compile and run the source.
|
2015-09-07 14:44:46 +00:00
|
|
|
static inline v8::Local<v8::Value> CompileRun(const char* source) {
|
|
|
|
return CompileRun(v8_str(source));
|
2015-06-26 14:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-07 14:44:46 +00:00
|
|
|
static inline v8::Local<v8::Value> CompileRun(
|
|
|
|
v8::Local<v8::Context> context, v8::ScriptCompiler::Source* script_source,
|
|
|
|
v8::ScriptCompiler::CompileOptions options) {
|
|
|
|
v8::Local<v8::Value> result;
|
|
|
|
if (v8::ScriptCompiler::Compile(context, script_source, options)
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Run(context)
|
|
|
|
.ToLocal(&result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return v8::Local<v8::Value>();
|
2009-11-04 08:51:48 +00:00
|
|
|
}
|
|
|
|
|
2012-10-01 09:41:18 +00:00
|
|
|
|
2014-03-10 11:58:56 +00:00
|
|
|
// Helper functions that compile and run the source with given origin.
|
2012-09-21 08:09:34 +00:00
|
|
|
static inline v8::Local<v8::Value> CompileRunWithOrigin(const char* source,
|
|
|
|
const char* origin_url,
|
|
|
|
int line_number,
|
|
|
|
int column_number) {
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
2015-09-07 14:44:46 +00:00
|
|
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
2021-01-12 11:54:50 +00:00
|
|
|
v8::ScriptOrigin origin(isolate, v8_str(origin_url), line_number,
|
|
|
|
column_number);
|
2014-03-19 13:24:13 +00:00
|
|
|
v8::ScriptCompiler::Source script_source(v8_str(source), origin);
|
2015-09-07 14:44:46 +00:00
|
|
|
return CompileRun(context, &script_source,
|
|
|
|
v8::ScriptCompiler::CompileOptions());
|
2014-03-13 08:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline v8::Local<v8::Value> CompileRunWithOrigin(
|
2014-03-14 10:20:33 +00:00
|
|
|
v8::Local<v8::String> source, const char* origin_url) {
|
2015-09-07 14:44:46 +00:00
|
|
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
|
|
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
2014-03-19 13:24:13 +00:00
|
|
|
v8::ScriptCompiler::Source script_source(
|
2021-01-12 11:54:50 +00:00
|
|
|
source, v8::ScriptOrigin(isolate, v8_str(origin_url)));
|
2015-09-07 14:44:46 +00:00
|
|
|
return CompileRun(context, &script_source,
|
|
|
|
v8::ScriptCompiler::CompileOptions());
|
2014-03-14 10:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline v8::Local<v8::Value> CompileRunWithOrigin(
|
|
|
|
const char* source, const char* origin_url) {
|
|
|
|
return CompileRunWithOrigin(v8_str(source), origin_url);
|
2012-09-21 08:09:34 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 15:09:20 +00:00
|
|
|
// Run a ScriptStreamingTask in a separate thread.
|
|
|
|
class StreamerThread : public v8::base::Thread {
|
|
|
|
public:
|
|
|
|
static void StartThreadForTaskAndJoin(
|
|
|
|
v8::ScriptCompiler::ScriptStreamingTask* task) {
|
|
|
|
StreamerThread thread(task);
|
|
|
|
CHECK(thread.Start());
|
|
|
|
thread.Join();
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit StreamerThread(v8::ScriptCompiler::ScriptStreamingTask* task)
|
|
|
|
: Thread(Thread::Options()), task_(task) {}
|
|
|
|
|
|
|
|
void Run() override { task_->Run(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
v8::ScriptCompiler::ScriptStreamingTask* task_;
|
|
|
|
};
|
|
|
|
|
2019-01-29 12:50:53 +00:00
|
|
|
// Takes a JSFunction and runs it through the test version of the optimizing
|
|
|
|
// pipeline, allocating the temporary compilation artifacts in a given Zone.
|
2019-05-02 12:17:06 +00:00
|
|
|
// For possible {flags} values, look at OptimizedCompilationInfo::Flag. If
|
|
|
|
// {out_broker} is not nullptr, returns the JSHeapBroker via that (transferring
|
|
|
|
// ownership to the caller).
|
2019-01-29 12:50:53 +00:00
|
|
|
i::Handle<i::JSFunction> Optimize(
|
|
|
|
i::Handle<i::JSFunction> function, i::Zone* zone, i::Isolate* isolate,
|
2019-05-02 12:17:06 +00:00
|
|
|
uint32_t flags,
|
|
|
|
std::unique_ptr<i::compiler::JSHeapBroker>* out_broker = nullptr);
|
2014-06-03 15:02:16 +00:00
|
|
|
|
|
|
|
static inline void ExpectString(const char* code, const char* expected) {
|
|
|
|
v8::Local<v8::Value> result = CompileRun(code);
|
|
|
|
CHECK(result->IsString());
|
2017-08-24 21:49:48 +00:00
|
|
|
v8::String::Utf8Value utf8(v8::Isolate::GetCurrent(), result);
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK_EQ(0, strcmp(expected, *utf8));
|
2014-06-03 15:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void ExpectInt32(const char* code, int expected) {
|
|
|
|
v8::Local<v8::Value> result = CompileRun(code);
|
|
|
|
CHECK(result->IsInt32());
|
2015-09-07 14:44:46 +00:00
|
|
|
CHECK_EQ(expected,
|
|
|
|
result->Int32Value(v8::Isolate::GetCurrent()->GetCurrentContext())
|
|
|
|
.FromJust());
|
2014-06-03 15:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void ExpectBoolean(const char* code, bool expected) {
|
|
|
|
v8::Local<v8::Value> result = CompileRun(code);
|
|
|
|
CHECK(result->IsBoolean());
|
2018-09-24 11:05:31 +00:00
|
|
|
CHECK_EQ(expected, result->BooleanValue(v8::Isolate::GetCurrent()));
|
2014-06-03 15:02:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void ExpectTrue(const char* code) {
|
|
|
|
ExpectBoolean(code, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void ExpectFalse(const char* code) {
|
|
|
|
ExpectBoolean(code, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void ExpectObject(const char* code,
|
|
|
|
v8::Local<v8::Value> expected) {
|
|
|
|
v8::Local<v8::Value> result = CompileRun(code);
|
|
|
|
CHECK(result->SameValue(expected));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void ExpectUndefined(const char* code) {
|
|
|
|
v8::Local<v8::Value> result = CompileRun(code);
|
|
|
|
CHECK(result->IsUndefined());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-11 09:53:50 +00:00
|
|
|
static inline void ExpectNull(const char* code) {
|
|
|
|
v8::Local<v8::Value> result = CompileRun(code);
|
|
|
|
CHECK(result->IsNull());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-29 13:59:34 +00:00
|
|
|
static inline void CheckDoubleEquals(double expected, double actual) {
|
|
|
|
const double kEpsilon = 1e-10;
|
|
|
|
CHECK_LE(expected, actual + kEpsilon);
|
|
|
|
CHECK_GE(expected, actual - kEpsilon);
|
|
|
|
}
|
|
|
|
|
2017-03-10 07:06:25 +00:00
|
|
|
static v8::debug::DebugDelegate dummy_delegate;
|
2015-06-16 07:11:11 +00:00
|
|
|
|
2015-12-02 12:47:16 +00:00
|
|
|
static inline void EnableDebugger(v8::Isolate* isolate) {
|
2017-03-10 07:06:25 +00:00
|
|
|
v8::debug::SetDebugDelegate(isolate, &dummy_delegate);
|
2015-06-16 07:11:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-02 12:47:16 +00:00
|
|
|
static inline void DisableDebugger(v8::Isolate* isolate) {
|
2017-03-10 07:06:25 +00:00
|
|
|
v8::debug::SetDebugDelegate(isolate, nullptr);
|
2015-12-02 12:47:16 +00:00
|
|
|
}
|
2015-06-16 07:11:11 +00:00
|
|
|
|
|
|
|
|
2015-07-15 12:26:06 +00:00
|
|
|
static inline void EmptyMessageQueues(v8::Isolate* isolate) {
|
2022-04-05 08:16:57 +00:00
|
|
|
while (v8::platform::PumpMessageLoop(CcTest::default_platform(), isolate)) {
|
2015-09-28 08:18:29 +00:00
|
|
|
}
|
2015-07-15 12:26:06 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 12:01:33 +00:00
|
|
|
class InitializedHandleScopeImpl;
|
2015-07-15 12:26:06 +00:00
|
|
|
|
2020-11-26 10:08:27 +00:00
|
|
|
class V8_NODISCARD InitializedHandleScope {
|
2014-07-30 13:54:45 +00:00
|
|
|
public:
|
2021-05-03 13:35:54 +00:00
|
|
|
explicit InitializedHandleScope(i::Isolate* isolate = nullptr);
|
2016-09-01 12:01:33 +00:00
|
|
|
~InitializedHandleScope();
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
// Prefixing the below with main_ reduces a lot of naming clashes.
|
|
|
|
i::Isolate* main_isolate() { return main_isolate_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
i::Isolate* main_isolate_;
|
2016-09-01 12:01:33 +00:00
|
|
|
std::unique_ptr<InitializedHandleScopeImpl> initialized_handle_scope_impl_;
|
2014-07-30 13:54:45 +00:00
|
|
|
};
|
|
|
|
|
2020-11-26 10:08:27 +00:00
|
|
|
class V8_NODISCARD HandleAndZoneScope : public InitializedHandleScope {
|
2014-07-30 13:54:45 +00:00
|
|
|
public:
|
2020-07-23 10:06:02 +00:00
|
|
|
explicit HandleAndZoneScope(bool support_zone_compression = false);
|
2016-09-01 12:01:33 +00:00
|
|
|
~HandleAndZoneScope();
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
// Prefixing the below with main_ reduces a lot of naming clashes.
|
2016-09-01 12:01:33 +00:00
|
|
|
i::Zone* main_zone() { return main_zone_.get(); }
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
private:
|
2016-09-20 16:07:25 +00:00
|
|
|
v8::internal::AccountingAllocator allocator_;
|
2016-09-01 12:01:33 +00:00
|
|
|
std::unique_ptr<i::Zone> main_zone_;
|
2014-07-30 13:54:45 +00:00
|
|
|
};
|
|
|
|
|
2016-12-16 13:24:07 +00:00
|
|
|
class StaticOneByteResource : public v8::String::ExternalOneByteStringResource {
|
|
|
|
public:
|
|
|
|
explicit StaticOneByteResource(const char* data) : data_(data) {}
|
|
|
|
|
2018-09-21 09:44:22 +00:00
|
|
|
~StaticOneByteResource() override = default;
|
2016-12-16 13:24:07 +00:00
|
|
|
|
2018-09-14 15:34:02 +00:00
|
|
|
const char* data() const override { return data_; }
|
2016-12-16 13:24:07 +00:00
|
|
|
|
2018-09-14 15:34:02 +00:00
|
|
|
size_t length() const override { return strlen(data_); }
|
2016-12-16 13:24:07 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
const char* data_;
|
|
|
|
};
|
|
|
|
|
2022-01-14 09:09:43 +00:00
|
|
|
// ManualGCScope allows for disabling GC heuristics. This is useful for tests
|
|
|
|
// that want to check specific corner cases around GC.
|
|
|
|
//
|
|
|
|
// The scope will finalize any ongoing GC on the provided Isolate. If no Isolate
|
|
|
|
// is manually provided, it is assumed that a CcTest setup (e.g.
|
|
|
|
// CcTest::InitializeVM()) is used.
|
2020-11-26 10:08:27 +00:00
|
|
|
class V8_NODISCARD ManualGCScope {
|
2017-06-12 11:03:01 +00:00
|
|
|
public:
|
2022-01-14 09:09:43 +00:00
|
|
|
explicit ManualGCScope(
|
|
|
|
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(CcTest::isolate_));
|
|
|
|
~ManualGCScope();
|
2017-06-12 11:03:01 +00:00
|
|
|
|
|
|
|
private:
|
2022-01-14 09:09:43 +00:00
|
|
|
const bool flag_concurrent_marking_;
|
|
|
|
const bool flag_concurrent_sweeping_;
|
|
|
|
const bool flag_stress_concurrent_allocation_;
|
|
|
|
const bool flag_stress_incremental_marking_;
|
|
|
|
const bool flag_parallel_marking_;
|
|
|
|
const bool flag_detect_ineffective_gcs_near_heap_limit_;
|
2017-06-12 11:03:01 +00:00
|
|
|
};
|
|
|
|
|
2022-03-23 19:33:48 +00:00
|
|
|
// This is a base class that can be overridden to implement a test platform. It
|
2022-04-05 08:16:57 +00:00
|
|
|
// delegates all operations to the default platform.
|
2017-08-01 14:16:30 +00:00
|
|
|
class TestPlatform : public v8::Platform {
|
|
|
|
public:
|
2022-04-05 08:16:57 +00:00
|
|
|
~TestPlatform() override = default;
|
2020-11-06 02:39:19 +00:00
|
|
|
|
2017-08-01 14:16:30 +00:00
|
|
|
// v8::Platform implementation.
|
2022-03-23 19:33:48 +00:00
|
|
|
v8::PageAllocator* GetPageAllocator() override;
|
|
|
|
void OnCriticalMemoryPressure() override;
|
|
|
|
int NumberOfWorkerThreads() override;
|
2017-11-15 14:08:34 +00:00
|
|
|
std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
|
2022-03-23 19:33:48 +00:00
|
|
|
v8::Isolate* isolate) override;
|
|
|
|
void CallOnWorkerThread(std::unique_ptr<v8::Task> task) override;
|
2018-04-30 16:50:03 +00:00
|
|
|
void CallDelayedOnWorkerThread(std::unique_ptr<v8::Task> task,
|
2022-03-23 19:33:48 +00:00
|
|
|
double delay_in_seconds) override;
|
2020-04-24 13:14:50 +00:00
|
|
|
std::unique_ptr<v8::JobHandle> PostJob(
|
|
|
|
v8::TaskPriority priority,
|
2022-03-23 19:33:48 +00:00
|
|
|
std::unique_ptr<v8::JobTask> job_task) override;
|
2022-05-26 20:34:54 +00:00
|
|
|
std::unique_ptr<v8::JobHandle> CreateJob(
|
|
|
|
v8::TaskPriority priority,
|
|
|
|
std::unique_ptr<v8::JobTask> job_task) override;
|
2022-03-23 19:33:48 +00:00
|
|
|
double MonotonicallyIncreasingTime() override;
|
|
|
|
double CurrentClockTimeMillis() override;
|
|
|
|
bool IdleTasksEnabled(v8::Isolate* isolate) override;
|
|
|
|
v8::TracingController* GetTracingController() override;
|
2017-08-01 14:16:30 +00:00
|
|
|
|
|
|
|
protected:
|
2022-04-05 08:16:57 +00:00
|
|
|
TestPlatform() = default;
|
2017-08-01 14:16:30 +00:00
|
|
|
};
|
|
|
|
|
2020-02-12 11:45:31 +00:00
|
|
|
#if defined(USE_SIMULATOR)
|
|
|
|
class SimulatorHelper {
|
|
|
|
public:
|
|
|
|
inline bool Init(v8::Isolate* isolate) {
|
|
|
|
simulator_ = reinterpret_cast<v8::internal::Isolate*>(isolate)
|
|
|
|
->thread_local_top()
|
|
|
|
->simulator_;
|
|
|
|
// Check if there is active simulator.
|
|
|
|
return simulator_ != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void FillRegisters(v8::RegisterState* state) {
|
|
|
|
#if V8_TARGET_ARCH_ARM
|
|
|
|
state->pc = reinterpret_cast<void*>(simulator_->get_pc());
|
|
|
|
state->sp = reinterpret_cast<void*>(
|
|
|
|
simulator_->get_register(v8::internal::Simulator::sp));
|
|
|
|
state->fp = reinterpret_cast<void*>(
|
|
|
|
simulator_->get_register(v8::internal::Simulator::r11));
|
|
|
|
state->lr = reinterpret_cast<void*>(
|
|
|
|
simulator_->get_register(v8::internal::Simulator::lr));
|
|
|
|
#elif V8_TARGET_ARCH_ARM64
|
|
|
|
if (simulator_->sp() == 0 || simulator_->fp() == 0) {
|
|
|
|
// It's possible that the simulator is interrupted while it is updating
|
|
|
|
// the sp or fp register. ARM64 simulator does this in two steps:
|
|
|
|
// first setting it to zero and then setting it to a new value.
|
|
|
|
// Bailout if sp/fp doesn't contain the new value.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
state->pc = reinterpret_cast<void*>(simulator_->pc());
|
|
|
|
state->sp = reinterpret_cast<void*>(simulator_->sp());
|
|
|
|
state->fp = reinterpret_cast<void*>(simulator_->fp());
|
|
|
|
state->lr = reinterpret_cast<void*>(simulator_->lr());
|
|
|
|
#elif V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64
|
|
|
|
state->pc = reinterpret_cast<void*>(simulator_->get_pc());
|
|
|
|
state->sp = reinterpret_cast<void*>(
|
|
|
|
simulator_->get_register(v8::internal::Simulator::sp));
|
|
|
|
state->fp = reinterpret_cast<void*>(
|
|
|
|
simulator_->get_register(v8::internal::Simulator::fp));
|
|
|
|
#elif V8_TARGET_ARCH_PPC || V8_TARGET_ARCH_PPC64
|
|
|
|
state->pc = reinterpret_cast<void*>(simulator_->get_pc());
|
|
|
|
state->sp = reinterpret_cast<void*>(
|
|
|
|
simulator_->get_register(v8::internal::Simulator::sp));
|
|
|
|
state->fp = reinterpret_cast<void*>(
|
|
|
|
simulator_->get_register(v8::internal::Simulator::fp));
|
|
|
|
state->lr = reinterpret_cast<void*>(simulator_->get_lr());
|
|
|
|
#elif V8_TARGET_ARCH_S390 || V8_TARGET_ARCH_S390X
|
|
|
|
state->pc = reinterpret_cast<void*>(simulator_->get_pc());
|
|
|
|
state->sp = reinterpret_cast<void*>(
|
|
|
|
simulator_->get_register(v8::internal::Simulator::sp));
|
|
|
|
state->fp = reinterpret_cast<void*>(
|
|
|
|
simulator_->get_register(v8::internal::Simulator::fp));
|
|
|
|
state->lr = reinterpret_cast<void*>(
|
|
|
|
simulator_->get_register(v8::internal::Simulator::ra));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
v8::internal::Simulator* simulator_;
|
|
|
|
};
|
|
|
|
#endif // USE_SIMULATOR
|
|
|
|
|
2020-11-18 11:19:56 +00:00
|
|
|
// The following should correspond to Chromium's kV8DOMWrapperTypeIndex and
|
|
|
|
// kV8DOMWrapperObjectIndex.
|
|
|
|
static const int kV8WrapperTypeIndex = 0;
|
|
|
|
static const int kV8WrapperObjectIndex = 1;
|
|
|
|
|
|
|
|
enum class ApiCheckerResult : uint8_t {
|
|
|
|
kNotCalled = 0,
|
|
|
|
kSlowCalled = 1 << 0,
|
|
|
|
kFastCalled = 1 << 1,
|
|
|
|
};
|
|
|
|
using ApiCheckerResultFlags = v8::base::Flags<ApiCheckerResult>;
|
|
|
|
DEFINE_OPERATORS_FOR_FLAGS(ApiCheckerResultFlags)
|
|
|
|
|
|
|
|
bool IsValidUnwrapObject(v8::Object* object);
|
|
|
|
|
2021-04-20 08:48:41 +00:00
|
|
|
template <typename T>
|
2020-11-18 11:19:56 +00:00
|
|
|
T* GetInternalField(v8::Object* wrapper) {
|
2021-04-20 08:48:41 +00:00
|
|
|
assert(kV8WrapperObjectIndex < wrapper->InternalFieldCount());
|
2020-11-18 11:19:56 +00:00
|
|
|
return reinterpret_cast<T*>(
|
2021-04-20 08:48:41 +00:00
|
|
|
wrapper->GetAlignedPointerFromInternalField(kV8WrapperObjectIndex));
|
2020-11-18 11:19:56 +00:00
|
|
|
}
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
#endif // ifndef CCTEST_H_
|