Skip to content

Backport "error when reading class file with unknown newer jdk version" to LTS #20862

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 2 commits into from
Jul 1, 2024
Merged
Changes from all 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
61 changes: 45 additions & 16 deletions compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ import io.{AbstractFile, ZipArchive}
import scala.util.control.NonFatal

object ClassfileParser {
object Header:
opaque type Version = Long

object Version:
val Unknown: Version = -1L

def brokenVersionAddendum(classfileVersion: Version)(using Context): String =
if classfileVersion.exists then
val (maj, min) = (classfileVersion.majorVersion, classfileVersion.minorVersion)
val scalaVersion = config.Properties.versionNumberString
i""" (version $maj.$min),
| please check the JDK compatibility of your Scala version ($scalaVersion)"""
else
""

def apply(major: Int, minor: Int): Version =
(major.toLong << 32) | (minor.toLong & 0xFFFFFFFFL)
extension (version: Version)
def exists: Boolean = version != Unknown
def majorVersion: Int = (version >> 32).toInt
def minorVersion: Int = (version & 0xFFFFFFFFL).toInt

import ClassfileConstants.*

/** Marker trait for unpicklers that can be embedded in classfiles. */
trait Embedded

Expand All @@ -50,6 +74,21 @@ object ClassfileParser {
mapOver(tp)
}
}

private[classfile] def parseHeader(classfile: AbstractFile)(using in: DataReader): Header.Version = {
val magic = in.nextInt
if (magic != JAVA_MAGIC)
throw new IOException(s"class file '${classfile}' has wrong magic number 0x${toHexString(magic)}, should be 0x${toHexString(JAVA_MAGIC)}")
val minorVersion = in.nextChar.toInt
val majorVersion = in.nextChar.toInt
if ((majorVersion < JAVA_MAJOR_VERSION) ||
((majorVersion == JAVA_MAJOR_VERSION) &&
(minorVersion < JAVA_MINOR_VERSION)))
throw new IOException(
s"class file '${classfile}' has unknown version $majorVersion.$minorVersion, should be at least $JAVA_MAJOR_VERSION.$JAVA_MINOR_VERSION")
Header.Version(majorVersion, minorVersion)
}

}

class ClassfileParser(
Expand All @@ -70,6 +109,7 @@ class ClassfileParser(
protected var classTParams: Map[Name, Symbol] = Map()

private var Scala2UnpicklingMode = Mode.Scala2Unpickling
private var classfileVersion: Header.Version = Header.Version.Unknown

classRoot.info = NoLoader().withDecls(instanceScope)
moduleRoot.info = NoLoader().withDecls(staticScope).withSourceModule(staticModule)
Expand All @@ -82,7 +122,7 @@ class ClassfileParser(
def run()(using Context): Option[Embedded] = try ctx.base.reusableDataReader.withInstance { reader =>
implicit val reader2 = reader.reset(classfile)
report.debuglog("[class] >> " + classRoot.fullName)
parseHeader()
classfileVersion = parseHeader(classfile)
this.pool = new ConstantPool
val res = parseClass()
this.pool = null
Expand All @@ -91,22 +131,11 @@ class ClassfileParser(
catch {
case e: RuntimeException =>
if (ctx.debug) e.printStackTrace()
val addendum = Header.Version.brokenVersionAddendum(classfileVersion)
throw new IOException(
i"""class file ${classfile.canonicalPath} is broken, reading aborted with ${e.getClass}
|${Option(e.getMessage).getOrElse("")}""")
}

private def parseHeader()(using in: DataReader): Unit = {
val magic = in.nextInt
if (magic != JAVA_MAGIC)
throw new IOException(s"class file '${classfile}' has wrong magic number 0x${toHexString(magic)}, should be 0x${toHexString(JAVA_MAGIC)}")
val minorVersion = in.nextChar.toInt
val majorVersion = in.nextChar.toInt
if ((majorVersion < JAVA_MAJOR_VERSION) ||
((majorVersion == JAVA_MAJOR_VERSION) &&
(minorVersion < JAVA_MINOR_VERSION)))
throw new IOException(
s"class file '${classfile}' has unknown version $majorVersion.$minorVersion, should be at least $JAVA_MAJOR_VERSION.$JAVA_MINOR_VERSION")
i""" class file ${classfile.canonicalPath} is broken$addendum,
| reading aborted with ${e.getClass}:
| ${Option(e.getMessage).getOrElse("")}""")
}

/** Return the class symbol of the given name. */
Expand Down