| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
package lockedregioncodeinjection; |
| 15 |
|
| 16 |
import static com.google.common.base.Preconditions.checkElementIndex; |
| 17 |
import static com.google.common.base.Preconditions.checkNotNull; |
| 18 |
import static com.google.common.base.Preconditions.checkState; |
| 19 |
|
| 20 |
import org.objectweb.asm.ClassVisitor; |
| 21 |
import org.objectweb.asm.MethodVisitor; |
| 22 |
import org.objectweb.asm.Opcodes; |
| 23 |
import org.objectweb.asm.commons.TryCatchBlockSorter; |
| 24 |
import org.objectweb.asm.tree.AbstractInsnNode; |
| 25 |
import org.objectweb.asm.tree.InsnList; |
| 26 |
import org.objectweb.asm.tree.InsnNode; |
| 27 |
import org.objectweb.asm.tree.LabelNode; |
| 28 |
import org.objectweb.asm.tree.LineNumberNode; |
| 29 |
import org.objectweb.asm.tree.MethodInsnNode; |
| 30 |
import org.objectweb.asm.tree.MethodNode; |
| 31 |
import org.objectweb.asm.tree.TypeInsnNode; |
| 32 |
import org.objectweb.asm.tree.TryCatchBlockNode; |
| 33 |
import org.objectweb.asm.tree.analysis.Analyzer; |
| 34 |
import org.objectweb.asm.tree.analysis.AnalyzerException; |
| 35 |
import org.objectweb.asm.tree.analysis.BasicValue; |
| 36 |
import org.objectweb.asm.tree.analysis.Frame; |
| 37 |
|
| 38 |
import java.util.ArrayList; |
| 39 |
import java.util.Arrays; |
| 40 |
import java.util.LinkedList; |
| 41 |
import java.util.List; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
class LockFindingClassVisitor extends ClassVisitor { |
| 63 |
private String className = null; |
| 64 |
private final List<LockTarget> targets; |
| 65 |
|
| 66 |
public LockFindingClassVisitor(List<LockTarget> targets, ClassVisitor chain) { |
| 67 |
super(Utils.ASM_VERSION, chain); |
| 68 |
this.targets = targets; |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public MethodVisitor visitMethod(int access, String name, String desc, String signature, |
| 73 |
String[] exceptions) { |
| 74 |
assert this.className != null; |
| 75 |
MethodNode mn = new TryCatchBlockSorter(null, access, name, desc, signature, exceptions); |
| 76 |
MethodVisitor chain = super.visitMethod(access, name, desc, signature, exceptions); |
| 77 |
return new LockFindingMethodVisitor(this.className, mn, chain); |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public void visit(int version, int access, String name, String signature, String superName, |
| 82 |
String[] interfaces) { |
| 83 |
this.className = name; |
| 84 |
super.visit(version, access, name, signature, superName, interfaces); |
| 85 |
} |
| 86 |
|
| 87 |
class LockFindingMethodVisitor extends MethodVisitor { |
| 88 |
private String owner; |
| 89 |
private MethodVisitor chain; |
| 90 |
private final String className; |
| 91 |
private final String methodName; |
| 92 |
|
| 93 |
public LockFindingMethodVisitor(String owner, MethodNode mn, MethodVisitor chain) { |
| 94 |
super(Utils.ASM_VERSION, mn); |
| 95 |
assert owner != null; |
| 96 |
this.owner = owner; |
| 97 |
this.chain = chain; |
| 98 |
className = owner; |
| 99 |
methodName = mn.name; |
| 100 |
} |
| 101 |
|
| 102 |
@SuppressWarnings("unchecked") |
| 103 |
@Override |
| 104 |
public void visitEnd() { |
| 105 |
MethodNode mn = (MethodNode) mv; |
| 106 |
|
| 107 |
Analyzer a = new Analyzer(new LockTargetStateAnalysis(targets)); |
| 108 |
|
| 109 |
LockTarget ownerMonitor = null; |
| 110 |
if ((mn.access & Opcodes.ACC_SYNCHRONIZED) != 0) { |
| 111 |
for (LockTarget t : targets) { |
| 112 |
if (t.getTargetDesc().equals("L" + owner + ";")) { |
| 113 |
ownerMonitor = t; |
| 114 |
if (ownerMonitor.getScoped()) { |
| 115 |
final String emsg = String.format( |
| 116 |
"scoped targets do not support synchronized methods in %s.%s()", |
| 117 |
className, methodName); |
| 118 |
throw new RuntimeException(emsg); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
try { |
| 125 |
a.analyze(owner, mn); |
| 126 |
} catch (AnalyzerException e) { |
| 127 |
throw new RuntimeException("Locked region code injection: " + e.getMessage(), e); |
| 128 |
} |
| 129 |
InsnList instructions = mn.instructions; |
| 130 |
|
| 131 |
Frame[] frames = a.getFrames(); |
| 132 |
List<Frame> frameMap = new LinkedList<>(); |
| 133 |
frameMap.addAll(Arrays.asList(frames)); |
| 134 |
|
| 135 |
List<List<TryCatchBlockNode>> handlersMap = new LinkedList<>(); |
| 136 |
|
| 137 |
for (int i = 0; i < instructions.size(); i++) { |
| 138 |
handlersMap.add(a.getHandlers(i)); |
| 139 |
} |
| 140 |
|
| 141 |
if (ownerMonitor != null) { |
| 142 |
AbstractInsnNode s = instructions.getFirst(); |
| 143 |
MethodInsnNode call = new MethodInsnNode(Opcodes.INVOKESTATIC, |
| 144 |
ownerMonitor.getPreOwner(), ownerMonitor.getPreMethod(), "()V", false); |
| 145 |
insertMethodCallBeforeSync(mn, frameMap, handlersMap, s, 0, call); |
| 146 |
} |
| 147 |
|
| 148 |
boolean anyDup = false; |
| 149 |
|
| 150 |
for (int i = 0; i < instructions.size(); i++) { |
| 151 |
AbstractInsnNode s = instructions.get(i); |
| 152 |
|
| 153 |
if (s.getOpcode() == Opcodes.MONITORENTER) { |
| 154 |
Frame f = frameMap.get(i); |
| 155 |
BasicValue operand = (BasicValue) f.getStack(f.getStackSize() - 1); |
| 156 |
if (operand instanceof LockTargetState) { |
| 157 |
LockTargetState state = (LockTargetState) operand; |
| 158 |
for (int j = 0; j < state.getTargets().size(); j++) { |
| 159 |
LockTarget target = state.getTargets().get(j); |
| 160 |
MethodInsnNode call = methodCall(target, true); |
| 161 |
if (target.getScoped()) { |
| 162 |
TypeInsnNode cast = typeCast(target); |
| 163 |
i += insertInvokeAcquire(mn, frameMap, handlersMap, s, i, |
| 164 |
call, cast); |
| 165 |
anyDup = true; |
| 166 |
} else { |
| 167 |
i += insertMethodCallBefore(mn, frameMap, handlersMap, s, i, call); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
if (s.getOpcode() == Opcodes.MONITOREXIT) { |
| 174 |
Frame f = frameMap.get(i); |
| 175 |
BasicValue operand = (BasicValue) f.getStack(f.getStackSize() - 1); |
| 176 |
if (operand instanceof LockTargetState) { |
| 177 |
LockTargetState state = (LockTargetState) operand; |
| 178 |
for (int j = 0; j < state.getTargets().size(); j++) { |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
checkState(instructions.get(i + 1).getType() == AbstractInsnNode.LABEL, |
| 184 |
"Expected to find label after monitor exit"); |
| 185 |
|
| 186 |
int labelIndex = i + 1; |
| 187 |
checkElementIndex(labelIndex, instructions.size()); |
| 188 |
|
| 189 |
LabelNode label = (LabelNode)instructions.get(labelIndex); |
| 190 |
|
| 191 |
checkNotNull(handlersMap.get(i)); |
| 192 |
checkElementIndex(0, handlersMap.get(i).size()); |
| 193 |
checkState(handlersMap.get(i).get(0).end == label, |
| 194 |
"Expected label to be the end of monitor exit's try block"); |
| 195 |
|
| 196 |
LockTarget target = state.getTargets().get(j); |
| 197 |
MethodInsnNode call = methodCall(target, false); |
| 198 |
if (target.getScoped()) { |
| 199 |
TypeInsnNode cast = typeCast(target); |
| 200 |
i += insertInvokeRelease(mn, frameMap, handlersMap, s, i, |
| 201 |
call, cast); |
| 202 |
anyDup = true; |
| 203 |
} else { |
| 204 |
insertMethodCallAfter(mn, frameMap, handlersMap, label, |
| 205 |
labelIndex, call); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
if (ownerMonitor != null && (s.getOpcode() == Opcodes.RETURN |
| 212 |
|| s.getOpcode() == Opcodes.ARETURN || s.getOpcode() == Opcodes.DRETURN |
| 213 |
|| s.getOpcode() == Opcodes.FRETURN || s.getOpcode() == Opcodes.IRETURN)) { |
| 214 |
MethodInsnNode call = |
| 215 |
new MethodInsnNode(Opcodes.INVOKESTATIC, ownerMonitor.getPostOwner(), |
| 216 |
ownerMonitor.getPostMethod(), "()V", false); |
| 217 |
insertMethodCallBeforeSync(mn, frameMap, handlersMap, s, i, call); |
| 218 |
i++; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
if (anyDup) { |
| 223 |
mn.maxStack++; |
| 224 |
} |
| 225 |
|
| 226 |
super.visitEnd(); |
| 227 |
mn.accept(chain); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public int insertInvokeAcquire(MethodNode mn, List<Frame> frameMap, |
| 234 |
List<List<TryCatchBlockNode>> handlersMap, AbstractInsnNode node, int index, |
| 235 |
MethodInsnNode call, TypeInsnNode cast) { |
| 236 |
InsnList instructions = mn.instructions; |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
instructions.insertBefore(node, new InsnNode(Opcodes.DUP)); |
| 241 |
frameMap.add(index, frameMap.get(index)); |
| 242 |
handlersMap.add(index, handlersMap.get(index)); |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
node = node.getNext(); |
| 251 |
if (!(node instanceof LabelNode)) { |
| 252 |
throw new RuntimeException(String.format("invalid bytecode sequence in %s.%s()", |
| 253 |
className, methodName)); |
| 254 |
} |
| 255 |
node = node.getNext(); |
| 256 |
index = instructions.indexOf(node); |
| 257 |
|
| 258 |
instructions.insertBefore(node, cast); |
| 259 |
frameMap.add(index, frameMap.get(index)); |
| 260 |
handlersMap.add(index, handlersMap.get(index)); |
| 261 |
|
| 262 |
instructions.insertBefore(node, call); |
| 263 |
frameMap.add(index, frameMap.get(index)); |
| 264 |
handlersMap.add(index, handlersMap.get(index)); |
| 265 |
|
| 266 |
return 3; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public int insertInvokeRelease(MethodNode mn, List<Frame> frameMap, |
| 273 |
List<List<TryCatchBlockNode>> handlersMap, AbstractInsnNode node, int index, |
| 274 |
MethodInsnNode call, TypeInsnNode cast) { |
| 275 |
InsnList instructions = mn.instructions; |
| 276 |
|
| 277 |
instructions.insertBefore(node, new InsnNode(Opcodes.DUP)); |
| 278 |
frameMap.add(index, frameMap.get(index)); |
| 279 |
handlersMap.add(index, handlersMap.get(index)); |
| 280 |
|
| 281 |
instructions.insertBefore(node, cast); |
| 282 |
frameMap.add(index, frameMap.get(index)); |
| 283 |
handlersMap.add(index, handlersMap.get(index)); |
| 284 |
|
| 285 |
instructions.insertBefore(node, call); |
| 286 |
frameMap.add(index, frameMap.get(index)); |
| 287 |
handlersMap.add(index, handlersMap.get(index)); |
| 288 |
|
| 289 |
return 3; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
public static MethodInsnNode methodCall(LockTarget target, boolean pre) { |
| 294 |
String spec = "()V"; |
| 295 |
if (!target.getScoped()) { |
| 296 |
if (pre) { |
| 297 |
return new MethodInsnNode( |
| 298 |
Opcodes.INVOKESTATIC, target.getPreOwner(), target.getPreMethod(), spec); |
| 299 |
} else { |
| 300 |
return new MethodInsnNode( |
| 301 |
Opcodes.INVOKESTATIC, target.getPostOwner(), target.getPostMethod(), spec); |
| 302 |
} |
| 303 |
} else { |
| 304 |
if (pre) { |
| 305 |
return new MethodInsnNode( |
| 306 |
Opcodes.INVOKEVIRTUAL, target.getPreOwner(), target.getPreMethod(), spec); |
| 307 |
} else { |
| 308 |
return new MethodInsnNode( |
| 309 |
Opcodes.INVOKEVIRTUAL, target.getPostOwner(), target.getPostMethod(), spec); |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
public static TypeInsnNode typeCast(LockTarget target) { |
| 315 |
if (!target.getScoped()) { |
| 316 |
return null; |
| 317 |
} else { |
| 318 |
|
| 319 |
return new TypeInsnNode(Opcodes.CHECKCAST, target.getPreOwner()); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
public static void insertMethodCallBeforeSync(MethodNode mn, List<Frame> frameMap, |
| 327 |
List<List<TryCatchBlockNode>> handlersMap, AbstractInsnNode node, int index, |
| 328 |
MethodInsnNode call) { |
| 329 |
List<TryCatchBlockNode> handlers = handlersMap.get(index); |
| 330 |
InsnList instructions = mn.instructions; |
| 331 |
LabelNode end = new LabelNode(); |
| 332 |
instructions.insert(node, end); |
| 333 |
frameMap.add(index, null); |
| 334 |
handlersMap.add(index, null); |
| 335 |
instructions.insertBefore(node, call); |
| 336 |
frameMap.add(index, null); |
| 337 |
handlersMap.add(index, null); |
| 338 |
|
| 339 |
LabelNode start = new LabelNode(); |
| 340 |
instructions.insert(node, start); |
| 341 |
frameMap.add(index, null); |
| 342 |
handlersMap.add(index, null); |
| 343 |
updateCatchHandler(mn, handlers, start, end, handlersMap); |
| 344 |
} |
| 345 |
|
| 346 |
public static void insertMethodCallAfter(MethodNode mn, List<Frame> frameMap, |
| 347 |
List<List<TryCatchBlockNode>> handlersMap, AbstractInsnNode node, int index, |
| 348 |
MethodInsnNode call) { |
| 349 |
List<TryCatchBlockNode> handlers = handlersMap.get(index + 1); |
| 350 |
InsnList instructions = mn.instructions; |
| 351 |
|
| 352 |
LabelNode end = new LabelNode(); |
| 353 |
instructions.insert(node, end); |
| 354 |
frameMap.add(index + 1, null); |
| 355 |
handlersMap.add(index + 1, null); |
| 356 |
|
| 357 |
instructions.insert(node, call); |
| 358 |
frameMap.add(index + 1, null); |
| 359 |
handlersMap.add(index + 1, null); |
| 360 |
|
| 361 |
LabelNode start = new LabelNode(); |
| 362 |
instructions.insert(node, start); |
| 363 |
frameMap.add(index + 1, null); |
| 364 |
handlersMap.add(index + 1, null); |
| 365 |
|
| 366 |
updateCatchHandler(mn, handlers, start, end, handlersMap); |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
public int insertMethodCallBefore(MethodNode mn, List<Frame> frameMap, |
| 373 |
List<List<TryCatchBlockNode>> handlersMap, AbstractInsnNode node, int index, |
| 374 |
MethodInsnNode call) { |
| 375 |
InsnList instructions = mn.instructions; |
| 376 |
|
| 377 |
instructions.insertBefore(node, call); |
| 378 |
frameMap.add(index, frameMap.get(index)); |
| 379 |
handlersMap.add(index, handlersMap.get(index)); |
| 380 |
|
| 381 |
return 1; |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
@SuppressWarnings("unchecked") |
| 386 |
public static void updateCatchHandler(MethodNode mn, List<TryCatchBlockNode> handlers, |
| 387 |
LabelNode start, LabelNode end, List<List<TryCatchBlockNode>> handlersMap) { |
| 388 |
if (handlers == null || handlers.size() == 0) { |
| 389 |
return; |
| 390 |
} |
| 391 |
|
| 392 |
InsnList instructions = mn.instructions; |
| 393 |
List<TryCatchBlockNode> newNodes = new ArrayList<>(handlers.size()); |
| 394 |
for (TryCatchBlockNode handler : handlers) { |
| 395 |
if (!(instructions.indexOf(handler.start) <= instructions.indexOf(start) |
| 396 |
&& instructions.indexOf(end) <= instructions.indexOf(handler.end))) { |
| 397 |
TryCatchBlockNode newNode = |
| 398 |
new TryCatchBlockNode(start, end, handler.handler, handler.type); |
| 399 |
newNodes.add(newNode); |
| 400 |
for (int i = instructions.indexOf(start); i <= instructions.indexOf(end); i++) { |
| 401 |
if (handlersMap.get(i) == null) { |
| 402 |
handlersMap.set(i, new ArrayList<>()); |
| 403 |
} |
| 404 |
handlersMap.get(i).add(newNode); |
| 405 |
} |
| 406 |
} else { |
| 407 |
for (int i = instructions.indexOf(start); i <= instructions.indexOf(end); i++) { |
| 408 |
if (handlersMap.get(i) == null) { |
| 409 |
handlersMap.set(i, new ArrayList<>()); |
| 410 |
} |
| 411 |
handlersMap.get(i).add(handler); |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
mn.tryCatchBlocks.addAll(0, newNodes); |
| 416 |
} |
| 417 |
} |