eed3si9n / jarjar-abrams

an experimental Scala extension of Jar Jar Links
27 stars 21 forks source link

Fix random "UTF8 string too large" exception in ScalaSigAnnotationVisitor #24

Closed vladimirkl closed 2 years ago

vladimirkl commented 2 years ago

This PR fixes random "UTF8 string too large" exception during shading, which I encountered in my closed source project. This exception is very hard to reproduce because it occurs during processing classes that have ScalaLongSignature annotation and annotation value should have zero bytes in certain places, which is not deterministic. The cause of this bug is nextChunk function in ScalaSigAnnotationVisitor class which can produce chunks equal to 65536 bytes breaking org.objectweb.asm.ByteVector#putUTF8. Stack trace for exception is following:

java.lang.IllegalArgumentException: UTF8 string too large
     at org.objectweb.asm.ByteVector.encodeUtf8(ByteVector.java:298)
     at org.objectweb.asm.ByteVector.putUTF8(ByteVector.java:265)
     at org.objectweb.asm.SymbolTable.addConstantUtf8(SymbolTable.java:774)
     at org.objectweb.asm.AnnotationWriter.visit(AnnotationWriter.java:198)
     at com.eed3si9n.jarjarabrams.scalasig.ScalaSigAnnotationVisitor.rewriteAnnotation(ScalaSigAnnotationVisitor.scala:91)
     at com.eed3si9n.jarjarabrams.scalasig.ScalaSigAnnotationVisitor.visitEnd(ScalaSigAnnotationVisitor.scala:47)
     at org.objectweb.asm.ClassReader.readElementValues(ClassReader.java:3003)
     at org.objectweb.asm.ClassReader.readElementValue(ClassReader.java:3173)
     at org.objectweb.asm.ClassReader.readElementValues(ClassReader.java:2993)
     at org.objectweb.asm.ClassReader.accept(ClassReader.java:608)
     at org.objectweb.asm.ClassReader.accept(ClassReader.java:424)
     at com.eed3si9n.jarjar.ScalaSigProcessor.process(ScalaSigProcessor.scala:15)
     at com.eed3si9n.jarjar.util.JarProcessorChain.process(JarProcessorChain.java:38)
     at com.eed3si9n.jarjar.JJProcessor.process(JJProcessor.scala:108)
     at com.eed3si9n.jarjarabrams.Shader$.$anonfun$bytecodeShader$6(Shader.scala:75)
     at com.eed3si9n.jarjarabrams.Shader$.$anonfun$shadeDirectory$4(Shader.scala:21

This PR extracts nextChunk to standalone function, fixes the bug and adds a test for it. Unfortunately cannot include actual class causing error because it is from closed source project and there is no deterministic way to recreate it.

vladimirkl commented 2 years ago

@eed3si9n Thank you for merging this. Is it possible to make a patch release for this library and sbt-assembly in near future?