WTF::Thread should have the threads stack bounds.
authorutatane.tea@gmail.com <utatane.tea@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 6 Jul 2017 01:57:43 +0000 (01:57 +0000)
committerutatane.tea@gmail.com <utatane.tea@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 6 Jul 2017 01:57:43 +0000 (01:57 +0000)
commit9b10619b426acda908816e41fb0fb4367b727d1a
tree5863743226d352976d759656f8294ccba8655435
parent0b95e29036bba6384b1b551b57680abb598e6f5b
WTF::Thread should have the threads stack bounds.
https://bugs.webkit.org/show_bug.cgi?id=173975

Reviewed by Keith Miller.

Source/JavaScriptCore:

There is a site in JSC that try to walk another thread's stack.
Currently, stack bounds are stored in WTFThreadData which is located
in TLS. Thus, only the thread itself can access its own WTFThreadData.
We workaround this situation by holding StackBounds in MachineThread in JSC,
but StackBounds should be put in WTF::Thread instead.

This patch moves StackBounds from WTFThreadData to WTF::Thread. StackBounds
information is tightly coupled with Thread. Thus putting it in WTF::Thread
is natural choice.

* heap/MachineStackMarker.cpp:
(JSC::MachineThreads::MachineThread::MachineThread):
(JSC::MachineThreads::MachineThread::captureStack):
* heap/MachineStackMarker.h:
(JSC::MachineThreads::MachineThread::stackBase):
(JSC::MachineThreads::MachineThread::stackEnd):
* runtime/InitializeThreading.cpp:
(JSC::initializeThreading):
* runtime/VM.cpp:
(JSC::VM::VM):
(JSC::VM::updateStackLimits):
(JSC::VM::committedStackByteCount):
* runtime/VM.h:
(JSC::VM::isSafeToRecurse):
* runtime/VMEntryScope.cpp:
(JSC::VMEntryScope::VMEntryScope):
* runtime/VMInlines.h:
(JSC::VM::ensureStackCapacityFor):
* runtime/VMTraps.cpp:
* yarr/YarrPattern.cpp:
(JSC::Yarr::YarrPatternConstructor::isSafeToRecurse):

Source/WebCore:

When creating WebThread, we first allocate WebCore::ThreadGlobalData in UI thread
and share it with WebThread.
The problem is that WebCore::ThreadGlobalData has CachedResourceRequestInitiators.
It allocates AtomicString, which requires WTFThreadData.

Before this patch, it was OK because WTFThreadData does not touch threading related
things except for ThreadSpecific<>. However, after this patch, it touches
WTF::Thread::current() which requires WTF::initializeThreading().

In this patch, we call WTF::initializeThreading() before allocating WebCore::ThreadGlobalData.
And we also call AtomicString::init() before calling WebCore::ThreadGlobalData since
WebCore::ThreadGlobalData allocates AtomicString.

This fixes crashes in the iOS web threading environment (UIWebView).

* platform/ios/wak/WebCoreThread.mm:
(StartWebThread):

Source/WTF:

We move StackBounds from WTFThreadData to WTF::Thread.
One important thing is that we should make valid StackBounds
visible to Thread::create() caller. When the caller get
WTF::Thread from Thread::create(), this WTF::Thread should
have a valid StackBounds. But StackBounds information can be
retrived only in the WTF::Thread's thread itself.

We also clean up WTF::initializeThreading. StringImpl::empty()
is now statically initialized by using constexpr constructor.
Thus we do not need to call StringImpl::empty() explicitly here.
And WTF::initializeThreading() does not have any main thread
affinity right now in all the platforms. So we fix the comment
in Threading.h. Then, now, WTF::initializeThreading() is called
in UI thread when using Web thread in iOS.

* wtf/StackBounds.h:
(WTF::StackBounds::emptyBounds):
(WTF::StackBounds::StackBounds):
* wtf/StackStats.cpp:
(WTF::StackStats::PerThreadStats::PerThreadStats):
* wtf/Threading.cpp:
(WTF::threadEntryPoint):
(WTF::Thread::create):
(WTF::Thread::currentMayBeNull):
(WTF::Thread::initialize):
(WTF::initializeThreading):
* wtf/Threading.h:
(WTF::Thread::stack):
* wtf/ThreadingPthreads.cpp:
(WTF::Thread::initializeCurrentThreadEvenIfNonWTFCreated):
(WTF::Thread::current):
(WTF::initializeCurrentThreadEvenIfNonWTFCreated): Deleted.
(WTF::Thread::currentMayBeNull): Deleted.
* wtf/ThreadingWin.cpp:
(WTF::Thread::initializeCurrentThreadEvenIfNonWTFCreated):
(WTF::Thread::initializeCurrentThreadInternal):
(WTF::Thread::current):
* wtf/WTFThreadData.cpp:
(WTF::WTFThreadData::WTFThreadData):
* wtf/WTFThreadData.h:
(WTF::WTFThreadData::stack): Deleted.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@219176 268f45cc-cd09-0410-ab3c-d52691b4dbfc
21 files changed:
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/heap/MachineStackMarker.cpp
Source/JavaScriptCore/heap/MachineStackMarker.h
Source/JavaScriptCore/runtime/InitializeThreading.cpp
Source/JavaScriptCore/runtime/VM.cpp
Source/JavaScriptCore/runtime/VM.h
Source/JavaScriptCore/runtime/VMEntryScope.cpp
Source/JavaScriptCore/runtime/VMInlines.h
Source/JavaScriptCore/runtime/VMTraps.cpp
Source/JavaScriptCore/yarr/YarrPattern.cpp
Source/WTF/ChangeLog
Source/WTF/wtf/StackBounds.h
Source/WTF/wtf/StackStats.cpp
Source/WTF/wtf/Threading.cpp
Source/WTF/wtf/Threading.h
Source/WTF/wtf/ThreadingPthreads.cpp
Source/WTF/wtf/ThreadingWin.cpp
Source/WTF/wtf/WTFThreadData.cpp
Source/WTF/wtf/WTFThreadData.h
Source/WebCore/ChangeLog
Source/WebCore/platform/ios/wak/WebCoreThread.mm