Write a copy of SMAP to a new annotation

To make it available for dynamically attached JVMTI agents.

`@SourceDebugExtension` annotation value is equal to the
SourceDebugExtension attribute value, which is checked now for all box
tests. The difference is that the annotation stored in the constant
pool, which is available for dynamically attached JVMTI agents.

 #KT-53438 Fixed
This commit is contained in:
Alexander Udalov
2022-07-13 14:10:08 +02:00
parent 1e6f7f6d58
commit 87d3ce6ded
7 changed files with 109 additions and 13 deletions
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.codegen.inline.FileMapping;
import org.jetbrains.kotlin.codegen.inline.SMAPBuilder;
import org.jetbrains.kotlin.codegen.inline.SourceMapper;
import org.jetbrains.kotlin.codegen.serialization.JvmSerializationBindings;
import org.jetbrains.kotlin.load.java.JvmAnnotationNames;
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin;
import org.jetbrains.org.objectweb.asm.*;
@@ -116,6 +117,14 @@ public abstract class AbstractClassBuilder implements ClassBuilder {
@Override
public void done() {
getVisitor().visitSource(sourceName, debugInfo);
if (debugInfo != null) {
AnnotationVisitor v =
getVisitor().visitAnnotation(JvmAnnotationNames.SOURCE_DEBUG_EXTENSION_DESC, false).visitArray("value");
for (String part : CodegenUtilKt.splitStringConstant(debugInfo)) {
v.visit(null, part);
}
v.visitEnd();
}
getVisitor().visitEnd();
}
@@ -70,15 +70,22 @@ class AnonymousObjectTransformer(
}
override fun visitAnnotation(desc: String, visible: Boolean): AnnotationVisitor? {
if (desc == JvmAnnotationNames.METADATA_DESC) {
// Empty inner class info because no inner classes are used in kotlin.Metadata and its arguments
val innerClassesInfo = FileBasedKotlinClass.InnerClassesInfo()
return FileBasedKotlinClass.convertAnnotationVisitor(metadataReader, desc, innerClassesInfo)
} else if (desc == DEBUG_METADATA_ANNOTATION_ASM_TYPE.descriptor) {
debugMetadataAnnotation = AnnotationNode(desc)
return debugMetadataAnnotation
when (desc) {
JvmAnnotationNames.METADATA_DESC -> {
// Empty inner class info because no inner classes are used in kotlin.Metadata and its arguments
val innerClassesInfo = FileBasedKotlinClass.InnerClassesInfo()
return FileBasedKotlinClass.convertAnnotationVisitor(metadataReader, desc, innerClassesInfo)
}
DEBUG_METADATA_ANNOTATION_ASM_TYPE.descriptor -> {
debugMetadataAnnotation = AnnotationNode(desc)
return debugMetadataAnnotation
}
JvmAnnotationNames.SOURCE_DEBUG_EXTENSION_DESC -> {
// The new value of @SourceDebugExtension will be written along with the new SMAP via ClassBuilder.visitSMAP.
return null
}
else -> return classBuilder.newAnnotation(desc, visible)
}
return classBuilder.newAnnotation(desc, visible)
}
override fun visitMethod(
@@ -10,8 +10,10 @@ import org.jetbrains.kotlin.backend.common.output.OutputFile
import org.jetbrains.kotlin.codegen.inline.RangeMapping
import org.jetbrains.kotlin.codegen.inline.SMAPParser
import org.jetbrains.kotlin.codegen.inline.toRange
import org.jetbrains.kotlin.load.java.JvmAnnotationNames
import org.jetbrains.kotlin.test.Assertions
import org.jetbrains.kotlin.utils.keysToMap
import org.jetbrains.org.objectweb.asm.AnnotationVisitor
import org.jetbrains.org.objectweb.asm.ClassReader
import org.jetbrains.org.objectweb.asm.ClassVisitor
import org.jetbrains.org.objectweb.asm.Opcodes
@@ -21,16 +23,55 @@ object CommonSMAPTestUtil {
fun extractSMAPFromClasses(outputFiles: Iterable<OutputFile>): List<SMAPAndFile> {
return outputFiles.map { outputFile ->
var debugInfo: String? = null
var sdeAnnotationValue: String? = null
ClassReader(outputFile.asByteArray()).accept(object : ClassVisitor(Opcodes.API_VERSION) {
override fun visitSource(source: String?, debug: String?) {
debugInfo = debug
}
override fun visitAnnotation(descriptor: String, visible: Boolean): AnnotationVisitor? {
if (descriptor != JvmAnnotationNames.SOURCE_DEBUG_EXTENSION_DESC) return super.visitAnnotation(descriptor, visible)
return object : AnnotationVisitor(Opcodes.API_VERSION) {
override fun visitArray(name: String): AnnotationVisitor? {
if (name != "value") return super.visitArray(name)
check(sdeAnnotationValue == null) { outputFile.relativePath }
return object : AnnotationVisitor(Opcodes.API_VERSION) {
val result = mutableListOf<String>()
override fun visit(name: String?, value: Any?) {
result.add(value as String)
}
override fun visitEnd() {
sdeAnnotationValue = result.joinToString("")
}
}
}
}
}
}, 0)
checkSmapVsAnnotation(outputFile.relativePath, debugInfo, sdeAnnotationValue)
SMAPAndFile(debugInfo, outputFile.sourceFiles.single(), outputFile.relativePath)
}
}
private fun checkSmapVsAnnotation(relativePath: String, debugInfo: String?, sdeAnnotationValue: String?) {
if (debugInfo == sdeAnnotationValue) return
if (debugInfo == null) {
error("@SourceDebugExtension is incorrectly generated for a class without SMAP: $relativePath")
}
if (sdeAnnotationValue == null) {
error("Missing @SourceDebugExtension annotation for a class with SMAP: $relativePath")
}
error(
"SMAP and @SourceDebugExtension value differs for $relativePath.\n" +
"SMAP:\n===\n$debugInfo\n===\n@SourceDebugExtension:\n===\n$sdeAnnotationValue\n"
)
}
fun checkNoConflictMappings(compiledSmap: List<SMAPAndFile>?, assertions: Assertions) {
if (compiledSmap == null) return
@@ -250,7 +250,9 @@ class BytecodeListingTextCollectingVisitor(
override fun visitAnnotation(desc: String, visible: Boolean): AnnotationVisitor? =
visitAnnotationImpl { args ->
classAnnotations.add("@" + renderAnnotation(desc, args))
renderClassAnnotation(desc, args)?.let {
classAnnotations.add("@$it")
}
}
private fun visitAnnotationImpl(end: (List<String>) -> Unit): AnnotationVisitor? =
@@ -300,12 +302,25 @@ class BytecodeListingTextCollectingVisitor(
private fun renderAnnotation(desc: String, args: List<String>): String {
val name = Type.getType(desc).className
return if (args.isEmpty() || desc == "Lkotlin/Metadata;" || desc == "Lkotlin/coroutines/jvm/internal/DebugMetadata;")
name
else
"$name(${args.joinToString(", ")})"
return renderAnnotationArguments(args, name)
}
private fun renderClassAnnotation(desc: String, args: List<String>): String? {
// Don't render @SourceDebugExtension to avoid difference in text dumps of full and light analysis, because the compiler never
// generates it in the light analysis mode (since method bodies are not analyzed and we don't know if there's an inline call there).
if (desc == "Lkotlin/jvm/internal/SourceDebugExtension;") return null
val name = Type.getType(desc).className
// Don't render contents of @Metadata/@DebugMetadata because they're binary.
if (desc == "Lkotlin/Metadata;" || desc == "Lkotlin/coroutines/jvm/internal/DebugMetadata;") return name
return renderAnnotationArguments(args, name)
}
private fun renderAnnotationArguments(args: List<String>, name: String): String =
if (args.isEmpty()) name else "$name(${args.joinToString(", ")})"
override fun visit(version: Int, access: Int, name: String, signature: String?, superName: String?, interfaces: Array<out String>?) {
className = name
classAccess = access