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

Conversation

armanbilge
Copy link
Member

Follow-up to #535 (comment). On Scala.js, index-out-of-bounds is undefined behavior.

Because Scala.js does not receive VM support to detect such erroneous conditions, checking them is typically too expensive.

https://www.scala-js.org/doc/semantics.html#undefined-behaviors

Jawn relies on these exceptions to detect parsing failures, so for correctness we rely on well-defined behavior. Thus this PR manually restores the out-of-bounds checks for JS only.

Inline comments incoming.

Comment on lines -25 to -30
scalaJSLinkerConfig ~= {
_.withSemantics(
_.withAsInstanceOfs(org.scalajs.linker.interface.CheckedBehavior.Unchecked)
.withArrayIndexOutOfBounds(org.scalajs.linker.interface.CheckedBehavior.Unchecked)
)
}
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 😅 ).

Comment on lines +44 to +49
final protected[this] def byte(i: Int): Byte = {
if (Platform.isJs) {
if (i < 0 || i >= src.length) throw new ArrayIndexOutOfBoundsException
}
src(i)
}
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?

Comment on lines +48 to +49
if (cs.length == 0)
throw InvalidLong(cs.toString)
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.

@rossabaker rossabaker merged commit cd8edef into typelevel:main May 31, 2023
@mdedetrich mdedetrich mentioned this pull request May 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants