Bug 213333 - Text manipulation should observe adjacent elements with new renderer together
Summary: Text manipulation should observe adjacent elements with new renderer together
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: HTML Editing (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Sihui Liu
URL:
Keywords: InRadar
Depends on:
Blocks: 213318
  Show dependency treegraph
 
Reported: 2020-06-17 23:52 PDT by Sihui Liu
Modified: 2020-06-26 09:42 PDT (History)
13 users (show)

See Also:


Attachments
Patch (18.09 KB, patch)
2020-06-18 00:26 PDT, Sihui Liu
no flags Details | Formatted Diff | Diff
Patch (19.84 KB, patch)
2020-06-18 10:52 PDT, Sihui Liu
no flags Details | Formatted Diff | Diff
Patch (18.41 KB, patch)
2020-06-22 14:04 PDT, Sihui Liu
no flags Details | Formatted Diff | Diff
Patch for landing (18.50 KB, patch)
2020-06-26 09:18 PDT, Sihui Liu
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Sihui Liu 2020-06-17 23:52:55 PDT
...
Comment 1 Sihui Liu 2020-06-18 00:26:48 PDT
Created attachment 402190 [details]
Patch
Comment 2 Wenson Hsieh 2020-06-18 09:03:58 PDT
Comment on attachment 402190 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=402190&action=review

> Source/WebCore/dom/Node.h:675
> +    bool m_isManipulated { false };

This overall approach seems reasonable to me, but we should avoid making Node larger (dump-class-layout on WebCore::Node with release  shows 72 bytes with no padding). We should probably put it in NodeRareData instead.
Comment 3 Darin Adler 2020-06-18 10:35:22 PDT
Comment on attachment 402190 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=402190&action=review

>> Source/WebCore/dom/Node.h:675
>> +    bool m_isManipulated { false };
> 
> This overall approach seems reasonable to me, but we should avoid making Node larger (dump-class-layout on WebCore::Node with release  shows 72 bytes with no padding). We should probably put it in NodeRareData instead.

That’s right. We must not add this bool to Node.
Comment 4 Sihui Liu 2020-06-18 10:52:05 PDT
Created attachment 402216 [details]
Patch
Comment 5 Sihui Liu 2020-06-18 10:55:29 PDT
Moved m_isManipulated to NodeRareData and reduced size of m_connectedFrameCount.
Comment 6 Ryosuke Niwa 2020-06-19 23:56:12 PDT
Comment on attachment 402216 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=402216&action=review

> Source/WebCore/ChangeLog:15
> +        To fix this issue completely, this patch adds a property isManipulated to NodeRareData. Now 
> +        TextManipulationController can know the state of all types of Nodes and thus can skip the manipulated Nodes 
> +        during observation.

Hm... using a bit in NodeRareData isn't great because
allocating NodeRareData would consume quite a bit of memory for each element.
Why not just replace m_manipulatedElements with m_manipulatedNodes?

Since we never de-reference objects in m_manipulatedElements, it's okay to store a stale pointer.
We could also make it void* or some kind of a new smart pointer type that disallows dereferencing if we wanted.
Comment 7 Darin Adler 2020-06-21 12:36:05 PDT
(In reply to Ryosuke Niwa from comment #6)
> Hm... using a bit in NodeRareData isn't great because
> allocating NodeRareData would consume quite a bit of memory for each element.
> Why not just replace m_manipulatedElements with m_manipulatedNodes?

That sounds smart.

> Since we never de-reference objects in m_manipulatedElements, it's okay to
> store a stale pointer.

I don’t think that’s right.

As far as I know, there is *nothing* safe you can do with pointers to deleted objects, even if they are not dereferenced, except maybe comparing to null. For example, comparing a pointer to a deleted object with a pointer to another object could give an incorrect result. A stale pointer can happen to be equal to a pointer to a new object that is allocated re-using the same memory.
Comment 8 Ryosuke Niwa 2020-06-21 13:39:31 PDT
(In reply to Darin Adler from comment #7)
> (In reply to Ryosuke Niwa from comment #6)
> > Since we never de-reference objects in m_manipulatedElements, it's okay to
> > store a stale pointer.
> 
> I don’t think that’s right.
> 
> As far as I know, there is *nothing* safe you can do with pointers to
> deleted objects, even if they are not dereferenced, except maybe comparing
> to null. For example, comparing a pointer to a deleted object with a pointer
> to another object could give an incorrect result. A stale pointer can happen
> to be equal to a pointer to a new object that is allocated re-using the same
> memory.

Sure, but that's still safe in this case. The worst thing that could happen is that we'd mistakenly think the content had already been manipulated and fail to notify the client. It's still safe in the sense of security guarantee although the program may still misbehave (by not reacting to newly inserted content) if we had such a bug.
Comment 9 Darin Adler 2020-06-21 13:42:46 PDT
(In reply to Ryosuke Niwa from comment #8)
> It's still safe in the sense of security
> guarantee although the program may still misbehave (by not reacting to newly
> inserted content) if we had such a bug.

OK, but could we use something like WeakPtr and have predictable behavior instead?
Comment 10 Sihui Liu 2020-06-22 14:04:18 PDT
Created attachment 402511 [details]
Patch
Comment 11 Sihui Liu 2020-06-22 14:07:00 PDT
Uploaded a new patch using m_manipulatedNodes and removing node from set when node is destroyed.
Comment 12 Geoffrey Garen 2020-06-25 14:02:22 PDT
Comment on attachment 402511 [details]
Patch

r=me

I slightly prefer the rare data solution, since I like it when you don't have to worry about doing interesting pointer safety things in your destructor. Also, you only pay the memory cost when you use the feature, which won't happen on most pages.

That said, I think this approach works, and resolves all review comments so far. We can always discuss the rare data approach more when we have a chance, and reconsider it.
Comment 13 Ryosuke Niwa 2020-06-25 17:40:52 PDT
I think the long term solution is to add more bit flags directly on Node and/or make it possible to create a WeakPtr to Node. I have vague plans for both.
Comment 14 Sihui Liu 2020-06-26 09:18:12 PDT
Created attachment 402873 [details]
Patch for landing
Comment 15 EWS 2020-06-26 09:41:19 PDT
Committed r263563: <https://trac.webkit.org/changeset/263563>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 402873 [details].
Comment 16 Radar WebKit Bug Importer 2020-06-26 09:42:19 PDT
<rdar://problem/64809758>
Comment 17 Radar WebKit Bug Importer 2020-06-26 09:42:22 PDT
<rdar://problem/64809761>