Skip to content

Fix uint8 overflow in array literal skipIndexesId and sourcePositions#2321

Open
Finomosec wants to merge 14 commits intomozilla:masterfrom
Finomosec:fix/uint8-overflow
Open

Fix uint8 overflow in array literal skipIndexesId and sourcePositions#2321
Finomosec wants to merge 14 commits intomozilla:masterfrom
Finomosec:fix/uint8-overflow

Conversation

@Finomosec
Copy link
Copy Markdown

Summary

  • CodeGenerator.visitArrayLiteral encodes skipIndexesId and sourcePositions as uint8 via addUint8, which overflows when a script contains more than 255 object/array literals (causing literalIds to exceed 255 entries)
  • Changed addUint8 to addUint16 for these values in CodeGenerator and updated corresponding reads in Interpreter (DoLiteralNewArray, DoSpread) to use getIndex (uint16)
  • Added bytecodeSpan entry for Icode_LITERAL_NEW_ARRAY to account for the new 2-byte encoding
  • Only affects interpreter mode (optimizationLevel = -1)

Reproduction

A script with 260+ object literals followed by a sparse array literal (e.g. [1, , 3]) triggers IllegalStateException (from Kit.codeBug() in addUint8) during compilation in interpreter mode.

Test plan

  • Added ArrayLiteralOverflowTest with 4 tests covering both interpreted and compiled modes, with and without spread operators
  • Full existing test suite passes without regressions

gbrail and others added 14 commits February 7, 2026 13:22
* Set `global` as part of initialisation.
* Add a test that `global` and `globalThis` are the same thing.
* Fix debugger eval for Script
* Fix debugger eval scope for top-level scripts
This is to avoid repeatedly parsing the replacement template in
case of multiple matches.
…sourcePositions

When a script contains more than 255 object/array literals, the literalIds
table grows beyond 255. The skipIndexesId (used for sparse array literals)
and sourcePositions (used for spread in sparse arrays) were encoded as uint8,
causing an IllegalStateException (Kit.codeBug) when the values exceeded 255.

Changed addUint8 to addUint16 in CodeGenerator.visitArrayLiteral and updated
the corresponding reads in Interpreter (DoLiteralNewArray, DoSpread) to use
getIndex (uint16) instead of single-byte reads.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gbrail
Copy link
Copy Markdown
Collaborator

gbrail commented Mar 7, 2026

I think this is potentially very helpful as I have been trying to run more standard benchmarks and we've run into some of these types of overflows. However I think that your actual change is buried behind a lot of other commits in this PR -- can you try to rebase your change against the current master branch? You should end up with just one commit that's different when you compare in the GitHub UI. Thanks!

rootProject.name=rhino-root
group=org.mozilla
version=1.9.1
version=1.9.1-PATCHED
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This needs to be reverted

* When a script contains more than 255 object/array literals, the literalIds
* table grows beyond 255, and skipIndexesId (encoded as uint8) overflows.
*/
public class ArrayLiteralOverflowTest {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This test should be modified to use Utils.runWithAllModes.

@andreabergia
Copy link
Copy Markdown
Contributor

This looks correct to me. I've put a couple of small comments, but they should be pretty easy to change.

@gbrail
Copy link
Copy Markdown
Collaborator

gbrail commented Mar 21, 2026

I'm happy to take this but someone (or something) needs to fix the PR so it doesn't have a copy of a whole bunch of other PRs in it. TBH I'm not sure how it got this way but I might suggest just creating a new PR and cherry-pick the one commit that actually changes something.

@rbri
Copy link
Copy Markdown
Collaborator

rbri commented Mar 22, 2026

as @Finomosec seems to be from germany, i tried to reach out to him via linkedin - maybe we can align. If not i will extract a simple PR from this one... 🤞

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.

8 participants