Skip to content

In components/script, remove unsafe JS related code #30862

Open
0 of 2 issues completed
Open

Description

@gterzian

Roadmap Issue

Description:
Spidermonkey decoupling, lessen unsafe code in script. Move unsafe SM related code out of script.

Area: Script

People: @gterzian @Taym95


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:

  1. Remove direct use of js::jsapi in components/script, outside of that found in dom/bindings.
  2. Remove direct use of js::jsapi dom/bindings, by expending the collection of safe wrappers found in js::rust, and forbid unsafe code in components/script.
  3. Remove direct use of js::rust in components/script, and replace it with our own Servo-specific abstraction all to be found in dom/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

Sub-issues

0 of 2 Issues completed
Loading 2 sub-issues

Activity

  1. added
    I-safety
    A-content/script
    B-meta
    L-javascript
    on Dec 12, 2023
  2. gterzian commented on Dec 18, 2023

    @gterzian
    MemberAuthor

    Couple of notes on JS objects.

    DomObject seems 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 inside use js::jsapi::Heap(although one can get a higher-level js::rust::HandleObject to 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 the Reflector concept.

    We could hide it's use, and the use of JS_ functions, in bindings.


    Elsewhere, it appears we are returning JSObject as 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 return

    fn GetChannelData(&self, cx: JSContext, channel: u32) -> Fallible<NonNull<JSObject>> {

    It appears codegen falls back to JSObject if nothing more specific is defined, see

    if returnType.isObject() or returnType.isSpiderMonkeyInterface():

    So in some ways, one approach is to look at our implementation of the WebIDL, and replace the use of JSObject with something more specific when appropriate. WebIDL doesn't seem to include anything like a JSObject, see https://webidl.spec.whatwg.org/#idl-object

  3. lwz23 commented on Sep 25, 2024

    @lwz23
    Contributor

    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.

  4. gterzian commented on Sep 25, 2024

    @gterzian
    MemberAuthor

    Ok, thanks! This the meta issue, so you would have to pick a sub-issue. @Taym95 do you have a good one in mind?

  5. Taym95 commented on May 9, 2025

    @Taym95
    Member

    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.

    Tests with unexpected results:
      ▶ CRASH [expected OK] /css/compositing/canvas-composite-modes.html
      │ 
      └ Caught signal 11 in thread "Script(1,1)"
    ================================================================================
    Rerunning 1 tests with unexpected results to detect flaky tests.
    Running 1 tests in web-platform-tests
    
      ▶ CRASH [expected OK] /css/compositing/canvas-composite-modes.html
      │ 
      └ Caught signal 11 in thread "Script(1,1)"
    
  6. added
    B-roadmap
    on Apr 27, 2026
  7. 26 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-content/scriptB-metaB-roadmapI-safetyL-javascript

    Type

    No type

    Projects

    • Status
      Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions