00001 package gov.nasa.arc.ase.jpf.jvm;
00002
00003 import gov.nasa.arc.ase.jpf.Engine;
00004 import gov.nasa.arc.ase.jpf.InternalErrorException;
00005 import gov.nasa.arc.ase.jpf.iScheduler;
00006 import gov.nasa.arc.ase.jpf.iSystemState;
00007 import gov.nasa.arc.ase.jpf.iThreadInfo;
00008 import gov.nasa.arc.ase.jpf.iTrailInfo;
00009 import gov.nasa.arc.ase.jpf.JPFErrorException;
00010 import gov.nasa.arc.ase.util.Debug;
00011 import gov.nasa.arc.ase.util.HashData;
00012 import java.util.Stack;
00013
00014 public class SystemState implements iSystemState, Storable, State {
00015
00016
00017
00018 public Scheduler sch;
00019
00020
00021
00022
00023 public KernelState ks;
00024
00025
00026
00027
00028 public TrailInfo ti;
00029
00030
00031
00032
00033 public VirtualMachine vm;
00034
00035
00036
00037
00038 public boolean violatedAssertion;
00039
00040
00041
00042
00043 public boolean uncaughtException;
00044
00045
00046
00047
00048 public String exceptionName;
00049
00050
00051
00052
00053
00054
00055 public boolean GCNeeded = false;
00056
00057 private SystemState(SystemState ss) {
00058 sch = (Scheduler)ss.sch.clone();
00059 sch.ss = this;
00060 ks = (KernelState)ss.ks.clone();
00061 ks.ss = this;
00062 ti = null;
00063 vm = null;
00064 violatedAssertion = ss.violatedAssertion;
00065 uncaughtException = ss.uncaughtException;
00066 exceptionName = ss.exceptionName;
00067
00068 GCNeeded = ss.GCNeeded;
00069
00070 }
00071
00072
00073
00074
00075
00076 public SystemState(VirtualMachine vm) {
00077 this.vm = vm;
00078
00079 ks = new KernelState(this);
00080
00081 newScheduler();
00082
00083 ti = null;
00084
00085
00086
00087
00088
00089
00090 }
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 public void activateGC() {
00125 GCNeeded = true;
00126 }
00127 public void backtrackTo(ArrayOffset storing, Object backtrack) {
00128 sch = (Scheduler)backtrack;
00129 }
00130 public Object clone() {
00131 return new SystemState(this);
00132 }
00133 public Object getBacktrackData() {
00134 return sch.clone();
00135 }
00136 public Reference getClass(String name) {
00137 if(ks.sa.containsClass(name))
00138 return ks.sa.get(name);
00139
00140 return null;
00141 }
00142 public Object getData() {
00143 return new ArrayWrapper(ks.getStoringData());
00144 }
00145 public int getDataSize(Object o) {
00146 return ((ArrayWrapper)o).size();
00147 }
00148 public Reference getObject(int reference) {
00149 return ks.da.get(reference);
00150 }
00151 public ThreadInfo getRunningThread() {
00152 if(sch.getThread() >= ks.tl.length()) return null;
00153
00154 return ks.tl.get(sch.getThread());
00155 }
00156 public iScheduler getScheduler() {
00157 return sch;
00158 }
00159
00160
00161 public int[] getStoringData() {
00162 return null;
00163 }
00164 public Thread getThread(int index) {
00165 return ks.tl.get(index);
00166 }
00167 public Thread getThread(Reference reference) {
00168 return getThread(((ElementInfo)reference).index);
00169 }
00170 public int getThreadCount() {
00171 return ks.tl.length();
00172 }
00173 public iThreadInfo getThreadInfo(int idx) {
00174 return ks.tl.get(idx);
00175 }
00176 public iTrailInfo getTrailInfo() {
00177 return ti;
00178 }
00179 public void hash(HashData hd) {
00180 ks.hash(hd);
00181 }
00182 public int hashCode() {
00183 HashData hd = new HashData();
00184
00185 hash(hd);
00186
00187 return hd.getValue();
00188 }
00189
00190
00191
00192
00193 public void initializeClass(ClassInfo ci) {
00194 ThreadInfo th = getRunningThread();
00195 if(th == null) return;
00196
00197 MethodInfo mi = ci.getStaticMethod("<clinit>()V");
00198 if(mi == null) return;
00199
00200 th.executeStaticMethod(mi);
00201 }
00202 public void newScheduler() {
00203 if(Engine.options.interactive)
00204 sch = new InteractiveScheduler(this);
00205 else if(VirtualMachine.options.path_simulation)
00206 sch = new PathScheduler(this, (JVMPath)vm.getErrorTrail_from_file());
00207 else if (VirtualMachine.options.verify)
00208 if (VirtualMachine.options.po_reduction)
00209 sch = new POScheduler(this);
00210 else if (VirtualMachine.options.random_order)
00211 sch = new RandomOrderScheduler(this);
00212 else
00213 sch = new DefaultScheduler(this);
00214 else
00215 sch = new RandomScheduler(this);
00216 }
00217
00218
00219
00220 public boolean nextSuccessor() {
00221 try {
00222
00223
00224
00225 ThreadInfo th;
00226
00227 violatedAssertion = false;
00228 uncaughtException = false;
00229 exceptionName = null;
00230 th = (ThreadInfo)sch.locateThread(this);
00231
00232 if(ks.isIntermediate()) {
00233 int t = ks.getIntermediateThread();
00234
00235 if (th != null)
00236 while(th != null && th.index != t) {
00237 sch.next();
00238 th = (ThreadInfo)sch.locateThread(this);
00239 }
00240 }
00241
00242
00243 if (th == null) return false;
00244
00245
00246
00247 try {
00248 if(th.executeStep()) {
00249 ks.clearIntermediate();
00250 } else {
00251 ks.setIntermediate();
00252 }
00253 } catch(UncaughtException e) {
00254 uncaughtException = true;
00255 exceptionName = e.getMessage();
00256 }
00257
00258 return true;
00259 } catch(JPFErrorException e) {
00260
00261 throw e;
00262 } catch(Exception e) {
00263 ks.jvmError(e, getRunningThread());
00264 }
00265
00266 return false;
00267 }
00268 public int random(int max) {
00269 return sch.random(max);
00270 }
00271 }