Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of Annotations in Dex code & UnconditionalBranchFolder #2068

Merged
merged 6 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix handling of Annotations in Dex code
  • Loading branch information
MarcMil committed Apr 15, 2024
commit 471910286b3944056b1e9e3821571a0379356c42
35 changes: 33 additions & 2 deletions src/main/java/soot/dexpler/DexAnnotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -113,6 +115,18 @@ public class DexAnnotation {
private final SootClass clazz;
private final Dependencies deps;

protected static Set<String> isHandled = new HashSet<>();

static {
isHandled.add(SootToDexUtils.getDexClassName(DALVIK_ANNOTATION_DEFAULT));
isHandled.add(SootToDexUtils.getDexClassName(DALVIK_ANNOTATION_SIGNATURE));
isHandled.add(SootToDexUtils.getDexClassName(DALVIK_ANNOTATION_MEMBERCLASSES));
isHandled.add(SootToDexUtils.getDexClassName(DALVIK_ANNOTATION_INNERCLASS));
isHandled.add(SootToDexUtils.getDexClassName(DALVIK_ANNOTATION_ENCLOSINGMETHOD));
isHandled.add(SootToDexUtils.getDexClassName(DALVIK_ANNOTATION_ENCLOSINGCLASS));
isHandled.add(SootToDexUtils.getDexClassName(JAVA_DEPRECATED));
}

public DexAnnotation(SootClass clazz, Dependencies deps) {
this.clazz = clazz;
this.deps = deps;
Expand Down Expand Up @@ -161,8 +175,11 @@ public void handleClassAnnotation(ClassDef classDef) {
// to methods through the creation of new
// AnnotationDefaultTag.
VisibilityAnnotationTag vt = (VisibilityAnnotationTag) t;
for (AnnotationTag a : vt.getAnnotations()) {
Iterator<AnnotationTag> it = vt.getAnnotations().iterator();
while (it.hasNext()) {
AnnotationTag a = it.next();
if (a.getType().equals("Ldalvik/annotation/AnnotationDefault;")) {
it.remove();
for (AnnotationElem ae : a.getElems()) {
if (ae instanceof AnnotationAnnotationElem) {
AnnotationAnnotationElem aae = (AnnotationAnnotationElem) ae;
Expand Down Expand Up @@ -228,8 +245,18 @@ public void handleClassAnnotation(ClassDef classDef) {
}
}
}
if (!(vt.getVisibility() == AnnotationConstants.RUNTIME_INVISIBLE)) {
if (vt.getVisibility() == AnnotationConstants.RUNTIME_INVISIBLE)
clazz.addTag(vt);
else {
// filter out the tags we handle explicitly
VisibilityAnnotationTag vbCopy = new VisibilityAnnotationTag(vt.getVisibility());
for (AnnotationTag tf : vt.getAnnotations()) {
if (!isHandled(tf)) {
vbCopy.addAnnotation(tf);
}
}
if (vbCopy.getAnnotations() != null && !vbCopy.getAnnotations().isEmpty())
clazz.addTag(vbCopy);
}
} else {
clazz.addTag(t);
Expand All @@ -238,6 +265,10 @@ public void handleClassAnnotation(ClassDef classDef) {
}
}

private boolean isHandled(AnnotationTag tf) {
return isHandled.contains(tf.getType());
}

private Type getSootType(AnnotationElem e) {
Type annotationType;
switch (e.getKind()) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/soot/toDex/DexPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,9 @@ private EncodedValue buildEncodedValueForAnnotation(AnnotationElem elem) {
Collection<AnnotationElem> elems = e.getValue().getElems();
if (!elems.isEmpty()) {
elements = new ArrayList<AnnotationElement>();
Set<String> alreadyWritten = new HashSet<String>();
Set<AnnotationElem> alreadyWritten = new HashSet<AnnotationElem>();
for (AnnotationElem ae : elems) {
if (!alreadyWritten.add(ae.getName())) {
if (!alreadyWritten.add(ae)) {
throw new DexPrinterException("Duplicate annotation attribute: " + ae.getName());
}
elements.add(new ImmutableAnnotationElement(ae.getName(), buildEncodedValueForAnnotation(ae)));
Expand Down Expand Up @@ -670,7 +670,7 @@ protected void addClassDefinition(ClassDef classDef) {
}
}

private Set<Annotation> buildClassAnnotations(SootClass c) {
protected Set<Annotation> buildClassAnnotations(SootClass c) {
Set<String> skipList = new HashSet<String>();
Set<Annotation> annotations = buildCommonAnnotations(c, skipList);

Expand Down Expand Up @@ -855,7 +855,7 @@ private Set<Annotation> buildCommonAnnotations(AbstractHost host, Set<String> sk
return annotations;
}

private List<ImmutableAnnotation> buildVisibilityAnnotationTag(VisibilityAnnotationTag t, Set<String> skipList) {
protected List<ImmutableAnnotation> buildVisibilityAnnotationTag(VisibilityAnnotationTag t, Set<String> skipList) {
if (t.getAnnotations() == null) {
return Collections.emptyList();
}
Expand Down
Loading