Skip to content

Commit 26c7a5b

Browse files
authored
Merge pull request diffblue#357 from diffblue/python_driver__improved_traiting_of_models_lib_and_standard_lib
Python driver improved traiting of models lib and Java standard lib
2 parents 924be12 + 650c9b9 commit 26c7a5b

File tree

3 files changed

+66
-47
lines changed

3 files changed

+66
-47
lines changed

benchmarks/LIBRARIES/README.txt

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,44 +24,37 @@ of the sub-libraries:
2424
1. Install "Java OpenJDK 8" by using the standard package management system of
2525
Ubuntu. (e.g. openjdk-8-jdk-headless)
2626
2. Create a directory "openjdk-8" into this directory.
27-
3. Locate the file "rt.jar" of "Java OpenJDK 8" on the disc. It typically
28-
resides at location:
29-
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar
30-
4. Copy the found "rt.jar" file into the created directory "openjdk-8".
31-
Optional: Instead of copying the "rt.jar", you can alternatively use "jar"
27+
3. Locate the files "rt.jar" and "jce.jar" of "Java OpenJDK 8" on the disc. They
28+
typically reside in the same directory:
29+
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib
30+
4. Copy the found files into the created directory "openjdk-8".
31+
Optional: Instead of copying the files, you can alternatively use "jar"
3232
utility with options "xvf" specified to unpack the content of
33-
the "rt.jar" file into the created directory "openjdk-8". This
33+
the files into the created directory "openjdk-8". This
3434
allows you to later remove/adopt/substitute some desired parts
3535
of the library.
36+
5. (Optional) Download also source code of the Package (not used by the tool; but might be
37+
useful for degugging purposes):
38+
http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/archive/tip.zip/src/share/classes
39+
NOTE: the link above was taken from the shell script of the models library:
40+
<security-scanner-root>/benchmarks/LIBRARIES/models/model/get-original-jdk.sh [line 10]
3641

37-
Where to find JAR with javax.crypto.*:
38-
- This is not in OpenJDK. It is only in the Oracle's JDK, see:
39-
https://stackoverflow.com/questions/14935447/why-am-i-getting-package-javax-crypto-does-not-exist
40-
- The package can be downloaded from:
41-
http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java-plat-419418.html#JCE-1_2_2-G-JS
42+
Alternative installation - use of Oracle's JDK:
4243

43-
44-
Alternative installation:
45-
46-
1. Download OpenJDK 8 .deb package, e.g. from:
47-
http://ftp.uk.debian.org/debian/pool/main/o/openjdk-8/openjdk-8-jre-headless_8u121-b13-1~bpo8+1_amd64.deb
48-
2. Extract its content into some directory.
49-
3. Find the "rt.jar" file in the unpacked directory. It typically is in:
50-
<your-unpack-dir>/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar
51-
2. Create a directory "openjdk-8" into this directory (of this README.txt file).
52-
4. Copy the found "rt.jar" file into the created directory "openjdk-8".
53-
54-
55-
IMPORTANT NOTE: In order to analyse Sakai, we need to use Oracle's JDK 8,
56-
because these packages are missing in the OpenJDK:
57-
javax.crypto.*
58-
sun.invoke.*
59-
com.sum.jmx.*
60-
You can download Oracle's JDK here:
44+
1. Download Oracle's JDK here:
6145
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
62-
Then, you need to take these JARs from the directory 'jre/lib':
63-
rt.jar
64-
jce.jar
46+
2. Create a directory "oracle-jdk-8" into this directory.
47+
3. Unpack JAR files "rt.jar" and "jce.jar" from the downloaded package into the
48+
created directory. Both JAR should be located in this directory in the package:
49+
jdk1.8.0_161/lib
50+
51+
52+
NOTE: You can install both versions of JDK. In that case, the driver script
53+
<security-scanner-root>/driver/run.py
54+
will first check for presence of OpenJDK version. If NOT present, then
55+
Oracle's version is checked for presence. Note though that the option
56+
--use-java-runtime-library must be passed to the script in order to
57+
enable the checks described above.
6558

6659

6760
(2) Apache Tomcat 9

