-
-
Notifications
You must be signed in to change notification settings - Fork 158
fix(runtime): reject oversized generic Array slice results #8843
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
Closed
Closed
Changes from all commits
Commits
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
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
126 changes: 126 additions & 0 deletions
126
crates/perry/tests/issue_5898_array_slice_invalid_length.rs
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,126 @@ | ||
| //! Regression coverage for the `Array.prototype.slice` invalid-length | ||
| //! subcluster in #5898. Generic slice receivers may have a `ToLength` above | ||
| //! the Array length limit; the result length must be rejected before indexed | ||
| //! reads, without narrowing or trying to materialise the full receiver. | ||
|
|
||
| use std::path::PathBuf; | ||
| use std::process::Command; | ||
|
|
||
| fn perry_bin() -> PathBuf { | ||
| PathBuf::from(env!("CARGO_BIN_EXE_perry")) | ||
| } | ||
|
|
||
| #[test] | ||
| fn generic_slice_rejects_oversized_results_before_index_access() { | ||
| let dir = tempfile::tempdir().expect("tempdir"); | ||
| let entry = dir.path().join("main.ts"); | ||
| let output = dir.path().join("main_bin"); | ||
| let runtime_dir = perry_bin() | ||
| .parent() | ||
| .expect("perry binary directory") | ||
| .to_path_buf(); | ||
| std::fs::write( | ||
| &entry, | ||
| r#" | ||
| let plainIndexReads = 0; | ||
| const plain: any = { length: 2 ** 32 }; | ||
| Object.defineProperty(plain, "0", { | ||
| get() { | ||
| plainIndexReads++; | ||
| return 1; | ||
| } | ||
| }); | ||
| try { | ||
| Array.prototype.slice.call(plain); | ||
| console.log("plain no throw"); | ||
| } catch (error) { | ||
| console.log("plain", error instanceof RangeError, plainIndexReads); | ||
| } | ||
|
|
||
| const aliased: any = { length: 2 ** 32 }; | ||
| aliased.slice = Array.prototype.slice; | ||
| try { | ||
| aliased.slice(0, 2 ** 32); | ||
| console.log("aliased no throw"); | ||
| } catch (error) { | ||
| console.log("aliased", error instanceof RangeError); | ||
| } | ||
|
|
||
| let proxyLengthReads = 0; | ||
| let proxyIndexReads = 0; | ||
| let proxyWrites = 0; | ||
| const proxy = new Proxy([], { | ||
| get(target: any, key: any, receiver: any) { | ||
| if (key === "length") { | ||
| proxyLengthReads++; | ||
| return 2 ** 32; | ||
| } | ||
| proxyIndexReads++; | ||
| return Reflect.get(target, key, receiver); | ||
| }, | ||
| set(target: any, key: any, value: any, receiver: any) { | ||
| proxyWrites++; | ||
| return Reflect.set(target, key, value, receiver); | ||
| } | ||
| }); | ||
| try { | ||
| Array.prototype.slice.call(proxy, 0, 2 ** 32); | ||
| console.log("proxy no throw"); | ||
| } catch (error) { | ||
| console.log( | ||
| "proxy", | ||
| error instanceof RangeError, | ||
| proxyLengthReads, | ||
| proxyIndexReads, | ||
| proxyWrites | ||
| ); | ||
| } | ||
|
|
||
| // A huge array-like is valid when the selected interval itself is small. | ||
| const tail: any = { length: 2 ** 32 + 1 }; | ||
| tail[2 ** 32] = "last"; | ||
| const selected = Array.prototype.slice.call(tail, -1); | ||
| console.log("tail", selected.length, selected[0]); | ||
| "#, | ||
| ) | ||
| .expect("write entry"); | ||
|
|
||
| let compile = Command::new(perry_bin()) | ||
| .current_dir(dir.path()) | ||
| .arg("compile") | ||
| .arg(&entry) | ||
| .arg("-o") | ||
| .arg(&output) | ||
| .arg("--no-cache") | ||
| .env("PERRY_LIB_DIR", &runtime_dir) | ||
| .env("PERRY_NO_AUTO_OPTIMIZE", "1") | ||
| .env("PERRY_RS4GC", "0") | ||
| .output() | ||
| .expect("run perry compile"); | ||
| assert!( | ||
| compile.status.success(), | ||
| "perry compile failed\nstdout:\n{}\nstderr:\n{}", | ||
| String::from_utf8_lossy(&compile.stdout), | ||
| String::from_utf8_lossy(&compile.stderr) | ||
| ); | ||
|
|
||
| let run = Command::new(&output) | ||
| .current_dir(dir.path()) | ||
| .output() | ||
| .expect("run compiled binary"); | ||
| assert!( | ||
| run.status.success(), | ||
| "compiled binary failed\nstdout:\n{}\nstderr:\n{}", | ||
| String::from_utf8_lossy(&run.stdout), | ||
| String::from_utf8_lossy(&run.stderr) | ||
| ); | ||
| assert_eq!( | ||
| String::from_utf8_lossy(&run.stdout), | ||
| concat!( | ||
| "plain true 0\n", | ||
| "aliased true\n", | ||
| "proxy true 1 0 0\n", | ||
| "tail 1 last\n" | ||
| ) | ||
| ); | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 32292
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 50370
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 28360
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 50369
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 50370
Root the input before boxed-primitive allocation.
to_objectpasses a heap string tojs_boxed_string_new, which allocates before rooting its copiedvalue. GC can evacuate the string, leavingvaluestale when wrapper installation uses the result. Root and reload the input before allocation, or updatejs_boxed_string_newto root and reload it.🤖 Prompt for AI Agents
Source: Learnings