Skip to content

Commit 0fcd216

Browse files
committed
Added trusted libraries scope
1 parent 8b2e736 commit 0fcd216

File tree

4 files changed

+89
-4
lines changed

4 files changed

+89
-4
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.utbot.framework
2+
3+
import mu.KotlinLogging
4+
import org.utbot.common.PathUtil.toPath
5+
import java.io.IOException
6+
7+
private val logger = KotlinLogging.logger {}
8+
9+
private val defaultUserTrustedLibrariesPath: String = "${utbotHomePath}/trustedLibraries.txt"
10+
private const val userTrustedLibrariesKey: String = "utbot.settings.trusted.libraries.path"
11+
12+
object TrustedLibraries {
13+
/**
14+
* JDK and some "trustworthy" open-source libraries.
15+
*/
16+
private val defaultTrustedLibraries: List<String> = listOf(
17+
"java",
18+
"sun",
19+
"javax",
20+
"com.sun",
21+
"org.omg",
22+
"org.xml",
23+
"org.w3c.dom",
24+
"com.google.common",
25+
"org.antlr.v4",
26+
"org.antlr.runtime",
27+
"com.alibaba.fastjson",
28+
"com.alibaba.fescar.core",
29+
"org.apache.pdfbox",
30+
"io.seata.core",
31+
"spoon"
32+
)
33+
34+
private val userTrustedLibraries: List<String>
35+
get() {
36+
val userTrustedLibrariesPath = System.getProperty(userTrustedLibrariesKey) ?: defaultUserTrustedLibrariesPath
37+
val userTrustedLibrariesFile = userTrustedLibrariesPath.toPath().toFile()
38+
39+
if (!userTrustedLibrariesFile.exists()) {
40+
return emptyList()
41+
}
42+
43+
return try {
44+
userTrustedLibrariesFile.readLines()
45+
} catch (e: IOException) {
46+
logger.info { e.message }
47+
48+
emptyList()
49+
}
50+
}
51+
52+
/**
53+
* Represents prefixes of packages for trusted libraries -
54+
* as the union of [defaultTrustedLibraries] and [userTrustedLibraries].
55+
*/
56+
val trustedLibraries: Set<String> by lazy { (defaultTrustedLibraries + userTrustedLibraries).toSet() }
57+
}

utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ import kotlin.reflect.KProperty
1010

1111
private val logger = KotlinLogging.logger {}
1212

13+
/**
14+
* Path to the utbot home folder.
15+
*/
16+
internal val utbotHomePath = "${System.getProperty("user.home")}/.utbot"
17+
1318
/**
1419
* Default path for properties file
1520
*/
16-
internal val defaultSettingsPath = "${System.getProperty("user.home")}/.utbot/settings.properties"
17-
internal const val defaultKeyForSettingsPath = "utbot.settings.path"
21+
private val defaultSettingsPath = "$utbotHomePath/settings.properties"
22+
private const val defaultKeyForSettingsPath = "utbot.settings.path"
1823

1924
internal class SettingDelegate<T>(val initializer: () -> T) {
2025
private var value = initializer()

utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ import org.utbot.engine.symbolic.asHardConstraint
106106
import org.utbot.engine.symbolic.asSoftConstraint
107107
import org.utbot.engine.symbolic.asAssumption
108108
import org.utbot.engine.symbolic.asUpdate
109+
import org.utbot.engine.util.trusted.isFromTrustedLibrary
109110
import org.utbot.engine.util.mockListeners.MockListener
110111
import org.utbot.engine.util.mockListeners.MockListenerController
111112
import org.utbot.engine.util.statics.concrete.associateEnumSootFieldsWithConcreteValues
@@ -2249,10 +2250,10 @@ class UtBotSymbolicEngine(
22492250
* Marks the [createdField] as speculatively not null if the [field] is considering as
22502251
* not producing [NullPointerException].
22512252
*
2252-
* @see [SootField.speculativelyCannotProduceNullPointerException], [markAsSpeculativelyNotNull]
2253+
* @see [SootField.speculativelyCannotProduceNullPointerException], [markAsSpeculativelyNotNull], [isFromTrustedLibrary].
22532254
*/
22542255
private fun checkAndMarkLibraryFieldSpeculativelyNotNull(field: SootField, createdField: SymbolicValue) {
2255-
if (maximizeCoverageUsingReflection || !field.declaringClass.isLibraryClass) {
2256+
if (maximizeCoverageUsingReflection || !field.declaringClass.isFromTrustedLibrary()) {
22562257
return
22572258
}
22582259

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.utbot.engine.util.trusted
2+
3+
import org.utbot.framework.TrustedLibraries
4+
import soot.SootClass
5+
6+
/**
7+
* Cache for already discovered trusted/untrusted packages.
8+
*/
9+
private val isPackageTrusted: MutableMap<String, Boolean> = mutableMapOf()
10+
11+
/**
12+
* Determines whether [this] class is from trusted libraries as defined in [TrustedLibraries].
13+
*/
14+
fun SootClass.isFromTrustedLibrary(): Boolean {
15+
isPackageTrusted[packageName]?.let {
16+
return it
17+
}
18+
19+
val isTrusted = TrustedLibraries.trustedLibraries.any { packageName.startsWith(it, ignoreCase = false) }
20+
21+
return isTrusted.also { isPackageTrusted[packageName] = it }
22+
}

0 commit comments

Comments
 (0)