Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Ensure that Scala 2 pickle attributes are also removed
  • Loading branch information
WojciechMazur committed Dec 30, 2025
commit e831daadc6a1915a76886b285faf1b3ee224703c
25 changes: 17 additions & 8 deletions project/ScalaLibraryPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ object ScalaLibraryPlugin extends AutoPlugin {
"Lscala/reflect/ScalaLongSignature;"
)

/** Scala 2 attribute names that should be stripped from class files */
private val Scala2PickleAttributes = Set("ScalaSig", "ScalaInlineInfo")

/** Check if an annotation descriptor is a Scala 2 pickle annotation */
private def isScala2PickleAnnotation(descriptor: String): Boolean =
Scala2PickleAnnotations.contains(descriptor)
Expand Down Expand Up @@ -142,9 +145,9 @@ object ScalaLibraryPlugin extends AutoPlugin {
val writer = new ClassWriter(0)
// Remove Scala 2 pickles and Scala signatures
val visitor = new ClassVisitor(Opcodes.ASM9, writer) {
override def visitAttribute(attr: Attribute): Unit = attr.`type` match {
case "ScalaSig" | "ScalaInlineInfo" => ()
case _ => super.visitAttribute(attr)
override def visitAttribute(attr: Attribute): Unit = {
val shouldRemove = Scala2PickleAttributes.contains(attr.`type`)
if (!shouldRemove) super.visitAttribute(attr)
}

override def visitAnnotation(desc: String, visible: Boolean): AnnotationVisitor =
Expand Down Expand Up @@ -204,20 +207,27 @@ object ScalaLibraryPlugin extends AutoPlugin {
output
}

/** Check if class file bytecode contains Scala 2 pickle annotations */
/** Check if class file bytecode contains Scala 2 pickle annotations or attributes */
private def hasScala2Pickles(bytes: Array[Byte]): Boolean = {
var found = false
var hasPickleAnnotation = false
var hasScalaSigAttr = false
var hasScalaInlineInfoAttr = false
val visitor = new ClassVisitor(Opcodes.ASM9) {
override def visitAnnotation(desc: String, visible: Boolean): AnnotationVisitor = {
if (isScala2PickleAnnotation(desc)) found = true
if (isScala2PickleAnnotation(desc)) hasPickleAnnotation = true
null
}
override def visitAttribute(attr: Attribute): Unit =
if (Scala2PickleAttributes.contains(attr.`type`)) attr.`type` match {
case "ScalaSig" => hasScalaSigAttr = true
case "ScalaInlineInfo" => hasScalaInlineInfoAttr = true
}
}
new ClassReader(bytes).accept(
visitor,
ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES
)
found
hasPickleAnnotation || hasScalaSigAttr || hasScalaInlineInfoAttr
}

def validateNoScala2Pickles(jar: File): Unit = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't validate that ScalaSig and ScalaInlineInfo attributes were removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've adjusted it so that both of these are now validated

Expand Down Expand Up @@ -388,7 +398,6 @@ object ScalaLibraryPlugin extends AutoPlugin {
}
}


/** Custom ASM Attribute for TASTY that can be written to class files */
private class TastyAttribute(val uuid: Array[Byte]) extends Attribute("TASTY") {
override def write(classWriter: ClassWriter, code: Array[Byte], codeLength: Int, maxStack: Int, maxLocals: Int): ByteVector = {
Expand Down