summaryrefslogtreecommitdiffstats
path: root/js/src/devtools/rootAnalysis/t/virtual/source.cpp
blob: 83633a3436a35f66aea07dba52ccf9049d9450d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#define ANNOTATE(property) __attribute__((annotate(property)))

extern void GC() ANNOTATE("GC Call");

void GC() {
  // If the implementation is too trivial, the function body won't be emitted at
  // all.
  asm("");
}

// Special-cased function -- code that can run JS has an artificial edge to
// js::RunScript.
namespace js {
void RunScript() { GC(); }
}  // namespace js

struct Cell {
  int f;
} ANNOTATE("GC Thing");

extern void foo();

void bar() { GC(); }

typedef void (*func_t)();

class Base {
 public:
  int ANNOTATE("field annotation") dummy;
  virtual void someGC() ANNOTATE("Base pure virtual method") = 0;
  virtual void someGC(int) ANNOTATE("overloaded Base pure virtual method") = 0;
  virtual void sibGC() = 0;
  virtual void onBase() { bar(); }
  func_t functionField;

  // For now, this is just to verify that the plugin doesn't crash. The
  // analysis code does not yet look at this annotation or output it anywhere
  // (though it *is* being recorded.)
  static float testAnnotations() ANNOTATE("static func");

  // Similar, though sixgill currently completely ignores parameter annotations.
  static double testParamAnnotations(Cell& ANNOTATE("param annotation")
                                         ANNOTATE("second param annot") cell)
      ANNOTATE("static func") ANNOTATE("second func");
};

float Base::testAnnotations() {
  asm("");
  return 1.1;
}

double Base::testParamAnnotations(Cell& cell) {
  asm("");
  return 1.2;
}

class Super : public Base {
 public:
  virtual void ANNOTATE("Super pure virtual") noneGC() = 0;
  virtual void allGC() = 0;
  virtual void onSuper() { asm(""); }
  void nonVirtualFunc() { asm(""); }
};

class Sub1 : public Super {
 public:
  void noneGC() override { foo(); }
  void someGC() override ANNOTATE("Sub1 override") ANNOTATE("second attr") {
    foo();
  }
  void someGC(int) override ANNOTATE("Sub1 override for int overload") {
    foo();
  }
  void allGC() override {
    foo();
    bar();
  }
  void sibGC() override { foo(); }
  void onBase() override { foo(); }
} ANNOTATE("CSU1") ANNOTATE("CSU2");

class Sub2 : public Super {
 public:
  void noneGC() override { foo(); }
  void someGC() override {
    foo();
    bar();
  }
  void someGC(int) override {
    foo();
    bar();
  }
  void allGC() override {
    foo();
    bar();
  }
  void sibGC() override { foo(); }
};

class Sibling : public Base {
 public:
  virtual void noneGC() { foo(); }
  void someGC() override {
    foo();
    bar();
  }
  void someGC(int) override {
    foo();
    bar();
  }
  virtual void allGC() {
    foo();
    bar();
  }
  void sibGC() override { bar(); }
};

class AutoSuppressGC {
 public:
  AutoSuppressGC() {}
  ~AutoSuppressGC() {}
} ANNOTATE("Suppress GC");

void use(Cell*) { asm(""); }

class nsISupports {
 public:
  virtual ANNOTATE("Can run script") void danger() { asm(""); }

  virtual ~nsISupports() = 0;
};

class nsIPrincipal : public nsISupports {
 public:
  ~nsIPrincipal() override{};
};

struct JSPrincipals {
  int debugToken;
  JSPrincipals() = default;
  virtual ~JSPrincipals() { GC(); }
};

class nsJSPrincipals : public nsIPrincipal, public JSPrincipals {
 public:
  void Release() { delete this; }
};

class SafePrincipals : public nsIPrincipal {
 public:
  ~SafePrincipals() { foo(); }
};

void f() {
  Sub1 s1;
  Sub2 s2;

  static Cell cell;
  {
    Cell* c1 = &cell;
    s1.noneGC();
    use(c1);
  }
  {
    Cell* c2 = &cell;
    s2.someGC();
    use(c2);
  }
  {
    Cell* c3 = &cell;
    s1.allGC();
    use(c3);
  }
  {
    Cell* c4 = &cell;
    s2.noneGC();
    use(c4);
  }
  {
    Cell* c5 = &cell;
    s2.someGC();
    use(c5);
  }
  {
    Cell* c6 = &cell;
    s2.allGC();
    use(c6);
  }

  Super* super = &s2;
  {
    Cell* c7 = &cell;
    super->noneGC();
    use(c7);
  }
  {
    Cell* c8 = &cell;
    super->someGC();
    use(c8);
  }
  {
    Cell* c9 = &cell;
    super->allGC();
    use(c9);
  }

  {
    Cell* c10 = &cell;
    s1.functionField();
    use(c10);
  }
  {
    Cell* c11 = &cell;
    super->functionField();
    use(c11);
  }
  {
    Cell* c12 = &cell;
    super->sibGC();
    use(c12);
  }

  Base* base = &s2;
  {
    Cell* c13 = &cell;
    base->sibGC();
    use(c13);
  }

  nsJSPrincipals pals;
  {
    Cell* c14 = &cell;
    nsISupports* p = &pals;
    p->danger();
    use(c14);
  }

  // Base defines, Sub1 overrides, static Super can call either.
  {
    Cell* c15 = &cell;
    super->onBase();
    use(c15);
  }

  {
    Cell* c16 = &cell;
    s2.someGC(7);
    use(c16);
  }

  {
    Cell* c17 = &cell;
    super->someGC(7);
    use(c17);
  }

  {
    nsJSPrincipals* princ = new nsJSPrincipals();
    Cell* c18 = &cell;
    delete princ;  // Can GC
    use(c18);
  }

  {
    nsJSPrincipals* princ = new nsJSPrincipals();
    nsISupports* supp = static_cast<nsISupports*>(princ);
    Cell* c19 = &cell;
    delete supp;  // Can GC
    use(c19);
  }

  {
    auto* safe = new SafePrincipals();
    Cell* c20 = &cell;
    delete safe;  // Cannot GC
    use(c20);
  }

  {
    auto* safe = new SafePrincipals();
    nsISupports* supp = static_cast<nsISupports*>(safe);
    Cell* c21 = &cell;
    delete supp;  // Compiler thinks destructor can GC.
    use(c21);
  }
}