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

Properly handle index-out-of-bounds on JS #537

Merged
merged 7 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
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
10 changes: 2 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ThisBuild / tlBaseVersion := "1.4"
ThisBuild / tlBaseVersion := "1.5"
lazy val scala212 = "2.12.17"
lazy val scala213 = "2.13.10"
lazy val scala3 = "3.2.2"
Expand All @@ -21,13 +21,7 @@ lazy val jawnSettings = Seq(

lazy val jawnSettingsJVM = List(Test / fork := true)
lazy val jawnSettingsJS = List(
tlVersionIntroduced := List("2.12", "2.13", "3").map(_ -> "1.2.0").toMap,
scalaJSLinkerConfig ~= {
_.withSemantics(
_.withAsInstanceOfs(org.scalajs.linker.interface.CheckedBehavior.Unchecked)
.withArrayIndexOutOfBounds(org.scalajs.linker.interface.CheckedBehavior.Unchecked)
)
}
Comment on lines -25 to -30
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although in production mode these are undefined behaviors, in development mode Scala.js raises fatal errors to help detect these issues. As a workaround, we were previously disabling this to silence the fatal errors (my bad 😅 ).

tlVersionIntroduced := List("2.12", "2.13", "3").map(_ -> "1.2.0").toMap
)
lazy val jawnSettingsNative = Seq(
tlVersionIntroduced := Map(
Expand Down
28 changes: 28 additions & 0 deletions parser/js/src/main/scala/jawn/Platform.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2012 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package org.typelevel.jawn

private object Platform {
final val isJvm = false
final val isJs = true
final val isNative = false
}
28 changes: 28 additions & 0 deletions parser/jvm/src/main/scala/jawn/Platform.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2012 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package org.typelevel.jawn

private object Platform {
final val isJvm = true
final val isJs = false
final val isNative = false
}
28 changes: 28 additions & 0 deletions parser/native/src/main/scala/jawn/Platform.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2012 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package org.typelevel.jawn

private object Platform {
final val isJvm = false
final val isJs = false
final val isNative = true
}
24 changes: 21 additions & 3 deletions parser/shared/src/main/scala/jawn/ByteArrayParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,29 @@ final class ByteArrayParser[J](src: Array[Byte]) extends SyncParser[J] with Byte
context: FContext[J],
stack: List[FContext[J]]
): Unit = {}
final protected[this] def byte(i: Int): Byte = src(i)
final protected[this] def at(i: Int): Char = src(i).toChar

final protected[this] def at(i: Int, k: Int): CharSequence =
final protected[this] def byte(i: Int): Byte = {
if (Platform.isJs) {
if (i < 0 || i >= src.length) throw new ArrayIndexOutOfBoundsException
}
src(i)
}
Comment on lines +44 to +49
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided that platform-specific guards would be the best way to implement this.

Because isJs is a compile-time constant, the Scala compiler will actually elide these checks on JVM+Native before it even reaches the bytecode. So there is zero-cost to JVM and Native due to this change.

The other option is that we split these sources across platform but I think that is worse for readability.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because isJs is a compile-time constant, the Scala compiler will actually elide these checks on JVM+Native before it even reaches the bytecode.

til. Is this eliding part of the Scala 2 inliner / Scala 3 inline keyword or just part of core scalac?

Its ifs the former maybe it makes sense to explicitly inline using @inline/inline keyword respectively so its more clearly?

In any case maybe it makes sense to add this as a brief comment to all of the Platform files for the different platforms?


final protected[this] def at(i: Int): Char = {
if (Platform.isJs) {
if (i < 0 || i >= src.length) throw new ArrayIndexOutOfBoundsException
}
src(i).toChar
}

final protected[this] def at(i: Int, k: Int): CharSequence = {
if (Platform.isJs) {
if (i < 0 || i >= src.length) throw new ArrayIndexOutOfBoundsException
if (k < 0 || k > src.length) throw new ArrayIndexOutOfBoundsException
if (i > k) throw new ArrayIndexOutOfBoundsException
}
new String(src, i, k - i, utf8)
}

final protected[this] def atEof(i: Int): Boolean = i >= src.length
}
16 changes: 14 additions & 2 deletions parser/shared/src/main/scala/jawn/CharSequenceParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,20 @@ final private[jawn] class CharSequenceParser[J](cs: CharSequence) extends SyncPa
final protected[this] def line(): Int = _line
final protected[this] def reset(i: Int): Int = i
final protected[this] def checkpoint(state: Int, i: Int, context: FContext[J], stack: List[FContext[J]]): Unit = ()
final protected[this] def at(i: Int): Char = cs.charAt(i)
final protected[this] def at(i: Int, j: Int): CharSequence = cs.subSequence(i, j)
final protected[this] def at(i: Int): Char = {
if (Platform.isJs) {
if (i < 0 || i >= cs.length) throw new IndexOutOfBoundsException
}
cs.charAt(i)
}
final protected[this] def at(i: Int, j: Int): CharSequence = {
if (Platform.isJs) {
if (i < 0 || i >= cs.length) throw new IndexOutOfBoundsException
if (j < 0 || j > cs.length) throw new IndexOutOfBoundsException
if (i > j) throw new IndexOutOfBoundsException
}
cs.subSequence(i, j)
}
final protected[this] def atEof(i: Int): Boolean = i == cs.length
final protected[this] def close(): Unit = ()
}
16 changes: 14 additions & 2 deletions parser/shared/src/main/scala/jawn/StringParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,20 @@ final private[jawn] class StringParser[J](s: String) extends SyncParser[J] with
final protected[this] def line(): Int = _line
final protected[this] def reset(i: Int): Int = i
final protected[this] def checkpoint(state: Int, i: Int, context: FContext[J], stack: List[FContext[J]]): Unit = {}
final protected[this] def at(i: Int): Char = s.charAt(i)
final protected[this] def at(i: Int, j: Int): CharSequence = s.substring(i, j)
final protected[this] def at(i: Int): Char = {
if (Platform.isJs) {
if (i < 0 || i >= s.length) throw new StringIndexOutOfBoundsException
}
s.charAt(i)
}
final protected[this] def at(i: Int, j: Int): CharSequence = {
if (Platform.isJs) {
if (i < 0 || i >= s.length) throw new StringIndexOutOfBoundsException
if (j < 0 || j > s.length) throw new StringIndexOutOfBoundsException
if (i > j) throw new StringIndexOutOfBoundsException
}
s.substring(i, j)
}
final protected[this] def atEof(i: Int): Boolean = i == s.length
final protected[this] def close(): Unit = ()
}
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
addSbtPlugin("org.typelevel" % "sbt-typelevel" % "0.4.20")
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.4")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.10.1")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.13.1")
addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.3.1")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.12")
3 changes: 3 additions & 0 deletions util/shared/src/main/scala/jawn/util/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ package object util {
var inverseSign: Long = -1L
var i: Int = 0

if (cs.length == 0)
throw InvalidLong(cs.toString)
Comment on lines +48 to +49
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an existing bug that also applies to JVM and Native.


if (cs.charAt(0) == '-') {
inverseSign = 1L
i = 1
Expand Down