Repository navigation
Description
Roadmap Issue
Description:
Spidermonkey decoupling, lessen unsafe code in script. Move unsafe SM related code out of script.
Area: Script
In script, you can find unsafe blocks peppered around, most(all?) of which relate to integration with the JS engine.
Integration takes place via mozjs, which consists of two crates: one of which is used as js::rust and is safe, the other is used as js::jsapi and is lower-level and unsafe.
There are three things we should do:
- Remove direct use of
js::jsapiincomponents/script, outside of that found indom/bindings. - Remove direct use of
js::jsapidom/bindings, by expending the collection of safe wrappers found injs::rust, and forbid unsafe code incomponents/script. - Remove direct use of
js::rustincomponents/script, and replace it with our own Servo-specific abstraction all to be found indom/bindings.
The goal of 1 and 2 is to remove unsafe code, either by expanding safe wrappers in js::rust or in dom/bindings while the goal of 3 is to make the use of the JS engine more modular. While 1 and 2 can be done in parallel, 3 should only be done after 1 and 2 are finished(I think).
Here is one example:
In AudioBuffer, we use js::jsapi::Heap directly, because there is no safe or higher-level equivalent defined in js::rust(see for example safe wrappers found in mozjs/src/gc/root.rs)
An unsafe call of Heap::handle is found here, it is necessary because of the use of the unsafe JS_GetArrayBufferViewBuffer.
So somehow, we should wrap that pattern in something safe, and hide it from view either in js::rust, or in dom/bindings.
Marking this as a Meta because we could open additional issues with more specifics(or open PR's directly that do no close this issue).
Sub-issues
Collapse Sub-issuesSub-issues
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsTodo
Activity
gterzian commentedon Dec 18, 2023
Couple of notes on JS objects.
DomObjectseems to be what WedIDL calls a Platform Object, in other words it's a safe Rust struct that implements a particular web interface, made available to running script via bindings to Spidermonkey.On the other hand, we also have
js::jsapi::JSObject, which is a raw javascript object. A pointer to it is often found insideuse js::jsapi::Heap(although one can get a higher-leveljs::rust::HandleObjectto it). These raw js objects seem to be used across script, often in combination with other unsafe api, for example here. in other places, it is hidden, for example behind theReflectorconcept.We could hide it's use, and the use of JS_ functions, in bindings.
Elsewhere, it appears we are returning
JSObjectas a default from WebIDL methods, while in fact more specific objects could be returned. For example the AudioBufferInterface returns a Float32Array from getChannelData, whereas our we returnservo/components/script/dom/audiobuffer.rs
Line 265 in 5c8fc73
It appears codegen falls back to JSObject if nothing more specific is defined, see
servo/components/script/dom/bindings/codegen/CodegenRust.py
Line 1517 in 5c8fc73
So in some ways, one approach is to look at our implementation of the WebIDL, and replace the use of
JSObjectwith something more specific when appropriate. WebIDL doesn't seem to include anything like a JSObject, see https://webidl.spec.whatwg.org/#idl-objectlwz23 commentedon Sep 25, 2024
I would like to try this issue, if there is no one working on it. Currently, iI'm working on detect the unsafe code use in Rust. So, I'm interested in this issue.
gterzian commentedon Sep 25, 2024
Ok, thanks! This the meta issue, so you would have to pick a sub-issue. @Taym95 do you have a good one in mind?
Taym95 commentedon May 9, 2025
After #31398 and the removal of unsafe from ImageData, we may have introduced a crash, though it's not always reproducible. The use of auto_root might fix it, but let's keep an eye on it, and if it appears again, let's open an issue and fix it.
script: use auto_root! for structuredclone transfer lists (servo#48148)
webxr: Replace unsafe transform casting (servo#48177)
26 remaining items
script: use auto_root! for structuredclone transfer lists (servo#48148)
webxr: Replace unsafe transform casting (servo#48177)
Make FileReader result access safe (servo#48179)
script: Safely root ByteLengthQueuingStrategy callback values (servo#…
script: use safe sequence conversion for tee cancellation reasons (se…
Make IndexedDB key-range conversion safe (servo#48212)
script: Move decodeAudioData buffer handling behind BufferSource (ser…
Make DOMMatrix number serialization safe (servo#48211)
Use safe rooted handles in stream callbacks (servo#48226)
Use MallocShallowSizeOf for Brotli stream state (servo#48220)
Replace WebGPU mapped view storage with HeapBufferSource<ArrayBufferU…
Make MaybeUnreflectedDom constructors safe (servo#48356)
Use rooted handle for TrustedScript conversion (servo#48354)
encoding: switch to explicit dom string replacement in text encoder (s…
encoding: switch to explicit dom string replacement in text encoder (…