comparison src/tests/gov/nasa/jpf/test/mc/threads/MissedPathTest.java @ 0:61d41facf527

initial v8 import (history reset)
author Peter Mehlitz <Peter.C.Mehlitz@nasa.gov>
date Fri, 23 Jan 2015 10:14:01 -0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:61d41facf527
1 /*
2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
4 * All rights reserved.
5 *
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0.
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package gov.nasa.jpf.test.mc.threads;
19
20 import org.junit.Test;
21
22 import gov.nasa.jpf.util.test.TestJPF;
23 import gov.nasa.jpf.vm.Verify;
24
25 /**
26 * test for missed paths in concurrent threads with very little interaction
27 */
28 public class MissedPathTest extends TestJPF {
29
30 static class X {
31 boolean pass;
32 }
33
34 static class InstanceFieldPropagation extends Thread {
35 X myX; // initially not set
36
37 @Override
38 public void run() {
39 if (myX != null){
40 Verify.println("T: accessed global myX");
41 if (!myX.pass){ // (2) won't fail unless main is between (0) and (1)
42 throw new AssertionError("gotcha");
43 }
44 }
45 }
46 }
47
48 @Test
49 public void testInstanceFieldPropagation () {
50 if (verifyAssertionErrorDetails("gotcha", "+vm.shared.break_on_exposure=true")) {
51 InstanceFieldPropagation mp = new InstanceFieldPropagation();
52 mp.start();
53
54 X x = new X();
55 Verify.println("M: new " + x);
56 mp.myX = x; // (0) exposure - x cannot become shared until this GOT executed
57
58 //Thread.yield(); // this would expose the error
59 Verify.println("M: x.pass=true");
60 x.pass = true; // (1) need to break BEFORE assignment or no error
61 }
62 }
63
64 //----------------------------------------------------------------------------------
65
66 static class Y {
67 X x;
68 }
69
70 static Y globalY; // initially not set
71
72 static class StaticFieldPropagation extends Thread {
73 @Override
74 public void run(){
75 if (globalY != null){
76 if (!globalY.x.pass){ // (2) won't fail unless main is between (0) and (1)
77 throw new AssertionError("gotcha");
78 }
79 }
80 }
81 }
82
83 @Test
84 public void testStaticFieldPropagation () {
85 if (verifyAssertionErrorDetails("gotcha", "+vm.shared.break_on_exposure=true")) {
86 StaticFieldPropagation mp = new StaticFieldPropagation();
87 mp.start();
88
89 X x = new X();
90 Y y = new Y();
91 y.x = x;
92
93 globalY = y; // (0) x not shared until this GOT executed
94
95 //Thread.yield(); // this would expose the error
96 x.pass = true; // (1) need to break BEFORE assignment or no error
97 }
98 }
99
100 //-------------------------------------------------------------------------------
101
102 static class PutContender extends Thread {
103 X myX;
104
105 @Override
106 public void run () {
107 myX = new X(); // competing put with exposure
108
109 if (myX != null) { // doesn't matter, we just want to GET myX
110 Verify.println("T: accessed global myX");
111 }
112 }
113 }
114
115 // this does not really belong here since it doesn't test not missing paths, but
116 // if the exposure CGs we use to avoid missing paths are not causing infinite loops.
117 // NOTE: turning off state matching is crucial here
118 @Test
119 public void testCompetingExposures(){
120 if (verifyNoPropertyViolation("+vm.storage.class=nil")){
121 PutContender mp = new PutContender();
122 mp.start();
123
124 X x = new X();
125 Verify.println("M: new " + x);
126 mp.myX = x; // this is one of the competing PUTs
127
128 Verify.println("M: x.pass=true");
129 x.pass = true; // irrelevant in this case
130 }
131 }
132 }