Skip to content

Preparation for atomicfu JVM IR plugin application in 1.7.20 #3455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration-testing/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
kotlin_version=1.7.10
coroutines_version=1.6.4-SNAPSHOT
asm_version=9.3

kotlin.code.style=official
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

package kotlinx.coroutines.validator

import org.junit.*
import org.junit.Assert.assertTrue
import org.junit.Test
import org.objectweb.asm.*
import org.objectweb.asm.ClassReader.*
import org.objectweb.asm.ClassWriter.*
import org.objectweb.asm.Opcodes.*
import java.util.jar.*
import kotlin.test.*

class MavenPublicationAtomicfuValidator {
private val ATOMIC_FU_REF = "Lkotlinx/atomicfu/".toByteArray()
private val KOTLIN_METADATA_DESC = "Lkotlin/Metadata;"

@Test
fun testNoAtomicfuInClasspath() {
Expand All @@ -33,20 +38,38 @@ class MavenPublicationAtomicfuValidator {
val foundClasses = mutableListOf<String>()
for (e in entries()) {
if (!e.name.endsWith(".class")) continue
// check the bytecode of the class for ATOMIC_FU_REF excluding metadata
val bytes = getInputStream(e).use { it.readBytes() }
loop@for (i in 0 until bytes.size - ATOMIC_FU_REF.size) {
for (j in 0 until ATOMIC_FU_REF.size) {
if (bytes[i + j] != ATOMIC_FU_REF[j]) continue@loop
}
val outBytes = bytes.eraseMetadata()
if (outBytes.checkBytes()) {
foundClasses += e.name // report error at the end with all class names
break@loop
}
}
if (foundClasses.isNotEmpty()) {
error("Found references to atomicfu in jar file $name in the following class files: ${
foundClasses.joinToString("") { "\n\t\t" + it }
foundClasses.joinToString("") { "\n\t\t" + it }
}")
}
close()
}

private fun ByteArray.checkBytes(): Boolean {
loop@for (i in 0 until this.size - ATOMIC_FU_REF.size) {
for (j in 0 until ATOMIC_FU_REF.size) {
if (this[i + j] != ATOMIC_FU_REF[j]) continue@loop
}
return true
}
return false
}

private fun ByteArray.eraseMetadata(): ByteArray {
val cw = ClassWriter(COMPUTE_MAXS or COMPUTE_FRAMES)
ClassReader(this).accept(object : ClassVisitor(ASM9, cw) {
override fun visitAnnotation(descriptor: String?, visible: Boolean): AnnotationVisitor? {
return if (descriptor == KOTLIN_METADATA_DESC) null else super.visitAnnotation(descriptor, visible)
}
}, SKIP_FRAMES)
return cw.toByteArray()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package kotlinx.coroutines.validator

import org.junit.*
import org.junit.Test
import java.util.jar.*
import kotlin.test.*
Expand Down
81 changes: 40 additions & 41 deletions kotlinx-coroutines-core/jvm/test/internal/SegmentListTest.kt
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
package kotlinx.coroutines.internal

import kotlinx.atomicfu.*
import org.junit.Test
import kotlin.test.*

class SegmentListTest {
@Test
fun testRemoveTail() {
val initialSegment = TestSegment(0, null, 2)
val head = AtomicRefHolder(initialSegment)
val tail = AtomicRefHolder(initialSegment)
val s1 = tail.ref.findSegmentAndMoveForward(1, tail.ref.value, ::createTestSegment).segment
assertFalse(s1.removed)
tail.ref.value.onSlotCleaned()
assertFalse(s1.removed)
head.ref.findSegmentAndMoveForward(2, head.ref.value, ::createTestSegment)
assertFalse(s1.removed)
tail.ref.findSegmentAndMoveForward(2, head.ref.value, ::createTestSegment)
assertTrue(s1.removed)
}

@Test
fun testClose() {
val initialSegment = TestSegment(0, null, 2)
val head = AtomicRefHolder(initialSegment)
val tail = AtomicRefHolder(initialSegment)
tail.ref.findSegmentAndMoveForward(1, tail.ref.value, ::createTestSegment)
assertEquals(tail.ref.value, tail.ref.value.close())
assertTrue(head.ref.findSegmentAndMoveForward(2, head.ref.value, ::createTestSegment).isClosed)
}
}

private class AtomicRefHolder<T>(initialValue: T) {
val ref = atomic(initialValue)
}

private class TestSegment(id: Long, prev: TestSegment?, pointers: Int) : Segment<TestSegment>(id, prev, pointers) {
override val maxSlots: Int get() = 1
}
private fun createTestSegment(id: Long, prev: TestSegment?) = TestSegment(id, prev, 0)
//package kotlinx.coroutines.internal
//
//import kotlinx.atomicfu.*
//import kotlin.test.*
//
//class SegmentListTest {
// @Test
// fun testRemoveTail() {
// val initialSegment = TestSegment(0, null, 2)
// val head = AtomicRefHolder(initialSegment)
// val tail = AtomicRefHolder(initialSegment)
// val s1 = tail.ref.findSegmentAndMoveForward(1, tail.ref.value, ::createTestSegment).segment
// assertFalse(s1.removed)
// tail.ref.value.onSlotCleaned()
// assertFalse(s1.removed)
// head.ref.findSegmentAndMoveForward(2, head.ref.value, ::createTestSegment)
// assertFalse(s1.removed)
// tail.ref.findSegmentAndMoveForward(2, head.ref.value, ::createTestSegment)
// assertTrue(s1.removed)
// }
//
// @Test
// fun testClose() {
// val initialSegment = TestSegment(0, null, 2)
// val head = AtomicRefHolder(initialSegment)
// val tail = AtomicRefHolder(initialSegment)
// tail.ref.findSegmentAndMoveForward(1, tail.ref.value, ::createTestSegment)
// assertEquals(tail.ref.value, tail.ref.value.close())
// assertTrue(head.ref.findSegmentAndMoveForward(2, head.ref.value, ::createTestSegment).isClosed)
// }
//}
//
//private class AtomicRefHolder<T>(initialValue: T) {
// val ref = atomic(initialValue)
//}
//
//private class TestSegment(id: Long, prev: TestSegment?, pointers: Int) : Segment<TestSegment>(id, prev, pointers) {
// override val maxSlots: Int get() = 1
//}
//private fun createTestSegment(id: Long, prev: TestSegment?) = TestSegment(id, prev, 0)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kotlinx.coroutines.internal

import kotlinx.coroutines.*
import kotlinx.coroutines.internal.*
import org.junit.Test
import java.util.*
import java.util.concurrent.CyclicBarrier
Expand Down Expand Up @@ -106,4 +107,4 @@ class SegmentQueueTest : TestBase() {
}.forEach { it.join() }
assertEquals(2, q.numberOfSegments)
}
}
}