1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "threading/ProtectedData.h"
8
9 #include "gc/Zone.h"
10 #include "vm/HelperThreads.h"
11 #include "vm/JSContext.h"
12
13 namespace js {
14
15 #ifdef JS_HAS_PROTECTED_DATA_CHECKS
16
17 /* static */ mozilla::Atomic<size_t, mozilla::SequentiallyConsistent>
18 AutoNoteSingleThreadedRegion::count(0);
19
20 template <AllowedHelperThread Helper>
OnHelperThread()21 static inline bool OnHelperThread() {
22 if (Helper == AllowedHelperThread::IonCompile ||
23 Helper == AllowedHelperThread::GCTaskOrIonCompile) {
24 if (CurrentThreadIsIonCompiling()) {
25 return true;
26 }
27 }
28
29 if (Helper == AllowedHelperThread::GCTask ||
30 Helper == AllowedHelperThread::GCTaskOrIonCompile) {
31 JSContext* cx = TlsContext.get();
32 if (cx->defaultFreeOp()->isCollecting()) {
33 return true;
34 }
35 }
36
37 return false;
38 }
39
check() const40 void CheckThreadLocal::check() const {
41 JSContext* cx = TlsContext.get();
42 MOZ_ASSERT(cx);
43 MOZ_ASSERT_IF(cx->isMainThreadContext(),
44 CurrentThreadCanAccessRuntime(cx->runtime()));
45 MOZ_ASSERT(id == ThreadId::ThisThreadId());
46 }
47
check() const48 void CheckContextLocal::check() const {
49 if (!cx_->isInitialized()) {
50 return;
51 }
52
53 JSContext* cx = TlsContext.get();
54 MOZ_ASSERT(cx);
55 MOZ_ASSERT_IF(cx->isMainThreadContext(),
56 CurrentThreadCanAccessRuntime(cx->runtime()));
57 MOZ_ASSERT(cx_ == cx);
58 }
59
60 template <AllowedHelperThread Helper>
check() const61 void CheckMainThread<Helper>::check() const {
62 if (OnHelperThread<Helper>()) {
63 return;
64 }
65
66 JSContext* cx = TlsContext.get();
67 MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime()));
68 }
69
70 template class CheckMainThread<AllowedHelperThread::None>;
71 template class CheckMainThread<AllowedHelperThread::GCTask>;
72 template class CheckMainThread<AllowedHelperThread::IonCompile>;
73
74 template <AllowedHelperThread Helper>
check() const75 void CheckZone<Helper>::check() const {
76 if (OnHelperThread<Helper>()) {
77 return;
78 }
79
80 if (zone->usedByHelperThread()) {
81 // This may only be accessed by the helper thread using this zone.
82 MOZ_ASSERT(zone->ownedByCurrentHelperThread());
83 } else {
84 // The main thread is permitted access to all zones. These accesses
85 // are threadsafe if the zone is not in use by a helper thread.
86 MOZ_ASSERT(CurrentThreadCanAccessRuntime(TlsContext.get()->runtime()));
87 }
88 }
89
90 template class CheckZone<AllowedHelperThread::None>;
91 template class CheckZone<AllowedHelperThread::GCTask>;
92 template class CheckZone<AllowedHelperThread::IonCompile>;
93 template class CheckZone<AllowedHelperThread::GCTaskOrIonCompile>;
94
95 template <GlobalLock Lock, AllowedHelperThread Helper>
check() const96 void CheckGlobalLock<Lock, Helper>::check() const {
97 if (OnHelperThread<Helper>()) {
98 return;
99 }
100
101 switch (Lock) {
102 case GlobalLock::GCLock:
103 TlsContext.get()->runtime()->gc.assertCurrentThreadHasLockedGC();
104 break;
105 case GlobalLock::ScriptDataLock:
106 TlsContext.get()->runtime()->assertCurrentThreadHasScriptDataAccess();
107 break;
108 case GlobalLock::HelperThreadLock:
109 gHelperThreadLock.assertOwnedByCurrentThread();
110 break;
111 }
112 }
113
114 template class CheckGlobalLock<GlobalLock::GCLock, AllowedHelperThread::None>;
115 template class CheckGlobalLock<GlobalLock::ScriptDataLock,
116 AllowedHelperThread::None>;
117 template class CheckGlobalLock<GlobalLock::HelperThreadLock,
118 AllowedHelperThread::None>;
119
120 template <AllowedHelperThread Helper>
check() const121 void CheckArenaListAccess<Helper>::check() const {
122 MOZ_ASSERT(zone);
123
124 if (OnHelperThread<Helper>()) {
125 return;
126 }
127
128 JSRuntime* rt = TlsContext.get()->runtime();
129 if (zone->isAtomsZone()) {
130 // The main thread can access the atoms arenas if it holds all the atoms
131 // table locks.
132 if (rt->currentThreadHasAtomsTableAccess()) {
133 return;
134 }
135
136 // Otherwise we must hold the GC lock if parallel parsing is running.
137 if (rt->isOffThreadParseRunning()) {
138 rt->gc.assertCurrentThreadHasLockedGC();
139 }
140 return;
141 }
142
143 CheckZone<AllowedHelperThread::None>::check();
144 }
145
146 template class CheckArenaListAccess<AllowedHelperThread::GCTask>;
147
148 #endif // JS_HAS_PROTECTED_DATA_CHECKS
149
150 } // namespace js
151