• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5var realms = [Realm.current(), Realm.create()];
6
7// Check stack trace filtering across security contexts.
8var thrower_script =
9    "(function () { Realm.eval(Realm.current(), 'throw Error()') })";
10Realm.shared = {
11  thrower_0: Realm.eval(realms[0], thrower_script),
12  thrower_1: Realm.eval(realms[1], thrower_script),
13};
14
15var script = "                                                                 \
16  Error.prepareStackTrace = function(a, b) { return b; };                      \
17  try {                                                                        \
18    Realm.shared.thrower_0();                                                  \
19  } catch (e) {                                                                \
20    Realm.shared.error_0 = e.stack;                                            \
21  }                                                                            \
22  try {                                                                        \
23    Realm.shared.thrower_1();                                                  \
24  } catch (e) {                                                                \
25    Realm.shared.error_1 = e.stack;                                            \
26  }                                                                            \
27";
28
29function assertNotIn(thrower, error) {
30  for (var i = 0; i < error.length; i++) {
31    assertFalse(false === error[i].getFunction());
32  }
33}
34
35Realm.eval(realms[1], script);
36assertSame(3, Realm.shared.error_0.length);
37assertSame(4, Realm.shared.error_1.length);
38
39assertTrue(Realm.shared.thrower_1 === Realm.shared.error_1[2].getFunction());
40assertNotIn(Realm.shared.thrower_0, Realm.shared.error_0);
41assertNotIn(Realm.shared.thrower_0, Realm.shared.error_1);
42
43Realm.eval(realms[0], script);
44assertSame(5, Realm.shared.error_0.length);
45assertSame(4, Realm.shared.error_1.length);
46
47assertTrue(Realm.shared.thrower_0 === Realm.shared.error_0[2].getFunction());
48assertNotIn(Realm.shared.thrower_1, Realm.shared.error_0);
49assertNotIn(Realm.shared.thrower_1, Realm.shared.error_1);
50
51
52// Check .caller filtering across security contexts.
53var caller_script = "(function (f) { f(); })";
54Realm.shared = {
55  caller_0 : Realm.eval(realms[0], caller_script),
56  caller_1 : Realm.eval(realms[1], caller_script),
57}
58
59script = "                                                                     \
60  function f_0() { Realm.shared.result_0 = arguments.callee.caller; };         \
61  function f_1() { Realm.shared.result_1 = arguments.callee.caller; };         \
62  Realm.shared.caller_0(f_0);                                                  \
63  Realm.shared.caller_1(f_1);                                                  \
64";
65
66Realm.eval(realms[1], script);
67assertSame(null, Realm.shared.result_0);
68assertSame(Realm.shared.caller_1, Realm.shared.result_1);
69
70Realm.eval(realms[0], script);
71assertSame(Realm.shared.caller_0, Realm.shared.result_0);
72assertSame(null, Realm.shared.result_1);
73
74
75// test that do not pollute / leak a function prototype v8/4217
76var realmIndex = Realm.create();
77var otherObject = Realm.eval(realmIndex, "Object");
78
79var f = Realm.eval(realmIndex, "function f(){}; f");
80f.prototype = null;
81
82var o = new f();
83var proto = Object.getPrototypeOf(o);
84assertFalse(proto === Object.prototype);
85assertTrue(proto === otherObject.prototype);
86
87o = Realm.eval(realmIndex, "new f()");
88proto = Object.getPrototypeOf(o);
89assertFalse(proto === Object.prototype);
90assertTrue(proto === otherObject.prototype);
91