blob: adbd1a5a373afc79d2de313b32e82543abb17487 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Steve Blocka7e24c12009-10-30 11:49:00 +000027//
28// Tests of logging functions from log.h
29
Steve Blocka7e24c12009-10-30 11:49:00 +000030#ifdef __linux__
Steve Blocka7e24c12009-10-30 11:49:00 +000031#include <pthread.h>
32#include <signal.h>
33#include <unistd.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000034#include <cmath>
Steve Blocka7e24c12009-10-30 11:49:00 +000035#endif // __linux__
36
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037#include "src/v8.h"
38
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039#include "src/log.h"
40#include "src/log-utils.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041#include "src/profiler/cpu-profiler.h"
42#include "src/snapshot/natives.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043#include "src/utils.h"
44#include "src/v8threads.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040045#include "src/version.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000046#include "src/vm-state-inl.h"
47#include "test/cctest/cctest.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000048
49using v8::internal::Address;
50using v8::internal::EmbeddedVector;
51using v8::internal::Logger;
Steve Blockd0582a62009-12-15 09:54:21 +000052using v8::internal::StrLength;
Steve Blocka7e24c12009-10-30 11:49:00 +000053
Andrei Popescu402d9372010-02-26 13:31:12 +000054namespace {
55
Ben Murdoch589d6972011-11-30 16:04:58 +000056
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000057#define SETUP_FLAGS() \
58 bool saved_log = i::FLAG_log; \
59 bool saved_prof = i::FLAG_prof; \
60 i::FLAG_log = true; \
61 i::FLAG_prof = true; \
62 i::FLAG_logfile = i::Log::kLogToTemporaryFile; \
63 i::FLAG_logfile_per_isolate = false
64
65
Andrei Popescu402d9372010-02-26 13:31:12 +000066class ScopedLoggerInitializer {
67 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000068 ScopedLoggerInitializer(bool saved_log, bool saved_prof, v8::Isolate* isolate)
69 : saved_log_(saved_log),
70 saved_prof_(saved_prof),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000071 temp_file_(NULL),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000072 isolate_(isolate),
73 isolate_scope_(isolate),
74 scope_(isolate),
75 env_(v8::Context::New(isolate)),
76 logger_(reinterpret_cast<i::Isolate*>(isolate)->logger()) {
Andrei Popescu402d9372010-02-26 13:31:12 +000077 env_->Enter();
78 }
79
80 ~ScopedLoggerInitializer() {
81 env_->Exit();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082 logger_->TearDown();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000083 if (temp_file_ != NULL) fclose(temp_file_);
Andrei Popescu402d9372010-02-26 13:31:12 +000084 i::FLAG_prof = saved_prof_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000085 i::FLAG_log = saved_log_;
Andrei Popescu402d9372010-02-26 13:31:12 +000086 }
87
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000088 v8::Local<v8::Context>& env() { return env_; }
Andrei Popescu402d9372010-02-26 13:31:12 +000089
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090 v8::Isolate* isolate() { return isolate_; }
91
92 Logger* logger() { return logger_; }
93
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000094 FILE* StopLoggingGetTempFile() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000095 temp_file_ = logger_->TearDown();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000096 CHECK(temp_file_);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000097 fflush(temp_file_);
98 rewind(temp_file_);
99 return temp_file_;
100 }
101
Andrei Popescu402d9372010-02-26 13:31:12 +0000102 private:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000103 const bool saved_log_;
Andrei Popescu402d9372010-02-26 13:31:12 +0000104 const bool saved_prof_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000105 FILE* temp_file_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000106 v8::Isolate* isolate_;
107 v8::Isolate::Scope isolate_scope_;
Andrei Popescu402d9372010-02-26 13:31:12 +0000108 v8::HandleScope scope_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000109 v8::Local<v8::Context> env_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000110 Logger* logger_;
Andrei Popescu402d9372010-02-26 13:31:12 +0000111
112 DISALLOW_COPY_AND_ASSIGN(ScopedLoggerInitializer);
113};
114
Andrei Popescu402d9372010-02-26 13:31:12 +0000115} // namespace
116
117
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000118static const char* StrNStr(const char* s1, const char* s2, int n) {
119 if (s1[n] == '\0') return strstr(s1, s2);
120 i::ScopedVector<char> str(n + 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121 i::StrNCpy(str, s1, static_cast<size_t>(n));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000122 str[n] = '\0';
123 char* found = strstr(str.start(), s2);
124 return found != NULL ? s1 + (found - str.start()) : NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000125}
126
127
John Reck59135872010-11-02 12:39:01 -0700128// BUG(913). Need to implement support for profiling multiple VM threads.
129#if 0
Steve Blocka7e24c12009-10-30 11:49:00 +0000130
131namespace {
132
133class LoopingThread : public v8::internal::Thread {
134 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100135 explicit LoopingThread(v8::internal::Isolate* isolate)
136 : v8::internal::Thread(isolate),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137 semaphore_(new v8::internal::Semaphore(0)),
Steve Blocka7e24c12009-10-30 11:49:00 +0000138 run_(true) {
139 }
140
141 virtual ~LoopingThread() { delete semaphore_; }
142
143 void Run() {
144 self_ = pthread_self();
145 RunLoop();
146 }
147
148 void SendSigProf() { pthread_kill(self_, SIGPROF); }
149
150 void Stop() { run_ = false; }
151
152 bool WaitForRunning() { return semaphore_->Wait(1000000); }
153
154 protected:
155 bool IsRunning() { return run_; }
156
157 virtual void RunLoop() = 0;
158
159 void SetV8ThreadId() {
160 v8_thread_id_ = v8::V8::GetCurrentThreadId();
161 }
162
163 void SignalRunning() { semaphore_->Signal(); }
164
165 private:
166 v8::internal::Semaphore* semaphore_;
167 bool run_;
168 pthread_t self_;
169 int v8_thread_id_;
170};
171
172
173class LoopingJsThread : public LoopingThread {
174 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100175 explicit LoopingJsThread(v8::internal::Isolate* isolate)
176 : LoopingThread(isolate) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000177 void RunLoop() {
Steve Block6ded16b2010-05-10 14:33:55 +0100178 v8::Locker locker;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000179 CHECK(CcTest::i_isolate() != NULL);
180 CHECK_GT(CcTest::i_isolate()->thread_manager()->CurrentId(), 0);
Steve Block6ded16b2010-05-10 14:33:55 +0100181 SetV8ThreadId();
Steve Blocka7e24c12009-10-30 11:49:00 +0000182 while (IsRunning()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000183 v8::HandleScope scope;
184 v8::Persistent<v8::Context> context = v8::Context::New();
Steve Block6ded16b2010-05-10 14:33:55 +0100185 CHECK(!context.IsEmpty());
186 {
187 v8::Context::Scope context_scope(context);
188 SignalRunning();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000189 CompileRun(
Steve Block6ded16b2010-05-10 14:33:55 +0100190 "var j; for (var i=0; i<10000; ++i) { j = Math.sin(i); }");
191 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000192 context.Dispose();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000193 i::OS::Sleep(v8::base::TimeDelta::FromMilliseconds(1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000194 }
195 }
196};
197
198
199class LoopingNonJsThread : public LoopingThread {
200 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100201 explicit LoopingNonJsThread(v8::internal::Isolate* isolate)
202 : LoopingThread(isolate) { }
Steve Blocka7e24c12009-10-30 11:49:00 +0000203 void RunLoop() {
204 v8::Locker locker;
205 v8::Unlocker unlocker;
206 // Now thread has V8's id, but will not run VM code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000207 CHECK(CcTest::i_isolate() != NULL);
208 CHECK_GT(CcTest::i_isolate()->thread_manager()->CurrentId(), 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000209 double i = 10;
210 SignalRunning();
211 while (IsRunning()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000212 i = std::sin(i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000213 i::OS::Sleep(v8::base::TimeDelta::FromMilliseconds(1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000214 }
215 }
216};
217
218
219class TestSampler : public v8::internal::Sampler {
220 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100221 explicit TestSampler(v8::internal::Isolate* isolate)
222 : Sampler(isolate, 0, true, true),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000223 semaphore_(new v8::internal::Semaphore(0)),
Steve Blocka7e24c12009-10-30 11:49:00 +0000224 was_sample_stack_called_(false) {
225 }
226
227 ~TestSampler() { delete semaphore_; }
228
229 void SampleStack(v8::internal::TickSample*) {
230 was_sample_stack_called_ = true;
231 }
232
233 void Tick(v8::internal::TickSample*) { semaphore_->Signal(); }
234
235 bool WaitForTick() { return semaphore_->Wait(1000000); }
236
237 void Reset() { was_sample_stack_called_ = false; }
238
239 bool WasSampleStackCalled() { return was_sample_stack_called_; }
240
241 private:
242 v8::internal::Semaphore* semaphore_;
243 bool was_sample_stack_called_;
244};
245
246
247} // namespace
248
249TEST(ProfMultipleThreads) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100250 TestSampler* sampler = NULL;
251 {
252 v8::Locker locker;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000253 sampler = new TestSampler(CcTest::i_isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100254 sampler->Start();
255 CHECK(sampler->IsActive());
256 }
257
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000258 LoopingJsThread jsThread(CcTest::i_isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000259 jsThread.Start();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000260 LoopingNonJsThread nonJsThread(CcTest::i_isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000261 nonJsThread.Start();
262
Ben Murdochb0fe1622011-05-05 13:52:32 +0100263 CHECK(!sampler->WasSampleStackCalled());
Steve Blocka7e24c12009-10-30 11:49:00 +0000264 jsThread.WaitForRunning();
265 jsThread.SendSigProf();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100266 CHECK(sampler->WaitForTick());
267 CHECK(sampler->WasSampleStackCalled());
268 sampler->Reset();
269 CHECK(!sampler->WasSampleStackCalled());
Steve Blocka7e24c12009-10-30 11:49:00 +0000270 nonJsThread.WaitForRunning();
271 nonJsThread.SendSigProf();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100272 CHECK(!sampler->WaitForTick());
273 CHECK(!sampler->WasSampleStackCalled());
274 sampler->Stop();
Steve Blocka7e24c12009-10-30 11:49:00 +0000275
276 jsThread.Stop();
277 nonJsThread.Stop();
278 jsThread.Join();
279 nonJsThread.Join();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100280
281 delete sampler;
Steve Blocka7e24c12009-10-30 11:49:00 +0000282}
283
284#endif // __linux__
285
286
Steve Block3ce2e202009-11-05 08:53:23 +0000287// Test for issue http://crbug.com/23768 in Chromium.
288// Heap can contain scripts with already disposed external sources.
289// We need to verify that LogCompiledFunctions doesn't crash on them.
290namespace {
291
292class SimpleExternalString : public v8::String::ExternalStringResource {
293 public:
294 explicit SimpleExternalString(const char* source)
Steve Blockd0582a62009-12-15 09:54:21 +0000295 : utf_source_(StrLength(source)) {
Steve Block3ce2e202009-11-05 08:53:23 +0000296 for (int i = 0; i < utf_source_.length(); ++i)
297 utf_source_[i] = source[i];
298 }
299 virtual ~SimpleExternalString() {}
300 virtual size_t length() const { return utf_source_.length(); }
301 virtual const uint16_t* data() const { return utf_source_.start(); }
302 private:
303 i::ScopedVector<uint16_t> utf_source_;
304};
305
306} // namespace
307
308TEST(Issue23768) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000309 v8::HandleScope scope(CcTest::isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000310 v8::Local<v8::Context> env = v8::Context::New(CcTest::isolate());
Steve Block3ce2e202009-11-05 08:53:23 +0000311 env->Enter();
312
313 SimpleExternalString source_ext_str("(function ext() {})();");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000314 v8::Local<v8::String> source =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000315 v8::String::NewExternalTwoByte(CcTest::isolate(), &source_ext_str)
316 .ToLocalChecked();
Steve Block3ce2e202009-11-05 08:53:23 +0000317 // Script needs to have a name in order to trigger InitLineEnds execution.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000318 v8::Local<v8::String> origin =
319 v8::String::NewFromUtf8(CcTest::isolate(), "issue-23768-test",
320 v8::NewStringType::kNormal)
321 .ToLocalChecked();
322 v8::Local<v8::Script> evil_script = CompileWithOrigin(source, origin);
Steve Block3ce2e202009-11-05 08:53:23 +0000323 CHECK(!evil_script.IsEmpty());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000324 CHECK(!evil_script->Run(env).IsEmpty());
Steve Block3ce2e202009-11-05 08:53:23 +0000325 i::Handle<i::ExternalTwoByteString> i_source(
326 i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source)));
327 // This situation can happen if source was an external string disposed
328 // by its owner.
329 i_source->set_resource(NULL);
330
331 // Must not crash.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000332 CcTest::i_isolate()->logger()->LogCompiledFunctions();
Steve Block3ce2e202009-11-05 08:53:23 +0000333}
334
335
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000336static void ObjMethod1(const v8::FunctionCallbackInfo<v8::Value>& args) {
Steve Blockd0582a62009-12-15 09:54:21 +0000337}
338
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000339
Steve Blockd0582a62009-12-15 09:54:21 +0000340TEST(LogCallbacks) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000341 SETUP_FLAGS();
342 v8::Isolate::CreateParams create_params;
343 create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
344 v8::Isolate* isolate = v8::Isolate::New(create_params);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000345 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000346 ScopedLoggerInitializer initialize_logger(saved_log, saved_prof, isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000347 Logger* logger = initialize_logger.logger();
Steve Blockd0582a62009-12-15 09:54:21 +0000348
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000349 v8::Local<v8::FunctionTemplate> obj = v8::Local<v8::FunctionTemplate>::New(
350 isolate, v8::FunctionTemplate::New(isolate));
351 obj->SetClassName(v8_str("Obj"));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000352 v8::Local<v8::ObjectTemplate> proto = obj->PrototypeTemplate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000353 v8::Local<v8::Signature> signature = v8::Signature::New(isolate, obj);
354 proto->Set(v8_str("method1"),
355 v8::FunctionTemplate::New(isolate, ObjMethod1,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000356 v8::Local<v8::Value>(), signature),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000357 static_cast<v8::PropertyAttribute>(v8::DontDelete));
Steve Blockd0582a62009-12-15 09:54:21 +0000358
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000359 initialize_logger.env()
360 ->Global()
361 ->Set(initialize_logger.env(), v8_str("Obj"),
362 obj->GetFunction(initialize_logger.env()).ToLocalChecked())
363 .FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000364 CompileRun("Obj.prototype.method1.toString();");
Steve Blockd0582a62009-12-15 09:54:21 +0000365
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000366 logger->LogCompiledFunctions();
Steve Blockd0582a62009-12-15 09:54:21 +0000367
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000368 bool exists = false;
369 i::Vector<const char> log(
370 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
371 CHECK(exists);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000372
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000373 i::EmbeddedVector<char, 100> ref_data;
374 i::SNPrintF(ref_data,
375 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"method1\"",
376 reinterpret_cast<intptr_t>(ObjMethod1));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000377
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000378 CHECK(StrNStr(log.start(), ref_data.start(), log.length()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000379 log.Dispose();
380 }
381 isolate->Dispose();
Steve Blockd0582a62009-12-15 09:54:21 +0000382}
383
384
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000385static void Prop1Getter(v8::Local<v8::String> property,
386 const v8::PropertyCallbackInfo<v8::Value>& info) {
Steve Blockd0582a62009-12-15 09:54:21 +0000387}
388
389static void Prop1Setter(v8::Local<v8::String> property,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000390 v8::Local<v8::Value> value,
391 const v8::PropertyCallbackInfo<void>& info) {
Steve Blockd0582a62009-12-15 09:54:21 +0000392}
393
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000394static void Prop2Getter(v8::Local<v8::String> property,
395 const v8::PropertyCallbackInfo<v8::Value>& info) {
Steve Blockd0582a62009-12-15 09:54:21 +0000396}
397
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000398
Steve Blockd0582a62009-12-15 09:54:21 +0000399TEST(LogAccessorCallbacks) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000400 SETUP_FLAGS();
401 v8::Isolate::CreateParams create_params;
402 create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
403 v8::Isolate* isolate = v8::Isolate::New(create_params);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000404 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000405 ScopedLoggerInitializer initialize_logger(saved_log, saved_prof, isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000406 Logger* logger = initialize_logger.logger();
Steve Blockd0582a62009-12-15 09:54:21 +0000407
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000408 v8::Local<v8::FunctionTemplate> obj = v8::Local<v8::FunctionTemplate>::New(
409 isolate, v8::FunctionTemplate::New(isolate));
410 obj->SetClassName(v8_str("Obj"));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000411 v8::Local<v8::ObjectTemplate> inst = obj->InstanceTemplate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000412 inst->SetAccessor(v8_str("prop1"), Prop1Getter, Prop1Setter);
413 inst->SetAccessor(v8_str("prop2"), Prop2Getter);
Steve Blockd0582a62009-12-15 09:54:21 +0000414
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000415 logger->LogAccessorCallbacks();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000416
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000417 bool exists = false;
418 i::Vector<const char> log(
419 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
420 CHECK(exists);
Steve Blockd0582a62009-12-15 09:54:21 +0000421
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000422 EmbeddedVector<char, 100> prop1_getter_record;
423 i::SNPrintF(prop1_getter_record,
424 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"get prop1\"",
425 reinterpret_cast<intptr_t>(Prop1Getter));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000426 CHECK(StrNStr(log.start(), prop1_getter_record.start(), log.length()));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000427
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000428 EmbeddedVector<char, 100> prop1_setter_record;
429 i::SNPrintF(prop1_setter_record,
430 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"set prop1\"",
431 reinterpret_cast<intptr_t>(Prop1Setter));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000432 CHECK(StrNStr(log.start(), prop1_setter_record.start(), log.length()));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000433
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000434 EmbeddedVector<char, 100> prop2_getter_record;
435 i::SNPrintF(prop2_getter_record,
436 "code-creation,Callback,-2,0x%" V8PRIxPTR ",1,\"get prop2\"",
437 reinterpret_cast<intptr_t>(Prop2Getter));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000438 CHECK(StrNStr(log.start(), prop2_getter_record.start(), log.length()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000439 log.Dispose();
440 }
441 isolate->Dispose();
Steve Block6ded16b2010-05-10 14:33:55 +0100442}
443
444
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000445typedef i::NativesCollection<i::TEST> TestSources;
Steve Blocka7e24c12009-10-30 11:49:00 +0000446
Ben Murdoch589d6972011-11-30 16:04:58 +0000447
448// Test that logging of code create / move events is equivalent to traversal of
449// a resulting heap.
Steve Blocka7e24c12009-10-30 11:49:00 +0000450TEST(EquivalenceOfLoggingAndTraversal) {
451 // This test needs to be run on a "clean" V8 to ensure that snapshot log
452 // is loaded. This is always true when running using tools/test.py because
453 // it launches a new cctest instance for every test. To be sure that launching
454 // cctest manually also works, please be sure that no tests below
455 // are using V8.
Steve Blocka7e24c12009-10-30 11:49:00 +0000456
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000457 // Start with profiling to capture all code events from the beginning.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000458 SETUP_FLAGS();
459 v8::Isolate::CreateParams create_params;
460 create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
461 v8::Isolate* isolate = v8::Isolate::New(create_params);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000462 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000463 ScopedLoggerInitializer initialize_logger(saved_log, saved_prof, isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000464 Logger* logger = initialize_logger.logger();
Steve Blocka7e24c12009-10-30 11:49:00 +0000465
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000466 // Compile and run a function that creates other functions.
467 CompileRun(
468 "(function f(obj) {\n"
469 " obj.test =\n"
470 " (function a(j) { return function b() { return j; } })(100);\n"
471 "})(this);");
472 logger->StopProfiler();
473 reinterpret_cast<i::Isolate*>(isolate)->heap()->CollectAllGarbage(
474 i::Heap::kMakeHeapIterableMask);
475 logger->StringEvent("test-logging-done", "");
Steve Blocka7e24c12009-10-30 11:49:00 +0000476
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000477 // Iterate heap to find compiled functions, will write to log.
478 logger->LogCompiledFunctions();
479 logger->StringEvent("test-traversal-done", "");
Steve Blocka7e24c12009-10-30 11:49:00 +0000480
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000481 bool exists = false;
482 i::Vector<const char> log(
483 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
484 CHECK(exists);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000485 v8::Local<v8::String> log_str =
486 v8::String::NewFromUtf8(isolate, log.start(),
487 v8::NewStringType::kNormal, log.length())
488 .ToLocalChecked();
489 initialize_logger.env()
490 ->Global()
491 ->Set(initialize_logger.env(), v8_str("_log"), log_str)
492 .FromJust();
Steve Blocka7e24c12009-10-30 11:49:00 +0000493
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400494 i::Vector<const char> source = TestSources::GetScriptsSource();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000495 v8::Local<v8::String> source_str =
496 v8::String::NewFromUtf8(isolate, source.start(),
497 v8::NewStringType::kNormal, source.length())
498 .ToLocalChecked();
499 v8::TryCatch try_catch(isolate);
500 v8::Local<v8::Script> script = CompileWithOrigin(source_str, "");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000501 if (script.IsEmpty()) {
502 v8::String::Utf8Value exception(try_catch.Exception());
503 printf("compile: %s\n", *exception);
504 CHECK(false);
505 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000506 v8::Local<v8::Value> result;
507 if (!script->Run(initialize_logger.env()).ToLocal(&result)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000508 v8::String::Utf8Value exception(try_catch.Exception());
509 printf("run: %s\n", *exception);
510 CHECK(false);
511 }
512 // The result either be a "true" literal or problem description.
513 if (!result->IsTrue()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000514 v8::Local<v8::String> s =
515 result->ToString(initialize_logger.env()).ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000516 i::ScopedVector<char> data(s->Utf8Length() + 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000517 CHECK(data.start());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000518 s->WriteUtf8(data.start());
519 printf("%s\n", data.start());
520 // Make sure that our output is written prior crash due to CHECK failure.
521 fflush(stdout);
522 CHECK(false);
523 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000524 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000525 isolate->Dispose();
Steve Blocka7e24c12009-10-30 11:49:00 +0000526}
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400527
528
529TEST(LogVersion) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000530 SETUP_FLAGS();
531 v8::Isolate::CreateParams create_params;
532 create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
533 v8::Isolate* isolate = v8::Isolate::New(create_params);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400534 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000535 ScopedLoggerInitializer initialize_logger(saved_log, saved_prof, isolate);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400536 bool exists = false;
537 i::Vector<const char> log(
538 i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true));
539 CHECK(exists);
540 i::EmbeddedVector<char, 100> ref_data;
541 i::SNPrintF(ref_data, "v8-version,%d,%d,%d,%d,%d", i::Version::GetMajor(),
542 i::Version::GetMinor(), i::Version::GetBuild(),
543 i::Version::GetPatch(), i::Version::IsCandidate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000544 CHECK(StrNStr(log.start(), ref_data.start(), log.length()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400545 log.Dispose();
546 }
547 isolate->Dispose();
548}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000549
550
551// https://crbug.com/539892
552// CodeCreateEvents with really large names should not crash.
553TEST(Issue539892) {
554 class : public i::CodeEventLogger {
555 public:
556 virtual void CodeMoveEvent(Address from, Address to) {}
557 virtual void CodeDeleteEvent(Address from) {}
558 virtual void CodeDisableOptEvent(i::Code* code,
559 i::SharedFunctionInfo* shared) {}
560
561 private:
562 virtual void LogRecordedBuffer(i::Code* code, i::SharedFunctionInfo* shared,
563 const char* name, int length) {}
564 } code_event_logger;
565 SETUP_FLAGS();
566 v8::Isolate::CreateParams create_params;
567 create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
568 v8::Isolate* isolate = v8::Isolate::New(create_params);
569
570 {
571 ScopedLoggerInitializer initialize_logger(saved_log, saved_prof, isolate);
572 Logger* logger = initialize_logger.logger();
573 logger->addCodeEventListener(&code_event_logger);
574
575 // Function with a really large name.
576 const char* source_text =
577 "(function "
578 "baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
579 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
580 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
581 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
582 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
583 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
584 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
585 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
586 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
587 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
588 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
589 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
590 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
591 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
592 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
593 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
594 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac"
595 "(){})();";
596
597 CompileRun(source_text);
598
599 // Must not crash.
600 logger->LogCompiledFunctions();
601 }
602 isolate->Dispose();
603}