Skip to content

Comments

refactor(allocator): refactor and improve safty comments of String::from_strs_array_in#9639

Merged
graphite-app[bot] merged 1 commit intomainfrom
03-07-refactor_allocator_optimize_and_improve_safty_comments_of_string_from_strs_array_in_
Mar 10, 2025
Merged

refactor(allocator): refactor and improve safty comments of String::from_strs_array_in#9639
graphite-app[bot] merged 1 commit intomainfrom
03-07-refactor_allocator_optimize_and_improve_safty_comments_of_string_from_strs_array_in_

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Mar 10, 2025

Follow-on after #9329.

More fully document the constraints that ensure the safety of String::from_strs_array_in.

The implementation in #9329 was already sound, but the comments didn't prove that (and in fact without checking the docs for ptr::copy_nonoverlapping and *mut T::add, I wasn't sure that it was sound if some of the input strings are zero length).

Also add a debug assertion to check the pointer calculations are correct.

Also refactor:

Use String::from_utf8_unchecked to construct the eventual String, instead of String::from_raw_parts_in. The 2 are currently equivalent, but allocator_api2::vec::Vec does not guaranteed that with_capacity_in won't allocate more bytes than requested. If it does, String::from_utf8_unchecked makes use of that spare capacity in the String, whereas String::from_raw_parts_in(ptr, len, len, allocator) doesn't.

That wasn't a soundness hole because both Vec and String are non-Drop, but it would have been a potential memory leak if they were.

Copy link
Member Author


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions github-actions bot added the C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior label Mar 10, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Mar 10, 2025

CodSpeed Performance Report

Merging #9639 will not alter performance

Comparing 03-07-refactor_allocator_optimize_and_improve_safty_comments_of_string_from_strs_array_in_ (44101bd) with main (ae7bb75)

Summary

✅ 39 untouched benchmarks

@overlookmotel overlookmotel force-pushed the 03-07-refactor_allocator_optimize_and_improve_safty_comments_of_string_from_strs_array_in_ branch from 8d9618b to c394d37 Compare March 10, 2025 06:53
@overlookmotel overlookmotel changed the title refactor(allocator): optimize and improve safty comments of String::from_strs_array_in refactor(allocator): refactor and improve safty comments of String::from_strs_array_in Mar 10, 2025
@overlookmotel overlookmotel marked this pull request as ready for review March 10, 2025 07:39
@overlookmotel
Copy link
Member Author

I think we can remove some branches by relying on the fact that &str cannot be longer than isize::MAX bytes. But I got cold feet about doing that. I'm not completely sure that is a 100% guarantee in all cases. Have asked on Rust lang forum: https://users.rust-lang.org/t/does-str-reliably-have-length-isize-max/126777

@overlookmotel overlookmotel requested a review from Dunqing March 10, 2025 07:45
@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label Mar 10, 2025
@graphite-app
Copy link
Contributor

graphite-app bot commented Mar 10, 2025

Merge activity

…from_strs_array_in` (#9639)

Follow-on after #9329.

More fully document the constraints that ensure the safety of `String::from_strs_array_in`.

The implementation in #9329 was already sound, but the comments didn't prove that (and in fact without checking the docs for `ptr::copy_nonoverlapping` and `*mut T::add`, I wasn't sure that it *was* sound if some of the input strings are zero length).

Also add a debug assertion to check the pointer calculations are correct.

Also refactor:

Use `String::from_utf8_unchecked` to construct the eventual `String`, instead of `String::from_raw_parts_in`. The 2 are currently equivalent, but `allocator_api2::vec::Vec` does not guaranteed that `with_capacity_in` won't allocate *more* bytes than requested. If it does, `String::from_utf8_unchecked` makes use of that spare capacity in the `String`, whereas `String::from_raw_parts_in(ptr, len, len, allocator)` doesn't.

That wasn't a soundness hole because both `Vec` and `String` are non-`Drop`, but it would have been a potential memory leak if they were.
@graphite-app graphite-app bot force-pushed the 03-07-refactor_allocator_optimize_and_improve_safty_comments_of_string_from_strs_array_in_ branch from c394d37 to 44101bd Compare March 10, 2025 09:16
@Dunqing
Copy link
Member

Dunqing commented Mar 10, 2025

Wow, thanks for taking time improving it, I learned a lot to about writing detailed safety comments!

@graphite-app graphite-app bot merged commit 44101bd into main Mar 10, 2025
33 checks passed
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Mar 10, 2025
@graphite-app graphite-app bot deleted the 03-07-refactor_allocator_optimize_and_improve_safty_comments_of_string_from_strs_array_in_ branch March 10, 2025 09:23
Boshen added a commit that referenced this pull request Mar 11, 2025
## [0.57.0] - 2025-03-11

- 510446a parser: [**BREAKING**] Align JSXNamespacedName with ESTree
(#9648) (Arnaud Barré)

- b0d979d semantic: [**BREAKING**] Make `Scoping::references` private
(#9629) (Boshen)

- 3c6f140 semantic: [**BREAKING**] Make `Scoping` methods consistent
(#9628) (Boshen)

- ef6e0cc semantic: [**BREAKING**] Combine `SymbolTable` and `ScopeTree`
into `Scoping` (#9615) (Boshen)

- 7331656 semantic: [**BREAKING**] Rename `SymbolTable` and `ScopeTree`
methods (#9613) (Boshen)

- 23738bf semantic: [**BREAKING**] Introduce `Scoping` (#9611) (Boshen)

### Features

- b6deff8 ecmascript: Support integer index access for array and string
in `may_have_side_effects` (#9603) (sapphi-red)
- 047fb01 minifier: Place `void 0` on right hand side if possible
(#9606) (sapphi-red)
- 36f8703 minifier: Compress `[] + string` to `string` (#9602)
(sapphi-red)
- 554c4ce minifier: Compress constant integer index access (#9604)
(sapphi-red)
- e3c2015 minifier: Allow compressing computed __proto__ more precisely
(#9595) (sapphi-red)
- 6a57198 minifier: Allow compressing computed constructor/prototype
keys precisely (#9594) (sapphi-red)
- 638007b parser: Apply `preserveParens` to `TSParenthesizedType`
(#9653) (Boshen)

### Bug Fixes

- eae1a41 ast: Align `TSImportType` field names with ts-eslint (#9664)
(Boshen)
- 96eef8b ecmascript: `(foo() + "").length` may have a side effect
(#9605) (sapphi-red)
- 24d9261 minifier: Remove names from functions / classes in normal pass
to make the minifier idempotent (#9608) (sapphi-red)
- 6ac3635 napi/parser: Disable raw transfer on unsupported platforms
(#9651) (overlookmotel)
- cfdcfdb parser: Fix end span for optional binding pattern without type
annotation (#9652) (Boshen)
- 26da65d parser: Parse asi after class accessor property (#9623)
(Boshen)
- 87462fd parser: Fix end span for `using` declaration (#9622) (Boshen)
- 29edb51 transformer: Fix module runner transform of export default
expression (#9661) (hi-ogawa)

### Documentation

- 31a2618 allocator: Add safety constraint for
`String::from_raw_parts_in` (#9640) (overlookmotel)

### Refactor

- 44101bd allocator: Refactor and improve safty comments of
`String::from_strs_array_in` (#9639) (overlookmotel)
- c6edafe napi: Remove `npm/oxc-*/` npm packages (#9631) (Boshen)
- a43c341 semantic: Add `Scoping` to `Semantic` (#9614) (Boshen)

Co-authored-by: Boshen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants