WebKit icon indicating copy to clipboard operation
WebKit copied to clipboard

Implement GC handling for trusted types API

Open lukewarlow opened this issue 1 year ago โ€ข 5 comments

69378bcdefcff22417914a427298160ae5dddd75

Implement GC handling for trusted types API
https://bugs.webkit.org/show_bug.cgi?id=268419

Reviewed by NOBODY (OOPS!).

This patch implements visitAdditionalChildren for TrustedTypePolicy calling visitJSFunction on callbacks.
TrustedTypePolicy is also an opaque root which is added inside of TrustedTypePolicyFactory visitAdditionalChildren.
It also marks TrustedTypePolicyFactory as being reachable via scriptExecutionContext.

* Source/WebCore/Sources.txt:
* Source/WebCore/WebCore.xcodeproj/project.pbxproj:
* Source/WebCore/bindings/js/JSTrustedTypePolicyCustom.cpp: Added.
(WebCore::JSTrustedTypePolicy::visitAdditionalChildren):
* Source/WebCore/bindings/js/JSTrustedTypePolicyFactoryCustom.cpp: Added.
(WebCore::JSTrustedTypePolicyFactory::visitAdditionalChildren):
* Source/WebCore/dom/TrustedTypePolicy.cpp:
(WebCore::TrustedTypePolicy::TrustedTypePolicy):
(WebCore::TrustedTypePolicy::createHTML):
(WebCore::TrustedTypePolicy::createScript):
(WebCore::TrustedTypePolicy::createScriptURL):
(WebCore::root):
* Source/WebCore/dom/TrustedTypePolicy.h:
(WebCore::TrustedTypePolicy::options const):
(WebCore::TrustedTypePolicy::WTF_RETURNS_LOCK):
* Source/WebCore/dom/TrustedTypePolicy.idl:
* Source/WebCore/dom/TrustedTypePolicyFactory.cpp:
(WebCore::TrustedTypePolicyFactory::create):
(WebCore::TrustedTypePolicyFactory::TrustedTypePolicyFactory):
* Source/WebCore/dom/TrustedTypePolicyFactory.h:
(WebCore::TrustedTypePolicyFactory::WTF_RETURNS_LOCK):
* Source/WebCore/dom/TrustedTypePolicyFactory.idl:
* Source/WebCore/dom/WindowOrWorkerGlobalScopeTrustedTypes.cpp:
(WebCore::DOMWindowTrustedTypes::trustedTypes const):
(WebCore::WorkerGlobalScopeTrustedTypes::trustedTypes const):

https://github.com/WebKit/WebKit/commit/69378bcdefcff22417914a427298160ae5dddd75

Misc iOS, tvOS & watchOS macOS Linux Windows
โœ… ๐Ÿงช style โœ… ๐Ÿ›  ios โœ… ๐Ÿ›  mac โœ… ๐Ÿ›  wpe โœ… ๐Ÿ›  wincairo
โœ… ๐Ÿงช bindings โœ… ๐Ÿ›  ios-sim โœ… ๐Ÿ›  mac-AS-debug โœ… ๐Ÿงช wpe-wk2
โœ… ๐Ÿงช webkitperl โœ… ๐Ÿงช ios-wk2 โœ… ๐Ÿงช api-mac โŒ ๐Ÿงช api-wpe
โœ… ๐Ÿงช ios-wk2-wpt โœ… ๐Ÿงช mac-wk1 โœ… ๐Ÿ›  gtk
โŒ ๐Ÿงช api-ios โœ… ๐Ÿงช mac-wk2 โœ… ๐Ÿงช gtk-wk2
โœ… ๐Ÿ›  tv โŒ ๐Ÿงช mac-AS-debug-wk2 โœ… ๐Ÿงช api-gtk
โœ… ๐Ÿ›  tv-sim
โœ… ๐Ÿ›  watch
โœ… ๐Ÿ›  watch-sim

lukewarlow avatar Feb 07 '24 13:02 lukewarlow

@rniwa is this the sort of changes you had in mind for GC handling of the trusted types API? It doesn't solve one of the memory issues that's come up so I think something is missing but not sure what exactly it is.

lukewarlow avatar Feb 07 '24 13:02 lukewarlow

I think there's a problem with the TrustedTypePolicyOptions callbacks. They're by default strong references which makes them GC roots which is a memory leak footgun.

I would suggest since you're already implementing visitAdditionalChildren to make them weak refs (the internal IDL attribute is IsWeakCallback) and manage their memory via the owner class (TrustedTypesPolicy's visitAdditionalChildren).

rreno avatar Mar 12 '24 18:03 rreno

I would suggest since you're already implementing visitAdditionalChildren to make them weak refs (the internal IDL attribute is IsWeakCallback) and manage their memory via the owner class (TrustedTypesPolicy's visitAdditionalChildren).

Okay I'll take a look into this and address it tomorrow

lukewarlow avatar Mar 12 '24 18:03 lukewarlow

The style linter wants me to name 3 variables the same thing and protector or protectedNullptr doesn't really feel great either way? I'm not sure what the appropriate thing to do in that case is.

lukewarlow avatar Mar 15 '24 21:03 lukewarlow

The style linter wants me to name 3 variables the same thing and protector or protectedNullptr doesn't really feel great either way? I'm not sure what the appropriate thing to do in that case is.

I think something like protected<type>Handler is reasonable.

rreno avatar Mar 15 '24 21:03 rreno

It's this style rule: https://webkit.org/code-style-guidelines/#names-protectors

rreno avatar Mar 15 '24 21:03 rreno

It's this style rule: https://webkit.org/code-style-guidelines/#names-protectors

Even if I change them to be called protectedCreateXXX it still complains that it's incorrectly named?

lukewarlow avatar Mar 15 '24 21:03 lukewarlow

It's this style rule: https://webkit.org/code-style-guidelines/#names-protectors

Even if I change them to be called protectedCreateXXX it still complains that it's incorrectly named?

The checker is probably getting confused because we're accessing members of m_options. protectedCreateXXX is more correct IMO. protectedNullptr is just silly.

Ah it's getting confused because we're explicitly setting it to nullptr. If you just default construct the smart pointers (RefPtr<Type> protectedCreateWhatever;) it will probably pass.

rreno avatar Mar 15 '24 21:03 rreno

LGTM, thanks Luke!

rreno avatar Mar 27 '24 16:03 rreno

Safe-Merge-Queue: Build #16768.

webkit-ews-buildbot avatar Apr 02 '24 21:04 webkit-ews-buildbot

Committed 276974@main (3fe3c3955914): https://commits.webkit.org/276974@main

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

webkit-commit-queue avatar Apr 02 '24 22:04 webkit-commit-queue