Skip to content

Commit

Permalink
codeLock优化
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuangpeng.zp committed Nov 29, 2022
1 parent b5b9c43 commit cdb72ce
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package com.alibaba.jvm.sandbox.core.enhance.weaver.asm;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.alibaba.jvm.sandbox.api.event.Event;
import com.alibaba.jvm.sandbox.core.enhance.weaver.CodeLock;
import com.alibaba.jvm.sandbox.core.manager.NativeMethodEnhanceAware;
import org.objectweb.asm.*;

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.AdviceAdapter;
import org.objectweb.asm.commons.JSRInlinerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.instrument.Instrumentation;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static com.alibaba.jvm.sandbox.core.util.SandboxStringUtils.toInternalClassName;
import static com.alibaba.jvm.sandbox.core.util.SandboxStringUtils.toJavaClassName;
import static org.apache.commons.lang3.ArrayUtils.contains;
Expand Down Expand Up @@ -168,8 +171,6 @@ public MethodVisitor visitMethod(final int access, final String name, final Stri
private final Label startCatchBlock = new Label();
private final Label endCatchBlock = new Label();
private int newLocal = -1;
// 代码锁
private final CodeLock codeLockForTracing = new CallAsmCodeLock(this);

// 加载ClassLoader
private void loadClassLoader() {
Expand Down Expand Up @@ -258,11 +259,6 @@ public void code() {
// 所以这里需要用一个标记为告知后续的代码编织,绕开super()和this()
private boolean isMethodEnter = false;

// 代码锁
private final CodeLock codeLockForTracing = new CallAsmCodeLock(this);



// 加载ClassLoader
private void loadClassLoader() {
push(targetClassLoaderObjectID);
Expand Down Expand Up @@ -338,7 +334,7 @@ private void loadReturn(int opcode) {

@Override
protected void onMethodExit(final int opcode) {
if (!isThrow(opcode)) {
if (!isThrow(opcode) && !codeLockForTracing.isLock()) {
codeLockForTracing.lock(new CodeLock.Block() {
@Override
public void code() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.alibaba.jvm.sandbox.core.enhance.weaver.asm;

import java.com.alibaba.jvm.sandbox.spy.Spy;

import com.alibaba.jvm.sandbox.core.enhance.weaver.CodeLock;

import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.AdviceAdapter;

import java.com.alibaba.jvm.sandbox.spy.Spy;

/**
* 方法重写
* ReWriteJavaMethod
Expand All @@ -17,6 +19,8 @@ public class ReWriteMethod extends AdviceAdapter implements Opcodes, AsmTypes, A

private final Type[] argumentTypeArray;

// 代码锁
protected final CodeLock codeLockForTracing = new CallAsmCodeLock(this);
/**
* Creates a new {@link AdviceAdapter}.
*
Expand Down Expand Up @@ -191,6 +195,48 @@ final protected void popRawRespond(Type returnType) {
}
}

@Override
public void visitIntInsn(final int opcode, final int operand) {
super.visitIntInsn(opcode,operand);
codeLockForTracing.code(opcode);
}

@Override
public void visitVarInsn(final int opcode, final int var) {
super.visitVarInsn(opcode,var);
codeLockForTracing.code(opcode);
}

@Override
public void visitTypeInsn(final int opcode, final String type) {
super.visitTypeInsn(opcode,type);
codeLockForTracing.code(opcode);
}

@Override
public void visitFieldInsn(
final int opcode, final String owner, final String name, final String descriptor) {
super.visitFieldInsn(opcode,owner,name,descriptor);
codeLockForTracing.code(opcode);
}

@Override
public void visitJumpInsn(final int opcode, final Label label) {
super.visitJumpInsn(opcode,label);
codeLockForTracing.code(opcode);
}

public void visitLdcInsn(final Object value) {
super.visitLdcInsn(value);
codeLockForTracing.code(Opcodes.LDC);
}

@Override
public void visitIincInsn(final int var, final int increment) {
super.visitIincInsn(var,increment);
codeLockForTracing.code(Opcodes.IINC);
}

final protected void processControl(String desc) {
processControl(desc, false);
}
Expand Down

0 comments on commit cdb72ce

Please sign in to comment.