Make gfxSkipCharsIterator stricter about its mSkipChars member being non-null
Categories
(Core :: Layout: Text and Fonts, task)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox108 | --- | wontfix |
People
(Reporter: dholbert, Assigned: dholbert)
References
(Blocks 1 open bug)
Details
Attachments
(4 obsolete files)
Some of our crash reports in nsTextFrame::ReflowText (bug 1686398) look a little bit like a null-deref when working with a local gfxSkipCharsIterator. For example, these two...
bp-8eb228eb-8397-4074-8f2d-3fd5a0220807
bp-b43d9bdf-c403-4de3-bbea-e03e20221003
...have a null-deref crash at the last line here (the call to SetSkippedOffset):
https://searchfox.org/mozilla-central/rev/c5c002f81f08a73e04868e0c2bf0eb113f200b03/layout/generic/nsTextFrame.cpp#9692-9696
gfxSkipCharsIterator end(provider.GetEndHint());
end.SetSkippedOffset(transformedOffset + transformedCharsFit);
The gfxSkipCharsIterator does have a pointer member-var, mSkipChars, so it's reasonable to suspect that maybe it's somehow null here and that's causing trouble.
We can adjust things a bit to guarantee that that pointer is never-null, to remove it as a source of suspicion here. Let's do that.
| Assignee | ||
Comment 1•4 years ago
|
||
This patch shouldn't change behavior.
This lets us remove the rarely-used no-arg constructor for gfxSkipCharsIterator
in the next patch. (That constructor was an alternate representation of an
invalid gfxSkipCharsIterator; but the Maybe<> representation added here makes
things a bit easier to reason about.)
| Assignee | ||
Comment 2•4 years ago
|
||
This patch doesn't change behavior.
This no-arg constructor only had one inadvertent usage -- it was used to
default-initialize one of ClusterIterator's member-vars, just before we
reassign it to something else in the constructor body. (I'm fixing that here
by explicitly constructing that member-var up-front in the init list, rather
than in the constructor body.)
Depends on D159902
| Assignee | ||
Comment 3•4 years ago
|
||
This patch doesn't change behavior; it's just using the type system to make it
clearer that this variable never gets assigned to a null pointer. (In fact, its
value is always initialized using the address of a C++ reference, which by
definition is never null.)
Note: we can't make the mSkipChars member itself into a C++ reference here;
that would prevent gfxSkipCharsIterator instances from being reassignable
(since C++ references can't be rebound), which would be a problem since we do
in fact reassign gfxSkipCharsIterator instances in a few places. In contrast,
this patch's NotNull-pointer mSkipChars does allow itself to be reassigned,
which in turn allows gfxSkipCharsIterator to remain reassignable.
Depends on D159903
| Assignee | ||
Comment 4•4 years ago
|
||
In practice, it's only instantiated on the stack; and that's kinda important,
to reduce the likelihood that it somehow outlives the 'gfxSkipChars' instance
that it maintains a raw pointer to and is iterating over.
Comment 6•4 years ago
|
||
| bugherder | ||
https://hg.mozilla.org/mozilla-central/rev/a2763d3ad250
https://hg.mozilla.org/mozilla-central/rev/d43777f8ef79
https://hg.mozilla.org/mozilla-central/rev/b7ee22f54a6c
https://hg.mozilla.org/mozilla-central/rev/f9d2d5f60fe9
| Assignee | ||
Comment 7•4 years ago
|
||
Hmm, might have to back this out; it looks like part 1 resulted in a small regression in the svg-text-getExtentOfChar-1.html microbenchmark. It's believable that using Maybe<> for validity is slightly slower than the invalidation mechanism that we had there.
Unfortunately that means parts 2 and 3 need to be backed out as well, since they're only possible if we have part 1.
| Assignee | ||
Comment 8•4 years ago
•
|
||
It seems possible that the two...
mSkipCharsIterator = Some(...)
...assignments in part 1 are producing an extra copy operation (where we have to generate a temporary Maybe instance as part of Some(...), only to copy its contents and then throw it away).
That might be adding some overhead here. Let me do some try runs with a patch to adjust those to use mSkipCharsIterator.reset(); mSkipCharsIterator.emplace(...); instead. (That's actually what I initially had, but it was a bit more verbose than the = Some(...) formulation.)
| Assignee | ||
Comment 9•4 years ago
|
||
(I'm testing the reset()/emplace() formulation on Try now; see bug 1797711 comment 2.)
| Assignee | ||
Updated•4 years ago
|
| Assignee | ||
Comment 11•4 years ago
|
||
Thanks, imoraru! Backed out to fix the perf regression, per bug 1797711 comment 5; I'm hoping to re-land an amended fix in the next day or two.
Updated•4 years ago
|
Comment 12•4 years ago
|
||
Comment on attachment 9299585 [details]
Bug 1796697 part 2: Remove gfxSkipCharsIterator's dummy no-arg constructor, as well as its associated now-unnecessary "IsInitialized()" API. r?jfkthame
Revision D159903 was moved to bug 1798953. Setting attachment 9299585 [details] to obsolete.
Updated•4 years ago
|
Comment 13•4 years ago
|
||
Comment on attachment 9299622 [details]
Bug 1796697 part 2: Annotate gfxSkipCharsIterator as MOZ_STACK_CLASS. r?jfkthame
Revision D159922 was moved to bug 1798953. Setting attachment 9299622 [details] to obsolete.
| Assignee | ||
Comment 14•4 years ago
|
||
The perf regression was associated with part 1 here. I spun off bug 1798953 to re-land some of the pieces here that weren't dependent on that.
| Assignee | ||
Comment 15•4 years ago
•
|
||
I'll post my results from a handful of explorations here. (For each one, I'll include relevant perfherder links from Try, comparing the experiment against a "baseline" revision which is using the unmodified m-c commit that the tested commit is based off of.)
In all of these, I'm specifically focusing on the svg-text-getExtentOfChar-1.html subtest (part of the "talos-perf-reftest-singletons" set of tests), which gets us into calls SVGTextFrame::GetExtentOfChar tens of thousands of times (hammering the code in part 1). I'll be focusing on macOS and Windows shippable builds (ignoring other configurations), since the regression was reported for macOS, and since most of our users are on Windows, and during my investigation I found that one possible approach happens to be specifically-bad on Windows for some reason (see (3) and (4) below).
(1) The originally-landed version of "part 1" does indeed show a ~20% regression on mac and ~10% regression on Windows.
(2) If I change the patch to use newly-introduced uncheckedEmplace/uncheckedRef APIs on Maybe<T> (non-asserting versions of the existing emplace/ref APIs), the regression goes away (the difference shrinks below 1%) on mac and windows.
This confirms the theory that the asserts themselves (not some other part of Maybe<>) are responsible for the perf regression here.
This uncheckedEmplace/uncheckedRef approach would be one possible way forward here, but I'm not pushing for it, since I don't think this bug's patches on their own are enough to merit opening this loophole around the guarantees that Maybe<>'s assertions provide right now, given that this bug itself is a bit speculative with no guarantee of any improvement in our crash data.
I had another idea of a way forward, though it didn't pan out:
(3) As a less-loopholey version of the above: I tried using a new uncheckedEmplace() just at instantiation time, but instead of adding & using uncheckedRef, I just called regular Maybe<T>::ref() once and then cached a pointer to the value that it returns, and re-used that pointer to avoid the assertion on subsequent usages. (This feels ~safe since this instance never needs to be reset or re-emplaced). That somehow gets us a small 6.83% improvement vs. baseline on mac, but still a 14% regression on Windows. I suspect the windows regression is just due to the fact that each usage has to chase a pointer to find the location of the object, even though it turns out the pointer is always pointing to the same extremely-local spot. This pointer-chasing must incur some sort of penalty or disable some sort of optimization.
(4) Confirming that theory about the reason for the Windows regression: I tried a patch with no Maybe usage at all, but just using a pointer to access the preexisting local gfxSkipCharsIterator member-var. As in (3), this inexplicably gave us a small speedup (now 3.8%) vs. baseline on macOS, vs. a substantial penalty (19%) on Windows.
It also occurred to me that perhaps there was a chance my "baseline" try runs got particularly lucky/unlucky in terms of a PGO run on some platform, so in some cases I repeated the try push in case it came out differently. But generally it did not come out differently; and I ran enough retriggers to be sure that outliers wouldn't be a concern. So, the measurements above are fairly robust.
In light of all the above, I don't have a ton of confidence that any particular approach here (aside from the guarantee-relaxing uncheckedEmplace()/uncheckedRef()) will be perf-neutral on all platforms. Given that plus the fact that this bug was kinda speculative anyway, I'm going to close this as WONTFIX and won't purse the attached patches further. If we have other ideas for how to potentially proceed here and tighten up the guarantees around gfxSkipCharsIterator validity, we can pursue them in fresh bug reports.
Updated•4 years ago
|
Updated•4 years ago
|
Updated•4 years ago
|
Description
•