Skip to content

Commit

Permalink
Fix hidden not match cases, and add anonymous class bind var and rank.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lynn Dai committed Jul 24, 2022
1 parent 4e70fb3 commit 57154e2
Show file tree
Hide file tree
Showing 22 changed files with 196 additions and 49 deletions.
Binary file added jar/enre_java_1.1.5.jar
Binary file not shown.
13 changes: 9 additions & 4 deletions src/main/java/TempOutput/JsonString.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import entity.properties.Relation;
import org.json.JSONObject;

import util.PathUtil;
import util.SingleCollect;
import util.Tuple;
import visitor.relationInf.RelationInf;
Expand Down Expand Up @@ -48,8 +47,8 @@ public static String JSONWriteRelation(Map<Integer, ArrayList<Tuple<Integer, Rel
SingleCollect singleCollect = SingleCollect.getSingleCollectInstance();
ProcessHidden processHidden = ProcessHidden.getProcessHiddeninstance();
if (hiddenPath != null){
processHidden.convertCSV(hiddenPath);
// System.out.println(processHidden.getResult());
processHidden.convertCSV2DB(hiddenPath);
// processHidden.outputConvertInfo("base-enre-out/hidden_convert.csv");
}

obj.put("schemaVersion","1");
Expand Down Expand Up @@ -143,6 +142,10 @@ public static String JSONWriteRelation(Map<Integer, ArrayList<Tuple<Integer, Rel
if (!((TypeEntity) entity).getInnerType().isEmpty()){
entityObj.put("innerType", ((TypeEntity) entity).getInnerType());
}
if (entity instanceof ClassEntity && ((ClassEntity) entity).getAnonymousRank() != 0){
entityObj.put("anonymousRank", ((ClassEntity) entity).getAnonymousRank());
entityObj.put("anonymousBindVar", ((ClassEntity) entity).getAnonymousBindVar());
}
}
//location
if (!(entity instanceof FileEntity || entity instanceof PackageEntity)){
Expand Down Expand Up @@ -248,7 +251,9 @@ public static String JSONWriteRelation(Map<Integer, ArrayList<Tuple<Integer, Rel
/**
* Output not match hidden
*/
processHidden.outputResult();
// if (hiddenPath != null) {
// processHidden.outputResult();
// }
return obj.toString();
}

Expand Down
167 changes: 132 additions & 35 deletions src/main/java/TempOutput/ProcessHidden.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import com.google.gson.stream.JsonReader;
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
import entity.MethodEntity;
import entity.TypeEntity;
import entity.VariableEntity;
import entity.*;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.json.JSONObject;

import util.SingleCollect;
import util.Tuple;
import util.Triple;

Expand Down Expand Up @@ -46,6 +44,16 @@ public static ProcessHidden getProcessHiddeninstance() {
return processHiddeninstance;
}

ArrayList<String> wholeFileList = new ArrayList<>();

public void setWholeFileList(ArrayList<String> wholeFileList) {
this.wholeFileList = wholeFileList;
}

public ArrayList<String> getWholeFileList(){
return this.wholeFileList;
}

static class HiddenEntity {

String qualifiedName = null;
Expand Down Expand Up @@ -184,7 +192,6 @@ public String process_class(String class_descriptor) {
}



public Tuple<String, String> process_field(String field) {
if (field.contains(":")){
String fieldName = field.split(":",2)[0];
Expand All @@ -195,16 +202,26 @@ public Tuple<String, String> process_field(String field) {
}


public ArrayList<String> process_parameter( String parameter){
public ArrayList<String> process_parameter(String parameter){
ArrayList<String> result = new ArrayList<>();
boolean isList = false;
if (parameter.contains(";")){
for (String par : parameter.split(";")){
if (process_type_signature(par).equals("")){
for (char s : par.toCharArray()){
result.add(process_type_signature(String.valueOf(s)));
if (s == '['){
isList = true;
continue;
}
if (isList){
result.add(process_type_signature("[".concat(String.valueOf(s))));
isList = false;
continue;
}
if (String.valueOf(s).equals("L")){
break;
}
result.add(process_type_signature(String.valueOf(s)));
}
if (par.contains("L")){
result.add(process_type_signature("L"+ par.split("L",2)[1]));
Expand All @@ -214,6 +231,25 @@ public ArrayList<String> process_parameter( String parameter){
}
}
}
else if (parameter.contains("[")){
for (char s : parameter.toCharArray()){
if (s == '['){
isList = true;
continue;
}
if (isList){
result.add(process_type_signature("[".concat(String.valueOf(s))));
isList = false;
continue;
}
result.add(process_type_signature(String.valueOf(s)));
}
}
else {
for (char s : parameter.toCharArray()){
result.add(process_type_signature(String.valueOf(s)));
}
}
return result;
}

Expand All @@ -237,16 +273,17 @@ public Triple<String, ArrayList<String>, String> process_method(String method) {
}


public void convertCSV(String csvPath) {
public void convertCSV2DB(String csvPath) {
HiddenEntity entity = null;
try (CSVReader csvReader = new CSVReader(new FileReader(csvPath));) {
String[] rows = null;
while ((rows = csvReader.readNext()) != null) {
for (String e: rows){
if (!e.equals("")){
if (e.contains("->")){
if (entity != null && entity.qualifiedName != null){
this.addEntity(entity.qualifiedName, entity);
if (entity != null
&& entity.getQualifiedName() != null){
this.addEntity(entity.getQualifiedName(), entity);
}
entity = new HiddenEntity();
entity.setOriginalSignature(e);
Expand Down Expand Up @@ -347,15 +384,21 @@ else if (para1.size() == para2.length){
}
}

SingleCollect singleCollect = SingleCollect.getSingleCollectInstance();

public String checkHidden(TypeEntity entity){
if (this.result.containsKey(entity.getQualifiedName())){
if (this.result.get(entity.getQualifiedName()).size() == 1){
this.result.get(entity.getQualifiedName()).get(0).setMatch(true);
this.result.get(entity.getQualifiedName()).get(0).setKind("Type");
return refactorHidden(this.result.get(entity.getQualifiedName()).get(0).getHiddenApi());
String qualifiedName = entity.getQualifiedName();
if (entity instanceof ClassEntity && ((ClassEntity) entity).getAnonymousRank()!=0){
qualifiedName = qualifiedName.replace("Anonymous_Class", String.valueOf(((ClassEntity) entity).getAnonymousRank()));
}
if (this.result.containsKey(qualifiedName)){
if (this.result.get(qualifiedName).size() == 1){
this.result.get(qualifiedName).get(0).setMatch(true);
this.result.get(qualifiedName).get(0).setKind("Type");
return refactorHidden(this.result.get(qualifiedName).get(0).getHiddenApi());
}
else {
for (HiddenEntity hiddenEntity: this.result.get(entity.getQualifiedName())){
for (HiddenEntity hiddenEntity: this.result.get(qualifiedName)){
if (hiddenEntity.getOriginalSignature().contains("<clinit>")){
hiddenEntity.setMatch(true);
hiddenEntity.setKind("Type");
Expand All @@ -368,23 +411,28 @@ public String checkHidden(TypeEntity entity){
}

public String checkHidden(MethodEntity entity, String parType){
if (this.result.containsKey(entity.getQualifiedName())){
if (this.result.get(entity.getQualifiedName()).size() == 1){
this.result.get(entity.getQualifiedName()).get(0).setMatch(true);
this.result.get(entity.getQualifiedName()).get(0).setKind("Method");
return refactorHidden(this.result.get(entity.getQualifiedName()).get(0).getHiddenApi());
String qualifiedName = entity.getQualifiedName();
BaseEntity parentClass = singleCollect.getEntityById(entity.getParentId());
if (parentClass instanceof ClassEntity && ((ClassEntity) parentClass).getAnonymousRank() != 0){
qualifiedName = qualifiedName.replace("Anonymous_Class", String.valueOf(((ClassEntity) parentClass).getAnonymousRank()));
}
if (this.result.containsKey(qualifiedName)){
if (this.result.get(qualifiedName).size() == 1){
this.result.get(qualifiedName).get(0).setMatch(true);
this.result.get(qualifiedName).get(0).setKind("Method");
return refactorHidden(this.result.get(qualifiedName).get(0).getHiddenApi());
}
else {
for (HiddenEntity hiddenEntity: this.result.get(entity.getQualifiedName())){
for (HiddenEntity hiddenEntity: this.result.get(qualifiedName)){
if (entity.getRawType() != null && hiddenEntity.getRawType() != null){
if (entity.isConstructor()){
if (!hiddenEntity.getParameter().isEmpty() && comparePara(hiddenEntity.getParameter(), parType.split(" "))){
if (comparePara(hiddenEntity.getParameter(), parType.split(" "))){
hiddenEntity.setMatch(true);
hiddenEntity.setKind("Method");
return refactorHidden(hiddenEntity.getHiddenApi());
}
} else if (entity.getRawType().equals(hiddenEntity.getRawType())){
if (!hiddenEntity.getParameter().isEmpty() && comparePara(hiddenEntity.getParameter(), parType.split(" "))){
} else if (JsonString.processRawType(entity.getRawType()).contains(hiddenEntity.getRawType())){
if (comparePara(hiddenEntity.getParameter(), parType.split(" "))){
hiddenEntity.setMatch(true);
hiddenEntity.setKind("Method");
return refactorHidden(hiddenEntity.getHiddenApi());
Expand All @@ -407,9 +455,14 @@ public String checkHidden(MethodEntity entity, String parType){
}

public String checkHidden(VariableEntity entity){
if (this.result.containsKey(entity.getQualifiedName())){
for (HiddenEntity hiddenEntity: this.result.get(entity.getQualifiedName())){
if (entity.getRawType().equals(hiddenEntity.getRawType())){
String qualifiedName = entity.getQualifiedName();
BaseEntity parentClass = singleCollect.getEntityById(entity.getParentId());
if (parentClass instanceof ClassEntity && ((ClassEntity) parentClass).getAnonymousRank() != 0){
qualifiedName = qualifiedName.replace("Anonymous_Class", String.valueOf(((ClassEntity) parentClass).getAnonymousRank()));
}
if (this.result.containsKey(qualifiedName)){
for (HiddenEntity hiddenEntity: this.result.get(qualifiedName)){
if (JsonString.processRawType(entity.getRawType()).contains(hiddenEntity.getRawType())){
hiddenEntity.setMatch(true);
hiddenEntity.setKind("Field");
return refactorHidden(hiddenEntity.getHiddenApi());
Expand All @@ -434,24 +487,44 @@ public String refactorHidden(ArrayList<String> hiddenApi){
return hidden;
}

public boolean checkBaseHidden(String signature){
String path = signature.split(";", 2)[0].substring(1);
if (path.contains("$")){
path = path.split("\\$", 2)[0];
}
for (String filePath : getWholeFileList()){
if (filePath.endsWith(path+".java")){
return true;
}
}
return false;
}

public void outputResult() throws IOException {
//output not match
String fileName = "base-enre-out\\hidden-not-match.csv";
String fileName = "base-enre-out/hidden-not-match.csv";
Writer out = null;
FileOutputStream fileOs = null;
fileOs = new FileOutputStream(fileName);
out = new OutputStreamWriter(fileOs, "GBK");
//字符数组是头行
CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT.withHeader("OriginalSignature", "processName", "processRawType", "processParameter", "kind").withQuote(null));
CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT.withHeader( "inBase", "OriginalSignature", "processName", "processRawType", "processParameter").withQuote(null));
List<Object> objects = new ArrayList<>();
for (ArrayList<HiddenEntity> hiddenEntities : this.getResult().values()) {
for (HiddenEntity entity : hiddenEntities){
if (!entity.isMatch()){
if (!entity.isMatch() && !entity.getOriginalSignature().contains("$$") && !entity.getOriginalSignature().startsWith("Lcom/google/android/collect")
&& !entity.getOriginalSignature().startsWith("Lcom/google/android/gles_jni") && !entity.getOriginalSignature().startsWith("Lcom/google/android/mms")
&& !entity.getOriginalSignature().startsWith("Lcom/google/android/rappor") && !entity.getOriginalSignature().startsWith("Lcom/google/android/util")
&& !entity.getOriginalSignature().startsWith("Lcom/sun") && !entity.getOriginalSignature().startsWith("Ldalvik")
&& !entity.getOriginalSignature().startsWith("Lgov/nist") && !entity.getOriginalSignature().startsWith("Ljava/")
&& !entity.getOriginalSignature().startsWith("Ljavax/") && !entity.getOriginalSignature().startsWith("Ljdk/")
&& !entity.getOriginalSignature().startsWith("Llibcore/") && !entity.getOriginalSignature().startsWith("Lorg/")
&& !entity.getOriginalSignature().startsWith("Lsun/") && !entity.getOriginalSignature().startsWith("Landroid/Manifest$")){
objects.add(checkBaseHidden(entity.getOriginalSignature()));
objects.add(entity.getOriginalSignature());
objects.add(entity.getQualifiedName());
objects.add(entity.getRawType());
objects.add(entity.getParameter());
objects.add(entity.getKind());
objects.add(entity.getParameter().toString());
//打印一行
printer.printRecord(objects);
//打印完后注意将数组clear掉
Expand Down Expand Up @@ -505,9 +578,33 @@ public void checkMatch(String qualifiedName, String rawType, String parameterTyp
}
}

public void outputConvertInfo(String outputFilePath) throws IOException {
Writer out;
FileOutputStream fileOs;
fileOs = new FileOutputStream(outputFilePath);
out = new OutputStreamWriter(fileOs, "GBK");
//字符数组是头行
CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT.withHeader("isBase", "OriginalSignature", "processName", "processRawType", "processParameter").withQuote(null));
List<Object> objects = new ArrayList<>();
for (ArrayList<HiddenEntity> hiddenEntities : this.getResult().values()) {
for (HiddenEntity entity : hiddenEntities){
objects.add(checkBaseHidden(entity.getOriginalSignature()));
objects.add(entity.getOriginalSignature());
objects.add(entity.getQualifiedName());
objects.add(entity.getRawType());
objects.add(entity.getParameter().toString());
//打印一行
printer.printRecord(objects);
//打印完后注意将数组clear掉
objects.clear();
}
}
out.flush();
}

public static void main(String[] args) throws IOException {
ProcessHidden processHidden = ProcessHidden.getProcessHiddeninstance();
processHidden.convertCSV("E:\\Android\\hiddenapi-flags.csv");
processHidden.convertCSV2DB("E:\\Android\\hiddenapi-flags.csv");
FileReader in = new FileReader("base-enre-out\\base-out-with-hidden.json");
JsonReader reader = new JsonReader(in);
reader.beginObject();
Expand Down Expand Up @@ -581,7 +678,7 @@ public static void main(String[] args) throws IOException {
objects.add(entity.getOriginalSignature());
objects.add(entity.getQualifiedName());
objects.add(entity.getRawType());
objects.add(entity.getParameter());
objects.add(entity.getParameter().toString());
//打印一行
printer.printRecord(objects);
//打印完后注意将数组clear掉
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/client/IdentifyEntities.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package client;

import TempOutput.ProcessHidden;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
Expand Down Expand Up @@ -119,6 +120,9 @@ public void run(){
whole_file_list.addAll(checkBin.get(binPath));
}

ProcessHidden processHidden = ProcessHidden.getProcessHiddeninstance();
processHidden.setWholeFileList(whole_file_list);

final ArrayList<CompilationUnitPair> pairs = new ArrayList<CompilationUnitPair>(whole_file_list.size());


Expand Down
20 changes: 20 additions & 0 deletions src/main/java/entity/ClassEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ public class ClassEntity extends TypeEntity{
protected String superClassName;
protected int superClassId = -1;

//mark whether the current class is anonymous, if it's not, the value will be 0, otherwise the value will be the order of anonymous
protected int anonymousRank = 0;

protected int anonymousBindVar = -1;

protected HashMap<String, Integer> staticMap = new HashMap<>();

public ClassEntity(int id, String name){
Expand Down Expand Up @@ -54,6 +59,21 @@ public void addStaticMap(String name, int id){
this.staticMap.put(name, id);
}

public void setAnonymousRank(int rank){
this.anonymousRank = rank;
}

public int getAnonymousRank(){
return this.anonymousRank;
}

public int getAnonymousBindVar(){
return this.anonymousBindVar;
}

public void setAnonymousBindVar(int bindVar){
this.anonymousBindVar = bindVar;
}

@Override
public String toString() {
Expand Down
Loading

0 comments on commit 57154e2

Please sign in to comment.