Skip to content

Crash when queuing a mutation record #32885

Closed

Description

@eerii

Describe the bug:

Servo crashes when editing or removing a tag in the DevTools inspector.

This comes from script/dom/element.rs::remove_first_matching_attribute, which calls script/dom/mutationobserver.rs::queue_a_mutation_record. The problematic check is in the first line:

if !target.global().as_window().get_exists_mut_observer() {
	return;
}

This calls an unsafe function down the stack, which ends up crashing the program. It calls the GetCurrentRealmOrNull from mozjs. The last call seems to be to JSContext::realm().

If this check is removed the crash goes away and everything seems to work, but I imagine that it is there for a reason and that there is a problem with how the node was constructed, it might be missing some information. But I may be a bit outside my depth here since I don't have that much experience with mozjs or realms.

To Reproduce:

  • Launch Servo with the DevTools server enabled: ./mach run --devtools=1234
  • In Firefox, go to about:debugging and connect to localhost:1234 (or the port number you specified)
  • Click inspect on one of the tabs, and go to the inspector tab
  • Edit one of the tag attributes (for example, change <p style="color:red;"> to <p style="color:blue;">)
  • Servo crashes

Platform:

Linux 6.10 (NixOS 24.05), happening on main (29a4cca).

Crash log
assertion failed: !GetCurrentRealmOrNull(*cx).is_null() (thread Script(1,1), at components/script/realms.rs:24)
   0: servoshell::backtrace::print
             at /persist/home/eri/Code/servo/ports/servoshell/backtrace.rs:18:5
   1: servoshell::panic_hook::panic_hook
             at /persist/home/eri/Code/servo/ports/servoshell/panic_hook.rs:38:17
   2: core::ops::function::Fn::call
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/ops/function.rs:79:5
   3: <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/alloc/src/boxed.rs:2034:9
      std::panicking::rust_panic_with_hook
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:783:13
   4: std::panicking::begin_panic_handler::{{closure}}
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:649:13
   5: std::sys_common::backtrace::__rust_end_short_backtrace
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/backtrace.rs:171:18
   6: rust_begin_unwind
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:645:5
   7: core::panicking::panic_fmt
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panicking.rs:72:14
   8: core::panicking::panic
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panicking.rs:145:5
   9: script::realms::AlreadyInRealm::assert_for_cx
             at /persist/home/eri/Code/servo/components/script/realms.rs:24:13
  10: script::dom::bindings::reflector::DomObject::global
             at /persist/home/eri/Code/servo/components/script/dom/bindings/reflector.rs:103:21
  11: script::dom::mutationobserver::MutationObserver::queue_a_mutation_record
             at /persist/home/eri/Code/servo/components/script/dom/mutationobserver.rs:136:13
  12: script::dom::element::Element::remove_first_matching_attribute::{{closure}}
             at /persist/home/eri/Code/servo/components/script/dom/element.rs:1628:13
  13: core::option::Option<T>::map
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/option.rs:1073:29
  14: script::dom::element::Element::remove_first_matching_attribute
             at /persist/home/eri/Code/servo/components/script/dom/element.rs:1615:9
  15: script::dom::element::Element::remove_attribute_by_name
             at /persist/home/eri/Code/servo/components/script/dom/element.rs:1607:9
  16: <script::dom::element::Element as script::dom::bindings::codegen::Bindings::ElementBinding::Element_Binding::ElementMethods>::RemoveAttribute
             at /persist/home/eri/Code/servo/components/script/dom/element.rs:2282:9
  17: script::devtools::handle_modify_attribute
             at /persist/home/eri/Code/servo/components/script/devtools.rs:223:21
  18: script::script_thread::ScriptThread::handle_msg_from_devtools
             at /persist/home/eri/Code/servo/components/script/script_thread.rs:2518:17
  19: script::script_thread::ScriptThread::handle_msgs::{{closure}}
             at /persist/home/eri/Code/servo/components/script/script_thread.rs:1959:48
  20: script::script_thread::ScriptThread::profile_event
             at /persist/home/eri/Code/servo/components/script/script_thread.rs:2214:13
  21: script::script_thread::ScriptThread::handle_msgs
             at /persist/home/eri/Code/servo/components/script/script_thread.rs:1951:26
  22: script::script_thread::ScriptThread::start
             at /persist/home/eri/Code/servo/components/script/script_thread.rs:1454:15
  23: <script::script_thread::ScriptThread as script_layout_interface::ScriptThreadFactory>::create::{{closure}}::{{closure}}
             at /persist/home/eri/Code/servo/components/script/script_thread.rs:845:25
  24: profile_traits::mem::ProfilerChan::run_with_memory_reporting
             at /persist/home/eri/Code/servo/components/shared/profile/mem.rs:91:9
  25: <script::script_thread::ScriptThread as script_layout_interface::ScriptThreadFactory>::create::{{closure}}
             at /persist/home/eri/Code/servo/components/script/script_thread.rs:843:17
  26: std::sys_common::backtrace::__rust_begin_short_backtrace
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/backtrace.rs:155:18
  27: std::thread::Builder::spawn_unchecked_::{{closure}}::{{closure}}
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/thread/mod.rs:528:17
  28: <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panic/unwind_safe.rs:272:9
  29: std::panicking::try::do_call
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:552:40
  30: __rust_try
  31: std::panicking::try
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:516:19
  32: std::panic::catch_unwind
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panic.rs:146:14
      std::thread::Builder::spawn_unchecked_::{{closure}}
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/thread/mod.rs:527:30
  33: core::ops::function::FnOnce::call_once{{vtable.shim}}
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/ops/function.rs:250:5
  34: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/alloc/src/boxed.rs:2020:9
      <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/alloc/src/boxed.rs:2020:9
      std::sys::pal::unix::thread::Thread::new::thread_start
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys/pal/unix/thread.rs:108:17
  35: start_thread
  36: __GI___clone3

