Skip to content

Adopt smart pointers in Webkit/NetworkProcess #47134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

jelee53
Copy link
Contributor

@jelee53 jelee53 commented Jun 24, 2025

9dfdee9

Adopt smart pointers in Webkit/NetworkProcess
https://bugs.webkit.org/show_bug.cgi?id=294928
rdar://154227081

Reviewed by Per Arne Vollan.

Smart pointer adoption as per the static analyzer.

* Source/WebKit/NetworkProcess/Downloads/DownloadManager.h:
* Source/WebKit/NetworkProcess/NetworkDataTask.cpp:
(WebKit::NetworkDataTask::sessionID const): Deleted.
(WebKit::NetworkDataTask::networkSession const): Deleted.
(WebKit::NetworkDataTask::networkSession): Deleted.
(WebKit::NetworkDataTask::checkedNetworkSession): Deleted.
* Source/WebKit/NetworkProcess/NetworkDataTask.h:
(WebKit::NetworkDataTask::sessionID const):
(WebKit::NetworkDataTask::networkSession const):
(WebKit::NetworkDataTask::networkSession):
(WebKit::NetworkDataTask::checkedNetworkSession):
* Source/WebKit/NetworkProcess/NetworkLoad.cpp:
(WebKit::NetworkLoad::startWithScheduling):
(WebKit::NetworkLoad::shouldCaptureExtraNetworkLoadMetrics const):
(WebKit::NetworkLoad::isAllowedToAskUserForCredentials const):
(WebKit::NetworkLoad::willPerformHTTPRedirection):
(WebKit::NetworkLoad::didReceiveChallenge):
(WebKit::NetworkLoad::didReceiveInformationalResponse):
(WebKit::NetworkLoad::notifyDidReceiveResponse):
(WebKit::NetworkLoad::didReceiveData):
(WebKit::NetworkLoad::didCompleteWithError):
(WebKit::NetworkLoad::didSendData):
(WebKit::NetworkLoad::wasBlocked):
(WebKit::NetworkLoad::cannotShowURL):
(WebKit::NetworkLoad::wasBlockedByRestrictions):
(WebKit::NetworkLoad::wasBlockedByDisabledFTP):
* Source/WebKit/NetworkProcess/NetworkLoadChecker.cpp:
(WebKit::NetworkLoadChecker::contentSecurityPolicy):
* Source/WebKit/NetworkProcess/NetworkProcess.cpp:
(WebKit::NetworkProcess::addStorageSession):
(WebKit::NetworkProcess::deleteAndRestrictWebsiteDataForRegistrableDomains):
(WebKit::NetworkProcess::downloadRequest):
(WebKit::NetworkProcess::resumeDownload):
(WebKit::NetworkProcess::cancelDownload):
(WebKit::NetworkProcess::publishDownloadProgress):
(WebKit::NetworkProcess::findPendingDownloadLocation):
(WebKit::NetworkProcess::dataTaskWithRequest):
* Source/WebKit/SaferCPPExpectations/UncheckedCallArgsCheckerExpectations:
* Source/WebKit/SaferCPPExpectations/UncheckedLocalVarsCheckerExpectations:

Canonical link: https://commits.webkit.org/296755@main

3709c29

Misc iOS, visionOS, tvOS & watchOS macOS Linux Windows
✅ 🧪 style ✅ 🛠 ios ✅ 🛠 mac ✅ 🛠 wpe ✅ 🛠 win
✅ 🛠 ios-sim ✅ 🛠 mac-AS-debug ✅ 🧪 wpe-wk2 ⏳ 🧪 win-tests
✅ 🧪 webkitperl ✅ 🧪 ios-wk2 ✅ 🧪 api-mac ✅ 🧪 api-wpe
✅ 🧪 ios-wk2-wpt ✅ 🛠 wpe-cairo
✅ 🧪 api-ios ✅ 🧪 mac-wk2 ✅ 🛠 gtk
✅ 🛠 vision ✅ 🧪 mac-AS-debug-wk2 ✅ 🧪 gtk-wk2
✅ 🛠 vision-sim ✅ 🧪 mac-wk2-stress ✅ 🧪 api-gtk
✅ 🧪 vision-wk2 ✅ 🧪 mac-intel-wk2 🛠 playstation
✅ 🛠 🧪 unsafe-merge ✅ 🛠 tv ✅ 🛠 mac-safer-cpp
✅ 🛠 tv-sim
✅ 🛠 watch
✅ 🛠 watch-sim

