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

Array max size auditing and fixes #6671

merged 5 commits into from May 13, 2021


Copy link

@headius headius commented May 12, 2021

This is a follow-up to #6670 that audits other locations we allocate arrays.

This also reduces the fallback maximum to Integer.MAX_VALUE - 2,
which seems to be the actual effective size for allocating a new
array. See jruby#6670.
@headius headius added this to the JRuby milestone May 12, 2021
@headius headius changed the title Move buffer length calc to utility method Array max size auditing and fixes May 12, 2021
Copy link
Member Author

headius commented May 12, 2021

Hmm... so it turns out this effective max array size varies across VMs. As pointed out in some answers here, the OpenJDK ArrayList impl uses MAX_VALUE - 8 to be safe, but there does not appear to be any way to query for the actual effective maximum.

We may want to make our max also MAX_VALUE - 8 since that would correspond to a 64-bit pointer alignment.

Based on explorations for jruby#6670 we found that the effective max
array size is actually Integer.MAX_VALUE - 2, so this localizes
the multiplication logic and uses that limit as the upper bound.

$ jruby -w -e 'foo = "x" * 2147483645; p :ok'

$ jruby -w -e 'foo = "x" * 2147483646; p :ok'
ArgumentError: argument too big
       * at org/jruby/
  <main> at -e:1
Copy link
Member Author

headius commented May 12, 2021

The array size maximum from AbstractCollection (mentioned in the SO answers above) appears to have been refactored into a utility similar to what we have in this PR:


I think it is probably appropriate for us to use MAX_VALUE - 8 since OpenJDK has normalized on that value.

@headius headius merged commit 35d6d38 into jruby:jruby-9.2 May 13, 2021
@headius headius deleted the array_overflow_fixes branch May 13, 2021 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.

None yet

1 participant