Updated wasm spec and enforced UTF-8 validation on binary strings

This commit is contained in:
Chad Retz
2017-05-24 17:17:37 -05:00
parent e9cdfc3b0f
commit 43333edfd0
4 changed files with 9 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package asmble.io
import asmble.ast.Node
import asmble.util.*
import java.nio.ByteBuffer
open class BinaryToAst(
val version: Long = 1L,
@ -215,7 +216,10 @@ open class BinaryToAst(
else -> error("Unknown value type: $type")
}
fun ByteReader.readString() = this.readVarUInt32AsInt().let { String(this.readBytes(it)) }
fun ByteReader.readString() = this.readVarUInt32AsInt().let {
// We have to use the decoder directly to get malformed-input errors
Charsets.UTF_8.newDecoder().decode(ByteBuffer.wrap(this.readBytes(it))).toString()
}
fun <T> ByteReader.readList(fn: (ByteReader) -> T) = this.readVarUInt32().let { listSize ->
(0 until listSize).map { fn(this) }
}

View File

@ -5,6 +5,7 @@ import asmble.AsmErr
sealed class IoErr(message: String, cause: Throwable? = null) : RuntimeException(message, cause), AsmErr {
class UnexpectedEnd : IoErr("Unexpected EOF") {
override val asmErrString get() = "unexpected end"
override val asmErrStrings get() = listOf(asmErrString, "length out of bounds")
}
class InvalidMagicNumber : IoErr("Invalid magic number") {

View File

@ -3,6 +3,7 @@ package asmble.run.jvm
import asmble.AsmErr
import java.lang.invoke.WrongMethodTypeException
import java.nio.BufferOverflowException
import java.nio.charset.MalformedInputException
open class ExceptionTranslator {
fun translate(ex: Throwable): List<String> = when (ex) {
@ -13,6 +14,7 @@ open class ExceptionTranslator {
is ArrayIndexOutOfBoundsException -> listOf("undefined element", "elements segment does not fit")
is AsmErr -> ex.asmErrStrings
is IndexOutOfBoundsException -> listOf("out of bounds memory access")
is MalformedInputException -> listOf("invalid UTF-8 encoding")
is NoSuchMethodException -> listOf("unknown import", "type mismatch")
is NullPointerException -> listOf("undefined element", "uninitialized element")
is StackOverflowError -> listOf("call stack exhausted")