-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
130 additions
and
22 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
src/main/java/org/jetbrains/kotlinx/lincheck/RecoverabilityTransformer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package org.jetbrains.kotlinx.lincheck | ||
|
||
import org.jetbrains.kotlinx.lincheck.TransformationClassLoader.ASM_API | ||
import org.jetbrains.kotlinx.lincheck.annotations.Recoverable | ||
import org.jetbrains.kotlinx.lincheck.nvm.CrashError | ||
import org.objectweb.asm.* | ||
import org.objectweb.asm.commons.GeneratorAdapter | ||
import org.objectweb.asm.commons.Method | ||
|
||
|
||
class RecoverabilityTransformer(cv: ClassVisitor) : ClassVisitor(ASM_API, cv) { | ||
private lateinit var name: String | ||
|
||
override fun visit( | ||
version: Int, | ||
access: Int, | ||
name: String?, | ||
signature: String?, | ||
superName: String?, | ||
interfaces: Array<out String>? | ||
) { | ||
super.visit(version, access, name, signature, superName, interfaces) | ||
this.name = name!! | ||
} | ||
|
||
|
||
override fun visitMethod( | ||
access: Int, | ||
name: String?, | ||
descriptor: String?, | ||
signature: String?, | ||
exceptions: Array<out String>? | ||
) = RecoverableMethodTransformer( | ||
super.visitMethod(access, name, descriptor, signature, exceptions), | ||
access, | ||
name, | ||
descriptor, | ||
this.name | ||
) | ||
} | ||
|
||
class RecoverableMethodTransformer( | ||
mv: MethodVisitor, | ||
access: Int, | ||
name: String?, | ||
private val descriptor: String?, | ||
private val className: String | ||
) : GeneratorAdapter(ASM_API, mv, access, name, descriptor) { | ||
private var shouldTransform = false | ||
lateinit var beforeName: String | ||
lateinit var recoverName: String | ||
|
||
override fun visitAnnotation(descriptor: String?, visible: Boolean): AnnotationVisitor { | ||
val av = super.visitAnnotation(descriptor, visible) | ||
if (descriptor != Type.getDescriptor(Recoverable::class.java)) return av | ||
shouldTransform = true | ||
return object : AnnotationVisitor(ASM_API, av) { | ||
override fun visit(name: String?, value: Any?) { | ||
super.visit(name, value) | ||
if (name == "recoverMethod") { | ||
recoverName = value as String | ||
} else if (name == "beforeMethod") { | ||
beforeName = value as String | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun visitCode() { | ||
super.visitCode() | ||
if (!shouldTransform) return | ||
val loop = Label() | ||
val startLabel = Label() | ||
val endLabel = Label() | ||
val catchLabel = Label() | ||
visitTryCatchBlock(startLabel, endLabel, catchLabel, Type.getInternalName(CrashError::class.java)) | ||
|
||
val resultIndex = newLocal(Type.BOOLEAN_TYPE) | ||
push(false) | ||
storeLocal(resultIndex) | ||
|
||
visitLabel(loop) | ||
visitLabel(startLabel) | ||
visitVarInsn(Opcodes.ALOAD, 0) | ||
visitVarInsn(Opcodes.ALOAD, 1) // get first parameter | ||
invokeVirtual(Type.getType("L$className;"), Method(beforeName, "(I)V")) | ||
push(true) | ||
storeLocal(resultIndex) | ||
visitLabel(endLabel) | ||
visitLabel(catchLabel) | ||
loadLocal(resultIndex) | ||
ifZCmp(EQ, loop) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters