Skip to content

Commit

Permalink
LF: reorganize errors in com.daml.lf.archive
Browse files Browse the repository at this point in the history
part of #9974

CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
remyhaemmerle-da committed Jul 8, 2021
1 parent 41b8448 commit 962b746
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import scalaz.std.option._
import scalaz.std.scalaFuture._
import scalaz.syntax.traverse._

import com.daml.lf.archive.ParseError
import com.daml.lf.archive
import com.daml.lf.data.ImmArray
import com.daml.lf.data.Ref
import com.daml.lf.data.Ref.ModuleName
Expand Down Expand Up @@ -289,8 +289,8 @@ class ScenarioService(implicit
respObs.onCompleted()

} catch {
case e: ParseError =>
respObs.onError(Status.INVALID_ARGUMENT.withDescription(e.error).asRuntimeException())
case e: archive.Error =>
respObs.onError(Status.INVALID_ARGUMENT.withDescription(e.msg).asRuntimeException())
case NonFatal(e) =>
respObs.onError(Status.INTERNAL.withDescription(e.toString).asRuntimeException())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import java.util.concurrent.atomic.AtomicLong

import akka.stream.Materializer
import com.daml.grpc.adapter.ExecutionSequencerFactory
import com.daml.lf.archive.{Decode, ParseError, Reader}
import com.daml.lf.archive.{Decode, Reader}
import com.daml.lf.data.{assertRight, ImmArray}
import com.daml.lf.data.Ref.{DottedName, Identifier, ModuleName, PackageId, QualifiedName}
import com.daml.lf.engine.script.ledgerinteraction.{IdeLedgerClient, ScriptTimeMode}
Expand Down Expand Up @@ -89,7 +89,7 @@ class Context(val contextId: Context.ContextId, languageVersion: LanguageVersion
dop.decodeScenarioModule(homePackageId, lfScenarioModule)
}

@throws[ParseError]
@throws[archive.Error]
def update(
unloadModules: Set[ModuleName],
loadModules: collection.Seq[ProtoScenarioModule],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package com.daml.lf.archive
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, File, FileInputStream, InputStream}
import java.util.zip.ZipInputStream

import com.daml.lf.archive.Errors.{InvalidDar, InvalidLegacyDar, InvalidZipEntry}
import com.daml.lf.data.TryOps.Bracket.bracket
import com.daml.lf.data.TryOps.sequence

Expand Down Expand Up @@ -52,7 +51,7 @@ class DarReader[A](
val buffer = new Array[Byte](4096)
for (n <- Iterator.continually(zip.read(buffer)).takeWhile(_ >= 0) if n > 0) {
output.write(buffer, 0, n)
if (output.size >= entrySizeThreshold) throw Errors.ZipBomb()
if (output.size >= entrySizeThreshold) throw Error.ZipBomb()
}
(output.size.toLong, new ByteArrayInputStream(output.toByteArray))
}
Expand Down Expand Up @@ -94,31 +93,6 @@ class DarReader[A](

}

object Errors {

import DarReader.ZipEntries

final case class InvalidDar(entries: ZipEntries, cause: Throwable)
extends RuntimeException(s"Invalid DAR: ${darInfo(entries): String}", cause)

final case class InvalidZipEntry(name: String, entries: ZipEntries)
extends RuntimeException(
s"Invalid zip entryName: ${name: String}, DAR: ${darInfo(entries): String}"
)

final case class InvalidLegacyDar(entries: ZipEntries)
extends RuntimeException(s"Invalid Legacy DAR: ${darInfo(entries)}")

final case class ZipBomb()
extends RuntimeException(s"An entry is too large, rejected as a possible zip bomb")

private def darInfo(entries: ZipEntries): String =
s"${entries.name}, content: [${darFileNames(entries).mkString(", "): String}}]"

private def darFileNames(entries: ZipEntries): Iterable[String] =
entries.entries.keys
}

object DarReader {

private val ManifestName = "META-INF/MANIFEST.MF"
Expand All @@ -129,7 +103,7 @@ object DarReader {
def getInputStreamFor(entryName: String): Try[(Long, InputStream)] = {
entries.get(entryName) match {
case Some((size, is)) => Success(size -> is)
case None => Failure(InvalidZipEntry(entryName, this))
case None => Failure(Error.InvalidZipEntry(entryName, this))
}
}

Expand All @@ -138,7 +112,7 @@ object DarReader {
): Try[Dar[String]] =
parseDalfNamesFromManifest(readDalfNamesFromManifest).recoverWith { case NonFatal(e1) =>
findLegacyDalfNames().recoverWith { case NonFatal(_) =>
Failure(InvalidDar(this, e1))
Failure(Error.InvalidDar(this, e1))
}
}

Expand All @@ -159,7 +133,7 @@ object DarReader {
case (List(prim), List(main)) => Success(Dar(main, List(prim)))
case (List(prim), Nil) => Success(Dar(prim, List.empty))
case (Nil, List(main)) => Success(Dar(main, List.empty))
case _ => Failure(InvalidLegacyDar(this))
case _ => Failure(Error.InvalidLegacyDar(this))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ sealed class Decode(onlySerializableDataDefs: Boolean) {
val decoder =
decoders
.lift(payload.version)
.getOrElse(throw ParseError(s"${payload.version} unsupported"))
.getOrElse(throw Error.Parsing(s"${payload.version} unsupported"))
(
payload.pkgId,
decoder.decoder.decodePackage(
Expand Down Expand Up @@ -59,13 +59,13 @@ object Decode extends Decode(onlySerializableDataDefs = false) {
private[lf] trait OfPackage[-Pkg] {
type ProtoScenarioModule
def protoScenarioModule(cis: CodedInputStream): ProtoScenarioModule
@throws[ParseError]
@throws[Error.Parsing]
def decodePackage(
packageId: PackageId,
lfPackage: Pkg,
onlySerializableDataDefs: Boolean = false,
): Package
@throws[ParseError]
@throws[Error.Parsing]
def decodeScenarioModule(packageId: PackageId, lfModuleForScenario: ProtoScenarioModule): Module
}

Expand All @@ -77,9 +77,9 @@ object Decode extends Decode(onlySerializableDataDefs = false) {

def checkIdentifier(s: String): Unit = {
if (s.isEmpty)
throw ParseError("empty identifier")
throw Error.Parsing("empty identifier")
else if (!(identifierStart(s.head) && s.tail.forall(identifierPart)))
throw ParseError(s"identifier $s contains invalid character")
throw Error.Parsing(s"identifier $s contains invalid character")
}

private val decimalPattern = "[+-]*[0-9]{0,28}(\\.[0-9]{0,10})*".r.pattern
Expand Down
Loading

0 comments on commit 962b746

Please sign in to comment.