public final static void appendToClasspath(Set<Path> _classpath, Set<Path> _to_append, boolean _preprocess) { for(Path p: _to_append) appendToClasspath(_classpath, p, _preprocess); }
public void addFiles(String _target_dir, Set<Path> _paths, boolean _overwrite) { for(Path p: _paths) this.addFile(_target_dir, p, _overwrite); }
/** * Returns the size of the original JAR file (before instrumentation). * @see #getInstrumentedFileSize() * @return */ public long getFileSize() { return this.jarWriter.getFileSize(); }
this.jarWriter.addManifestEntry("VULAS-classInstrStats", "[" + this.classCount + " total, " + this.instrControl.countClassesInstrumentedAlready() + " existed, " + this.instrControl.countClassesInstrumentedSuccess() + " ok, " + this.instrControl.countClassesInstrumentedFailure() + " err]"); this.jarWriter.addManifestEntry("VULAS-constructStats", "[" + constructs.size() + " constructs]"); if(JarAnalyzer.getAppContext()!=null) this.jarWriter.addManifestEntry("VULAS-appContext", JarAnalyzer.getAppContext().getMvnGroup() + ":" + JarAnalyzer.getAppContext().getArtifact() + ":" + JarAnalyzer.getAppContext().getVersion()); this.jarWriter.register("^WEB-INF/classes/.*.class$", this); this.jarWriter.register("^WEB-INF/lib/.*.jar$", this); continue; else this.jarWriter.addFile("WEB-INF/lib", l, true); this.jarWriter.addFile("WEB-INF/classes/", Paths.get(this.inclDir.toString(), "vulas-cfg.properties"), true); this.jarWriter.addFile("WEB-INF/classes/", Paths.get(this.inclDir.toString(), "vulas-cfg.xml"), true); this.jarWriter.addFile("WEB-INF/classes/", Paths.get(this.inclDir.toString(), "log4j.properties"), false); this.jarWriter.setClassifier("vulas-instr"); this.jarWriter.rewrite(this.workDir);
for(Path lib: libs) { try { writer = new JarWriter(lib); if(writer.hasEntry("vulas-core.properties") && !writer.isRewrittenByVulas()) { writer.setClassifier("and-config"); writer.register("vulas-core.properties", this); writer.rewrite(this.inclDir);
try { final JarWriter jw = new JarWriter(_to_append); if(jw.hasManifestEntry("Class-Path")) { jw.skipManifestEntry("Class-Path"); jw.setClassifier(jw.getSHA1()); appended_path = jw.rewrite(VulasConfiguration.getGlobal().getTmpDir()); _classpath.add(appended_path);
public String getFileName() { return this.jarWriter.getOriginalJarFileName().toString(); }
if(dir==null) dir = java.nio.file.Files.createTempDirectory("rewritten_jar_"); this.rewrittenFile = Paths.get(dir.toString(), this.getRewriteJarFileName().toString()).toFile(); final JarOutputStream jos = new JarOutputStream(fos, this.createModifiedManifest()); InputStream is = null; byte[] bytes = new byte[1024];
@Override public void analyze(final File _file) throws FileAnalysisException { try { this.jar = new JarFile(_file, false, java.util.zip.ZipFile.OPEN_READ); this.jarWriter = new JarWriter(_file.toPath()); this.url = _file.getAbsolutePath().toString(); } catch (IllegalStateException e) { log.error("IllegalStateException when analyzing file [" + _file + "]: " + e.getMessage()); throw new FileAnalysisException("Error when analyzing file [" + _file + "]: " + e.getMessage(), e); } catch (IOException e) { log.error("IOException when analyzing file [" + _file + "]: " + e.getMessage()); throw new FileAnalysisException("Error when analyzing file [" + _file + "]: " + e.getMessage(), e); } catch (Exception e) { log.error("Exception when analyzing file [" + _file + "]: " + e.getMessage()); throw new FileAnalysisException("Error when analyzing file [" + _file + "]: " + e.getMessage(), e); } }
/** * Returns the size of the instrumented JAR file (or -1 if no instrumentation took place). * @see #getFileSize() * @return */ public long getInstrumentedFileSize() { return this.jarWriter.getInstrumentedFileSize(); }
this.jarWriter.extract(this.tmpDir);
/** * Returns a {@link Library} representing the analyzed Java archive. * @return * @throws FileAnalysisException */ public Library getLibrary() throws FileAnalysisException { final Library lib = new Library(this.getSHA1()); lib.setDigestAlgorithm(DigestAlgorithm.SHA1); lib.setConstructs(this.getSharedConstructs()); lib.setLibraryId(this.libraryId); final Set<Property> p = new HashSet<Property>(); if(this.jarWriter.getOriginalManifest()!=null) { for(Object key: this.jarWriter.getOriginalManifest().getMainAttributes().keySet()) { p.add(new Property(PropertySource.JAVA_MANIFEST, key.toString(), this.jarWriter.getOriginalManifest().getMainAttributes().get(key).toString())); } } lib.setProperties(p); return lib; }
/** * See here: http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html * @throws JarAnalysisException */ protected void createInstrumentedArchive() throws JarAnalysisException { // Additional manifest file entries this.jarWriter.addManifestEntry("VULAS-classInstrStats", "[" + this.classCount + " total, " + this.instrControl.countClassesInstrumentedAlready() + " existed, " + this.instrControl.countClassesInstrumentedSuccess() + " ok, " + this.instrControl.countClassesInstrumentedFailure() + " err]"); this.jarWriter.addManifestEntry("VULAS-constructStats", "[" + constructs.size() + " constructs]"); if(JarAnalyzer.getAppContext()!=null) this.jarWriter.addManifestEntry("VULAS-appContext", JarAnalyzer.getAppContext().getMvnGroup() + ":" + JarAnalyzer.getAppContext().getArtifact() + ":" + JarAnalyzer.getAppContext().getVersion()); // Register this JarAnalyzer for callbacks this.jarWriter.register(".*.class$", this); // Rename if(this.rename) this.jarWriter.setClassifier("vulas-instr"); // Rewrite this.jarWriter.rewrite(this.workDir); // Stats this.instrControl.logStatistics(); }
public String getFileName() { return this.aarWriter.getOriginalJarFileName().toString(); } }
/** * Called during the construction in order to have some classes frozen. * @return */ private final void freezeClasses() { try { final JavaMethodId jmi = JavaId.parseMethodQName("com.sap.Test.test()"); final ConstructUsage cu = new ConstructUsage(jmi, null, -1); final Loader l = new Loader(this.getClass().getClassLoader()); final Configuration cfg = VulasConfiguration.getGlobal().getConfiguration(); ConstructIdUtil.getInstance(); final JarWriter jw = new JarWriter(Paths.get(DynamicTransformer.class.getClassLoader().getResource(DynamicTransformer.class.getName().replace('.', '/') + ".class").toString())); final JarAnalyzer ja = new JarAnalyzer(); ja.analyze(Paths.get(DynamicTransformer.class.getClassLoader().getResource(DynamicTransformer.class.getName().replace('.', '/') + ".class").toString()).toFile()); } // Getting an exception does not matter in the context of freezing some classes catch(Exception e) {;} }
Path appended_path = null; if (exclude_jars.isEmpty()) appended_path = JarWriter.appendToClasspath(this.preparedDepClasspath, p, preprocess); else if (!exclude_jars.contains(p.getFileName().toString(), ComparisonMode.PATTERN, CaseSensitivity.CASE_INSENSITIVE)) appended_path = JarWriter.appendToClasspath(this.preparedDepClasspath, p, preprocess); else log.info("[" + p + "] excluded from reachability analysis"); Path appended_path = null; if (exclude_jars.isEmpty()) appended_path = JarWriter.appendToClasspath(this.preparedAppClasspath, p, preprocess); else if (!exclude_jars.contains(p.getFileName().toString(), ComparisonMode.PATTERN, CaseSensitivity.CASE_INSENSITIVE)) appended_path = JarWriter.appendToClasspath(this.preparedAppClasspath, p, preprocess); else log.info("[" + p + "] excluded from reachability analysis");
public String toString() { final StringBuilder b = new StringBuilder(); final String classname = this.getClass().getName().substring(1 + this.getClass().getName().lastIndexOf(".")); b.append(classname + "[jar/war=").append(this.jarWriter.getOriginalJarFileName()); b.append(", libId=").append( (this.libraryId==null?"false":this.libraryId.toString())); b.append(", instr=").append(this.instrument); b.append(", instrCtx=").append( (JarAnalyzer.getAppContext()==null?"false":JarAnalyzer.getAppContext().toString(false)) ).append("]"); return b.toString(); }
/** * Returns the file name of the to-be-rewritten JAR. * @return */ public Path getRewriteJarFileName() { final Path complete_path = Paths.get(this.originalJar.getName()); Path path = null; // Classifier exists, i.e., change file name if(this.classifier!=null) { final String original_filename = this.getOriginalJarFileName().toString(); String new_filename = null; final int idx = original_filename.lastIndexOf('.'); if(idx!=-1) new_filename = original_filename.substring(0, idx) + "-" + this.classifier + original_filename.substring(idx); else new_filename = original_filename + "-" + this.classifier; path = Paths.get(new_filename); } // Use original file name else { path = complete_path.getFileName(); } return path; }
JarWriter.log.info("Extracted [" + this.getOriginalJarFileName() + "] to [" + to + "]"); return to;