driver/mkbench.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,18 @@ def collect_java_binaries(app_path, list_of_classpaths, entry_point, temp_dir, o
9191
java_binaries = CollectedJavaBinaries()
9292
_find_java_binaries(app_path, java_binaries)
9393

94+
library_directories = []
9495
java_libraries = CollectedJavaBinaries()
9596
for path in list_of_classpaths:
9697
if os.path.isdir(path):
9798
_find_java_binaries(path, java_libraries)
99+
library_directories.append(path)
100+
else:
101+
java_libraries.classpath_jar_files.append(path)
98102

99103
prof["num_classes"] = len(java_binaries.class_files)
100-
prof["num_classpath_jar_files"] = len(java_binaries.classpath_jar_files)
104+
prof["num_classpath_jar_files"] = len(java_binaries.classpath_jar_files) + len(java_libraries.classpath_jar_files)
105+
prof["num_classpath_directories"] = len(library_directories)
101106

102107
# First we read packages of all collected class files.
103108
classes_info, java_class_info_call_duration = _read_info_of_class_files(
@@ -138,11 +143,13 @@ def collect_java_binaries(app_path, list_of_classpaths, entry_point, temp_dir, o
138143
if not os.path.isdir(os.path.dirname(output_json)):
139144
os.makedirs(os.path.dirname(output_json))
140145
with open(output_json, "w") as ofile:
141-
ofile.write(json.dumps({
146+
program_json = {
142147
"jar": java_binaries.jar_file,
143-
"classpath": [p for p in java_binaries.classpath_jar_files + java_libraries.classpath_jar_files + list_of_classpaths if os.path.exists(p)],
144-
"entry-point": entry_point
145-
}, sort_keys=True, indent=4))
148+
"classpath": [p for p in java_binaries.classpath_jar_files + java_libraries.classpath_jar_files + library_directories if os.path.exists(p)],
149+
}
150+
if entry_point is not None:
151+
program_json["entry-point"] = entry_point
152+
ofile.write(json.dumps(program_json, sort_keys=True, indent=4))
146153

147154
# Lastly, we complete and return statistics from this stage.
148155
prof["duration"] = time.time() - prof_start_time

driver/run.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,26 @@ def get_diffblue_models_library_props():
2424
props["diffblue_models_library"]["paths"].append(pathname)
2525
else:
2626
props["diffblue_models_library"]["error"] = "Cannot access Diffblue Models Library's file " + pathname
27-
return props
28-
pathname = os.path.join(get_benchmark_library_dir(), "oracle-jdk-8")
29-
if os.path.isdir(pathname):
30-
props["diffblue_models_library"]["paths"].append(pathname)
31-
else:
32-
pathname = os.path.join(get_benchmark_library_dir(), "openjdk-8")
33-
if os.path.isdir(pathname):
34-
props["diffblue_models_library"]["paths"].append(pathname)
35-
else:
36-
print("Warning: cannot access JDK install directory " + pathname)
27+
return props
28+
29+
def get_java_runtime_library():
30+
props = {"java_runtime_library": {"paths": [], "error": None}}
31+
pathname = os.path.join(get_benchmark_library_dir(), "openjdk-8")
32+
if not os.path.isdir(pathname):
33+
pathname = os.path.join(get_benchmark_library_dir(), "oracle-jdk-8")
34+
if not os.path.isdir(pathname):
35+
props["java_runtime_library"]["error"] = (
36+
"Cannot find Java standard library on neither of the paths: " +
37+
os.path.join(get_benchmark_library_dir(), "openjdk-8") + ", " + pathname
38+
)
39+
return props
40+
jars_selection = ["rt.jar", "jce.jar"]
41+
for jar in jars_selection:
42+
jar_full_path = os.path.join(pathname, jar)
43+
if os.path.isfile(jar_full_path):
44+
props["java_runtime_library"]["paths"].append(jar_full_path)
45+
if os.path.join(pathname, "rt.jar") not in props["java_runtime_library"]["paths"]:
46+
props["java_runtime_library"]["error"] = "Cannot find 'rt.jar' Java standard library package in the directory " + pathname
3747
return props
3848

3949
def get_apache_tomcat_props():
@@ -58,6 +68,7 @@ def get_spring_framework_props():
5868

5969
result = {}
6070
result.update(get_diffblue_models_library_props())
71+
result.update(get_java_runtime_library())
6172
result.update(get_apache_tomcat_props())
6273
result.update(get_spring_framework_props())
6374
return result
@@ -119,6 +130,10 @@ def __parse_cmd_line():
119130
"during the analysis.")
120131
parser.add_argument("--use-models-library", action="store_true",
121132
help="Add the Diffblue Models Library's JAR file to the classpath of the security-scanner.")
133+
parser.add_argument("--use-java-runtime-library", action="store_true",
134+
help="Add the Java standard library to the classpath. First, there will be attempt to add "
135+
"OpenJDK version of the library. If it is not found (e.g. not installed), then the "
136+
"Oracle's version of the library is searched for.")
122137
parser.add_argument("--use-apache-tomcat", action="store_true",
123138
help="Add the Apache Tomcat's JAR files to the classpath of the security-scanner.")
124139
parser.add_argument("--use-spring-framework", action="store_true",
@@ -161,6 +176,7 @@ def evaluate(cmdline, common_libraries):
161176
cmdline.input_path,
162177
cmdline.libraries
163178
+ (common_libraries["diffblue_models_library"]["paths"] if cmdline.use_models_library else [])
179+
+ (common_libraries["java_runtime_library"]["paths"] if cmdline.use_java_runtime_library else [])
164180
+ (common_libraries["apache_tomcat"]["paths"] if cmdline.use_apache_tomcat else [])
165181
+ (common_libraries["spring_framework"]["paths"] if cmdline.use_spring_framework else []),
166182
cmdline.entry_point,
@@ -313,6 +329,9 @@ def __main():
313329
if cmdline.use_models_library and common_libraries["diffblue_models_library"]["error"] is not None:
314330
print("ERROR: " + common_libraries["diffblue_models_library"]["error"])
315331
return
332+
if cmdline.use_java_runtime_library and common_libraries["java_runtime_library"]["error"] is not None:
333+
print("ERROR: " + common_libraries["java_runtime_library"]["error"])
334+
return
316335
if cmdline.use_apache_tomcat and common_libraries["apache_tomcat"]["error"] is not None:
317336
print("ERROR: " + common_libraries["apache_tomcat"]["error"])
318337
return

0 commit comments

Comments
 (0)