@jelee53 jelee53 self-assigned this Jun 24, 2025
@jelee53 jelee53 added the New Bugs Unclassified bugs are placed in this component until the correct component can be determined. label Jun 24, 2025
@jelee53 jelee53 requested review from RupinMittal and pvollan June 24, 2025 21:06
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Jun 24, 2025
@webkit-ews-buildbot
Copy link
Collaborator

Safer C++ Build #41445 (f5843d0)

❌ Found 1 failing file with 1 issue. Please address these issues before landing. See WebKit Guidelines for Safer C++ Programming.
(cc @rniwa)

@jelee53 jelee53 removed the merging-blocked Applied to prevent a change from being merged label Jun 25, 2025
@jelee53 jelee53 force-pushed the eng/Adopt-smart-pointers-in-Webkit-NetworkProcess branch from f5843d0 to 09aac3d Compare June 25, 2025 05:01
@rniwa
Copy link
Member

rniwa commented Jun 25, 2025

Adopt smart pointers in Webkit/NetworkProcess
rdar://154227081
https://bugs.webkit.org/show_bug.cgi?id=294928

We typically place Bugzilla URL before radar.

return;
Ref scheduler = m_task->networkSession()->networkLoadScheduler();
Ref scheduler = CheckedPtr { task->networkSession() }->networkLoadScheduler();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, we typically want to add a helper function which returns a CheckedPtr so that we can rewrite this as:

Ref scheduler = task->checkedNetworkSession()->networkLoadScheduler();

@@ -89,9 +89,10 @@ void NetworkLoad::start()

void NetworkLoad::startWithScheduling()
{
if (!m_task || !m_task->networkSession())
RefPtr task = m_task;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can inline the contents of NetworkDataTask::networkSession() into the header file and avoid . i.e. move the following two functions in NetworkDataTask.cpp to NetworkDataTask.h. That'll make these functions to be "trivial" and static analyzer will no longer warn m_task being uncounted:

const NetworkSession* NetworkDataTask::networkSession() const
{
    return m_session.get();
}

NetworkSession* NetworkDataTask::networkSession()
{
    return m_session.get();
}

Copy link
Contributor Author

@jelee53 jelee53 Jun 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm seeing that this results in a circular dependency. I think it may not be possible.

EDIT: Rupin was able to help fix the circular dependency but forward declaring one of the "circular" classes. Functions have been moved to the header.

@jelee53 jelee53 force-pushed the eng/Adopt-smart-pointers-in-Webkit-NetworkProcess branch from 09aac3d to ba6fe0f Compare June 25, 2025 05:37
@jelee53 jelee53 force-pushed the eng/Adopt-smart-pointers-in-Webkit-NetworkProcess branch from ba6fe0f to 974c7a2 Compare June 26, 2025 00:06
Copy link
Contributor

@pvollan pvollan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

R=me.

@@ -537,7 +537,7 @@ ContentSecurityPolicy* NetworkLoadChecker::contentSecurityPolicy()
if (!m_contentSecurityPolicy && m_cspResponseHeaders) {
// FIXME: Pass the URL of the protected resource instead of its origin.
m_contentSecurityPolicy = makeUnique<ContentSecurityPolicy>(URL { protectedOrigin()->toRawString() }, nullptr, m_networkResourceLoader.get());
m_contentSecurityPolicy->didReceiveHeaders(*m_cspResponseHeaders, String { m_referrer }, ContentSecurityPolicy::ReportParsingErrors::No);
CheckedPtr { m_contentSecurityPolicy.get() }->didReceiveHeaders(*m_cspResponseHeaders, String { m_referrer }, ContentSecurityPolicy::ReportParsingErrors::No);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you also could add a helper function for this, e.g. checkedContentSecurityPolicy()?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The helper is added, but it should be used here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I don't think that can be done here. checkedContentSecurityPolicy() actually invokes contentSecurityPolicy() so there's going to be some recursive errors there.

That being said - @pvollan & I talked it over and it's probably just better to remove checkedContentSecurityPolicy() entirely because there's not much of a use case. contentSecurityPolicy() is a private function and all of its usages in this file are already safe usages (e.g CheckedPtr foo = contentSecurityPolicy() ... )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, yeah, you could use it here too as it's also null-checked above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chatted with @pvollan in one of the slack threads.

The consensus is that constructing a CheckedPtr at the call-site is probably the better move because if we created a CheckedPtr foo at the top of the function, it might have to be re-assigned later.

@jelee53 jelee53 force-pushed the eng/Adopt-smart-pointers-in-Webkit-NetworkProcess branch from 974c7a2 to 654e108 Compare June 26, 2025 19:16
@jelee53 jelee53 force-pushed the eng/Adopt-smart-pointers-in-Webkit-NetworkProcess branch from 654e108 to 9ccaade Compare June 26, 2025 21:36
@jelee53 jelee53 changed the title Adopt smart pointers in Webkit/NetworkProcess Adopt some smart pointers in Webkit/NetworkProcess Jun 26, 2025
@webkit-ews-buildbot
Copy link
Collaborator

Safer C++ Build #41732 (9ccaade)

❌ Found 1 failing file with 1 issue. Please address these issues before landing. See WebKit Guidelines for Safer C++ Programming.
(cc @rniwa)

if (!m_documentURL.isEmpty())
m_contentSecurityPolicy->setDocumentURL(m_documentURL);
}
return m_contentSecurityPolicy.get();
}

