Skip to content

Commit

Permalink
Merge pull request #94 from vorpal-research/class-loader-instrumenter
Browse files Browse the repository at this point in the history
New instrumentation and class loading
  • Loading branch information
AbdullinAM authored Oct 13, 2023
2 parents 26245a1 + 51234bc commit e09313d
Show file tree
Hide file tree
Showing 37 changed files with 576 additions and 309 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package org.vorpal.research.kex.asm.manager
import org.vorpal.research.kex.asm.util.AccessModifier
import org.vorpal.research.kex.asm.util.accessModifier
import org.vorpal.research.kex.config.kexConfig
import org.vorpal.research.kex.util.asmString
import org.vorpal.research.kex.util.KfgTargetFilter
import org.vorpal.research.kfg.ClassManager
import org.vorpal.research.kfg.Package
import org.vorpal.research.kfg.ir.Method
import org.vorpal.research.kfg.ir.value.instruction.ReturnInst
import org.vorpal.research.kfg.type.objectType
Expand All @@ -29,26 +28,19 @@ object MethodManager {

object InlineManager {
val inliningEnabled = kexConfig.getBooleanValue("inliner", "enabled", true)
private val ignorePackages = hashSetOf<Package>()
private val ignoreClasses = hashSetOf<String>()
private val ignores = hashSetOf<KfgTargetFilter>()

init {
ignorePackages.addAll(
kexConfig.getMultipleStringValue("inliner", "ignorePackage", ",").map {
Package.parse(it)
ignores.addAll(
kexConfig.getMultipleStringValue("inliner", "ignore", ",").map {
KfgTargetFilter.parse(it)
}
)
ignoreClasses.addAll(
kexConfig.getMultipleStringValue("inliner", "ignoreClass", ",").map {
it.asmString
}
)
ignorePackages += Package.parse("org.vorpal.research.kex.intrinsics.*")
ignores += KfgTargetFilter.parse("package org.vorpal.research.kex.intrinsics.*")
}

fun isIgnored(method: Method) = when {
ignorePackages.any { it.isParent(method.klass.pkg) } -> true
ignoreClasses.any { method.cm[it] == method.klass } -> true
ignores.any { it.matches(method.klass) } -> true
else -> false
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package org.vorpal.research.kex.asm.transform

import org.vorpal.research.kex.ExecutionContext
import org.vorpal.research.kex.trace.symbolic.InstructionTraceCollector
import org.vorpal.research.kex.trace.symbolic.TraceCollectorProxy
import org.vorpal.research.kex.util.asmString
import org.vorpal.research.kex.util.insertAfter
import org.vorpal.research.kex.util.insertBefore
import org.vorpal.research.kex.util.wrapValue
import org.vorpal.research.kex.util.wrapUpValue
import org.vorpal.research.kfg.ClassManager
import org.vorpal.research.kfg.Package
import org.vorpal.research.kfg.arrayListClass
import org.vorpal.research.kfg.ir.CatchBlock
import org.vorpal.research.kfg.ir.Class
import org.vorpal.research.kfg.ir.Location
import org.vorpal.research.kfg.ir.Method
import org.vorpal.research.kfg.ir.value.EmptyUsageContext
import org.vorpal.research.kfg.ir.value.ThisRef
Expand Down Expand Up @@ -54,10 +55,16 @@ import org.vorpal.research.kfg.type.stringType
import org.vorpal.research.kfg.visitor.MethodVisitor

class SymbolicTraceInstrumenter(
private val executionContext: ExecutionContext,
override val cm: ClassManager,
private val ignores: Set<Package> = setOf()
) : MethodVisitor, InstructionBuilder {
companion object {
val SYMBOLIC_TRACE_LOCATION = Location(
pkg = Package.parse("org.vorpal.research.kex.asm.transform"),
file = "SymbolicTraceInstrumenter",
line = 62
)

private val INSTRUCTION_TRACE_COLLECTOR = InstructionTraceCollector::class.java
.canonicalName
.asmString
Expand All @@ -68,8 +75,6 @@ class SymbolicTraceInstrumenter(
}

override val ctx: UsageContext = EmptyUsageContext
override val cm get() = executionContext.cm

override val instructions: InstructionFactory
get() = cm.instruction
override val types: TypeFactory
Expand All @@ -87,6 +92,18 @@ class SymbolicTraceInstrumenter(

override fun cleanup() {}

private fun Instruction.mapLocation(): Instruction {
this.withLocation(SYMBOLIC_TRACE_LOCATION)
return this
}

private fun List<Instruction>.mapLocation(): List<Instruction> {
for (inst in this) {
inst.withLocation(SYMBOLIC_TRACE_LOCATION)
}
return this
}

private fun prepareStaticInitializer(method: Method) {
val entryInstructions = buildList {
traceCollector = getNewCollector()
Expand Down Expand Up @@ -161,7 +178,7 @@ class SymbolicTraceInstrumenter(
)
}
super.visit(method)
method.body.entry.first().insertBefore(methodEntryInstructions)
method.body.entry.first().insertBefore(methodEntryInstructions.mapLocation())
}

override fun visitArrayLoadInst(inst: ArrayLoadInst) {
Expand Down Expand Up @@ -190,8 +207,8 @@ class SymbolicTraceInstrumenter(
)
)
}
inst.insertBefore(before)
inst.insertAfter(after)
inst.insertBefore(before.mapLocation())
inst.insertAfter(after.mapLocation())
}

override fun visitArrayStoreInst(inst: ArrayStoreInst) {
Expand Down Expand Up @@ -221,8 +238,8 @@ class SymbolicTraceInstrumenter(
)
)
}
inst.insertBefore(before)
inst.insertAfter(after)
inst.insertBefore(before.mapLocation())
inst.insertAfter(after.mapLocation())
}

override fun visitBinaryInst(inst: BinaryInst) {
Expand All @@ -247,7 +264,7 @@ class SymbolicTraceInstrumenter(
)
)
}
inst.insertAfter(instrumented)
inst.insertAfter(instrumented.mapLocation())
}

override fun visitBranchInst(inst: BranchInst) {
Expand All @@ -263,7 +280,7 @@ class SymbolicTraceInstrumenter(
"${inst.cond}".asValue
)
)
inst.insertBefore(instrumented)
inst.insertBefore(instrumented.mapLocation())
}

override fun visitCallInst(inst: CallInst) {
Expand Down Expand Up @@ -347,7 +364,7 @@ class SymbolicTraceInstrumenter(
)
)
}
inst.insertBefore(instrumented)
inst.insertBefore(instrumented.mapLocation())
}

