forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 58
[JSC] Throw instead of crashing when a Vector that grows with script input cannot grow #666
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
Open
robobun
wants to merge
3
commits into
main
Choose a base branch
from
robobun/9e4c94a4/vector-append-oom
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
JSTests/stress/out-of-memory-when-script-sized-vector-cannot-grow.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| //@ slow! | ||
| //@ if $buildType == "debug" then runDefault("--maxSingleAllocationSize=1048576") else skip end | ||
|
|
||
| // Each builtin below keeps one entry per script-controlled item in a WTF::Vector. When that Vector | ||
| // cannot grow, because the allocation fails or because a Vector holds at most 2^31 bytes, the | ||
| // builtin has to throw the out-of-memory RangeError. It used to crash. | ||
| // --maxSingleAllocationSize makes every allocation of more than 1 MiB fail, so a few hundred thousand | ||
| // items are enough here. No string below is longer than 1 MiB, because creating one would crash too. | ||
|
|
||
| function shouldBe(actual, expected) { | ||
| if (actual !== expected) | ||
| throw new Error(`bad value: expected ${JSON.stringify(expected)} but got ${JSON.stringify(actual)}`); | ||
| } | ||
|
|
||
| function shouldThrowOutOfMemory(func) { | ||
| let error = null; | ||
| try { | ||
| func(); | ||
| } catch (e) { | ||
| error = e; | ||
| } | ||
| if (String(error) !== "RangeError: Out of memory") | ||
| throw new Error(`bad error: ${String(error)} from ${func}`); | ||
| } | ||
|
|
||
| // Never ends on its own: the only way out of the consumer is the throw, which has to close it. | ||
| function endless(value) { | ||
| const iterable = { | ||
| closed: false, | ||
| [Symbol.iterator]() { | ||
| return { | ||
| next() { return { done: false, value }; }, | ||
| return() { iterable.closed = true; return { }; }, | ||
| }; | ||
| }, | ||
| }; | ||
| return iterable; | ||
| } | ||
|
|
||
| // String.prototype.replaceAll(string, string): the offset of every match. | ||
| shouldThrowOutOfMemory(() => "a".repeat(200000).replaceAll("a", "c")); | ||
| shouldThrowOutOfMemory(() => "a".repeat(200000).replaceAll("", "c")); | ||
| shouldBe("banana".replaceAll("a", "o"), "bonono"); | ||
|
|
||
| // String.prototype.replace(regexp, string): one part per "$" reference of the replacement. | ||
| shouldThrowOutOfMemory(() => "x".replace(/(x)/g, "$1".repeat(100000))); | ||
| shouldThrowOutOfMemory(() => "x".replace(/(x)/g, "$$".repeat(100000))); | ||
| shouldThrowOutOfMemory(() => "x".replace(/(?<name>x)/g, "-$<name>".repeat(50000))); | ||
| shouldBe("x".replace(/(x)/g, "[$1$$$&]"), "[x$x]"); | ||
|
|
||
| // String.prototype.split(string): the end of every piece. | ||
| shouldThrowOutOfMemory(() => ",".repeat(400000).split(",")); | ||
| shouldThrowOutOfMemory(() => "\u3042".repeat(400000).split("\u3042")); | ||
| shouldThrowOutOfMemory(() => ",;".repeat(400000).split(",;")); | ||
| shouldBe(JSON.stringify("a,b,,c".split(",")), `["a","b","","c"]`); | ||
|
|
||
| // JSON.parse with a reviver: the source range of every array element. | ||
| shouldThrowOutOfMemory(() => JSON.parse("[" + "1,".repeat(50000) + "1]", (key, value) => value)); | ||
| shouldBe(JSON.stringify(JSON.parse("[1,[2,3]]", (key, value) => value)), "[1,[2,3]]"); | ||
|
|
||
| // FinalizationRegistry.prototype.register: every registration, in one list per unregister token. | ||
| { | ||
| const registry = new FinalizationRegistry(() => { }); | ||
| const target = { }; | ||
| const token = { }; | ||
| shouldThrowOutOfMemory(() => { | ||
| for (;;) | ||
| registry.register(target, 1, token); | ||
| }); | ||
| shouldBe(registry.unregister(token), true); | ||
| shouldBe(registry.unregister(token), false); | ||
|
|
||
| shouldThrowOutOfMemory(() => { | ||
| for (;;) | ||
| registry.register(target, 1); | ||
| }); | ||
| // The list of the registrations without a token is full now. When a token dies before its target, the | ||
| // end of the collection moves the registration to that list. Nothing can throw there, so it is dropped. | ||
| (function () { | ||
| for (let i = 0; i < 100; ++i) | ||
| registry.register(target, 2, { }); | ||
| })(); | ||
| fullGC(); | ||
| registry.register(target, 3, token); | ||
| shouldBe(registry.unregister(token), true); | ||
| } | ||
|
|
||
| // The end of a collection also moves the held values of the registrations whose target died to a second | ||
| // list. What does not fit there is dropped too. One list of registrations stops below the size that this | ||
| // second list stops at, so the registrations are spread over many tokens, each with its own target. The | ||
| // second list overflows even when the last few of them are still reachable at the collection. | ||
| { | ||
| const registry = new FinalizationRegistry(() => { }); | ||
| (function () { | ||
| for (let t = 0; t < 30; ++t) { | ||
| const target = { }; | ||
| const token = { }; | ||
| for (let i = 0; i < 5000; ++i) | ||
| registry.register(target, 1, token); | ||
| } | ||
| })(); | ||
| fullGC(); | ||
| const target = { }; | ||
| const token = { }; | ||
| registry.register(target, 1, token); | ||
| shouldBe(registry.unregister(token), true); | ||
| } | ||
|
|
||
| // While a token is alive, the held values move to a list of that token, so that unregister() still finds | ||
| // them. No cleanup callback runs before this script ends, so that list fills up over several collections. | ||
| { | ||
| const registry = new FinalizationRegistry(() => { }); | ||
| const token = { }; | ||
| for (let round = 0; round < 7; ++round) { | ||
| (function () { | ||
| for (let t = 0; t < 6; ++t) { | ||
| const target = { }; | ||
| for (let i = 0; i < 5000; ++i) | ||
| registry.register(target, 1, token); | ||
| } | ||
| })(); | ||
| fullGC(); | ||
| } | ||
| shouldBe(registry.unregister(token), true); | ||
| shouldBe(registry.unregister(token), false); | ||
| } | ||
|
|
||
| // Intl.ListFormat: every string of the iterable. | ||
| { | ||
| const listFormat = new Intl.ListFormat("en"); | ||
| for (const format of [items => listFormat.format(items), items => listFormat.formatToParts(items)]) { | ||
| const items = endless("a"); | ||
| shouldThrowOutOfMemory(() => format(items)); | ||
| shouldBe(items.closed, true); | ||
| } | ||
| shouldBe(listFormat.format(["a", "b", "c"]), "a, b, and c"); | ||
| } | ||
|
|
||
| // WebAssembly.Tag: every parameter type. The compile options: every name of the builtins list. | ||
| if (typeof WebAssembly === "object") { | ||
| const parameters = endless("i32"); | ||
| shouldThrowOutOfMemory(() => new WebAssembly.Tag({ parameters })); | ||
| shouldBe(parameters.closed, true); | ||
|
|
||
| const emptyModule = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0]); | ||
| const builtins = endless("js-string"); | ||
| shouldThrowOutOfMemory(() => WebAssembly.validate(emptyModule, { builtins })); | ||
| shouldBe(builtins.closed, true); | ||
| shouldBe(WebAssembly.validate(emptyModule, { builtins: ["js-string"] }), true); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.