Bug 198814 - WebResourceLoadStatisticsStore should not use its network session if invalidated
Summary: WebResourceLoadStatisticsStore should not use its network session if invalidated
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Page Loading (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: youenn fablet
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2019-06-12 16:57 PDT by youenn fablet
Modified: 2019-06-14 16:05 PDT (History)
7 users (show)

See Also:


Attachments
Patch (3.50 KB, patch)
2019-06-12 17:09 PDT, youenn fablet
no flags Details | Formatted Diff | Diff
Patch (4.29 KB, patch)
2019-06-12 17:40 PDT, youenn fablet
no flags Details | Formatted Diff | Diff
Patch (10.46 KB, patch)
2019-06-14 12:33 PDT, youenn fablet
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description youenn fablet 2019-06-12 16:57:15 PDT
WebResourceLoadStatisticsStore should not use its network session if invalidated
Comment 1 youenn fablet 2019-06-12 17:09:34 PDT
Created attachment 372002 [details]
Patch
Comment 2 youenn fablet 2019-06-12 17:10:08 PDT
<rdar://problem/49608908>
Comment 3 Geoffrey Garen 2019-06-12 17:16:51 PDT
Comment on attachment 372002 [details]
Patch

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

> Source/WebKit/ChangeLog:4
> +        WebResourceLoadStatisticsStore should not use its network session if invalidated
> +        https://bugs.webkit.org/show_bug.cgi?id=198814

What kind of bug does this fix? Can we test it?

> Source/WebKit/NetworkProcess/Classifier/WebResourceLoadStatisticsStore.cpp:1027
> +void WebResourceLoadStatisticsStore::invalidateAndCancel()
> +{
> +    m_networkSession = nullptr;
> +}

Is the purpose here to avoid further updates, or to guard against use after free? If use after free, I'd suggest WeakPtr instead.

> Source/WebKit/NetworkProcess/NetworkSession.cpp:104
> +    if (m_resourceLoadStatistics)
> +        m_resourceLoadStatistics->invalidateAndCancel();

For whatever updates we were receiving before this patch, can we add assertions that we aren't updated after being invalidated?
Comment 4 youenn fablet 2019-06-12 17:30:44 PDT
(In reply to Geoffrey Garen from comment #3)
> Comment on attachment 372002 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=372002&action=review
> 
> > Source/WebKit/ChangeLog:4
> > +        WebResourceLoadStatisticsStore should not use its network session if invalidated
> > +        https://bugs.webkit.org/show_bug.cgi?id=198814
> 
> What kind of bug does this fix? Can we test it?

The issue as I understand it is the following:
- NetworkSession is invalidated so is removed from the network process map. Its storage session is also removed.
- NetworkSession stays alive as it is refed by other objects.
- Its WebResourceLoadStatisticsStore is still processing some tasks, for instance in a background thread and will hop back to the main thread
- When in the main thread, WebResourceLoadStatisticsStore tries to get its storage session from its NetworkSession. Since NetworkSession is invalidated, there is no more a storage session and we are hitting a RELEASE_ASSERT.

Testing this is probably inherently racy. I think the idea would be to:
1. Create a network session ID
2. Trigger some WebResourceLoadStatisticsStore background processing
3. Delete the network session ID as soon as possible.
Not sure about how to trigger step 2, probably Brent or John would know.

> > Source/WebKit/NetworkProcess/Classifier/WebResourceLoadStatisticsStore.cpp:1027
> > +void WebResourceLoadStatisticsStore::invalidateAndCancel()
> > +{
> > +    m_networkSession = nullptr;
> > +}
> 
> Is the purpose here to avoid further updates, or to guard against use after
> free? If use after free, I'd suggest WeakPtr instead.

The idea is to avoid further updates, given its storage session is gone.

m_networkSession is already a WeakPtr. Although this patch makes WeakPtr unnecessary, it seems good to keep it as a WeakPtr instead of using a raw pointer.

> > Source/WebKit/NetworkProcess/NetworkSession.cpp:104
> > +    if (m_resourceLoadStatistics)
> > +        m_resourceLoadStatistics->invalidateAndCancel();
> 
> For whatever updates we were receiving before this patch, can we add
> assertions that we aren't updated after being invalidated?

Good idea. While I do not think this can happen now, this could happen in the future.
Comment 5 youenn fablet 2019-06-12 17:40:47 PDT
Created attachment 372009 [details]
Patch
Comment 6 John Wilander 2019-06-12 17:53:55 PDT
Comment on attachment 372009 [details]
Patch

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

One way to test this would be to schedule a task with a fixed timeout, invalidate, and make sure we exit gracefully. It could be a dummy task for testing that uses the ITP background thread logic.

> Source/WebKit/NetworkProcess/Classifier/WebResourceLoadStatisticsStore.cpp:1024
> +void WebResourceLoadStatisticsStore::invalidateAndCancel()

"AndCancel" sounds a little too open ended. Cancel what? Could we go with something like "AndCancelBackgroundTasks"?

> Source/WebKit/NetworkProcess/Classifier/WebResourceLoadStatisticsStore.cpp:1026
> +    m_networkSession = nullptr;

Setting this to null automatically cancels everything?
Comment 7 Brent Fulgham 2019-06-13 17:15:34 PDT
Comment on attachment 372009 [details]
Patch

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

I think this looks like a reasonable change. However, It's not marked for review, so I haven't r+'d it.

>> Source/WebKit/NetworkProcess/Classifier/WebResourceLoadStatisticsStore.cpp:1026
>> +    m_networkSession = nullptr;
> 
> Setting this to null automatically cancels everything?

m_networkSession is just a WeakPtr, so I don't think anything will get cancelled. However, it will prevent all of the methods in this class to return early without doing work.
Comment 8 Brent Fulgham 2019-06-13 17:17:20 PDT
Comment on attachment 372002 [details]
Patch

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

>>> Source/WebKit/NetworkProcess/Classifier/WebResourceLoadStatisticsStore.cpp:1027
>>> +}
>> 
>> Is the purpose here to avoid further updates, or to guard against use after free? If use after free, I'd suggest WeakPtr instead.
> 
> The idea is to avoid further updates, given its storage session is gone.
> 
> m_networkSession is already a WeakPtr. Although this patch makes WeakPtr unnecessary, it seems good to keep it as a WeakPtr instead of using a raw pointer.

Agreed. Nulling it out seems fine. If we happen to know that m_networkSession is soon to be invalid, clearing it ourselves seems like a good idea.
Comment 9 youenn fablet 2019-06-13 17:19:06 PDT
(In reply to Brent Fulgham from comment #7)
> Comment on attachment 372009 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=372009&action=review
> 
> I think this looks like a reasonable change. However, It's not marked for
> review, so I haven't r+'d it.

I plan to write a test that will (flakily) cover this case.

> >> Source/WebKit/NetworkProcess/Classifier/WebResourceLoadStatisticsStore.cpp:1026
> >> +    m_networkSession = nullptr;
> > 
> > Setting this to null automatically cancels everything?
> 
> m_networkSession is just a WeakPtr, so I don't think anything will get
> cancelled. However, it will prevent all of the methods in this class to
> return early without doing work.

Right, it is cancelling all actions related to NetworkSession/NetworkSessionStorage, all IPC...
It is not cancelling postTask/completion handler things.
It is also not cancelling actions related to memory/persistent store which as I see it is probably fine.
Comment 10 youenn fablet 2019-06-14 12:33:32 PDT
Created attachment 372135 [details]
Patch
Comment 11 Geoffrey Garen 2019-06-14 14:59:26 PDT
Comment on attachment 372135 [details]
Patch

r=me
Comment 12 WebKit Commit Bot 2019-06-14 16:05:37 PDT
Comment on attachment 372135 [details]
Patch

Clearing flags on attachment: 372135

Committed r246449: <https://trac.webkit.org/changeset/246449>
Comment 13 WebKit Commit Bot 2019-06-14 16:05:39 PDT
All reviewed patches have been landed.  Closing bug.