Activity

  1. added
    C-untriaged
    on Jul 30, 2024
  2. mrobinson commented on Jul 30, 2024

    @mrobinson
    Member

    Where is the entry point from DevTools into the script code? Perhaps we just need to make sure to enter a realm before making any modifications to the DOM?

  3. eerii commented on Jul 30, 2024

    @eerii
    MemberAuthor

    Where is the entry point from DevTools into the script code? Perhaps we just need to make sure to enter a realm before making any modifications to the DOM?

    I think it is handle_msg_from_devtools from script/script_tread, which calls the functions in script/devtools. The messages are sent from each of the devtools actors using an IpcSender.

  4. gterzian commented on Jul 30, 2024

    @gterzian
    Member

    make sure to enter a realm before making any modifications to the DOM

    Sounds like it.

    Example:

    let _realm = enter_realm(document.window());

  5. eerii commented on Jul 30, 2024

    @eerii
    MemberAuthor

    That was it! I added this to handle_modify_attribute and it no longer crashes. It makes sense since we are modifying the DOM. I'm going to fix the DevTools reply and I'll open a PR.

    diff --git a/components/script/devtools.rs b/components/script/devtools.rs
    index 597ed25dfd..a5f8c4ea22 100644
    --- a/components/script/devtools.rs
    +++ b/components/script/devtools.rs
    @@ -198,6 +198,11 @@ pub fn handle_modify_attribute(
         node_id: String,
         modifications: Vec<Modification>,
     ) {
    +    let Some(document) = documents.find_document(pipeline) else {
    +        return warn!("document for pipeline id {} is not found", &pipeline);
    +    };
    +    let _realm = enter_realm(document.window());
    +
         let node = match find_node_by_unique_id(documents, pipeline, &node_id) {
             None => {
                 return warn!(
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

    C-untriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions