Skip to content

Commit

Permalink
Merge pull request #284 from alibaba/develop-for-20200113
Browse files Browse the repository at this point in the history
Develop for 20200113
  • Loading branch information
dongchenxu authored Jun 17, 2020
2 parents c6db30d + ce0cbef commit 770b334
Show file tree
Hide file tree
Showing 10 changed files with 363 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ public MethodVisitor visitMethod(final int access, final String name, final Stri

private final Label beginLabel = new Label();
private final Label endLabel = new Label();
private final Label startCatchBlock = new Label();
private final Label endCatchBlock = new Label();
private int newlocal = -1;

// 用来标记一个方法是否已经进入
// JVM中的构造函数非常特殊,super();this();是在构造函数方法体执行之外进行,如果在这个之前进行了任何的流程改变操作
Expand Down Expand Up @@ -271,30 +274,28 @@ public void code() {
}
}

/**
* 加载异常
*/
private void loadThrow() {
dup();
}

@Override
public void visitMaxs(int maxStack, int maxLocals) {
mark(endLabel);
visitTryCatchBlock(beginLabel, endLabel, mark(), ASM_TYPE_THROWABLE.getInternalName());
mv.visitLabel(startCatchBlock);
visitTryCatchBlock(beginLabel, endLabel, startCatchBlock, ASM_TYPE_THROWABLE.getInternalName());

codeLockForTracing.lock(new CodeLock.Block() {
@Override
public void code() {
loadThrow();
newlocal = newLocal(ASM_TYPE_THROWABLE);
storeLocal(newlocal);
loadLocal(newlocal);
push(namespace);
push(listenerId);
invokeStatic(ASM_TYPE_SPY, ASM_METHOD_Spy$spyMethodOnThrows);
processControl();
loadLocal(newlocal);
}
});

throwException();
mv.visitLabel(endCatchBlock);
super.visitMaxs(maxStack, maxLocals);
}

Expand Down Expand Up @@ -431,11 +432,14 @@ public void visitTryCatchBlock(Label start, Label end, Label handler, String typ
asmTryCatchBlocks.add(new AsmTryCatchBlock(start, end, handler, type));
}



@Override
public void visitEnd() {
for (AsmTryCatchBlock tcb : asmTryCatchBlocks) {
super.visitTryCatchBlock(tcb.start, tcb.end, tcb.handler, tcb.type);
}
super.visitLocalVariable("t",ASM_TYPE_THROWABLE.getDescriptor(),null,startCatchBlock,endCatchBlock,newlocal);
super.visitEnd();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ private void finishProgress(final Progress progress, final int cCnt, final int m
/*
* 形变观察所影响的类
*/
private void reTransformClasses(final int watchId,
final List<Class<?>> waitingReTransformClasses,
final Progress progress) {
private void reTransformClasses(
SandboxClassFileTransformer transformer,
final int watchId,
final List<Class<?>> waitingReTransformClasses,
final Progress progress) {

// 需要形变总数
final int total = waitingReTransformClasses.size();
Expand Down Expand Up @@ -116,6 +118,10 @@ private void reTransformClasses(final int watchId,
);
}
}
// 在真正做retransform 前的一刻,做addTransformer,避免java.lang.ClassCircularityError
if(null != transformer){
inst.addTransformer(transformer, true);
}
inst.retransformClasses(waitingReTransformClass);
logger.info("watch={} in module={} single reTransform {} success, at index={};total={};",
watchId, coreModule.getUniqueId(), waitingReTransformClass,
Expand Down Expand Up @@ -179,9 +185,6 @@ private int watch(final Matcher matcher,
// 注册到CoreModule中
coreModule.getSandboxClassFileTransformers().add(sandClassFileTransformer);

// 注册到JVM加载上ClassFileTransformer处理新增的类
inst.addTransformer(sandClassFileTransformer, true);

// 查找需要渲染的类集合
final List<Class<?>> waitingReTransformClasses = classDataSource.findForReTransform(matcher);
logger.info("watch={} in module={} found {} classes for watch(ing).",
Expand All @@ -197,7 +200,7 @@ private int watch(final Matcher matcher,
try {

// 应用JVM
reTransformClasses(watchId, waitingReTransformClasses, progress);
reTransformClasses(sandClassFileTransformer,watchId, waitingReTransformClasses, progress);

// 计数
cCnt += sandClassFileTransformer.getAffectStatistic().cCnt();
Expand Down Expand Up @@ -264,7 +267,7 @@ public void delete(final int watcherId,
beginProgress(progress, waitingReTransformClasses.size());
try {
// 应用JVM
reTransformClasses(watcherId, waitingReTransformClasses, progress);
reTransformClasses(null, watcherId, waitingReTransformClasses, progress);
} finally {
finishProgress(progress, cCnt, mCnt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,37 @@ && isSandboxPrefix(internalClassName)) {

// 类被com.alibaba.jvm.sandbox开头的ClassLoader所加载
if (null != loader
&& isSandboxPrefix(loader.getClass().getName())) {
// fix issue #267
&& isSandboxPrefix(normalizeClass(loader.getClass().getName()))) {
return true;
}

return false;

}

/**
* 标准化类名
* <p>
* 入参:com.alibaba.jvm.sandbox
* 返回:com/alibaba/jvm/sandbox
* </p>
*
* @param className 类名
* @return 标准化类名
*/
private static String normalizeClass(String className) {
return className.replace(".", "/");
}

/**
* 是否是sandbox自身的类
* <p>
* 需要注意internalClassName的格式形如: com/alibaba/jvm/sandbox
*
* @param internalClassName 类资源名
* @return true / false
*/
private static boolean isSandboxPrefix(String internalClassName) {
return internalClassName.startsWith(SANDBOX_FAMILY_CLASS_RES_PREFIX)
&& !isQaTestPrefix(internalClassName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.alibaba.jvm.sandbox.qatest.core.enhance.listener.TracingAdviceListener;
import com.alibaba.jvm.sandbox.qatest.core.enhance.target.Calculator;
import com.alibaba.jvm.sandbox.qatest.core.util.JvmHelper;
import com.alibaba.jvm.sandbox.qatest.core.util.JvmHelper.ThirdTransformer;

import org.junit.Test;

import static com.alibaba.jvm.sandbox.api.ProcessController.returnImmediately;
Expand Down Expand Up @@ -975,4 +977,25 @@ protected void before(Advice advice) throws Throwable {
"AFTER|com.alibaba.jvm.sandbox.qatest.core.enhance.target.Calculator.<init>(com.alibaba.jvm.sandbox.qatest.core.enhance.target.Calculator$TestCase)|TRUE"
);
}

@Test
@Override
public void cal$report$multiEnhance() throws Throwable {
final TracingAdviceListener listener;
final Class<?> calculatorClass = JvmHelper
.createJvm()
.defineClass(
Calculator.class,
new JvmHelper.Transformer(
CALCULATOR_REPORT_FILTER,
listener = new TracingAdviceListener()
),new ThirdTransformer(CALCULATOR_REPORT_FILTER,null)
).loadClass(CALCULATOR_CLASS_NAME);
report(newInstance(calculatorClass), "test");
listener.assertTracing(
"BEFORE|com.alibaba.jvm.sandbox.qatest.core.enhance.target.Calculator.report(java.lang.String)|TRUE",
"RETURN|com.alibaba.jvm.sandbox.qatest.core.enhance.target.Calculator.report(java.lang.String)|TRUE",
"AFTER|com.alibaba.jvm.sandbox.qatest.core.enhance.target.Calculator.report(java.lang.String)|TRUE"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import com.alibaba.jvm.sandbox.api.event.BeforeEvent;
import com.alibaba.jvm.sandbox.api.event.Event;
import com.alibaba.jvm.sandbox.qatest.core.enhance.listener.LineNumTracingEventListener;
import com.alibaba.jvm.sandbox.qatest.core.enhance.listener.TracingAdviceListener;
import com.alibaba.jvm.sandbox.qatest.core.enhance.listener.TracingEventListener;
import com.alibaba.jvm.sandbox.qatest.core.enhance.target.Calculator;
import com.alibaba.jvm.sandbox.qatest.core.util.JvmHelper;
import com.alibaba.jvm.sandbox.qatest.core.util.JvmHelper.ThirdTransformer;

import org.junit.Ignore;
import org.junit.Test;

Expand Down Expand Up @@ -972,4 +975,25 @@ public void onEvent(Event event) throws Throwable {
RETURN
);
}

@Test
@Override
public void cal$report$multiEnhance() throws Throwable {
final TracingEventListener listener;
final Class<?> calculatorClass = JvmHelper
.createJvm()
.defineClass(
Calculator.class,
new JvmHelper.Transformer(
CALCULATOR_REPORT_FILTER,
listener = new TracingEventListener(),
BEFORE, RETURN, THROWS
),new JvmHelper.ThirdTransformer(CALCULATOR_REPORT_FILTER,null)
).loadClass(CALCULATOR_CLASS_NAME);
report(newInstance(calculatorClass), "test");
listener.assertEventTracing(
BEFORE,
RETURN
);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.alibaba.jvm.sandbox.qatest.core.enhance;

import org.junit.Test;

/**
* Calculator类测试用例接口
* <p>
Expand Down Expand Up @@ -269,4 +271,10 @@ public interface ICalculatorTestCase {
*/
void cal$init_with_TestCase$before$changeParameters() throws Throwable;

/**
* {@code <init>(TestCase)}:调用跟踪 测试多次增强
*
* @throws Throwable 用例抛出异常
*/
void cal$report$multiEnhance() throws Throwable;
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,11 @@ public static void settCaseInStatic(TestCase tCase){
tCaseInStatic=tCase;
}

/**
* 空方法
*/
public void report(String param){

}

}
Loading

0 comments on commit 770b334

Please sign in to comment.