Change in the Conflict Tracker analysis
[jpf-core.git] / src / main / gov / nasa / jpf / jvm / JVMSystemClassLoaderInfo.java
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.jvm;
19
20 import gov.nasa.jpf.Config;
21 import gov.nasa.jpf.JPF;
22 import gov.nasa.jpf.util.JPFLogger;
23 import gov.nasa.jpf.vm.ClassFileContainer;
24 import gov.nasa.jpf.vm.ClassInfo;
25 import gov.nasa.jpf.vm.ClassLoaderInfo;
26 import gov.nasa.jpf.vm.ClassParseException;
27 import gov.nasa.jpf.vm.MethodInfo;
28 import gov.nasa.jpf.vm.SystemClassLoaderInfo;
29 import gov.nasa.jpf.vm.VM;
30 import java.io.File;
31 import java.io.IOException;
32
33 /**
34  * a SystemClassLoaderInfo that reads standard Java classfiles from *.class and *.jar files, and creates code using a concrete
35  * value JVM instruction set
36  */
37 public class JVMSystemClassLoaderInfo extends SystemClassLoaderInfo {
38
39   static JPFLogger log = JPF.getLogger("class");
40   protected JVMCodeBuilder defaultCodeBuilder;
41
42   public JVMSystemClassLoaderInfo (VM vm, int appId) {
43     super(vm, appId);
44
45     defaultCodeBuilder = createDefaultCodeBuilder(config, appId);
46
47     JVMClassInfo.init(config);
48
49     // now we can notify
50     vm.registerClassLoader(this);
51   }
52
53   /**
54    * override this if you need a different default CodeBuilder
55    */
56   protected JVMCodeBuilder createDefaultCodeBuilder (Config config, int appId) {
57     String key = config.getIndexableKey("jvm.insn_factory.class", appId);
58     JVMInstructionFactory insnFactory = config.getEssentialInstance(key, JVMInstructionFactory.class);
59     return new JVMCodeBuilder(insnFactory);
60   }
61
62   @Override
63   protected ClassFileContainer createClassFileContainer (String spec) {
64     int i = spec.indexOf(".jar");
65     if (i > 0) {
66       // its a jar
67       int j = i + 4;
68       int len = spec.length();
69       String jarPath;
70       String pathPrefix = null;
71       File jarFile;
72       if (j == len) {
73         // no path prefix, plain jar
74         jarPath = spec;
75       } else {
76         if (spec.charAt(j) == '/') {
77           pathPrefix = spec.substring(j);
78           jarPath = spec.substring(0, j);
79         } else {
80           return null;
81         }
82       }
83       jarFile = new File(jarPath);
84       if (jarFile.isFile()) {
85         try {
86           return new JarClassFileContainer(jarFile, pathPrefix);
87         } catch (IOException ix) {
88           return null;
89         }
90       } else {
91         return null;
92       }
93
94     } else {
95       // a dir
96       File dir = new File(spec);
97       if (dir.isDirectory()) {
98         return new DirClassFileContainer(dir);
99       } else {
100         return null;
101       }
102     }
103   }
104
105   protected void addSystemBootClassPath () {
106     String v = System.getProperty("sun.boot.class.path");
107     if (v != null) {
108       for (String pn : v.split(File.pathSeparator)) {
109         if (pn != null && !pn.isEmpty()) {
110           ClassFileContainer cfc = createClassFileContainer(pn);
111           if (cfc != null) {
112             cp.addClassFileContainer(cfc);
113           }
114         }
115       }
116     } else {
117       // Hmm, maybe we are not executing on OpenJDK
118     }
119   }
120
121   /**
122    * this is the main method to create the ClassPath, which is called from the ctor
123    */
124   @Override
125   protected void initializeSystemClassPath (VM vm, int appId) {
126     Config conf = vm.getConfig();
127     File[] pathElements;
128
129     // explicit "classpath[.id]" settings have precedence
130     pathElements = getPathElements(conf, "classpath", appId);
131     if (pathElements != null) {
132       for (File f : pathElements) {
133         addClassPathElement(f.getAbsolutePath());
134       }
135     }
136
137     // we optionally append boot_classpath
138     pathElements = getPathElements(conf, "vm.boot_classpath", appId);
139     if (pathElements != null) {
140       for (File f : pathElements) {
141         if (f.getName().equals("<system>")) {
142           addSystemBootClassPath();
143         } else {
144           addClassPathElement( f.getAbsolutePath());
145         }
146       }
147     }
148
149     log.info("collected system classpath: ", cp);
150   }
151
152   /**
153    * override this if you have different CodeBuilders for different types
154    * NOTE - this CodeBuilder is not completely initialized yet, clients still have to call startMethod(mi) on it
155    */
156   protected JVMCodeBuilder getCodeBuilder (String clsName) {
157     return defaultCodeBuilder;
158   }
159
160   /**
161    * used for automatically created code such as AnnotationProxies, direct calls, native calls and run starts
162    * NOTE - this cannot be called recursively or concurrently
163    */
164   protected JVMCodeBuilder getSystemCodeBuilder (ClassFile cf, MethodInfo mi) {
165     defaultCodeBuilder.reset(cf, mi);
166     return defaultCodeBuilder;
167   }
168   
169   @Override
170   protected ClassInfo createClassInfo (String clsName, String url, byte[] data, ClassLoaderInfo definingLoader) throws ClassParseException {
171     ClassFile cf = new ClassFile(data);
172     JVMCodeBuilder cb = getCodeBuilder(clsName);
173     ClassInfo ci = new JVMClassInfo(clsName, definingLoader, cf, url, cb);
174     setAttributes(ci);
175     
176     return ci;
177   }
178 }