| 1 | /* |
| 2 | * Copyright (C) 2005-2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this library; see the file COPYING.LIB. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #pragma once |
| 22 | |
| 23 | #include <mutex> |
| 24 | #include <wtf/Assertions.h> |
| 25 | #include <wtf/ForbidHeapAllocation.h> |
| 26 | #include <wtf/Lock.h> |
| 27 | #include <wtf/Noncopyable.h> |
| 28 | #include <wtf/RefPtr.h> |
| 29 | #include <wtf/Threading.h> |
| 30 | #include <wtf/text/AtomStringTable.h> |
| 31 | |
| 32 | namespace JSC { |
| 33 | |
| 34 | // To make it safe to use JavaScript on multiple threads, it is |
| 35 | // important to lock before doing anything that allocates a |
| 36 | // JavaScript data structure or that interacts with shared state |
| 37 | // such as the protect count hash table. The simplest way to lock |
| 38 | // is to create a local JSLockHolder object in the scope where the lock |
| 39 | // must be held and pass it the context that requires protection. |
| 40 | // The lock is recursive so nesting is ok. The JSLock |
| 41 | // object also acts as a convenience short-hand for running important |
| 42 | // initialization routines. |
| 43 | |
| 44 | // To avoid deadlock, sometimes it is necessary to temporarily |
| 45 | // release the lock. Since it is recursive you actually have to |
| 46 | // release all locks held by your thread. This is safe to do if |
| 47 | // you are executing code that doesn't require the lock, and you |
| 48 | // reacquire the right number of locks at the end. You can do this |
| 49 | // by constructing a locally scoped JSLock::DropAllLocks object. The |
| 50 | // DropAllLocks object takes care to release the JSLock only if your |
| 51 | // thread acquired it to begin with. |
| 52 | |
| 53 | class CallFrame; |
| 54 | class VM; |
| 55 | class JSGlobalObject; |
| 56 | class JSLock; |
| 57 | |
| 58 | // This class is used to protect the initialization of the legacy single |
| 59 | // shared VM. |
| 60 | class GlobalJSLock { |
| 61 | WTF_MAKE_NONCOPYABLE(GlobalJSLock); |
| 62 | public: |
| 63 | JS_EXPORT_PRIVATE GlobalJSLock(); |
| 64 | JS_EXPORT_PRIVATE ~GlobalJSLock(); |
| 65 | private: |
| 66 | static Lock s_sharedInstanceMutex; |
| 67 | }; |
| 68 | |
| 69 | class JSLockHolder { |
| 70 | public: |
| 71 | JS_EXPORT_PRIVATE JSLockHolder(VM*); |
| 72 | JS_EXPORT_PRIVATE JSLockHolder(VM&); |
| 73 | JS_EXPORT_PRIVATE JSLockHolder(JSGlobalObject*); |
| 74 | |
| 75 | JS_EXPORT_PRIVATE ~JSLockHolder(); |
| 76 | |
| 77 | private: |
| 78 | RefPtr<VM> m_vm; |
| 79 | }; |
| 80 | |
| 81 | class JSLock : public ThreadSafeRefCounted<JSLock> { |
| 82 | WTF_MAKE_NONCOPYABLE(JSLock); |
| 83 | public: |
| 84 | JSLock(VM*); |
| 85 | JS_EXPORT_PRIVATE ~JSLock(); |
| 86 | |
| 87 | JS_EXPORT_PRIVATE void lock(); |
| 88 | JS_EXPORT_PRIVATE void unlock(); |
| 89 | |
| 90 | static void lock(JSGlobalObject*); |
| 91 | static void unlock(JSGlobalObject*); |
| 92 | static void lock(VM&); |
| 93 | static void unlock(VM&); |
| 94 | |
| 95 | VM* vm() { return m_vm; } |
| 96 | |
| 97 | Optional<RefPtr<Thread>> ownerThread() const |
| 98 | { |
| 99 | if (m_hasOwnerThread) |
| 100 | return m_ownerThread; |
| 101 | return WTF::nullopt; |
| 102 | } |
| 103 | bool currentThreadIsHoldingLock() { return m_hasOwnerThread && m_ownerThread.get() == &Thread::current(); } |
| 104 | |
| 105 | void willDestroyVM(VM*); |
| 106 | |
| 107 | class DropAllLocks { |
| 108 | WTF_MAKE_NONCOPYABLE(DropAllLocks); |
| 109 | public: |
| 110 | JS_EXPORT_PRIVATE DropAllLocks(JSGlobalObject*); |
| 111 | JS_EXPORT_PRIVATE DropAllLocks(VM*); |
| 112 | JS_EXPORT_PRIVATE DropAllLocks(VM&); |
| 113 | JS_EXPORT_PRIVATE ~DropAllLocks(); |
| 114 | |
| 115 | void setDropDepth(unsigned depth) { m_dropDepth = depth; } |
| 116 | unsigned dropDepth() const { return m_dropDepth; } |
| 117 | |
| 118 | private: |
| 119 | intptr_t m_droppedLockCount; |
| 120 | RefPtr<VM> m_vm; |
| 121 | unsigned m_dropDepth; |
| 122 | }; |
| 123 | |
| 124 | void makeWebThreadAware() |
| 125 | { |
| 126 | m_isWebThreadAware = true; |
| 127 | } |
| 128 | |
| 129 | bool isWebThreadAware() const { return m_isWebThreadAware; } |
| 130 | |
| 131 | private: |
| 132 | void lock(intptr_t lockCount); |
| 133 | void unlock(intptr_t unlockCount); |
| 134 | |
| 135 | void didAcquireLock(); |
| 136 | void willReleaseLock(); |
| 137 | |
| 138 | unsigned dropAllLocks(DropAllLocks*); |
| 139 | void grabAllLocks(DropAllLocks*, unsigned lockCount); |
| 140 | |
| 141 | Lock m_lock; |
| 142 | bool m_isWebThreadAware { false }; |
| 143 | // We cannot make m_ownerThread an optional (instead of pairing it with an explicit |
| 144 | // m_hasOwnerThread) because currentThreadIsHoldingLock() may be called from a |
| 145 | // different thread, and an optional is vulnerable to races. |
| 146 | // See https://bugs.webkit.org/show_bug.cgi?id=169042#c6 |
| 147 | bool m_hasOwnerThread { false }; |
| 148 | RefPtr<Thread> m_ownerThread; |
| 149 | intptr_t m_lockCount; |
| 150 | unsigned m_lockDropDepth; |
| 151 | bool m_shouldReleaseHeapAccess; |
| 152 | VM* m_vm; |
| 153 | AtomStringTable* m_entryAtomStringTable; |
| 154 | }; |
| 155 | |
| 156 | } // namespace |
| 157 | |