Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a2d0255

Browse files
committedJul 19, 2018
Check tasty file hash
1 parent f3c8458 commit a2d0255

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed
 

‎compiler/src/dotty/tools/backend/jvm/GenBCode.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import scala.tools.asm
2626
import scala.tools.asm.tree._
2727
import tpd._
2828
import StdNames._
29+
import dotty.tools.dotc.core.tasty.TastyPickler
2930
import dotty.tools.io._
3031

3132
class GenBCode extends Phase {
@@ -221,9 +222,9 @@ class GenBCodePipeline(val entryPoints: List[Symbol], val int: DottyBackendInter
221222
val outstream = new DataOutputStream(outTastyFile.bufferedOutput)
222223
try outstream.write(binary)
223224
finally outstream.close()
224-
// TASTY attribute is created but 0 bytes are stored in it.
225-
// A TASTY attribute has length 0 if and only if the .tasty file exists.
226-
Array.empty[Byte]
225+
// TASTY attribute is created but only the header bytes are stored in it.
226+
// A TASTY attribute has length `headerSize` if and only if the .tasty file exists.
227+
java.util.Arrays.copyOf(binary, TastyPickler.headerSize)
227228
} else {
228229
// Create an empty file to signal that a tasty section exist in the corresponding .class
229230
// This is much cheaper and simpler to check than doing classfile parsing

‎compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package classfile
66
import Contexts._, Symbols._, Types._, Names._, StdNames._, NameOps._, Scopes._, Decorators._
77
import SymDenotations._, unpickleScala2.Scala2Unpickler._, Constants._, Annotations._, util.Positions._
88
import NameKinds.{ModuleClassName, DefaultGetterName}
9+
import dotty.tools.dotc.core.tasty.TastyPickler
910
import ast.tpd._
1011
import java.io.{ ByteArrayInputStream, ByteArrayOutputStream, DataInputStream, File, IOException }
1112
import java.nio
@@ -785,7 +786,7 @@ class ClassfileParser(
785786

786787
if (scan(tpnme.TASTYATTR)) {
787788
val attrLen = in.nextInt
788-
if (attrLen == 0) { // A tasty attribute implies the existence of the .tasty file
789+
if (attrLen == TastyPickler.headerSize) { // A tasty attribute implies the existence of the .tasty file
789790
val tastyBytes: Array[Byte] = classfile.underlyingSource match { // TODO: simplify when #3552 is fixed
790791
case None =>
791792
ctx.error("Could not load TASTY from .tasty for virtual file " + classfile)
@@ -816,8 +817,11 @@ class ClassfileParser(
816817
Array.empty
817818
}
818819
}
819-
if (tastyBytes.nonEmpty)
820+
if (tastyBytes.nonEmpty) {
821+
for (i <- 0 until TastyPickler.headerSize if tastyBytes(i) != in.nextByte)
822+
ctx.error("Contents of TASTY file did not correspond to classfile. One of the files might be outdated or corrupted.")
820823
return unpickleTASTY(tastyBytes)
824+
}
821825
}
822826
else return unpickleTASTY(in.nextBytes(attrLen))
823827
}

‎compiler/src/dotty/tools/dotc/core/tasty/TastyPickler.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ class TastyPickler(val rootCls: ClassSymbol) {
2929
val uuidHi: Long = sections.iterator.map(x => pjwHash64(x._2.bytes)).fold(0L)(_ ^ _)
3030

3131
val headerBuffer = {
32-
val buf = new TastyBuffer(header.length + 24)
32+
val buf = new TastyBuffer(TastyPickler.headerSize)
3333
for (ch <- header) buf.writeByte(ch.toByte)
34+
// TODO use writeUncompressedLong to ensure that the header has a constant size across versions
3435
buf.writeNat(MajorVersion)
3536
buf.writeNat(MinorVersion)
3637
buf.writeUncompressedLong(uuidLow)
3738
buf.writeUncompressedLong(uuidHi)
39+
assert(buf.length == TastyPickler.headerSize)
3840
buf
3941
}
4042

@@ -90,3 +92,7 @@ class TastyPickler(val rootCls: ClassSymbol) {
9092
h
9193
}
9294
}
95+
96+
object TastyPickler {
97+
val headerSize = header.length + 18
98+
}

0 commit comments

Comments
 (0)
Please sign in to comment.