2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
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
10 * http://www.apache.org/licenses/LICENSE-2.0.
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.
19 package gov.nasa.jpf.test.mc.basic;
22 import gov.nasa.jpf.ListenerAdapter;
23 import gov.nasa.jpf.jvm.bytecode.EXECUTENATIVE;
24 import gov.nasa.jpf.jvm.bytecode.GETFIELD;
25 import gov.nasa.jpf.search.Search;
26 import gov.nasa.jpf.util.test.TestJPF;
27 import gov.nasa.jpf.vm.ChoiceGenerator;
28 import gov.nasa.jpf.vm.FieldInfo;
29 import gov.nasa.jpf.vm.Instruction;
30 import gov.nasa.jpf.vm.VM;
31 import gov.nasa.jpf.vm.StackFrame;
32 import gov.nasa.jpf.vm.SystemState;
33 import gov.nasa.jpf.vm.ThreadInfo;
34 import gov.nasa.jpf.vm.Verify;
35 import gov.nasa.jpf.vm.choice.IntChoiceFromSet;
36 import gov.nasa.jpf.vm.choice.IntIntervalGenerator;
38 import org.junit.Test;
41 * regression test for cascaded ChoiceGenerators
43 public class CascadedCGTest extends TestJPF {
45 public static class IntChoiceCascader extends ListenerAdapter {
49 public void instructionExecuted(VM vm, ThreadInfo ti, Instruction nextInsn, Instruction executedInsn) {
50 SystemState ss = vm.getSystemState();
52 if (executedInsn instanceof EXECUTENATIVE) { // break on native method exec
53 EXECUTENATIVE exec = (EXECUTENATIVE) executedInsn;
55 if (exec.getExecutedMethodName().equals("getInt")){// this insn did create a CG
56 if (!ti.isFirstStepInsn()){
59 IntIntervalGenerator cg = new IntIntervalGenerator("listenerCG", 3,4);
60 ss.setNextChoiceGenerator(cg);
61 System.out.println("# listener registered " + cg);
63 } else { // reexecution
65 ChoiceGenerator<?>[] curCGs = ss.getCurrentChoiceGenerators();
66 assert curCGs.length == 2;
68 IntIntervalGenerator cg = ss.getCurrentChoiceGenerator("listenerCG", IntIntervalGenerator.class);
69 assert cg != null : "no 'listenerCG' IntIntervalGenerator found";
70 int i = cg.getNextChoice();
71 System.out.println("# current listener CG choice: " + i);
73 cg = ss.getCurrentChoiceGenerator("verifyGetInt(II)", IntIntervalGenerator.class);
74 assert cg != null : "no 'verifyGetInt(II)' IntIntervalGenerator found";
75 int j = cg.getNextChoice();
76 System.out.println("# current insn CG choice: " + j);
86 public void testCascadedIntIntervals () {
87 if (verifyNoPropertyViolation("+listener=.test.mc.basic.CascadedCGTest$IntChoiceCascader")){
88 int i = Verify.getInt( 1, 2);
89 System.out.print("i=");
90 System.out.println(i);
92 assert IntChoiceCascader.result == 21;
97 //--- mixed data and thread CG
99 // this listener replaces all GETFIELD "mySharedField" results with configured
100 // choice values (i.e. it is a simplified field Perturbator).
101 // The demo point is that it is not aware of that such GETFIELDs might also be
102 // scheduling points because of shared object field access, and it should work
103 // the same no matter if there also was a ThreadChoice/context switch or not
105 // NOTE: while the cascaded CG interface is easy to use (almost the same as the
106 // single CG interface), the context can be quite tricky because the cascaded
107 // CG (the scheduling point in this case) means the corresponding instruction
108 // is already rescheduled and might have been cut short in insn specific ways
109 // (in this case before pushing the field value on the operand stack). For this
110 // reason a simple ti.isFirstStepInsn() check is not sufficient. There might not
111 // have been a reschedule if there was only one thread, or even if this is the
112 // first step insn, the corresponding CG might have been not related to the
113 // getfield but some action in the preceeding thread (e.g. a terminate).
114 // In this case, the simple solution is based on that we want the data CG
115 // unconditionally, so we check if there is a corresponding current CG
116 // (which means this is not the first step insn)
118 public static class FieldAccessCascader extends ListenerAdapter {
121 public void instructionExecuted(VM vm, ThreadInfo ti, Instruction nextInsn, Instruction executedInsn) {
122 SystemState ss = vm.getSystemState();
124 if (executedInsn instanceof GETFIELD){
125 GETFIELD getInsn = (GETFIELD) executedInsn;
126 FieldInfo fi = getInsn.getFieldInfo();
127 if (fi.getName().equals("mySharedField")){
129 IntChoiceFromSet cg = ss.getCurrentChoiceGenerator("fieldReplace", IntChoiceFromSet.class);
132 // we might get here after a preceding rescheduling exec, i.e.
133 // partial execution (with successive re-execution), or after
134 // non-rescheduling exec has been completed (only one runnable thread).
135 // In the first case we have to restore the operand stack so that
137 if (!ti.willReExecuteInstruction()){
138 // restore old operand stack contents
139 StackFrame frame = ti.getModifiableTopFrame();
142 frame.pushRef( getInsn.getLastThis());
145 cg = new IntChoiceFromSet("fieldReplace", 42, 43);
146 ss.setNextChoiceGenerator(cg);
147 ti.reExecuteInstruction();
149 System.out.println("# listener registered CG: " + cg);
152 StackFrame frame = ti.getModifiableTopFrame();
154 int v = cg.getNextChoice();
158 System.out.println("# listener replacing " + n + " with " + v);
164 //--- those are just for debugging purposes
166 public void stateBacktracked(Search search) {
167 System.out.println("#------ [" + search.getDepth() + "] backtrack: " + search.getStateId());
171 public void stateAdvanced(Search search){
172 System.out.println("#------ " + search.getStateId() + " isNew: " + search.isNewState() + ", isEnd: " + search.isEndState());
176 public void threadScheduled(VM vm, ThreadInfo ti){
177 System.out.println("# running thread: " + ti);
181 public void threadTerminated(VM vm, ThreadInfo ti){
182 System.out.println("# terminated thread: " + ti);
186 public void threadStarted(VM vm, ThreadInfo ti){
187 System.out.println("# started thread: " + ti);
191 public void choiceGeneratorAdvanced (VM vm, ChoiceGenerator<?> currentCG) {
192 System.out.println("# choice: " + currentCG);
196 int mySharedField = -1;
199 public void testMixedThreadDataCGs () {
200 if (verifyNoPropertyViolation("+listener=.test.mc.basic.CascadedCGTest$FieldAccessCascader")){
201 Thread t = new Thread(){
204 int n = mySharedField;
205 System.out.print("<thread> mySharedField read: ");
206 System.out.println( n);
207 assert n == 42 || n == 43; // regardless of main thread exec state
213 System.out.println("<main> mySharedField write: 7");