CheckedPtr<ContentSecurityPolicy> NetworkLoadChecker::checkedContentSecurityPolicy()
{
return CheckedPtr { contentSecurityPolicy() };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it sufficient with just return contentSecurityPolicy(); here?

@jelee53 jelee53 changed the title Adopt some smart pointers in Webkit/NetworkProcess Adopt smart pointers in Webkit/NetworkProcess Jun 27, 2025
@jelee53 jelee53 force-pushed the eng/Adopt-smart-pointers-in-Webkit-NetworkProcess branch from 9ccaade to 2ff4526 Compare June 27, 2025 00:42
@pvollan pvollan added the unsafe-merge-queue Applied to send a pull request to merge-queue, but skip building and testing label Jun 27, 2025
@@ -537,7 +537,7 @@ ContentSecurityPolicy* NetworkLoadChecker::contentSecurityPolicy()
if (!m_contentSecurityPolicy && m_cspResponseHeaders) {
// FIXME: Pass the URL of the protected resource instead of its origin.
m_contentSecurityPolicy = makeUnique<ContentSecurityPolicy>(URL { protectedOrigin()->toRawString() }, nullptr, m_networkResourceLoader.get());
m_contentSecurityPolicy->didReceiveHeaders(*m_cspResponseHeaders, String { m_referrer }, ContentSecurityPolicy::ReportParsingErrors::No);
CheckedPtr { m_contentSecurityPolicy.get() }->didReceiveHeaders(*m_cspResponseHeaders, String { m_referrer }, ContentSecurityPolicy::ReportParsingErrors::No);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The helper is added, but it should be used here.

@@ -2271,19 +2271,21 @@ void NetworkProcess::findPendingDownloadLocation(NetworkDataTask& networkDataTas
if (networkDataTask->state() == NetworkDataTask::State::Canceling || networkDataTask->state() == NetworkDataTask::State::Completed)
return;

if (downloadManager().download(downloadID)) {
CheckedRef downloadManagerCheckedRef = downloadManager();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
CheckedRef downloadManagerCheckedRef = downloadManager();
CheckedRef downloadManager = this->downloadManager();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Anne for this helpful suggestion!

I originally called it downloadManagerCheckedRef instead of downloadManager because the build couldn't distinguish between downloadManager vs downloadManager() (aka variable vs function). But if I'm explicitly doing this->, the computer knows that it's a member function and thus shadowing is avoided. Smart thinking!

@jelee53 jelee53 removed the unsafe-merge-queue Applied to send a pull request to merge-queue, but skip building and testing label Jun 27, 2025
@jelee53 jelee53 force-pushed the eng/Adopt-smart-pointers-in-Webkit-NetworkProcess branch from 2ff4526 to 3709c29 Compare June 27, 2025 18:21
@jelee53 jelee53 marked this pull request as ready for review June 27, 2025 19:14
@jelee53 jelee53 requested a review from cdumez as a code owner June 27, 2025 19:14
@pvollan pvollan requested review from rniwa and annevk June 27, 2025 19:32
@pvollan pvollan added the unsafe-merge-queue Applied to send a pull request to merge-queue, but skip building and testing label Jun 27, 2025
https://bugs.webkit.org/show_bug.cgi?id=294928
rdar://154227081

Reviewed by Per Arne Vollan.

Smart pointer adoption as per the static analyzer.

* Source/WebKit/NetworkProcess/Downloads/DownloadManager.h:
* Source/WebKit/NetworkProcess/NetworkDataTask.cpp:
(WebKit::NetworkDataTask::sessionID const): Deleted.
(WebKit::NetworkDataTask::networkSession const): Deleted.
(WebKit::NetworkDataTask::networkSession): Deleted.
(WebKit::NetworkDataTask::checkedNetworkSession): Deleted.
* Source/WebKit/NetworkProcess/NetworkDataTask.h:
(WebKit::NetworkDataTask::sessionID const):
(WebKit::NetworkDataTask::networkSession const):
(WebKit::NetworkDataTask::networkSession):
(WebKit::NetworkDataTask::checkedNetworkSession):
* Source/WebKit/NetworkProcess/NetworkLoad.cpp:
(WebKit::NetworkLoad::startWithScheduling):
(WebKit::NetworkLoad::shouldCaptureExtraNetworkLoadMetrics const):
(WebKit::NetworkLoad::isAllowedToAskUserForCredentials const):
(WebKit::NetworkLoad::willPerformHTTPRedirection):
(WebKit::NetworkLoad::didReceiveChallenge):
(WebKit::NetworkLoad::didReceiveInformationalResponse):
(WebKit::NetworkLoad::notifyDidReceiveResponse):
(WebKit::NetworkLoad::didReceiveData):
(WebKit::NetworkLoad::didCompleteWithError):
(WebKit::NetworkLoad::didSendData):
(WebKit::NetworkLoad::wasBlocked):
(WebKit::NetworkLoad::cannotShowURL):
(WebKit::NetworkLoad::wasBlockedByRestrictions):
(WebKit::NetworkLoad::wasBlockedByDisabledFTP):
* Source/WebKit/NetworkProcess/NetworkLoadChecker.cpp:
(WebKit::NetworkLoadChecker::contentSecurityPolicy):
* Source/WebKit/NetworkProcess/NetworkProcess.cpp:
(WebKit::NetworkProcess::addStorageSession):
(WebKit::NetworkProcess::deleteAndRestrictWebsiteDataForRegistrableDomains):
(WebKit::NetworkProcess::downloadRequest):
(WebKit::NetworkProcess::resumeDownload):
(WebKit::NetworkProcess::cancelDownload):
(WebKit::NetworkProcess::publishDownloadProgress):
(WebKit::NetworkProcess::findPendingDownloadLocation):
(WebKit::NetworkProcess::dataTaskWithRequest):
* Source/WebKit/SaferCPPExpectations/UncheckedCallArgsCheckerExpectations:
* Source/WebKit/SaferCPPExpectations/UncheckedLocalVarsCheckerExpectations:

Canonical link: https://commits.webkit.org/296755@main
@webkit-commit-queue webkit-commit-queue force-pushed the eng/Adopt-smart-pointers-in-Webkit-NetworkProcess branch from 3709c29 to 9dfdee9 Compare June 27, 2025 22:00
@webkit-commit-queue
Copy link
Collaborator

Committed 296755@main (9dfdee9): https://commits.webkit.org/296755@main

Reviewed commits have been landed. Closing PR #47134 and removing active labels.

@webkit-commit-queue webkit-commit-queue merged commit 9dfdee9 into WebKit:main Jun 27, 2025
@webkit-commit-queue webkit-commit-queue removed the unsafe-merge-queue Applied to send a pull request to merge-queue, but skip building and testing label Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
New Bugs Unclassified bugs are placed in this component until the correct component can be determined.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants