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