Skip to content

Commit 45f193a

Browse files
committed
[JSC] $.agent.start should throw instead of crashing when thread creation fails
https://bugs.webkit.org/show_bug.cgi?id=328940 rdar://187977643 Reviewed by Yusuke Suzuki. Starting enough long-lived agents exhausts the thread limit and hits the RELEASE_ASSERT in Thread::create. The live agent cap only applies in fuzzer mode. Add Thread::tryCreate and throw an OOM error from $.agent.start on failure. * Source/JavaScriptCore/jsc.cpp: (JSC_DEFINE_HOST_FUNCTION): * Source/WTF/wtf/Threading.cpp: (WTF::Thread::create): (WTF::Thread::tryCreate): * Source/WTF/wtf/Threading.h: Canonical link: https://commits.webkit.org/324842@main
1 parent 3ef14df commit 45f193a

3 files changed

Lines changed: 27 additions & 7 deletions

File tree

‎Source/JavaScriptCore/jsc.cpp‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2739,7 +2739,7 @@ JSC_DEFINE_HOST_FUNCTION(functionDollarAgentStart, (JSGlobalObject* globalObject
27392739
return JSValue::encode(throwException(globalObject, scope, createError(globalObject, "Too many live agents"_s)));
27402740
}
27412741

2742-
Thread::create(
2742+
RefPtr thread = Thread::tryCreate(
27432743
"JSC Agent"_s,
27442744
[sourceCode = WTF::move(sourceCode).isolatedCopy(), workerPath = WTF::move(workerPath).isolatedCopy(), &didStartLock, &didStartCondition, &didStart] () {
27452745
CommandLine commandLine(CommandLine::CommandLineForWorkers);
@@ -2764,8 +2764,13 @@ JSC_DEFINE_HOST_FUNCTION(functionDollarAgentStart, (JSGlobalObject* globalObject
27642764
exitProcess(EXIT_FAILURE);
27652765
});
27662766
numLiveAgents.fetch_sub(1);
2767-
})->detach();
2768-
2767+
});
2768+
if (!thread) [[unlikely]] {
2769+
numLiveAgents.fetch_sub(1);
2770+
return JSValue::encode(throwOutOfMemoryError(globalObject, scope, "Failed to create agent thread"_s));
2771+
}
2772+
thread->detach();
2773+
27692774
{
27702775
Locker locker { didStartLock };
27712776
while (!didStart)

‎Source/WTF/wtf/Threading.cpp‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ void Thread::entryPoint(NewThreadContext* newThreadContext)
232232
{
233233
Function<void()> function;
234234
{
235-
// Ref is already incremented by Thread::create.
235+
// Ref is already incremented by Thread::tryCreate.
236236
Ref context = adoptRef(*newThreadContext);
237237
// Block until our creating thread has completed any extra setup work, including establishing ThreadIdentifier.
238238
MutexLocker locker(context->mutex);
@@ -262,6 +262,13 @@ void Thread::entryPoint(NewThreadContext* newThreadContext)
262262
}
263263

264264
Ref<Thread> Thread::create(ASCIILiteral name, Function<void()>&& entryPoint, ThreadType threadType, QOS qos, SchedulingPolicy schedulingPolicy, StackAllocationSpecification stackSpec)
265+
{
266+
RefPtr thread = tryCreate(name, WTF::move(entryPoint), threadType, qos, schedulingPolicy, stackSpec);
267+
RELEASE_ASSERT(thread);
268+
return thread.releaseNonNull();
269+
}
270+
271+
RefPtr<Thread> Thread::tryCreate(ASCIILiteral name, Function<void()>&& entryPoint, ThreadType threadType, QOS qos, SchedulingPolicy schedulingPolicy, StackAllocationSpecification stackSpec)
265272
{
266273
WTF::initialize();
267274

@@ -277,7 +284,10 @@ Ref<Thread> Thread::create(ASCIILiteral name, Function<void()>&& entryPoint, Thr
277284
stackSpec = StackAllocationSpecification::RequestSize(maybeSize.value());
278285
}
279286
bool success = thread->establishHandle(context.get(), stackSpec, qos, schedulingPolicy);
280-
RELEASE_ASSERT(success);
287+
if (!success) {
288+
context->deref();
289+
return nullptr;
290+
}
281291

282292
#if HAVE(STACK_BOUNDS_FOR_NEW_THREAD)
283293
thread->m_stack = StackBounds::newThreadStackBounds(thread->m_handle);

‎Source/WTF/wtf/Threading.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,10 @@ class WTF_CAPABILITY("is current") Thread : public ThreadSafeRefCountedAndCanMak
132132
static dispatch_qos_class_t LEGACY_NODELETE dispatchQOSClass(QOS);
133133
#endif
134134

135-
// Returns nullptr if thread creation failed.
136135
// The thread name must be a literal since on some platforms it's passed in to the thread.
137136
WTF_EXPORT_PRIVATE static Ref<Thread> create(ASCIILiteral threadName, Function<void()>&&, ThreadType = ThreadType::Unknown, QOS = defaultQOS, SchedulingPolicy = defaultSchedulingPolicy, StackAllocationSpecification = { });
137+
// Returns nullptr if thread creation failed.
138+
WTF_EXPORT_PRIVATE static RefPtr<Thread> tryCreate(ASCIILiteral threadName, Function<void()>&&, ThreadType = ThreadType::Unknown, QOS = defaultQOS, SchedulingPolicy = defaultSchedulingPolicy, StackAllocationSpecification = { });
138139

139140
// Returns Thread object.
140141
static Thread& currentSingleton();
@@ -402,7 +403,11 @@ class WTF_CAPABILITY("is current") Thread : public ThreadSafeRefCountedAndCanMak
402403
WordLock m_mutex;
403404
StackBounds m_stack { StackBounds::emptyBounds() };
404405
ThreadSafeWeakHashSet<ThreadGroup> m_threadGroups;
405-
PlatformThreadHandle m_handle;
406+
#if OS(WINDOWS)
407+
PlatformThreadHandle m_handle { INVALID_HANDLE_VALUE };
408+
#else
409+
PlatformThreadHandle m_handle { };
410+
#endif
406411
const uint32_t m_uid;
407412
#if OS(WINDOWS)
408413
ThreadIdentifier m_id { 0 };

0 commit comments

Comments
 (0)