| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
package lockedregioncodeinjection; |
| 15 |
|
| 16 |
import org.objectweb.asm.Type; |
| 17 |
import org.objectweb.asm.tree.AbstractInsnNode; |
| 18 |
import org.objectweb.asm.tree.MethodInsnNode; |
| 19 |
import org.objectweb.asm.tree.analysis.AnalyzerException; |
| 20 |
import org.objectweb.asm.tree.analysis.BasicInterpreter; |
| 21 |
import org.objectweb.asm.tree.analysis.BasicValue; |
| 22 |
|
| 23 |
import java.util.ArrayList; |
| 24 |
import java.util.List; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public class LockTargetStateAnalysis extends BasicInterpreter { |
| 31 |
|
| 32 |
private final List<LockTarget> targetLocks; |
| 33 |
|
| 34 |
public LockTargetStateAnalysis(List<LockTarget> targetLocks) { |
| 35 |
super(Utils.ASM_VERSION); |
| 36 |
this.targetLocks = targetLocks; |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
public BasicValue naryOperation(AbstractInsnNode inst, @SuppressWarnings("rawtypes") List args) |
| 41 |
throws AnalyzerException { |
| 42 |
|
| 43 |
|
| 44 |
@SuppressWarnings("unchecked") |
| 45 |
BasicValue base = super.naryOperation(inst, args); |
| 46 |
if (!(inst instanceof MethodInsnNode)) { |
| 47 |
return base; |
| 48 |
} |
| 49 |
|
| 50 |
MethodInsnNode invoke = (MethodInsnNode) inst; |
| 51 |
Type returnType = Type.getReturnType(invoke.desc); |
| 52 |
if (returnType.equals(Type.VOID_TYPE)) { |
| 53 |
return base; |
| 54 |
} |
| 55 |
|
| 56 |
List<LockTarget> types = new ArrayList<>(); |
| 57 |
|
| 58 |
for (LockTarget target : targetLocks) { |
| 59 |
if (returnType.getDescriptor().equals(target.getTargetDesc())) { |
| 60 |
types.add(target); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return new LockTargetState(base.getType(), types); |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public BasicValue newValue(Type type) { |
| 69 |
BasicValue base = super.newValue(type); |
| 70 |
List<LockTarget> types = new ArrayList<>(); |
| 71 |
|
| 72 |
if (type == null) { |
| 73 |
return base; |
| 74 |
} |
| 75 |
for (LockTarget target : targetLocks) { |
| 76 |
if (type.getDescriptor().equals(target.getTargetDesc())) { |
| 77 |
types.add(target); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if (types.isEmpty()) { |
| 82 |
return base; |
| 83 |
} |
| 84 |
|
| 85 |
return new LockTargetState(base.getType(), types); |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public BasicValue merge(BasicValue v1, BasicValue v2) { |
| 90 |
BasicValue base = super.merge(v1, v2); |
| 91 |
|
| 92 |
if (!(v1 instanceof LockTargetState)) { |
| 93 |
return base; |
| 94 |
} |
| 95 |
if (!(v2 instanceof LockTargetState)) { |
| 96 |
return base; |
| 97 |
} |
| 98 |
|
| 99 |
LockTargetState state1 = (LockTargetState) v1; |
| 100 |
LockTargetState state2 = (LockTargetState) v2; |
| 101 |
|
| 102 |
List<LockTarget> newList = new ArrayList<>(state1.getTargets()); |
| 103 |
for (LockTarget otherTarget : state2.getTargets()) { |
| 104 |
if (!newList.contains(otherTarget)) { |
| 105 |
newList.add(otherTarget); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
return new LockTargetState(base.getType(), newList); |
| 110 |
} |
| 111 |
} |