Bug 165839

Summary: Deleting a character converted from pinyin after an image causes a Safari crash
Product: WebKit Reporter: Ryosuke Niwa <rniwa>
Component: HTML EditingAssignee: Ryosuke Niwa <rniwa>
Status: RESOLVED FIXED    
Severity: Normal CC: andersca, ap, bdakin, commit-queue, dbates, enrica, wenson_hsieh
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Fixes the crash
none
Updated per review comments
none
Added a API test
none
Added a API test
none
Patch for landing none

Description Ryosuke Niwa 2016-12-13 22:04:51 PST
Deleting a Chinese character typed using pinyin on Mac on the same line as an image causes Safari to crash.
Comment 1 Ryosuke Niwa 2016-12-13 22:05:53 PST
<rdar://problem/27951933>
Comment 2 Ryosuke Niwa 2016-12-13 22:13:28 PST
Created attachment 297066 [details]
Fixes the crash
Comment 3 Wenson Hsieh 2016-12-14 08:39:34 PST
Comment on attachment 297066 [details]
Fixes the crash

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

> Source/WebKit2/ChangeLog:11
> +        Fixed it buy omitting the image as done in r176412 since encoding NSFileWrapper, etc... would require

Nit - buy => by
Comment 4 Alexey Proskuryakov 2016-12-14 13:05:00 PST
Comment on attachment 297066 [details]
Fixes the crash

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

> ChangeLog:8
> +        Added a manual test. I tried but I couldn't make textInputController to trigger the crash.

Very surprising! textInputController can do everything that an input method does.

> Source/WebKit2/ChangeLog:12
> +        quite a bit of work, and IME doesn't really need to see the image in its attributed string.

Is the change in string length and character offsets observable?
Comment 5 Ryosuke Niwa 2016-12-14 13:50:46 PST
(In reply to comment #4)
> Comment on attachment 297066 [details]
> Fixes the crash
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=297066&action=review
> 
> > ChangeLog:8
> > +        Added a manual test. I tried but I couldn't make textInputController to trigger the crash.
> 
> Very surprising! textInputController can do everything that an input method
> does.

I think Chinese IME somehow calls into WKWebView via one of the standard AppKit NSView selectors.

> > Source/WebKit2/ChangeLog:12
> > +        quite a bit of work, and IME doesn't really need to see the image in its attributed string.
> 
> Is the change in string length and character offsets observable?

I can’t think of a way.
Comment 6 Tim Horton 2016-12-14 17:14:26 PST
Comment on attachment 297066 [details]
Fixes the crash

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

> Source/WebKit2/ChangeLog:15
> +        Also converted assertions in typeFromObject and encode inside ArgumentCodersMac to release assertions
> +        to crash early inside a WebProcess instead of killing the entire UIProcess for a similar bug in the future.

I don't think this is the right fix for this part of the problem: the UI process should guard against this crash (and kill the offending Web process), not the Web process (otherwise, a compromised Web process could still cause this).
Comment 7 Ryosuke Niwa 2016-12-14 19:34:01 PST
(In reply to comment #6)
> Comment on attachment 297066 [details]
> Fixes the crash
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=297066&action=review
> 
> > Source/WebKit2/ChangeLog:15
> > +        Also converted assertions in typeFromObject and encode inside ArgumentCodersMac to release assertions
> > +        to crash early inside a WebProcess instead of killing the entire UIProcess for a similar bug in the future.
> 
> I don't think this is the right fix for this part of the problem: the UI
> process should guard against this crash (and kill the offending Web
> process), not the Web process (otherwise, a compromised Web process could
> still cause this).

Okay, I’d just revert this part of the change then since I’m not familiar with IPC / WebKit2 enough to make that change.
Comment 8 Ryosuke Niwa 2016-12-14 19:46:32 PST
Created attachment 297160 [details]
Updated per review comments
Comment 9 Alexey Proskuryakov 2016-12-15 09:47:28 PST
> I think Chinese IME somehow calls into WKWebView via one of the standard AppKit NSView selectors.

An input method can't do that, they are out of process and limited by API provided to them. AppKit itself can, of course. If it's something it does, the right thing to do it to make that testable.

Additionally, commands can be sent via -doCommandBySelector: - this is not quite the same as calling WebHTMLView methods directly, but could be close enough to trigger the crash.
Comment 10 Ryosuke Niwa 2016-12-15 17:42:26 PST
(In reply to comment #9)
> > I think Chinese IME somehow calls into WKWebView via one of the standard AppKit NSView selectors.
> 
> An input method can't do that, they are out of process and limited by API
> provided to them. AppKit itself can, of course. If it's something it does,
> the right thing to do it to make that testable.
> 
> Additionally, commands can be sent via -doCommandBySelector: - this is not
> quite the same as calling WebHTMLView methods directly, but could be close
> enough to trigger the crash.

In that case, I think it's probably better if someone who knows about AppKit worked on this bug. I really don't know how to do that.
Comment 11 Ryosuke Niwa 2016-12-16 18:57:20 PST
It turns out that we can just call attributedSubstringForProposedRange on WKWebView and it’d reproduce the crash since the code doesn’t depend on any states related to input methods. After a whole day of fiddling with the exact condition, I’ve successfully made a WebKit API test.
Comment 12 Ryosuke Niwa 2016-12-16 19:01:14 PST
Created attachment 297380 [details]
Added a API test
Comment 13 Ryosuke Niwa 2016-12-16 19:03:56 PST
Created attachment 297381 [details]
Added a API test
Comment 14 Wenson Hsieh 2016-12-16 19:12:58 PST
Comment on attachment 297381 [details]
Added a API test

Test looks good to me!
Comment 15 Ryosuke Niwa 2016-12-16 19:40:09 PST
Created attachment 297385 [details]
Patch for landing
Comment 16 WebKit Commit Bot 2016-12-16 20:04:45 PST
Comment on attachment 297385 [details]
Patch for landing

Clearing flags on attachment: 297385

Committed r209957: <http://trac.webkit.org/changeset/209957>
Comment 17 WebKit Commit Bot 2016-12-16 20:04:52 PST
All reviewed patches have been landed.  Closing bug.