Skip to content

Commit

Permalink
Merge pull request #275 from z529192557/develop-for-20200115-issue-260
Browse files Browse the repository at this point in the history
Develop for 20200115 issue 260
  • Loading branch information
dongchenxu authored Jun 17, 2020
2 parents 5748793 + 1f67721 commit c6db30d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 13 deletions.
7 changes: 0 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@

<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
Expand Down Expand Up @@ -74,7 +69,6 @@
</configuration>
</plugin>

<!--
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
Expand All @@ -88,7 +82,6 @@
</execution>
</executions>
</plugin>
-->

<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.alibaba.jvm.sandbox.core.classloader;

/**
* @author zhuangpeng
* @since 2020/1/15
*/
public class BusinessClassLoaderHolder {

private static final ThreadLocal<DelegateBizClassLoader> holder = new ThreadLocal<DelegateBizClassLoader>();

public static void setBussinessClassLoader(ClassLoader classLoader){
if(null == classLoader){
return;
}
DelegateBizClassLoader delegateBizClassLoader = new DelegateBizClassLoader(classLoader);
holder.set(delegateBizClassLoader);
}


public static void removeBussinessClassLoader(){
holder.remove();
}

public static DelegateBizClassLoader getBussinessClassLoader(){

return holder.get();
}

public static class DelegateBizClassLoader extends ClassLoader{
public DelegateBizClassLoader(ClassLoader parent){
super(parent);
}

@Override
public Class<?> loadClass(final String javaClassName, final boolean resolve) throws ClassNotFoundException {
return super.loadClass(javaClassName,resolve);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.Collection;
import java.util.Enumeration;

import com.alibaba.jvm.sandbox.core.classloader.BusinessClassLoaderHolder.DelegateBizClassLoader;

/**
* 可路由的URLClassLoader
*
Expand Down Expand Up @@ -92,6 +94,14 @@ public Class<?> loadClass(String javaClassName) throws ClassNotFoundException {
}
return aClass;
} catch (Exception cause) {
DelegateBizClassLoader delegateBizClassLoader = BusinessClassLoaderHolder.getBussinessClassLoader();
try {
if(null != delegateBizClassLoader){
return delegateBizClassLoader.loadClass(javaClassName,resolve);
}
} catch (Exception e) {
//忽略异常,继续往下加载
}
return RoutingURLClassLoader.super.loadClass(javaClassName, resolve);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.alibaba.jvm.sandbox.api.event.Event;
import com.alibaba.jvm.sandbox.api.event.InvokeEvent;
import com.alibaba.jvm.sandbox.api.listener.EventListener;
import com.alibaba.jvm.sandbox.core.classloader.BusinessClassLoaderHolder;
import com.alibaba.jvm.sandbox.core.util.ObjectIDs;
import com.alibaba.jvm.sandbox.core.util.SandboxProtector;
import org.slf4j.Logger;
Expand Down Expand Up @@ -336,6 +337,8 @@ public Spy.Ret handleOnBefore(int listenerId, int targetClassLoaderObjectID, Obj
final int processId = process.getProcessId();

final ClassLoader javaClassLoader = ObjectIDs.instance.getObject(targetClassLoaderObjectID);
//放置业务类加载器
BusinessClassLoaderHolder.setBussinessClassLoader(javaClassLoader);
final BeforeEvent event = process.getEventFactory().makeBeforeEvent(
processId,
invokeId,
Expand All @@ -355,12 +358,20 @@ public Spy.Ret handleOnBefore(int listenerId, int targetClassLoaderObjectID, Obj

@Override
public Spy.Ret handleOnThrows(int listenerId, Throwable throwable) throws Throwable {
return handleOnEnd(listenerId, throwable, false);
try{
return handleOnEnd(listenerId, throwable, false);
}finally {
BusinessClassLoaderHolder.removeBussinessClassLoader();
}
}

@Override
public Spy.Ret handleOnReturn(int listenerId, Object object) throws Throwable {
return handleOnEnd(listenerId, object, true);
try{
return handleOnEnd(listenerId, object, true);
}finally {
BusinessClassLoaderHolder.removeBussinessClassLoader();
}
}


Expand Down Expand Up @@ -437,6 +448,7 @@ public void handleOnCallBefore(int listenerId, int lineNumber, String owner, Str
// 在守护区内产生的事件不需要响应
if (SandboxProtector.instance.isInProtecting()) {
logger.debug("listener={} is in protecting, ignore processing call-before-event", listenerId);
return;
}

final EventProcessor wrap = mappingOfEventProcessor.get(listenerId);
Expand Down Expand Up @@ -480,6 +492,7 @@ public void handleOnCallReturn(int listenerId) throws Throwable {
// 在守护区内产生的事件不需要响应
if (SandboxProtector.instance.isInProtecting()) {
logger.debug("listener={} is in protecting, ignore processing call-return-event", listenerId);
return;
}

final EventProcessor wrap = mappingOfEventProcessor.get(listenerId);
Expand Down Expand Up @@ -517,6 +530,7 @@ public void handleOnCallThrows(int listenerId, String throwException) throws Thr
// 在守护区内产生的事件不需要响应
if (SandboxProtector.instance.isInProtecting()) {
logger.debug("listener={} is in protecting, ignore processing call-throws-event", listenerId);
return;
}

final EventProcessor wrap = mappingOfEventProcessor.get(listenerId);
Expand Down Expand Up @@ -554,6 +568,7 @@ public void handleOnLine(int listenerId, int lineNumber) throws Throwable {
// 在守护区内产生的事件不需要响应
if (SandboxProtector.instance.isInProtecting()) {
logger.debug("listener={} is in protecting, ignore processing call-line-event", listenerId);
return;
}

final EventProcessor wrap = mappingOfEventProcessor.get(listenerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
public class AsmUtils {

/**
* 获取两个类型的共同父类
* just the same
* {@code org.objectweb.asm.ClassWriter#getCommonSuperClass(String, String)}
*
* @param type1 类型1
* @param type2 类型2
* @param loader 所在ClassLoader
* @return 共同的父类
*/
public static String getCommonSuperClass(String type1, String type2, ClassLoader loader) {
return getCommonSuperClassImplByAsm(type1, type2, loader);
Expand All @@ -28,10 +34,10 @@ private static String getCommonSuperClassImplByAsm(String type1, String type2, C
InputStream inputStreamOfType1 = null, inputStreamOfType2 = null;
try {
//targetClassLoader 为null,说明是BootStrapClassLoader,不能显式引用,故使用系统类加载器间接引用
if(null == targetClassLoader){
if (null == targetClassLoader) {
targetClassLoader = ClassLoader.getSystemClassLoader();
}
if(null == targetClassLoader){
if (null == targetClassLoader) {
return "java/lang/Object";
}
inputStreamOfType1 = targetClassLoader.getResourceAsStream(type1 + ".class");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class SandboxClassUtils {
* 是否是SANDBOX家族所管理的类
* <p>
* SANDBOX家族所管理的类包括:
* <li>{@code com.alibaba.jvm.sandbox.}开头的类名</li>
* <li>被{@code com.alibaba.jvm.sandbox.}开头的ClassLoader所加载的类</li>
* 1. {@code com.alibaba.jvm.sandbox.}开头的类名
* 2. 被{@code com.alibaba.jvm.sandbox.}开头的ClassLoader所加载的类
* </p>
*
* @param internalClassName 类资源名
Expand Down

0 comments on commit c6db30d

Please sign in to comment.