Skip to content

Commit 7f557e9

Browse files
authored
Preparation for atomicfu JVM IR plugin application in 1.7.20 (#3455)
* Fix for integration-testing:mavenTest: exclude metadata when checking classes bytecode for ATOMIC_REF * Moved SegmentBasedQueue implementation to the main module * Removed SegmentListTest
1 parent 5322c8d commit 7f557e9

File tree

7 files changed

+37
-51
lines changed

7 files changed

+37
-51
lines changed

integration-testing/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ java {
1818

1919
dependencies {
2020
testImplementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
21+
testImplementation "org.ow2.asm:asm:$asm_version"
2122
}
2223

2324
sourceSets {

integration-testing/gradle.properties

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
kotlin_version=1.7.10
22
coroutines_version=1.6.4-SNAPSHOT
3+
asm_version=9.3
34

45
kotlin.code.style=official

integration-testing/src/mavenTest/kotlin/MavenPublicationAtomicfuValidator.kt

+33-8
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44

55
package kotlinx.coroutines.validator
66

7-
import org.junit.*
8-
import org.junit.Assert.assertTrue
7+
import org.junit.Test
8+
import org.objectweb.asm.*
9+
import org.objectweb.asm.ClassReader.*
10+
import org.objectweb.asm.ClassWriter.*
11+
import org.objectweb.asm.Opcodes.*
912
import java.util.jar.*
13+
import kotlin.test.*
1014

1115
class MavenPublicationAtomicfuValidator {
1216
private val ATOMIC_FU_REF = "Lkotlinx/atomicfu/".toByteArray()
17+
private val KOTLIN_METADATA_DESC = "Lkotlin/Metadata;"
1318

1419
@Test
1520
fun testNoAtomicfuInClasspath() {
@@ -34,19 +39,39 @@ class MavenPublicationAtomicfuValidator {
3439
for (e in entries()) {
3540
if (!e.name.endsWith(".class")) continue
3641
val bytes = getInputStream(e).use { it.readBytes() }
37-
loop@for (i in 0 until bytes.size - ATOMIC_FU_REF.size) {
38-
for (j in 0 until ATOMIC_FU_REF.size) {
39-
if (bytes[i + j] != ATOMIC_FU_REF[j]) continue@loop
40-
}
42+
// The atomicfu compiler plugin does not remove atomic properties from metadata,
43+
// so for now we check that there are no ATOMIC_FU_REF left in the class bytecode excluding metadata.
44+
// This may be reverted after the fix in the compiler plugin transformer (for Kotlin 1.8.0).
45+
val outBytes = bytes.eraseMetadata()
46+
if (outBytes.checkBytes()) {
4147
foundClasses += e.name // report error at the end with all class names
42-
break@loop
4348
}
4449
}
4550
if (foundClasses.isNotEmpty()) {
4651
error("Found references to atomicfu in jar file $name in the following class files: ${
47-
foundClasses.joinToString("") { "\n\t\t" + it }
52+
foundClasses.joinToString("") { "\n\t\t" + it }
4853
}")
4954
}
5055
close()
5156
}
57+
58+
private fun ByteArray.checkBytes(): Boolean {
59+
loop@for (i in 0 until this.size - ATOMIC_FU_REF.size) {
60+
for (j in 0 until ATOMIC_FU_REF.size) {
61+
if (this[i + j] != ATOMIC_FU_REF[j]) continue@loop
62+
}
63+
return true
64+
}
65+
return false
66+
}
67+
68+
private fun ByteArray.eraseMetadata(): ByteArray {
69+
val cw = ClassWriter(COMPUTE_MAXS or COMPUTE_FRAMES)
70+
ClassReader(this).accept(object : ClassVisitor(ASM9, cw) {
71+
override fun visitAnnotation(descriptor: String?, visible: Boolean): AnnotationVisitor? {
72+
return if (descriptor == KOTLIN_METADATA_DESC) null else super.visitAnnotation(descriptor, visible)
73+
}
74+
}, SKIP_FRAMES)
75+
return cw.toByteArray()
76+
}
5277
}

integration-testing/src/mavenTest/kotlin/MavenPublicationVersionValidator.kt

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
package kotlinx.coroutines.validator
66

7-
import org.junit.*
87
import org.junit.Test
98
import java.util.jar.*
109
import kotlin.test.*

kotlinx-coroutines-core/jvm/test/internal/SegmentListTest.kt

-41
This file was deleted.

kotlinx-coroutines-core/jvm/test/internal/SegmentQueueTest.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package kotlinx.coroutines.internal
22

33
import kotlinx.coroutines.*
4+
import kotlinx.coroutines.internal.*
45
import org.junit.Test
56
import java.util.*
67
import java.util.concurrent.CyclicBarrier
@@ -106,4 +107,4 @@ class SegmentQueueTest : TestBase() {
106107
}.forEach { it.join() }
107108
assertEquals(2, q.numberOfSegments)
108109
}
109-
}
110+
}

0 commit comments

Comments
 (0)