override fun visitCastInst(inst: CastInst) {
Expand All @@ -373,8 +390,8 @@ class SymbolicTraceInstrumenter(
)
)
}
inst.insertBefore(before)
inst.insertAfter(after)
inst.insertBefore(before.mapLocation())
inst.insertAfter(after.mapLocation())
}

override fun visitCatchInst(inst: CatchInst) {
Expand All @@ -401,7 +418,7 @@ class SymbolicTraceInstrumenter(
}
}
}
inst.insertAfter(instrumented)
inst.insertAfter(instrumented.mapLocation())
}

override fun visitCmpInst(inst: CmpInst) {
Expand All @@ -425,7 +442,7 @@ class SymbolicTraceInstrumenter(
)
)
}
inst.insertAfter(instrumented)
inst.insertAfter(instrumented.mapLocation())
}

override fun visitEnterMonitorInst(inst: EnterMonitorInst) {
Expand All @@ -441,7 +458,7 @@ class SymbolicTraceInstrumenter(
inst.owner
)
)
inst.insertAfter(instrumented)
inst.insertAfter(instrumented.mapLocation())
}

override fun visitExitMonitorInst(inst: ExitMonitorInst) {
Expand All @@ -457,7 +474,7 @@ class SymbolicTraceInstrumenter(
inst.owner
)
)
inst.insertAfter(instrumented)
inst.insertAfter(instrumented.mapLocation())
}

override fun visitFieldLoadInst(inst: FieldLoadInst) {
Expand Down Expand Up @@ -496,8 +513,8 @@ class SymbolicTraceInstrumenter(
)
)
}
inst.insertBefore(before)
inst.insertAfter(after)
inst.insertBefore(before.mapLocation())
inst.insertAfter(after.mapLocation())
}

override fun visitFieldStoreInst(inst: FieldStoreInst) {
Expand Down Expand Up @@ -543,8 +560,8 @@ class SymbolicTraceInstrumenter(
)
)
}
inst.insertBefore(before)
inst.insertAfter(after)
inst.insertBefore(before.mapLocation())
inst.insertAfter(after.mapLocation())
}

override fun visitInstanceOfInst(inst: InstanceOfInst) {
Expand All @@ -567,7 +584,7 @@ class SymbolicTraceInstrumenter(
)
)
}
inst.insertAfter(instrumented)
inst.insertAfter(instrumented.mapLocation())
}

override fun visitInvokeDynamicInst(inst: InvokeDynamicInst) {
Expand Down Expand Up @@ -613,7 +630,7 @@ class SymbolicTraceInstrumenter(
)
)
}
inst.insertAfter(instrumented)
inst.insertAfter(instrumented.mapLocation())
}

override fun visitJumpInst(inst: JumpInst) {
Expand All @@ -624,7 +641,7 @@ class SymbolicTraceInstrumenter(
val instrumented = collectorClass.interfaceCall(
jumpMethod, traceCollector, listOf("$inst".asValue)
)
inst.insertBefore(instrumented)
inst.insertBefore(instrumented.mapLocation())
}

override fun visitNewArrayInst(inst: NewArrayInst) {
Expand Down Expand Up @@ -674,7 +691,7 @@ class SymbolicTraceInstrumenter(
)
)
}
inst.insertAfter(instrumented)
inst.insertAfter(instrumented.mapLocation())
}

override fun visitNewInst(inst: NewInst) {
Expand All @@ -685,13 +702,13 @@ class SymbolicTraceInstrumenter(
val instrumented = collectorClass.interfaceCall(
newMethod, traceCollector, listOf("$inst".asValue)
)
inst.insertAfter(instrumented)
inst.insertAfter(instrumented.mapLocation())
}

override fun visitPhiInst(inst: PhiInst) {
// if phi is part of a catch block, we need to handle the `CatchInst` first
if (inst.parent is CatchBlock) return
inst.insertAfter(buildPhi(inst))
inst.insertAfter(buildPhi(inst).mapLocation())
}

private fun buildPhi(inst: PhiInst): List<Instruction> = buildList {
Expand Down Expand Up @@ -732,7 +749,7 @@ class SymbolicTraceInstrumenter(
)
)
}
inst.insertBefore(instrumented)
inst.insertBefore(instrumented.mapLocation())
}

override fun visitSwitchInst(inst: SwitchInst) {
Expand All @@ -753,7 +770,7 @@ class SymbolicTraceInstrumenter(
)
)
}
inst.insertBefore(instrumented)
inst.insertBefore(instrumented.mapLocation())
}

override fun visitTableSwitchInst(inst: TableSwitchInst) {
Expand All @@ -774,7 +791,7 @@ class SymbolicTraceInstrumenter(
)
)
}
inst.insertBefore(instrumented)
inst.insertBefore(instrumented.mapLocation())
}

override fun visitThrowInst(inst: ThrowInst) {
Expand All @@ -791,7 +808,7 @@ class SymbolicTraceInstrumenter(
inst.throwable
)
)
inst.insertBefore(instrumented)
inst.insertBefore(instrumented.mapLocation())
}

override fun visitUnaryInst(inst: UnaryInst) {
Expand All @@ -818,8 +835,8 @@ class SymbolicTraceInstrumenter(
)
)
}
inst.insertBefore(before)
inst.insertAfter(after)
inst.insertBefore(before.mapLocation())
inst.insertAfter(after.mapLocation())
}

private fun addNullityConstraint(inst: Instruction, value: Value): List<Instruction> = buildList {
Expand Down Expand Up @@ -959,7 +976,7 @@ class SymbolicTraceInstrumenter(
) = method.specialCall(this, instance, args)

private fun Value.wrapped(list: MutableList<Instruction>): Value = when {
this.type.isPrimitive -> wrapValue(this).also {
this.type.isPrimitive -> wrapUpValue(this).also {
list += it
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,9 +646,11 @@ open class DescriptorBuilder : StringInfoContext() {
return string
}

fun klass(type: KexType): ObjectDescriptor {
fun klass(type: KexType): ObjectDescriptor = klass("$type")

fun klass(typeName: String): ObjectDescriptor {
val klass = `object`(KexJavaClass())
klass["name", KexString()] = string("$type")
klass["name", KexString()] = string(typeName)
return klass
}
}
Expand Down
Loading

0 comments on commit e09313d

Please sign in to comment.