Skip to content

Commit

Permalink
Extend scope of monitoring stack for excess variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Sep 13, 2020
1 parent 77e6e5f commit 197703a
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 72 deletions.
134 changes: 62 additions & 72 deletions byte-buddy-dep/src/main/java/net/bytebuddy/asm/Advice.java
Original file line number Diff line number Diff line change
Expand Up @@ -9746,11 +9746,6 @@ public TypeDescription getThrowable() {
*/
protected abstract static class AdviceVisitor extends ExceptionTableSensitiveMethodVisitor implements Dispatcher.RelocationHandler.Relocation {

/**
* The actual method visitor that is underlying this method visitor to which all instructions are written.
*/
protected final MethodVisitor methodVisitor;

/**
* A description of the instrumented method.
*/
Expand Down Expand Up @@ -9790,7 +9785,6 @@ protected abstract static class AdviceVisitor extends ExceptionTableSensitiveMet
* Creates a new advice visitor.
*
* @param methodVisitor The actual method visitor that is underlying this method visitor to which all instructions are written.
* @param delegate A delegate to which all instructions of the original method are written to. Must delegate to {@code methodVisitor}.
* @param implementationContext The implementation context to use.
* @param assigner The assigner to use.
* @param exceptionHandler The stack manipulation to apply within a suppression handler.
Expand All @@ -9803,7 +9797,6 @@ protected abstract static class AdviceVisitor extends ExceptionTableSensitiveMet
* @param readerFlags The ASM reader flags that were set.
*/
protected AdviceVisitor(MethodVisitor methodVisitor,
MethodVisitor delegate,
Context implementationContext,
Assigner assigner,
StackManipulation exceptionHandler,
Expand All @@ -9814,8 +9807,7 @@ protected AdviceVisitor(MethodVisitor methodVisitor,
List<? extends TypeDescription> postMethodTypes,
int writerFlags,
int readerFlags) {
super(OpenedClassReader.ASM_API, delegate);
this.methodVisitor = methodVisitor;
super(OpenedClassReader.ASM_API, methodVisitor);
this.instrumentedMethod = instrumentedMethod;
preparationStart = new Label();
argumentHandler = methodExit.getArgumentHandlerFactory().resolve(instrumentedMethod,
Expand Down Expand Up @@ -9873,11 +9865,11 @@ protected void onAfterExceptionTable() {
methodExit.prepare();
methodEnter.initialize();
methodExit.initialize();
stackMapFrameHandler.injectInitializationFrame(methodVisitor);
stackMapFrameHandler.injectInitializationFrame(mv);
methodEnter.apply();
methodVisitor.visitLabel(preparationStart);
methodSizeHandler.requireStackSize(argumentHandler.prepare(methodVisitor));
stackMapFrameHandler.injectStartFrame(methodVisitor);
mv.visitLabel(preparationStart);
methodSizeHandler.requireStackSize(argumentHandler.prepare(mv));
stackMapFrameHandler.injectStartFrame(mv);
onUserStart();
}

Expand All @@ -9903,13 +9895,13 @@ protected void onVisitIincInsn(int offset, int increment) {

@Override
public void onVisitFrame(int type, int localVariableLength, Object[] localVariable, int stackSize, Object[] stack) {
stackMapFrameHandler.translateFrame(methodVisitor, type, localVariableLength, localVariable, stackSize, stack);
stackMapFrameHandler.translateFrame(mv, type, localVariableLength, localVariable, stackSize, stack);
}

@Override
public void visitMaxs(int stackSize, int localVariableLength) {
onUserEnd();
methodVisitor.visitMaxs(methodSizeHandler.compoundStackSize(stackSize), methodSizeHandler.compoundLocalVariableLength(localVariableLength));
mv.visitMaxs(methodSizeHandler.compoundStackSize(stackSize), methodSizeHandler.compoundLocalVariableLength(localVariableLength));
}

@Override
Expand Down Expand Up @@ -9965,7 +9957,6 @@ protected WithoutExitAdvice(MethodVisitor methodVisitor,
int writerFlags,
int readerFlags) {
super(methodVisitor,
methodVisitor,
implementationContext,
assigner,
exceptionHandler,
Expand Down Expand Up @@ -10058,8 +10049,7 @@ protected WithExitAdvice(MethodVisitor methodVisitor,
List<? extends TypeDescription> postMethodTypes,
int writerFlags,
int readerFlags) {
super(methodVisitor,
new StackAwareMethodVisitor(methodVisitor, instrumentedMethod),
super(new StackAwareMethodVisitor(methodVisitor, instrumentedMethod),
implementationContext,
assigner,
exceptionHandler,
Expand Down Expand Up @@ -10125,32 +10115,32 @@ protected void onVisitInsn(int opcode) {

@Override
protected void onUserEnd() {
methodVisitor.visitLabel(returnHandler);
mv.visitLabel(returnHandler);
onUserReturn();
stackMapFrameHandler.injectCompletionFrame(methodVisitor);
stackMapFrameHandler.injectCompletionFrame(mv);
methodExit.apply();
onExitAdviceReturn();
if (instrumentedMethod.getReturnType().represents(boolean.class)
|| instrumentedMethod.getReturnType().represents(byte.class)
|| instrumentedMethod.getReturnType().represents(short.class)
|| instrumentedMethod.getReturnType().represents(char.class)
|| instrumentedMethod.getReturnType().represents(int.class)) {
methodVisitor.visitVarInsn(Opcodes.ILOAD, argumentHandler.returned());
methodVisitor.visitInsn(Opcodes.IRETURN);
mv.visitVarInsn(Opcodes.ILOAD, argumentHandler.returned());
mv.visitInsn(Opcodes.IRETURN);
} else if (instrumentedMethod.getReturnType().represents(long.class)) {
methodVisitor.visitVarInsn(Opcodes.LLOAD, argumentHandler.returned());
methodVisitor.visitInsn(Opcodes.LRETURN);
mv.visitVarInsn(Opcodes.LLOAD, argumentHandler.returned());
mv.visitInsn(Opcodes.LRETURN);
} else if (instrumentedMethod.getReturnType().represents(float.class)) {
methodVisitor.visitVarInsn(Opcodes.FLOAD, argumentHandler.returned());
methodVisitor.visitInsn(Opcodes.FRETURN);
mv.visitVarInsn(Opcodes.FLOAD, argumentHandler.returned());
mv.visitInsn(Opcodes.FRETURN);
} else if (instrumentedMethod.getReturnType().represents(double.class)) {
methodVisitor.visitVarInsn(Opcodes.DLOAD, argumentHandler.returned());
methodVisitor.visitInsn(Opcodes.DRETURN);
mv.visitVarInsn(Opcodes.DLOAD, argumentHandler.returned());
mv.visitInsn(Opcodes.DRETURN);
} else if (!instrumentedMethod.getReturnType().represents(void.class)) {
methodVisitor.visitVarInsn(Opcodes.ALOAD, argumentHandler.returned());
methodVisitor.visitInsn(Opcodes.ARETURN);
mv.visitVarInsn(Opcodes.ALOAD, argumentHandler.returned());
mv.visitInsn(Opcodes.ARETURN);
} else {
methodVisitor.visitInsn(Opcodes.RETURN);
mv.visitInsn(Opcodes.RETURN);
}
methodSizeHandler.requireStackSize(instrumentedMethod.getReturnType().getStackSize().getSize());
}
Expand Down Expand Up @@ -10226,20 +10216,20 @@ protected void onUserReturn() {
|| instrumentedMethod.getReturnType().represents(short.class)
|| instrumentedMethod.getReturnType().represents(char.class)
|| instrumentedMethod.getReturnType().represents(int.class)) {
stackMapFrameHandler.injectReturnFrame(methodVisitor);
methodVisitor.visitVarInsn(Opcodes.ISTORE, argumentHandler.returned());
stackMapFrameHandler.injectReturnFrame(mv);
mv.visitVarInsn(Opcodes.ISTORE, argumentHandler.returned());
} else if (instrumentedMethod.getReturnType().represents(long.class)) {
stackMapFrameHandler.injectReturnFrame(methodVisitor);
methodVisitor.visitVarInsn(Opcodes.LSTORE, argumentHandler.returned());
stackMapFrameHandler.injectReturnFrame(mv);
mv.visitVarInsn(Opcodes.LSTORE, argumentHandler.returned());
} else if (instrumentedMethod.getReturnType().represents(float.class)) {
stackMapFrameHandler.injectReturnFrame(methodVisitor);
methodVisitor.visitVarInsn(Opcodes.FSTORE, argumentHandler.returned());
stackMapFrameHandler.injectReturnFrame(mv);
mv.visitVarInsn(Opcodes.FSTORE, argumentHandler.returned());
} else if (instrumentedMethod.getReturnType().represents(double.class)) {
stackMapFrameHandler.injectReturnFrame(methodVisitor);
methodVisitor.visitVarInsn(Opcodes.DSTORE, argumentHandler.returned());
stackMapFrameHandler.injectReturnFrame(mv);
mv.visitVarInsn(Opcodes.DSTORE, argumentHandler.returned());
} else if (!instrumentedMethod.getReturnType().represents(void.class)) {
stackMapFrameHandler.injectReturnFrame(methodVisitor);
methodVisitor.visitVarInsn(Opcodes.ASTORE, argumentHandler.returned());
stackMapFrameHandler.injectReturnFrame(mv);
mv.visitVarInsn(Opcodes.ASTORE, argumentHandler.returned());
}
}

Expand Down Expand Up @@ -10315,72 +10305,72 @@ protected WithExceptionHandling(MethodVisitor methodVisitor,

@Override
protected void onUserPrepare() {
methodVisitor.visitTryCatchBlock(userStart, returnHandler, exceptionHandler, throwable.getInternalName());
mv.visitTryCatchBlock(userStart, returnHandler, exceptionHandler, throwable.getInternalName());
}

@Override
protected void onUserStart() {
methodVisitor.visitLabel(userStart);
mv.visitLabel(userStart);
}

@Override
protected void onUserReturn() {
stackMapFrameHandler.injectReturnFrame(methodVisitor);
stackMapFrameHandler.injectReturnFrame(mv);
if (instrumentedMethod.getReturnType().represents(boolean.class)
|| instrumentedMethod.getReturnType().represents(byte.class)
|| instrumentedMethod.getReturnType().represents(short.class)
|| instrumentedMethod.getReturnType().represents(char.class)
|| instrumentedMethod.getReturnType().represents(int.class)) {
methodVisitor.visitVarInsn(Opcodes.ISTORE, argumentHandler.returned());
mv.visitVarInsn(Opcodes.ISTORE, argumentHandler.returned());
} else if (instrumentedMethod.getReturnType().represents(long.class)) {
methodVisitor.visitVarInsn(Opcodes.LSTORE, argumentHandler.returned());
mv.visitVarInsn(Opcodes.LSTORE, argumentHandler.returned());
} else if (instrumentedMethod.getReturnType().represents(float.class)) {
methodVisitor.visitVarInsn(Opcodes.FSTORE, argumentHandler.returned());
mv.visitVarInsn(Opcodes.FSTORE, argumentHandler.returned());
} else if (instrumentedMethod.getReturnType().represents(double.class)) {
methodVisitor.visitVarInsn(Opcodes.DSTORE, argumentHandler.returned());
mv.visitVarInsn(Opcodes.DSTORE, argumentHandler.returned());
} else if (!instrumentedMethod.getReturnType().represents(void.class)) {
methodVisitor.visitVarInsn(Opcodes.ASTORE, argumentHandler.returned());
mv.visitVarInsn(Opcodes.ASTORE, argumentHandler.returned());
}
methodVisitor.visitInsn(Opcodes.ACONST_NULL);
methodVisitor.visitVarInsn(Opcodes.ASTORE, argumentHandler.thrown());
mv.visitInsn(Opcodes.ACONST_NULL);
mv.visitVarInsn(Opcodes.ASTORE, argumentHandler.thrown());
Label endOfHandler = new Label();
methodVisitor.visitJumpInsn(Opcodes.GOTO, endOfHandler);
methodVisitor.visitLabel(exceptionHandler);
stackMapFrameHandler.injectExceptionFrame(methodVisitor);
methodVisitor.visitVarInsn(Opcodes.ASTORE, argumentHandler.thrown());
mv.visitJumpInsn(Opcodes.GOTO, endOfHandler);
mv.visitLabel(exceptionHandler);
stackMapFrameHandler.injectExceptionFrame(mv);
mv.visitVarInsn(Opcodes.ASTORE, argumentHandler.thrown());
if (instrumentedMethod.getReturnType().represents(boolean.class)
|| instrumentedMethod.getReturnType().represents(byte.class)
|| instrumentedMethod.getReturnType().represents(short.class)
|| instrumentedMethod.getReturnType().represents(char.class)
|| instrumentedMethod.getReturnType().represents(int.class)) {
methodVisitor.visitInsn(Opcodes.ICONST_0);
methodVisitor.visitVarInsn(Opcodes.ISTORE, argumentHandler.returned());
mv.visitInsn(Opcodes.ICONST_0);
mv.visitVarInsn(Opcodes.ISTORE, argumentHandler.returned());
} else if (instrumentedMethod.getReturnType().represents(long.class)) {
methodVisitor.visitInsn(Opcodes.LCONST_0);
methodVisitor.visitVarInsn(Opcodes.LSTORE, argumentHandler.returned());
mv.visitInsn(Opcodes.LCONST_0);
mv.visitVarInsn(Opcodes.LSTORE, argumentHandler.returned());
} else if (instrumentedMethod.getReturnType().represents(float.class)) {
methodVisitor.visitInsn(Opcodes.FCONST_0);
methodVisitor.visitVarInsn(Opcodes.FSTORE, argumentHandler.returned());
mv.visitInsn(Opcodes.FCONST_0);
mv.visitVarInsn(Opcodes.FSTORE, argumentHandler.returned());
} else if (instrumentedMethod.getReturnType().represents(double.class)) {
methodVisitor.visitInsn(Opcodes.DCONST_0);
methodVisitor.visitVarInsn(Opcodes.DSTORE, argumentHandler.returned());
mv.visitInsn(Opcodes.DCONST_0);
mv.visitVarInsn(Opcodes.DSTORE, argumentHandler.returned());
} else if (!instrumentedMethod.getReturnType().represents(void.class)) {
methodVisitor.visitInsn(Opcodes.ACONST_NULL);
methodVisitor.visitVarInsn(Opcodes.ASTORE, argumentHandler.returned());
mv.visitInsn(Opcodes.ACONST_NULL);
mv.visitVarInsn(Opcodes.ASTORE, argumentHandler.returned());
}
methodVisitor.visitLabel(endOfHandler);
mv.visitLabel(endOfHandler);
methodSizeHandler.requireStackSize(StackSize.SINGLE.getSize());
}

@Override
protected void onExitAdviceReturn() {
methodVisitor.visitVarInsn(Opcodes.ALOAD, argumentHandler.thrown());
mv.visitVarInsn(Opcodes.ALOAD, argumentHandler.thrown());
Label endOfHandler = new Label();
methodVisitor.visitJumpInsn(Opcodes.IFNULL, endOfHandler);
methodVisitor.visitVarInsn(Opcodes.ALOAD, argumentHandler.thrown());
methodVisitor.visitInsn(Opcodes.ATHROW);
methodVisitor.visitLabel(endOfHandler);
stackMapFrameHandler.injectPostCompletionFrame(methodVisitor);
mv.visitJumpInsn(Opcodes.IFNULL, endOfHandler);
mv.visitVarInsn(Opcodes.ALOAD, argumentHandler.thrown());
mv.visitInsn(Opcodes.ATHROW);
mv.visitLabel(endOfHandler);
stackMapFrameHandler.injectPostCompletionFrame(mv);
}
}
}
Expand Down

0 comments on commit 197703a

Please sign in to comment.