Skip to content

Commit deeedf4

Browse files
David Kilzerddkilzer
authored andcommitted
[ASan] Generate a crash log when a service worker fails to terminate
<https://bugs.webkit.org/show_bug.cgi?id=327943> <rdar://190313539> Reviewed by Zak Ridouh. When a service worker does not stop within its termination timeout, `SWContextManager::serviceWorkerFailedToTerminate()` exits the process without a crash report, so run-webkit-tests reports a ServiceWorkerProcess crash with no crash log and no backtrace of the worker thread that failed to stop. In ASan builds on Cocoa platforms, call `abort_with_reason()` instead, which writes a crash report with every thread's backtrace and a reason that names this termination path. Move `WebKit::ReasonCode` to WTF as `WTF::WebKitReasonCode` so WebCore and WebKit share one set of codes in the `OS_REASON_WEBKIT` namespace, and add a code for this path. No new tests since this change is not directly testable. * Source/WTF/WTF.xcodeproj/project.pbxproj: * Source/WTF/wtf/CMakeLists.txt: * Source/WTF/wtf/PlatformCocoa.cmake: * Source/WTF/wtf/darwin/WebKitReasonCode.h: Add. * Source/WebCore/workers/service/context/SWContextManager.cpp: (WebCore::SWContextManager::serviceWorkerFailedToTerminate): * Source/WebKit/Platform/IPC/cocoa/ConnectionCocoa.mm: (IPC::Connection::kill): * Source/WebKit/Platform/cocoa/XPCUtilities.h: * Source/WebKit/Platform/cocoa/XPCUtilities.mm: (WebKit::terminateWithReason): * Source/WebKit/UIProcess/Cocoa/AuxiliaryProcessProxyCocoa.mm: (WebKit::AuxiliaryProcessProxy::platformStartConnectionTerminationWatchdog): * Source/WebKit/UIProcess/Launcher/cocoa/ProcessLauncherCocoa.mm: (WebKit::ProcessLauncher::terminateXPCConnection): Canonical link: https://commits.webkit.org/324071@main
1 parent 539e018 commit deeedf4

10 files changed

Lines changed: 73 additions & 15 deletions

File tree

‎Source/WTF/WTF.xcodeproj/project.pbxproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
39D12598FEA545039883C96C /* OrderedHashSet.h in Headers */ = {isa = PBXBuildFile; fileRef = B6397A84DB2844D9A8644961 /* OrderedHashSet.h */; settings = {ATTRIBUTES = (Private, ); }; };
114114
3A1337AA28F915B400F29B73 /* UniqueRefVector.h in Headers */ = {isa = PBXBuildFile; fileRef = 3A1337A928F915B400F29B73 /* UniqueRefVector.h */; settings = {ATTRIBUTES = (Private, ); }; };
115115
3A5BCFA3298768E300E0ABB2 /* RefVector.h in Headers */ = {isa = PBXBuildFile; fileRef = 3A5BCFA2298768E300E0ABB2 /* RefVector.h */; settings = {ATTRIBUTES = (Private, ); }; };
116+
260A79A260060345C45191CE /* WebKitReasonCode.h in Headers */ = {isa = PBXBuildFile; fileRef = 4908EC45E9463252E413D60C /* WebKitReasonCode.h */; settings = {ATTRIBUTES = (Private, ); }; };
116117
44235FF72D76449300F4A6CB /* XPCExtras.h in Headers */ = {isa = PBXBuildFile; fileRef = 44235FF62D76448F00F4A6CB /* XPCExtras.h */; settings = {ATTRIBUTES = (Private, ); }; };
117118
4427C5AA21F6D6C300A612A4 /* ASCIICType.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4427C5A921F6D6C300A612A4 /* ASCIICType.cpp */; };
118119
4448265228D18CA600D916E8 /* VectorCF.h in Headers */ = {isa = PBXBuildFile; fileRef = 4448265128D18CA600D916E8 /* VectorCF.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -1379,6 +1380,7 @@
13791380
41036C754D844912316DC00E /* CanBorrow.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CanBorrow.h; sourceTree = "<group>"; };
13801381
413FE8F51F8D2EAB00F6D7D7 /* CallbackAggregator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CallbackAggregator.h; sourceTree = "<group>"; };
13811382
430B47871AAAAC1A001223DA /* StringCommon.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringCommon.h; sourceTree = "<group>"; };
1383+
4908EC45E9463252E413D60C /* WebKitReasonCode.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WebKitReasonCode.h; sourceTree = "<group>"; };
13821384
44235FF62D76448F00F4A6CB /* XPCExtras.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XPCExtras.h; sourceTree = "<group>"; };
13831385
4427C5A921F6D6C300A612A4 /* ASCIICType.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ASCIICType.cpp; sourceTree = "<group>"; };
13841386
4434F91B27948A65007E3E8A /* TollFreeBridging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TollFreeBridging.h; sourceTree = "<group>"; };
@@ -2280,6 +2282,7 @@
22802282
53FC70CF23FB950C005B1990 /* OSLogPrintStream.mm */,
22812283
449EEC7D2D93788B008E7C80 /* TypeCastsOSObject.h */,
22822284
37C7CC291EA40A73007BD956 /* WeakLinking.h */,
2285+
4908EC45E9463252E413D60C /* WebKitReasonCode.h */,
22832286
44235FF62D76448F00F4A6CB /* XPCExtras.h */,
22842287
46BD41C92EC723B2002ED70B /* XPCObjectPtr.h */,
22852288
);
@@ -4142,6 +4145,7 @@
41424145
DD3DC88A27A4BF8E007E5B61 /* WeakRandomNumber.h in Headers */,
41434146
4606FEE72B1E901800D704F1 /* WeakRef.h in Headers */,
41444147
DDF3073B27C086CD006A526F /* WebCoreThread.h in Headers */,
4148+
260A79A260060345C45191CE /* WebKitReasonCode.h in Headers */,
41454149
DD4901EA27B4748A00D7E50D /* WindowsExtras.h in Headers */,
41464150
DD3DC8AE27A4BF8E007E5B61 /* WordLock.h in Headers */,
41474151
DD3DC92327A4BF8E007E5B61 /* WorkerPool.h in Headers */,

