Skip to content

Don't panic when retrieving slices from detached typed arrays - #780

Merged
jdm merged 7 commits into
servo:mainfrom
jdm:detached
Jul 19, 2026
Merged

Don't panic when retrieving slices from detached typed arrays#780
jdm merged 7 commits into
servo:mainfrom
jdm:detached

Conversation

@jdm

@jdm jdm commented Jul 14, 2026

Copy link
Copy Markdown
Member

By using Option<NonNull<[..]>> instead of *mut [..] we can ensure that we always check for detached buffers and don't need to panic.

Testing: New unit test for detached ArrayBuffer.
Servo PR: servo/servo#46504

@sagudev

sagudev commented Jul 14, 2026

Copy link
Copy Markdown
Member

BTW Here are some notes about this from other PR: #775 (comment)

@jdm

jdm commented Jul 14, 2026

Copy link
Copy Markdown
Member Author

I believe this is addressing that note; we now always return an empty slice for detached buffers, and servo/servo#46069 helps centralize that logic even more.

Comment thread mozjs/src/typedarray.rs Outdated
@sagudev

sagudev commented Jul 14, 2026

Copy link
Copy Markdown
Member

I believe this is addressing that note; we now always return an empty slice for detached buffers, and servo/servo#46069 helps centralize that logic even more.

My only worry is that we collapse detached/not computed into empty slice, which can hide logical problems elsewhere (like using detached buffer) which should not occur if we follow the spec (and the spec is correctly written).

@jdm

jdm commented Jul 14, 2026

Copy link
Copy Markdown
Member Author

I believe this is addressing that note; we now always return an empty slice for detached buffers, and servo/servo#46069 helps centralize that logic even more.

My only worry is that we collapse detached/not computed into empty slice, which can hide logical problems elsewhere (like using detached buffer) which should not occur if we follow the spec (and the spec is correctly written).

I'm confused, since https://webidl.spec.whatwg.org/#dfn-get-buffer-source-copy already collapses that result. Is the concern that code that doesn't use that algorithm may have logic that treats detached array buffers differently, and we won't be able to implement that correctly?

@sagudev

sagudev commented Jul 14, 2026

Copy link
Copy Markdown
Member

I believe this is addressing that note; we now always return an empty slice for detached buffers, and servo/servo#46069 helps centralize that logic even more.

My only worry is that we collapse detached/not computed into empty slice, which can hide logical problems elsewhere (like using detached buffer) which should not occur if we follow the spec (and the spec is correctly written).

I'm confused, since https://webidl.spec.whatwg.org/#dfn-get-buffer-source-copy already collapses that result. Is the concern that code that doesn't use that algorithm may have logic that treats detached array buffers differently, and we won't be able to implement that correctly?

Yeah, but maybe I am just paranoid. Wasn't there a website where one could find all IsDetachedBuffer in all specs?

@jdm

jdm commented Jul 14, 2026

Copy link
Copy Markdown
Member Author

I'm trying to find out what that site is on Matrix, because I have forgotten it. However, I did come across https://w3c.github.io/IndexedDB/#convert-a-value-to-a-key

@jdm

jdm commented Jul 14, 2026

Copy link
Copy Markdown
Member Author

@jdm

jdm commented Jul 14, 2026

Copy link
Copy Markdown
Member Author

Perhaps it's enough to add an API to determine if an array buffer is detached, though.

@sagudev

sagudev commented Jul 14, 2026

Copy link
Copy Markdown
Member

Perhaps it's enough to add an API to determine if an array buffer is detached, though.

but this bring us again to

can hide logical problems elsewhere (like using detached buffer) which should not occur if we follow the spec (and the spec is correctly written).

I consider such crashes as good as one could easily see what is wrong (missing detached buffer check) and how it needs to be fixed.

@jdm

jdm commented Jul 14, 2026

Copy link
Copy Markdown
Member Author

I don't like relying on a panic for this, because it only manifests if test code or a webpage triggers it. If we want to force callers to consider this case, we should change as_slice/as_slice_mut to return an enum instead. servo/servo#46069 means that won't be a huge annoyance for callsites any more.

@sagudev

sagudev commented Jul 14, 2026

Copy link
Copy Markdown
Member

I don't like relying on a panic for this, because it only manifests if test code or a webpage triggers it.

Fair.

If we want to force callers to consider this case, we should change as_slice/as_slice_mut to return an enum instead. servo/servo#46069 means that won't be a huge annoyance for callsites any more.

That would be ideal.

@TimvdLippe

Copy link
Copy Markdown

Yeah, but maybe I am just paranoid. Wasn't there a website where one could find all IsDetachedBuffer in all specs?

Yes that website is https://dontcallmedom.github.io/webdex/i.html although I don't see it listed

@sagudev

sagudev commented Jul 15, 2026

Copy link
Copy Markdown
Member

Yeah, but maybe I am just paranoid. Wasn't there a website where one could find all IsDetachedBuffer in all specs?

Yes that website is https://dontcallmedom.github.io/webdex/i.html although I don't see it listed

Thanks, bookmarked. It's here: https://dontcallmedom.github.io/webdex/i.html#IsDetachedBuffer%40%40ecmascript%25%25abstract-op although references are very vague.

pull Bot pushed a commit to Haofei/servo that referenced this pull request Jul 18, 2026
…nions. (servo#46605)

In preparation for the changes from
servo/mozjs#780, this enables replacing more
hand-rolled logic for interacting with typed arrays with the
consolidated spec logic in
[get_buffer_source_copy](https://webidl.spec.whatwg.org/#dfn-get-buffer-source-copy).

Testing: Existing tests suffice.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
jdm added 7 commits July 19, 2026 12:34
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
…etached.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
@jdm
jdm added this pull request to the merge queue Jul 19, 2026
@jdm
jdm removed this pull request from the merge queue due to a manual request Jul 19, 2026
@jdm
jdm enabled auto-merge July 19, 2026 16:37
@jdm
jdm added this pull request to the merge queue Jul 19, 2026
Merged via the queue into servo:main with commit f5cbf8a Jul 19, 2026
46 checks passed
pull Bot pushed a commit to AKJUS/servo that referenced this pull request Jul 20, 2026
…servo#46504)

Updates mozjs to a revision that includes
servo/mozjs#780.

Testing: Blob tests don't panic any more.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants