Skip to content

Commit 3735aad

Browse files
IlyaMuravjovSaloed
authored andcommitted
Allow configuring paths to instrumentation jars, approximation jars and java home (#166)
1 parent 553da91 commit 3735aad

9 files changed

Lines changed: 51 additions & 40 deletions

File tree

usvm-jvm-instrumentation/src/main/kotlin/org/usvm/instrumentation/agent/Agent.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Agent {
1111
companion object {
1212
@JvmStatic
1313
fun premain(arguments: String?, instrumentation: Instrumentation) {
14-
val collectorsJarPath = InstrumentationModuleConstants.pathToUsvmCollectorsJar
14+
val collectorsJarPath = System.getenv(InstrumentationModuleConstants.envVarForPathToUsvmCollectorsJarPath)
1515
val collectorsJar = JarFile(File(collectorsJarPath))
1616
instrumentation.appendToBootstrapClassLoaderSearch(collectorsJar)
1717
val instrumenterFactoryClassname = arguments ?: JcRuntimeTraceInstrumenterFactory::class.java.name
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.usvm.instrumentation.executor
2+
3+
data class InstrumentationProcessPaths(
4+
val pathToUsvmInstrumentationJar: String = System.getenv("usvm-jvm-instrumentation-jar"),
5+
val pathToUsvmCollectorsJar: String = System.getenv("usvm-jvm-collectors-jar"),
6+
val pathToJava: String = System.getenv()["JAVA_HOME"] ?: System.getProperty("java.home")
7+
)

usvm-jvm-instrumentation/src/main/kotlin/org/usvm/instrumentation/executor/InstrumentationProcessRunner.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import kotlin.time.Duration
2222
class InstrumentationProcessRunner(
2323
private val testingProjectClasspath: String,
2424
private val jcClasspath: JcClasspath,
25-
private val javaHome: String,
25+
private val instrumentationProcessPaths: InstrumentationProcessPaths,
2626
private val jcPersistenceLocation: String?,
2727
private val instrumentationClassFactory: KClass<out JcInstrumenterFactory<out JcInstrumenter>>
2828
) {
@@ -33,13 +33,13 @@ class InstrumentationProcessRunner(
3333
constructor(
3434
testingProjectClasspath: List<String>,
3535
jcClasspath: JcClasspath,
36-
javaHome: String,
36+
instrumentationProcessPaths: InstrumentationProcessPaths,
3737
jcPersistenceLocation: String?,
3838
instrumentationClassFactory: KClass<JcInstrumenterFactory<out JcInstrumenter>>
3939
) : this(
4040
testingProjectClasspath.joinToString(File.pathSeparator),
4141
jcClasspath,
42-
javaHome,
42+
instrumentationProcessPaths,
4343
jcPersistenceLocation,
4444
instrumentationClassFactory
4545
)
@@ -49,14 +49,14 @@ class InstrumentationProcessRunner(
4949
private val jvmArgs: List<String> by lazy {
5050
val instrumentationClassNameFactoryName = instrumentationClassFactory.java.name
5151
val memoryLimit = listOf("-Xmx1g")
52-
val pathToJava = Paths.get(InstrumentationModuleConstants.pathToJava)
52+
val pathToJava = Paths.get(instrumentationProcessPaths.pathToJava)
5353
val usvmClasspath = System.getProperty("java.class.path")
5454
val javaVersionSpecificArguments = OpenModulesContainer.javaVersionSpecificArguments
5555
val instrumentedProcessClassName =
5656
InstrumentedProcess::class.qualifiedName ?: error("Can't find instumented process")
5757
listOf(pathToJava.resolve("bin${File.separatorChar}${osSpecificJavaExecutable()}").toString()) +
5858
listOf("-ea") +
59-
listOf("-javaagent:${InstrumentationModuleConstants.pathToUsvmInstrumentationJar}=$instrumentationClassNameFactoryName") +
59+
listOf("-javaagent:${instrumentationProcessPaths.pathToUsvmInstrumentationJar}=$instrumentationClassNameFactoryName") +
6060
memoryLimit +
6161
javaVersionSpecificArguments +
6262
listOf("-classpath", usvmClasspath) +
@@ -67,7 +67,7 @@ class InstrumentationProcessRunner(
6767
this += listOf("-cp", testingProjectClasspath)
6868
this += listOf("-t", "${InstrumentationModuleConstants.concreteExecutorProcessTimeout}")
6969
this += listOf("-p", "$rdPort")
70-
this += listOf("-javahome", javaHome)
70+
this += listOf("-javahome", instrumentationProcessPaths.pathToJava)
7171

7272
if (jcPersistenceLocation != null) {
7373
this += listOf("-persistence", jcPersistenceLocation)
@@ -80,6 +80,8 @@ class InstrumentationProcessRunner(
8080
val rdPort = NetUtils.findFreePort(0)
8181
val workerCommand = jvmArgs + createWorkerProcessArgs(rdPort)
8282
val pb = ProcessBuilder(workerCommand).inheritIO()
83+
pb.environment()[InstrumentationModuleConstants.envVarForPathToUsvmCollectorsJarPath] =
84+
instrumentationProcessPaths.pathToUsvmCollectorsJar
8385
val process = pb.start()
8486
rdProcessRunner =
8587
RdProcessRunner(process = process, rdPort = rdPort, jcClasspath = jcClasspath, lifetime = processLifetime)

usvm-jvm-instrumentation/src/main/kotlin/org/usvm/instrumentation/executor/UTestConcreteExecutor.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class UTestConcreteExecutor(
1818
instrumentationClassFactory: KClass<out JcInstrumenterFactory<*>>,
1919
testingProjectClasspath: String,
2020
private val jcClasspath: JcClasspath,
21-
private val javaHome: String,
21+
instrumentationProcessPaths: InstrumentationProcessPaths,
2222
private val jcPersistenceLocation: String?,
2323
private val timeout: Duration
2424
) : AutoCloseable {
@@ -27,14 +27,14 @@ class UTestConcreteExecutor(
2727
instrumentationClassFactory: KClass<out JcInstrumenterFactory<*>>,
2828
testingProjectClasspath: List<String>,
2929
jcClasspath: JcClasspath,
30-
javaHome: String,
30+
instrumentationProcessPaths: InstrumentationProcessPaths,
3131
jcPersistenceLocation: String?,
3232
timeout: Duration
3333
) : this(
3434
instrumentationClassFactory,
3535
testingProjectClasspath.joinToString(File.pathSeparator),
3636
jcClasspath,
37-
javaHome,
37+
instrumentationProcessPaths,
3838
jcPersistenceLocation,
3939
timeout
4040
)
@@ -44,7 +44,7 @@ class UTestConcreteExecutor(
4444
private val instrumentationProcessRunner = InstrumentationProcessRunner(
4545
testingProjectClasspath,
4646
jcClasspath,
47-
javaHome,
47+
instrumentationProcessPaths,
4848
jcPersistenceLocation,
4949
instrumentationClassFactory
5050
)

usvm-jvm-instrumentation/src/main/kotlin/org/usvm/instrumentation/util/Constants.kt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ object InstrumentationModuleConstants {
2323

2424
const val nameForExistingButNullString = "USVM_GENERATED_NULL_STRING"
2525

26-
//Passes as environment parameter
27-
val pathToUsvmInstrumentationJar: String
28-
get() = System.getenv("usvm-jvm-instrumentation-jar")
29-
30-
val pathToUsvmCollectorsJar: String
31-
get() = System.getenv("usvm-jvm-collectors-jar")
32-
33-
val pathToJava: String
34-
get() = System.getenv()["JAVA_HOME"] ?: System.getProperty("java.home")
35-
26+
//Environment variable used to pass path to collectors jar from main process to instrumented process
27+
val envVarForPathToUsvmCollectorsJarPath = "usvm-jvm-collectors-jar"
3628
}

usvm-jvm-instrumentation/src/test/kotlin/org/usvm/instrumentation/executor/UTestConcreteExecutorTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ abstract class UTestConcreteExecutorTest {
1818
lateinit var jcClasspath: JcClasspath
1919
lateinit var jcPersistenceLocation: String
2020
lateinit var uTestConcreteExecutor: UTestConcreteExecutor
21+
var instrumentationProcessPaths = InstrumentationProcessPaths()
2122

2223

2324
@JvmStatic
@@ -27,7 +28,7 @@ abstract class UTestConcreteExecutorTest {
2728
val db = jacodb {
2829
loadByteCode(cp)
2930
installFeatures(InMemoryHierarchy)
30-
jre = File(InstrumentationModuleConstants.pathToJava)
31+
jre = File(instrumentationProcessPaths.pathToJava)
3132
persistent(location = jcPersistenceLocation)
3233
}
3334
db.awaitBackgroundJobs()
@@ -40,7 +41,7 @@ abstract class UTestConcreteExecutorTest {
4041
instrumentationClassFactory = JcRuntimeTraceInstrumenterFactory::class,
4142
testingProjectClasspath = testJarPath,
4243
jcClasspath = jcClasspath,
43-
javaHome = InstrumentationModuleConstants.pathToJava,
44+
instrumentationProcessPaths = instrumentationProcessPaths,
4445
jcPersistenceLocation = jcPersistenceLocation,
4546
timeout = InstrumentationModuleConstants.testExecutionTimeout
4647
)

usvm-jvm/src/main/kotlin/org/usvm/types/ClassScorer.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,21 @@ import org.jacodb.impl.fs.className
1111
import org.jacodb.impl.storage.withoutAutoCommit
1212
import org.jooq.DSLContext
1313
import org.objectweb.asm.tree.ClassNode
14-
import org.usvm.util.USVM_API_JAR_PATH
15-
import org.usvm.util.USVM_APPROXIMATIONS_JAR_PATH
14+
import org.usvm.util.ApproximationPaths
1615
import java.util.concurrent.ConcurrentHashMap
1716

1817
typealias ScoreCache<Result> = ConcurrentHashMap<Long, Result>
1918

20-
private val usvmApiJarPath = System.getenv(USVM_API_JAR_PATH)
21-
private val usvmApproximationsJarPath = System.getenv(USVM_APPROXIMATIONS_JAR_PATH)
22-
2319
class ScorerIndexer<Result : Comparable<Result>>(
2420
private val persistence: JcDatabasePersistence,
2521
private val location: RegisteredLocation,
2622
private val cache: ScoreCache<Result>,
2723
private val scorer: (RegisteredLocation, ClassNode) -> Result,
24+
approximationPaths: ApproximationPaths = ApproximationPaths(),
2825
) : ByteCodeIndexer {
2926
private val interner = persistence.symbolInterner
3027

31-
private val bad: Boolean = usvmApiJarPath in location.path || usvmApproximationsJarPath in location.path
28+
private val bad: Boolean = approximationPaths.presentPaths.any { it in location.path }
3229

3330
override fun index(classNode: ClassNode) {
3431
val clazzSymbolId = interner.findOrNew(classNode.name.className)

usvm-jvm/src/main/kotlin/org/usvm/util/JcApproximationUtils.kt

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,17 @@ import org.usvm.machine.logger
1111
import java.io.File
1212
import java.util.concurrent.ConcurrentHashMap
1313

14-
const val USVM_API_JAR_PATH = "usvm.jvm.api.jar.path"
15-
const val USVM_APPROXIMATIONS_JAR_PATH = "usvm.jvm.approximations.jar.path"
14+
data class ApproximationPaths(
15+
val usvmApiJarPath: String? = System.getenv("usvm.jvm.api.jar.path"),
16+
val usvmApproximationsJarPath: String? = System.getenv("usvm.jvm.approximations.jar.path")
17+
) {
18+
val namedPaths = mapOf(
19+
"USVM API" to usvmApiJarPath,
20+
"USVM Approximations" to usvmApproximationsJarPath
21+
)
22+
val presentPaths: Set<String> = namedPaths.values.filterNotNull().toSet()
23+
val allPathsArePresent = namedPaths.values.all { it != null }
24+
}
1625

1726
private val classpathApproximations: MutableMap<JcClasspath, Set<String>> = ConcurrentHashMap()
1827

@@ -29,19 +38,21 @@ val JcClassType.isUsvmInternalClass: Boolean
2938

3039
suspend fun JcDatabase.classpathWithApproximations(
3140
dirOrJars: List<File>,
32-
features: List<JcClasspathFeature> = emptyList()
41+
features: List<JcClasspathFeature> = emptyList(),
42+
approximationPaths: ApproximationPaths = ApproximationPaths(),
3343
): JcClasspath {
34-
val usvmApiJarPath = System.getenv(USVM_API_JAR_PATH)
35-
val usvmApproximationsJarPath = System.getenv(USVM_APPROXIMATIONS_JAR_PATH)
36-
37-
if (usvmApiJarPath == null || usvmApproximationsJarPath == null) {
44+
if (!approximationPaths.allPathsArePresent) {
45+
logger.warn {
46+
"Classpath with approximations is requested, but some jar paths are missing: $approximationPaths"
47+
}
3848
return classpath(dirOrJars, features)
3949
}
4050

41-
logger.info { "Load USVM API: $usvmApiJarPath" }
42-
logger.info { "Load USVM Approximations: $usvmApproximationsJarPath" }
51+
approximationPaths.namedPaths.forEach { (name, path) ->
52+
logger.info { "Load $name: $path" }
53+
}
4354

44-
val approximationsPath = setOf(File(usvmApiJarPath), File(usvmApproximationsJarPath))
55+
val approximationsPath = approximationPaths.presentPaths.map { File(it) }
4556

4657
val cpWithApproximations = dirOrJars + approximationsPath
4758
val featuresWithApproximations = features + listOf(Approximations)

usvm-jvm/src/test/kotlin/org/usvm/util/UTestRunner.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import org.jacodb.api.JcClasspath
44
import org.usvm.instrumentation.executor.UTestConcreteExecutor
55
import org.usvm.instrumentation.instrumentation.JcRuntimeTraceInstrumenterFactory
66
import org.usvm.instrumentation.util.InstrumentationModuleConstants
7+
import org.usvm.instrumentation.executor.InstrumentationProcessPaths
78

89
object UTestRunner {
910

@@ -16,7 +17,7 @@ object UTestRunner {
1617
instrumentationClassFactory = JcRuntimeTraceInstrumenterFactory::class,
1718
testingProjectClasspath = pathToJars,
1819
jcClasspath = classpath,
19-
javaHome = InstrumentationModuleConstants.pathToJava,
20+
instrumentationProcessPaths = InstrumentationProcessPaths(),
2021
jcPersistenceLocation = null,
2122
timeout = InstrumentationModuleConstants.testExecutionTimeout
2223
)

0 commit comments

Comments
 (0)