-
Notifications
You must be signed in to change notification settings - Fork 875
/
ApkDashboard.java
212 lines (162 loc) · 6.88 KB
/
ApkDashboard.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright 2016 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.classyshark.silverghost.translator.apk.dashboard;
import com.google.classyshark.silverghost.contentreader.dex.DexlibLoader;
import com.google.classyshark.silverghost.translator.apk.dashboard.manifest.ManifestInspector;
import com.google.classyshark.silverghost.translator.java.dex.MultidexReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.jf.dexlib2.dexbacked.DexBackedDexFile;
import org.jf.dexlib2.iface.DexFile;
import org.ow2.asmdex.ApplicationReader;
import org.ow2.asmdex.ApplicationVisitor;
import org.ow2.asmdex.Opcodes;
public class ApkDashboard {
public ArrayList<ClassesDexDataEntry> classesDexEntries = new ArrayList<>();
public ArrayList<ClassesDexDataEntry> customClassesDexEntries = new ArrayList<>();
public List<String> nativeLibs = new ArrayList<>();
public ArrayList<String> nativeDependencies = new ArrayList<>();
public List<String> nativeErrors = new ArrayList<>();
public List<String> allClasses = new ArrayList<>();
public File apkFile;
public static final String[] NEW_LINE = {" ", " "};
public ApkDashboard(File apkFile) {
this.apkFile = apkFile;
}
public void inspect() {
// TODO add exception for not calling inspect
MultidexReader.fillApkDashboard(apkFile, this);
}
public List<ClassesDexDataEntry> getAllDexEntries() {
ArrayList<ClassesDexDataEntry> result = new ArrayList<>();
result.addAll(classesDexEntries);
result.addAll(customClassesDexEntries);
return result;
}
public List<String> getFullPathNativeLibNamesSorted() {
Collections.sort(nativeLibs);
return nativeLibs;
}
public List<String> getNativeLibNamesSorted() {
Set<String> uniqueDependencies = new LinkedHashSet<>(nativeDependencies);
LinkedList<String> sortedNativeDependencies = new LinkedList<>(uniqueDependencies);
Collections.sort(sortedNativeDependencies);
List<String> nativeLibNames =
extractLibNamesFromFullPaths(getFullPathNativeLibNamesSorted());
return nativeLibNames;
}
public String getPrivateLibErrorTag(String nativeLib) {
boolean isPrivate = PrivateNativeLibsInspector.isPrivate(nativeLib, getNativeLibNamesSorted());
if (isPrivate) {
return " -- private api!";
} else {
return "";
}
}
public static Set<String> getClassesWithNativeMethodsPerDexIndex(int dexIndex,
File classesDex) {
ClassesDexDataEntry dexInspectionsData =
ApkDashboard.fillAnalysisPerClassesDexIndex(dexIndex, classesDex);
return dexInspectionsData.classesWithNativeMethods;
}
public List<String> getJavaDependenciesErrors() {
JavaDependenciesInspector ddi = new JavaDependenciesInspector(allClasses);
List<String> result = ddi.getInspections();
return result;
}
public static ClassesDexDataEntry fillAnalysisPerClassesDexIndex(int dexIndex, File classesDex) {
ClassesDexDataEntry dexData = new ClassesDexDataEntry(dexIndex);
try {
InputStream is = new FileInputStream(classesDex);
ApplicationVisitor av = new ApkNativeMethodsVisitor(dexData);
ApplicationReader ar = new ApplicationReader(Opcodes.ASM4, is);
ar.accept(av, 0);
} catch (Exception e) {
e.printStackTrace();
}
try {
DexFile dxFile = DexlibLoader.loadDexFile(classesDex);
DexBackedDexFile dataPack = (DexBackedDexFile) dxFile;
dexData.allMethods = dataPack.getMethodCount();
//dexData.syntheticAccessors =
// new SyntheticAccessorsInspector(dxFile).getSyntheticAccessors();
} catch (Exception e) {
e.printStackTrace();
System.out.println("here " + e);
}
return dexData;
}
private static List<String> extractLibNamesFromFullPaths(List<String> nativeLibs) {
// libs/x86/lib.so\n ==> libs.so
LinkedList<String> result = new LinkedList<>();
for (String nativeLib : nativeLibs) {
String simpleNativeLibName = nativeLib;
if (nativeLib.contains("/")) {
simpleNativeLibName = simpleNativeLibName.substring(simpleNativeLibName.lastIndexOf("/") + 1,
simpleNativeLibName.length() - 1);
}
result.add(simpleNativeLibName);
}
return result;
}
public List<String> getManifestRecommendations() {
ManifestInspector mi = new ManifestInspector(apkFile);
List<String> result = mi.getInspections();
return result;
}
public String toString() {
String[] columnHeaders = {"Recommendation", "Description"};
List<String[]> rows = new LinkedList<>();
rows.add(NEW_LINE);
for (ClassesDexDataEntry dexEntry : getAllDexEntries()) {
addRow(rows, dexEntry.getName(),
String.valueOf(dexEntry.allMethods) + " methods");
}
rows.add(NEW_LINE);
for (String javaDepError : getJavaDependenciesErrors()) {
addRow(rows, "Java ", javaDepError);
}
rows.add(NEW_LINE);
for (String systemBroadcast : getManifestRecommendations()) {
addRow(rows, "System Broadcast ", systemBroadcast);
}
rows.add(NEW_LINE);
for (String nativeLib : getNativeLibNamesSorted()) {
if (!getPrivateLibErrorTag(nativeLib).isEmpty()) {
addRow(rows, "Native Error ", nativeLib + " " +
getPrivateLibErrorTag(nativeLib));
}
}
String[][] array = new String[rows.size()][];
for (int i = 0; i < rows.size(); i++) {
array[i] = rows.get(i);
}
return Table.getTable(columnHeaders, array).toString();
}
private void addRow(List<String[]> data, String param1, String param2) {
List<String> row = new LinkedList<>();
row.add(param1);
row.add(param2);
data.add(row.toArray(new String[0]));
}
}