Skip to content

Commit

Permalink
Replace AtomicLong.VMSupportsCS8() with a stub (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndkoval authored Mar 20, 2024
1 parent a0b1e19 commit 4a2dbd5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ internal class LincheckClassVisitor(
signature: String?,
exceptions: Array<String>?
): MethodVisitor {
if (access and ACC_NATIVE != 0 && methodName == "VMSupportsCS8") {
// Replace native method VMSupportsCS8 in AtomicLong with our stub.
// TODO: remove this code when javaagents are merged.
val mv = super.visitMethod(access xor ACC_NATIVE, methodName, desc, signature, exceptions)
return VMSupportsCS8MethodGenerator(GeneratorAdapter(mv, access xor ACC_NATIVE, methodName, desc))
}
var mv = super.visitMethod(access, methodName, desc, signature, exceptions)
if (access and ACC_NATIVE != 0) return mv
if (transformationMode == STRESS || transformationMode == VERIFICATION) {
Expand Down Expand Up @@ -108,6 +114,21 @@ internal class LincheckClassVisitor(
return mv
}

/**
* Generates body of a native method `VMSupportsCS8()`.
* Native methods in java.util can not be transformed, so we should replace them with stubs.
* TODO: remove this code when javaagents are merged.
*/
private class VMSupportsCS8MethodGenerator(val adapter: GeneratorAdapter) : MethodVisitor(ASM_API, null) {
override fun visitEnd() = adapter.run {
visitCode()
push(true) // suppose that we always have CAS for Long
returnValue()
visitMaxs(1, 0)
visitEnd()
}
}

private class CoroutineCancellabilitySupportMethodTransformer(
mv: MethodVisitor,
access: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Lincheck
*
* Copyright (C) 2019 - 2024 JetBrains s.r.o.
*
* This Source Code Form is subject to the terms of the
* Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
* with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.jetbrains.kotlinx.lincheck_test.transformation

import org.jetbrains.kotlinx.lincheck.annotations.Operation
import org.jetbrains.kotlinx.lincheck_test.*
import java.util.concurrent.atomic.AtomicLong

/**
* Checks that the AtomicLong.VMSupportsCS8() native method
* is correctly transformed.
*/
class AtomicLongTest : AbstractLincheckTest() {
val counter = AtomicLong()

@Operation
fun inc() = counter.incrementAndGet()
}

0 comments on commit 4a2dbd5

Please sign in to comment.