2011-04-01 14:38:30 +00:00
|
|
|
// Copyright 2006-2009 the V8 project authors. All rights reserved.
|
2013-03-07 11:12:26 +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.
|
2009-05-05 15:57:47 +00:00
|
|
|
//
|
|
|
|
// Tests of logging functions from log.h
|
|
|
|
|
2009-07-28 09:10:29 +00:00
|
|
|
#ifdef __linux__
|
2009-09-04 11:16:51 +00:00
|
|
|
#include <pthread.h>
|
2009-07-28 09:10:29 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
2013-04-19 14:20:36 +00:00
|
|
|
#include <cmath>
|
2009-09-04 11:16:51 +00:00
|
|
|
#endif // __linux__
|
2009-05-05 15:57:47 +00:00
|
|
|
|
2013-05-02 20:18:42 +00:00
|
|
|
// TODO(dcarney): remove
|
|
|
|
#define V8_ALLOW_ACCESS_TO_PERSISTENT_ARROW
|
|
|
|
#define V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR
|
|
|
|
#define V8_ALLOW_ACCESS_TO_PERSISTENT_IMPLICIT
|
|
|
|
|
2009-07-28 09:10:29 +00:00
|
|
|
#include "v8.h"
|
2009-05-05 15:57:47 +00:00
|
|
|
#include "log.h"
|
2010-04-06 10:36:38 +00:00
|
|
|
#include "cpu-profiler.h"
|
2011-07-14 11:38:18 +00:00
|
|
|
#include "natives.h"
|
2009-09-04 11:16:51 +00:00
|
|
|
#include "v8threads.h"
|
2011-07-13 11:31:22 +00:00
|
|
|
#include "v8utils.h"
|
2009-05-05 15:57:47 +00:00
|
|
|
#include "cctest.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
#include "vm-state-inl.h"
|
2009-05-05 15:57:47 +00:00
|
|
|
|
2009-05-20 09:04:13 +00:00
|
|
|
using v8::internal::Address;
|
2009-05-25 08:25:36 +00:00
|
|
|
using v8::internal::EmbeddedVector;
|
2009-05-05 15:57:47 +00:00
|
|
|
using v8::internal::Logger;
|
2009-11-11 09:50:06 +00:00
|
|
|
using v8::internal::StrLength;
|
2009-05-05 15:57:47 +00:00
|
|
|
|
2010-02-17 13:23:46 +00:00
|
|
|
namespace {
|
|
|
|
|
2011-09-14 11:47:03 +00:00
|
|
|
|
2010-02-17 13:23:46 +00:00
|
|
|
class ScopedLoggerInitializer {
|
|
|
|
public:
|
2010-03-01 10:41:34 +00:00
|
|
|
explicit ScopedLoggerInitializer(bool prof_lazy)
|
2011-07-13 11:31:22 +00:00
|
|
|
: saved_log_(i::FLAG_log),
|
|
|
|
saved_prof_lazy_(i::FLAG_prof_lazy),
|
2010-02-17 13:23:46 +00:00
|
|
|
saved_prof_(i::FLAG_prof),
|
|
|
|
saved_prof_auto_(i::FLAG_prof_auto),
|
2011-07-13 11:31:22 +00:00
|
|
|
temp_file_(NULL),
|
|
|
|
// Need to run this prior to creating the scope.
|
2010-03-01 10:41:34 +00:00
|
|
|
trick_to_run_init_flags_(init_flags_(prof_lazy)),
|
2013-03-15 12:06:53 +00:00
|
|
|
scope_(v8::Isolate::GetCurrent()),
|
2013-05-08 07:45:16 +00:00
|
|
|
env_(v8::Context::New(v8::Isolate::GetCurrent())),
|
2013-04-08 15:16:55 +00:00
|
|
|
logger_(i::Isolate::Current()->logger()) {
|
2010-02-17 13:23:46 +00:00
|
|
|
env_->Enter();
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScopedLoggerInitializer() {
|
|
|
|
env_->Exit();
|
2013-04-08 15:16:55 +00:00
|
|
|
logger_->TearDown();
|
2011-07-13 11:31:22 +00:00
|
|
|
if (temp_file_ != NULL) fclose(temp_file_);
|
2010-02-17 13:23:46 +00:00
|
|
|
i::FLAG_prof_lazy = saved_prof_lazy_;
|
|
|
|
i::FLAG_prof = saved_prof_;
|
|
|
|
i::FLAG_prof_auto = saved_prof_auto_;
|
2011-07-13 11:31:22 +00:00
|
|
|
i::FLAG_log = saved_log_;
|
2010-02-17 13:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v8::Handle<v8::Context>& env() { return env_; }
|
|
|
|
|
2013-04-08 15:16:55 +00:00
|
|
|
Logger* logger() { return logger_; }
|
|
|
|
|
2011-07-13 11:31:22 +00:00
|
|
|
FILE* StopLoggingGetTempFile() {
|
2013-04-08 15:16:55 +00:00
|
|
|
temp_file_ = logger_->TearDown();
|
2011-07-13 11:31:22 +00:00
|
|
|
CHECK_NE(NULL, temp_file_);
|
|
|
|
fflush(temp_file_);
|
|
|
|
rewind(temp_file_);
|
|
|
|
return temp_file_;
|
|
|
|
}
|
|
|
|
|
2010-02-17 13:23:46 +00:00
|
|
|
private:
|
2010-03-01 10:41:34 +00:00
|
|
|
static bool init_flags_(bool prof_lazy) {
|
2011-07-13 11:31:22 +00:00
|
|
|
i::FLAG_log = true;
|
2010-02-17 13:23:46 +00:00
|
|
|
i::FLAG_prof = true;
|
|
|
|
i::FLAG_prof_lazy = prof_lazy;
|
|
|
|
i::FLAG_prof_auto = false;
|
2011-07-13 11:31:22 +00:00
|
|
|
i::FLAG_logfile = i::Log::kLogToTemporaryFile;
|
2010-02-17 13:23:46 +00:00
|
|
|
return prof_lazy;
|
|
|
|
}
|
|
|
|
|
2011-07-13 11:31:22 +00:00
|
|
|
const bool saved_log_;
|
2010-02-17 13:23:46 +00:00
|
|
|
const bool saved_prof_lazy_;
|
|
|
|
const bool saved_prof_;
|
|
|
|
const bool saved_prof_auto_;
|
2011-07-13 11:31:22 +00:00
|
|
|
FILE* temp_file_;
|
2010-02-17 13:23:46 +00:00
|
|
|
const bool trick_to_run_init_flags_;
|
|
|
|
v8::HandleScope scope_;
|
|
|
|
v8::Handle<v8::Context> env_;
|
2013-04-08 15:16:55 +00:00
|
|
|
Logger* logger_;
|
2010-02-17 13:23:46 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ScopedLoggerInitializer);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
2011-07-13 11:31:22 +00:00
|
|
|
static const char* StrNStr(const char* s1, const char* s2, int n) {
|
|
|
|
if (s1[n] == '\0') return strstr(s1, s2);
|
|
|
|
i::ScopedVector<char> str(n + 1);
|
|
|
|
i::OS::StrNCpy(str, s1, static_cast<size_t>(n));
|
|
|
|
str[n] = '\0';
|
|
|
|
char* found = strstr(str.start(), s2);
|
|
|
|
return found != NULL ? s1 + (found - str.start()) : NULL;
|
2009-05-25 08:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(ProfLazyMode) {
|
2010-03-01 10:41:34 +00:00
|
|
|
ScopedLoggerInitializer initialize_logger(true);
|
2013-04-08 15:16:55 +00:00
|
|
|
Logger* logger = initialize_logger.logger();
|
2009-05-25 08:25:36 +00:00
|
|
|
|
2011-04-01 14:38:30 +00:00
|
|
|
if (!i::V8::UseCrankshaft()) return;
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2013-04-08 15:16:55 +00:00
|
|
|
logger->StringEvent("test-start", "");
|
2011-07-13 11:31:22 +00:00
|
|
|
CompileRun("var a = (function(x) { return x + 1; })(10);");
|
2013-04-08 15:16:55 +00:00
|
|
|
logger->StringEvent("test-profiler-start", "");
|
2011-07-13 11:31:22 +00:00
|
|
|
v8::V8::ResumeProfiler();
|
|
|
|
CompileRun(
|
|
|
|
"var b = (function(x) { return x + 2; })(10);\n"
|
|
|
|
"var c = (function(x) { return x + 3; })(10);\n"
|
|
|
|
"var d = (function(x) { return x + 4; })(10);\n"
|
|
|
|
"var e = (function(x) { return x + 5; })(10);");
|
|
|
|
v8::V8::PauseProfiler();
|
2013-04-08 15:16:55 +00:00
|
|
|
logger->StringEvent("test-profiler-stop", "");
|
2011-07-13 11:31:22 +00:00
|
|
|
CompileRun("var f = (function(x) { return x + 6; })(10);");
|
|
|
|
// Check that profiling can be resumed again.
|
2013-04-08 15:16:55 +00:00
|
|
|
logger->StringEvent("test-profiler-start-2", "");
|
2011-07-13 11:31:22 +00:00
|
|
|
v8::V8::ResumeProfiler();
|
|
|
|
CompileRun(
|
|
|
|
"var g = (function(x) { return x + 7; })(10);\n"
|
|
|
|
"var h = (function(x) { return x + 8; })(10);\n"
|
|
|
|
"var i = (function(x) { return x + 9; })(10);\n"
|
|
|
|
"var j = (function(x) { return x + 10; })(10);");
|
|
|
|
v8::V8::PauseProfiler();
|
2013-04-08 15:16:55 +00:00
|
|
|
logger->StringEvent("test-profiler-stop-2", "");
|
|
|
|
logger->StringEvent("test-stop", "");
|
2011-07-13 11:31:22 +00:00
|
|
|
|
|
|
|
bool exists = false;
|
|
|
|
i::Vector<const char> log(
|
|
|
|
i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
|
|
|
|
CHECK(exists);
|
|
|
|
|
|
|
|
const char* test_start_position =
|
|
|
|
StrNStr(log.start(), "test-start,", log.length());
|
|
|
|
CHECK_NE(NULL, test_start_position);
|
|
|
|
const char* test_profiler_start_position =
|
|
|
|
StrNStr(log.start(), "test-profiler-start,", log.length());
|
|
|
|
CHECK_NE(NULL, test_profiler_start_position);
|
|
|
|
CHECK_GT(test_profiler_start_position, test_start_position);
|
|
|
|
const char* test_profiler_stop_position =
|
|
|
|
StrNStr(log.start(), "test-profiler-stop,", log.length());
|
|
|
|
CHECK_NE(NULL, test_profiler_stop_position);
|
|
|
|
CHECK_GT(test_profiler_stop_position, test_profiler_start_position);
|
|
|
|
const char* test_profiler_start_2_position =
|
|
|
|
StrNStr(log.start(), "test-profiler-start-2,", log.length());
|
|
|
|
CHECK_NE(NULL, test_profiler_start_2_position);
|
|
|
|
CHECK_GT(test_profiler_start_2_position, test_profiler_stop_position);
|
2009-05-25 08:25:36 +00:00
|
|
|
|
2009-10-28 09:13:11 +00:00
|
|
|
// Nothing must be logged until profiling is resumed.
|
2011-07-13 11:31:22 +00:00
|
|
|
CHECK_EQ(NULL, StrNStr(test_start_position,
|
|
|
|
"code-creation,",
|
|
|
|
static_cast<int>(test_profiler_start_position -
|
|
|
|
test_start_position)));
|
2009-05-25 08:25:36 +00:00
|
|
|
// Nothing must be logged while profiling is suspended.
|
2011-07-13 11:31:22 +00:00
|
|
|
CHECK_EQ(NULL, StrNStr(test_profiler_stop_position,
|
|
|
|
"code-creation,",
|
|
|
|
static_cast<int>(test_profiler_start_2_position -
|
|
|
|
test_profiler_stop_position)));
|
2009-05-25 08:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-26 14:51:31 +00:00
|
|
|
// BUG(913). Need to implement support for profiling multiple VM threads.
|
|
|
|
#if 0
|
2009-09-04 11:16:51 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class LoopingThread : public v8::internal::Thread {
|
|
|
|
public:
|
2011-03-18 20:35:07 +00:00
|
|
|
explicit LoopingThread(v8::internal::Isolate* isolate)
|
|
|
|
: v8::internal::Thread(isolate),
|
2009-09-04 11:16:51 +00:00
|
|
|
semaphore_(v8::internal::OS::CreateSemaphore(0)),
|
|
|
|
run_(true) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~LoopingThread() { delete semaphore_; }
|
|
|
|
|
|
|
|
void Run() {
|
|
|
|
self_ = pthread_self();
|
|
|
|
RunLoop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SendSigProf() { pthread_kill(self_, SIGPROF); }
|
|
|
|
|
|
|
|
void Stop() { run_ = false; }
|
|
|
|
|
|
|
|
bool WaitForRunning() { return semaphore_->Wait(1000000); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool IsRunning() { return run_; }
|
|
|
|
|
|
|
|
virtual void RunLoop() = 0;
|
|
|
|
|
|
|
|
void SetV8ThreadId() {
|
|
|
|
v8_thread_id_ = v8::V8::GetCurrentThreadId();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SignalRunning() { semaphore_->Signal(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
v8::internal::Semaphore* semaphore_;
|
|
|
|
bool run_;
|
|
|
|
pthread_t self_;
|
|
|
|
int v8_thread_id_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class LoopingJsThread : public LoopingThread {
|
|
|
|
public:
|
2011-03-18 20:35:07 +00:00
|
|
|
explicit LoopingJsThread(v8::internal::Isolate* isolate)
|
|
|
|
: LoopingThread(isolate) { }
|
2009-09-04 11:16:51 +00:00
|
|
|
void RunLoop() {
|
2010-03-04 13:25:23 +00:00
|
|
|
v8::Locker locker;
|
2011-03-18 20:35:07 +00:00
|
|
|
CHECK(i::Isolate::Current() != NULL);
|
|
|
|
CHECK_GT(i::Isolate::Current()->thread_manager()->CurrentId(), 0);
|
2010-03-04 13:25:23 +00:00
|
|
|
SetV8ThreadId();
|
2009-09-04 11:16:51 +00:00
|
|
|
while (IsRunning()) {
|
|
|
|
v8::HandleScope scope;
|
|
|
|
v8::Persistent<v8::Context> context = v8::Context::New();
|
2010-03-04 13:25:23 +00:00
|
|
|
CHECK(!context.IsEmpty());
|
|
|
|
{
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
SignalRunning();
|
2011-07-13 11:31:22 +00:00
|
|
|
CompileRun(
|
2010-03-04 13:25:23 +00:00
|
|
|
"var j; for (var i=0; i<10000; ++i) { j = Math.sin(i); }");
|
|
|
|
}
|
2009-09-04 11:16:51 +00:00
|
|
|
context.Dispose();
|
2010-03-04 15:03:36 +00:00
|
|
|
i::OS::Sleep(1);
|
2009-09-04 11:16:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class LoopingNonJsThread : public LoopingThread {
|
|
|
|
public:
|
2011-03-18 20:35:07 +00:00
|
|
|
explicit LoopingNonJsThread(v8::internal::Isolate* isolate)
|
|
|
|
: LoopingThread(isolate) { }
|
2009-09-04 11:16:51 +00:00
|
|
|
void RunLoop() {
|
|
|
|
v8::Locker locker;
|
|
|
|
v8::Unlocker unlocker;
|
|
|
|
// Now thread has V8's id, but will not run VM code.
|
2011-03-18 20:35:07 +00:00
|
|
|
CHECK(i::Isolate::Current() != NULL);
|
|
|
|
CHECK_GT(i::Isolate::Current()->thread_manager()->CurrentId(), 0);
|
2009-09-04 11:16:51 +00:00
|
|
|
double i = 10;
|
|
|
|
SignalRunning();
|
|
|
|
while (IsRunning()) {
|
|
|
|
i = sin(i);
|
|
|
|
i::OS::Sleep(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class TestSampler : public v8::internal::Sampler {
|
|
|
|
public:
|
2011-03-18 20:35:07 +00:00
|
|
|
explicit TestSampler(v8::internal::Isolate* isolate)
|
|
|
|
: Sampler(isolate, 0, true, true),
|
2009-09-04 11:16:51 +00:00
|
|
|
semaphore_(v8::internal::OS::CreateSemaphore(0)),
|
|
|
|
was_sample_stack_called_(false) {
|
|
|
|
}
|
|
|
|
|
|
|
|
~TestSampler() { delete semaphore_; }
|
|
|
|
|
|
|
|
void SampleStack(v8::internal::TickSample*) {
|
|
|
|
was_sample_stack_called_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tick(v8::internal::TickSample*) { semaphore_->Signal(); }
|
|
|
|
|
|
|
|
bool WaitForTick() { return semaphore_->Wait(1000000); }
|
|
|
|
|
|
|
|
void Reset() { was_sample_stack_called_ = false; }
|
|
|
|
|
|
|
|
bool WasSampleStackCalled() { return was_sample_stack_called_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
v8::internal::Semaphore* semaphore_;
|
|
|
|
bool was_sample_stack_called_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(ProfMultipleThreads) {
|
2010-12-07 11:31:57 +00:00
|
|
|
TestSampler* sampler = NULL;
|
|
|
|
{
|
|
|
|
v8::Locker locker;
|
2011-03-18 20:35:07 +00:00
|
|
|
sampler = new TestSampler(v8::internal::Isolate::Current());
|
2010-12-07 11:31:57 +00:00
|
|
|
sampler->Start();
|
|
|
|
CHECK(sampler->IsActive());
|
|
|
|
}
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
LoopingJsThread jsThread(v8::internal::Isolate::Current());
|
2009-09-04 11:16:51 +00:00
|
|
|
jsThread.Start();
|
2011-03-18 20:35:07 +00:00
|
|
|
LoopingNonJsThread nonJsThread(v8::internal::Isolate::Current());
|
2009-09-04 11:16:51 +00:00
|
|
|
nonJsThread.Start();
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
CHECK(!sampler->WasSampleStackCalled());
|
2009-09-04 11:16:51 +00:00
|
|
|
jsThread.WaitForRunning();
|
|
|
|
jsThread.SendSigProf();
|
2010-12-07 11:31:57 +00:00
|
|
|
CHECK(sampler->WaitForTick());
|
|
|
|
CHECK(sampler->WasSampleStackCalled());
|
|
|
|
sampler->Reset();
|
|
|
|
CHECK(!sampler->WasSampleStackCalled());
|
2009-09-04 11:16:51 +00:00
|
|
|
nonJsThread.WaitForRunning();
|
|
|
|
nonJsThread.SendSigProf();
|
2010-12-07 11:31:57 +00:00
|
|
|
CHECK(!sampler->WaitForTick());
|
|
|
|
CHECK(!sampler->WasSampleStackCalled());
|
|
|
|
sampler->Stop();
|
2009-09-04 11:16:51 +00:00
|
|
|
|
|
|
|
jsThread.Stop();
|
|
|
|
nonJsThread.Stop();
|
|
|
|
jsThread.Join();
|
|
|
|
nonJsThread.Join();
|
2010-12-07 11:31:57 +00:00
|
|
|
|
|
|
|
delete sampler;
|
2009-09-04 11:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // __linux__
|
|
|
|
|
|
|
|
|
2009-10-07 12:20:02 +00:00
|
|
|
// Test for issue http://crbug.com/23768 in Chromium.
|
|
|
|
// Heap can contain scripts with already disposed external sources.
|
|
|
|
// We need to verify that LogCompiledFunctions doesn't crash on them.
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class SimpleExternalString : public v8::String::ExternalStringResource {
|
|
|
|
public:
|
|
|
|
explicit SimpleExternalString(const char* source)
|
2009-11-11 09:50:06 +00:00
|
|
|
: utf_source_(StrLength(source)) {
|
2009-10-07 12:20:02 +00:00
|
|
|
for (int i = 0; i < utf_source_.length(); ++i)
|
|
|
|
utf_source_[i] = source[i];
|
|
|
|
}
|
|
|
|
virtual ~SimpleExternalString() {}
|
|
|
|
virtual size_t length() const { return utf_source_.length(); }
|
|
|
|
virtual const uint16_t* data() const { return utf_source_.start(); }
|
|
|
|
private:
|
|
|
|
i::ScopedVector<uint16_t> utf_source_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(Issue23768) {
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(v8::Isolate::GetCurrent());
|
2013-05-02 20:18:42 +00:00
|
|
|
v8::Handle<v8::Context> env = v8::Context::New(v8::Isolate::GetCurrent());
|
2009-10-07 12:20:02 +00:00
|
|
|
env->Enter();
|
|
|
|
|
|
|
|
SimpleExternalString source_ext_str("(function ext() {})();");
|
|
|
|
v8::Local<v8::String> source = v8::String::NewExternal(&source_ext_str);
|
|
|
|
// Script needs to have a name in order to trigger InitLineEnds execution.
|
|
|
|
v8::Handle<v8::String> origin = v8::String::New("issue-23768-test");
|
|
|
|
v8::Handle<v8::Script> evil_script = v8::Script::Compile(source, origin);
|
|
|
|
CHECK(!evil_script.IsEmpty());
|
|
|
|
CHECK(!evil_script->Run().IsEmpty());
|
|
|
|
i::Handle<i::ExternalTwoByteString> i_source(
|
|
|
|
i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source)));
|
|
|
|
// This situation can happen if source was an external string disposed
|
|
|
|
// by its owner.
|
|
|
|
i_source->set_resource(NULL);
|
|
|
|
|
|
|
|
// Must not crash.
|
2013-04-08 15:16:55 +00:00
|
|
|
i::Isolate::Current()->logger()->LogCompiledFunctions();
|
2009-10-07 12:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-20 12:15:46 +00:00
|
|
|
static v8::Handle<v8::Value> ObjMethod1(const v8::Arguments& args) {
|
|
|
|
return v8::Handle<v8::Value>();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LogCallbacks) {
|
2010-03-01 10:41:34 +00:00
|
|
|
ScopedLoggerInitializer initialize_logger(false);
|
2013-04-08 15:16:55 +00:00
|
|
|
Logger* logger = initialize_logger.logger();
|
2009-11-20 12:15:46 +00:00
|
|
|
|
|
|
|
v8::Persistent<v8::FunctionTemplate> obj =
|
2013-01-25 08:31:46 +00:00
|
|
|
v8::Persistent<v8::FunctionTemplate>::New(v8::Isolate::GetCurrent(),
|
|
|
|
v8::FunctionTemplate::New());
|
2011-07-13 11:31:22 +00:00
|
|
|
obj->SetClassName(v8_str("Obj"));
|
2009-11-20 12:15:46 +00:00
|
|
|
v8::Handle<v8::ObjectTemplate> proto = obj->PrototypeTemplate();
|
2013-05-02 20:18:42 +00:00
|
|
|
v8::Local<v8::Signature> signature =
|
|
|
|
v8::Signature::New(v8::Handle<v8::FunctionTemplate>(*obj));
|
2011-07-13 11:31:22 +00:00
|
|
|
proto->Set(v8_str("method1"),
|
2009-11-20 12:15:46 +00:00
|
|
|
v8::FunctionTemplate::New(ObjMethod1,
|
|
|
|
v8::Handle<v8::Value>(),
|
|
|
|
signature),
|
|
|
|
static_cast<v8::PropertyAttribute>(v8::DontDelete));
|
|
|
|
|
2010-02-17 13:23:46 +00:00
|
|
|
initialize_logger.env()->Global()->Set(v8_str("Obj"), obj->GetFunction());
|
2011-07-13 11:31:22 +00:00
|
|
|
CompileRun("Obj.prototype.method1.toString();");
|
2009-11-23 09:19:24 +00:00
|
|
|
|
2013-04-08 15:16:55 +00:00
|
|
|
logger->LogCompiledFunctions();
|
2009-11-20 12:15:46 +00:00
|
|
|
|
2011-07-13 11:31:22 +00:00
|
|
|
bool exists = false;
|
|
|
|
i::Vector<const char> log(
|
|
|
|
i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
|
|
|
|
CHECK(exists);
|
|
|
|
|
|
|
|
i::EmbeddedVector<char, 100> ref_data;
|
2009-11-20 12:15:46 +00:00
|
|
|
i::OS::SNPrintF(ref_data,
|
2012-11-28 11:01:10 +00:00
|
|
|
"code-creation,Callback,-3,0x%" V8PRIxPTR ",1,\"method1\"\0",
|
2011-07-13 11:31:22 +00:00
|
|
|
ObjMethod1);
|
|
|
|
|
|
|
|
CHECK_NE(NULL, StrNStr(log.start(), ref_data.start(), log.length()));
|
2009-11-20 12:15:46 +00:00
|
|
|
|
2013-01-25 08:31:46 +00:00
|
|
|
obj.Dispose(v8::Isolate::GetCurrent());
|
2009-11-20 12:15:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-25 16:39:18 +00:00
|
|
|
static v8::Handle<v8::Value> Prop1Getter(v8::Local<v8::String> property,
|
|
|
|
const v8::AccessorInfo& info) {
|
|
|
|
return v8::Handle<v8::Value>();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Prop1Setter(v8::Local<v8::String> property,
|
|
|
|
v8::Local<v8::Value> value,
|
|
|
|
const v8::AccessorInfo& info) {
|
|
|
|
}
|
|
|
|
|
|
|
|
static v8::Handle<v8::Value> Prop2Getter(v8::Local<v8::String> property,
|
|
|
|
const v8::AccessorInfo& info) {
|
|
|
|
return v8::Handle<v8::Value>();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LogAccessorCallbacks) {
|
2010-03-01 10:41:34 +00:00
|
|
|
ScopedLoggerInitializer initialize_logger(false);
|
2013-04-08 15:16:55 +00:00
|
|
|
Logger* logger = initialize_logger.logger();
|
2009-11-25 16:39:18 +00:00
|
|
|
|
|
|
|
v8::Persistent<v8::FunctionTemplate> obj =
|
2013-01-25 08:31:46 +00:00
|
|
|
v8::Persistent<v8::FunctionTemplate>::New(v8::Isolate::GetCurrent(),
|
|
|
|
v8::FunctionTemplate::New());
|
2011-07-13 11:31:22 +00:00
|
|
|
obj->SetClassName(v8_str("Obj"));
|
2009-11-25 16:39:18 +00:00
|
|
|
v8::Handle<v8::ObjectTemplate> inst = obj->InstanceTemplate();
|
2011-07-13 11:31:22 +00:00
|
|
|
inst->SetAccessor(v8_str("prop1"), Prop1Getter, Prop1Setter);
|
|
|
|
inst->SetAccessor(v8_str("prop2"), Prop2Getter);
|
2009-11-25 16:39:18 +00:00
|
|
|
|
2013-04-08 15:16:55 +00:00
|
|
|
logger->LogAccessorCallbacks();
|
2011-07-13 11:31:22 +00:00
|
|
|
|
|
|
|
bool exists = false;
|
|
|
|
i::Vector<const char> log(
|
|
|
|
i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
|
|
|
|
CHECK(exists);
|
2009-11-25 16:39:18 +00:00
|
|
|
|
|
|
|
EmbeddedVector<char, 100> prop1_getter_record;
|
|
|
|
i::OS::SNPrintF(prop1_getter_record,
|
2012-11-28 11:01:10 +00:00
|
|
|
"code-creation,Callback,-3,0x%" V8PRIxPTR ",1,\"get prop1\"",
|
2009-11-25 16:39:18 +00:00
|
|
|
Prop1Getter);
|
2011-07-13 11:31:22 +00:00
|
|
|
CHECK_NE(NULL,
|
|
|
|
StrNStr(log.start(), prop1_getter_record.start(), log.length()));
|
|
|
|
|
2009-11-25 16:39:18 +00:00
|
|
|
EmbeddedVector<char, 100> prop1_setter_record;
|
|
|
|
i::OS::SNPrintF(prop1_setter_record,
|
2012-11-28 11:01:10 +00:00
|
|
|
"code-creation,Callback,-3,0x%" V8PRIxPTR ",1,\"set prop1\"",
|
2009-11-25 16:39:18 +00:00
|
|
|
Prop1Setter);
|
2011-07-13 11:31:22 +00:00
|
|
|
CHECK_NE(NULL,
|
|
|
|
StrNStr(log.start(), prop1_setter_record.start(), log.length()));
|
|
|
|
|
2009-11-25 16:39:18 +00:00
|
|
|
EmbeddedVector<char, 100> prop2_getter_record;
|
|
|
|
i::OS::SNPrintF(prop2_getter_record,
|
2012-11-28 11:01:10 +00:00
|
|
|
"code-creation,Callback,-3,0x%" V8PRIxPTR ",1,\"get prop2\"",
|
2009-11-25 16:39:18 +00:00
|
|
|
Prop2Getter);
|
2011-07-13 11:31:22 +00:00
|
|
|
CHECK_NE(NULL,
|
|
|
|
StrNStr(log.start(), prop2_getter_record.start(), log.length()));
|
2009-11-25 16:39:18 +00:00
|
|
|
|
2013-01-25 08:31:46 +00:00
|
|
|
obj.Dispose(v8::Isolate::GetCurrent());
|
2010-02-17 13:23:46 +00:00
|
|
|
}
|
2009-11-25 16:39:18 +00:00
|
|
|
|
2010-02-17 13:23:46 +00:00
|
|
|
|
2010-03-01 10:41:34 +00:00
|
|
|
TEST(IsLoggingPreserved) {
|
|
|
|
ScopedLoggerInitializer initialize_logger(false);
|
2013-04-08 15:16:55 +00:00
|
|
|
Logger* logger = initialize_logger.logger();
|
2010-03-01 10:41:34 +00:00
|
|
|
|
2013-04-08 15:16:55 +00:00
|
|
|
CHECK(logger->is_logging());
|
|
|
|
logger->ResumeProfiler();
|
|
|
|
CHECK(logger->is_logging());
|
|
|
|
logger->PauseProfiler();
|
|
|
|
CHECK(logger->is_logging());
|
2010-03-01 10:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-14 11:38:18 +00:00
|
|
|
typedef i::NativesCollection<i::TEST> TestSources;
|
|
|
|
|
2011-09-14 11:47:03 +00:00
|
|
|
|
|
|
|
// Test that logging of code create / move events is equivalent to traversal of
|
|
|
|
// a resulting heap.
|
2009-05-20 09:04:13 +00:00
|
|
|
TEST(EquivalenceOfLoggingAndTraversal) {
|
2009-05-25 08:25:36 +00:00
|
|
|
// This test needs to be run on a "clean" V8 to ensure that snapshot log
|
|
|
|
// is loaded. This is always true when running using tools/test.py because
|
|
|
|
// it launches a new cctest instance for every test. To be sure that launching
|
|
|
|
// cctest manually also works, please be sure that no tests below
|
|
|
|
// are using V8.
|
|
|
|
//
|
|
|
|
// P.S. No, V8 can't be re-initialized after disposal, see include/v8.h.
|
2009-05-25 19:39:52 +00:00
|
|
|
CHECK(!i::V8::IsRunning());
|
2009-05-25 08:25:36 +00:00
|
|
|
|
2011-07-13 11:31:22 +00:00
|
|
|
// Start with profiling to capture all code events from the beginning.
|
|
|
|
ScopedLoggerInitializer initialize_logger(false);
|
2013-04-08 15:16:55 +00:00
|
|
|
Logger* logger = initialize_logger.logger();
|
2009-05-20 09:04:13 +00:00
|
|
|
|
|
|
|
// Compile and run a function that creates other functions.
|
2011-07-13 11:31:22 +00:00
|
|
|
CompileRun(
|
2009-05-20 12:47:30 +00:00
|
|
|
"(function f(obj) {\n"
|
|
|
|
" obj.test =\n"
|
|
|
|
" (function a(j) { return function b() { return j; } })(100);\n"
|
2009-05-25 08:25:36 +00:00
|
|
|
"})(this);");
|
2011-07-13 11:31:22 +00:00
|
|
|
v8::V8::PauseProfiler();
|
2011-09-19 18:36:47 +00:00
|
|
|
HEAP->CollectAllGarbage(i::Heap::kMakeHeapIterableMask);
|
2013-04-08 15:16:55 +00:00
|
|
|
logger->StringEvent("test-logging-done", "");
|
2009-05-20 09:04:13 +00:00
|
|
|
|
|
|
|
// Iterate heap to find compiled functions, will write to log.
|
2013-04-08 15:16:55 +00:00
|
|
|
logger->LogCompiledFunctions();
|
|
|
|
logger->StringEvent("test-traversal-done", "");
|
2011-07-13 11:31:22 +00:00
|
|
|
|
|
|
|
bool exists = false;
|
|
|
|
i::Vector<const char> log(
|
|
|
|
i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
|
|
|
|
CHECK(exists);
|
|
|
|
v8::Handle<v8::String> log_str = v8::String::New(log.start(), log.length());
|
|
|
|
initialize_logger.env()->Global()->Set(v8_str("_log"), log_str);
|
|
|
|
|
2011-07-14 11:38:18 +00:00
|
|
|
i::Vector<const unsigned char> source = TestSources::GetScriptsSource();
|
|
|
|
v8::Handle<v8::String> source_str = v8::String::New(
|
|
|
|
reinterpret_cast<const char*>(source.start()), source.length());
|
|
|
|
v8::TryCatch try_catch;
|
|
|
|
v8::Handle<v8::Script> script = v8::Script::Compile(source_str, v8_str(""));
|
|
|
|
if (script.IsEmpty()) {
|
|
|
|
v8::String::Utf8Value exception(try_catch.Exception());
|
|
|
|
printf("compile: %s\n", *exception);
|
|
|
|
CHECK(false);
|
|
|
|
}
|
|
|
|
v8::Handle<v8::Value> result = script->Run();
|
|
|
|
if (result.IsEmpty()) {
|
|
|
|
v8::String::Utf8Value exception(try_catch.Exception());
|
|
|
|
printf("run: %s\n", *exception);
|
|
|
|
CHECK(false);
|
2009-05-20 09:04:13 +00:00
|
|
|
}
|
2011-07-13 11:31:22 +00:00
|
|
|
// The result either be a "true" literal or problem description.
|
2011-07-14 11:38:18 +00:00
|
|
|
if (!result->IsTrue()) {
|
|
|
|
v8::Local<v8::String> s = result->ToString();
|
2013-05-03 06:47:24 +00:00
|
|
|
i::ScopedVector<char> data(s->Utf8Length() + 1);
|
2011-07-13 11:31:22 +00:00
|
|
|
CHECK_NE(NULL, data.start());
|
2013-05-03 06:47:24 +00:00
|
|
|
s->WriteUtf8(data.start());
|
2011-07-13 11:31:22 +00:00
|
|
|
printf("%s\n", data.start());
|
|
|
|
// Make sure that our output is written prior crash due to CHECK failure.
|
|
|
|
fflush(stdout);
|
|
|
|
CHECK(false);
|
|
|
|
}
|
2009-05-20 09:04:13 +00:00
|
|
|
}
|