Debian Codegraph @

js/public/RealmOptions.h

raw
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 /*
8 * Options specified when creating a realm to determine its behavior, immutable
9 * options determining the behavior of an existing realm, and mutable options on
10 * an existing realm that may be changed when desired.
11 */
12
13 #ifndef js_RealmOptions_h
14 #define js_RealmOptions_h
15
16 #include "mozilla/Assertions.h" // MOZ_ASSERT
17 #include "mozilla/Maybe.h"
18
19 #include "jstypes.h" // JS_PUBLIC_API
20
21 #include "js/Class.h" // JSTraceOp
22 #include "js/RefCounted.h"
23
24 struct JS_PUBLIC_API JSContext;
25 class JS_PUBLIC_API JSObject;
26
27 namespace JS {
28
29 class JS_PUBLIC_API Compartment;
30 class JS_PUBLIC_API Realm;
31 class JS_PUBLIC_API Zone;
32
33 } // namespace JS
34
35 namespace JS {
36
37 /**
38 * Specification for which compartment/zone a newly created realm should use.
39 */
40 enum class CompartmentSpecifier {
41 // Create a new realm and compartment in the single runtime wide system
42 // zone. The meaning of this zone is left to the embedder.
43 NewCompartmentInSystemZone,
44
45 // Create a new realm and compartment in a particular existing zone.
46 NewCompartmentInExistingZone,
47
48 // Create a new zone/compartment.
49 NewCompartmentAndZone,
50
51 // Create a new realm in an existing compartment.
52 ExistingCompartment,
53 };
54
55 struct LocaleString : js::RefCounted<LocaleString> {
56 const char* chars_;
57
58 explicit LocaleString(const char* chars) : chars_(chars) {}
59
60 auto chars() const { return chars_; }
61 };
62
63 /**
64 * RealmCreationOptions specifies options relevant to creating a new realm, that
65 * are either immutable characteristics of that realm or that are discarded
66 * after the realm has been created.
67 *
68 * Access to these options on an existing realm is read-only: if you need
69 * particular selections, you must make them before you create the realm.
70 */
71 class JS_PUBLIC_API RealmCreationOptions {
72 public:
73 RealmCreationOptions() : comp_(nullptr) {}
74
75 JSTraceOp getTrace() const { return traceGlobal_; }
76 RealmCreationOptions& setTrace(JSTraceOp op) {
77 traceGlobal_ = op;
78 return *this;
79 }
80
81 Zone* zone() const {
82 MOZ_ASSERT(compSpec_ == CompartmentSpecifier::NewCompartmentInExistingZone);
83 return zone_;
84 }
85 Compartment* compartment() const {
86 MOZ_ASSERT(compSpec_ == CompartmentSpecifier::ExistingCompartment);
87 return comp_;
88 }
89 CompartmentSpecifier compartmentSpecifier() const { return compSpec_; }
90
91 // Set the compartment/zone to use for the realm. See CompartmentSpecifier
92 // above.
93 RealmCreationOptions& setNewCompartmentInSystemZone();
94 RealmCreationOptions& setNewCompartmentInExistingZone(JSObject* obj);
95 RealmCreationOptions& setNewCompartmentAndZone();
96 RealmCreationOptions& setExistingCompartment(JSObject* obj);
97 RealmCreationOptions& setExistingCompartment(Compartment* compartment);
98
99 // Certain compartments are implementation details of the embedding, and
100 // references to them should never leak out to script. This flag causes this
101 // realm to skip firing onNewGlobalObject and makes addDebuggee a no-op for
102 // this global.
103 //
104 // Debugger visibility is per-compartment, not per-realm (it's only practical
105 // to enforce visibility on compartment boundaries), so if a realm is being
106 // created in an extant compartment, its requested visibility must match that
107 // of the compartment.
108 bool invisibleToDebugger() const { return invisibleToDebugger_; }
109 RealmCreationOptions& setInvisibleToDebugger(bool flag) {
110 invisibleToDebugger_ = flag;
111 return *this;
112 }
113
114 // Determines whether this realm should preserve JIT code on non-shrinking
115 // GCs.
116 //
117 // Useful for embedders who know their code is relatively stable. See
118 // Bug 1068697 for motivation.
119 //
120 // This is a hint not a guarantee and can be overriden by other heuristics in
121 // the engine.
122 bool preserveJitCode() const { return preserveJitCode_; }
123 RealmCreationOptions& setPreserveJitCode(bool flag) {
124 preserveJitCode_ = flag;
125 return *this;
126 }
127
128 // Determines whether 1) the global Atomic property is defined and atomic
129 // operations are supported, and 2) whether shared-memory operations are
130 // supported.
131 bool getSharedMemoryAndAtomicsEnabled() const;
132 RealmCreationOptions& setSharedMemoryAndAtomicsEnabled(bool flag);
133
134 // Determines (if getSharedMemoryAndAtomicsEnabled() is true) whether the
135 // global SharedArrayBuffer property is defined. If the property is not
136 // defined, shared array buffer functionality can only be invoked if the
137 // host/embedding specifically acts to expose it.
138 //
139 // This option defaults to true: embeddings unable to tolerate a global
140 // SharedAraryBuffer property must opt out of it.
141 bool defineSharedArrayBufferConstructor() const {
142 return defineSharedArrayBufferConstructor_;
143 }
144 RealmCreationOptions& setDefineSharedArrayBufferConstructor(bool flag) {
145 defineSharedArrayBufferConstructor_ = flag;
146 return *this;
147 }
148
149 // Structured clone operations support the cloning of shared memory objects
150 // (SharedArrayBuffer or or a shared WASM Memory object) *optionally* -- at
151 // the discretion of the embedder code that performs the cloning. When a
152 // structured clone operation encounters a shared memory object and cloning
153 // shared memory objects has not been enabled, the clone fails and an
154 // error is thrown.
155 //
156 // In the web embedding context, shared memory object cloning is disabled
157 // either because
158 //
159 // 1) *no* way of supporting it is available (because the
160 // Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy HTTP
161 // headers are not respected to force the page into its own process), or
162 // 2) the aforementioned HTTP headers don't specify that the page should be
163 // opened in its own process.
164 //
165 // These two scenarios demand different error messages, and this option can be
166 // used to specify which scenario is in play.
167 //
168 // In the former case, if COOP/COEP support is not enabled, set this option to
169 // false. (This is the default.)
170 //
171 // In the latter case, if COOP/COEP weren't used to force this page into its
172 // own process, set this option to true.
173 //
174 // (Embeddings that are not the web and do not wish to support structured
175 // cloning of shared memory objects will get a "bad" web-centric error message
176 // no matter what. At present, SpiderMonkey does not offer a way for such
177 // embeddings to use an embedding-specific error message.)
178 bool getCoopAndCoepEnabled() const;
179 RealmCreationOptions& setCoopAndCoepEnabled(bool flag);
180
181 bool getToSourceEnabled() const { return toSource_; }
182 RealmCreationOptions& setToSourceEnabled(bool flag) {
183 toSource_ = flag;
184 return *this;
185 }
186
187 // This flag doesn't affect JS engine behavior. It is used by Gecko to
188 // mark whether content windows and workers are "Secure Context"s. See
189 // https://w3c.github.io/webappsec-secure-contexts/
190 // https://bugzilla.mozilla.org/show_bug.cgi?id=1162772#c34
191 bool secureContext() const { return secureContext_; }
192 RealmCreationOptions& setSecureContext(bool flag) {
193 secureContext_ = flag;
194 return *this;
195 }
196
197 // Non-standard option to freeze certain builtin constructors and seal their
198 // prototypes. Also defines these constructors on the global as non-writable
199 // and non-configurable.
200 bool freezeBuiltins() const { return freezeBuiltins_; }
201 RealmCreationOptions& setFreezeBuiltins(bool flag) {
202 freezeBuiltins_ = flag;
203 return *this;
204 }
205
206 // Force all date/time methods in JavaScript to use the UTC timezone for
207 // fingerprinting protection.
208 bool forceUTC() const { return forceUTC_; }
209 RealmCreationOptions& setForceUTC(bool flag) {
210 forceUTC_ = flag;
211 return *this;
212 }
213
214 RefPtr<LocaleString> locale() const { return locale_; }
215 RealmCreationOptions& setLocaleCopyZ(const char* locale);
216
217 // Always use the fdlibm implementation of math functions instead of the
218 // platform native libc implementations. Useful for fingerprinting protection
219 // and cross-platform consistency.
220 bool alwaysUseFdlibm() const { return alwaysUseFdlibm_; }
221 RealmCreationOptions& setAlwaysUseFdlibm(bool flag) {
222 alwaysUseFdlibm_ = flag;
223 return *this;
224 }
225
226 uint64_t profilerRealmID() const { return profilerRealmID_; }
227 RealmCreationOptions& setProfilerRealmID(uint64_t id) {
228 profilerRealmID_ = id;
229 return *this;
230 }
231
232 private:
233 JSTraceOp traceGlobal_ = nullptr;
234 CompartmentSpecifier compSpec_ = CompartmentSpecifier::NewCompartmentAndZone;
235 union {
236 Compartment* comp_;
237 Zone* zone_;
238 };
239 uint64_t profilerRealmID_ = 0;
240 RefPtr<LocaleString> locale_;
241 bool invisibleToDebugger_ = false;
242 bool preserveJitCode_ = false;
243 bool sharedMemoryAndAtomics_ = false;
244 bool defineSharedArrayBufferConstructor_ = true;
245 bool coopAndCoep_ = false;
246 bool toSource_ = false;
247
248 bool secureContext_ = false;
249 bool freezeBuiltins_ = false;
250 bool forceUTC_ = false;
251 bool alwaysUseFdlibm_ = false;
252 };
253
254 // This is a wrapper for mozilla::RTPCallerType, that can't easily
255 // be exposed to the JS engine for layering reasons.
256 struct RTPCallerTypeToken {
257 uint8_t value;
258 };
259
260 /**
261 * RealmBehaviors specifies behaviors of a realm that can be changed after the
262 * realm's been created.
263 */
264 class JS_PUBLIC_API RealmBehaviors {
265 public:
266 RealmBehaviors() = default;
267
268 // When a JS::ReduceMicrosecondTimePrecisionCallback callback is defined via
269 // JS::SetReduceMicrosecondTimePrecisionCallback, a JS::RTPCallerTypeToken (a
270 // wrapper for mozilla::RTPCallerType) needs to be set for every Realm.
271 mozilla::Maybe<RTPCallerTypeToken> reduceTimerPrecisionCallerType() const {
272 return rtpCallerType;
273 }
274 RealmBehaviors& setReduceTimerPrecisionCallerType(RTPCallerTypeToken type) {
275 rtpCallerType = mozilla::Some(type);
276 return *this;
277 }
278
279 // For certain globals, we know enough about the code that will run in them
280 // that we can discard script source entirely.
281 bool discardSource() const { return discardSource_; }
282 RealmBehaviors& setDiscardSource(bool flag) {
283 discardSource_ = flag;
284 return *this;
285 }
286
287 bool clampAndJitterTime() const { return clampAndJitterTime_; }
288 RealmBehaviors& setClampAndJitterTime(bool flag) {
289 clampAndJitterTime_ = flag;
290 return *this;
291 }
292
293 // A Realm can stop being "live" in all the ways that matter before its global
294 // is actually GCed. Consumers that tear down parts of a Realm or its global
295 // before that point should set isNonLive accordingly.
296 bool isNonLive() const { return isNonLive_; }
297 RealmBehaviors& setNonLive() {
298 isNonLive_ = true;
299 return *this;
300 }
301
302 private:
303 mozilla::Maybe<RTPCallerTypeToken> rtpCallerType;
304 bool discardSource_ = false;
305 bool clampAndJitterTime_ = true;
306 bool isNonLive_ = false;
307 };
308
309 /**
310 * RealmOptions specifies realm characteristics: both those that can't be
311 * changed on a realm once it's been created (RealmCreationOptions), and those
312 * that can be changed on an existing realm (RealmBehaviors).
313 */
314 class JS_PUBLIC_API RealmOptions {
315 public:
316 explicit RealmOptions() : creationOptions_(), behaviors_() {}
317
318 RealmOptions(const RealmCreationOptions& realmCreation,
319 const RealmBehaviors& realmBehaviors)
320 : creationOptions_(realmCreation), behaviors_(realmBehaviors) {}
321
322 // RealmCreationOptions specify fundamental realm characteristics that must
323 // be specified when the realm is created, that can't be changed after the
324 // realm is created.
325 RealmCreationOptions& creationOptions() { return creationOptions_; }
326 const RealmCreationOptions& creationOptions() const {
327 return creationOptions_;
328 }
329
330 // RealmBehaviors specify realm characteristics that can be changed after
331 // the realm is created.
332 RealmBehaviors& behaviors() { return behaviors_; }
333 const RealmBehaviors& behaviors() const { return behaviors_; }
334
335 private:
336 RealmCreationOptions creationOptions_;
337 RealmBehaviors behaviors_;
338 };
339
340 extern JS_PUBLIC_API const RealmCreationOptions& RealmCreationOptionsRef(
341 Realm* realm);
342
343 extern JS_PUBLIC_API const RealmCreationOptions& RealmCreationOptionsRef(
344 JSContext* cx);
345
346 extern JS_PUBLIC_API const RealmBehaviors& RealmBehaviorsRef(Realm* realm);
347
348 extern JS_PUBLIC_API const RealmBehaviors& RealmBehaviorsRef(JSContext* cx);
349
350 extern JS_PUBLIC_API void SetRealmNonLive(Realm* realm);
351
352 // This behaves like RealmBehaviors::setReduceTimerPrecisionCallerType, but
353 // can be used even after the Realm has already been created.
354 extern JS_PUBLIC_API void SetRealmReduceTimerPrecisionCallerType(
355 Realm* realm, RTPCallerTypeToken type);
356
357 } // namespace JS
358
359 #endif // js_RealmOptions_h