cb70ee67bc50a919748212579eb6e2496272a701
[IRC.git] / Robust / src / Tests / OwnershipAnalysisTest / test01 / test01.java
1
2 public class Parameter {
3   flag w;
4   int a;
5   int b;
6   Parameter f;
7   Parameter g;
8   Penguin p;
9   Foo h;
10   
11   public Parameter() { a = 0; b = 0; f = null; g = null; }
12   
13   public void bar() { foo(); }
14   public void foo() { bar(); }
15 }
16
17 public class Penguin {
18   int x;
19   int y;
20   Foo h;    
21   
22   public Penguin() { x = 0; y = 0; }
23   
24   public void bar() { x = 1; }
25 }
26
27 public class Voo {
28   flag f; int x; Baw b; Baw bb;
29   
30   public Voo() {}
31   
32   static Voo getAVoo( Voo bogus ) {
33     while( false ) {
34       if( false ) {
35         return new Voo();
36       } else {
37         return new Voo();
38       }
39     }
40     return new Voo();
41   }
42 }
43
44 public class Baw {
45   int y;
46   Foo f;
47   String s;
48
49   public Baw() {}
50   
51   public void doTheBaw( Voo v ) { v = new Voo(); }
52 }
53
54
55 public class Foo {
56   flag f;
57   
58   public Foo() {}
59   
60   public Foo x;
61   public Foo y;
62   public Foo z;
63   public Baw b;
64
65   public Foo getAFoo() {
66     return new Foo();
67   }
68
69   public Foo newFooChain( Foo a ) {
70     Foo b = new Foo();
71     b.x = a;
72     return b;
73   }
74
75   public void ruinSomeFoos( Foo a, Foo b ) {
76     a.x = b.x;
77   }
78   
79   static public void aStaticMethod( Foo p0, Foo p1 ) {
80     Foo f0 = new Foo();
81     Foo f1 = new Foo();
82     Foo f2 = new Foo();
83     
84     f0.x = f1;
85     p0.x = f0;
86     p1.x = f1;
87     p1.x = f2;
88   }
89   
90   static public void m1_( Foo p0 ) {
91     Foo g0 = new Foo();
92     Foo g1 = new Foo();
93     
94     g0.x = p0;
95     p0.x = g1;
96   }
97   
98   static public void m2_( Foo p0 ) {
99     Foo g0 = new Foo();
100     
101     g0.x = p0;
102     g0.y = p0;
103   }
104   
105   static public void m3_( Foo p0 ) {
106     Foo g0 = new Foo();
107     
108     p0.x = g0;
109     p0.y = g0;
110   }
111
112   static public void m4_( Foo p0 ) {
113     p0.x = p0;
114   }
115
116   static public void m5_( Foo p0 ) {
117     Foo g0 = new Foo();
118     p0.x = g0;
119     g0.x = p0;
120   }
121
122   static public void m6_( Foo p0, Foo p1 ) {
123     Foo g0 = new Foo();
124     Foo g1 = new Foo();
125     Foo g2 = new Foo();
126     Foo g3 = new Foo();
127
128     g0.x = p0;
129     p0.x = g1;
130
131     g2.x = p1;
132     p1.x = g3;
133
134     p0.y = p1;
135   }
136
137   static public void m7_( Foo p0, Foo p1 ) {
138     Foo g0 = new Foo();
139     Foo g1 = new Foo();
140     Foo g2 = new Foo();
141     Foo g3 = new Foo();
142
143     g0.x = p0;
144     p0.x = g1;
145
146     g2.x = p1;
147     p1.x = g3;
148
149     p0.y = p1;
150     p0.z = p1;
151   }
152
153   static public void m8_( Foo p0, Foo p1 ) {
154     Foo g0 = new Foo();
155     Foo g1 = new Foo();
156     Foo g2 = new Foo();
157     Foo g3 = new Foo();
158
159     g0.x = p0;
160     p0.x = g1;
161
162     g2.x = p1;
163     p1.x = g3;
164
165     p0.y = p1;
166     p1.y = p0;
167   }
168 }
169
170
171
172 // this empty task should still create a non-empty
173 // ownership graph showing parameter allocation
174 // look for the parameter s as a label referencing
175 // a heap region that is multi-object, flagged, not summary
176 task Startup( StartupObject s{ initialstate } ) {
177
178   Parameter p0 = new Parameter();
179
180   //int a, b, c;
181
182
183   int a = 1;
184   int b = 2;
185   int c = 3;
186
187   b = c;
188   a = b;
189
190
191   taskexit( s{ !initialstate } );
192 }
193
194
195 task NewObjectA( Foo a{ f }, Foo b{ f } ) {
196
197     Foo c = new Foo();
198     Foo d = new Foo();
199
200     c.x = d;
201     a.x = c;
202
203     taskexit( a{ !f }, b{ !f } );
204 }
205
206
207 task NewObjectB( Foo a{ f }, Foo b{ f } ) {
208
209     Foo c = new Foo();
210     Foo d = new Foo();
211
212     a.x = c;
213     c.x = d;
214
215     taskexit( a{ !f }, b{ !f } );
216 }
217
218
219 task NewObjectC( Foo a{ f }, Foo b{ f } ) {
220
221     Foo z = new Foo();
222     a.x = z;
223     a.y = z;
224
225     Foo c;
226
227     while( false ) {
228         c     = new Foo();
229         Foo f = new Foo();
230         c.x   = f;
231         c.y   = f;
232     }
233
234     taskexit( a{ !f }, b{ !f } );
235 }
236
237
238 task forMethod( Foo p0{ f } ) {
239
240     Foo a0;
241     Foo a1;
242
243     while( false ) {    
244         a1 = a0;            
245         if( false ) {
246             a0 = new Foo();
247         }
248     }
249
250     Foo z = new Foo();
251     a1.x = z;
252     z.x  = a1;
253
254
255     taskexit( p0{ !f } );
256 }
257
258
259 // this task allocates a new object, so there should
260 // be a heap region for the parameter, and several
261 // heap regions for the allocation site, but the label
262 // merely points to the newest region
263 task NewObjectInMethod( Voo v{ f } ) {
264     Voo w = new Voo();
265     Baw b = new Baw();
266     b.doTheBaw( w );
267
268     taskexit( v{ !f } );
269 }
270
271
272 task ClobberInitParamReflex( Voo v{ f }, Voo w{ f } ) {
273     v.b = v.bb;
274
275     taskexit( v{ !f }, w{ !f } );
276 }
277
278
279 task BackToItself( Parameter p0{ w } ) {
280
281     Penguin p = new Penguin();
282     p0.p = p;
283     p.h = p0.h;
284
285     while( false ) {
286         Parameter p1   = new Parameter();
287                   p1.h = new Foo();
288         Penguin   q    = new Penguin();
289         p1.p = q;
290         q.h  = p1.h;
291     }
292
293     taskexit( p0{ !w } );
294 }
295
296
297 task SummaryNodeTokens( Foo p0{ f } ) {
298
299     while( false ) {
300         Foo a = new Foo();
301         a.x   = new Foo();
302         a.x.x = new Foo();
303     }
304     
305     Foo b;
306     while( false ) {
307         Foo c = new Foo();
308         c.x = b;
309         b = c;
310     }
311
312     taskexit( p0{ !f } );
313 }
314
315
316 task strongUpdates( Foo p0{ f } ) {
317
318     Foo b = new Foo();
319
320     Foo a = new Foo();
321     if( false ) {
322         a.x = new Foo();
323         a.y = new Foo();
324     } else if( false ) {
325         a.x = new Foo();
326         a.y = new Foo();
327     }
328
329     // this should effect a strong update
330     a.x = b;
331
332
333     if( false ) {
334         p0.x = new Foo();
335         p0.y = new Foo();
336     } else if( false ) {
337         p0.x = new Foo();
338         p0.y = new Foo();
339     }
340
341     // p0 points to a multiple-object heap region
342     // so this should not make a strong update
343     p0.x = b;
344     
345     taskexit( p0{ !f } );
346 }
347
348
349 task ObjectChainByMethodCalls( Foo a{ f } ) {
350
351   Foo f = a.getAFoo();
352   Foo g = a.newFooChain( f );
353   // g -> f
354
355   taskexit( a{ !f } );
356 }
357
358
359 task arrayAssignments( Foo a{ f } ) {
360
361   Foo f[] = new Foo[3];
362
363   Foo g = new Foo();
364   Foo h = new Foo();
365
366   f[1] = g;
367   f[2] = h;
368
369   Foo i = f[0];
370
371   taskexit( a{ !f } );
372 }
373
374
375 public class Zool {
376   public Zool() {}
377
378   public static void FooToBaw( Baw x, Foo y ) {
379     x.f.b = y.b;
380   }
381 }
382
383 task showPrunedEdgesInMethodCall( Foo a{ f } ) {
384
385   Foo b = new Foo();
386   b.x   = a;
387   b.y   = new Foo();
388   b.b   = new Baw();
389
390   Baw c = new Baw();
391   c.f   = new Foo();
392   c.f.b = new Baw();
393   
394   Zool.FooToBaw( c, b );
395
396   taskexit( a{ !f } );
397 }
398
399 task showStringIsImmutable( Foo a{ f } ) {
400
401   Baw x = new Baw();
402   Baw y = new Baw();
403
404   Foo f = new Foo();
405   String s = new String();
406
407   x.f = f;
408   y.f = f;
409
410   x.s = s;
411   y.s = s;
412
413   taskexit( a{ !f } );
414 }
415
416
417 task methodTest( Foo p0{ f } ) {
418
419     Foo up0 = new Foo();
420     Foo up1 = new Foo();
421     Foo up2 = new Foo();
422
423     Foo a0;
424     Foo a1;
425
426     if( false ) {
427         a0    = new Foo();
428         up0.x = a0;     
429         a0.x  = new Foo();
430         //Foo temp = new Foo();
431     }
432
433     if( false ) {
434         a0    = new Foo();
435         a0.x  = new Foo();
436         a1    = a0;
437         up1.x = a0;
438     }
439
440     if( false ) {
441         a1    = new Foo();
442         up2.x = a1;
443     }
444
445     // Foo.test( a0, a1 );
446
447     taskexit( p0{ !f } );
448 }
449
450
451 task getNewFromMethod( Foo p0{ f } ) { 
452   Voo g = Voo.getAVoo( null );
453
454   taskexit( p0{ !f } );
455 }
456
457
458 task methodTest01_( Foo p0{ f }, Foo p1{ f } ) {
459
460   Foo a0before = new Foo();
461   if( false ) {
462     a0before.x = new Foo();
463   } else {
464     a0before.x = new Foo();
465   }
466
467   Foo a0after = new Foo();
468   if( false ) {
469     a0after.x = new Foo();
470   } else {
471     a0after.x = new Foo();
472   }
473
474   Foo.m1_( a0after );
475
476
477   taskexit( p0{ !f }, p1{ !f } );
478 }
479
480
481 task methodTest02_( Foo p0{ f }, Foo p1{ f } ) {
482
483   Foo a0before = new Foo();
484   if( false ) {
485     a0before.x = new Foo();
486   } else {
487     a0before.x = new Foo();
488   }
489
490   Foo a0after = new Foo();
491   if( false ) {
492     a0after.x = new Foo();
493   } else {
494     a0after.x = new Foo();
495   }
496
497   Foo.m2_( a0after );
498
499
500   taskexit( p0{ !f }, p1{ !f } );
501 }
502
503
504 task methodTest03_( Foo p0{ f }, Foo p1{ f } ) {
505
506   Foo a0before = new Foo();
507   if( false ) {
508     a0before.x = new Foo();
509   } else {
510     a0before.x = new Foo();
511   }
512
513   Foo a0after = new Foo();
514   if( false ) {
515     a0after.x = new Foo();
516   } else {
517     a0after.x = new Foo();
518   }
519
520   Foo.m3_( a0after );
521
522
523   taskexit( p0{ !f }, p1{ !f } );
524 }
525
526
527 task methodTest04_( Foo p0{ f }, Foo p1{ f } ) {
528
529   Foo a0before = new Foo();
530   if( false ) {
531     a0before.x = new Foo();
532   } else {
533     a0before.x = new Foo();
534   }
535
536   Foo a0after = new Foo();
537   if( false ) {
538     a0after.x = new Foo();
539   } else {
540     a0after.x = new Foo();
541   }
542
543   Foo.m4_( a0after );
544
545
546   taskexit( p0{ !f }, p1{ !f } );
547 }
548
549
550 task methodTest05_( Foo p0{ f }, Foo p1{ f } ) {
551
552   Foo a0before = new Foo();
553   if( false ) {
554     a0before.x = new Foo();
555   } else {
556     a0before.x = new Foo();
557   }
558
559   Foo a0after = new Foo();
560   if( false ) {
561     a0after.x = new Foo();
562   } else {
563     a0after.x = new Foo();
564   }
565
566   Foo.m5_( a0after );
567
568
569   taskexit( p0{ !f }, p1{ !f } );
570 }
571
572
573 task methodTest06_( Foo p0{ f }, Foo p1{ f } ) {
574
575   Foo a0before = new Foo();
576   if( false ) {
577     a0before.x = new Foo();
578   } else {
579     a0before.x = new Foo();
580   }
581
582   Foo a0after = new Foo();
583   if( false ) {
584     a0after.x = new Foo();
585   } else {
586     a0after.x = new Foo();
587   }
588
589   Foo a1before = new Foo();
590   if( false ) {
591     a1before.x = new Foo();
592   } else {
593     a1before.x = new Foo();
594   }
595
596   Foo a1after = new Foo();
597   if( false ) {
598     a1after.x = new Foo();
599   } else {
600     a1after.x = new Foo();
601   }
602
603   Foo.m6_( a0after, a1after );
604
605
606   taskexit( p0{ !f }, p1{ !f } );
607 }
608
609
610 task methodTest07_( Foo p0{ f }, Foo p1{ f } ) {
611
612   Foo a0before = new Foo();
613   if( false ) {
614     a0before.x = new Foo();
615   } else {
616     a0before.x = new Foo();
617   }
618
619   Foo a0after = new Foo();
620   if( false ) {
621     a0after.x = new Foo();
622   } else {
623     a0after.x = new Foo();
624   }
625
626   Foo a1before = new Foo();
627   if( false ) {
628     a1before.x = new Foo();
629   } else {
630     a1before.x = new Foo();
631   }
632
633   Foo a1after = new Foo();
634   if( false ) {
635     a1after.x = new Foo();
636   } else {
637     a1after.x = new Foo();
638   }
639
640   Foo.m7_( a0after, a1after );
641
642
643   taskexit( p0{ !f }, p1{ !f } );
644 }
645
646
647 task methodTest08_( Foo p0{ f }, Foo p1{ f } ) {
648
649   Foo a0before = new Foo();
650   if( false ) {
651     a0before.x = new Foo();
652   } else {
653     a0before.x = new Foo();
654   }
655
656   Foo a0after = new Foo();
657   if( false ) {
658     a0after.x = new Foo();
659   } else {
660     a0after.x = new Foo();
661   }
662
663   Foo a1before = new Foo();
664   if( false ) {
665     a1before.x = new Foo();
666   } else {
667     a1before.x = new Foo();
668   }
669
670   Foo a1after = new Foo();
671   if( false ) {
672     a1after.x = new Foo();
673   } else {
674     a1after.x = new Foo();
675   }
676
677   Foo.m8_( a0after, a1after );
678
679
680   taskexit( p0{ !f }, p1{ !f } );
681 }