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  *
4  * Copyright 2021 Mozilla Foundation
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18 
19 #include "wasm/WasmTlsData.h"
20 
21 #include "js/HeapAPI.h"
22 #include "util/Memory.h"
23 #include "vm/JSContext.h"
24 
25 using namespace js;
26 using namespace js::wasm;
27 
CreateTlsData(uint32_t globalDataLength)28 UniqueTlsData wasm::CreateTlsData(uint32_t globalDataLength) {
29   void* allocatedBase = js_calloc(TlsDataAlign + offsetof(TlsData, globalArea) +
30                                   globalDataLength);
31   if (!allocatedBase) {
32     return nullptr;
33   }
34 
35   auto* tlsData = reinterpret_cast<TlsData*>(
36       AlignBytes(uintptr_t(allocatedBase), TlsDataAlign));
37   tlsData->allocatedBase = allocatedBase;
38 
39   return UniqueTlsData(tlsData);
40 }
41 
setInterrupt()42 void TlsData::setInterrupt() {
43   interrupt = true;
44   stackLimit = UINTPTR_MAX;
45 }
46 
isInterrupted() const47 bool TlsData::isInterrupted() const {
48   return interrupt || stackLimit == UINTPTR_MAX;
49 }
50 
resetInterrupt(JSContext * cx)51 void TlsData::resetInterrupt(JSContext* cx) {
52   interrupt = false;
53   stackLimit = cx->stackLimitForJitCode(JS::StackForUntrustedScript);
54 }
55