Skip to content
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

Very large string literals generate class files with errors #7527

Open
scabug opened this issue May 29, 2013 · 6 comments
Open

Very large string literals generate class files with errors #7527

scabug opened this issue May 29, 2013 · 6 comments
Labels

Comments

@scabug
Copy link

scabug commented May 29, 2013

When attempting to run the attached file with a large (>65535 character) String literal, I get the following error:

Exception in thread "main" java.lang.ClassFormatError: Unknown constant tag 32 in class file LongStringLiteral$delayedInit$body
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
	at LongStringLiteral$.<init>(LongStringLiteral.scala:1)
	at LongStringLiteral$.<clinit>(LongStringLiteral.scala)
	at LongStringLiteral.main(LongStringLiteral.scala)
@scabug
Copy link
Author

scabug commented May 29, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7527?orig=1
Reporter: Matt Russell (mattrussell)
Affected Versions: 2.10.1
Attachments:

@scabug
Copy link
Author

scabug commented Mar 14, 2014

@Blaisorblade said:
I've just run into (I believe) the same bug.
Scalac should give an error instead of blindly outputting the string. Without inspecting the code, I'd claim that if classfiles are only generated with ASM, ASM itself should error out with an exception (and Scalac should catch it and report it appropriately).

According to the JVM spec, the byte length of strings literals in class files is stored in a 2-byte field, so if the byte length is greater than 65536 it won't fit. The character length might even be less while still triggering the problem.
http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4.7

@scabug
Copy link
Author

scabug commented Jul 23, 2015

John Thywissen (jthywiss) said:
As of Scala 2.11.7, as embedded in Scala IDE 4.1.0.v-2_11-201505250839-aeb633d:

A very long triple quoted string results in scala.tools.asm throwing an IllegalArgumentException, which is reported in the UI as:
Error in Scala compiler: null
SBT builder crashed while compiling. The error message is 'null'. Check Error Log for details.

java.lang.IllegalArgumentException
at scala.tools.asm.ByteVector.putUTF8(ByteVector.java:213)
at scala.tools.asm.ClassWriter.newUTF8(ClassWriter.java:1092)
at scala.tools.asm.ClassWriter.newString(ClassWriter.java:1525)
at scala.tools.asm.ClassWriter.newConstItem(ClassWriter.java:1042)
......

@scabug
Copy link
Author

scabug commented Jul 24, 2015

@Blaisorblade said:
Can you confirm this happens for literals of more than 65535 bytes? If so, it's an evolution of the same underlying problem: ASM now rejects such illegal strings, but the compiler does not handle the situation gracefully.

@scabug
Copy link
Author

scabug commented Jul 24, 2015

@som-snytt said:
Yes, it happens at 64k chars.

$ scalac -d /tmp LongStringLiteral.scala 
error: java.lang.IllegalArgumentException
	at scala.tools.asm.ByteVector.encodeUTF8(ByteVector.java:273)
	at scala.tools.asm.ByteVector.putUTF8(ByteVector.java:234)
	at scala.tools.asm.ClassWriter.newUTF8(ClassWriter.java:1092)
	at scala.tools.asm.ClassWriter.newString(ClassWriter.java:1525)
	at scala.tools.asm.ClassWriter.newConstItem(ClassWriter.java:1042)
	at scala.tools.asm.MethodWriter.visitLdcInsn(MethodWriter.java:1134)
	at scala.tools.nsc.backend.jvm.GenASM$JPlainBuilder.genConstant(GenASM.scala:1578)
	at scala.tools.nsc.backend.jvm.GenASM$JPlainBuilder.scala$tools$nsc$backend$jvm$GenASM$JPlainBuilder$$genInstr$1(GenASM.scala:2292)
	at scala.tools.nsc.backend.jvm.GenASM$JPlainBuilder$$anonfun$genBlock$1$2.apply(GenASM.scala:2223)
	at scala.tools.nsc.backend.jvm.GenASM$JPlainBuilder$$anonfun$genBlock$1$2.apply(GenASM.scala:2209)
	at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33)
	at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:186)
	at scala.tools.nsc.backend.icode.BasicBlocks$BasicBlock.foreach(BasicBlocks.scala:195)

@wang7x
Copy link

wang7x commented Nov 15, 2017

meet same error when call length() on a very large string scastie example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants