blob: aed7466a0992c0130a3a343791547015409b62af [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// 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.
27
28#include "v8.h"
29#include "platform.h"
30#include "cctest.h"
31
32
33v8::internal::Semaphore* semaphore = NULL;
34
35
36v8::Handle<v8::Value> Signal(const v8::Arguments& args) {
37 semaphore->Signal();
38 return v8::Undefined();
39}
40
41
42v8::Handle<v8::Value> TerminateCurrentThread(const v8::Arguments& args) {
Steve Block6ded16b2010-05-10 14:33:55 +010043 CHECK(!v8::V8::IsExecutionTerminating());
Steve Blocka7e24c12009-10-30 11:49:00 +000044 v8::V8::TerminateExecution();
45 return v8::Undefined();
46}
47
48
49v8::Handle<v8::Value> Fail(const v8::Arguments& args) {
50 CHECK(false);
51 return v8::Undefined();
52}
53
54
55v8::Handle<v8::Value> Loop(const v8::Arguments& args) {
Steve Block6ded16b2010-05-10 14:33:55 +010056 CHECK(!v8::V8::IsExecutionTerminating());
Steve Blocka7e24c12009-10-30 11:49:00 +000057 v8::Handle<v8::String> source =
58 v8::String::New("try { doloop(); fail(); } catch(e) { fail(); }");
Steve Block6ded16b2010-05-10 14:33:55 +010059 v8::Handle<v8::Value> result = v8::Script::Compile(source)->Run();
60 CHECK(result.IsEmpty());
61 CHECK(v8::V8::IsExecutionTerminating());
Steve Blocka7e24c12009-10-30 11:49:00 +000062 return v8::Undefined();
63}
64
65
66v8::Handle<v8::Value> DoLoop(const v8::Arguments& args) {
67 v8::TryCatch try_catch;
Steve Block6ded16b2010-05-10 14:33:55 +010068 CHECK(!v8::V8::IsExecutionTerminating());
Steve Blocka7e24c12009-10-30 11:49:00 +000069 v8::Script::Compile(v8::String::New("function f() {"
70 " var term = true;"
71 " try {"
72 " while(true) {"
73 " if (term) terminate();"
74 " term = false;"
75 " }"
76 " fail();"
77 " } catch(e) {"
78 " fail();"
79 " }"
80 "}"
81 "f()"))->Run();
82 CHECK(try_catch.HasCaught());
83 CHECK(try_catch.Exception()->IsNull());
84 CHECK(try_catch.Message().IsEmpty());
85 CHECK(!try_catch.CanContinue());
Steve Block6ded16b2010-05-10 14:33:55 +010086 CHECK(v8::V8::IsExecutionTerminating());
Steve Blocka7e24c12009-10-30 11:49:00 +000087 return v8::Undefined();
88}
89
90
Steve Blockd0582a62009-12-15 09:54:21 +000091v8::Handle<v8::Value> DoLoopNoCall(const v8::Arguments& args) {
92 v8::TryCatch try_catch;
Steve Block6ded16b2010-05-10 14:33:55 +010093 CHECK(!v8::V8::IsExecutionTerminating());
Steve Blockd0582a62009-12-15 09:54:21 +000094 v8::Script::Compile(v8::String::New("var term = true;"
95 "while(true) {"
96 " if (term) terminate();"
97 " term = false;"
98 "}"))->Run();
99 CHECK(try_catch.HasCaught());
100 CHECK(try_catch.Exception()->IsNull());
101 CHECK(try_catch.Message().IsEmpty());
102 CHECK(!try_catch.CanContinue());
Steve Block6ded16b2010-05-10 14:33:55 +0100103 CHECK(v8::V8::IsExecutionTerminating());
Steve Blockd0582a62009-12-15 09:54:21 +0000104 return v8::Undefined();
105}
106
107
Steve Blocka7e24c12009-10-30 11:49:00 +0000108v8::Handle<v8::ObjectTemplate> CreateGlobalTemplate(
Steve Blockd0582a62009-12-15 09:54:21 +0000109 v8::InvocationCallback terminate,
110 v8::InvocationCallback doloop) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000111 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
112 global->Set(v8::String::New("terminate"),
113 v8::FunctionTemplate::New(terminate));
114 global->Set(v8::String::New("fail"), v8::FunctionTemplate::New(Fail));
115 global->Set(v8::String::New("loop"), v8::FunctionTemplate::New(Loop));
Steve Blockd0582a62009-12-15 09:54:21 +0000116 global->Set(v8::String::New("doloop"), v8::FunctionTemplate::New(doloop));
Steve Blocka7e24c12009-10-30 11:49:00 +0000117 return global;
118}
119
120
121// Test that a single thread of JavaScript execution can terminate
122// itself.
123TEST(TerminateOnlyV8ThreadFromThreadItself) {
124 v8::HandleScope scope;
125 v8::Handle<v8::ObjectTemplate> global =
Steve Blockd0582a62009-12-15 09:54:21 +0000126 CreateGlobalTemplate(TerminateCurrentThread, DoLoop);
127 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global);
128 v8::Context::Scope context_scope(context);
Steve Block6ded16b2010-05-10 14:33:55 +0100129 CHECK(!v8::V8::IsExecutionTerminating());
Steve Blockd0582a62009-12-15 09:54:21 +0000130 // Run a loop that will be infinite if thread termination does not work.
131 v8::Handle<v8::String> source =
132 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }");
133 v8::Script::Compile(source)->Run();
134 // Test that we can run the code again after thread termination.
Steve Block6ded16b2010-05-10 14:33:55 +0100135 CHECK(!v8::V8::IsExecutionTerminating());
Steve Blockd0582a62009-12-15 09:54:21 +0000136 v8::Script::Compile(source)->Run();
137 context.Dispose();
138}
139
140
141// Test that a single thread of JavaScript execution can terminate
142// itself in a loop that performs no calls.
143TEST(TerminateOnlyV8ThreadFromThreadItselfNoLoop) {
144 v8::HandleScope scope;
145 v8::Handle<v8::ObjectTemplate> global =
146 CreateGlobalTemplate(TerminateCurrentThread, DoLoopNoCall);
Steve Blocka7e24c12009-10-30 11:49:00 +0000147 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global);
148 v8::Context::Scope context_scope(context);
Steve Block6ded16b2010-05-10 14:33:55 +0100149 CHECK(!v8::V8::IsExecutionTerminating());
Steve Blocka7e24c12009-10-30 11:49:00 +0000150 // Run a loop that will be infinite if thread termination does not work.
151 v8::Handle<v8::String> source =
152 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }");
153 v8::Script::Compile(source)->Run();
Steve Block6ded16b2010-05-10 14:33:55 +0100154 CHECK(!v8::V8::IsExecutionTerminating());
Steve Blocka7e24c12009-10-30 11:49:00 +0000155 // Test that we can run the code again after thread termination.
156 v8::Script::Compile(source)->Run();
157 context.Dispose();
158}
159
160
161class TerminatorThread : public v8::internal::Thread {
162 void Run() {
163 semaphore->Wait();
Steve Block6ded16b2010-05-10 14:33:55 +0100164 CHECK(!v8::V8::IsExecutionTerminating());
Steve Blocka7e24c12009-10-30 11:49:00 +0000165 v8::V8::TerminateExecution();
166 }
167};
168
169
170// Test that a single thread of JavaScript execution can be terminated
171// from the side by another thread.
172TEST(TerminateOnlyV8ThreadFromOtherThread) {
173 semaphore = v8::internal::OS::CreateSemaphore(0);
174 TerminatorThread thread;
175 thread.Start();
176
177 v8::HandleScope scope;
Steve Blockd0582a62009-12-15 09:54:21 +0000178 v8::Handle<v8::ObjectTemplate> global = CreateGlobalTemplate(Signal, DoLoop);
Steve Blocka7e24c12009-10-30 11:49:00 +0000179 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global);
180 v8::Context::Scope context_scope(context);
Steve Block6ded16b2010-05-10 14:33:55 +0100181 CHECK(!v8::V8::IsExecutionTerminating());
Steve Blocka7e24c12009-10-30 11:49:00 +0000182 // Run a loop that will be infinite if thread termination does not work.
183 v8::Handle<v8::String> source =
184 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }");
185 v8::Script::Compile(source)->Run();
186
187 thread.Join();
188 delete semaphore;
189 semaphore = NULL;
190 context.Dispose();
191}
192
193
194class LoopingThread : public v8::internal::Thread {
195 public:
196 void Run() {
197 v8::Locker locker;
198 v8::HandleScope scope;
199 v8_thread_id_ = v8::V8::GetCurrentThreadId();
Steve Blockd0582a62009-12-15 09:54:21 +0000200 v8::Handle<v8::ObjectTemplate> global =
201 CreateGlobalTemplate(Signal, DoLoop);
Steve Blocka7e24c12009-10-30 11:49:00 +0000202 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global);
203 v8::Context::Scope context_scope(context);
Steve Block6ded16b2010-05-10 14:33:55 +0100204 CHECK(!v8::V8::IsExecutionTerminating());
Steve Blocka7e24c12009-10-30 11:49:00 +0000205 // Run a loop that will be infinite if thread termination does not work.
206 v8::Handle<v8::String> source =
207 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }");
208 v8::Script::Compile(source)->Run();
209 context.Dispose();
210 }
211
212 int GetV8ThreadId() { return v8_thread_id_; }
213
214 private:
215 int v8_thread_id_;
216};
217
218
219// Test that multiple threads using V8 can be terminated from another
220// thread when using Lockers and preemption.
221TEST(TerminateMultipleV8Threads) {
222 {
223 v8::Locker locker;
224 v8::V8::Initialize();
225 v8::Locker::StartPreemption(1);
226 semaphore = v8::internal::OS::CreateSemaphore(0);
227 }
228 LoopingThread thread1;
229 thread1.Start();
230 LoopingThread thread2;
231 thread2.Start();
232 // Wait until both threads have signaled the semaphore.
233 semaphore->Wait();
234 semaphore->Wait();
235 {
236 v8::Locker locker;
237 v8::V8::TerminateExecution(thread1.GetV8ThreadId());
238 v8::V8::TerminateExecution(thread2.GetV8ThreadId());
239 }
240 thread1.Join();
241 thread2.Join();
242
243 delete semaphore;
244 semaphore = NULL;
245}
246
247
248int call_count = 0;
249
250
251v8::Handle<v8::Value> TerminateOrReturnObject(const v8::Arguments& args) {
252 if (++call_count == 10) {
Steve Block6ded16b2010-05-10 14:33:55 +0100253 CHECK(!v8::V8::IsExecutionTerminating());
Steve Blocka7e24c12009-10-30 11:49:00 +0000254 v8::V8::TerminateExecution();
255 return v8::Undefined();
256 }
257 v8::Local<v8::Object> result = v8::Object::New();
258 result->Set(v8::String::New("x"), v8::Integer::New(42));
259 return result;
260}
261
262
263v8::Handle<v8::Value> LoopGetProperty(const v8::Arguments& args) {
264 v8::TryCatch try_catch;
Steve Block6ded16b2010-05-10 14:33:55 +0100265 CHECK(!v8::V8::IsExecutionTerminating());
Steve Blocka7e24c12009-10-30 11:49:00 +0000266 v8::Script::Compile(v8::String::New("function f() {"
267 " try {"
268 " while(true) {"
269 " terminate_or_return_object().x;"
270 " }"
271 " fail();"
272 " } catch(e) {"
273 " fail();"
274 " }"
275 "}"
276 "f()"))->Run();
277 CHECK(try_catch.HasCaught());
278 CHECK(try_catch.Exception()->IsNull());
279 CHECK(try_catch.Message().IsEmpty());
280 CHECK(!try_catch.CanContinue());
Steve Block6ded16b2010-05-10 14:33:55 +0100281 CHECK(v8::V8::IsExecutionTerminating());
Steve Blocka7e24c12009-10-30 11:49:00 +0000282 return v8::Undefined();
283}
284
285
286// Test that we correctly handle termination exceptions if they are
287// triggered by the creation of error objects in connection with ICs.
288TEST(TerminateLoadICException) {
289 v8::HandleScope scope;
290 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
291 global->Set(v8::String::New("terminate_or_return_object"),
292 v8::FunctionTemplate::New(TerminateOrReturnObject));
293 global->Set(v8::String::New("fail"), v8::FunctionTemplate::New(Fail));
294 global->Set(v8::String::New("loop"),
295 v8::FunctionTemplate::New(LoopGetProperty));
296
297 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global);
298 v8::Context::Scope context_scope(context);
Steve Block6ded16b2010-05-10 14:33:55 +0100299 CHECK(!v8::V8::IsExecutionTerminating());
Steve Blocka7e24c12009-10-30 11:49:00 +0000300 // Run a loop that will be infinite if thread termination does not work.
301 v8::Handle<v8::String> source =
302 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }");
303 call_count = 0;
304 v8::Script::Compile(source)->Run();
305 // Test that we can run the code again after thread termination.
Steve Block6ded16b2010-05-10 14:33:55 +0100306 CHECK(!v8::V8::IsExecutionTerminating());
Steve Blocka7e24c12009-10-30 11:49:00 +0000307 call_count = 0;
308 v8::Script::Compile(source)->Run();
309 context.Dispose();
310}
Leon Clarkef7060e22010-06-03 12:02:55 +0100311
312v8::Handle<v8::Value> ReenterAfterTermination(const v8::Arguments& args) {
313 v8::TryCatch try_catch;
314 CHECK(!v8::V8::IsExecutionTerminating());
315 v8::Script::Compile(v8::String::New("function f() {"
316 " var term = true;"
317 " try {"
318 " while(true) {"
319 " if (term) terminate();"
320 " term = false;"
321 " }"
322 " fail();"
323 " } catch(e) {"
324 " fail();"
325 " }"
326 "}"
327 "f()"))->Run();
328 CHECK(try_catch.HasCaught());
329 CHECK(try_catch.Exception()->IsNull());
330 CHECK(try_catch.Message().IsEmpty());
331 CHECK(!try_catch.CanContinue());
332 CHECK(v8::V8::IsExecutionTerminating());
333 v8::Script::Compile(v8::String::New("function f() { fail(); } f()"))->Run();
334 return v8::Undefined();
335}
336
337// Test that reentry into V8 while the termination exception is still pending
338// (has not yet unwound the 0-level JS frame) does not crash.
339TEST(TerminateAndReenterFromThreadItself) {
340 v8::HandleScope scope;
341 v8::Handle<v8::ObjectTemplate> global =
342 CreateGlobalTemplate(TerminateCurrentThread, ReenterAfterTermination);
343 v8::Persistent<v8::Context> context = v8::Context::New(NULL, global);
344 v8::Context::Scope context_scope(context);
345 CHECK(!v8::V8::IsExecutionTerminating());
346 v8::Handle<v8::String> source =
347 v8::String::New("try { loop(); fail(); } catch(e) { fail(); }");
348 v8::Script::Compile(source)->Run();
349 CHECK(!v8::V8::IsExecutionTerminating());
350 // Check we can run JS again after termination.
351 CHECK(v8::Script::Compile(v8::String::New("function f() { return true; }"
352 "f()"))->Run()->IsTrue());
353 context.Dispose();
354}
355