Open Bug 1636015 Opened 6 years ago Updated 1 year ago

Adding anonymous content to WeakMap key causes crashes [@ mozilla::dom::TryPreserveWrapper ]

Categories

(Core :: DOM: Core & HTML, defect)

defect

Tracking

()

Tracking Status
firefox76 --- affected

People

(Reporter: emk, Unassigned)

Details

Steps to reproduce:

  1. Navigate to any web page.
  2. Open Browser Content Toolbox.
  3. Type into following scripts:
div = tabs[0].content.document.createElement("div");
div.textContent = "this is a test element";
anonymousContent = tabs[0].content.document.insertAnonymousContent(div);
wm = new WeakMap();
wm.set(anonymousContent, {});

Actual result:
Tab crashes.
https://crash-stats.mozilla.org/report/index/6952008a-005c-46aa-b371-fe21f0200507

Expected result:
Tab should not crash.

Summary: Adding anonymous content to WeakMap key causes crashes → Adding anonymous content to WeakMap key causes crashes [@ mozilla::dom::TryPreserveWrapper ]

Bug 1020244 added AnonymouseContent. Why is AnonymouseContent a non-wrapper cached CC participant? It contradicts this assertion. (addProperty is nullptr for non-wrapper cached classes.)

Flags: needinfo?(patrickbrosset+bugzilla)

I guess there wasn't any use case for AnonymousContent being wrapper cached. It was after all meant to be used by chrome JS only in such way that js keeps pointer to the object as long it needs it.
But it is https://bugzilla.mozilla.org/show_bug.cgi?id=1020244#c53 which caused dropping wrapper caching.

Is a WeakMap key counted as a reference to the object? ChromeUtils.nondeterministicGetWeakMapKeys() will regain the object access even if all other references are dropped.

I hit this bug when I was trying to fix bug 1482279.

(In reply to Masatoshi Kimura [:emk] from comment #3)

Is a WeakMap key counted as a reference to the object? ChromeUtils.nondeterministicGetWeakMapKeys() will regain the object access even if all other references are dropped.

A WeakMap reference that doesn't keep the object alive is not counted as a reference. ChromeUtils.nondeterministicGetWeakMapKeys() was intended as a way to write tests, so I generally haven't thought about what the impacts are. The CC does have to track weak maps individually already, so we could probably actually clear weak maps that we have decided are dead. I filed bug 1636170 on that.

Also, SpiderMonkey now supports actual weak references, so it might be worth considering converting uses of nondeterministicGetWeakMapKeys outside of weak map tests to use them. I'm not sure if they are riding to release yet, so it may have to wait.

Although, weak references may have the same issue and the CC is not aware of them, so I'm not sure we can fix it easily in the CC...

(In reply to Andrew McCreight [:mccr8] from comment #4)

ChromeUtils.nondeterministicGetWeakMapKeys() was intended as a way to write tests

To be clear, the affected code is test-only.
Without permissive COWs, we have to manage SpecialPowers proxies per global object. I tried using WeakMaps and hit this bug.

(clearing needinfo, apologies for not responding earlier, but I'm no longer participating actively in the development of the project)

Flags: needinfo?(patrickbrosset+bugzilla)
You need to log in before you can comment on or make changes to this bug.