Skip to content

Commit 25a0527

Browse files
Don't instrument args reads at code line 0 (722). (#724)
It is code inserted by compiler to check notnull-ness and other invariants.
1 parent e179228 commit 25a0527

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/jvm/main/org/jetbrains/kotlinx/lincheck/transformation/LincheckClassVisitor.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ internal class LincheckClassVisitor(
8383
fun MethodVisitor.newAdapter() = GeneratorAdapter(this, access, methodName, desc)
8484
fun MethodVisitor.newNonRemappingAdapter() = GeneratorAdapterWithoutLocals(this, access, methodName, desc)
8585

86+
val isStatic = access and ACC_STATIC != 0
87+
8688
if (instrumentationMode == STRESS) {
8789
return if (methodName != "<clinit>" && methodName != "<init>") {
8890
CoroutineCancellabilitySupportTransformer(mv, access, className, methodName, desc)
@@ -118,7 +120,7 @@ internal class LincheckClassVisitor(
118120
analyzerAdapter
119121
}
120122
val locals: Map<Int, List<LocalVariableInfo>> = methods[methodName + desc]?.variables ?: emptyMap()
121-
mv = LocalVariablesAccessTransformer(fileName, className, methodName, mv.newAdapter(), locals)
123+
mv = LocalVariablesAccessTransformer(fileName, className, methodName, mv.newAdapter(), desc, isStatic, locals)
122124

123125
// Inline method call transformer relies on the original variables' indices, so it should go before (in FIFO order)
124126
// all transformers which can create local variables.
@@ -263,7 +265,7 @@ internal class LincheckClassVisitor(
263265
aa
264266
}
265267
val locals: Map<Int, List<LocalVariableInfo>> = methods[methodName + desc]?.variables ?: emptyMap()
266-
mv = LocalVariablesAccessTransformer(fileName, className, methodName, mv.newAdapter(), locals)
268+
mv = LocalVariablesAccessTransformer(fileName, className, methodName, mv.newAdapter(), desc, isStatic, locals)
267269
// Inline method call transformer relies on the original variables' indices, so it should go before (in FIFO order)
268270
// all transformers which can create local variables.
269271
// We cannot use trick with
@@ -308,6 +310,8 @@ internal open class ManagedStrategyMethodVisitor(
308310
adapter.push(codeLocationId)
309311
}
310312

313+
protected fun isKnownLineNumber(): Boolean = lineNumber > 0
314+
311315
override fun visitLineNumber(line: Int, start: Label) {
312316
lineNumber = line
313317
super.visitLineNumber(line, start)

src/jvm/main/org/jetbrains/kotlinx/lincheck/transformation/transformers/LocalVariablesAccessTransformer.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.jetbrains.kotlinx.lincheck.transformation.transformers
1212

13+
import org.jetbrains.kotlinx.lincheck.tracedata.Types.convertAsmMethodType
1314
import org.jetbrains.kotlinx.lincheck.tracedata.VariableDescriptor
1415
import org.jetbrains.kotlinx.lincheck.tracedata.variableCache
1516
import org.jetbrains.kotlinx.lincheck.transformation.*
@@ -23,10 +24,13 @@ internal class LocalVariablesAccessTransformer(
2324
className: String,
2425
methodName: String,
2526
adapter: GeneratorAdapter,
27+
desc: String,
28+
isStatic: Boolean,
2629
private val locals: Map<Int, List<LocalVariableInfo>>,
2730
) : ManagedStrategyMethodVisitor(fileName, className, methodName, adapter) {
2831
private var turnoffTransform = false
2932
private val visitedLabels = HashSet<Label>()
33+
private val numberOfLocals = convertAsmMethodType(desc).argumentTypes.size + if (isStatic) 0 else 1
3034

3135
override fun visitLabel(label: Label) = adapter.run {
3236
visitedLabels += label
@@ -94,6 +98,12 @@ internal class LocalVariablesAccessTransformer(
9498
}
9599

96100
private fun visitReadVarInsn(localVariableInfo: LocalVariableInfo, opcode: Int, varIndex: Int) = adapter.run {
101+
// Skip variable read if it is in an unknown line of code and variable is argument
102+
// It can be code inserted by compiler to check Null invariant
103+
if (!isKnownLineNumber() && varIndex < numberOfLocals) {
104+
visitVarInsn(opcode, varIndex)
105+
return
106+
}
97107
invokeIfInAnalyzedCode(
98108
original = {
99109
visitVarInsn(opcode, varIndex)

0 commit comments

Comments
 (0)