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:
4 * Copyright 2016 Mozilla Foundation
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #include "wasm/WasmRealm.h"
21 #include "vm/GlobalObject.h"
23 #include "wasm/WasmDebug.h"
24 #include "wasm/WasmInstance.h"
25 #include "wasm/WasmProcess.h"
27 #include "debugger/DebugAPI-inl.h"
28 #include "wasm/WasmInstance-inl.h"
33 wasm::Realm::Realm(JSRuntime
* rt
) : runtime_(rt
) {}
35 wasm::Realm::~Realm() { MOZ_ASSERT(instances_
.empty()); }
37 struct InstanceComparator
{
38 const Instance
& target
;
39 explicit InstanceComparator(const Instance
& target
) : target(target
) {}
41 int operator()(const Instance
* instance
) const {
42 if (instance
== &target
) {
46 // Instances can share code, so the segments can be equal (though they
47 // can't partially overlap). If the codeBases are equal, we sort by
48 // Instance address. Thus a Code may map to many instances.
50 // Compare by the first tier, always.
52 Tier instanceTier
= instance
->code().stableTier();
53 Tier targetTier
= target
.code().stableTier();
55 if (instance
->codeBase(instanceTier
) == target
.codeBase(targetTier
)) {
56 return instance
< &target
? -1 : 1;
59 return target
.codeBase(targetTier
) < instance
->codeBase(instanceTier
) ? -1
64 bool wasm::Realm::registerInstance(JSContext
* cx
,
65 Handle
<WasmInstanceObject
*> instanceObj
) {
66 MOZ_ASSERT(runtime_
== cx
->runtime());
68 Instance
& instance
= instanceObj
->instance();
69 MOZ_ASSERT(this == &instance
.realm()->wasm
);
71 instance
.ensureProfilingLabels(cx
->runtime()->geckoProfiler().enabled());
73 if (instance
.debugEnabled() &&
74 instance
.realm()->debuggerObservesAllExecution()) {
75 instance
.debug().ensureEnterFrameTrapsState(cx
, &instance
, true);
79 if (!instances_
.reserve(instances_
.length() + 1)) {
83 auto runtimeInstances
= cx
->runtime()->wasmInstances
.lock();
84 if (!runtimeInstances
->reserve(runtimeInstances
->length() + 1)) {
88 // To avoid implementing rollback, do not fail after mutations start.
90 InstanceComparator
cmp(instance
);
93 // The following section is not unsafe, but simulated OOM do not consider
94 // the fact that these insert calls are guarded by the previous reserve
96 AutoEnterOOMUnsafeRegion oomUnsafe
;
100 BinarySearchIf(instances_
, 0, instances_
.length(), cmp
, &index
));
101 MOZ_ALWAYS_TRUE(instances_
.insert(instances_
.begin() + index
, &instance
));
103 MOZ_ALWAYS_FALSE(BinarySearchIf(runtimeInstances
.get(), 0,
104 runtimeInstances
->length(), cmp
, &index
));
106 runtimeInstances
->insert(runtimeInstances
->begin() + index
, &instance
));
109 // Notify the debugger after wasmInstances is unlocked.
110 DebugAPI::onNewWasmInstance(cx
, instanceObj
);
114 void wasm::Realm::unregisterInstance(Instance
& instance
) {
115 InstanceComparator
cmp(instance
);
118 if (BinarySearchIf(instances_
, 0, instances_
.length(), cmp
, &index
)) {
119 instances_
.erase(instances_
.begin() + index
);
122 auto runtimeInstances
= runtime_
->wasmInstances
.lock();
123 if (BinarySearchIf(runtimeInstances
.get(), 0, runtimeInstances
->length(), cmp
,
125 runtimeInstances
->erase(runtimeInstances
->begin() + index
);
129 void wasm::Realm::ensureProfilingLabels(bool profilingEnabled
) {
130 for (Instance
* instance
: instances_
) {
131 instance
->ensureProfilingLabels(profilingEnabled
);
135 void wasm::Realm::addSizeOfExcludingThis(MallocSizeOf mallocSizeOf
,
136 size_t* realmTables
) {
137 *realmTables
+= instances_
.sizeOfExcludingThis(mallocSizeOf
);
140 void wasm::InterruptRunningCode(JSContext
* cx
) {
141 auto runtimeInstances
= cx
->runtime()->wasmInstances
.lock();
142 for (Instance
* instance
: runtimeInstances
.get()) {
143 instance
->setInterrupt();
147 void wasm::ResetInterruptState(JSContext
* cx
) {
148 auto runtimeInstances
= cx
->runtime()->wasmInstances
.lock();
149 for (Instance
* instance
: runtimeInstances
.get()) {
150 instance
->resetInterrupt(cx
);