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

XS clamps Array.prototype.{findLast,findLastIndex} index values too low #1017

Open
gibson042 opened this issue Jan 29, 2023 · 6 comments
Open
Labels
confirmed issue reported has been reproduced

Comments

@gibson042
Copy link

Environment: XS 13.0.0 32 4

Description
Array.prototype.{findLast,findLastIndex} index clamping does not conform with the ECMAScript specification.

Steps to Reproduce

[
       Array.prototype.findLast.call({ length: 2 ** 53, [2**53 - 2]: "<maximum index>" }, (v, i) => true),
  Array.prototype.findLastIndex.call({ length: 2 ** 53, [2**53 - 2]: "<maximum index>" }, (v, i) => true),
]

test262 pull request: tc39/test262#3775

Actual behavior

[undefined, -1]

Expected behavior

["<maximum index>", 9007199254740990]

Array.prototype.findLast and findLastIndex are both specified to initialize len as LengthOfArrayLike(O), where LengthOfArrayLike uses ToLength to enforce a maximum result of 253 - 1. Therefore, both methods should be willing to start with an index of up to 253 - 2 (and find and findIndex should also be willing to iterate up to that point, even though it would take an absurdly long time).

@phoddie
Copy link
Collaborator

phoddie commented Jan 29, 2023

Thanks for the report. I'm able to reproduce your results. We'll take a look.

@phoddie phoddie added the confirmed issue reported has been reproduced label Jan 29, 2023
@phoddie
Copy link
Collaborator

phoddie commented Jan 31, 2023

Short answer – There's no immediate intention to address this.

Longer answer - This is a consequence of an implementation limit in XS which uses 32-bit values for indices to minimize RAM use on microcontrollers. A build option could use 64-bit values instead. That would favor conformance over RAM use. Making and verifying those changes is significant work for which a strong motivation doesn't currently exist.

@gibson042
Copy link
Author

Hmm, would it be possible to shunt such cases into a slow path? Practical code is exceedingly unlikely to ever hit it and can remain efficient, but you would be able to avoid potentially exploitable spec divergence.

@phoddie
Copy link
Collaborator

phoddie commented Feb 1, 2023

Hmm, would it be possible to shunt such cases into a slow path?

The existence of such a path is all-but-certain to have performance implications on code that doesn't need it. And it will add complexity to the engine, which works against the goal of having a robust implementation. That slow case would still need to be optional because there's no benefit benefit to embedded uses so there's no benefit in carrying the code. Maybe there's an elegant solution, but it isn't obvious at the moment.

The solution we recommend would have no real cost on 64-bit systems and would likely address issues beyond just this edge case. There would likely be little or no new code to maintain, as it is mostly a change of type. The effort is making those changes, verifying conformance, updating snapshot read/write, checking the changes against various compilers & ports, etc.

But, if you wouldn't mind, please help me understand: what's the "potentially exploitable spec divergence"?

@gibson042
Copy link
Author

But, if you wouldn't mind, please help me understand: what's the "potentially exploitable spec divergence"?

Deviation from specified behavior that can allow buggy or malicious code to behave differently across environments (e.g., on a 64-bit developer workstation vs. on a real embedded device), making its detection more difficult and less likely. And willful violations of the spec can in some cases encourage the latter. Proper support of the spec by implementations, even if with degraded performance (but hopefully only significant for edge cases), discourages that and encourages cross-platform code.

@phoddie
Copy link
Collaborator

phoddie commented Feb 1, 2023

Thank you for the clarification. I understand your point is about the general value of conformance with the standard, not a specific vulnerability caused by this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed issue reported has been reproduced
Projects
None yet
Development

No branches or pull requests

2 participants