00001 package gov.nasa.arc.ase.jpf.jvm;
00002
00003 import de.fub.bytecode.*;
00004 import edu.ksu.cis.bandera.jjjc.decompiler.*;
00005 import gov.nasa.arc.ase.jpf.*;
00006 import gov.nasa.arc.ase.jpf.jvm.reflection.Reflection;
00007 import gov.nasa.arc.ase.util.Counter;
00008 import gov.nasa.arc.ase.util.Debug;
00009 import gov.nasa.arc.ase.util.Comma;
00010
00011 import gov.nasa.arc.ase.util.HashPool;
00012
00013 import java.io.*;
00014 import java.lang.reflect.InvocationTargetException;
00015 import java.lang.reflect.Method;
00016 import java.util.*;
00017
00018 public class VirtualMachine implements iVirtualMachine {
00019 private SystemState system_state;
00020
00021
00022
00023 private KernelState kernel_state;
00024
00025 private StateStack stack = new StateStack();
00026 private boolean first_max_stack_reached = true;
00027
00028 private String mainClass;
00029 private JVMPath errorPath;
00030 private int pos_in_errorPath;
00031 private Counter matching_states;
00032
00033
00034 public static HashPool fieldsPool = new HashPool();
00035 public static HashPool framePool = new HashPool();
00036 public static HashPool objectInfoPool = new HashPool();
00037 public static HashPool threadPool = new HashPool();
00038
00039
00040
00041 private static boolean gc_needed = false;
00042
00043
00044 public static JVMOptions options;
00045
00046 private static boolean initial_state = true;
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 public VirtualMachine(String classname) {
00058 new JPFVM();
00059 mainClass = classname;
00060 stack.setMaxDepth(VirtualMachine.options.search_depth);
00061 system_state = new SystemState();
00062 kernel_state = new KernelState();
00063
00064
00065
00066
00067
00068
00069 ClassInfo ci = JPFVM.getJPFVM().getClass(classname);
00070 int mainclass = kernel_state.static_area.addClass(ci);
00071
00072 ThreadInfo th = ci.executeMain(kernel_state, mainclass);
00073 ci.initialize(system_state, kernel_state, th);
00074
00075 pushInitialSystemState();
00076 Engine.store.initial(kernel_state);
00077 }
00078 public static void activateGC() { gc_needed = true; }
00079 public boolean Backtrack() {
00080
00081
00082
00083 if (stack.isEmpty()) return false;
00084
00085
00086
00087
00088
00089 SystemState ss = (SystemState)stack.Pop();
00090 if (ss == null) return false;
00091
00092 system_state = (SystemState)ss.clone();
00093
00094
00095 int[] data;
00096
00097
00098
00099
00100
00101
00102 data = system_state.getBacktrackData();
00103 kernel_state.revertTo(data);
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 system_state.restoreAtomicData(kernel_state);
00120 TransitionResult.atomicLevel = system_state.atomicLevel;
00121
00122 return true;
00123 }
00124 public Path buildPath() {
00125 int s = stack.size();
00126
00127
00128
00129
00130
00131
00132
00133 JVMPath p = new JVMPath(s == 0 ? s : s - 1, 0);
00134
00135
00136 for(int index = 1; index < s; index++)
00137 p.set(index-1,
00138 ((SystemState)stack.get(index)).getTrailInfo().getThread());
00139
00140 return p;
00141 }
00142 public boolean CheckInvariant(iProperty p) {
00143 return p.property(kernel_state);
00144 }
00145 public FilenameLinePair ErrorBackward() {
00146 Backtrack();
00147 pos_in_errorPath--;
00148 JVMPathEntry e = ((JVMPath)errorPath).getErrorMove(pos_in_errorPath);
00149 return new FilenameLinePair(e.file, e.line);
00150 }
00151 public FilenameLinePair ErrorForward() {
00152 if (pos_in_errorPath >= errorPath.size())
00153 return null;
00154 JVMPathEntry e = ((JVMPath)errorPath).getErrorMove(pos_in_errorPath);
00155 Scheduler sch = system_state.getScheduler();
00156 sch.setCurrentThread(e.thread);
00157 Forward();
00158 pos_in_errorPath++;
00159 if (pos_in_errorPath >= errorPath.size())
00160 return null;
00161 e = ((JVMPath)errorPath).getErrorMove(pos_in_errorPath);
00162 return new FilenameLinePair(e.file, e.line);
00163 }
00164 public FilenameLinePair ErrorInit() {
00165 ((JPFOptions)Engine.options).po_reduction = false;
00166 reinit(mainClass);
00167 pos_in_errorPath = 0;
00168 JVMPathEntry e = ((JVMPath)errorPath).getErrorMove(pos_in_errorPath);
00169 return new FilenameLinePair(e.file, e.line);
00170 }
00171 public int ErrorPathLength() {
00172 return errorPath.size();
00173 }
00174 public int Forward() {
00175
00176
00177
00178 boolean result;
00179
00180 if (stack.Full(system_state.getScheduler().getSafeMoves())) {
00181 if (first_max_stack_reached) {
00182
00183
00184
00185
00186
00187
00188
00189 first_max_stack_reached = false;
00190 }
00191 return RESULT.EXIT;
00192 }
00193
00194 result = system_state.nextSuccessor(kernel_state);
00195 if (!result) {
00196 if(system_state.handleIntermediate(kernel_state))
00197 return Forward();
00198 if (kernel_state.isTerminated()) {
00199
00200
00201
00202 return RESULT.EXIT;
00203 } else {
00204
00205
00206
00207 return RESULT.END;
00208 }
00209 } else {
00210
00211
00212
00213
00214 if (VirtualMachine.options.garbage_collection && gc_needed) {
00215 kernel_state.gc();
00216 gc_needed = false;
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 pushSystemState();
00228
00229
00230
00231 result = Engine.store.put(kernel_state);
00232 if (result)
00233 return RESULT.NEW;
00234 else
00235 return RESULT.OLD;
00236
00237
00238
00239
00240 }
00241 }
00242 public ErrorTrailInterface getErrorTrail() {
00243 Vector v = new Vector();
00244 v.addElement("");
00245 v.addElement("");
00246 v.addElement("========================");
00247 v.addElement(" *** Path to error: ***");
00248 v.addElement("========================");
00249 int Elements_in_trail = stack.size();
00250 v.addElement("Steps to error: " + (Elements_in_trail-1));
00251 for(int index = 1; index < Elements_in_trail; index++)
00252 try {
00253 v.add(index + ": " + ((SystemState)stack.get(index)).getTrailInfo().toString());
00254 } catch(NullPointerException e) {
00255 v.add(index + ":");
00256 }
00257
00258 ErrorTrail e = new ErrorTrail(v.size());
00259 for(int index = 0; index < v.size(); index++)
00260 e.add((String)v.elementAt(index));
00261
00262 return e;
00263 }
00264
00265
00266
00267
00268
00269 public Path getErrorTrail_from_file() {
00270 JVMPath ePath = null;
00271 try {
00272 String filename = mainClass + ".error.path";
00273 FileReader fr = new FileReader(filename);
00274 LineNumberReader ifr = new LineNumberReader(fr);
00275 String ln = ifr.readLine();
00276 StringTokenizer t;
00277
00278 if(ln !=null) {
00279 t = new StringTokenizer(ln, " :\t\r\n");
00280
00281 t.nextToken();
00282 int size = Integer.parseInt(t.nextToken());
00283 ePath = new JVMPath(size);
00284
00285
00286 for(int index = 0; index < size; index++) {
00287 ln = ifr.readLine();
00288 t = new StringTokenizer(ln, " :\t\r\n");
00289
00290
00291 t.nextToken();
00292 int thread = Integer.parseInt(t.nextToken());
00293
00294
00295 t.nextToken();
00296 int line = Integer.parseInt(t.nextToken());
00297
00298
00299 t.nextToken();
00300 String classname = t.nextToken();
00301
00302
00303 t.nextToken();
00304 int random = Integer.parseInt(t.nextToken());
00305
00306 ePath.setErrorMove(index, line, classname, thread, random);
00307
00308 }
00309 }
00310 fr.close();
00311
00312 } catch (Exception e) {
00313 e.printStackTrace();
00314 }
00315 return ePath;
00316 }
00317
00318
00319
00320
00321
00322
00323 public Path getErrorTrail_to_file() {
00324 int s = stack.size();
00325 int size = (s == 0 ? s : s - 1);
00326
00327
00328 try {
00329 PrintWriter out = new PrintWriter(new OutputStreamWriter(
00330 new FileOutputStream(mainClass + ".error.path")));
00331
00332 out.print("Size:"+ size +"\n");
00333
00334 for(int index = 1; index < s; index++) {
00335 int line = ((SystemState)stack.get(index)).getTrailInfo().getLine();
00336 String classname = ((SystemState)stack.get(index)).getTrailInfo().getClassname();
00337 int thread = ((SystemState)stack.get(index)).getTrailInfo().getThread();
00338 int random = ((SystemState)stack.get(index)).getTrailInfo().getRandom();
00339
00340 out.print("Thread:"+ thread +"\t Line:"+ line +"\t Class:"+ classname +"\t Random:"+random +"\n");
00341 }
00342 out.close();
00343 } catch (Exception e) {
00344
00345 e.printStackTrace();
00346 }
00347 return null;
00348 }
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 public Path getErrorTrail1() {
00381 int s = stack.size();
00382 errorPath = new JVMPath(s == 0 ? s : s - 1);
00383 for(int index = 1; index < s; index++)
00384 errorPath.setErrorMove(index-1,
00385 ((SystemState)stack.get(index)).getTrailInfo().getLine(),
00386 ((SystemState)stack.get(index)).getTrailInfo().getClassname(),
00387 ((SystemState)stack.get(index)).getTrailInfo().getThread(),
00388 ((SystemState)stack.get(index)).getTrailInfo().getRandom()
00389 );
00390
00391
00392
00393 return errorPath;
00394 }
00395 public iKernelState getKernelState(){
00396 return kernel_state;
00397 }
00398 public int getSafeMoves() {
00399 return system_state.getScheduler().getSafeMoves();
00400 }
00401 public static String getStatus() {
00402
00403
00404
00405 return " fsp " + Comma.format(fieldsPool.size()) +
00406 " frp " + Comma.format(framePool.size()) +
00407 " tp " + Comma.format(threadPool.size()) +
00408 " oip " + Comma.format(objectInfoPool.size());
00409
00410 }
00411 public iSystemState getSystemState(){
00412 return system_state;
00413 }
00414 public int nextThreadToExecute() {
00415 if (pos_in_errorPath >= errorPath.size())
00416 return -1;
00417 JVMPathEntry e = ((JVMPath)errorPath).getErrorMove(pos_in_errorPath);
00418 return e.thread;
00419 }
00420 public int previousThreadToExecute() {
00421 if (pos_in_errorPath < 1)
00422 return -1;
00423 JVMPathEntry e = ((JVMPath)errorPath).getErrorMove(pos_in_errorPath-1);
00424 return e.thread;
00425 }
00426 public void printStackTrace() {
00427 ((ThreadInfo)kernel_state.getThreadInfo(system_state.getScheduler().getCurrentThread())).printStackTrace();
00428 }
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687 public void pushInitialSystemState() {
00688
00689
00690
00691 system_state.setBacktrackData(kernel_state.getBacktrackData());
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710 SystemState ss = (SystemState)system_state.clone();
00711 stack.Push(ss);
00712 }
00713 public void pushSystemState() {
00714 Scheduler sch = system_state.getScheduler();
00715 stack.UpdateTop(sch);
00716 sch.initialize();
00717 system_state.atomicLevel = TransitionResult.atomicLevel;
00718 TransitionResult.initialize();
00719 stack.UpdateTopExtra(system_state);
00720
00721
00722
00723 system_state.setBacktrackData(kernel_state.getBacktrackData());
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742 SystemState ss = (SystemState)system_state.clone();
00743 stack.Push(ss);
00744 }
00745 public void reinit(String classname){
00746 new JPFVM();
00747
00748 Runner.race_conditions = 0;
00749
00750 stack = new StateStack();
00751 stack.setMaxDepth(VirtualMachine.options.search_depth);
00752
00753 Repository.clearCache();
00754
00755 StaticMap.reset();
00756
00757
00758 system_state = new SystemState();
00759 kernel_state = new KernelState();
00760
00761 ClassInfo ci = JPFVM.getJPFVM().getClass(classname);
00762 int mainclass = kernel_state.static_area.addClass(ci);
00763
00764 ThreadInfo th = ci.executeMain(kernel_state, mainclass);
00765 ci.initialize(system_state, kernel_state, th);
00766
00767 pushInitialSystemState();
00768 Engine.store.initial(kernel_state);
00769 }
00770 public void ShowErrorTrail() {
00771 BufferedWriter out = null;
00772
00773 Debug.println(Debug.WARNING, "\n");
00774 Debug.println(Debug.WARNING, "========================");
00775 Debug.println(Debug.WARNING, " *** Path to error: ***");
00776 Debug.println(Debug.WARNING, "========================");
00777 int Elements_in_trail = stack.size();
00778 Debug.println(Debug.WARNING, "Steps to error: " + (Elements_in_trail-1));
00779 try{
00780 out = new BufferedWriter(new FileWriter("error.trail"));
00781 } catch (IOException e) {};
00782 for(int index = 1; index < Elements_in_trail; index++) {
00783 Debug.print(Debug.WARNING, index + ": ");
00784 ((SystemState)stack.get(index)).getTrailInfo().print();
00785 ((SystemState)stack.get(index)).getTrailInfo().dump2file(out);
00786 }
00787 try {out.close();} catch (IOException e) {};
00788 }
00789
00790
00791
00792
00793 public void ShowErrorTrail(Path ePath) {
00794 reinit(mainClass);
00795 for(int i = 0; i < errorPath.size()-1; i++) {
00796 FilenameLinePair pair = ErrorForward();
00797 Debug.println(Debug.WARNING, pair.getFilename() + " " +
00798 pair.getLine());
00799 }
00800 }
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860 public int simForward() {
00861 boolean result;
00862
00863 result = system_state.nextSuccessor(kernel_state);
00864 if (!result) {
00865 if (kernel_state.isTerminated()){
00866
00867
00868
00869 return RESULT.EXIT;
00870 }
00871 else{
00872
00873
00874
00875 return RESULT.END;
00876 }
00877 }
00878 else {
00879
00880
00881
00882
00883 if (VirtualMachine.options.garbage_collection && gc_needed) {
00884 kernel_state.gc();
00885 gc_needed = false;
00886 }
00887
00888
00889 if (Runner.race_conditions >= VirtualMachine.options.race_level)
00890 return RESULT.EXIT;
00891
00892 if (Reader.interrupt()){
00893 Debug.println(Debug.ERROR, "EXECUTION HAS BEEN INTERRUPTED !!");
00894 return RESULT.EXIT;
00895 }
00896 return RESULT.NEW;
00897 }
00898 }
00899
00900
00901
00902
00903
00904 public void SimulateErrorTrail(Path ePath) {
00905 JVMPathEntry e;
00906 Scheduler sch;
00907 int s;
00908
00909 int old_line = 0;
00910
00911 reinit(mainClass);
00912 pos_in_errorPath = 0;
00913
00914 while(pos_in_errorPath < ePath.size()) {
00915 e = ((JVMPath)ePath).getErrorMove(pos_in_errorPath);
00916
00917 sch = system_state.getScheduler();
00918 sch.setCurrentThread(e.thread);
00919 sch.setCurrentRandom(e.random);
00920 Forward();
00921 s = stack.size();
00922
00923 int line_sim = ((SystemState)stack.get(s-1)).getTrailInfo().getLine();
00924 int random_sim = ((SystemState)stack.get(s-1)).getTrailInfo().getRandom();
00925
00926 if(e.line != line_sim) {
00927
00928 while(e.line == old_line && pos_in_errorPath < ePath.size()-1) {
00929 pos_in_errorPath++;
00930 e = ((JVMPath)ePath).getErrorMove(pos_in_errorPath);
00931 }
00932
00933 if(e.line != line_sim) {
00934 Debug.println(Debug.ERROR, "!!!!! Warning: mismatch line numbers: program "+ line_sim +" ; error trail "+e.line);
00935 Debug.println(Debug.ERROR, "! This may not be a real counter-example !");
00936
00937 pos_in_errorPath = ePath.size();
00938 }
00939 }
00940
00941 if(e.random != random_sim)
00942 Debug.println(Debug.ERROR, "!!!!! Warning: mismatch random numbers: program "+ random_sim +" ; error trail "+e.random);
00943
00944 old_line = e.line;
00945 pos_in_errorPath++;
00946 }
00947 }
00948 }