Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
| 2 | // |
| 3 | // Tests of logging functions from log.h |
| 4 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5 | #ifdef __linux__ |
| 6 | #include <math.h> |
| 7 | #include <pthread.h> |
| 8 | #include <signal.h> |
| 9 | #include <unistd.h> |
| 10 | #endif // __linux__ |
| 11 | |
| 12 | #include "v8.h" |
| 13 | #include "log.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 14 | #include "cpu-profiler.h" |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 15 | #include "natives.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16 | #include "v8threads.h" |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 17 | #include "v8utils.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 18 | #include "cctest.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 19 | #include "vm-state-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 20 | |
| 21 | using v8::internal::Address; |
| 22 | using v8::internal::EmbeddedVector; |
| 23 | using v8::internal::Logger; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 24 | using v8::internal::StrLength; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 25 | |
| 26 | namespace i = v8::internal; |
| 27 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | class ScopedLoggerInitializer { |
| 31 | public: |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 32 | explicit ScopedLoggerInitializer(bool prof_lazy) |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 33 | : saved_log_(i::FLAG_log), |
| 34 | saved_prof_lazy_(i::FLAG_prof_lazy), |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 35 | saved_prof_(i::FLAG_prof), |
| 36 | saved_prof_auto_(i::FLAG_prof_auto), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 37 | temp_file_(NULL), |
| 38 | // Need to run this prior to creating the scope. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 39 | trick_to_run_init_flags_(init_flags_(prof_lazy)), |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 40 | scope_(), |
| 41 | env_(v8::Context::New()) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 42 | env_->Enter(); |
| 43 | } |
| 44 | |
| 45 | ~ScopedLoggerInitializer() { |
| 46 | env_->Exit(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 47 | LOGGER->TearDown(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 48 | if (temp_file_ != NULL) fclose(temp_file_); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 49 | i::FLAG_prof_lazy = saved_prof_lazy_; |
| 50 | i::FLAG_prof = saved_prof_; |
| 51 | i::FLAG_prof_auto = saved_prof_auto_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 52 | i::FLAG_log = saved_log_; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | v8::Handle<v8::Context>& env() { return env_; } |
| 56 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 57 | FILE* StopLoggingGetTempFile() { |
| 58 | temp_file_ = LOGGER->TearDown(); |
| 59 | CHECK_NE(NULL, temp_file_); |
| 60 | fflush(temp_file_); |
| 61 | rewind(temp_file_); |
| 62 | return temp_file_; |
| 63 | } |
| 64 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 65 | private: |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 66 | static bool init_flags_(bool prof_lazy) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 67 | i::FLAG_log = true; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 68 | i::FLAG_prof = true; |
| 69 | i::FLAG_prof_lazy = prof_lazy; |
| 70 | i::FLAG_prof_auto = false; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 71 | i::FLAG_logfile = i::Log::kLogToTemporaryFile; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 72 | return prof_lazy; |
| 73 | } |
| 74 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 75 | const bool saved_log_; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 76 | const bool saved_prof_lazy_; |
| 77 | const bool saved_prof_; |
| 78 | const bool saved_prof_auto_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 79 | FILE* temp_file_; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 80 | const bool trick_to_run_init_flags_; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 81 | v8::HandleScope scope_; |
| 82 | v8::Handle<v8::Context> env_; |
| 83 | |
| 84 | DISALLOW_COPY_AND_ASSIGN(ScopedLoggerInitializer); |
| 85 | }; |
| 86 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 87 | } // namespace |
| 88 | |
| 89 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 90 | static const char* StrNStr(const char* s1, const char* s2, int n) { |
| 91 | if (s1[n] == '\0') return strstr(s1, s2); |
| 92 | i::ScopedVector<char> str(n + 1); |
| 93 | i::OS::StrNCpy(str, s1, static_cast<size_t>(n)); |
| 94 | str[n] = '\0'; |
| 95 | char* found = strstr(str.start(), s2); |
| 96 | return found != NULL ? s1 + (found - str.start()) : NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | |
| 100 | TEST(ProfLazyMode) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 101 | ScopedLoggerInitializer initialize_logger(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 102 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 103 | if (!i::V8::UseCrankshaft()) return; |
| 104 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 105 | LOGGER->StringEvent("test-start", ""); |
| 106 | CompileRun("var a = (function(x) { return x + 1; })(10);"); |
| 107 | LOGGER->StringEvent("test-profiler-start", ""); |
| 108 | v8::V8::ResumeProfiler(); |
| 109 | CompileRun( |
| 110 | "var b = (function(x) { return x + 2; })(10);\n" |
| 111 | "var c = (function(x) { return x + 3; })(10);\n" |
| 112 | "var d = (function(x) { return x + 4; })(10);\n" |
| 113 | "var e = (function(x) { return x + 5; })(10);"); |
| 114 | v8::V8::PauseProfiler(); |
| 115 | LOGGER->StringEvent("test-profiler-stop", ""); |
| 116 | CompileRun("var f = (function(x) { return x + 6; })(10);"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 117 | // Check that profiling can be resumed again. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 118 | LOGGER->StringEvent("test-profiler-start-2", ""); |
| 119 | v8::V8::ResumeProfiler(); |
| 120 | CompileRun( |
| 121 | "var g = (function(x) { return x + 7; })(10);\n" |
| 122 | "var h = (function(x) { return x + 8; })(10);\n" |
| 123 | "var i = (function(x) { return x + 9; })(10);\n" |
| 124 | "var j = (function(x) { return x + 10; })(10);"); |
| 125 | v8::V8::PauseProfiler(); |
| 126 | LOGGER->StringEvent("test-profiler-stop-2", ""); |
| 127 | LOGGER->StringEvent("test-stop", ""); |
| 128 | |
| 129 | bool exists = false; |
| 130 | i::Vector<const char> log( |
| 131 | i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true)); |
| 132 | CHECK(exists); |
| 133 | |
| 134 | const char* test_start_position = |
| 135 | StrNStr(log.start(), "test-start,", log.length()); |
| 136 | CHECK_NE(NULL, test_start_position); |
| 137 | const char* test_profiler_start_position = |
| 138 | StrNStr(log.start(), "test-profiler-start,", log.length()); |
| 139 | CHECK_NE(NULL, test_profiler_start_position); |
| 140 | CHECK_GT(test_profiler_start_position, test_start_position); |
| 141 | const char* test_profiler_stop_position = |
| 142 | StrNStr(log.start(), "test-profiler-stop,", log.length()); |
| 143 | CHECK_NE(NULL, test_profiler_stop_position); |
| 144 | CHECK_GT(test_profiler_stop_position, test_profiler_start_position); |
| 145 | const char* test_profiler_start_2_position = |
| 146 | StrNStr(log.start(), "test-profiler-start-2,", log.length()); |
| 147 | CHECK_NE(NULL, test_profiler_start_2_position); |
| 148 | CHECK_GT(test_profiler_start_2_position, test_profiler_stop_position); |
| 149 | |
| 150 | // Nothing must be logged until profiling is resumed. |
| 151 | CHECK_EQ(NULL, StrNStr(test_start_position, |
| 152 | "code-creation,", |
| 153 | static_cast<int>(test_profiler_start_position - |
| 154 | test_start_position))); |
| 155 | // Nothing must be logged while profiling is suspended. |
| 156 | CHECK_EQ(NULL, StrNStr(test_profiler_stop_position, |
| 157 | "code-creation,", |
| 158 | static_cast<int>(test_profiler_start_2_position - |
| 159 | test_profiler_stop_position))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 163 | // BUG(913). Need to implement support for profiling multiple VM threads. |
| 164 | #if 0 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 165 | |
| 166 | namespace { |
| 167 | |
| 168 | class LoopingThread : public v8::internal::Thread { |
| 169 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 170 | explicit LoopingThread(v8::internal::Isolate* isolate) |
| 171 | : v8::internal::Thread(isolate), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 172 | semaphore_(v8::internal::OS::CreateSemaphore(0)), |
| 173 | run_(true) { |
| 174 | } |
| 175 | |
| 176 | virtual ~LoopingThread() { delete semaphore_; } |
| 177 | |
| 178 | void Run() { |
| 179 | self_ = pthread_self(); |
| 180 | RunLoop(); |
| 181 | } |
| 182 | |
| 183 | void SendSigProf() { pthread_kill(self_, SIGPROF); } |
| 184 | |
| 185 | void Stop() { run_ = false; } |
| 186 | |
| 187 | bool WaitForRunning() { return semaphore_->Wait(1000000); } |
| 188 | |
| 189 | protected: |
| 190 | bool IsRunning() { return run_; } |
| 191 | |
| 192 | virtual void RunLoop() = 0; |
| 193 | |
| 194 | void SetV8ThreadId() { |
| 195 | v8_thread_id_ = v8::V8::GetCurrentThreadId(); |
| 196 | } |
| 197 | |
| 198 | void SignalRunning() { semaphore_->Signal(); } |
| 199 | |
| 200 | private: |
| 201 | v8::internal::Semaphore* semaphore_; |
| 202 | bool run_; |
| 203 | pthread_t self_; |
| 204 | int v8_thread_id_; |
| 205 | }; |
| 206 | |
| 207 | |
| 208 | class LoopingJsThread : public LoopingThread { |
| 209 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 210 | explicit LoopingJsThread(v8::internal::Isolate* isolate) |
| 211 | : LoopingThread(isolate) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 212 | void RunLoop() { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 213 | v8::Locker locker; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 214 | CHECK(i::Isolate::Current() != NULL); |
| 215 | CHECK_GT(i::Isolate::Current()->thread_manager()->CurrentId(), 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 216 | SetV8ThreadId(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 217 | while (IsRunning()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 218 | v8::HandleScope scope; |
| 219 | v8::Persistent<v8::Context> context = v8::Context::New(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 220 | CHECK(!context.IsEmpty()); |
| 221 | { |
| 222 | v8::Context::Scope context_scope(context); |
| 223 | SignalRunning(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 224 | CompileRun( |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 225 | "var j; for (var i=0; i<10000; ++i) { j = Math.sin(i); }"); |
| 226 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 227 | context.Dispose(); |
| 228 | i::OS::Sleep(1); |
| 229 | } |
| 230 | } |
| 231 | }; |
| 232 | |
| 233 | |
| 234 | class LoopingNonJsThread : public LoopingThread { |
| 235 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 236 | explicit LoopingNonJsThread(v8::internal::Isolate* isolate) |
| 237 | : LoopingThread(isolate) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 238 | void RunLoop() { |
| 239 | v8::Locker locker; |
| 240 | v8::Unlocker unlocker; |
| 241 | // Now thread has V8's id, but will not run VM code. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 242 | CHECK(i::Isolate::Current() != NULL); |
| 243 | CHECK_GT(i::Isolate::Current()->thread_manager()->CurrentId(), 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 244 | double i = 10; |
| 245 | SignalRunning(); |
| 246 | while (IsRunning()) { |
| 247 | i = sin(i); |
| 248 | i::OS::Sleep(1); |
| 249 | } |
| 250 | } |
| 251 | }; |
| 252 | |
| 253 | |
| 254 | class TestSampler : public v8::internal::Sampler { |
| 255 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 256 | explicit TestSampler(v8::internal::Isolate* isolate) |
| 257 | : Sampler(isolate, 0, true, true), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 258 | semaphore_(v8::internal::OS::CreateSemaphore(0)), |
| 259 | was_sample_stack_called_(false) { |
| 260 | } |
| 261 | |
| 262 | ~TestSampler() { delete semaphore_; } |
| 263 | |
| 264 | void SampleStack(v8::internal::TickSample*) { |
| 265 | was_sample_stack_called_ = true; |
| 266 | } |
| 267 | |
| 268 | void Tick(v8::internal::TickSample*) { semaphore_->Signal(); } |
| 269 | |
| 270 | bool WaitForTick() { return semaphore_->Wait(1000000); } |
| 271 | |
| 272 | void Reset() { was_sample_stack_called_ = false; } |
| 273 | |
| 274 | bool WasSampleStackCalled() { return was_sample_stack_called_; } |
| 275 | |
| 276 | private: |
| 277 | v8::internal::Semaphore* semaphore_; |
| 278 | bool was_sample_stack_called_; |
| 279 | }; |
| 280 | |
| 281 | |
| 282 | } // namespace |
| 283 | |
| 284 | TEST(ProfMultipleThreads) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 285 | TestSampler* sampler = NULL; |
| 286 | { |
| 287 | v8::Locker locker; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 288 | sampler = new TestSampler(v8::internal::Isolate::Current()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 289 | sampler->Start(); |
| 290 | CHECK(sampler->IsActive()); |
| 291 | } |
| 292 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 293 | LoopingJsThread jsThread(v8::internal::Isolate::Current()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 294 | jsThread.Start(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 295 | LoopingNonJsThread nonJsThread(v8::internal::Isolate::Current()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 296 | nonJsThread.Start(); |
| 297 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 298 | CHECK(!sampler->WasSampleStackCalled()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 299 | jsThread.WaitForRunning(); |
| 300 | jsThread.SendSigProf(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 301 | CHECK(sampler->WaitForTick()); |
| 302 | CHECK(sampler->WasSampleStackCalled()); |
| 303 | sampler->Reset(); |
| 304 | CHECK(!sampler->WasSampleStackCalled()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 305 | nonJsThread.WaitForRunning(); |
| 306 | nonJsThread.SendSigProf(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 307 | CHECK(!sampler->WaitForTick()); |
| 308 | CHECK(!sampler->WasSampleStackCalled()); |
| 309 | sampler->Stop(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 310 | |
| 311 | jsThread.Stop(); |
| 312 | nonJsThread.Stop(); |
| 313 | jsThread.Join(); |
| 314 | nonJsThread.Join(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 315 | |
| 316 | delete sampler; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | #endif // __linux__ |
| 320 | |
| 321 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 322 | // Test for issue http://crbug.com/23768 in Chromium. |
| 323 | // Heap can contain scripts with already disposed external sources. |
| 324 | // We need to verify that LogCompiledFunctions doesn't crash on them. |
| 325 | namespace { |
| 326 | |
| 327 | class SimpleExternalString : public v8::String::ExternalStringResource { |
| 328 | public: |
| 329 | explicit SimpleExternalString(const char* source) |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 330 | : utf_source_(StrLength(source)) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 331 | for (int i = 0; i < utf_source_.length(); ++i) |
| 332 | utf_source_[i] = source[i]; |
| 333 | } |
| 334 | virtual ~SimpleExternalString() {} |
| 335 | virtual size_t length() const { return utf_source_.length(); } |
| 336 | virtual const uint16_t* data() const { return utf_source_.start(); } |
| 337 | private: |
| 338 | i::ScopedVector<uint16_t> utf_source_; |
| 339 | }; |
| 340 | |
| 341 | } // namespace |
| 342 | |
| 343 | TEST(Issue23768) { |
| 344 | v8::HandleScope scope; |
| 345 | v8::Handle<v8::Context> env = v8::Context::New(); |
| 346 | env->Enter(); |
| 347 | |
| 348 | SimpleExternalString source_ext_str("(function ext() {})();"); |
| 349 | v8::Local<v8::String> source = v8::String::NewExternal(&source_ext_str); |
| 350 | // Script needs to have a name in order to trigger InitLineEnds execution. |
| 351 | v8::Handle<v8::String> origin = v8::String::New("issue-23768-test"); |
| 352 | v8::Handle<v8::Script> evil_script = v8::Script::Compile(source, origin); |
| 353 | CHECK(!evil_script.IsEmpty()); |
| 354 | CHECK(!evil_script->Run().IsEmpty()); |
| 355 | i::Handle<i::ExternalTwoByteString> i_source( |
| 356 | i::ExternalTwoByteString::cast(*v8::Utils::OpenHandle(*source))); |
| 357 | // This situation can happen if source was an external string disposed |
| 358 | // by its owner. |
| 359 | i_source->set_resource(NULL); |
| 360 | |
| 361 | // Must not crash. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 362 | LOGGER->LogCompiledFunctions(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 366 | static v8::Handle<v8::Value> ObjMethod1(const v8::Arguments& args) { |
| 367 | return v8::Handle<v8::Value>(); |
| 368 | } |
| 369 | |
| 370 | TEST(LogCallbacks) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 371 | ScopedLoggerInitializer initialize_logger(false); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 372 | |
| 373 | v8::Persistent<v8::FunctionTemplate> obj = |
| 374 | v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 375 | obj->SetClassName(v8_str("Obj")); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 376 | v8::Handle<v8::ObjectTemplate> proto = obj->PrototypeTemplate(); |
| 377 | v8::Local<v8::Signature> signature = v8::Signature::New(obj); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 378 | proto->Set(v8_str("method1"), |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 379 | v8::FunctionTemplate::New(ObjMethod1, |
| 380 | v8::Handle<v8::Value>(), |
| 381 | signature), |
| 382 | static_cast<v8::PropertyAttribute>(v8::DontDelete)); |
| 383 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 384 | initialize_logger.env()->Global()->Set(v8_str("Obj"), obj->GetFunction()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 385 | CompileRun("Obj.prototype.method1.toString();"); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 386 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 387 | LOGGER->LogCompiledFunctions(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 388 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 389 | bool exists = false; |
| 390 | i::Vector<const char> log( |
| 391 | i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true)); |
| 392 | CHECK(exists); |
| 393 | |
| 394 | i::EmbeddedVector<char, 100> ref_data; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 395 | i::OS::SNPrintF(ref_data, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 396 | "code-creation,Callback,0x%" V8PRIxPTR ",1,\"method1\"\0", |
| 397 | ObjMethod1); |
| 398 | |
| 399 | CHECK_NE(NULL, StrNStr(log.start(), ref_data.start(), log.length())); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 400 | |
| 401 | obj.Dispose(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | |
| 405 | static v8::Handle<v8::Value> Prop1Getter(v8::Local<v8::String> property, |
| 406 | const v8::AccessorInfo& info) { |
| 407 | return v8::Handle<v8::Value>(); |
| 408 | } |
| 409 | |
| 410 | static void Prop1Setter(v8::Local<v8::String> property, |
| 411 | v8::Local<v8::Value> value, |
| 412 | const v8::AccessorInfo& info) { |
| 413 | } |
| 414 | |
| 415 | static v8::Handle<v8::Value> Prop2Getter(v8::Local<v8::String> property, |
| 416 | const v8::AccessorInfo& info) { |
| 417 | return v8::Handle<v8::Value>(); |
| 418 | } |
| 419 | |
| 420 | TEST(LogAccessorCallbacks) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 421 | ScopedLoggerInitializer initialize_logger(false); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 422 | |
| 423 | v8::Persistent<v8::FunctionTemplate> obj = |
| 424 | v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 425 | obj->SetClassName(v8_str("Obj")); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 426 | v8::Handle<v8::ObjectTemplate> inst = obj->InstanceTemplate(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 427 | inst->SetAccessor(v8_str("prop1"), Prop1Getter, Prop1Setter); |
| 428 | inst->SetAccessor(v8_str("prop2"), Prop2Getter); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 429 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 430 | LOGGER->LogAccessorCallbacks(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 431 | |
| 432 | bool exists = false; |
| 433 | i::Vector<const char> log( |
| 434 | i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true)); |
| 435 | CHECK(exists); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 436 | |
| 437 | EmbeddedVector<char, 100> prop1_getter_record; |
| 438 | i::OS::SNPrintF(prop1_getter_record, |
| 439 | "code-creation,Callback,0x%" V8PRIxPTR ",1,\"get prop1\"", |
| 440 | Prop1Getter); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 441 | CHECK_NE(NULL, |
| 442 | StrNStr(log.start(), prop1_getter_record.start(), log.length())); |
| 443 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 444 | EmbeddedVector<char, 100> prop1_setter_record; |
| 445 | i::OS::SNPrintF(prop1_setter_record, |
| 446 | "code-creation,Callback,0x%" V8PRIxPTR ",1,\"set prop1\"", |
| 447 | Prop1Setter); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 448 | CHECK_NE(NULL, |
| 449 | StrNStr(log.start(), prop1_setter_record.start(), log.length())); |
| 450 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 451 | EmbeddedVector<char, 100> prop2_getter_record; |
| 452 | i::OS::SNPrintF(prop2_getter_record, |
| 453 | "code-creation,Callback,0x%" V8PRIxPTR ",1,\"get prop2\"", |
| 454 | Prop2Getter); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 455 | CHECK_NE(NULL, |
| 456 | StrNStr(log.start(), prop2_getter_record.start(), log.length())); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 457 | |
| 458 | obj.Dispose(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 459 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 460 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 461 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 462 | TEST(IsLoggingPreserved) { |
| 463 | ScopedLoggerInitializer initialize_logger(false); |
| 464 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 465 | CHECK(LOGGER->is_logging()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 466 | LOGGER->ResumeProfiler(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 467 | CHECK(LOGGER->is_logging()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 468 | LOGGER->PauseProfiler(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 469 | CHECK(LOGGER->is_logging()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 473 | typedef i::NativesCollection<i::TEST> TestSources; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 474 | |
| 475 | // Test that logging of code create / move / delete events |
| 476 | // is equivalent to traversal of a resulting heap. |
| 477 | TEST(EquivalenceOfLoggingAndTraversal) { |
| 478 | // This test needs to be run on a "clean" V8 to ensure that snapshot log |
| 479 | // is loaded. This is always true when running using tools/test.py because |
| 480 | // it launches a new cctest instance for every test. To be sure that launching |
| 481 | // cctest manually also works, please be sure that no tests below |
| 482 | // are using V8. |
| 483 | // |
| 484 | // P.S. No, V8 can't be re-initialized after disposal, see include/v8.h. |
| 485 | CHECK(!i::V8::IsRunning()); |
| 486 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 487 | // Start with profiling to capture all code events from the beginning. |
| 488 | ScopedLoggerInitializer initialize_logger(false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 489 | |
| 490 | // Compile and run a function that creates other functions. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 491 | CompileRun( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 492 | "(function f(obj) {\n" |
| 493 | " obj.test =\n" |
| 494 | " (function a(j) { return function b() { return j; } })(100);\n" |
| 495 | "})(this);"); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 496 | v8::V8::PauseProfiler(); |
| 497 | HEAP->CollectAllGarbage(true); |
| 498 | LOGGER->StringEvent("test-logging-done", ""); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 499 | |
| 500 | // Iterate heap to find compiled functions, will write to log. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 501 | LOGGER->LogCompiledFunctions(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 502 | LOGGER->StringEvent("test-traversal-done", ""); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 503 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 504 | bool exists = false; |
| 505 | i::Vector<const char> log( |
| 506 | i::ReadFile(initialize_logger.StopLoggingGetTempFile(), &exists, true)); |
| 507 | CHECK(exists); |
| 508 | v8::Handle<v8::String> log_str = v8::String::New(log.start(), log.length()); |
| 509 | initialize_logger.env()->Global()->Set(v8_str("_log"), log_str); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 510 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 511 | i::Vector<const unsigned char> source = TestSources::GetScriptsSource(); |
| 512 | v8::Handle<v8::String> source_str = v8::String::New( |
| 513 | reinterpret_cast<const char*>(source.start()), source.length()); |
| 514 | v8::TryCatch try_catch; |
| 515 | v8::Handle<v8::Script> script = v8::Script::Compile(source_str, v8_str("")); |
| 516 | if (script.IsEmpty()) { |
| 517 | v8::String::Utf8Value exception(try_catch.Exception()); |
| 518 | printf("compile: %s\n", *exception); |
| 519 | CHECK(false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 520 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 521 | v8::Handle<v8::Value> result = script->Run(); |
| 522 | if (result.IsEmpty()) { |
| 523 | v8::String::Utf8Value exception(try_catch.Exception()); |
| 524 | printf("run: %s\n", *exception); |
| 525 | CHECK(false); |
| 526 | } |
| 527 | // The result either be a "true" literal or problem description. |
| 528 | if (!result->IsTrue()) { |
| 529 | v8::Local<v8::String> s = result->ToString(); |
| 530 | i::ScopedVector<char> data(s->Length() + 1); |
| 531 | CHECK_NE(NULL, data.start()); |
| 532 | s->WriteAscii(data.start()); |
| 533 | printf("%s\n", data.start()); |
| 534 | // Make sure that our output is written prior crash due to CHECK failure. |
| 535 | fflush(stdout); |
| 536 | CHECK(false); |
| 537 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 538 | } |