‎Source/WTF/wtf/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ elseif (APPLE)
752752
darwin/OSLogPrintStream.h
753753
darwin/TypeCastsOSObject.h
754754
darwin/WeakLinking.h
755+
darwin/WebKitReasonCode.h
755756
darwin/XPCExtras.h
756757
darwin/XPCObjectPtr.h
757758

‎Source/WTF/wtf/PlatformCocoa.cmake‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ list(APPEND WTF_PUBLIC_HEADERS
9090
darwin/DarwinExtras.h
9191
darwin/OSLogPrintStream.h
9292
darwin/WeakLinking.h
93+
darwin/WebKitReasonCode.h
9394
darwin/XPCExtras.h
9495

9596
ios/WebCoreThread.h
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (C) 2026 Apple Inc. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
* 1. Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
*
13+
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21+
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
#pragma once
27+
28+
#include <cstdint>
29+
30+
namespace WTF {
31+
32+
// Reason codes WebKit passes in the OS_REASON_WEBKIT namespace to
33+
// terminate_with_reason() and abort_with_reason() (see ReasonSPI.h),
34+
// shared so that each code identifies one termination path in crash
35+
// reports. Values are recorded in crash reports, so never renumber an
36+
// existing code; only append.
37+
enum class WebKitReasonCode : uint64_t {
38+
WatchdogTimerFired,
39+
Invalidation,
40+
ConnectionKilled,
41+
MessageCheckKilled,
42+
ServiceWorkerFailedToTerminate,
43+
};
44+
45+
} // namespace WTF
46+
47+
using WTF::WebKitReasonCode;

‎Source/WebCore/workers/service/context/SWContextManager.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
#include <wtf/TZoneMallocInlines.h>
3636
#include <wtf/WTFProcess.h>
3737

38+
#if ASAN_ENABLED && PLATFORM(COCOA)
39+
#include <wtf/darwin/WebKitReasonCode.h>
40+
#include <wtf/spi/darwin/ReasonSPI.h>
41+
#endif
42+
3843
namespace WebCore {
3944

4045
WTF_MAKE_TZONE_ALLOCATED_IMPL(SWContextManager::ServiceWorkerTerminationRequest);
@@ -267,8 +272,14 @@ void SWContextManager::serviceWorkerFailedToTerminate(ServiceWorkerIdentifier se
267272
{
268273
UNUSED_PARAM(serviceWorkerIdentifier);
269274
RELEASE_LOG_ERROR(ServiceWorker, "Failed to terminate service worker with identifier %s, killing the service worker process", serviceWorkerIdentifier.loggingString().utf8());
275+
#if ASAN_ENABLED && PLATFORM(COCOA)
276+
// Abort with a reason instead of exiting so the crash report names this termination path
277+
// and captures the backtrace of the worker thread that failed to stop.
278+
abort_with_reason(OS_REASON_WEBKIT, std::to_underlying(WebKitReasonCode::ServiceWorkerFailedToTerminate), "SWContextManager::serviceWorkerFailedToTerminate", 0);
279+
#else
270280
ASSERT_NOT_REACHED();
271281
terminateProcess(EXIT_FAILURE);
282+
#endif
272283
}
273284

274285
SWContextManager::ServiceWorkerTerminationRequest::ServiceWorkerTerminationRequest(SWContextManager& manager, ServiceWorkerIdentifier serviceWorkerIdentifier, Seconds timeout)

‎Source/WebKit/Platform/IPC/cocoa/ConnectionCocoa.mm‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,8 +740,8 @@ static bool shouldLogIncomingMessageHandling()
740740
bool Connection::kill(std::optional<MessageName> invalidMessageName)
741741
{
742742
if (OSObjectPtr xpcConnection = m_xpcConnection) {
743-
auto reasonCode = invalidMessageName ? WebKit::ReasonCode::MessageCheckKilled : WebKit::ReasonCode::ConnectionKilled;
744-
terminateWithReason(xpcConnection, reasonCode, "Connection::kill", invalidMessageName);
743+
auto reasonCode = invalidMessageName ? WebKitReasonCode::MessageCheckKilled : WebKitReasonCode::ConnectionKilled;
744+
WebKit::terminateWithReason(xpcConnection, reasonCode, "Connection::kill", invalidMessageName);
745745
m_didRequestProcessTermination = true;
746746
return true;
747747
}

‎Source/WebKit/Platform/cocoa/XPCUtilities.h‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,15 @@
2626
#pragma once
2727

2828
#include <optional>
29+
#include <wtf/darwin/WebKitReasonCode.h>
2930
#include <wtf/darwin/XPCExtras.h>
3031

3132
namespace IPC { enum class MessageName : uint16_t; }
3233

3334
namespace WebKit {
3435

35-
enum class ReasonCode : uint64_t {
36-
WatchdogTimerFired,
37-
Invalidation,
38-
ConnectionKilled,
39-
MessageCheckKilled,
40-
};
41-
4236
#if !USE(EXTENSIONKIT_PROCESS_TERMINATION)
43-
void terminateWithReason(xpc_connection_t, ReasonCode, const char* reason, std::optional<IPC::MessageName> invalidMessageName = std::nullopt);
37+
void terminateWithReason(xpc_connection_t, WebKitReasonCode, const char* reason, std::optional<IPC::MessageName> invalidMessageName = std::nullopt);
4438
#endif
4539

4640
}

‎Source/WebKit/Platform/cocoa/XPCUtilities.mm‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@
3131
namespace WebKit {
3232

3333
#if !USE(EXTENSIONKIT_PROCESS_TERMINATION)
34-
void terminateWithReason(xpc_connection_t connection, ReasonCode reasonCode, const char* reason, std::optional<IPC::MessageName> invalidMessageName)
34+
void terminateWithReason(xpc_connection_t connection, WebKitReasonCode reasonCode, const char* reason, std::optional<IPC::MessageName> invalidMessageName)
3535
{
3636
if (!connection)
3737
return;
3838

3939
#if ASAN_ENABLED
4040
// Unlike xpc_connection_kill(), this leaves a crash report that run-webkit-tests can match.
4141
// Not done unconditionally because the SPI is blocked for embedders with a sandbox (Bug 224499).
42-
if (reasonCode == ReasonCode::MessageCheckKilled || reasonCode == ReasonCode::WatchdogTimerFired) {
43-
// Encode the ReasonCode in the low byte; for a message check, pack the failing
42+
if (reasonCode == WebKitReasonCode::MessageCheckKilled || reasonCode == WebKitReasonCode::WatchdogTimerFired) {
43+
// Encode the WebKitReasonCode in the low byte; for a message check, pack the failing
4444
// IPC message name above it so the crash report identifies which check failed.
4545
uint64_t code = std::to_underlying(reasonCode);
4646
if (invalidMessageName)

‎Source/WebKit/UIProcess/Cocoa/AuxiliaryProcessProxyCocoa.mm‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121

122122
Ref assertion = ProcessAndUIAssertion::create(processID(), reason, ProcessAssertionType::Background, environmentIdentifier());
123123
auto terminationHandler = [assertion = WTF::move(assertion), xpcConnection = WTF::move(xpcConnection)]() {
124-
terminateWithReason(xpcConnection.get(), ReasonCode::WatchdogTimerFired, reason);
124+
terminateWithReason(xpcConnection.get(), WebKitReasonCode::WatchdogTimerFired, reason);
125125
};
126126
#endif
127127

‎Source/WebKit/UIProcess/Launcher/cocoa/ProcessLauncherCocoa.mm‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ static ASCIILiteral serviceName(const ProcessLauncher::LaunchOptions& launchOpti
589589

590590
xpc_connection_cancel(m_xpcConnection.get());
591591
#if !USE(EXTENSIONKIT)
592-
terminateWithReason(m_xpcConnection.get(), WebKit::ReasonCode::Invalidation, "ProcessLauncher::platformInvalidate");
592+
terminateWithReason(m_xpcConnection.get(), WebKitReasonCode::Invalidation, "ProcessLauncher::platformInvalidate");
593593
#endif
594594
m_xpcConnection = nullptr;
595595
}

0 commit comments

Comments
 (0)