Skip to content

Commit

Permalink
Fix of bug with classloader
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniilStepanov committed Nov 23, 2023
1 parent ab40c0f commit 2b7fc64
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.usvm.instrumentation.agent

import org.usvm.instrumentation.classloader.UtilClassLoader
import org.usvm.instrumentation.classloader.WorkerClassLoader
import org.usvm.instrumentation.instrumentation.JcInstrumenterFactory
import org.usvm.instrumentation.util.toByteArray
Expand All @@ -17,6 +18,7 @@ class ClassTransformer(
private val instrumenterFactoryInstance =
Class.forName(instrumenterClassName).constructors.first().newInstance() as JcInstrumenterFactory<*>
private val instrumenterCache = HashMap<String, ByteArray>()
private val utilClassLoader = UtilClassLoader()


override fun transform(
Expand All @@ -34,7 +36,7 @@ class ClassTransformer(
return instrumenterCache.getOrPut(className) {
val instrumenter = instrumenterFactoryInstance.create(loader.jcClasspath)
val instrumentedClassNode = instrumenter.instrumentClass(classfileBuffer.toClassNode())
instrumentedClassNode.toByteArray(loader, checkClass = true)
instrumentedClassNode.toByteArray(utilClassLoader, checkClass = true)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.usvm.instrumentation.classloader

class UtilClassLoader: ClassLoader()
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class URLClassPathLoader(private val classPath: List<File>) {
fun getCodeSigners(): Array<CodeSigner>?
}

private val urlClassLoader = URLClassLoader(classPath.map { it.toURI().toURL() }.toTypedArray())
private val urlClassLoader = URLClassLoader(classPath.map { it.toURI().toURL() }.toTypedArray(), null)

fun getResource(name: String): Resource {
val resourceUrl = urlClassLoader.getResource(name) ?: error("Resource $name not found on classpath $classPath")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ val JcInst.enclosingMethod
fun JcType.toJavaClass(classLoader: ClassLoader): Class<*> =
when (this) {
is JcPrimitiveType -> toJavaClass()
is JcArrayType -> findClassInLoader(toJvmType(), classLoader) ?: throw TestExecutorException("Can't find class in classpath")
is JcArrayType -> findClassInLoader(toJvmType(), classLoader)
is JcClassType -> this.jcClass.toJavaClass(classLoader)
else -> findClassInLoader(typeName, classLoader) ?: throw TestExecutorException("Can't find class in classpath")
else -> findClassInLoader(typeName, classLoader)
}

private fun JcPrimitiveType.toJavaClass(): Class<*> {
Expand Down Expand Up @@ -104,12 +104,16 @@ fun JcType.toJcClass(): JcClassOrInterface? =
else -> error("Unexpected type")
}

fun JcClassOrInterface.toJavaClass(classLoader: ClassLoader): Class<*> =
findClassInLoader(name, classLoader) ?: throw TestExecutorException("Can't find class in classpath")
fun JcClassOrInterface.toJavaClass(classLoader: ClassLoader, initialize: Boolean = false): Class<*> =
findClassInLoader(name, classLoader, initialize)


fun findClassInLoader(name: String, classLoader: ClassLoader): Class<*>? =
Class.forName(name, true, classLoader)
fun findClassInLoader(name: String, classLoader: ClassLoader, initialize: Boolean = false): Class<*> =
try {
Class.forName(name, initialize, classLoader)
} catch (e: Throwable) {
throw TestExecutorException("Can't find class in classpath")
}

fun JcField.toJavaField(classLoader: ClassLoader): Field? =
enclosingClass.toType().toJavaClass(classLoader).getFieldByName(name)
Expand Down

0 comments on commit 2b7fc64

Please sign in to comment.